Search in sources :

Example 71 with InetAddress

use of java.net.InetAddress in project zookeeper by apache.

the class QuorumPeerTest method testQuorumPeerListendOnSpecifiedClientIP.

/**
     * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2301
     */
@Test
public void testQuorumPeerListendOnSpecifiedClientIP() throws IOException {
    long myId = 1;
    File dataDir = ClientBase.createTmpDir();
    int clientPort = PortAssignment.unique();
    Map<Long, QuorumServer> peersView = new HashMap<Long, QuorumServer>();
    InetAddress clientIP = InetAddress.getLoopbackAddress();
    peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT));
    /**
         * QuorumPeer constructor without QuorumVerifier
         */
    QuorumPeer peer1 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit);
    String hostString1 = peer1.cnxnFactory.getLocalAddress().getHostString();
    assertEquals(clientIP.getHostAddress(), hostString1);
    // cleanup
    peer1.shutdown();
    /**
         * QuorumPeer constructor with QuorumVerifier
         */
    peersView.clear();
    clientPort = PortAssignment.unique();
    peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT));
    QuorumPeer peer2 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit);
    String hostString2 = peer2.cnxnFactory.getLocalAddress().getHostString();
    assertEquals(clientIP.getHostAddress(), hostString2);
    // cleanup
    peer2.shutdown();
}
Also used : HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 72 with InetAddress

use of java.net.InetAddress in project crate by crate.

the class AzureUnicastHostsProvider method listIPAddresses.

public static List<String> listIPAddresses(NetworkResourceProviderClient networkResourceProviderClient, String rgName, String vnetName, String subnetName, String discoveryMethod, HostType hostType, ESLogger logger) {
    List<String> ipList = new ArrayList<>();
    List<ResourceId> ipConfigurations = new ArrayList<>();
    try {
        List<Subnet> subnets = networkResourceProviderClient.getVirtualNetworksOperations().get(rgName, vnetName).getVirtualNetwork().getSubnets();
        if (discoveryMethod.equalsIgnoreCase(AzureDiscovery.VNET)) {
            for (Subnet subnet : subnets) {
                ipConfigurations.addAll(subnet.getIpConfigurations());
            }
        } else {
            for (Subnet subnet : subnets) {
                if (subnet.getName().equalsIgnoreCase(subnetName)) {
                    ipConfigurations.addAll(subnet.getIpConfigurations());
                }
            }
        }
        for (ResourceId resourceId : ipConfigurations) {
            String[] nicURI = resourceId.getId().split("/");
            NetworkInterface nic = networkResourceProviderClient.getNetworkInterfacesOperations().get(rgName, nicURI[nicURI.length - 3]).getNetworkInterface();
            ArrayList<NetworkInterfaceIpConfiguration> ips = nic.getIpConfigurations();
            // find public ip address
            for (NetworkInterfaceIpConfiguration ipConfiguration : ips) {
                String networkAddress = null;
                // Let's detect if we want to use public or private IP
                switch(hostType) {
                    case PRIVATE_IP:
                        InetAddress privateIp = InetAddress.getByName(ipConfiguration.getPrivateIpAddress());
                        if (privateIp != null) {
                            networkAddress = NetworkAddress.format(privateIp);
                        } else {
                            logger.trace("no private ip provided. ignoring [{}]...", nic.getName());
                        }
                        break;
                    case PUBLIC_IP:
                        if (ipConfiguration.getPublicIpAddress() != null) {
                            String[] pipID = ipConfiguration.getPublicIpAddress().getId().split("/");
                            PublicIpAddress pip = networkResourceProviderClient.getPublicIpAddressesOperations().get(rgName, pipID[pipID.length - 1]).getPublicIpAddress();
                            networkAddress = NetworkAddress.format(InetAddress.getByName(pip.getIpAddress()));
                        }
                        if (networkAddress == null) {
                            logger.trace("no public ip provided. ignoring [{}]...", nic.getName());
                        }
                        break;
                }
                if (networkAddress == null) {
                    // We have a bad parameter here or not enough information from azure
                    logger.warn("no network address found. ignoring [{}]...", nic.getName());
                    continue;
                } else {
                    ipList.add(networkAddress);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Could not retrieve IP addresses for unicast host list", e);
    }
    return ipList;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Example 73 with InetAddress

use of java.net.InetAddress in project jcollectd by collectd.

the class UdpReceiver method getSocket.

public DatagramSocket getSocket() throws IOException {
    if (_socket == null) {
        if (_bindAddress == null) {
            _socket = new DatagramSocket(_port);
        } else {
            InetAddress addr = InetAddress.getByName(_bindAddress);
            if (addr.isMulticastAddress()) {
                MulticastSocket mcast = new MulticastSocket(_port);
                if (_ifAddress != null) {
                    mcast.setInterface(InetAddress.getByName(_ifAddress));
                }
                mcast.joinGroup(addr);
                _socket = mcast;
            } else {
                _socket = new DatagramSocket(_port, addr);
            }
        }
    }
    return _socket;
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) InetAddress(java.net.InetAddress)

Example 74 with InetAddress

use of java.net.InetAddress in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 75 with InetAddress

use of java.net.InetAddress in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2063 UnknownHostException (java.net.UnknownHostException)377 IOException (java.io.IOException)284 Test (org.junit.Test)272 InetSocketAddress (java.net.InetSocketAddress)240 NetworkInterface (java.net.NetworkInterface)178 ArrayList (java.util.ArrayList)158 SocketException (java.net.SocketException)148 Inet6Address (java.net.Inet6Address)97 HashMap (java.util.HashMap)95 Inet4Address (java.net.Inet4Address)88 Socket (java.net.Socket)73 LinkAddress (android.net.LinkAddress)70 DatagramPacket (java.net.DatagramPacket)67 Token (org.apache.cassandra.dht.Token)67 DatagramSocket (java.net.DatagramSocket)61 RouteInfo (android.net.RouteInfo)56 Map (java.util.Map)55 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)50