Example usage for com.google.common.net InetAddresses forString

List of usage examples for com.google.common.net InetAddresses forString

Introduction

In this page you can find the example usage for com.google.common.net InetAddresses forString.

Prototype

public static InetAddress forString(String ipString) 

Source Link

Document

Returns the InetAddress having the given string representation.

Usage

From source file:org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.java

public static Action nxLoadTunIPv4Action(String ipAddress, boolean groupBucket) {
    int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
    long ipl = ip & 0xffffffffL;
    return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
            BigInteger.valueOf(ipl), 31, groupBucket);
}

From source file:org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.java

public static Action nxLoadArpSpaAction(String ipAddress) {
    int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
    long ipl = ip & 0xffffffffL;
    return nxLoadArpSpaAction(BigInteger.valueOf(ipl));
}

From source file:org.opendaylight.sfc.ofrenderer.openflow.SfcOfFlowProgrammerImpl.java

/**
 * Configure the NshVxgpe NSH Next Hop by matching on the NSH pathId and
 * index stored in the NSH header./* ww w .j ava2  s.c o  m*/
 *
 * @param sffNodeName
 *            - the SFF to write the flow to
 * @param dstIp
 *            - the VxLan GPE tunnel destination IP
 * @param nshNsp
 *            - NSH Service Path to match on
 * @param nshNsi
 *            - NSH Index to match on
 */
@Override
public void configureNshVxgpeNextHopFlow(final String sffNodeName, final String dstIp, final String nshProxyIp,
        final long nshNsp, final short nshNsi) {
    MatchBuilder match = SfcOpenflowUtils.getNshMatches(nshNsp, nshNsi);

    List<Action> actionList = new ArrayList<>();
    if (nshProxyIp != null) {
        Action actionSetNwDst = SfcOpenflowUtils.createActionNxSetTunIpv4Dst(nshProxyIp, actionList.size());
        actionList.add(actionSetNwDst);
        if (dstIp != null) {
            int ip = InetAddresses.coerceToInteger(InetAddresses.forString(dstIp));
            long ipl = ip & 0xffffffffL;
            Action actionSetNshC3 = SfcOpenflowUtils.createActionNxLoadNshc3(ipl, actionList.size());
            actionList.add(actionSetNshC3);
        }
    } else if (dstIp != null) {
        Action actionSetNwDst = SfcOpenflowUtils.createActionNxSetTunIpv4Dst(dstIp, actionList.size());
        actionList.add(actionSetNwDst);
    }

    FlowBuilder nextHopFlow = configureNextHopFlow(match, actionList);
    sfcOfFlowWriter.writeFlow(flowRspId, sffNodeName, nextHopFlow);
}

From source file:org.neoscoinj.core.PeerGroup.java

private boolean maybeCheckForLocalhostPeer() {
    checkState(lock.isHeldByCurrentThread());
    if (localhostCheckState == LocalhostCheckState.NOT_TRIED) {
        // Do a fast blocking connect to see if anything is listening.
        try {// ww  w.  j a v  a 2  s .  co  m
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), params.getPort()),
                    vConnectTimeoutMillis);
            localhostCheckState = LocalhostCheckState.FOUND;
            try {
                socket.close();
            } catch (IOException e) {
                // Ignore.
            }
            return true;
        } catch (IOException e) {
            log.info("Localhost peer not detected.");
            localhostCheckState = LocalhostCheckState.NOT_THERE;
        }
    }
    return false;
}

From source file:net.yacy.grid.tools.Domains.java

/**
 * resolve a host address using a local DNS cache and a DNS lookup if necessary
 * @param clienthost/*from   w  w  w. j  av  a2 s  .co  m*/
 * @return the hosts InetAddress or null if the address cannot be resolved
 */
