Search in sources :

Example 96 with InterfaceAddress

use of java.net.InterfaceAddress in project cloudstack by apache.

the class NetUtils method getNetworkParams.

public static String[] getNetworkParams(final NetworkInterface nic) {
    List<InterfaceAddress> addrs = nic.getInterfaceAddresses();
    if (addrs == null || addrs.size() == 0) {
        return null;
    }
    // reverse addresses because it has reverse order as "ip addr show"
    Collections.reverse(addrs);
    InterfaceAddress addr = null;
    for (final InterfaceAddress iaddr : addrs) {
        final InetAddress inet = iaddr.getAddress();
        if (!inet.isLinkLocalAddress() && !inet.isLoopbackAddress() && !inet.isMulticastAddress() && inet.getAddress().length == 4) {
            addr = iaddr;
            break;
        }
    }
    if (addr == null) {
        return null;
    }
    final String[] result = new String[3];
    result[0] = addr.getAddress().getHostAddress();
    try {
        final byte[] mac = nic.getHardwareAddress();
        result[1] = byte2Mac(mac);
    } catch (final SocketException e) {
        s_logger.debug("Caught exception when trying to get the mac address ", e);
    }
    result[2] = prefix2Netmask(addr.getNetworkPrefixLength());
    return result;
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) InetAddress(java.net.InetAddress)

Example 97 with InterfaceAddress

use of java.net.InterfaceAddress in project graylog2-server by Graylog2.

the class OshiNetworkProbe method networkStats.

@Override
public NetworkStats networkStats() {
    String primaryInterface = "";
    final NetworkParams networkParams = service.getOs().getNetworkParams();
    final String defaultGateway = networkParams.getIpv4DefaultGateway();
    Map<String, NetworkStats.Interface> ifaces = new HashMap<>();
    for (NetworkIF it : service.getHal().getNetworkIFs()) {
        if (Strings.isNotBlank(defaultGateway)) {
            for (InterfaceAddress that : it.queryNetworkInterface().getInterfaceAddresses()) {
                try {
                    final IpSubnet ipSubnet = new IpSubnet(that.getAddress().getHostAddress() + "/" + that.getNetworkPrefixLength());
                    if (ipSubnet.contains(defaultGateway)) {
                        primaryInterface = it.getName();
                    }
                } catch (UnknownHostException e) {
                    LOG.warn("Couldn't find primary interface", e);
                }
            }
        }
        NetworkStats.Interface anInterface = NetworkStats.Interface.create(it.getName(), it.queryNetworkInterface().getInterfaceAddresses().stream().map(address -> address.getAddress().getHostAddress()).collect(Collectors.toSet()), it.getMacaddr(), it.getMTU(), NetworkStats.InterfaceStats.create(it.getPacketsRecv(), it.getInErrors(), it.getInDrops(), -1, -1, it.getPacketsSent(), it.getOutErrors(), -1, -1, -1, it.getCollisions(), it.getBytesRecv(), it.getBytesSent()));
        ifaces.putIfAbsent(anInterface.name(), anInterface);
    }
    final InternetProtocolStats stats = service.getOs().getInternetProtocolStats();
    final InternetProtocolStats.TcpStats ipv4Stats = stats.getTCPv4Stats();
    final InternetProtocolStats.TcpStats ipv6Stats = stats.getTCPv6Stats();
    final NetworkStats.TcpStats tcpStats = NetworkStats.TcpStats.create(ipv4Stats.getConnectionsActive() + ipv6Stats.getConnectionsActive(), ipv4Stats.getConnectionsPassive() + ipv6Stats.getConnectionsPassive(), ipv4Stats.getConnectionFailures() + ipv6Stats.getConnectionFailures(), ipv4Stats.getConnectionsReset() + ipv6Stats.getConnectionsReset(), ipv4Stats.getConnectionsEstablished() + ipv6Stats.getConnectionsEstablished(), ipv4Stats.getSegmentsReceived() + ipv6Stats.getSegmentsReceived(), ipv4Stats.getSegmentsSent() + ipv6Stats.getSegmentsSent(), ipv4Stats.getSegmentsRetransmitted() + ipv6Stats.getSegmentsRetransmitted(), ipv4Stats.getInErrors() + ipv6Stats.getInErrors(), ipv4Stats.getOutResets() + ipv6Stats.getOutResets());
    return NetworkStats.create(primaryInterface, ifaces, tcpStats);
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) InterfaceAddress(java.net.InterfaceAddress) NetworkIF(oshi.hardware.NetworkIF) IpSubnet(org.graylog2.utilities.IpSubnet) InternetProtocolStats(oshi.software.os.InternetProtocolStats) NetworkParams(oshi.software.os.NetworkParams)

