Example usage for java.net InetAddress isSiteLocalAddress

List of usage examples for java.net InetAddress isSiteLocalAddress

Introduction

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

Prototype

public boolean isSiteLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a site local address.

Usage

From source file:com.httrack.android.HTTrackActivity.java

/**
 * Return the IPv6 address.//from w  w  w .  j av a 2  s  .  c o  m
 * 
 * @return The ipv6 address, or @c null if no IPv6 connectivity is available.
 */
protected static InetAddress getIPv6Address() {
    try {
        for (final Enumeration<NetworkInterface> interfaces = NetworkInterface
                .getNetworkInterfaces(); interfaces.hasMoreElements();) {
            final NetworkInterface iface = interfaces.nextElement();
            for (final Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses
                    .hasMoreElements();) {
                final InetAddress address = addresses.nextElement();
                Log.d(HTTrackActivity.class.getSimpleName(), "seen interface: " + address.toString());
                if (address instanceof Inet6Address) {
                    if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()
                            && !address.isSiteLocalAddress() && !address.isMulticastAddress()) {
                        return address;
                    }
                }
            }
        }
    } catch (final SocketException se) {
        Log.w(HTTrackActivity.class.getSimpleName(), "could not enumerate interfaces", se);
    }
    return null;
}

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

private void onSipServletResponse(SipServletResponse message, ActorRef self, ActorRef sender) throws Exception {
    this.lastResponse = message;

    final int code = message.getStatus();
    switch (code) {
    case SipServletResponse.SC_CALL_BEING_FORWARDED: {
        forwarding(message);//  ww  w  .j  a v  a  2  s. c  om
        break;
    }
    case SipServletResponse.SC_RINGING:
    case SipServletResponse.SC_SESSION_PROGRESS: {
        if (!is(ringing)) {
            if (logger.isInfoEnabled()) {
                logger.info("Got 180 Ringing for Call: " + self().path() + " To: " + to + " sender: "
                        + sender.path() + " observers size: " + observers.size());
            }
            fsm.transition(message, ringing);
        }
        break;
    }
    case SipServletResponse.SC_BUSY_HERE:
    case SipServletResponse.SC_BUSY_EVERYWHERE:
    case SipServletResponse.SC_DECLINE: {
        sendCallInfoToObservers();

        //Important. If state is DIALING, then do nothing about the BUSY. If not DIALING state move to failingBusy
        //                // Notify the observers.
        //                external = CallStateChanged.State.BUSY;
        //                final CallStateChanged event = new CallStateChanged(external);
        //                for (final ActorRef observer : observers) {
        //                    observer.tell(event, self);
        //                }

        // XXX shouldnt it move to failingBusy IF dialing ????
        //                if (is(dialing)) {
        //                    break;
        //                } else {
        //                    fsm.transition(message, failingBusy);
        //                }
        fsm.transition(message, failingBusy);
        break;
    }
    case SipServletResponse.SC_UNAUTHORIZED:
    case SipServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED: {
        // Handles Auth for https://bitbucket.org/telestax/telscale-restcomm/issue/132/implement-twilio-sip-out
        if (this.username == null || this.password == null) {
            sendCallInfoToObservers();
            fsm.transition(message, failed);
        } else {
            AuthInfo authInfo = this.factory.createAuthInfo();
            String authHeader = message.getHeader("Proxy-Authenticate");
            if (authHeader == null) {
                authHeader = message.getHeader("WWW-Authenticate");
            }
            String tempRealm = authHeader.substring(authHeader.indexOf("realm=\"") + "realm=\"".length());
            String realm = tempRealm.substring(0, tempRealm.indexOf("\""));
            authInfo.addAuthInfo(message.getStatus(), realm, this.username, this.password);
            SipServletRequest challengeRequest = message.getSession()
                    .createRequest(message.getRequest().getMethod());
            challengeRequest.addAuthHeader(message, authInfo);
            challengeRequest.setContent(this.invite.getContent(), this.invite.getContentType());
            this.invite = challengeRequest;
            // https://github.com/Mobicents/RestComm/issues/147 Make sure we send the SDP again
            this.invite.setContent(message.getRequest().getContent(), "application/sdp");
            challengeRequest.send();
        }
        break;
    }
    // https://github.com/Mobicents/RestComm/issues/148
    // Session in Progress Response should trigger MMS to start the Media Session
    // case SipServletResponse.SC_SESSION_PROGRESS:
    case SipServletResponse.SC_OK: {
        if (is(dialing) || (is(ringing) && !"inbound".equals(direction))) {
            fsm.transition(message, updatingMediaSession);
        }
        break;
    }
    default: {
        if (code >= 400 && code != 487) {
            if (code == 487 && isOutbound()) {
                String initialIpBeforeLB = null;
                String initialPortBeforeLB = null;
                try {
                    initialIpBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemoteAddr");
                    initialPortBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemotePort");
                } catch (Exception e) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Exception during check of LB custom headers for IP address and port");
                    }
                }
                final SipServletRequest ack = message.createAck();
                addCustomHeaders(ack);
                SipSession session = message.getSession();

                if (initialIpBeforeLB != null) {
                    if (initialPortBeforeLB == null)
                        initialPortBeforeLB = "5060";
                    if (logger.isInfoEnabled()) {
                        logger.info("We are behind load balancer, will use: " + initialIpBeforeLB + ":"
                                + initialPortBeforeLB + " for ACK message, ");
                    }
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = factory.createSipURI(null, realIP);
                    ack.setRequestURI(uri);
                } else if (!ack.getHeaders("Route").hasNext()) {
                    final SipServletRequest originalInvite = message.getRequest();
                    final SipURI realInetUri = (SipURI) originalInvite.getRequestURI();
                    if ((SipURI) session.getAttribute("realInetUri") == null) {
                        session.setAttribute("realInetUri", realInetUri);
                    }
                    final InetAddress ackRURI = InetAddress.getByName(((SipURI) ack.getRequestURI()).getHost());
                    final int ackRURIPort = ((SipURI) ack.getRequestURI()).getPort();

                    if (realInetUri != null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress()
                            || ackRURI.isLoopbackAddress()) && (ackRURIPort != realInetUri.getPort())) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Using the real ip address and port of the sip client "
                                    + realInetUri.toString() + " as a request uri of the ACK");
                        }
                        ack.setRequestURI(realInetUri);
                    }
                }
                ack.send();
                if (logger.isInfoEnabled()) {
                    logger.info("Just sent out ACK : " + ack.toString());
                }
            }
            this.fail = true;
            sendCallInfoToObservers();
            fsm.transition(message, stopping);
        }
    }
    }
}

