Search in sources :

Example 76 with Inet6Address

use of java.net.Inet6Address in project LogHub by fbacchella.

the class AbstractNameResolver method processMessage.

@Override
public boolean processMessage(Event event, String field, String destination) throws ProcessorException {
    Object addr = event.get(field);
    InetAddress ipaddr = null;
    String toresolv = null;
    // If a string was given, convert it to a Inet?Address
    if (addr instanceof String) {
        try {
            ipaddr = Helpers.parseIpAddres((String) addr);
        } catch (UnknownHostException e) {
            throw event.buildException("invalid IP address '" + addr + "': " + e.getMessage());
        }
    } else if (addr instanceof InetAddress) {
        ipaddr = (InetAddress) addr;
    }
    if (ipaddr instanceof Inet4Address) {
        Inet4Address ipv4 = (Inet4Address) ipaddr;
        byte[] parts = ipv4.getAddress();
        // the & 0xFF is needed because bytes are signed bytes
        toresolv = String.format("%d.%d.%d.%d.in-addr.arpa.", parts[3] & 0xFF, parts[2] & 0xFF, parts[1] & 0xFF, parts[0] & 0xFF);
    } else if (ipaddr instanceof Inet6Address) {
        Inet6Address ipv6 = (Inet6Address) ipaddr;
        byte[] parts = ipv6.getAddress();
        StringBuilder buffer = new StringBuilder();
        for (int i = parts.length - 1; i >= 0; i--) {
            buffer.append(String.format("%x.%x.", parts[i] & 0x0F, (parts[i] & 0xF0) >> 4));
        }
        buffer.append("ip6.arpa");
        toresolv = buffer.toString();
    }
    if (toresolv != null) {
        // If a query was build, use it
        return resolve(event, toresolv, destination);
    } else if (addr instanceof String) {
        // if addr was a String, it's used a a hostname
        event.put(destination, addr);
        return true;
    } else {
        return false;
    }
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 77 with Inet6Address

use of java.net.Inet6Address in project jbossws-cxf by jbossws.

the class JBWS1178TestCaseForked method testHostAddress.

@Test
@RunAsClient
public void testHostAddress() throws Exception {
    InetAddress inetAddr = InetAddress.getByName(getServerHost());
    String hostAddress = inetAddr instanceof Inet6Address ? "[" + inetAddr.getHostAddress() + "]" : inetAddr.getHostAddress();
    URL wsdlURL = new URL("http://" + hostAddress + ":" + getServerPort() + "/jaxws-jbws1178/testpattern?wsdl");
    QName serviceName = new QName("http://org.jboss.ws/jbws1178", "EndpointService");
    Service service = Service.create(wsdlURL, serviceName);
    Endpoint port = service.getPort(Endpoint.class);
    Map<String, Object> reqCtx = ((BindingProvider) port).getRequestContext();
    URL epURL = new URL((String) reqCtx.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY));
    assertEqualsIpv6FormatAware(wsdlURL.getHost(), epURL.getHost());
}
Also used : QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) Inet6Address(java.net.Inet6Address) BindingProvider(javax.xml.ws.BindingProvider) InetAddress(java.net.InetAddress) URL(java.net.URL) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Test(org.junit.Test) JBossWSTest(org.jboss.wsf.test.JBossWSTest)

Example 78 with Inet6Address

use of java.net.Inet6Address in project network-monitor by caarmen.

the class NetworkInterfaceDataSource method getContentValues.

@Override
public ContentValues getContentValues() {
    Log.v(TAG, "getContentValues");
    ContentValues result = new ContentValues(1);
    try {
        // It's possible for the device to have multiple interfaces up at a given time.
        // This can happen on "normal" phones when switching between WiFi and 3G,
        // or on some devices which have multiple interfaces up all the time.
        // We'll save the name and addresses of all interfaces.  If there happen to
        // be multiple ones, we'll return a delimited list.
        // In most cases, we should have one interface name, one IP v4 address, and one IP v6 address.
        List<String> interfaceNames = new ArrayList<>();
        List<String> ipv4Addresses = new ArrayList<>();
        List<String> ipv6Addresses = new ArrayList<>();
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (isValidNetworkInterface(networkInterface)) {
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                if (inetAddresses.hasMoreElements())
                    interfaceNames.add(networkInterface.getName());
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (inetAddress instanceof Inet6Address)
                        ipv6Addresses.add(inetAddress.getHostAddress());
                    else
                        ipv4Addresses.add(inetAddress.getHostAddress());
                }
            }
        }
        result.put(NetMonColumns.NETWORK_INTERFACE, TextUtils.join(";", interfaceNames));
        result.put(NetMonColumns.IPV4_ADDRESS, TextUtils.join(";", ipv4Addresses));
        result.put(NetMonColumns.IPV6_ADDRESS, TextUtils.join(";", ipv6Addresses));
    } catch (SocketException e) {
        Log.e(TAG, "getContentValues Could not retrieve NetworkInterfaces:  " + e.getMessage(), e);
    }
    return result;
}
Also used : ContentValues(android.content.ContentValues) SocketException(java.net.SocketException) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 79 with Inet6Address

