Search in sources :

Example 61 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class SIPBalancerForwarder method getLoopbackUri.

public SipURI getLoopbackUri(Request request, boolean isIpv6) {
    if (logger.isDebugEnabled())
        logger.debug("Check request for loop. Request: " + request);
    SipURI uri = null;
    RouteHeader route = (RouteHeader) request.getHeader(RouteHeader.NAME);
    if (route != null) {
        if (route.getAddress().getURI().isSipURI()) {
            uri = (SipURI) route.getAddress().getURI();
        }
    } else {
        if (request.getRequestURI().isSipURI()) {
            uri = (SipURI) request.getRequestURI();
        }
    }
    if (uri.getHost().matches(".*[a-zA-Z]+.*")) {
        if (logger.isDebugEnabled())
            logger.debug("We are going to patch URI because it has domain name instead of IP : " + uri);
        try {
            uri.setHost(InetAddress.getByName(uri.getHost()).getHostAddress());
        } catch (UnknownHostException | ParseException e) {
            e.printStackTrace();
        }
    }
    if (uri != null) {
        if (!isIpv6) {
            if (uri.getHost().equals(balancerRunner.balancerContext.externalHost) || uri.getHost().equals(balancerRunner.balancerContext.publicIP) || (balancerRunner.balancerContext.externalIpLoadBalancerAddresses != null && balancerRunner.balancerContext.externalIpLoadBalancerAddresses.contains(uri.getHost()))) {
                int port = uri.getPort();
                for (int externalPort : balancerRunner.balancerContext.externalPorts) if (port == externalPort)
                    return uri;
            }
            if (uri.getHost().equals(balancerRunner.balancerContext.internalHost) || uri.getHost().equals(balancerRunner.balancerContext.publicIP) || (balancerRunner.balancerContext.internalIpLoadBalancerAddresses != null && balancerRunner.balancerContext.internalIpLoadBalancerAddresses.contains(uri.getHost()))) {
                int port = uri.getPort();
                for (int internalPort : balancerRunner.balancerContext.internalPorts) if (port == internalPort)
                    return uri;
            }
        } else {
            if (uri.getHost().equals(balancerRunner.balancerContext.externalIpv6Host) || uri.getHost().equals(balancerRunner.balancerContext.publicIPv6) || (balancerRunner.balancerContext.externalIpv6LoadBalancerAddresses != null && balancerRunner.balancerContext.externalIpv6LoadBalancerAddresses.contains(uri.getHost()))) {
                int port = uri.getPort();
                for (int externalIpv6Port : balancerRunner.balancerContext.externalIpv6Ports) if (port == externalIpv6Port)
                    return uri;
            }
            if (uri.getHost().equals(balancerRunner.balancerContext.internalIpv6Host) || uri.getHost().equals(balancerRunner.balancerContext.publicIPv6) || (balancerRunner.balancerContext.internalIpv6LoadBalancerAddresses != null && balancerRunner.balancerContext.internalIpv6LoadBalancerAddresses.contains(uri.getHost()))) {
                int port = uri.getPort();
                for (int internalIpv6Port : balancerRunner.balancerContext.internalIpv6Ports) if (port == internalIpv6Port)
                    return uri;
            }
        }
    }
    return null;
}
Also used : RouteHeader(javax.sip.header.RouteHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) UnknownHostException(java.net.UnknownHostException) ParseException(java.text.ParseException) SipURI(javax.sip.address.SipURI) ListeningPoint(javax.sip.ListeningPoint)

Example 62 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class SIPBalancerForwarder method createRecordRouteHeaders.

private void createRecordRouteHeaders(ArrayList<ListeningPoint> listeningPoints, RecordRouteHeader[] activeHeaders) throws ParseException {
    for (ListeningPoint listeningPoint : listeningPoints) {
        SipURI internalLocalUri = balancerRunner.balancerContext.addressFactory.createSipURI(null, listeningPoint.getIPAddress());
        internalLocalUri.setPort(listeningPoint.getPort());
        internalLocalUri.setTransportParam(listeningPoint.getTransport());
        // See RFC 3261 19.1.1 for lr parameter
        internalLocalUri.setLrParam();
        Address internalLocalAddress = balancerRunner.balancerContext.addressFactory.createAddress(internalLocalUri);
        internalLocalAddress.setURI(internalLocalUri);
        if (logger.isDebugEnabled()) {
            logger.debug("adding internal Record Router Header :" + internalLocalAddress);
        }
        activeHeaders[getIndexByTransport(listeningPoint.getTransport())] = balancerRunner.balancerContext.headerFactory.createRecordRouteHeader(internalLocalAddress);
    }
}
Also used : InetAddress(java.net.InetAddress) HeaderAddress(javax.sip.header.HeaderAddress) Address(javax.sip.address.Address) ListeningPoint(javax.sip.ListeningPoint) SipURI(javax.sip.address.SipURI)

