Skip to content

Commit

Permalink
Use IP address library
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed May 11, 2022
1 parent d26ee2d commit cb2aea7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
1 change: 1 addition & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ FairEmail uses:
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/).
* [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt).
* [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE).
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ dependencies {
def rxjava2_version = "2.2.21"
def svg_version = "1.4"
def compress_version = "1.21"
def ipaddress_version = "5.3.4"
def canary_version = "2.8.1"

// https://developer.android.com/jetpack/androidx/releases/startup
Expand Down Expand Up @@ -579,6 +580,10 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.commons/commons-compress
implementation "org.apache.commons:commons-compress:$compress_version"

// https://seancfoley.github.io/IPAddress/
// https://mvnrepository.com/artifact/com.github.seancfoley/ipaddress
implementation "com.github.seancfoley:ipaddress:$ipaddress_version"

// https://github.com/square/leakcanary
// https://square.github.io/leakcanary/getting_started/
// https://mvnrepository.com/artifact/com.squareup.leakcanary/leakcanary-android
Expand Down
1 change: 1 addition & 0 deletions app/src/main/assets/ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ FairEmail uses:
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/).
* [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt).
* [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE).
21 changes: 4 additions & 17 deletions app/src/main/java/eu/faircode/email/ConnectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import inet.ipaddr.IPAddressString;

public class ConnectionHelper {
static final List<String> PREF_NETWORK = Collections.unmodifiableList(Arrays.asList(
"metered", "roaming", "rlah", "require_validated", "vpn_only" // update network state
Expand Down Expand Up @@ -558,23 +560,8 @@ static boolean isLocalAddress(String host) {

static boolean inSubnet(final String ip, final String net, final int prefix) {
try {
byte[] _ip = InetAddress.getByName(ip).getAddress();
byte[] _net = InetAddress.getByName(net).getAddress();

if (_ip.length != _net.length)
return false;

int i = 0;
int p = prefix;
while (p >= 8) {
if (_ip[i] != _net[i])
return false;
++i;
p -= 8;
}

int m = (0xFF00 >> p) & 0xFF;
return (_ip[i] & m) == (_net[i] & m);
return new IPAddressString(net + "/" + prefix).getAddress()
.contains(new IPAddressString(ip).getAddress());
} catch (Throwable ex) {
Log.w(ex);
return false;
Expand Down

0 comments on commit cb2aea7

Please sign in to comment.