From source file:org.mobicents.servlet.restcomm.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  va  2s .  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("toInetUri");
            SipURI fromInetUri = (SipURI) request.getSession().getAttribute("fromInetUri");
            InetAddress byeRURI = null;
            try {
                byeRURI = InetAddress.getByName(((SipURI) clonedBye.getRequestURI()).getHost());
            } catch (UnknownHostException e) {
            }
            if (toInetUri != null && byeRURI == null) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real 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 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 (logger.isInfoEnabled()) {
                    logger.info("Using the real 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");
                }
                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.isInfoEnabled()) {
                        logger.info("We are behind load balancer, will use Initial Remote Address "
                                + initialIpBeforeLB + ":" + initialPortBeforeLB
                                + " for the cloned BYE request");
                    }
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = sipFactory.createSipURI(null, realIP);
                    clonedBye.setRequestURI(uri);
                } 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.mobicents.servlet.restcomm.telephony.CallManager.java

private void info(final SipServletRequest request) throws IOException {
    final ActorRef self = self();
    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 INFO request: \n %s", request));
        }//from w  w  w . ja v  a 2s. c o m
        request.getSession().setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, request);
        SipServletRequest clonedInfo = linkedB2BUASession.createRequest("INFO");
        linkedB2BUASession.setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, clonedInfo);

        // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307
        SipURI toInetUri = (SipURI) request.getSession().getAttribute("toInetUri");
        SipURI fromInetUri = (SipURI) request.getSession().getAttribute("fromInetUri");
        InetAddress infoRURI = null;
        try {
            infoRURI = InetAddress.getByName(((SipURI) clonedInfo.getRequestURI()).getHost());
        } catch (UnknownHostException e) {
        }
        if (patchForNatB2BUASessions) {
            if (toInetUri != null && infoRURI == null) {

                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneBye request");
                }
                clonedInfo.setRequestURI(toInetUri);
            } else if (toInetUri != null && (infoRURI.isSiteLocalAddress() || infoRURI.isAnyLocalAddress()
                    || infoRURI.isLoopbackAddress())) {

                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneInfo request");
                }
                clonedInfo.setRequestURI(toInetUri);
            } else if (fromInetUri != null && (infoRURI.isSiteLocalAddress() || infoRURI.isAnyLocalAddress()
                    || infoRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + fromInetUri.toString()
                            + " as a request uri of the CloneInfo request");
                }

                clonedInfo.setRequestURI(fromInetUri);
            }
        }
        clonedInfo.send();
    } else {
        final ActorRef call = (ActorRef) application.getAttribute(Call.class.getName());
        call.tell(request, self);
    }
}

