Search in sources :

Example 21 with Header

use of javax.sip.header.Header in project load-balancer by RestComm.

the class Shootist method sendInitialInvite.

public void sendInitialInvite() {
    try {
        if (!started)
            start();
        String fromName = "BigGuy";
        String fromSipAddress = "here.com";
        String fromDisplayName = "The Master Blaster";
        String toSipAddress = "there.com";
        String toUser = "LittleGuy";
        String toDisplayName = "The Little Blister";
        // create >From Header
        SipURI fromAddress = addressFactory.createSipURI(fromName, fromSipAddress);
        Address fromNameAddress = addressFactory.createAddress(fromAddress);
        fromNameAddress.setDisplayName(fromDisplayName);
        FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "12345");
        // create To Header
        SipURI toAddress = addressFactory.createSipURI(toUser, toSipAddress);
        Address toNameAddress = addressFactory.createAddress(toAddress);
        toNameAddress.setDisplayName(toDisplayName);
        ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
        // create Request URI
        SipURI requestURI = addressFactory.createSipURI(toUser, peerHostPort);
        // Create ViaHeaders
        ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
        String ipAddress = udpListeningPoint.getIPAddress();
        ViaHeader viaHeader = headerFactory.createViaHeader(ipAddress, sipProvider.getListeningPoint(transport).getPort(), transport, null);
        // add via headers
        viaHeaders.add(viaHeader);
        // Create ContentTypeHeader
        ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
        // Create a new CallId header
        CallIdHeader callIdHeader = sipProvider.getNewCallId();
        // Create a new Cseq header
        CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);
        // Create a new MaxForwardsHeader
        MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
        // Create the request.
        Request request = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
        // Create contact headers
        String host = "127.0.0.1";
        SipURI contactUrl = addressFactory.createSipURI(fromName, host);
        contactUrl.setPort(udpListeningPoint.getPort());
        contactUrl.setLrParam();
        // Create the contact name address.
        SipURI contactURI = addressFactory.createSipURI(fromName, host);
        contactURI.setPort(sipProvider.getListeningPoint(transport).getPort());
        Address contactAddress = addressFactory.createAddress(contactURI);
        // Add the contact address.
        contactAddress.setDisplayName(fromName);
        contactHeader = headerFactory.createContactHeader(contactAddress);
        request.addHeader(contactHeader);
        // You can add extension headers of your own making
        // to the outgoing SIP request.
        // Add the extension header.
        Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
        request.addHeader(extensionHeader);
        String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n" + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
        byte[] contents = sdpData.getBytes();
        request.setContent(contents, contentTypeHeader);
        // You can add as many extension headers as you
        // want.
        extensionHeader = headerFactory.createHeader("My-Other-Header", "my new header value ");
        request.addHeader(extensionHeader);
        Header callInfoHeader = headerFactory.createHeader("Call-Info", "<http://www.antd.nist.gov>");
        request.addHeader(callInfoHeader);
        // Create the client transaction.
        inviteTid = sipProvider.getNewClientTransaction(request);
        // send the request out.
        inviteTid.sendRequest();
        dialog = inviteTid.getDialog();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        fail("Unxpected exception ");
    }
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ArrayList(java.util.ArrayList) Request(javax.sip.message.Request) SipURI(javax.sip.address.SipURI) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) PeerUnavailableException(javax.sip.PeerUnavailableException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) CSeqHeader(javax.sip.header.CSeqHeader) CSeqHeader(javax.sip.header.CSeqHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ContactHeader(javax.sip.header.ContactHeader) ToHeader(javax.sip.header.ToHeader) FromHeader(javax.sip.header.FromHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) Header(javax.sip.header.Header) ViaHeader(javax.sip.header.ViaHeader) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader)

Example 22 with Header

use of javax.sip.header.Header in project load-balancer by RestComm.

the class SIPBalancerForwarder method securityCheck.

/**
 * @param request
 * @return
 */
private boolean securityCheck(Request request) {
    // User-Agent: sipcli/v1.8
    // User-Agent: friendly-scanner
    // To: "sipvicious" <sip:100@1.1.1.1>
    // From: "sipvicious" <sip:100@1.1.1.1>;tag=3336353363346565313363340133313330323436343236
    // From: "1" <sip:1@87.202.36.237>;tag=3e7a78de
    Header userAgentHeader = request.getHeader("User-Agent");
    Header toHeader = request.getHeader("To");
    Header fromHeader = request.getHeader("From");
    for (String blockedValue : balancerRunner.balancerContext.blockedList) {
        if (userAgentHeader != null && userAgentHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
            return false;
        } else if (toHeader != null && toHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
            return false;
        } else if (fromHeader != null && fromHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
            return false;
        }
    }
    return true;
}
Also used : ReasonHeader(javax.sip.header.ReasonHeader) ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) Header(javax.sip.header.Header) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) FromHeader(javax.sip.header.FromHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader)

Example 23 with Header

use of javax.sip.header.Header in project jain-sip.ha by RestComm.

