Search in sources :

Example 26 with Inet4Address

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

the class MulticastLoopbackModeTest method hasConfiguredNetworkInterface.

/**
     * Replies if a network interface was properly configured.
     *
     * @return <code>true</code> if there is at least one configured interface;
     * <code>false</code> otherwise.
     */
protected static boolean hasConfiguredNetworkInterface() {
    try {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface i = e.nextElement();
            Enumeration<InetAddress> as = i.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address && !a.isLoopbackAddress() && !a.isMulticastAddress()) {
                    return true;
                }
            }
        }
    } catch (Exception ignored) {
    // silently cast the exceptions
    }
    return false;
}
Also used : Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 27 with Inet4Address

use of java.net.Inet4Address 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 Inet4Address

use of java.net.Inet4Address 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 Inet4Address

use of java.net.Inet4Address 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 Inet4Address

use of java.net.Inet4Address in project j2objc by google.

the class NetworkInterfaceTest method testLoopback.

public void testLoopback() throws Exception {
    // We know lo shouldn't have a hardware address.
    NetworkInterface lo = NetworkInterface.getByName("lo0");
    assertNull(lo.getHardwareAddress());
    // But eth0, if it exists, should...
    NetworkInterface eth0 = NetworkInterface.getByName("eth0");
    if (eth0 != null) {
        assertEquals(6, eth0.getHardwareAddress().length);
        for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
            if (ia.getAddress() instanceof Inet4Address) {
                assertNotNull(ia.getBroadcast());
            }
        }
    }
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface)

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