From source file:org.blue.star.plugins.check_ping.java

public boolean execute_check() {

    for (String hostname : addresses) {

        try {/*from   ww  w. j  ava  2  s. c o  m*/

            InetAddress inet = InetAddress.getByName(hostname);

            long execute_time = 0;
            long packet_loss = 0;
            for (int pings = 0; pings < max_packets; pings++) {
                boolean reachable = false;
                long ping_time = System.currentTimeMillis();
                if (network_interface != null) {
                    reachable = inet.isReachable(network_interface, 0, utils_h.timeout_interval);
                } else {
                    reachable = inet.isReachable(utils_h.timeout_interval);
                }
                execute_time += (System.currentTimeMillis() - ping_time);
                if (!reachable)
                    packet_loss++;
            }
            rta = execute_time / max_packets;
            pl = (int) packet_loss / max_packets * 100;

            if (verbose > 0) {
                System.out.println("rta = " + rta);
                System.out.println("pl = " + pl);
            }

            if (verbose > 1) {
                System.out.println("isAnyLocalAddress = " + inet.isAnyLocalAddress());
                System.out.println("isLinkLocalAddress = " + inet.isLinkLocalAddress());
                System.out.println("isLoopbackAddress = " + inet.isLoopbackAddress());
                System.out.println("isMCGlobal = " + inet.isMCGlobal());
                System.out.println("isMCLinkLocal = " + inet.isMCLinkLocal());
                System.out.println("isMCNodeLocal = " + inet.isMCNodeLocal());
                System.out.println("isMCOrgLocal = " + inet.isMCOrgLocal());
                System.out.println("isMCSiteLocal = " + inet.isMCSiteLocal());
                System.out.println("isMulticastAddress = " + inet.isMulticastAddress());
                System.out.println("isSiteLocalAddress = " + inet.isSiteLocalAddress());
                System.out.println("isReachable = " + inet.isReachable(utils_h.timeout_interval));
                System.out.println("getCanonicalHostName = " + inet.getCanonicalHostName());
                System.out.println("getHostAddress = " + inet.getHostAddress());
                System.out.println("getHostName = " + inet.getHostName());
                System.out.println("getClass.getName = " + inet.getClass().getName());
            }

            /* The list is only used to check alternatives, if we pass don't do more */
            if (packet_loss != max_packets)
                break;

        } catch (Exception e) {
            warn_text = e.getMessage();
            e.printStackTrace();
        }

    }

    if (pl >= cpl || rta >= crta || rta < 0)
        check_state = common_h.STATE_CRITICAL;
    else if (pl >= wpl || rta >= wrta)
        check_state = common_h.STATE_WARNING;
    else if (pl >= 0 && rta >= 0)
        check_state = common_h.STATE_OK;

    return true;
}

