Search in sources :

Example 81 with Inet4Address

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;
}
Also used : Packet(org.pcap4j.packet.Packet) TcpPacket(org.pcap4j.packet.TcpPacket) IpV4Packet(org.pcap4j.packet.IpV4Packet) UdpPacket(org.pcap4j.packet.UdpPacket) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException)

Example 82 with Inet4Address

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;
}
Also used : Inet4Address(java.net.Inet4Address) Enumeration(java.util.Enumeration) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 83 with Inet4Address

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;
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 84 with Inet4Address

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) {
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress)

Example 85 with Inet4Address

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;
}
Also used : Inet4Address(java.net.Inet4Address)

Aggregations

Inet4Address (java.net.Inet4Address)184 InetAddress (java.net.InetAddress)85 Inet6Address (java.net.Inet6Address)45 NetworkInterface (java.net.NetworkInterface)39 UnknownHostException (java.net.UnknownHostException)28 LinkAddress (android.net.LinkAddress)24 SocketException (java.net.SocketException)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)19 InterfaceAddress (java.net.InterfaceAddress)17 ByteBuffer (java.nio.ByteBuffer)17 RouteInfo (android.net.RouteInfo)12 LinkProperties (android.net.LinkProperties)7 InetSocketAddress (java.net.InetSocketAddress)6 Test (org.junit.Test)6 WifiDisplay (android.hardware.display.WifiDisplay)5 WifiDisplaySessionInfo (android.hardware.display.WifiDisplaySessionInfo)5 DhcpResults (android.net.DhcpResults)5 IpConfiguration (android.net.IpConfiguration)5 IpAssignment (android.net.IpConfiguration.IpAssignment)5