use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.
the class LinkAddressTest method testConstructors.
public void testConstructors() throws SocketException {
LinkAddress address;
// Valid addresses work as expected.
address = new LinkAddress(V4_ADDRESS, 25);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(25, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
address = new LinkAddress(V6_ADDRESS, 127);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(127, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
// Nonsensical flags/scopes or combinations thereof are acceptable.
address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(64, address.getPrefixLength());
assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
assertEquals(RT_SCOPE_LINK, address.getScope());
address = new LinkAddress(V4 + "/23", 123, 456);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(23, address.getPrefixLength());
assertEquals(123, address.getFlags());
assertEquals(456, address.getScope());
// InterfaceAddress doesn't have a constructor. Fetch some from an interface.
List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
// We expect to find 127.0.0.1/8 and ::1/128, in any order.
LinkAddress ipv4Loopback, ipv6Loopback;
assertEquals(2, addrs.size());
if (addrs.get(0).getAddress() instanceof Inet4Address) {
ipv4Loopback = new LinkAddress(addrs.get(0));
ipv6Loopback = new LinkAddress(addrs.get(1));
} else {
ipv4Loopback = new LinkAddress(addrs.get(1));
ipv6Loopback = new LinkAddress(addrs.get(0));
}
assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
assertEquals(8, ipv4Loopback.getPrefixLength());
assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
assertEquals(128, ipv6Loopback.getPrefixLength());
// Null addresses are rejected.
try {
address = new LinkAddress(null, 24);
fail("Null InetAddress should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("Null string should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((InterfaceAddress) null);
fail("Null string should cause NullPointerException");
} catch (NullPointerException expected) {
}
// Invalid prefix lengths are rejected.
try {
address = new LinkAddress(V4_ADDRESS, -1);
fail("Negative IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, -1);
fail("Negative IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4_ADDRESS, 33);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
// Multicast addresses are rejected.
try {
address = new LinkAddress("224.0.0.2/32");
fail("IPv4 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress("ff02::1/128");
fail("IPv6 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
use of java.net.Inet4Address in project jdk8u_jdk by JetBrains.
the class ListenAddress method main.
public static void main(String[] args) throws Exception {
ListeningConnector connector = (ListeningConnector) findConnector("com.sun.jdi.SocketListen");
// check wildcard address
check(connector, (InetAddress) null);
// iterate over all IPv4 addresses and check that binding to
// that address results in the correct result from startListening(Map)
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nifs.nextElement();
Enumeration addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = (InetAddress) addrs.nextElement();
// JPDA implementation only currently supports IPv4
if (!(addr instanceof Inet4Address)) {
continue;
}
check(connector, addr);
}
}
if (failures > 0) {
throw new RuntimeException(failures + " test(s) failed - see output for details.");
}
}
use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.
the class KeepalivePacketData method nattKeepalivePacket.
/**
* Creates an IPsec NAT-T keepalive packet with the specified parameters.
*/
public static KeepalivePacketData nattKeepalivePacket(InetAddress srcAddress, int srcPort, InetAddress dstAddress, int dstPort) throws InvalidPacketException {
if (!(srcAddress instanceof Inet4Address) || !(dstAddress instanceof Inet4Address)) {
throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
}
if (dstPort != NATT_PORT) {
throw new InvalidPacketException(ERROR_INVALID_PORT);
}
int length = IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH + 1;
ByteBuffer buf = ByteBuffer.allocate(length);
buf.order(ByteOrder.BIG_ENDIAN);
// IP version and TOS
buf.putShort((short) 0x4500);
buf.putShort((short) length);
// ID, flags, offset
buf.putInt(0);
// TTL
buf.put((byte) 64);
buf.put((byte) OsConstants.IPPROTO_UDP);
int ipChecksumOffset = buf.position();
// IP checksum
buf.putShort((short) 0);
buf.put(srcAddress.getAddress());
buf.put(dstAddress.getAddress());
buf.putShort((short) srcPort);
buf.putShort((short) dstPort);
// UDP length
buf.putShort((short) (length - 20));
int udpChecksumOffset = buf.position();
// UDP checksum
buf.putShort((short) 0);
// NAT-T keepalive
buf.put((byte) 0xff);
buf.putShort(ipChecksumOffset, IpUtils.ipChecksum(buf, 0));
buf.putShort(udpChecksumOffset, IpUtils.udpChecksum(buf, 0, IPV4_HEADER_LENGTH));
return new KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, buf.array());
}
use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.
the class WifiDisplayController method getSessionInfo.
private WifiDisplaySessionInfo getSessionInfo(WifiP2pGroup info, int session) {
if (info == null) {
return null;
}
Inet4Address addr = getInterfaceAddress(info);
WifiDisplaySessionInfo sessionInfo = new WifiDisplaySessionInfo(!info.getOwner().deviceAddress.equals(mThisDevice.deviceAddress), session, info.getOwner().deviceAddress + " " + info.getNetworkName(), info.getPassphrase(), (addr != null) ? addr.getHostAddress() : "");
if (DEBUG) {
Slog.d(TAG, sessionInfo.toString());
}
return sessionInfo;
}
use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.
the class WifiDisplayController method getInterfaceAddress.
private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
NetworkInterface iface;
try {
iface = NetworkInterface.getByName(info.getInterface());
} catch (SocketException ex) {
Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface(), ex);
return null;
}
Enumeration<InetAddress> addrs = iface.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
if (addr instanceof Inet4Address) {
return (Inet4Address) addr;
}
}
Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface() + " because it had no IPv4 addresses.");
return null;
}
Aggregations