Skip to content

Commit

Permalink
fix(android): return netmask of first IPv4 address (#634)
Browse files Browse the repository at this point in the history
fixes #633
  • Loading branch information
C-Otto authored Oct 26, 2022
1 parent 351da4d commit f740f45
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

import javax.annotation.Nonnull;
Expand All @@ -48,13 +50,18 @@ public abstract class ConnectivityReceiver {

private static String getSubnet(InetAddress inetAddress) throws SocketException {
NetworkInterface netAddress = NetworkInterface.getByInetAddress(inetAddress);
int mask =
0xffffffff
<< (32
- netAddress
.getInterfaceAddresses()
.get(1)
.getNetworkPrefixLength());
List<InterfaceAddress> addresses = netAddress.getInterfaceAddresses();

short networkPrefixLength = 0;
for (InterfaceAddress address : addresses) {
boolean isIpV4 = address.getAddress().getAddress().length == 4;
if (isIpV4) {
networkPrefixLength = address.getNetworkPrefixLength();
break;
}
}

int mask = 0xffffffff << (32 - networkPrefixLength);
return String.format(
Locale.US,
"%d.%d.%d.%d",
Expand Down Expand Up @@ -145,7 +152,7 @@ protected WritableMap createConnectivityEventMap(@Nullable final String requeste
// Add the connection state information
boolean isConnected =
!mConnectionType.equals(ConnectionType.NONE)
&& !mConnectionType.equals(ConnectionType.UNKNOWN);
&& !mConnectionType.equals(ConnectionType.UNKNOWN);
event.putBoolean("isConnected", isConnected);

// Add the internet reachable information
Expand Down

0 comments on commit f740f45

Please sign in to comment.