public static InetAddress dnsResolve(final String host0) {
    // consider to call stripToHostName() before calling this
    if (host0 == null || host0.isEmpty())
        return null;
    final String host = host0.toLowerCase().trim();

    if (host0.endsWith(".yacyh")) {
        // that should not happen here
        return null;
    }

    // try to resolve host by doing a name cache lookup
    InetAddress ip = NAME_CACHE_HIT.get(host);
    if (ip != null) {
        //System.out.println("DNSLOOKUP-CACHE-HIT(CONC) " + host);
        cacheHit_Hit++;
        return ip;
    }
    cacheHit_Miss++;
    if (NAME_CACHE_MISS.containsKey(host)) {
        //System.out.println("DNSLOOKUP-CACHE-MISS(CONC) " + host);
        cacheMiss_Hit++;
        return null;
    }
    cacheMiss_Miss++;

    // call dnsResolveNetBased(host) using concurrency to interrupt execution in case of a time-out
    final Object sync_obj_new = new Object();
    Object sync_obj = LOOKUP_SYNC.putIfAbsent(host, sync_obj_new);
    if (sync_obj == null)
        sync_obj = sync_obj_new;
    synchronized (sync_obj) {
        // now look again if the host is in the cache where it may be meanwhile because of the synchronization

        ip = NAME_CACHE_HIT.get(host);
        if (ip != null) {
            //System.out.println("DNSLOOKUP-CACHE-HIT(SYNC) " + host);
            LOOKUP_SYNC.remove(host);
            cacheHit_Hit++;
            return ip;
        }
        cacheHit_Miss++;
        if (NAME_CACHE_MISS.containsKey(host)) {
            //System.out.println("DNSLOOKUP-CACHE-MISS(SYNC) " + host);
            LOOKUP_SYNC.remove(host);
            cacheMiss_Hit++;
            return null;
        }
        cacheMiss_Miss++;

        // do the dns lookup on the dns server
        //if (!matchesList(host, nameCacheNoCachingPatterns)) System.out.println("DNSLOOKUP " + host);
        try {
            //final long t = System.currentTimeMillis();
            String oldName = Thread.currentThread().getName();
            Thread.currentThread().setName("Domains: DNS resolve of '" + host + "'"); // thread dump show which host is resolved
            if (InetAddresses.isInetAddress(host)) {
                try {
                    ip = InetAddresses.forString(host);
                } catch (final IllegalArgumentException e) {
                    ip = null;
                }
            }
            Thread.currentThread().setName(oldName);
            if (ip == null)
                try {
                    ip = InetAddress.getByName(host);
                    //ip = TimeoutRequest.getByName(host, 1000); // this makes the DNS request to backbone
                } catch (final UncheckedTimeoutException e) {
                    // in case of a timeout - maybe cause of massive requests - do not fill NAME_CACHE_MISS
                    LOOKUP_SYNC.remove(host);
                    return null;
                }
            //.out.println("DNSLOOKUP-*LOOKUP* " + host + ", time = " + (System.currentTimeMillis() - t) + "ms");
        } catch (final Throwable e) {
            // add new entries
            NAME_CACHE_MISS.insertIfAbsent(host, PRESENT);
            cacheMiss_Insert++;
            LOOKUP_SYNC.remove(host);
            return null;
        }

        if (ip == null) {
            // add new entries
            NAME_CACHE_MISS.insertIfAbsent(host, PRESENT);
            cacheMiss_Insert++;
            LOOKUP_SYNC.remove(host);
            return null;
        }

        if (!ip.isLoopbackAddress() && !matchesList(host, nameCacheNoCachingPatterns)) {
            // add new ip cache entries
            NAME_CACHE_HIT.insertIfAbsent(host, ip);
            cacheHit_Insert++;

            // add also the isLocal host name caches
            final boolean localp = ip.isAnyLocalAddress() || ip.isLinkLocalAddress() || ip.isSiteLocalAddress();
            if (!localp) {
                if (globalHosts != null)
                    try {
                        globalHosts.add(host);
                    } catch (final IOException e) {
                    }
            }
        }
        LOOKUP_SYNC.remove(host);
        return ip;
    }
}

From source file:org.opendaylight.sfc.l2renderer.openflow.SfcL2FlowProgrammerOFimpl.java

/**
 * This flow assures that traffic will be resubmitted from last SFF to egress table (classifier) when coexistence
 * is allowed. Otherwise, this flow is not necessary to create.
 *
 * @param sffNodeName - the SFF to write the flow to
 * @param nshNsp - the NSH Service Path to match on
 * @param nshNsi - the NSH Service Index to match on
 *///  w ww. ja  v  a2 s .co m