From source file:org.mobicents.servlet.restcomm.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) {
            }//  www. j  av a 2  s .c  o m
            // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307
            SipURI toInetUri = (SipURI) request.getSession().getAttribute("toInetUri");
            if (toInetUri != null && ackRURI == null) {
                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("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");
                }
                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.isInfoEnabled()) {
                        logger.info("We are behind load balancer, will use Initial Remote Address "
                                + initialIpBeforeLB + ":" + initialPortBeforeLB + " for the ACK request");
                    }
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = sipFactory.createSipURI(null, realIP);
                    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.apache.solr.cloud.ZkController.java

private String normalizeHostName(String host) throws IOException {

    if (host == null || host.length() == 0) {
        String hostaddress;//from w  w  w  .  j  ava  2  s  .c o m
        try {
            hostaddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            hostaddress = "127.0.0.1"; // cannot resolve system hostname, fall through
        }
        // Re-get the IP again for "127.0.0.1", the other case we trust the hosts
        // file is right.
        if ("127.0.0.1".equals(hostaddress)) {
            Enumeration<NetworkInterface> netInterfaces = null;
            try {
                netInterfaces = NetworkInterface.getNetworkInterfaces();
                while (netInterfaces.hasMoreElements()) {
                    NetworkInterface ni = netInterfaces.nextElement();
                    Enumeration<InetAddress> ips = ni.getInetAddresses();
                    while (ips.hasMoreElements()) {
                        InetAddress ip = ips.nextElement();
                        if (ip.isSiteLocalAddress()) {
                            hostaddress = ip.getHostAddress();
                        }
                    }
                }
            } catch (Exception e) {
                SolrException.log(log, "Error while looking for a better host name than 127.0.0.1", e);
            }
        }
        host = hostaddress;
    } else {
        if (URLUtil.hasScheme(host)) {
            host = URLUtil.removeScheme(host);
        }
    }

    return host;
}

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

