Example usage for java.net InetAddress getAddress

List of usage examples for java.net InetAddress getAddress

Introduction

In this page you can find the example usage for java.net InetAddress getAddress.

Prototype

public byte[] getAddress() 

Source Link

Document

Returns the raw IP address of this InetAddress object.

Usage

From source file:org.bgp4j.net.NetworkLayerReachabilityInformation.java

public NetworkLayerReachabilityInformation(InetAddress source) {
    byte[] raw = source.getAddress();

    setPrefix(8 * raw.length, raw);
}

From source file:org.csploit.android.net.Network.java

public boolean isInternal(InetAddress address) {
    return isInternal(address.getAddress());
}

From source file:org.springframework.cloud.sleuth.stream.Host.java

public int getIpv4() {
    InetAddress inetAddress = null;
    try {/*from ww  w .j a v a2  s  .co m*/
        inetAddress = InetAddress.getByName(this.address);
    } catch (final UnknownHostException e) {
        throw new IllegalArgumentException(e);
    }
    return ByteBuffer.wrap(inetAddress.getAddress()).getInt();
}

From source file:org.apache.hms.controller.rest.ClusterManager.java

public ClusterManager() {
    InetAddress addr;
    try {//  w  ww .  j  a  va  2 s  . c o m
        addr = InetAddress.getLocalHost();
        byte[] ipAddr = addr.getAddress();
        HOSTNAME = addr.getHostName();
    } catch (UnknownHostException e) {
        HOSTNAME = "localhost";
    }
    StringBuilder buffer = new StringBuilder();
    buffer.append("http://");
    buffer.append(HOSTNAME);
    buffer.append(":");
    buffer.append(Controller.CONTROLLER_PORT);
    buffer.append("/");
    buffer.append(Controller.CONTROLLER_PREFIX);
    DEFAULT_URL = buffer.toString();
}

From source file:org.wso2.carbon.appfactory.ext.appserver.LeaderElector.java

private void addEvenListener() {
    //add listener to Complete Topology Event
    topologyEventReceiver.addEventListener(new CompleteTopologyEventListener() {

        @Override/*from  w ww .j a v  a2 s  .c  o m*/
        protected void onEvent(Event event) {

            if (log.isDebugEnabled()) {
                log.debug("[CompleteTopologyEventListener] Received: " + event.getClass());
            }

            try {
                String currentNodeIp = InetAddress.getLocalHost().getHostAddress();
                Map<String, InetAddress> memberIpMap = new HashMap<String, InetAddress>();

                for (Service service : ((CompleteTopologyEvent) event).getTopology().getServices()) {
                    for (Cluster cluster : service.getClusters()) {
                        for (Member member : cluster.getMembers()) {
                            MemberStatus memStatus = member.getStatus();
                            if (MemberStatus.Activated.equals(memStatus)) {
                                memberIpMap.put(member.getMemberIp(),
                                        InetAddress.getByName(member.getMemberIp()));
                            }
                        }
                    }
                    /*if the current node ip is not there in the service cluster; it should be a
                    different service cluster*/
                    if (memberIpMap.get(currentNodeIp) != null) {
                        break;
                    } else {
                        memberIpMap.clear();
                    }
                }

                if (memberIpMap.isEmpty()) {
                    return;
                }

                List<InetAddress> memberIpList = new ArrayList<InetAddress>(memberIpMap.values());
                Collections.sort(memberIpList, new Comparator<InetAddress>() {

                    @Override
                    public int compare(InetAddress addr1, InetAddress addr2) {
                        byte[] ba1 = addr1.getAddress();
                        byte[] ba2 = addr2.getAddress();

                        // general ordering: ipv4 before ipv6
                        if (ba1.length < ba2.length) {
                            return -1;
                        }
                        if (ba1.length > ba2.length) {
                            return 1;
                        }

                        // we have 2 ips of the same type, so we have to compare each byte
                        for (int i = 0; i < ba1.length; i++) {
                            int b1 = unsignedByteToInt(ba1[i]);
                            int b2 = unsignedByteToInt(ba2[i]);
                            if (b1 == b2) {
                                continue;
                            }
                            if (b1 < b2) {
                                return -1;
                            } else {
                                return 1;
                            }
                        }
                        return 0;
                    }

                    private int unsignedByteToInt(byte b) {
                        return (int) b & 0xFF;
                    }
                });

                if ((memberIpMap.get(currentNodeIp) != null)
                        && currentNodeIp.equals(memberIpList.get(0).getHostAddress())) {
                    isNotifyEligible = true;
                } else {
                    isNotifyEligible = false;
                }

            } catch (UnknownHostException e) {
                log.error("Unable to identify the host ip " + e.getMessage(), e);
            }
        }
    });
}