@Override
public void configureVxlanGpeLastHopAppCoexistTransportEgressFlow(String sffNodeName, long nshNsp, short nshNsi,
        String sffIp) {
    //
    if (getTableEgress() == APP_COEXISTENCE_NOT_SET) {
        LOG.debug("configureVxlanGpeAppCoexistTransportEgressFlow NO AppCoexistence configured, skipping flow");
        return;
    }

    int ip = InetAddresses.coerceToInteger(InetAddresses.forString(sffIp));
    long ipl = ip & 0xffffffffL;

    MatchBuilder match = new MatchBuilder();
    SfcOpenflowUtils.addMatchNshNsp(match, nshNsp);
    SfcOpenflowUtils.addMatchNshNsi(match, nshNsi);
    SfcOpenflowUtils.addMatchNshNsc1(match, ipl);

    int order = 0;
    List<Action> actionList = new ArrayList<>();
    actionList.add(SfcOpenflowUtils.createActionNxMoveNsi(order++));
    actionList.add(SfcOpenflowUtils.createActionNxMoveNsp(order++));
    actionList.add(SfcOpenflowUtils.createActionNxMoveNsc1ToTunIpv4DstRegister(order++));
    actionList.add(SfcOpenflowUtils.createActionNxMoveNsc2ToTunIdRegister(order++));
    actionList.add(SfcOpenflowUtils.createActionNxSetNshc1(0L, order++));
    actionList.add(SfcOpenflowUtils.createActionResubmitTable(getTableEgress(), order++));

    Long vxlanOfPort = SfcOvsUtil.getVxlanOfPort(sffNodeName);
    MatchBuilder ofpMatch = new MatchBuilder(match.build())
            .setInPort(new NodeConnectorId(vxlanOfPort.toString()));
    String port = OutputPortValues.INPORT.toString();

    FlowBuilder transportEgressFlow = configureTransportEgressFlow(ofpMatch, actionList, port, order,
            FLOW_PRIORITY_TRANSPORT_EGRESS + 11);
    sfcL2FlowWriter.writeFlowToConfig(flowRspId, sffNodeName, transportEgressFlow);
}

From source file:org.opendaylight.sfc.util.openflow.SfcOpenflowUtils.java

public static Action createActionNxSetTunIpv4Dst(String ipStr, int order) {
    int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipStr));
    long ipl = ip & 0xffffffffL;
    ActionBuilder ab = createActionBuilder(order);
    ab.setAction(nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
            BigInteger.valueOf(ipl), 31, false));

    return ab.build();
}

From source file:net.yacy.cora.protocol.Domains.java

/**
 * resolve a host address using a local DNS cache and a DNS lookup if necessary
 * @param clienthost/*  w w w .  j  av  a 2 s  .  c om*/
 * @return the hosts InetAddress or null if the address cannot be resolved
 */
