Search in sources :

Example 26 with Inet6Address

use of java.net.Inet6Address in project hazelcast by hazelcast.

the class DefaultAddressPickerTest method findIPv4NonLoopbackInterface.

private static InetAddress findIPv4NonLoopbackInterface() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isLoopbackAddress()) {
                    continue;
                }
                if (address instanceof Inet6Address) {
                    continue;
                }
                return address;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 27 with Inet6Address

use of java.net.Inet6Address in project JAirPort by froks.

the class JmDNSImpl method getServiceInfoFromCache.

ServiceInfoImpl getServiceInfoFromCache(String type, String name, String subtype, boolean persistent) {
    // Check if the answer is in the cache.
    ServiceInfoImpl info = new ServiceInfoImpl(type, name, subtype, 0, 0, 0, persistent, (byte[]) null);
    DNSEntry pointerEntry = this.getCache().getDNSEntry(new DNSRecord.Pointer(type, DNSRecordClass.CLASS_ANY, false, 0, info.getQualifiedName()));
    if (pointerEntry instanceof DNSRecord) {
        ServiceInfoImpl cachedInfo = (ServiceInfoImpl) ((DNSRecord) pointerEntry).getServiceInfo(persistent);
        if (cachedInfo != null) {
            // To get a complete info record we need to retrieve the service, address and the text bytes.
            Map<Fields, String> map = cachedInfo.getQualifiedNameMap();
            byte[] srvBytes = null;
            String server = "";
            DNSEntry serviceEntry = this.getCache().getDNSEntry(info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_ANY);
            if (serviceEntry instanceof DNSRecord) {
                ServiceInfo cachedServiceEntryInfo = ((DNSRecord) serviceEntry).getServiceInfo(persistent);
                if (cachedServiceEntryInfo != null) {
                    cachedInfo = new ServiceInfoImpl(map, cachedServiceEntryInfo.getPort(), cachedServiceEntryInfo.getWeight(), cachedServiceEntryInfo.getPriority(), persistent, (byte[]) null);
                    srvBytes = cachedServiceEntryInfo.getTextBytes();
                    server = cachedServiceEntryInfo.getServer();
                }
            }
            DNSEntry addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
            if (addressEntry instanceof DNSRecord) {
                ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                if (cachedAddressInfo != null) {
                    for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
                        cachedInfo.addAddress(address);
                    }
                    cachedInfo._setText(cachedAddressInfo.getTextBytes());
                }
            }
            addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_ANY);
            if (addressEntry instanceof DNSRecord) {
                ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                if (cachedAddressInfo != null) {
                    for (Inet6Address address : cachedAddressInfo.getInet6Addresses()) {
                        cachedInfo.addAddress(address);
                    }
                    cachedInfo._setText(cachedAddressInfo.getTextBytes());
                }
            }
            DNSEntry textEntry = this.getCache().getDNSEntry(cachedInfo.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_ANY);
            if (textEntry instanceof DNSRecord) {
                ServiceInfo cachedTextInfo = ((DNSRecord) textEntry).getServiceInfo(persistent);
                if (cachedTextInfo != null) {
                    cachedInfo._setText(cachedTextInfo.getTextBytes());
                }
            }
            if (cachedInfo.getTextBytes().length == 0) {
                cachedInfo._setText(srvBytes);
            }
            if (cachedInfo.hasData()) {
                info = cachedInfo;
            }
        }
    }
    return info;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) Inet4Address(java.net.Inet4Address) Fields(javax.jmdns.ServiceInfo.Fields) Inet6Address(java.net.Inet6Address)

Example 28 with Inet6Address

use of java.net.Inet6Address in project JAirPort by froks.

the class ServiceInfoImpl method clone.

/*
     * (non-Javadoc)
     * @see javax.jmdns.ServiceInfo#clone()
     */
@Override
public ServiceInfoImpl clone() {
    ServiceInfoImpl serviceInfo = new ServiceInfoImpl(this.getQualifiedNameMap(), _port, _weight, _priority, _persistent, _text);
    Inet6Address[] ipv6Addresses = this.getInet6Addresses();
    for (Inet6Address address : ipv6Addresses) {
        serviceInfo._ipv6Addresses.add(address);
    }
    Inet4Address[] ipv4Addresses = this.getInet4Addresses();
    for (Inet4Address address : ipv4Addresses) {
        serviceInfo._ipv4Addresses.add(address);
    }
    return serviceInfo;
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address)

Example 29 with Inet6Address

use of java.net.Inet6Address in project JAirPort by froks.

the class ServiceInfoImpl method updateRecord.

/**
     * JmDNS callback to update a DNS record.
     *
     * @param dnsCache
     * @param now
     * @param rec
     */
