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]);
}
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;
}
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;
}
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));
}
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));
}
Aggregations