Example usage for java.net InetAddress isLoopbackAddress

List of usage examples for java.net InetAddress isLoopbackAddress

Introduction

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

Prototype

public boolean isLoopbackAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a loopback address.

Usage

From source file:org.restcomm.connect.telephony.CallManager.java

public void bye(final Object message) throws IOException {
    final ActorRef self = self();
    final SipServletRequest request = (SipServletRequest) message;
    final SipApplicationSession application = request.getApplicationSession();

    // if this response is coming from a client that is in a p2p session with another registered client
    // we will just proxy the response
    SipSession linkedB2BUASession = B2BUAHelper.getLinkedSession(request);
    if (linkedB2BUASession != null) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("B2BUA: Got BYE request: \n %s", request));
        }//from www . ja  va2 s .  c  o  m

        //Prepare the BYE request to the linked session
        request.getSession().setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, request);
        SipServletRequest clonedBye = linkedB2BUASession.createRequest("BYE");
        linkedB2BUASession.setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, clonedBye);

        if (patchForNatB2BUASessions) {
            // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307
            SipURI toInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.TO_INET_URI);
            SipURI fromInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.FROM_INET_URI);
            InetAddress byeRURI = null;
            try {
                byeRURI = InetAddress.getByName(((SipURI) clonedBye.getRequestURI()).getHost());
            } catch (UnknownHostException e) {
            }
            boolean isBehindLB = false;
            final String initialIpBeforeLB = request.getHeader("X-Sip-Balancer-InitialRemoteAddr");
            String initialPortBeforeLB = request.getHeader("X-Sip-Balancer-InitialRemotePort");
            if (initialIpBeforeLB != null) {
                if (initialPortBeforeLB == null)
                    initialPortBeforeLB = "5060";
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "We are behind load balancer, checking if the request URI needs to be patched");
                }
                isBehindLB = true;
            }
            if (logger.isDebugEnabled()) {
                logger.debug("toInetUri: " + toInetUri + " fromInetUri: " + fromInetUri + " byeRURI: " + byeRURI
                        + " initialIpBeforeLB: " + initialIpBeforeLB + " initialPortBeforeLB: "
                        + initialPortBeforeLB);
            }
            if (toInetUri != null && byeRURI == null) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real To inet ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneBye request");
                }
                clonedBye.setRequestURI(toInetUri);
            } else if (toInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress()
                    || byeRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real To inet ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneBye request");
                }
                clonedBye.setRequestURI(toInetUri);
            } else if (fromInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress()
                    || byeRURI.isLoopbackAddress())) {
                if (isBehindLB) {
                    // https://github.com/RestComm/Restcomm-Connect/issues/1357
                    boolean patchRURI = isLBPatchRURI(clonedBye, initialIpBeforeLB, initialPortBeforeLB);
                    if (patchRURI) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "We are behind load balancer, but Using the real ip address of the sip client "
                                            + fromInetUri.toString()
                                            + " as a request uri of the CloneBye request");
                        }
                        clonedBye.setRequestURI(fromInetUri);
                    }
                } else {
                    if (logger.isInfoEnabled()) {
                        logger.info("Using the real From inet ip  address of the sip client "
                                + fromInetUri.toString() + " as a request uri of the CloneBye request");
                    }
                    clonedBye.setRequestURI(fromInetUri);
                }
            } else if (toInetUri == null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress()
                    || byeRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info(
                            "Public IP toInetUri from SipSession is null, will check LB headers from last Response");
                }
                if (isBehindLB) {
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = sipFactory.createSipURI(null, realIP);
                    boolean patchRURI = isLBPatchRURI(clonedBye, initialIpBeforeLB, initialPortBeforeLB);
                    if (patchRURI) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("We are behind load balancer, will use: " + initialIpBeforeLB + ":"
                                    + initialPortBeforeLB + " for the cloned BYE message");
                        }
                        clonedBye.setRequestURI(uri);
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info("We are behind load balancer, will use Initial Remote Address "
                                + initialIpBeforeLB + ":" + initialPortBeforeLB
                                + " for the cloned BYE request");
                    }
                } else {
                    if (logger.isInfoEnabled()) {
                        logger.info("LB Headers are also null");
                    }
                }
            }
        }
        B2BUAHelper.updateCDR(request, CallStateChanged.State.COMPLETED);
        //Prepare 200 OK for received BYE
        SipServletResponse okay = request.createResponse(Response.OK);
        okay.send();
        //Send the Cloned BYE
        if (logger.isInfoEnabled()) {
            logger.info(String.format("B2BUA: Will send out Cloned BYE request: \n %s", clonedBye));
        }
        clonedBye.send();
    } else {
        final ActorRef call = (ActorRef) application.getAttribute(Call.class.getName());
        if (call != null)
            call.tell(request, self);
    }
}