Example 98 with InterfaceAddress

use of java.net.InterfaceAddress in project graylog2-server by Graylog2.

the class JmxNetworkProbe method buildInterface.

private NetworkStats.Interface buildInterface(NetworkInterface networkInterface) throws SocketException {
    final String macAddress = bytesToMacAddressString(networkInterface.getHardwareAddress());
    List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
    final Set<String> addresses = new HashSet<>(interfaceAddresses.size());
    for (InterfaceAddress interfaceAddress : interfaceAddresses) {
        addresses.add(interfaceAddress.getAddress().getHostAddress());
    }
    return NetworkStats.Interface.create(networkInterface.getName(), addresses, macAddress, networkInterface.getMTU(), null);
}
Also used : InterfaceAddress(java.net.InterfaceAddress) HashSet(java.util.HashSet)

Example 99 with InterfaceAddress

use of java.net.InterfaceAddress in project orWall by EthACKdotOrg.

the class NetworkHelper method getMask.

public static String getMask(String intf) {
    try {
        NetworkInterface ntwrk = NetworkInterface.getByName(intf);
        Iterator<InterfaceAddress> addrList = ntwrk.getInterfaceAddresses().iterator();
        while (addrList.hasNext()) {
            InterfaceAddress addr = addrList.next();
            InetAddress ip = addr.getAddress();
            if (ip instanceof Inet4Address) {
                String mask = ip.getHostAddress() + "/" + addr.getNetworkPrefixLength();
                return mask;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 100 with InterfaceAddress

use of java.net.InterfaceAddress in project fdroidclient by f-droid.

the class WifiStateChangeService method setIpInfoFromNetworkInterface.

/**
 * Search for known Wi-Fi, Hotspot, and local network interfaces and get
 * the IP Address info from it.  This is necessary because network
 * interfaces in Hotspot/AP mode do not show up in the regular
 * {@link WifiManager} queries, and also on
 * {@link android.os.Build.VERSION_CODES#LOLLIPOP Android 5.0} and newer,
 * {@link WifiManager#getDhcpInfo()} returns an invalid netmask.
 *
 * @see <a href="https://issuetracker.google.com/issues/37015180">netmask of WifiManager.getDhcpInfo() is always zero on Android 5.0</a>
 */
private void setIpInfoFromNetworkInterface() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        if (networkInterfaces == null) {
            return;
        }
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface netIf = networkInterfaces.nextElement();
            for (Enumeration<InetAddress> inetAddresses = netIf.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (inetAddress.isLoopbackAddress() || inetAddress instanceof Inet6Address) {
                    continue;
                }
                if (netIf.getDisplayName().contains("wlan0") || netIf.getDisplayName().contains("eth0") || netIf.getDisplayName().contains("ap0")) {
                    FDroidApp.ipAddressString = inetAddress.getHostAddress();
                    for (InterfaceAddress address : netIf.getInterfaceAddresses()) {
                        short networkPrefixLength = address.getNetworkPrefixLength();
                        if (networkPrefixLength > 32) {
                            // java.lang.IllegalArgumentException: Value [64] not in range [0,32]
                            continue;
                        }
                        try {
                            String cidr = String.format(Locale.ENGLISH, "%s/%d", FDroidApp.ipAddressString, networkPrefixLength);
                            FDroidApp.subnetInfo = new SubnetUtils(cidr).getInfo();
                            break;
                        } catch (IllegalArgumentException e) {
                            if (BuildConfig.DEBUG) {
                                e.printStackTrace();
                            } else {
                                Log.i(TAG, "Getting subnet failed: " + e.getLocalizedMessage());
                            }
                        }
                    }
                }
            }
        }
    } catch (SocketException e) {
        Log.e(TAG, "Could not get ip address", e);
    }
}
Also used : SocketException(java.net.SocketException) SubnetUtils(org.apache.commons.net.util.SubnetUtils) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)104 NetworkInterface (java.net.NetworkInterface)69 InetAddress (java.net.InetAddress)60 SocketException (java.net.SocketException)27 Inet4Address (java.net.Inet4Address)26 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)8 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 List (java.util.List)5 InetSocketAddress (java.net.InetSocketAddress)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3