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.opendaylight.ipsec.domain.IPsecRule.java

/**
 * If the (from, to) pair match the rule.
 * @param from source address/*from   w  w  w  .  j av a2  s . co m*/
 * @param to destination address
 * @return match result
 */
public boolean match(InetAddress from, InetAddress to) {
    return matchBits(from.getAddress(), source.getAddress(), srcPrefixLen)
            && matchBits(to.getAddress(), destination.getAddress(), dstPrefixLen);
}

From source file:com.adobe.ags.curly.controller.AuthHandler.java

/**
 * ISP DNS providers commonly redirect to their own branded search pages in
 * order to drive revenue This greedy business practice can result in a hung
 * connection so we have to detect it and avoid those redirects at all
 * costs./*from  w  ww .j a  v a  2s.  c  o m*/
 *
 * @param address
 * @return
 */
private boolean isDnsRedirect(InetAddress address) {
    byte[] ip = address.getAddress();
    // Detect TWC rr.com redirects -- note that bytes are signed values 
    // so we have alias them back to positive integers first
    return (ip[0] & 0x0ff) == 198 && (ip[1] & 0x0ff) == 105;
}

From source file:org.apache.hadoop.hbase.TestIPv6NIOServerSocketChannel.java

/**
 * Checks whether we are running with java.net.preferIPv4Stack=true
 *//*  w w  w .  ja va  2  s. c  o m*/
public void ensurePreferIPv4() throws IOException {
    InetAddress[] addrs = InetAddress.getAllByName("localhost");
    for (InetAddress addr : addrs) {
        LOG.info("resolved localhost as:" + addr);
        Assert.assertEquals(4, addr.getAddress().length); //ensure 4 byte ipv4 address
    }
}

From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public byte[] getLocalAddressRaw() {
    byte[] result = null;
    try {//  w  w  w. j a  v  a  2  s . c  om
        InetAddress localHost = getLocalAddress();
        result = localHost.getAddress();
    } catch (UnknownHostException ex) {
        if (fLog.isErrorEnabled()) {
            fLog.error("Unable to determine local host address. " + ex);
        }
    }
    return result;

}

From source file:com.nokia.dempsy.messagetransport.tcp.TcpSender.java

private DataOutputStream getDataOutputStream() throws MessageTransportException, IOException {
    if (dataOutputStream == null) // socket must also be null.
    {//from   w  w w .  ja  v a 2 s  . co  m
        socket = makeSocket(destination);

        // There is a really odd circumstance (at least on Linux) where a connection 
        //  to a port in the dynamic range, while there is no listener on that port,
        //  from the same system/network interface, can result in a local port selection
        //  that's the same as the port that the connection attempt is to. In this case,
        //  for some reason the Socket instantiation (and connection) succeeds without
        //  a listener. We need to force a failure if this is the case.
        if (isLocalAddress == IsLocalAddress.Unknown) {
            if (socket.isBound()) {
                InetAddress localSocketAddress = socket.getLocalAddress();
                isLocalAddress = (Arrays.equals(localSocketAddress.getAddress(),
                        destination.inetAddress.getAddress())) ? IsLocalAddress.Yes : IsLocalAddress.No;
            }
        }

        if (isLocalAddress == IsLocalAddress.Yes) {
            if (socket.getLocalPort() == destination.port)
                throw new IOException("Connection to self same port!!!");
        }

        dataOutputStream = new DataOutputStream(socket.getOutputStream());
    }

    return dataOutputStream;
}

From source file:net.timewalker.ffmq4.utils.id.UUIDProvider.java

/** 
 * Constructor/*from   w  w  w  .  j ava2  s .  co m*/
 */
private UUIDProvider() {
    try {
        this.seed = new MTRandom();

        // Try to find localhost address
        byte[] ifBytes = null;
        InetAddress inetaddress = InetAddress.getLocalHost();
        ifBytes = inetaddress != null ? inetaddress.getAddress() : null;
        if (ifBytes == null) {
            // Cannot determine local node address,
            // use a random value instead
            log.warn("Cannot determine localhost address, falling back to random value ...");
            ifBytes = new byte[4];
            seed.nextBytes(ifBytes);
        }

        StringBuilder base = new StringBuilder();

        String s = hexFormat(getInt(ifBytes));
        String s1 = hexFormat(hashCode());
        base.append("-");
        base.append(s.substring(0, 4));
        base.append("-");
        base.append(s.substring(4));
        base.append("-");
        base.append(s1.substring(0, 4));
        base.append("-");
        base.append(s1.substring(4));
        fixedPart = base.toString();
        seed.nextInt();
    } catch (Exception e) {
        log.fatal("Could not initialise UUID generator", e);
        throw new IllegalStateException("Could not initialise UUID generator : " + e.getMessage());
    }
}

From source file:lineage2.loginserver.serverpackets.ServerList.java

/**
 * Method writeImpl.//from  www . ja va  2s.c  o m
 */
@Override
protected void writeImpl() {
    writeC(0x04);
    writeC(_servers.size());
    writeC(_lastServer);
    for (ServerData server : _servers) {
        writeC(server.serverId);
        InetAddress i4 = server.ip;
        byte[] raw = i4.getAddress();
        writeC(raw[0] & 0xff);
        writeC(raw[1] & 0xff);
        writeC(raw[2] & 0xff);
        writeC(raw[3] & 0xff);
        writeD(server.port);
        writeC(server.ageLimit);
        writeC(server.pvp ? 0x01 : 0x00);
        writeH(server.online);
        writeH(server.maxPlayers);
        writeC(server.status ? 0x01 : 0x00);
        writeD(server.type);
        writeC(server.brackets ? 0x01 : 0x00);
    }
    writeH(0x00);
    writeC(_servers.size());
    for (ServerData server : _servers) {
        writeC(server.serverId);
        writeC(server.playerSize);
        writeC(server.deleteChars.length);
        for (int t : server.deleteChars) {
            writeD((int) (t - (System.currentTimeMillis() / 1000L)));
        }
    }
}