From source file:org.wso2.carbon.apimgt.impl.utils.APIUtil.java

private static InetAddress getLocalAddress() {
    Enumeration<NetworkInterface> ifaces = null;
    try {//  www. j a v a 2s.c  om
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        log.error("Failed to get host address", e);
    }
    if (ifaces != null) {
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();

            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
    }

    return null;
}

From source file:org.mobicents.servlet.restcomm.telephony.Call.java

private void sendBye(Hangup hangup) throws IOException, TransitionNotFoundException, TransitionFailedException,
        TransitionRollbackException {/*from  w w  w . j a v  a  2  s.  c o m*/
    final SipSession session = invite.getSession();
    String sessionState = session.getState().name();
    if (sessionState == SipSession.State.INITIAL.name()
            || (sessionState == SipSession.State.EARLY.name() && isInbound())) {
        final SipServletResponse resp = invite.createResponse(Response.SERVER_INTERNAL_ERROR);
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            resp.addHeader("Reason", hangup.getMessage());
        }
        addCustomHeaders(resp);
        resp.send();
        fsm.transition(hangup, completed);
        return;
    }
    if (sessionState == SipSession.State.EARLY.name()) {
        final SipServletRequest cancel = invite.createCancel();
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            cancel.addHeader("Reason", hangup.getMessage());
        }
        addCustomHeaders(cancel);
        cancel.send();
        fsm.transition(hangup, completed);
        return;
    } else {
        final SipServletRequest bye = session.createRequest("BYE");
        addCustomHeaders(bye);
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            bye.addHeader("Reason", hangup.getMessage());
        }
        SipURI realInetUri = (SipURI) session.getAttribute("realInetUri");
        InetAddress byeRURI = InetAddress.getByName(((SipURI) bye.getRequestURI()).getHost());

        // INVITE sip:+12055305520@107.21.247.251 SIP/2.0
        // Record-Route: <sip:10.154.28.245:5065;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0>
        // Record-Route: <sip:10.154.28.245:5060;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0>
        // Record-Route: <sip:67.231.8.195;lr=on;ftag=gK0043eb81>
        // Record-Route: <sip:67.231.4.204;r2=on;lr=on;ftag=gK0043eb81>
        // Record-Route: <sip:192.168.6.219;r2=on;lr=on;ftag=gK0043eb81>
        // Accept: application/sdp
        // Allow: INVITE,ACK,CANCEL,BYE
        // Via: SIP/2.0/UDP 10.154.28.245:5065;branch=z9hG4bK1cdb.193075b2.058724zsd_0
        // Via: SIP/2.0/UDP 10.154.28.245:5060;branch=z9hG4bK1cdb.193075b2.058724_0
        // Via: SIP/2.0/UDP 67.231.8.195;branch=z9hG4bK1cdb.193075b2.0
        // Via: SIP/2.0/UDP 67.231.4.204;branch=z9hG4bK1cdb.f9127375.0
        // Via: SIP/2.0/UDP 192.168.16.114:5060;branch=z9hG4bK00B6ff7ff87ed50497f
        // From: <sip:+1302109762259@192.168.16.114>;tag=gK0043eb81
        // To: <sip:12055305520@192.168.6.219>
        // Call-ID: 587241765_133360558@192.168.16.114
        // CSeq: 393447729 INVITE
        // Max-Forwards: 67
        // Contact: <sip:+1302109762259@192.168.16.114:5060>
        // Diversion: <sip:+112055305520@192.168.16.114:5060>;privacy=off;screen=no; reason=unknown; counter=1
        // Supported: replaces
        // Content-Disposition: session;handling=required
        // Content-Type: application/sdp
        // Remote-Party-ID: <sip:+1302109762259@192.168.16.114:5060>;privacy=off;screen=no
        // X-Sip-Balancer-InitialRemoteAddr: 67.231.8.195
        // X-Sip-Balancer-InitialRemotePort: 5060
        // Route: <sip:10.13.169.214:5080;transport=udp;lr>
        // Content-Length: 340

        ListIterator<String> recordRouteList = invite.getHeaders(RecordRouteHeader.NAME);

        if (invite.getHeader("X-Sip-Balancer-InitialRemoteAddr") != null) {
            if (logger.isInfoEnabled()) {
                logger.info(
                        "We are behind LoadBalancer and will remove the first two RecordRoutes since they are the LB node");
            }
            recordRouteList.next();
            recordRouteList.remove();
            recordRouteList.next();
            recordRouteList.remove();
        }
        if (recordRouteList.hasNext()) {
            if (logger.isInfoEnabled()) {
                logger.info("Record Route is set, wont change the Request URI");
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("Checking RURI, realInetUri: " + realInetUri + " byeRURI: " + byeRURI);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("byeRURI.isSiteLocalAddress(): " + byeRURI.isSiteLocalAddress());
                logger.debug("byeRURI.isAnyLocalAddress(): " + byeRURI.isAnyLocalAddress());
                logger.debug("byeRURI.isLoopbackAddress(): " + byeRURI.isLoopbackAddress());
            }
            if (realInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress()
                    || byeRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + realInetUri.toString()
                            + " as a request uri of the BYE request");
                }
                bye.setRequestURI(realInetUri);
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("Will sent out BYE to: " + bye.getRequestURI());
        }
        bye.send();
    }
}

