use of java.net.Inet4Address in project trex-stateless-gui by cisco-system-traffic-generator.
the class PacketUpdater method updateSrcAddress.
/**
* Update source IP address
*/
private Packet updateSrcAddress(Packet packet) {
try {
if (importedProperties.isSourceEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getSrcAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress().equals(defaultSrcAddress)) {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating source IP", ex);
}
return packet;
}
use of java.net.Inet4Address in project ddf by codice.
the class InetAddressUtil method getFirstNonLoopbackAddress.
/**
* @param preferIpv4
* @param preferIPv6
* @return
* @throws SocketException
*/
public static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
Enumeration en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface i = (NetworkInterface) en.nextElement();
Enumeration en2 = i.getInetAddresses();
while (en2.hasMoreElements()) {
InetAddress addr = (InetAddress) en2.nextElement();
if (!addr.isLoopbackAddress()) {
if (addr instanceof Inet4Address) {
if (preferIPv6) {
continue;
}
return addr;
}
if (addr instanceof Inet6Address) {
if (preferIpv4) {
continue;
}
return addr;
}
}
}
}
return null;
}
use of java.net.Inet4Address in project android_frameworks_base by crdroidandroid.
the class CommonTimeUtils method transactSetSockaddr.
public int transactSetSockaddr(int method_code, InetSocketAddress addr) {
android.os.Parcel data = android.os.Parcel.obtain();
android.os.Parcel reply = android.os.Parcel.obtain();
int ret_val = ERROR;
try {
data.writeInterfaceToken(mInterfaceDesc);
if (null == addr) {
data.writeInt(0);
} else {
data.writeInt(1);
final InetAddress a = addr.getAddress();
final byte[] b = a.getAddress();
final int p = addr.getPort();
if (a instanceof Inet4Address) {
int v4addr = (((int) b[0] & 0xFF) << 24) | (((int) b[1] & 0xFF) << 16) | (((int) b[2] & 0xFF) << 8) | ((int) b[3] & 0xFF);
data.writeInt(AF_INET);
data.writeInt(v4addr);
data.writeInt(p);
} else if (a instanceof Inet6Address) {
int i;
Inet6Address v6 = (Inet6Address) a;
data.writeInt(AF_INET6);
for (i = 0; i < 4; ++i) {
int aword = (((int) b[(i * 4) + 0] & 0xFF) << 24) | (((int) b[(i * 4) + 1] & 0xFF) << 16) | (((int) b[(i * 4) + 2] & 0xFF) << 8) | ((int) b[(i * 4) + 3] & 0xFF);
data.writeInt(aword);
}
data.writeInt(p);
// flow info
data.writeInt(0);
data.writeInt(v6.getScopeId());
} else {
return ERROR_BAD_VALUE;
}
}
mRemote.transact(method_code, data, reply, 0);
ret_val = reply.readInt();
} catch (RemoteException e) {
ret_val = ERROR_DEAD_OBJECT;
} finally {
reply.recycle();
data.recycle();
}
return ret_val;
}
use of java.net.Inet4Address in project android_frameworks_base by crdroidandroid.
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 android_frameworks_base by crdroidandroid.
the class DhcpResults method setIpAddress.
// Utils for jni population - false on success
// Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon.
public boolean setIpAddress(String addrString, int prefixLength) {
try {
Inet4Address addr = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
ipAddress = new LinkAddress(addr, prefixLength);
} catch (IllegalArgumentException | ClassCastException e) {
Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength);
return true;
}
return false;
}
Aggregations