Search in sources :

Example 31 with InterfaceAddress

use of java.net.InterfaceAddress in project cosmic by MissionCriticalCloud.

the class NetUtils method getLocalCidrs.

public static String[] getLocalCidrs() {
    final String defaultHostIp = getDefaultHostIp();
    final List<String> cidrList = new ArrayList<>();
    try {
        for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
                for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
                    final InetAddress addr = address.getAddress();
                    final int prefixLength = address.getNetworkPrefixLength();
                    if (prefixLength < MAX_CIDR && prefixLength > 0) {
                        final String ip = addr.getHostAddress();
                        if (ip.equalsIgnoreCase(defaultHostIp)) {
                            cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
                        }
                    }
                }
            }
        }
    } catch (final SocketException e) {
        s_logger.warn("UnknownHostException in getLocalCidrs().", e);
    }
    return cidrList.toArray(new String[0]);
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 32 with InterfaceAddress

use of java.net.InterfaceAddress in project cosmic by MissionCriticalCloud.

the class NetUtils method getNetworkParams.

public static String[] getNetworkParams(final NetworkInterface nic) {
    final List<InterfaceAddress> addrs = nic.getInterfaceAddresses();
    if (addrs == null || addrs.size() == 0) {
        return null;
    }
    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 33 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class TestUtils method createInterfaceAddress.

@Nonnull
public static InterfaceAddress createInterfaceAddress(final InetAddress address, final String broadcast, final int maskLength) throws ReflectiveOperationException, UnknownHostException {
    final InterfaceAddress interfaceAddress = Reflection.getConstructor(InterfaceAddress.class).newInstance();
    Reflection.setFieldValue(interfaceAddress, "address", address);
    Reflection.setFieldValue(interfaceAddress, "broadcast", InetAddress.getByName(broadcast));
    Reflection.setFieldValue(interfaceAddress, "maskLength", (short) maskLength);
    return interfaceAddress;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) Nonnull(javax.annotation.Nonnull)

Example 34 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpNotifyReceiverTest method onReceive_LocationとSourceが不一致のメッセージは無視する.

@Test
public void onReceive_LocationとSourceが不一致のメッセージは無視する() throws Exception {
    final SsdpNotifyReceiver receiver = spy(new SsdpNotifyReceiver(Address.IP_V4, NetworkUtils.getAvailableInet4Interfaces().get(0)));
    final InterfaceAddress address = TestUtils.createInterfaceAddress("192.0.2.1", "255.255.0.0", 24);
    doReturn(address).when(receiver).getInterfaceAddress();
    final NotifyListener listener = mock(NotifyListener.class);
    receiver.setNotifyListener(listener);
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    receiver.onReceive(InetAddress.getByName("192.0.2.3"), data, data.length);
    verify(listener, never()).onReceiveNotify(ArgumentMatchers.any(SsdpRequest.class));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

Example 35 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpMessageDelegateTest method getScopeId_インターフェースに紐付かないIPv6なら0.

@Test
public void getScopeId_インターフェースに紐付かないIPv6なら0() throws Exception {
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    final HttpRequest request = new HttpRequest().readData(new ByteArrayInputStream(data, 0, data.length));
    final InterfaceAddress ifa = TestUtils.createInterfaceAddress("fe80::a831:801b:8dc6:421f", "255.255.255.0", 0);
    final SsdpMessageDelegate delegate = new SsdpMessageDelegate(request, ifa);
    assertThat(delegate.getScopeId(), is(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InterfaceAddress(java.net.InterfaceAddress) Test(org.junit.Test)

Aggregations

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