From source file:org.restcomm.connect.telephony.CallManager.java

private void ack(SipServletRequest request) throws IOException {
    SipServletResponse response = B2BUAHelper.getLinkedResponse(request);
    // if this is an ACK that belongs to a B2BUA session, then we proxy it to the other client
    if (response != null) {
        SipServletRequest ack = response.createAck();
        //            if (!ack.getHeaders("Route").hasNext() && patchForNatB2BUASessions) {
        if (patchForNatB2BUASessions) {
            InetAddress ackRURI = null;
            try {
                ackRURI = InetAddress.getByName(((SipURI) ack.getRequestURI()).getHost());
            } catch (UnknownHostException e) {
            }/*from www  .j  a  va2  s  . c o  m*/
            boolean isBehindLB = false;
            final String initialIpBeforeLB = response.getHeader("X-Sip-Balancer-InitialRemoteAddr");
            String initialPortBeforeLB = response.getHeader("X-Sip-Balancer-InitialRemotePort");
            if (initialIpBeforeLB != null) {
                if (initialPortBeforeLB == null)
                    initialPortBeforeLB = "5060";
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "We are behind load balancer, checking if the request URI needs to be patched");
                }
                isBehindLB = true;
            }
            // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307
            SipURI toInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.TO_INET_URI);
            if (toInetUri != null && ackRURI == null) {
                if (isBehindLB) {
                    // https://github.com/RestComm/Restcomm-Connect/issues/1357
                    boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB);
                    if (patchRURI) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "We are behind load balancer, but Using the real ip address of the sip client "
                                            + toInetUri.toString() + " as a request uri of the ACK request");
                        }
                        ack.setRequestURI(toInetUri);
                    } else {
                        // https://github.com/RestComm/Restcomm-Connect/issues/1357
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "removing the toInetUri to avoid the other subsequent requests using it "
                                            + toInetUri.toString());
                        }
                        request.getSession().removeAttribute(B2BUAHelper.TO_INET_URI);
                    }
                } else {
                    if (logger.isInfoEnabled()) {
                        logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                                + " as a request uri of the ACK request");
                    }
                    ack.setRequestURI(toInetUri);
                }
            } else if (toInetUri != null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress()
                    || ackRURI.isLoopbackAddress())) {
                if (isBehindLB) {
                    // https://github.com/RestComm/Restcomm-Connect/issues/1357
                    boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB);
                    if (patchRURI) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "We are behind load balancer, but Using the real ip address of the sip client "
                                            + toInetUri.toString() + " as a request uri of the ACK request");
                        }
                        ack.setRequestURI(toInetUri);
                    } else {
                        // https://github.com/RestComm/Restcomm-Connect/issues/1357
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "removing the toInetUri to avoid the other subsequent requests using it "
                                            + toInetUri.toString());
                        }
                        request.getSession().removeAttribute(B2BUAHelper.TO_INET_URI);
                    }
                } else {
                    if (logger.isInfoEnabled()) {
                        logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                                + " as a request uri of the ACK request");
                    }
                    ack.setRequestURI(toInetUri);
                }
            } else if (toInetUri == null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress()
                    || ackRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info(
                            "Public IP toInetUri from SipSession is null, will check LB headers from last Response");
                }
                if (isBehindLB) {
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = sipFactory.createSipURI(null, realIP);
                    boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB);
                    if (patchRURI) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("We are behind load balancer, will use Initial Remote Address "
                                    + initialIpBeforeLB + ":" + initialPortBeforeLB + " for the ACK request");
                        }
                        ack.setRequestURI(uri);
                    }
                } else {
                    if (logger.isInfoEnabled()) {
                        logger.info("LB Headers are also null");
                    }
                }
            }
        }
        ack.send();
        SipApplicationSession sipApplicationSession = request.getApplicationSession();
        // Defaulting the sip application session to 1h
        sipApplicationSession.setExpires(60);
    } else {
        if (logger.isInfoEnabled()) {
            logger.info("Linked Response couldn't be found for ACK request");
        }
        final ActorRef call = (ActorRef) request.getApplicationSession().getAttribute(Call.class.getName());
        if (call != null) {
            if (logger.isInfoEnabled()) {
                logger.info("Will send ACK to call actor: " + call.path());
            }
            call.tell(request, self());
        }
    }
    // else {
    // SipSession sipSession = request.getSession();
    // SipApplicationSession sipAppSession = request.getApplicationSession();
    // if(sipSession.getInvalidateWhenReady()){
    // logger.info("Invalidating sipSession: "+sipSession.getId());
    // sipSession.invalidate();
    // }
    // if(sipAppSession.getInvalidateWhenReady()){
    // logger.info("Invalidating sipAppSession: "+sipAppSession.getId());
    // sipAppSession.invalidate();
    // }
    // }
}

