Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): return netmask of first IPv4 address #634

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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