private void onSipServletResponse(SipServletResponse message, ActorRef self, ActorRef sender) throws Exception {
    this.lastResponse = message;

    final int code = message.getStatus();
    switch (code) {
    case SipServletResponse.SC_CALL_BEING_FORWARDED: {
        forwarding(message);//from  w w w . j a va2 s.  c  o m
        break;
    }
    case SipServletResponse.SC_RINGING:
    case SipServletResponse.SC_SESSION_PROGRESS: {
        if (!is(ringing)) {
            if (logger.isInfoEnabled()) {
                logger.info("Got 180 Ringing for Call: " + self().path() + " To: " + to + " sender: "
                        + sender.path() + " observers size: " + observers.size());
            }
            fsm.transition(message, ringing);
        }
        break;
    }
    case SipServletResponse.SC_BUSY_HERE:
    case SipServletResponse.SC_BUSY_EVERYWHERE:
    case SipServletResponse.SC_DECLINE: {
        sendCallInfoToObservers();

        //Important. If state is DIALING, then do nothing about the BUSY. If not DIALING state move to failingBusy
        //                // Notify the observers.
        //                external = CallStateChanged.State.BUSY;
        //                final CallStateChanged event = new CallStateChanged(external);
        //                for (final ActorRef observer : observers) {
        //                    observer.tell(event, self);
        //                }

        // XXX shouldnt it move to failingBusy IF dialing ????
        //                if (is(dialing)) {
        //                    break;
        //                } else {
        //                    fsm.transition(message, failingBusy);
        //                }
        if (!is(failingNoAnswer))
            fsm.transition(message, failingBusy);
        break;
    }
    case SipServletResponse.SC_UNAUTHORIZED:
    case SipServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED: {
        // Handles Auth for https://bitbucket.org/telestax/telscale-restcomm/issue/132/implement-twilio-sip-out
        if ((this.username != null || this.username.isEmpty())
                && (this.password != null && this.password.isEmpty())) {
            sendCallInfoToObservers();
            fsm.transition(message, failed);
        } else {
            AuthInfo authInfo = this.factory.createAuthInfo();
            String authHeader = message.getHeader("Proxy-Authenticate");
            if (authHeader == null) {
                authHeader = message.getHeader("WWW-Authenticate");
            }
            String tempRealm = authHeader.substring(authHeader.indexOf("realm=\"") + "realm=\"".length());
            String realm = tempRealm.substring(0, tempRealm.indexOf("\""));
            authInfo.addAuthInfo(message.getStatus(), realm, this.username, this.password);
            SipServletRequest challengeRequest = message.getSession()
                    .createRequest(message.getRequest().getMethod());
            challengeRequest.addAuthHeader(message, authInfo);
            challengeRequest.setContent(this.invite.getContent(), this.invite.getContentType());
            this.invite = challengeRequest;
            // https://github.com/Mobicents/RestComm/issues/147 Make sure we send the SDP again
            this.invite.setContent(message.getRequest().getContent(), "application/sdp");
            if (outboundToIms) {
                final SipURI uri = factory.createSipURI(null, imsProxyAddress);
                uri.setPort(imsProxyPort);
                uri.setLrParam(true);
                challengeRequest.pushRoute(uri);
            }
            challengeRequest.send();
        }
        break;
    }
    // https://github.com/Mobicents/RestComm/issues/148
    // Session in Progress Response should trigger MMS to start the Media Session
    // case SipServletResponse.SC_SESSION_PROGRESS:
    case SipServletResponse.SC_OK: {
        if (is(dialing) || (is(ringing) && !"inbound".equals(direction))) {
            fsm.transition(message, updatingMediaSession);
        }
        break;
    }
    default: {
        if (code >= 400 && code != 487) {
            if (code == 487 && isOutbound()) {
                String initialIpBeforeLB = null;
                String initialPortBeforeLB = null;
                try {
                    initialIpBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemoteAddr");
                    initialPortBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemotePort");
                } catch (Exception e) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Exception during check of LB custom headers for IP address and port");
                    }
                }
                final SipServletRequest ack = message.createAck();
                addCustomHeaders(ack);
                SipSession session = message.getSession();

                if (initialIpBeforeLB != null) {
                    if (initialPortBeforeLB == null)
                        initialPortBeforeLB = "5060";
                    if (logger.isInfoEnabled()) {
                        logger.info("We are behind load balancer, will use: " + initialIpBeforeLB + ":"
                                + initialPortBeforeLB + " for ACK message, ");
                    }
                    String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
                    SipURI uri = factory.createSipURI(((SipURI) ack.getRequestURI()).getUser(), realIP);

                    ack.setRequestURI(uri);
                } else if (!ack.getHeaders("Route").hasNext()) {
                    final SipServletRequest originalInvite = message.getRequest();
                    final SipURI realInetUri = (SipURI) originalInvite.getRequestURI();
                    if ((SipURI) session.getAttribute("realInetUri") == null) {
                        session.setAttribute("realInetUri", realInetUri);
                    }
                    final InetAddress ackRURI = InetAddress.getByName(((SipURI) ack.getRequestURI()).getHost());
                    final int ackRURIPort = ((SipURI) ack.getRequestURI()).getPort();

                    if (realInetUri != null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress()
                            || ackRURI.isLoopbackAddress()) && (ackRURIPort != realInetUri.getPort())) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Using the real ip address and port of the sip client "
                                    + realInetUri.toString() + " as a request uri of the ACK");
                        }

                        realInetUri.setUser(((SipURI) ack.getRequestURI()).getUser());
                        ack.setRequestURI(realInetUri);
                    }
                }
                ack.send();
                if (logger.isInfoEnabled()) {
                    logger.info("Just sent out ACK : " + ack.toString());
                }
            }
            this.fail = true;
            fsm.transition(message, stopping);
        }
    }
    }
}

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