From source file:org.restcomm.connect.telephony.Call.java

private void sendBye(Hangup hangup) throws IOException, TransitionNotFoundException, TransitionFailedException,
        TransitionRollbackException {/*  w  ww.  jav  a 2 s . co m*/
    final SipSession session = invite.getSession();
    final String sessionState = session.getState().name();
    if (sessionState == SipSession.State.TERMINATED.name()) {
        if (logger.isInfoEnabled()) {
            logger.info("SipSession already TERMINATED, will not send BYE");
        }
        return;
    } else {
        if (logger.isInfoEnabled()) {
            logger.info("About to send BYE, session state: " + sessionState);
        }
    }
    if (sessionState == SipSession.State.INITIAL.name()
            || (sessionState == SipSession.State.EARLY.name() && isInbound())) {
        int sipResponse = (enable200OkDelay && hangup.getSipResponse() != null) ? hangup.getSipResponse()
                : Response.SERVER_INTERNAL_ERROR;
        final SipServletResponse resp = invite.createResponse(sipResponse);
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            resp.addHeader("Reason", hangup.getMessage());
        }
        addCustomHeaders(resp);
        resp.send();
        fsm.transition(hangup, completed);
        return;
    }
    if (sessionState == SipSession.State.EARLY.name()) {
        final SipServletRequest cancel = invite.createCancel();
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            cancel.addHeader("Reason", hangup.getMessage());
        }
        addCustomHeaders(cancel);
        cancel.send();
        external = CallStateChanged.State.CANCELED;
        fsm.transition(hangup, completed);
        return;
    } else {
        final SipServletRequest bye = session.createRequest("BYE");
        addCustomHeaders(bye);
        if (hangup.getMessage() != null && !hangup.getMessage().equals("")) {
            bye.addHeader("Reason", hangup.getMessage());
        }
        SipURI realInetUri = (SipURI) session.getAttribute("realInetUri");
        InetAddress byeRURI = InetAddress.getByName(((SipURI) bye.getRequestURI()).getHost());

        // INVITE sip:+12055305520@107.21.247.251 SIP/2.0
        // Record-Route: <sip:10.154.28.245:5065;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0>
        // Record-Route: <sip:10.154.28.245:5060;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0>
        // Record-Route: <sip:67.231.8.195;lr=on;ftag=gK0043eb81>
        // Record-Route: <sip:67.231.4.204;r2=on;lr=on;ftag=gK0043eb81>
        // Record-Route: <sip:192.168.6.219;r2=on;lr=on;ftag=gK0043eb81>
        // Accept: application/sdp
        // Allow: INVITE,ACK,CANCEL,BYE
        // Via: SIP/2.0/UDP 10.154.28.245:5065;branch=z9hG4bK1cdb.193075b2.058724zsd_0
        // Via: SIP/2.0/UDP 10.154.28.245:5060;branch=z9hG4bK1cdb.193075b2.058724_0
        // Via: SIP/2.0/UDP 67.231.8.195;branch=z9hG4bK1cdb.193075b2.0
        // Via: SIP/2.0/UDP 67.231.4.204;branch=z9hG4bK1cdb.f9127375.0
        // Via: SIP/2.0/UDP 192.168.16.114:5060;branch=z9hG4bK00B6ff7ff87ed50497f
        // From: <sip:+1302109762259@192.168.16.114>;tag=gK0043eb81
        // To: <sip:12055305520@192.168.6.219>
        // Call-ID: 587241765_133360558@192.168.16.114
        // CSeq: 393447729 INVITE
        // Max-Forwards: 67
        // Contact: <sip:+1302109762259@192.168.16.114:5060>
        // Diversion: <sip:+112055305520@192.168.16.114:5060>;privacy=off;screen=no; reason=unknown; counter=1
        // Supported: replaces
        // Content-Disposition: session;handling=required
        // Content-Type: application/sdp
        // Remote-Party-ID: <sip:+1302109762259@192.168.16.114:5060>;privacy=off;screen=no
        // X-Sip-Balancer-InitialRemoteAddr: 67.231.8.195
        // X-Sip-Balancer-InitialRemotePort: 5060
        // Route: <sip:10.13.169.214:5080;transport=udp;lr>
        // Content-Length: 340

        ListIterator<String> recordRouteList = invite.getHeaders(RecordRouteHeader.NAME);

        if (invite.getHeader("X-Sip-Balancer-InitialRemoteAddr") != null) {
            if (logger.isInfoEnabled()) {
                logger.info(
                        "We are behind LoadBalancer and will remove the first two RecordRoutes since they are the LB node");
            }
            recordRouteList.next();
            recordRouteList.remove();
            recordRouteList.next();
            recordRouteList.remove();
        }
        if (recordRouteList.hasNext()) {
            if (logger.isInfoEnabled()) {
                logger.info("Record Route is set, wont change the Request URI");
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("Checking RURI, realInetUri: " + realInetUri + " byeRURI: " + byeRURI);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("byeRURI.isSiteLocalAddress(): " + byeRURI.isSiteLocalAddress());
                logger.debug("byeRURI.isAnyLocalAddress(): " + byeRURI.isAnyLocalAddress());
                logger.debug("byeRURI.isLoopbackAddress(): " + byeRURI.isLoopbackAddress());
            }
            if (realInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress()
                    || byeRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info("real ip address of the sip client " + realInetUri.toString()
                            + " is not null, checking if the request URI needs to be patched");
                }
                boolean patchRURI = true;
                try {
                    // https://github.com/RestComm/Restcomm-Connect/issues/1336 checking if the initial IP and Port behind LB is part of the route set or not
                    ListIterator<? extends Address> routes = bye.getAddressHeaders(RouteHeader.NAME);
                    while (routes.hasNext() && patchRURI) {
                        SipURI route = (SipURI) routes.next().getURI();
                        String routeHost = route.getHost();
                        int routePort = route.getPort();
                        if (routePort < 0) {
                            routePort = 5060;
                        }
                        if (logger.isDebugEnabled()) {
                            logger.debug("Checking if route " + routeHost + ":" + routePort
                                    + " is matching ip and port of realNetURI " + realInetUri.getHost() + ":"
                                    + realInetUri.getPort() + " for the BYE request");
                        }
                        if (routeHost.equalsIgnoreCase(realInetUri.getHost())
                                && routePort == realInetUri.getPort()) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("route " + route + " is matching ip and port of realNetURI "
                                        + realInetUri.getHost() + ":" + realInetUri.getPort()
                                        + " for the BYE request, so not patching the Request-URI");
                            }
                            patchRURI = false;
                        }
                    }
                } catch (ServletParseException e) {
                    logger.error("Impossible to parse the route set from the BYE " + bye, e);
                }
                if (patchRURI) {
                    if (logger.isInfoEnabled()) {
                        logger.info("Using the real ip address of the sip client " + realInetUri.toString()
                                + " as a request uri of the BYE request");
                    }
                    bye.setRequestURI(realInetUri);
                }
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("Will sent out BYE to: " + bye.getRequestURI());
        }
        try {
            bye.send();
            sentBye = true;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Exception during Send Bye: " + e.toString());
            }
        }
    }
}