Example 63 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class SIPBalancerForwarder method stampRecordRoute.

private RecordRouteHeader stampRecordRoute(RecordRouteHeader rrh, RouteHeaderHints hints, String transport) {
    SipURI uri = (SipURI) rrh.getAddress().getURI();
    try {
        if (hints.serverAssignedNode != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("About to stamp RecordRoute for hints:\n" + hints.serverAssignedNode.toString() + "\n");
            }
            uri.setParameter(ROUTE_PARAM_NODE_HOST, hints.serverAssignedNode.getIp());
            uri.setParameter(ROUTE_PARAM_NODE_PORT, hints.serverAssignedNode.getProperties().get(transport.toLowerCase() + "Port").toString());
            uri.setParameter(ROUTE_PARAM_NODE_VERSION, hints.version);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("No serverAssignedNode could be found, not stamping the record route\n");
            }
        }
    } catch (ParseException e) {
        logger.warn("Problem adding rrh", e);
    }
    return rrh;
}
Also used : ParseException(java.text.ParseException) SipURI(javax.sip.address.SipURI)

Example 64 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class WorstCaseUdpTestAffinityAlgorithm method processAssignedExternalRequest.

public Node processAssignedExternalRequest(Request request, Node assignedNode) {
    Boolean isIpV6 = LbUtils.isValidInet6Address(assignedNode.getIp());
    // if((y++)%2==0) if(request.getHeader("CSeq").toString().contains("1")) return assignedNode;
    String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
    CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
    long cseq = cs.getSeqNumber();
    if (callIdMap.get(callId) != null) {
        assignedNode = callIdMap.get(callId);
    }
    ViaHeader via = (ViaHeader) request.getHeader(Via.NAME);
    String transport = via.getTransport().toLowerCase();
    RouteHeader route = (RouteHeader) request.getHeader(RouteHeader.NAME);
    SipURI uri = null;
    if (route != null) {
        uri = (SipURI) route.getAddress().getURI();
    } else {
        uri = (SipURI) request.getRequestURI();
    }
    try {
        Node node;
        if (!request.getMethod().equalsIgnoreCase("ACK")) {
            // Gvag: new transaction should go to a new node
            // getNodeA(callId+cseq);
            Node newNode = nextAvailableNode(isIpV6);
            if (newNode == null) {
                // for(Node currNode:invocationContext.nodes) {
                for (Node currNode : invocationContext.sipNodeMap(isIpV6).values()) {
                    if (!currNode.equals(assignedNode)) {
                        newNode = currNode;
                    }
                }
            }
            node = newNode;
        } else
            node = assignedNode;
        uri.setHost(node.getIp());
        if (balancerContext.internalTransport != null) {
            transport = balancerContext.internalTransport.toLowerCase();
        } else if (balancerContext.terminateTLSTraffic)
            if (transport.equalsIgnoreCase(ListeningPoint.TLS))
                transport = ListeningPoint.TCP.toLowerCase();
            else if (transport.equalsIgnoreCase(ListeningPointExt.WSS))
                transport = ListeningPointExt.WS.toLowerCase();
        Integer port = Integer.parseInt(node.getProperties().get(transport + "Port"));
        uri.setPort(port);
        callIdMap.put(callId, node);
        setNodeA(callId + cseq, node);
        if (request.getRequestURI().isSipURI()) {
            SipURI ruri = (SipURI) request.getRequestURI();
            String rurihostid = ruri.getHost() + ruri.getPort();
            String originalhostid = assignedNode.getIp() + assignedNode.getProperties().get(transport + "Port");
            if (rurihostid.equals(originalhostid)) {
                ruri.setPort(port);
                ruri.setHost(node.getIp());
            }
        }
        return node;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return assignedNode;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteHeader(javax.sip.header.RouteHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) ViaHeader(javax.sip.header.ViaHeader) Node(org.mobicents.tools.heartbeat.api.Node) ParseException(java.text.ParseException) SipURI(javax.sip.address.SipURI)

Example 65 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class TestSipListener method processSubscribe.

/**
 * Process the invite request.
 */
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
        logger.info("notifier: got an Subscribe sending OK");
        logger.info("notifier:  " + request);
        logger.info("notifier : dialog = " + requestEvent.getDialog());
        EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
        // this.gotSubscribeRequest = true;
        // Always create a ServerTransaction, best as early as possible in the code
        Response response = null;
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        // Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
        boolean isInitial = requestEvent.getDialog() == null;
        if (isInitial) {
            // JvB: need random tags to test forking
            String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
            response = protocolObjects.messageFactory.createResponse(202, request);
            ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
            // Sanity check: to header should not ahve a tag. Else the dialog
            // should have matched
            // Application is supposed to set.
            toHeader.setTag(toTag);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        } else {
            response = protocolObjects.messageFactory.createResponse(202, request);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        }
        // Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
        Address address = null;
        if (!isIpv6)
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
        else
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
        ((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
        ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
        response.addHeader(contactHeader);
        // Expires header is mandatory in 2xx responses to SUBSCRIBE
        ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
        if (expires == null) {
            // rather short
            expires = protocolObjects.headerFactory.createExpiresHeader(30);
        }
        response.addHeader(expires);
        /*
			 * JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2: 
			 * "[...] a NOTIFY message is always sent immediately after any 200-
			 * class response to a SUBSCRIBE request"
			 * 
			 *  Do this before creating the NOTIFY request below
			 */
        st.sendResponse(response);
        /*
			 * NOTIFY requests MUST contain a "Subscription-State" header with a
			 * value of "active", "pending", or "terminated". The "active" value
			 * indicates that the subscription has been accepted and has been
			 * authorized (in most cases; see section 5.2.). The "pending" value
			 * indicates that the subscription has been received, but that
			 * policy information is insufficient to accept or deny the
			 * subscription at this time. The "terminated" value indicates that
			 * the subscription is not active.
			 */
        if (sendNotify) {
            Request notifyRequest = dialog.createRequest("NOTIFY");
            // Mark the contact header, to check that the remote contact is updated
            // ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
            // Initial state is pending, second time we assume terminated (Expires==0)
            SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
            allSubscriptionStates.add(sstate.getState().toLowerCase());
            // Need a reason for terminated
            if (sstate.getState().equalsIgnoreCase("terminated")) {
                sstate.setReasonCode("deactivated");
            }
            notifyRequest.addHeader(sstate);
            notifyRequest.setHeader(eventHeader);
            notifyRequest.setHeader(contactHeader);
            // notifyRequest.setHeader(routeHeader);
            ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
            if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
                Thread.sleep(timeToWaitBetweenSubsNotify);
            }
            // Let the other side know that the tx is pending acceptance
            // 
            dialog.sendRequest(ct);
            logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
            logger.info("Dialog " + dialog);
            logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
            if (expires.getExpires() != 0) {
                Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
                myEventSource.start();
            }
        }
    } catch (Throwable ex) {
        logger.info(ex.getMessage(), ex);
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) Address(javax.sip.address.Address) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) EventHeader(javax.sip.header.EventHeader) SipURI(javax.sip.address.SipURI) Response(javax.sip.message.Response) ViaHeader(javax.sip.header.ViaHeader) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) ServerTransaction(javax.sip.ServerTransaction) SipProvider(javax.sip.SipProvider)

Aggregations

SipURI (javax.sip.address.SipURI)70 ParseException (java.text.ParseException)27 Request (javax.sip.message.Request)24 Address (javax.sip.address.Address)22 InvalidArgumentException (javax.sip.InvalidArgumentException)20 ViaHeader (javax.sip.header.ViaHeader)20 SipException (javax.sip.SipException)19 ToHeader (javax.sip.header.ToHeader)18 ContactHeader (javax.sip.header.ContactHeader)17 FromHeader (javax.sip.header.FromHeader)17 Test (org.junit.Test)17 RouteHeader (javax.sip.header.RouteHeader)16 AppServer (org.mobicents.tools.sip.balancer.AppServer)16 EventListener (org.mobicents.tools.sip.balancer.EventListener)16 CSeqHeader (javax.sip.header.CSeqHeader)13 CallIdHeader (javax.sip.header.CallIdHeader)13 Header (javax.sip.header.Header)13 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)13 RecordRouteHeader (javax.sip.header.RecordRouteHeader)13 URI (javax.sip.address.URI)11