use of java.net.Inet6Address in project BiglyBT by BiglySoftware.

the class DHTTransportUDPImpl method writeTransfer.

@Override
public void writeTransfer(DHTTransportProgressListener listener, DHTTransportContact target, byte[] handler_key, byte[] key, byte[] data, long timeout) throws DHTTransportException {
    InetAddress ia = target.getAddress().getAddress();
    if ((ia instanceof Inet4Address && v6) || (ia instanceof Inet6Address && !v6)) {
        throw (new DHTTransportException("Incompatible address"));
    }
    xfer_handler.writeTransfer(listener, target, handler_key, key, data, timeout);
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 80 with Inet6Address

use of java.net.Inet6Address in project BiglyBT by BiglySoftware.

the class DHTTransportUDPImpl method externalAddressChange.

protected void externalAddressChange(final DHTTransportUDPContactImpl reporter, final InetSocketAddress new_address, boolean force) throws DHTTransportException {
    /*
			 * A node has reported that our external address and the one he's seen a
			 * message coming from differ. Natural explanations are along the lines of
			 * 1) my address is dynamically allocated by my ISP and it has changed
			 * 2) I have multiple network interfaces
			 * 3) there's some kind of proxy going on
			 * 4) this is a DOS attempting to stuff me up
			 *
			 * We assume that our address won't change more frequently than once every
			 * 5 minutes
			 * We assume that if we can successfully obtain an external address by
			 * using the above explicit check then this is accurate
			 * Only in the case where the above check fails do we believe the address
			 * that we've been told about
			 */
    InetAddress ia = new_address.getAddress();
    if (ia == null) {
        Debug.out("reported new external address '" + new_address + "' is unresolved");
        throw (new DHTTransportException("Address '" + new_address + "' is unresolved"));
    }
    if ((ia instanceof Inet4Address && v6) || (ia instanceof Inet6Address && !v6)) {
        return;
    }
    final String new_ip = ia.getHostAddress();
    if (new_ip.equals(external_address)) {
        return;
    }
    try {
        this_mon.enter();
        long now = SystemTime.getCurrentTime();
        if (now - last_address_change < min_address_change_period) {
            return;
        }
        if (contact_history.size() < CONTACT_HISTORY_MAX && !force) {
            if (!initial_address_change_deferred) {
                initial_address_change_deferred = true;
                logger.log("Node " + reporter.getString() + " has reported that the external IP address is '" + new_address + "': deferring new checks");
                new DelayedEvent("DHTTransportUDP:delayAC", 30 * 1000, new AERunnable() {

                    @Override
                    public void runSupport() {
                        try {
                            externalAddressChange(reporter, new_address, true);
                        } catch (Throwable e) {
                        }
                    }
                });
            }
            return;
        }
        logger.log("Node " + reporter.getString() + " has reported that the external IP address is '" + new_address + "'");
        if (invalidExternalAddress(ia)) {
            logger.log("     This is invalid as it is a private address.");
            return;
        }
        if (reporter.getExternalAddress().getAddress().getHostAddress().equals(new_ip)) {
            logger.log("     This is invalid as it is the same as the reporter's address.");
            return;
        }
        last_address_change = now;
        if (min_address_change_period == MIN_ADDRESS_CHANGE_PERIOD_INIT_DEFAULT) {
            min_address_change_period = MIN_ADDRESS_CHANGE_PERIOD_NEXT_DEFAULT;
        }
    } finally {
        this_mon.exit();
    }
    final String old_external_address = external_address;
    // we need to perform this test on a separate thread otherwise we'll block in the UDP handling
    // code because we're already running on the "process" callback from the UDP handler
    // (the test attempts to ping contacts)
    new AEThread2("DHTTransportUDP:getAddress", true) {

        @Override
        public void run() {
            try {
                this_mon.enter();
                if (address_changing) {
                    return;
                }
                address_changing = true;
            } finally {
                this_mon.exit();
            }
            try {
                getExternalAddress(new_ip, logger);
                if (old_external_address.equals(external_address)) {
                    return;
                }
                setLocalContact();
            } finally {
                try {
                    this_mon.enter();
                    address_changing = false;
                } finally {
                    this_mon.exit();
                }
            }
        }
    }.start();
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)286 InetAddress (java.net.InetAddress)196 Inet4Address (java.net.Inet4Address)110 NetworkInterface (java.net.NetworkInterface)54 UnknownHostException (java.net.UnknownHostException)51 SocketException (java.net.SocketException)32 InetSocketAddress (java.net.InetSocketAddress)31 IOException (java.io.IOException)29 LinkAddress (android.net.LinkAddress)28 Test (org.junit.Test)26 RouteInfo (android.net.RouteInfo)21 IpPrefix (android.net.IpPrefix)19 ArrayList (java.util.ArrayList)19 LinkProperties (android.net.LinkProperties)15 ByteBuffer (java.nio.ByteBuffer)9 InterfaceAddress (java.net.InterfaceAddress)7 StringJoiner (java.util.StringJoiner)7 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6