From source file:carnero.cgeo.cgBase.java

public String getLocalIpAddress() {
    try {//from   w  w  w. j  a v  a2s.  c om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException e) {
        // nothing
    }

    return null;
}

From source file:com.vuze.plugin.azVPN_PIA.Checker.java

private int handleUnboundOrLoopback(InetAddress bindIP, StringBuilder sReply) {

    int newStatusID = STATUS_ID_OK;

    InetAddress newBindIP = null;
    NetworkInterface newBindNetworkInterface = null;

    String s;/*w  ww  . j a va2  s .co  m*/

    if (bindIP.isAnyLocalAddress()) {
        addReply(sReply, CHAR_WARN, "pia.vuze.unbound");
    } else {
        addReply(sReply, CHAR_BAD, "pia.vuze.loopback");
    }

    try {
        NetworkAdmin networkAdmin = NetworkAdmin.getSingleton();

        // Find a bindable address that starts with 10.
        InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses();

        for (InetAddress bindableAddress : bindableAddresses) {
            if (matchesVPNIP(bindableAddress)) {
                newBindIP = bindableAddress;
                newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP);

                addReply(sReply, CHAR_GOOD, "pia.found.bindable.vpn", new String[] { "" + newBindIP });

                break;
            }
        }

        // Find a Network Interface that has an address that starts with 10.
        NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces();

        boolean foundNIF = false;
        for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) {
            NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses();
            for (NetworkAdminNetworkInterfaceAddress a : addresses) {
                InetAddress address = a.getAddress();
                if (address instanceof Inet4Address) {
                    if (matchesVPNIP(address)) {
                        s = texts.getLocalisedMessageText("pia.possible.vpn",
                                new String[] { "" + address, networkAdminInterface.getName() + " ("
                                        + networkAdminInterface.getDisplayName() + ")" });

                        if (newBindIP == null) {
                            foundNIF = true;
                            newBindIP = address;

                            // Either one should work
                            //newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP);
                            newBindNetworkInterface = NetworkInterface
                                    .getByName(networkAdminInterface.getName());

                            s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("pia.assuming.vpn");
                        } else if (address.equals(newBindIP)) {
                            s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("pia.same.address");
                            foundNIF = true;
                        } else {
                            if (newStatusID != STATUS_ID_BAD) {
                                newStatusID = STATUS_ID_WARN;
                            }
                            s = CHAR_WARN + " " + s + ". "
                                    + texts.getLocalisedMessageText("pia.not.same.address");
                        }

                        addLiteralReply(sReply, s);

                        if (rebindNetworkInterface) {
                            // stops message below from being added, we'll rebind later
                            foundNIF = true;
                        }

                    }
                }
            }
        }

        if (!foundNIF) {
            addReply(sReply, CHAR_BAD, "pia.interface.not.found");
        }

        // Check if default routing goes through 10.*, by connecting to address
        // via socket.  Address doesn't need to be reachable, just routable.
        // This works on Windows, but on Mac returns a wildcard address
        DatagramSocket socket = new DatagramSocket();
        socket.connect(testSocketAddress, 0);
        InetAddress localAddress = socket.getLocalAddress();
        socket.close();

        if (!localAddress.isAnyLocalAddress()) {
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress);

            s = texts.getLocalisedMessageText("pia.nonvuze.probable.route",
                    new String[] { "" + localAddress, networkInterface == null ? "null"
                            : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" });

            if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) {

                if (newBindIP == null) {
                    newBindIP = localAddress;
                    newBindNetworkInterface = networkInterface;

                    s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("pia.assuming.vpn");
                } else if (localAddress.equals(newBindIP)) {
                    s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("pia.same.address");
                } else {
                    // Vuze not bound. We already found a boundable address, but it's not this one
                    /* Possibly good case:
                     * - Vuze: unbound
                     * - Found Bindable: 10.100.1.6
                     * - Default Routing: 10.255.1.1
                     * -> Split network
                     */
                    if (newStatusID != STATUS_ID_BAD) {
                        newStatusID = STATUS_ID_WARN;
                    }
                    s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("pia.not.same.future.address")
                            + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting")
                            + " " + texts.getLocalisedMessageText(
                                    "default.routing.not.vpn.network.splitting.unbound");
                }

                addLiteralReply(sReply, s);

            } else {
                s = CHAR_WARN + " " + s;
                if (!bindIP.isLoopbackAddress()) {
                    s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting");
                }

                if (newBindIP == null && foundNIF) {
                    if (newStatusID != STATUS_ID_BAD) {
                        newStatusID = STATUS_ID_WARN;
                    }
                    s += " " + texts
                            .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound");
                }

                addLiteralReply(sReply, s);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        addReply(sReply, CHAR_BAD, "pia.nat.error", new String[] { e.toString() });
    }

    if (newBindIP == null) {
        addReply(sReply, CHAR_BAD, "pia.vpn.ip.detect.fail");
        return STATUS_ID_BAD;
    }

    rebindNetworkInterface(newBindNetworkInterface, newBindIP, sReply);
    return newStatusID;
}

