Search in sources :

Example 6 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class UDPMessageChannel method processIncomingDataPacket.

/**
     * Process an incoming datagram
     *
     * @param packet
     *            is the incoming datagram packet.
     */
private void processIncomingDataPacket(DatagramPacket packet) throws Exception {
    this.peerAddress = packet.getAddress();
    int packetLength = packet.getLength();
    // Read bytes and put it in a eueue.
    byte[] bytes = packet.getData();
    byte[] msgBytes = new byte[packetLength];
    System.arraycopy(bytes, 0, msgBytes, 0, packetLength);
    // Do debug logging.
    if (sipStack.isLoggingEnabled()) {
        this.sipStack.getStackLogger().logDebug("UDPMessageChannel: processIncomingDataPacket : peerAddress = " + peerAddress.getHostAddress() + "/" + packet.getPort() + " Length = " + packetLength);
    }
    SIPMessage sipMessage = null;
    try {
        this.receptionTime = System.currentTimeMillis();
        sipMessage = myParser.parseSIPMessage(msgBytes);
        myParser = null;
    } catch (ParseException ex) {
        // let go of the parser reference.
        myParser = null;
        if (sipStack.isLoggingEnabled()) {
            this.sipStack.getStackLogger().logDebug("Rejecting message !  " + new String(msgBytes));
            this.sipStack.getStackLogger().logDebug("error message " + ex.getMessage());
            this.sipStack.getStackLogger().logException(ex);
        }
        // JvB: send a 400 response for requests (except ACK)
        // Currently only UDP, @todo also other transports
        String msgString = new String(msgBytes, 0, packetLength);
        if (!msgString.startsWith("SIP/") && !msgString.startsWith("ACK ")) {
            String badReqRes = createBadReqRes(msgString, ex);
            if (badReqRes != null) {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("Sending automatic 400 Bad Request:");
                    sipStack.getStackLogger().logDebug(badReqRes);
                }
                try {
                    this.sendMessage(badReqRes.getBytes(), peerAddress, packet.getPort(), "UDP", false);
                } catch (IOException e) {
                    this.sipStack.getStackLogger().logException(e);
                }
            } else {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("Could not formulate automatic 400 Bad Request");
                }
            }
        }
        return;
    }
    if (sipMessage == null) {
        if (sipStack.isLoggingEnabled()) {
            this.sipStack.getStackLogger().logDebug("Rejecting message !  + Null message parsed.");
        }
        if (pingBackRecord.get(packet.getAddress().getHostAddress() + ":" + packet.getPort()) == null) {
            byte[] retval = "\r\n\r\n".getBytes();
            DatagramPacket keepalive = new DatagramPacket(retval, 0, retval.length, packet.getAddress(), packet.getPort());
            ((UDPMessageProcessor) this.messageProcessor).sock.send(keepalive);
            this.sipStack.getTimer().schedule(new PingBackTimerTask(packet.getAddress().getHostAddress(), packet.getPort()), 1000);
        }
        return;
    }
    ViaList viaList = sipMessage.getViaHeaders();
    // Check for the required headers.
    if (sipMessage.getFrom() == null || sipMessage.getTo() == null || sipMessage.getCallId() == null || sipMessage.getCSeq() == null || sipMessage.getViaHeaders() == null) {
        String badmsg = new String(msgBytes);
        if (sipStack.isLoggingEnabled()) {
            this.sipStack.getStackLogger().logError("bad message " + badmsg);
            this.sipStack.getStackLogger().logError(">>> Dropped Bad Msg " + "From = " + sipMessage.getFrom() + "To = " + sipMessage.getTo() + "CallId = " + sipMessage.getCallId() + "CSeq = " + sipMessage.getCSeq() + "Via = " + sipMessage.getViaHeaders());
        }
        return;
    }
    // For response, just get the port from the packet.
    if (sipMessage instanceof SIPRequest) {
        Via v = (Via) viaList.getFirst();
        Hop hop = sipStack.addressResolver.resolveAddress(v.getHop());
        this.peerPort = hop.getPort();
        this.peerProtocol = v.getTransport();
        this.peerPacketSourceAddress = packet.getAddress();
        this.peerPacketSourcePort = packet.getPort();
        try {
            this.peerAddress = packet.getAddress();
            // Check to see if the received parameter matches
            // the peer address and tag it appropriately.
            boolean hasRPort = v.hasParameter(Via.RPORT);
            if (hasRPort || !hop.getHost().equals(this.peerAddress.getHostAddress())) {
                v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());
            }
            if (hasRPort) {
                v.setParameter(Via.RPORT, Integer.toString(this.peerPacketSourcePort));
            }
        } catch (java.text.ParseException ex1) {
            InternalErrorHandler.handleException(ex1);
        }
    } else {
        this.peerPacketSourceAddress = packet.getAddress();
        this.peerPacketSourcePort = packet.getPort();
        this.peerAddress = packet.getAddress();
        this.peerPort = packet.getPort();
        this.peerProtocol = ((Via) viaList.getFirst()).getTransport();
    }
    this.processMessage(sipMessage);
}
Also used : Hop(javax.sip.address.Hop) IOException(java.io.IOException) SIPRequest(gov.nist.javax.sip.message.SIPRequest) Via(gov.nist.javax.sip.header.Via) ViaList(gov.nist.javax.sip.header.ViaList) DatagramPacket(java.net.DatagramPacket) SIPMessage(gov.nist.javax.sip.message.SIPMessage) ParseException(java.text.ParseException) ParseException(java.text.ParseException)

