Example usage for java.net InetAddress isAnyLocalAddress

List of usage examples for java.net InetAddress isAnyLocalAddress

Introduction

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

Prototype

public boolean isAnyLocalAddress() 

Source Link

Document

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

Usage

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

private void sendBye(Hangup hangup) throws IOException, TransitionNotFoundException, TransitionFailedException,
        TransitionRollbackException {//from www.ja v  a  2  s  . co  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) {
            }/*w  ww  .j  a  va2s. 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 {/*  ww w . j av a 2s .com*/
    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());
            }
        }
    }
}