From source file:com.vuze.plugin.azVPN_Air.Checker.java

private int handleUnboundOrLoopback(InetAddress bindIP, StringBuilder sReply) {

    int newStatusID = STATUS_ID_OK;

    InetAddress newBindIP = null;
    NetworkInterface newBindNetworkInterface = null;

    String s;/*www.  j  av a2  s. com*/

    if (bindIP.isAnyLocalAddress()) {
        addReply(sReply, CHAR_WARN, "airvpn.vuze.unbound");
    } else {
        addReply(sReply, CHAR_BAD, "airvpn.vuze.loopback");
    }

    try {
        NetworkAdmin networkAdmin = NetworkAdmin.getSingleton();

        // Find a bindable address that starts with 10.
        InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses();

        for (InetAddress bindableAddress : bindableAddresses) {
            if (matchesVPNIP(bindableAddress)) {
                newBindIP = bindableAddress;
                newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP);

                addReply(sReply, CHAR_GOOD, "airvpn.found.bindable.vpn", new String[] { "" + newBindIP });

                break;
            }
        }

        // Find a Network Interface that has an address that starts with 10.
        NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces();

        boolean foundNIF = false;
        for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) {
            NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses();
            for (NetworkAdminNetworkInterfaceAddress a : addresses) {
                InetAddress address = a.getAddress();
                if (address instanceof Inet4Address) {
                    if (matchesVPNIP(address)) {
                        s = texts.getLocalisedMessageText("airvpn.possible.vpn",
                                new String[] { "" + address, networkAdminInterface.getName() + " ("
                                        + networkAdminInterface.getDisplayName() + ")" });

                        if (newBindIP == null) {
                            foundNIF = true;
                            newBindIP = address;

                            // Either one should work
                            //newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP);
                            newBindNetworkInterface = NetworkInterface
                                    .getByName(networkAdminInterface.getName());

                            s = CHAR_GOOD + " " + s + ". "
                                    + texts.getLocalisedMessageText("airvpn.assuming.vpn");
                        } else if (address.equals(newBindIP)) {
                            s = CHAR_GOOD + " " + s + ". "
                                    + texts.getLocalisedMessageText("airvpn.same.address");
                            foundNIF = true;
                        } else {
                            if (newStatusID != STATUS_ID_BAD) {
                                newStatusID = STATUS_ID_WARN;
                            }
                            s = CHAR_WARN + " " + s + ". "
                                    + texts.getLocalisedMessageText("airvpn.not.same.address");
                        }

                        addLiteralReply(sReply, s);

                        if (rebindNetworkInterface) {
                            // stops message below from being added, we'll rebind later
                            foundNIF = true;
                        }

                    }
                }
            }
        }

        if (!foundNIF) {
            addReply(sReply, CHAR_BAD, "airvpn.interface.not.found");
        }

        // Check if default routing goes through 10.*, by connecting to address
        // via socket.  Address doesn't need to be reachable, just routable.
        // This works on Windows, but on Mac returns a wildcard address
        DatagramSocket socket = new DatagramSocket();
        socket.connect(testSocketAddress, 0);
        InetAddress localAddress = socket.getLocalAddress();
        socket.close();

        if (!localAddress.isAnyLocalAddress()) {
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress);

            s = texts.getLocalisedMessageText("airvpn.nonvuze.probable.route",
                    new String[] { "" + localAddress, networkInterface == null ? "null"
                            : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" });

            if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) {

                if (newBindIP == null) {
                    newBindIP = localAddress;
                    newBindNetworkInterface = networkInterface;

                    s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.assuming.vpn");
                } else if (localAddress.equals(newBindIP)) {
                    s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.same.address");
                } else {
                    // Vuze not bound. We already found a boundable address, but it's not this one
                    /* Possibly good case:
                     * - Vuze: unbound
                     * - Found Bindable: 10.100.1.6
                     * - Default Routing: 10.255.1.1
                     * -> Split network
                     */
                    if (newStatusID != STATUS_ID_BAD) {
                        newStatusID = STATUS_ID_WARN;
                    }
                    s = CHAR_WARN + " " + s + " "
                            + texts.getLocalisedMessageText("airvpn.not.same.future.address") + " "
                            + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " "
                            + texts.getLocalisedMessageText(
                                    "default.routing.not.vpn.network.splitting.unbound");
                }

                addLiteralReply(sReply, s);

            } else {
                s = CHAR_WARN + " " + s;
                if (!bindIP.isLoopbackAddress()) {
                    s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting");
                }

                if (newBindIP == null && foundNIF) {
                    if (newStatusID != STATUS_ID_BAD) {
                        newStatusID = STATUS_ID_WARN;
                    }
                    s += " " + texts
                            .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound");
                }

                addLiteralReply(sReply, s);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        addReply(sReply, CHAR_BAD, "airvpn.nat.error", new String[] { e.toString() });
    }

    if (newBindIP == null) {
        addReply(sReply, CHAR_BAD, "airvpn.vpn.ip.detect.fail");
        return STATUS_ID_BAD;
    }

    rebindNetworkInterface(newBindNetworkInterface, newBindIP, sReply);
    return newStatusID;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public String getHostOrIP() {
    try {/*  w w w.j  a v a 2  s  .  c o  m*/
        InetAddress i = java.net.InetAddress.getLocalHost();
        if (i.isLoopbackAddress()) {
            Enumeration<NetworkInterface> nie = NetworkInterface.getNetworkInterfaces();
            while (nie.hasMoreElements()) {
                NetworkInterface current = nie.nextElement();
                if (!current.isLoopback()) {
                    Enumeration<InetAddress> iae = current.getInetAddresses();
                    while (iae.hasMoreElements()) {
                        InetAddress currentI = iae.nextElement();
                        if (!currentI.isLoopbackAddress()) {
                            return currentI.getHostAddress();
                        }
                    }
                }
            }
        }
        return i.getHostAddress();
    } catch (Throwable t) {
        com.codename1.io.Log.e(t);
        return null;
    }
}