Example 7 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class UDPMessageChannel method sendMessage.

/**
     * Return a reply from a pre-constructed reply. This sends the message back
     * to the entity who caused us to create this channel in the first place.
     *
     * @param sipMessage
     *            Message string to send.
     * @throws IOException
     *             If there is a problem with sending the message.
     */
public void sendMessage(SIPMessage sipMessage) throws IOException {
    if (sipStack.isLoggingEnabled() && this.sipStack.isLogStackTraceOnMessageSend()) {
        if (sipMessage instanceof SIPRequest && ((SIPRequest) sipMessage).getRequestLine() != null) {
            /*
                 * We dont want to log empty trace messages.
                 */
            this.sipStack.getStackLogger().logStackTrace(StackLogger.TRACE_INFO);
        } else {
            this.sipStack.getStackLogger().logStackTrace(StackLogger.TRACE_INFO);
        }
    }
    // Test and see where we are going to send the messsage. If the message
    // is sent back to oursleves, just
    // shortcircuit processing.
    long time = System.currentTimeMillis();
    try {
        for (MessageProcessor messageProcessor : sipStack.getMessageProcessors()) {
            if (messageProcessor.getIpAddress().equals(this.peerAddress) && messageProcessor.getPort() == this.peerPort && messageProcessor.getTransport().equals(this.peerProtocol)) {
                MessageChannel messageChannel = messageProcessor.createMessageChannel(this.peerAddress, this.peerPort);
                if (messageChannel instanceof RawMessageChannel) {
                    ((RawMessageChannel) messageChannel).processMessage(sipMessage);
                    if (sipStack.isLoggingEnabled())
                        sipStack.getStackLogger().logDebug("Self routing message");
                    return;
                }
            }
        }
        byte[] msg = sipMessage.encodeAsBytes(this.getTransport());
        sendMessage(msg, peerAddress, peerPort, peerProtocol, sipMessage instanceof SIPRequest);
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        sipStack.getStackLogger().logError("An exception occured while sending message", ex);
        throw new IOException("An exception occured while sending message");
    } finally {
        if (sipStack.getStackLogger().isLoggingEnabled(ServerLogger.TRACE_MESSAGES) && !sipMessage.isNullRequest())
            logMessage(sipMessage, peerAddress, peerPort, time);
        else if (sipStack.getStackLogger().isLoggingEnabled(ServerLogger.TRACE_DEBUG))
            sipStack.getStackLogger().logDebug("Sent EMPTY Message");
    }
}
Also used : IOException(java.io.IOException) SIPRequest(gov.nist.javax.sip.message.SIPRequest) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 8 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPTransactionStack method removeTransactionHash.

/**
     * Remove the transaction from transaction hash.
     */
protected void removeTransactionHash(SIPTransaction sipTransaction) {
    SIPRequest sipRequest = sipTransaction.getOriginalRequest();
    if (sipRequest == null)
        return;
    if (sipTransaction instanceof SIPClientTransaction) {
        String key = sipTransaction.getTransactionId();
        if (stackLogger.isLoggingEnabled()) {
            stackLogger.logStackTrace();
            stackLogger.logDebug("removing client Tx : " + key);
        }
        clientTransactionTable.remove(key);
    } else if (sipTransaction instanceof SIPServerTransaction) {
        String key = sipTransaction.getTransactionId();
        serverTransactionTable.remove(key);
        if (stackLogger.isLoggingEnabled()) {
            stackLogger.logDebug("removing server Tx : " + key);
        }
    }
}
Also used : SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 9 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPDialog method storeFirstTransactionInfo.