@Override
public void updateRecord(DNSCache dnsCache, long now, DNSEntry rec) {
    if ((rec instanceof DNSRecord) && !rec.isExpired(now)) {
        boolean serviceUpdated = false;
        switch(rec.getRecordType()) {
            case // IPv4
            TYPE_A:
                if (rec.getName().equalsIgnoreCase(this.getServer())) {
                    _ipv4Addresses.add((Inet4Address) ((DNSRecord.Address) rec).getAddress());
                    serviceUpdated = true;
                }
                break;
            case // IPv6
            TYPE_AAAA:
                if (rec.getName().equalsIgnoreCase(this.getServer())) {
                    _ipv6Addresses.add((Inet6Address) ((DNSRecord.Address) rec).getAddress());
                    serviceUpdated = true;
                }
                break;
            case TYPE_SRV:
                if (rec.getName().equalsIgnoreCase(this.getQualifiedName())) {
                    DNSRecord.Service srv = (DNSRecord.Service) rec;
                    boolean serverChanged = (_server == null) || !_server.equalsIgnoreCase(srv.getServer());
                    _server = srv.getServer();
                    _port = srv.getPort();
                    _weight = srv.getWeight();
                    _priority = srv.getPriority();
                    if (serverChanged) {
                        _ipv4Addresses.clear();
                        _ipv6Addresses.clear();
                        for (DNSEntry entry : dnsCache.getDNSEntryList(_server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN)) {
                            this.updateRecord(dnsCache, now, entry);
                        }
                        for (DNSEntry entry : dnsCache.getDNSEntryList(_server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN)) {
                            this.updateRecord(dnsCache, now, entry);
                        }
                    // We do not want to trigger the listener in this case as it will be triggered if the address resolves.
                    } else {
                        serviceUpdated = true;
                    }
                }
                break;
            case TYPE_TXT:
                if (rec.getName().equalsIgnoreCase(this.getQualifiedName())) {
                    DNSRecord.Text txt = (DNSRecord.Text) rec;
                    _text = txt.getText();
                    serviceUpdated = true;
                }
                break;
            case TYPE_PTR:
                if ((this.getSubtype().length() == 0) && (rec.getSubtype().length() != 0)) {
                    _subtype = rec.getSubtype();
                    serviceUpdated = true;
                }
                break;
            default:
                break;
        }
        if (serviceUpdated && this.hasData()) {
            JmDNSImpl dns = this.getDns();
            if (dns != null) {
                ServiceEvent event = ((DNSRecord) rec).getServiceEvent(dns);
                event = new ServiceEventImpl(dns, event.getType(), event.getName(), this);
                dns.handleServiceResolved(event);
            }
        }
        // This is done, to notify the wait loop in method JmDNS.waitForInfoData(ServiceInfo info, int timeout);
        synchronized (this) {
            this.notifyAll();
        }
    }
}
Also used : InetAddress(java.net.InetAddress) Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) ServiceEvent(javax.jmdns.ServiceEvent) Service(javax.jmdns.impl.DNSRecord.Service) Text(javax.jmdns.impl.DNSRecord.Text) Text(javax.jmdns.impl.DNSRecord.Text) Service(javax.jmdns.impl.DNSRecord.Service)

Example 30 with Inet6Address

use of java.net.Inet6Address in project guava by google.

the class InetAddressesTest method testIsatapAddresses.

public void testIsatapAddresses() {
    InetAddress ipv4 = InetAddresses.forString("1.2.3.4");
    String[] validIsatapAddresses = { "2001:db8::5efe:102:304", // Private Multicast? Not likely.
    "2001:db8::100:5efe:102:304", "2001:db8::200:5efe:102:304", // Public Multicast? Also unlikely.
    "2001:db8::300:5efe:102:304" };
    String[] nonIsatapAddresses = { "::1.2.3.4", "3ffe::1", "::", "::1", "2001:db8::0040:5efe:102:304", "2001:db8::5ffe:102:304", "2001:db8::5eff:102:304", // Teredo address; not ISATAP
    "2001:0:102:203:200:5efe:506:708" };
    for (int i = 0; i < validIsatapAddresses.length; i++) {
        InetAddress ip = InetAddresses.forString(validIsatapAddresses[i]);
        assertTrue(InetAddresses.isIsatapAddress((Inet6Address) ip));
        assertEquals("checking '" + validIsatapAddresses[i] + "'", ipv4, InetAddresses.getIsatapIPv4Address((Inet6Address) ip));
    }
    for (int i = 0; i < nonIsatapAddresses.length; i++) {
        InetAddress ip = InetAddresses.forString(nonIsatapAddresses[i]);
        assertFalse(InetAddresses.isIsatapAddress((Inet6Address) ip));
        try {
            InetAddresses.getIsatapIPv4Address((Inet6Address) ip);
            fail("IllegalArgumentException expected for '" + nonIsatapAddresses[i] + "'");
        } catch (IllegalArgumentException expected) {
        // expected behavior
        }
    }
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)137 InetAddress (java.net.InetAddress)92 Inet4Address (java.net.Inet4Address)45 NetworkInterface (java.net.NetworkInterface)28 LinkAddress (android.net.LinkAddress)21 IpPrefix (android.net.IpPrefix)19 IOException (java.io.IOException)18 UnknownHostException (java.net.UnknownHostException)17 LinkProperties (android.net.LinkProperties)15 SocketException (java.net.SocketException)15 RouteInfo (android.net.RouteInfo)14 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuffer (java.nio.ByteBuffer)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 RaParams (android.net.ip.RouterAdvertisementDaemon.RaParams)5 Pair (android.util.Pair)5 ProvisioningChange (android.net.LinkProperties.ProvisioningChange)4 StructNdMsg (android.net.netlink.StructNdMsg)4