Example usage for java.net InetAddress getByAddress

List of usage examples for java.net InetAddress getByAddress

Introduction

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

Prototype

public static InetAddress getByAddress(byte[] addr) throws UnknownHostException 

Source Link

Document

Returns an InetAddress object given the raw IP address .

Usage

From source file:sdn_competition.PcapReader_Main.java

/**
* inetAddress(byte[] buf): converts an array of four bytes to an InetAddress
*///from w  w  w  .  java  2 s. c  o  m
private static InetAddress inetAddress(byte[] buf) {
    InetAddress addr = null;
    try {
        addr = InetAddress.getByAddress(buf);
    } catch (UnknownHostException uhe) {
    }
    return addr;
}

From source file:slash.navigation.mapview.browser.BrowserMapView.java

protected void checkLocalhostResolution() {
    try {//www .j  av a 2  s .  c  o m
        InetAddress localhost = InetAddress.getByName("localhost");
        log.info("localhost is resolved to: " + localhost);
        String localhostName = localhost.getHostAddress();
        log.info("IP of localhost is: " + localhostName);
        if (!localhostName.equals("127.0.0.1"))
            throw new Exception("localhost does not resolve to 127.0.0.1");

        InetAddress ip = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
        log.info("127.0.0.1 is resolved to: " + ip);
        String ipName = localhost.getHostName();
        log.info("Name of 127.0.0.1 is: " + ipName);
        if (!ipName.equals("localhost"))
            throw new Exception("127.0.0.1 does not resolve to localhost");
    } catch (Exception e) {
        final String message = "Probably faulty network setup: " + getLocalizedMessage(e)
                + ".\nPlease check your network settings.";
        log.severe(message);
        invokeLater(new Runnable() {
            public void run() {
                showMessageDialog(getComponent(), message, "Error", ERROR_MESSAGE);
            }
        });
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private void readIpv4Src(ByteBuffer data, int nxmLen, boolean hasMask) {
    if (hasMask) {
        if ((nxmLen != 2 * 4) || (data.remaining() < 2 * 4))
            return;
        else {//from  w  w w  . ja  v a2  s .c o  m
            byte[] sbytes = new byte[4];
            data.get(sbytes);
            try {
                this.nwSrc = InetAddress.getByAddress(sbytes);
            } catch (UnknownHostException e) {
                return;
            }
            byte[] mbytes = new byte[4];
            data.get(mbytes);
            try {
                this.networkSourceMask = InetAddress.getByAddress(mbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwSrcState = MatchFieldState.MATCH_FIELD_WITH_MASK;
            this.match_len += 12;
            int prefixlen = getNetworkMaskPrefixLength(mbytes);
            this.wildcards ^= (((1 << 6) - 1) << 8); // Sync with 0F 1.0 Match
            this.wildcards |= ((32 - prefixlen) << 8); // Sync with 0F 1.0 Match

        }
    } else {
        if ((nxmLen != 4) || (data.remaining() < 4))
            return;
        else {
            byte[] sbytes = new byte[4];
            data.get(sbytes);
            try {
                this.nwSrc = InetAddress.getByAddress(sbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwSrcState = MatchFieldState.MATCH_FIELD_ONLY;
            this.match_len += 8;
            this.wildcards ^= (((1 << 6) - 1) << 8); // Sync with 0F 1.0 Match
        }
    }
}

From source file:org.commoncrawl.io.NIOHttpConnection.java

public void open() throws IOException {

    if (_state != State.IDLE)
        throw new IOException("Invalid State");

    _openTime = System.currentTimeMillis();

    if (_url.getHost().length() == 0 || _method.length() == 0 || _httpVersionString.length() == 0)
        throw new IOException("Invalid Base HTTP Parameters Specified");

    setState(State.AWAITING_RESOLUTION, null);

    InetSocketAddress socketAddress = getProxyServer();

    if (socketAddress == null) {
        InetAddress addressToConnectTo = null;

        // get host name ...
        String hostName = _url.getHost();

        // figure out if it url is an explicit IP Address ...
        byte[] ipAddress = IPAddressUtils.textToNumericFormatV4(hostName);

        // if this IS an IP address (vs a hostname that needs to be resolved...)
        if (ipAddress != null) {
            // set address to connect to ...
            addressToConnectTo = InetAddress.getByAddress(ipAddress);
        }/*  w w  w .  j  a  v  a 2s  .c  om*/
        // now if address to connect to is still null...
        if (addressToConnectTo == null) {
            // see if someone overloaded resolved address ...
            addressToConnectTo = getResolvedAddress();
        }

        // now if address to connect to is not null, convert it to a socket
        // address
        if (addressToConnectTo != null) {
            // TODO: FIX THIS FOR HTTPS
            socketAddress = new InetSocketAddress(addressToConnectTo,
                    (_url.getPort() == -1) ? 80 : _url.getPort());
        }
    }
    // now, if socket address is NOT null, directly connect to the specified
    // address, bypassing dns lookup
    if (socketAddress != null) {
        startConnect(socketAddress);
    }
    // otherwise delegate to resolver (to figure out ip address)
    else {
        LOG.info("Sending Host:" + _url.getHost() + " to resolver");
        _resolver.resolve(this, _url.getHost(), false, true, _dnsTimeout);
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private void readIpv4Dst(ByteBuffer data, int nxmLen, boolean hasMask) {
    if (hasMask) {
        if ((nxmLen != 2 * 4) || (data.remaining() < 2 * 4))
            return;
        else {/*  w  w  w  .  j  av a  2  s . c  om*/
            byte[] dbytes = new byte[4];
            data.get(dbytes);
            try {
                this.nwDst = InetAddress.getByAddress(dbytes);
            } catch (UnknownHostException e) {
                return;
            }
            byte[] mbytes = new byte[4];
            data.get(mbytes);
            try {
                this.networkDestinationMask = InetAddress.getByAddress(mbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwDstState = MatchFieldState.MATCH_FIELD_WITH_MASK;
            this.match_len += 12;
            int prefixlen = getNetworkMaskPrefixLength(mbytes);
            this.wildcards ^= (((1 << 6) - 1) << 14); // Sync with 0F 1.0 Match
            this.wildcards |= ((32 - prefixlen) << 14); // Sync with 0F 1.0 Match
        }
    } else {
        if ((nxmLen != 4) || (data.remaining() < 4))
            return;
        else {
            byte[] dbytes = new byte[4];
            data.get(dbytes);
            try {
                this.nwDst = InetAddress.getByAddress(dbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwDstState = MatchFieldState.MATCH_FIELD_ONLY;
            this.wildcards ^= (((1 << 6) - 1) << 14); // Sync with 0F 1.0 Match
            this.match_len += 8;
        }
    }
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

public RequestPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);// ww w . j  av  a2s . co  m
    int exactLength = bbuf.position();

    this.requestID = bbuf.getLong();
    this.stop = bbuf.get() == (byte) 1;
    exactLength += (8 + 1);

    // addresses
    byte[] ca = new byte[4];
    bbuf.get(ca);
    int cport = (int) bbuf.getShort();
    cport = cport >= 0 ? cport : cport + 2 * (Short.MAX_VALUE + 1);
    this.clientAddress = cport != 0 ? new InetSocketAddress(InetAddress.getByAddress(ca), cport) : null;
    byte[] la = new byte[4];
    bbuf.get(la);
    int lport = (int) bbuf.getShort();
    lport = lport >= 0 ? lport : lport + 2 * (Short.MAX_VALUE + 1);
    this.listenAddress = lport != 0 ? new InetSocketAddress(InetAddress.getByAddress(la), lport) : null;
    exactLength += (4 + 2 + 4 + 2);

    // other non-final fields
    this.entryReplica = bbuf.getInt();
    this.entryTime = bbuf.getLong();
    this.shouldReturnRequestValue = bbuf.get() == (byte) 1;
    this.forwardCount = bbuf.getInt();
    exactLength += (4 + 8 + 1 + 4);

    // digest related fields
    this.broadcasted = bbuf.get() == (byte) 1;
    int digestLength = bbuf.getInt();
    if (digestLength > 0)
        bbuf.get(this.digest = new byte[digestLength]);

    // highly variable length fields

    // requestValue
    int reqValLen = bbuf.getInt();
    byte[] reqValBytes = new byte[reqValLen];
    bbuf.get(reqValBytes);
    this.requestValue = reqValBytes.length > 0 ? new String(reqValBytes, CHARSET) : null;
    exactLength += (4 + reqValBytes.length);

    // responseValue
    int respValLen = bbuf.getInt();
    byte[] respValBytes = new byte[respValLen];
    bbuf.get(respValBytes);
    this.responseValue = respValBytes.length > 0 ? new String(respValBytes, CHARSET) : null;
    exactLength += (4 + respValBytes.length);

    int numBatched = bbuf.getInt();
    if (numBatched == 0)
        return;
    // else
    // batched requests
    this.batched = new RequestPacket[numBatched];
    for (int i = 0; i < numBatched; i++) {
        int len = bbuf.getInt();
        byte[] element = new byte[len];
        bbuf.get(element);
        this.batched[i] = new RequestPacket(element);
    }
    assert (exactLength > 0);
}

From source file:com.ebay.jetstream.messaging.transport.zookeeper.ZooKeeperTransport.java

private String createKey(String path, ZooKeeperDataWrapper wrapper) {
    byte[] orig = ((JetstreamMessage) wrapper.getOrginalData()).getMsgOrigination();
    try {/*www. j a v a  2  s. c  om*/
        String hostname = InetAddress.getByAddress(orig).getHostName();
        String key = hostname + "_" + path;
        return key;
    } catch (UnknownHostException e) {
        LOGGER.warn("UnknownHostException while creating the key :" + path, e);
    }
    return null;
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private void readIpv6Src(ByteBuffer data, int nxmLen, boolean hasMask) {
    if (hasMask) {
        if ((nxmLen != 2 * 16) || (data.remaining() < 2 * 16))
            return;
        else {/*from ww w  . j  ava2  s  .co  m*/
            byte[] sbytes = new byte[16];
            data.get(sbytes);
            try {
                this.nwSrc = InetAddress.getByAddress(sbytes);
            } catch (UnknownHostException e) {
                return;
            }
            byte[] mbytes = new byte[16];
            data.get(mbytes);
            try {
                this.networkSourceMask = InetAddress.getByAddress(mbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwSrcState = MatchFieldState.MATCH_FIELD_WITH_MASK;
            this.match_len += 36;
        }
    } else {
        if ((nxmLen != 16) || (data.remaining() < 16))
            return;
        else {
            byte[] sbytes = new byte[16];
            data.get(sbytes);
            try {
                this.nwSrc = InetAddress.getByAddress(sbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwSrcState = MatchFieldState.MATCH_FIELD_ONLY;
            this.match_len += 20;
        }
    }
}

From source file:io.divolte.server.recordmapping.DslRecordMapping.java

private static Optional<InetAddress> tryParseIpv4(final String ip) {
    final byte[] result = new byte[4];
    final String[] parts = StringUtils.split(ip, '.');
    if (parts.length != 4) {
        return Optional.empty();
    }//from w w  w. jav a 2s.  c  o m
    try {
        for (int c = 0; c < 4; c++) {
            final int x = Integer.parseInt(parts[c]);
            if (x < 0x00 || x > 0xff) {
                return Optional.empty();
            }
            result[c] = (byte) (x & 0xff);
        }
    } catch (NumberFormatException nfe) {
        return Optional.empty();
    }

    try {
        return Optional.of(InetAddress.getByAddress(result));
    } catch (UnknownHostException e) {
        return Optional.empty();
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private void readIpv6Dst(ByteBuffer data, int nxmLen, boolean hasMask) {
    if (hasMask) {
        if ((nxmLen != 2 * 16) || (data.remaining() < 2 * 16))
            return;
        else {//w w  w  .j a  v a2s.  c o  m
            byte[] dbytes = new byte[16];
            data.get(dbytes);
            try {
                this.nwDst = InetAddress.getByAddress(dbytes);
            } catch (UnknownHostException e) {
                return;
            }
            byte[] mbytes = new byte[16];
            data.get(mbytes);
            try {
                this.networkDestinationMask = InetAddress.getByAddress(mbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwDstState = MatchFieldState.MATCH_FIELD_WITH_MASK;
            this.match_len += 36;
        }
    } else {
        if ((nxmLen != 16) || (data.remaining() < 16))
            return;
        else {
            byte[] dbytes = new byte[16];
            data.get(dbytes);
            try {
                this.nwDst = InetAddress.getByAddress(dbytes);
            } catch (UnknownHostException e) {
                return;
            }
            this.nwDstState = MatchFieldState.MATCH_FIELD_ONLY;
            this.match_len += 20;
        }
    }
}