public static InetAddress dnsResolve(final String host0) {
    // consider to call stripToHostName() before calling this
    if (host0 == null || host0.isEmpty())
        return null;
    final String host = host0.toLowerCase().trim();

    if (MemoryControl.shortStatus()) {
        NAME_CACHE_HIT.clear();
        NAME_CACHE_MISS.clear();
    }

    if (host0.endsWith(".yacyh")) {
        // that should not happen here
        return null;
    }

    // try to resolve host by doing a name cache lookup
    InetAddress ip = NAME_CACHE_HIT.get(host);
    if (ip != null) {
        //System.out.println("DNSLOOKUP-CACHE-HIT(CONC) " + host);
        cacheHit_Hit++;
        return ip;
    }
    cacheHit_Miss++;
    if (NAME_CACHE_MISS.containsKey(host)) {
        //System.out.println("DNSLOOKUP-CACHE-MISS(CONC) " + host);
        cacheMiss_Hit++;
        return null;
    }
    cacheMiss_Miss++;

    // call dnsResolveNetBased(host) using concurrency to interrupt execution in case of a time-out
    final Object sync_obj_new = new Object();
    Object sync_obj = LOOKUP_SYNC.putIfAbsent(host, sync_obj_new);
    if (sync_obj == null)
        sync_obj = sync_obj_new;
    synchronized (sync_obj) {
        // now look again if the host is in the cache where it may be meanwhile because of the synchronization

        ip = NAME_CACHE_HIT.get(host);
        if (ip != null) {
            //System.out.println("DNSLOOKUP-CACHE-HIT(SYNC) " + host);
            LOOKUP_SYNC.remove(host);
            cacheHit_Hit++;
            return ip;
        }
        cacheHit_Miss++;
        if (NAME_CACHE_MISS.containsKey(host)) {
            //System.out.println("DNSLOOKUP-CACHE-MISS(SYNC) " + host);
            LOOKUP_SYNC.remove(host);
            cacheMiss_Hit++;
            return null;
        }
        cacheMiss_Miss++;

        // do the dns lookup on the dns server
        //if (!matchesList(host, nameCacheNoCachingPatterns)) System.out.println("DNSLOOKUP " + host);
        try {
            //final long t = System.currentTimeMillis();
            String oldName = Thread.currentThread().getName();
            Thread.currentThread().setName("Domains: DNS resolve of '" + host + "'"); // thread dump show which host is resolved
            if (InetAddresses.isInetAddress(host)) {
                try {
                    ip = InetAddresses.forString(host);
                    log.info("using guava for host resolution:" + host);
                } catch (final IllegalArgumentException e) {
                    ip = null;
                }
            }
            Thread.currentThread().setName(oldName);
            if (ip == null)
                try {
                    ip = timeLimiter.callWithTimeout(new Callable<InetAddress>() {
                        @Override
                        public InetAddress call() throws Exception {
                            return InetAddress.getByName(host);
                        }
                    }, 3000L, TimeUnit.MILLISECONDS, false);
                    //ip = TimeoutRequest.getByName(host, 1000); // this makes the DNS request to backbone
                } catch (final UncheckedTimeoutException e) {
                    // in case of a timeout - maybe cause of massive requests - do not fill NAME_CACHE_MISS
                    LOOKUP_SYNC.remove(host);
                    return null;
                }
            //.out.println("DNSLOOKUP-*LOOKUP* " + host + ", time = " + (System.currentTimeMillis() - t) + "ms");
        } catch (final Throwable e) {
            // add new entries
            NAME_CACHE_MISS.insertIfAbsent(host, PRESENT);
            cacheMiss_Insert++;
            LOOKUP_SYNC.remove(host);
            return null;
        }

        if (ip == null) {
            // add new entries
            NAME_CACHE_MISS.insertIfAbsent(host, PRESENT);
            cacheMiss_Insert++;
            LOOKUP_SYNC.remove(host);
            return null;
        }

        if (!ip.isLoopbackAddress() && !matchesList(host, nameCacheNoCachingPatterns)) {
            // add new ip cache entries
            NAME_CACHE_HIT.insertIfAbsent(host, ip);
            cacheHit_Insert++;

            // add also the isLocal host name caches
            final boolean localp = ip.isAnyLocalAddress() || ip.isLinkLocalAddress() || ip.isSiteLocalAddress();
            if (!localp) {
                if (globalHosts != null)
                    try {
                        globalHosts.add(host);
                    } catch (final IOException e) {
                    }
            }
        }
        LOOKUP_SYNC.remove(host);
        return ip;
    }
}

From source file:org.opendaylight.sxp.core.SxpNode.java

/**
 * Start SxpNode//from  w ww. j  av a  2s.  c o m
 */
public synchronized SxpNode start() {
    channelInitializationWait("Error while starting");
    if (isEnabled()) {
        return this;
    }
    this.sourceIp = InetAddresses.forString(Search.getAddress(getNodeIdentity().getSourceIp()));
    final SxpNode node = this;
    try {
        ConnectFacade.createServer(node, handlerFactoryServer).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()) {
                    serverChannel = channelFuture.channel();
                    LOG.info(node + " Server created [" + getSourceIp().getHostAddress() + ":" + getServerPort()
                            + "]");
                    node.setTimer(TimerType.RetryOpenTimer, node.getRetryOpenTime());
                } else {
                    LOG.error(node + " Server [" + node.getSourceIp().getHostAddress() + ":" + getServerPort()
                            + "] Could not be created " + channelFuture.cause());
                }
                serverChannelInit.set(false);
            }
        }).syncUninterruptibly();
    } catch (Exception e) {
        LOG.debug("Failed to bind SxpNode {} to ip", this, e);
    }
    return this;
}

From source file:org.bitcoinj_extra.core.PeerGroup.java

private boolean maybeCheckForLocalhostPeer() {
    checkState(lock.isHeldByCurrentThread());
    if (localhostCheckState == LocalhostCheckState.NOT_TRIED) {
        // Do a fast blocking connect to see if anything is listening.
        Socket socket = null;//from   w w  w .  j  ava  2  s.c om
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), params.getPort()),
                    vConnectTimeoutMillis);
            localhostCheckState = LocalhostCheckState.FOUND;
            return true;
        } catch (IOException e) {
            log.info("Localhost peer not detected.");
            localhostCheckState = LocalhostCheckState.NOT_THERE;
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }
        }
    }
    return false;
}