From source file:com.lucid.touchstone.data.LineDocMaker.java

public LineDocMaker(String fileName, StreamProvider stream) {
    try {/*from  w w w  .  java 2s  . c o  m*/
        InetAddress addr = InetAddress.getLocalHost();

        // Get IP Address
        ipAddr = addr.getAddress();
        this.fileName = fileName;
        this.streamProvider = stream;

        open();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.topology.bgp_ls.config.nodes.impl.PeerConfigurationParser.java

public PeerConfiguration parseConfiguration(HierarchicalConfiguration config) throws ConfigurationException {
    PeerConfigurationImpl peerConfig = new PeerConfigurationImpl();
    List<HierarchicalConfiguration> clientConfigs = config.configurationsAt("Client");
    List<HierarchicalConfiguration> capabilityConfigs = config.configurationsAt("Capabilities");

    try {/*from  ww  w .j  av  a 2s .c  om*/
        peerConfig.setPeerName(config.getString("[@name]"));
    } catch (NoSuchElementException e) {
        throw new ConfigurationException("peer name not set", e);
    }

    if (clientConfigs.size() > 1) {
        throw new ConfigurationException("duplicate <Client/> element");
    } else if (clientConfigs.size() == 0) {
        throw new ConfigurationException("missing <Client/> element");
    } else
        peerConfig.setClientConfig(clientConfigurationParser.parseConfig(clientConfigs.get(0)));

    if (capabilityConfigs.size() > 1) {
        throw new ConfigurationException("duplicate <Capabilities/> element");
    } else if (capabilityConfigs.size() == 1)
        peerConfig.setCapabilities(capabilityParser.parseConfig(capabilityConfigs.get(0)));

    try {
        peerConfig.setLocalAS(config.getInt("AutonomousSystem[@local]"));
    } catch (NoSuchElementException e) {
        throw new ConfigurationException("local AS number not given", e);
    }
    try {
        peerConfig.setRemoteAS(config.getInt("AutonomousSystem[@remote]"));
    } catch (NoSuchElementException e) {
        throw new ConfigurationException("remote AS number not given", e);
    }

    try {
        long identifier = config.getLong("BgpIdentifier[@local]");

        if (!isValidBgpIdentifier(identifier))
            throw new ConfigurationException("Invalid local BGP identifier: " + identifier);

        peerConfig.setLocalBgpIdentifier(identifier);
    } catch (NoSuchElementException e) {
        throw new ConfigurationException("local BGP identifier not given", e);
    }
    try {
        long identifier;
        String idString = config.getString("BgpIdentifier[@remote]");
        try {
            InetAddress addr = Inet4Address.getByName(idString);
            byte[] idArray = addr.getAddress();
            identifier = ((long) idArray[3] & 0xFF) | (((long) idArray[2] & 0xFF) << 8)
                    | (((long) idArray[1] & 0xFF) << 16) | (((long) idArray[0] & 0xFF) << 24);

        } catch (Exception e) {
            identifier = Long.parseLong(idString);
        }

        if (!isValidBgpIdentifier(identifier))
            throw new ConfigurationException("Invalid remote BGP identifier: " + identifier);

        peerConfig.setRemoteBgpIdentifier(identifier);
    } catch (NoSuchElementException e) {
        throw new ConfigurationException("remote BGP identifier not given", e);
    }
    peerConfig.setHoldTime(config.getInt("Timers[@holdTime]", 0));
    peerConfig.setIdleHoldTime(config.getInt("Timers[@idleHoldTime]", 0));
    peerConfig.setDelayOpenTime(config.getInt("Timers[@delayOpenTime]", 0));
    peerConfig.setConnectRetryTime(config.getInt("Timers[@connectRetryTime]", 0));
    peerConfig.setAutomaticStartInterval(config.getInt("Timers[@automaticStartInterval]", 0));

    peerConfig.setAllowAutomaticStart(config.getBoolean("Options[@allowAutomaticStart]", true));
    peerConfig.setAllowAutomaticStop(config.getBoolean("Options[@allowAutomaticStop]", false));
    peerConfig.setDampPeerOscillation(config.getBoolean("Options[@dampPeerOscillation]", false));
    peerConfig.setCollisionDetectEstablishedState(
            config.getBoolean("Options[@collisionDetectEstablishedState]", false));
    peerConfig.setDelayOpen(config.getBoolean("Options[@delayOpen]", false));
    peerConfig.setPassiveTcpEstablishment(config.getBoolean("Options[@passiveTcpEstablishment]", false));
    peerConfig.setHoldTimerDisabled(config.getBoolean("Options[@holdTimerDisabled]", false));

    return peerConfig;
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

public String readIp4ApAddress() {
    try {//from   w w w  .  ja  v  a 2 s. c o m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if (intf.getName().contains("wlan")) {
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
                        mLog.debug("found AP address [" + inetAddress.getHostAddress() + "]");
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        mLog.debug("failed to read ip address in access point mode");
    }
    return null;
}