From source file:com.netxforge.oss2.config.MergeableSpecific.java

/**
 * <p>Constructor for MergeableSpecific.</p>
 *
 * @param specific a {@link java.lang.String} object.
 *///from  w  w  w.  ja  va  2s  .  c  o m
public MergeableSpecific(final String specific) {
    m_specific = specific;
    final InetAddress addr = InetAddressUtils.addr(specific);
    if (addr == null) {
        throw new IllegalArgumentException("Unable to get InetAddress for " + specific);
    }
    m_value = addr.getAddress();
}

From source file:io.netlibs.bgp.protocol.NetworkLayerReachabilityInformation.java

public NetworkLayerReachabilityInformation(final InetAddress source) {
    final byte[] raw = source.getAddress();
    this.setPrefix(8 * raw.length, raw);
}

From source file:org.jasig.cas.adaptors.generic.remote.RemoteAddressAuthenticationHandler.java

/**
 * Checks if a subnet contains a specific IP address.
 *
 * @param network The network address./*from  ww  w. j  a va  2  s . co  m*/
 * @param netmask The subnet mask.
 * @param ip The IP address to check.
 * @return A boolean value.
 */
private boolean containsAddress(final InetAddress network, final InetAddress netmask, final InetAddress ip) {
    logger.debug("Checking IP address: {} in {} by {}", ip, network, netmask);

    final byte[] networkBytes = network.getAddress();
    final byte[] netmaskBytes = netmask.getAddress();
    final byte[] ipBytes = ip.getAddress();

    /* check IPv4/v6-compatibility or parameters: */
    if (networkBytes.length != netmaskBytes.length || netmaskBytes.length != ipBytes.length) {
        logger.debug("Network address {}, subnet mask {} and/or host address {}"
                + " have different sizes! (return false ...)", network, netmask, ip);
        return false;
    }

    /* Check if the masked network and ip addresses match: */
    for (int i = 0; i < netmaskBytes.length; i++) {
        final int mask = netmaskBytes[i] & HEX_RIGHT_SHIFT_COEFFICIENT;
        if ((networkBytes[i] & mask) != (ipBytes[i] & mask)) {
            logger.debug("{} is not in {}/{}", ip, network, netmask);
            return false;
        }
    }
    logger.debug("{} is in {}/{}", ip, network, netmask);
    return true;
}

From source file:com.streamsets.pipeline.stage.processor.geolocation.TestGeolocationProcessor.java

@Test
public void testConversion() throws Exception {
    String[] ips = { "128.101.101.101", "8.8.8.8" };
    for (String ipAsString : ips) {
        InetAddress ip = InetAddress.getByName(ipAsString);
        byte[] ipAsBytes = ip.getAddress();
        int ipAsInt = GeolocationProcessor.ipAsBytesToInt(ipAsBytes);
        Assert.assertArrayEquals(ipAsBytes, GeolocationProcessor.ipAsIntToBytes(ipAsInt));
        Assert.assertArrayEquals(ipAsBytes, GeolocationProcessor.ipAsStringToBytes(ipAsString));
        Assert.assertEquals(ipAsString, GeolocationProcessor.ipAsIntToString(ipAsInt));
        Assert.assertEquals(ipAsString, GeolocationProcessor.ipAsIntToString(ipAsInt));
        Assert.assertEquals(ipAsInt, GeolocationProcessor.ipAsStringToInt(ipAsString));
        Assert.assertArrayEquals(ipAsBytes, GeolocationProcessor.ipAsStringToBytes(ipAsString));
    }/*  w  w  w. ja va  2s .c o m*/
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * This method uses JNDI to look up an address in DNS and return its name
 * /*  www.  j av a2s.c  o  m*/
 * @param addr
 *
 * @return the host name associated with the address or null if lookup isn't possible or there is
 *         no host name for this address
 */
public static String reverseDNS(InetAddress addr) {
    byte[] addrBytes = addr.getAddress();
    // reverse the address suitable for reverse lookup
    String lookup = "";
    for (int index = addrBytes.length - 1; index >= 0; index--) {
        lookup = lookup + (addrBytes[index] & 0xff) + '.';
    }
    lookup += "in-addr.arpa";
    // System.out.println("Looking up: " + lookup);

    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        DirContext ctx = new InitialDirContext(env);
        Attributes attrs = ctx.getAttributes(lookup, new String[] { "PTR" });
        for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
            Attribute attr = (Attribute) ae.next();
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                Object elem = vals.nextElement();
                if ("PTR".equals(attr.getID()) && elem != null) {
                    return elem.toString();
                }
            }
        }
        ctx.close();
    } catch (Exception e) {
        // ignored
    }
    return null;
}