private static void storeFirstTransactionInfo(SIPDialog dialog, SIPTransaction transaction) {
    dialog.firstTransaction = transaction;
    dialog.firstTransactionSeen = true;
    dialog.firstTransactionIsServerTransaction = transaction.isServerTransaction();
    dialog.firstTransactionSecure = transaction.getRequest().getRequestURI().getScheme().equalsIgnoreCase("sips");
    dialog.firstTransactionPort = transaction.getPort();
    dialog.firstTransactionId = transaction.getBranchId();
    dialog.firstTransactionMethod = transaction.getMethod();
    if (dialog.isServer()) {
        SIPServerTransaction st = (SIPServerTransaction) transaction;
        SIPResponse response = st.getLastResponse();
        dialog.contactHeader = response != null ? response.getContactHeader() : null;
    } else {
        SIPClientTransaction ct = (SIPClientTransaction) transaction;
        if (ct != null) {
            SIPRequest sipRequest = ct.getOriginalRequest();
            dialog.contactHeader = sipRequest.getContactHeader();
        }
    }
}
Also used : SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 10 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPDialog method createFromNOTIFY.

/**
     * Creates a new dialog based on a received NOTIFY. The dialog state is initialized
     * appropriately. The NOTIFY differs in the From tag
     * 
     * Made this a separate method to clearly distinguish what's happening here - this is a
     * non-trivial case
     * 
     * @param subscribeTx - the transaction started with the SUBSCRIBE that we sent
     * @param notifyST - the ServerTransaction created for an incoming NOTIFY
     * @return -- a new dialog created from the subscribe original SUBSCRIBE transaction.
     * 
     * 
     */
public static SIPDialog createFromNOTIFY(SIPClientTransaction subscribeTx, SIPTransaction notifyST) {
    SIPDialog d = new SIPDialog(notifyST);
    //
    // The above sets d.firstTransaction to NOTIFY (ST), correct that
    //
    d.serverTransactionFlag = false;
    // they share this one
    d.lastTransaction = subscribeTx;
    storeFirstTransactionInfo(d, subscribeTx);
    d.terminateOnBye = false;
    d.localSequenceNumber = subscribeTx.getCSeq();
    SIPRequest not = (SIPRequest) notifyST.getRequest();
    d.remoteSequenceNumber = not.getCSeq().getSeqNumber();
    d.setDialogId(not.getDialogId(true));
    d.setLocalTag(not.getToTag());
    d.setRemoteTag(not.getFromTag());
    // to properly create the Dialog object.
    // If not the stack will throw an exception when creating the response.
    d.setLastResponse(subscribeTx, subscribeTx.getLastResponse());
    // Dont use setLocal / setRemote here, they make other assumptions
    d.localParty = not.getTo().getAddress();
    d.remoteParty = not.getFrom().getAddress();
    // initialize d's route set based on the NOTIFY. Any proxies must have
    // Record-Routed
    d.addRoute(not);
    // set state, *after* setting route set!
    d.setState(CONFIRMED_STATE);
    return d;
}
Also used : SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Aggregations

SIPRequest (gov.nist.javax.sip.message.SIPRequest)32 IOException (java.io.IOException)17 ParseException (java.text.ParseException)17 SipException (javax.sip.SipException)16 InvalidArgumentException (javax.sip.InvalidArgumentException)10 SIPResponse (gov.nist.javax.sip.message.SIPResponse)9 Via (gov.nist.javax.sip.header.Via)8 ObjectInUseException (javax.sip.ObjectInUseException)8 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)6 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)6 DialogDoesNotExistException (javax.sip.DialogDoesNotExistException)6 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)6 Hop (javax.sip.address.Hop)6 SipURI (javax.sip.address.SipURI)4 ListeningPointImpl (gov.nist.javax.sip.ListeningPointImpl)3 From (gov.nist.javax.sip.header.From)3 Route (gov.nist.javax.sip.header.Route)3 To (gov.nist.javax.sip.header.To)3 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)3 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)3