the class SimpleB2BUAHandler method forwardResponse.

/**
 * @param responseEvent
 * @throws InvalidArgumentException
 */
@SuppressWarnings("unchecked")
private void forwardResponse(Response receivedResponse) throws SipException, InvalidArgumentException {
    if (receivedResponse.getStatusCode() == 202) {
        return;
    }
    System.out.println("Forwarding " + receivedResponse);
    final ServerTransaction origServerTransaction = this.getServerTransaction();
    Response forgedResponse = null;
    try {
        forgedResponse = messageFactory.createResponse(receivedResponse.getStatusCode(), origServerTransaction.getRequest());
    } catch (ParseException e) {
        throw new SipException("Failed to forge message", e);
    }
    // if ((getIncomingDialog() == null || getIncomingDialog().getState() == DialogState.EARLY) && localTag != null && getIncomingDialog() != null && getIncomingDialog().isServer()) {
    // // no tag set in the response, since the dialog creating transaction didn't had it
    // try {
    // ((ToHeader)forgedResponse.getHeader(ToHeader.NAME)).setTag(localTag);
    // } catch (ParseException e) {
    // throw new SipException("Failed to set local tag", e);
    // }
    // }
    // copy headers
    ListIterator<String> lit = receivedResponse.getHeaderNames();
    String headerName = null;
    ListIterator<Header> headersIterator = null;
    while (lit.hasNext()) {
        headerName = lit.next();
        if (SimpleB2BUAHandler.getHeadersToOmmitOnResponseCopy().contains(headerName)) {
            continue;
        } else {
            forgedResponse.removeHeader(headerName);
            headersIterator = receivedResponse.getHeaders(headerName);
            while (headersIterator.hasNext()) {
                forgedResponse.addLast((Header) headersIterator.next().clone());
            }
        }
    }
    // Copy content
    final byte[] rawOriginal = receivedResponse.getRawContent();
    if (rawOriginal != null && rawOriginal.length != 0) {
        final byte[] copy = new byte[rawOriginal.length];
        System.arraycopy(rawOriginal, 0, copy, 0, copy.length);
        try {
            forgedResponse.setContent(copy, (ContentTypeHeader) forgedResponse.getHeader(ContentTypeHeader.NAME));
        } catch (ParseException e) {
            throw new SipException("Failed to copy content.", e);
        }
    }
    // set contact if the received response had it
    if (receivedResponse.getHeader(ContactHeader.NAME) != null) {
        final String transport = ((ViaHeader) forgedResponse.getHeader(ViaHeader.NAME)).getTransport();
        forgedResponse.setHeader(((ListeningPointImpl) sipProvider.getListeningPoint(transport)).createContactHeader());
    }
    // set the to tag mandatory so that the dialog gets replicated
    if (((MessageExt) forgedResponse).getToHeader().getTag() == null) {
        try {
            ((MessageExt) forgedResponse).getToHeader().setTag("696");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    origServerTransaction.sendResponse(forgedResponse);
}
Also used : Response(javax.sip.message.Response) MessageExt(gov.nist.javax.sip.message.MessageExt) ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) ContentLengthHeader(javax.sip.header.ContentLengthHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) Header(javax.sip.header.Header) CSeqHeader(javax.sip.header.CSeqHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) EventHeader(javax.sip.header.EventHeader) FromHeader(javax.sip.header.FromHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) ViaHeader(javax.sip.header.ViaHeader) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 24 with Header

use of javax.sip.header.Header in project smscgateway by RestComm.

the class TxSipServerSbb method onMESSAGE.

// *********
// SIP Event Handlers
public void onMESSAGE(javax.sip.RequestEvent event, ActivityContextInterface aci) {
    if (this.logger.isFineEnabled()) {
        this.logger.fine("onMESSAGE " + event);
    }
    Sip sip = sipManagement.getSipByName(SipManagement.SIP_NAME);
    try {
        final Request request = event.getRequest();
        byte[] message = request.getRawContent();
        final ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
        final String toUser = ((SipUri) toHeader.getAddress().getURI()).getUser();
        final FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
        final String fromUser = ((SipUri) fromHeader.getAddress().getURI()).getUser();
        // Persist this message
        TargetAddress ta = this.createDestTargetAddress(toUser, sip.getNetworkId());
        byte[] udh = null;
        Header udhHeader = request.getHeader(SipXHeaders.XSmsUdh);
        if (udhHeader != null) {
            udh = this.hexStringToByteArray(((SIPHeader) udhHeader).getValue());
        }
        Header codingHeader = request.getHeader(SipXHeaders.XSmsCoding);
        DataCodingSchemeImpl codingSchme = dcsGsm7;
        if (codingHeader != null) {
            int dcs = Integer.parseInt(((SIPHeader) codingHeader).getValue());
            codingSchme = this.createDataCodingScheme(dcs);
        }
        Date validityPeriod = null;
        Header validityHeader = request.getHeader(SipXHeaders.XSmsValidty);
        if (validityHeader != null) {
            try {
                validityPeriod = MessageUtil.parseDate(((SIPHeader) validityHeader).getValue());
            } catch (ParseException e) {
                logger.severe("ParseException when parsing ValidityPeriod field: " + e.getMessage(), e);
                if (smscPropertiesManagement.isGenerateRejectionCdr()) {
                    generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e.getMessage(), true);
                }
                ServerTransaction serverTransaction = event.getServerTransaction();
                Response res;
                try {
                    res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
                    event.getServerTransaction().sendResponse(res);
                } catch (Exception e1) {
                    this.logger.severe("Exception while trying to send 500 response to sip", e1);
                }
                return;
            }
        }
        // Registered Delivery
        int regDeliveryInt = 0;
        Header regDeliveryHeader = request.getHeader(SipXHeaders.XRegDelivery);
        if (regDeliveryHeader != null) {
            regDeliveryInt = Integer.parseInt(((SIPHeader) regDeliveryHeader).getValue());
        }
        Sms sms = null;
        try {
            sms = this.createSmsEvent(fromUser, message, ta, persistence, udh, codingSchme, validityPeriod, regDeliveryInt, sip.getNetworkId());
            this.processSms(sms, persistence);
        } catch (SmscProcessingException e1) {
            if (!e1.isSkipErrorLogging()) {
                if (e1.isIsWarning()) {
                    this.logger.warning(e1.getMessage());
                } else {
                    this.logger.severe(e1.getMessage(), e1);
                }
                smscStatAggregator.updateMsgInFailedAll();
            }
            if (smscPropertiesManagement.isGenerateRejectionCdr() && !e1.isMessageRejectCdrCreated()) {
                if (sms != null) {
                    generateCDR(sms, CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), false, true);
                } else {
                    generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
                }
            }
            ServerTransaction serverTransaction = event.getServerTransaction();
            Response res;
            try {
                res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
                event.getServerTransaction().sendResponse(res);
            } catch (Exception e) {
                this.logger.severe("Exception while trying to send Ok response to sip", e);
            }
            return;
        } catch (Throwable e1) {
            this.logger.severe("Exception while processing a message from sip", e1);
            smscStatAggregator.updateMsgInFailedAll();
            if (smscPropertiesManagement.isGenerateRejectionCdr()) {
                generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
            }
            ServerTransaction serverTransaction = event.getServerTransaction();
            Response res;
            try {
                // TODO: we possibly need to response ERROR message to sip
                res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
                event.getServerTransaction().sendResponse(res);
            } catch (Exception e) {
                this.logger.severe("Exception while trying to send Ok response to sip", e);
            }
            return;
        }
        ServerTransaction serverTransaction = event.getServerTransaction();
        Response res;
        try {
            res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
            event.getServerTransaction().sendResponse(res);
        } catch (Exception e) {
            this.logger.severe("Exception while trying to send Ok response to sip", e);
        }
    } catch (Exception e) {
        this.logger.severe("Error while trying to process received the SMS " + event, e);
    }
}
Also used : FromHeader(javax.sip.header.FromHeader) SmscProcessingException(org.mobicents.smsc.library.SmscProcessingException) Request(javax.sip.message.Request) TargetAddress(org.mobicents.smsc.library.TargetAddress) SipUri(gov.nist.javax.sip.address.SipUri) DataCodingSchemeImpl(org.restcomm.protocols.ss7.map.smstpdu.DataCodingSchemeImpl) Date(java.util.Date) SmscProcessingException(org.mobicents.smsc.library.SmscProcessingException) ParseException(java.text.ParseException) Response(javax.sip.message.Response) ToHeader(javax.sip.header.ToHeader) FromHeader(javax.sip.header.FromHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) Header(javax.sip.header.Header) SIPHeader(gov.nist.javax.sip.header.SIPHeader) ToHeader(javax.sip.header.ToHeader) Sms(org.mobicents.smsc.library.Sms) Sip(org.mobicents.smsc.domain.Sip) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction)

Aggregations

Header (javax.sip.header.Header)24 FromHeader (javax.sip.header.FromHeader)19 ToHeader (javax.sip.header.ToHeader)19 ViaHeader (javax.sip.header.ViaHeader)19 CallIdHeader (javax.sip.header.CallIdHeader)18 ContactHeader (javax.sip.header.ContactHeader)17 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)17 Request (javax.sip.message.Request)16 ParseException (java.text.ParseException)15 SipURI (javax.sip.address.SipURI)15 RouteHeader (javax.sip.header.RouteHeader)15 RecordRouteHeader (javax.sip.header.RecordRouteHeader)14 CSeqHeader (javax.sip.header.CSeqHeader)13 ContentTypeHeader (javax.sip.header.ContentTypeHeader)13 ReasonHeader (javax.sip.header.ReasonHeader)13 SipException (javax.sip.SipException)11 ArrayList (java.util.ArrayList)10 InvalidArgumentException (javax.sip.InvalidArgumentException)10 TransactionUnavailableException (javax.sip.TransactionUnavailableException)9 Response (javax.sip.message.Response)9