private void info(final SipServletRequest request) throws IOException {
    final ActorRef self = self();
    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 INFO request: \n %s", request));
        }/*from   w  w  w  . j a va2  s .  c  o m*/
        request.getSession().setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, request);
        SipServletRequest clonedInfo = linkedB2BUASession.createRequest("INFO");
        linkedB2BUASession.setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, clonedInfo);

        // 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 infoRURI = null;
        try {
            infoRURI = InetAddress.getByName(((SipURI) clonedInfo.getRequestURI()).getHost());
        } catch (UnknownHostException e) {
        }
        if (patchForNatB2BUASessions) {
            if (toInetUri != null && infoRURI == null) {

                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneBye request");
                }
                clonedInfo.setRequestURI(toInetUri);
            } else if (toInetUri != null && (infoRURI.isSiteLocalAddress() || infoRURI.isAnyLocalAddress()
                    || infoRURI.isLoopbackAddress())) {

                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + toInetUri.toString()
                            + " as a request uri of the CloneInfo request");
                }
                clonedInfo.setRequestURI(toInetUri);
            } else if (fromInetUri != null && (infoRURI.isSiteLocalAddress() || infoRURI.isAnyLocalAddress()
                    || infoRURI.isLoopbackAddress())) {
                if (logger.isInfoEnabled()) {
                    logger.info("Using the real ip address of the sip client " + fromInetUri.toString()
                            + " as a request uri of the CloneInfo request");
                }

                clonedInfo.setRequestURI(fromInetUri);
            }
        }
        clonedInfo.send();
    } else {
        final ActorRef call = (ActorRef) application.getAttribute(Call.class.getName());
        call.tell(request, self);
    }
}

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

private SipURI getInitialIpAddressPort(SipServletMessage message)
        throws ServletParseException, UnknownHostException {
    // Issue #268 - https://bitbucket.org/telestax/telscale-restcomm/issue/268
    // First get the Initial Remote Address (real address that the request came from)
    // Then check the following:
    // 1. If contact header address is private network address
    // 2. If there are no "Record-Route" headers (there is no proxy in the call)
    // 3. If contact header address != real ip address
    // Finally, if all of the above are true, create a SIP URI using the realIP address and the SIP port
    // and store it to the sip session to be used as request uri later
    SipURI uri = null;//ww w.ja v a 2  s.c om
    try {
        String realIP = message.getInitialRemoteAddr();
        Integer realPort = message.getInitialRemotePort();
        if (realPort == null || realPort == -1) {
            realPort = 5060;
        }

        if (realPort == 0) {
            realPort = message.getRemotePort();
        }

        final ListIterator<String> recordRouteHeaders = message.getHeaders("Record-Route");
        final Address contactAddr = factory.createAddress(message.getHeader("Contact"));

        InetAddress contactInetAddress = InetAddress.getByName(((SipURI) contactAddr.getURI()).getHost());
        InetAddress inetAddress = InetAddress.getByName(realIP);

        int remotePort = message.getRemotePort();
        int contactPort = ((SipURI) contactAddr.getURI()).getPort();
        String remoteAddress = message.getRemoteAddr();

        // Issue #332: https://telestax.atlassian.net/browse/RESTCOMM-332
        final String initialIpBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemoteAddr");
        String initialPortBeforeLB = message.getHeader("X-Sip-Balancer-InitialRemotePort");
        String contactAddress = ((SipURI) contactAddr.getURI()).getHost();

        if (initialIpBeforeLB != null) {
            if (initialPortBeforeLB == null)
                initialPortBeforeLB = "5060";
            if (logger.isInfoEnabled()) {
                logger.info("We are behind load balancer, storing Initial Remote Address " + initialIpBeforeLB
                        + ":" + initialPortBeforeLB + " to the session for later use");
            }
            realIP = initialIpBeforeLB + ":" + initialPortBeforeLB;
            uri = factory.createSipURI(null, realIP);
        } else if (contactInetAddress.isSiteLocalAddress() && !recordRouteHeaders.hasNext()
                && !contactInetAddress.toString().equalsIgnoreCase(inetAddress.toString())) {
            if (logger.isInfoEnabled()) {
                logger.info("Contact header address " + contactAddr.toString()
                        + " is a private network ip address, storing Initial Remote Address " + realIP + ":"
                        + realPort + " to the session for later use");
            }
            realIP = realIP + ":" + realPort;
            uri = factory.createSipURI(null, realIP);
        }
    } catch (Exception e) {
        logger.warning("Exception while trying to get the Initial IP Address and Port: " + e);

    }
    return uri;
}