Пример #1
0
 /**
  * Find a matching client SUBSCRIBE to the incoming notify. NOTIFY requests are matched to such
  * SUBSCRIBE requests if they contain the same "Call-ID", a "To" header "tag" parameter which
  * matches the "From" header "tag" parameter of the SUBSCRIBE, and the same "Event" header field.
  * Rules for comparisons of the "Event" headers are described in section 7.2.1. If a matching
  * NOTIFY request contains a "Subscription-State" of "active" or "pending", it creates a new
  * subscription and a new dialog (unless they have already been created by a matching response, as
  * described above).
  *
  * @param notifyMessage
  */
 public SIPClientTransaction findSubscribeTransaction(SIPRequest notifyMessage) {
   synchronized (clientTransactions) {
     Iterator<SIPClientTransaction> it = clientTransactions.iterator();
     String thisToTag = notifyMessage.getTo().getTag();
     if (thisToTag == null) return null;
     Event eventHdr = (Event) notifyMessage.getHeader(EventHeader.NAME);
     if (eventHdr == null) return null;
     while (it.hasNext()) {
       SIPClientTransaction ct = (SIPClientTransaction) it.next();
       // SIPRequest sipRequest = ct.getOriginalRequest();
       String fromTag = ct.from.getTag();
       Event hisEvent = ct.event;
       // Event header is mandatory but some slopply clients
       // dont include it.
       if (hisEvent == null) continue;
       if (ct.method.equals(Request.SUBSCRIBE)
           && fromTag.equalsIgnoreCase(thisToTag)
           && hisEvent != null
           && eventHdr.match(hisEvent)
           && notifyMessage.getCallId().getCallId().equalsIgnoreCase(ct.callId.getCallId()))
         return ct;
     }
   }
   return null;
 }
Пример #2
0
 /** Hash table for quick lookup of transactions. */
 protected void addTransactionHash(SIPTransaction sipTransaction) {
   SIPRequest sipRequest = sipTransaction.getOriginalRequest();
   // Via via = sipRequest.getTopmostVia();
   // Cannot cache old style requests.
   /**
    * if (via.getBranch() == null || ! via.getBranch().toUpperCase().startsWith
    * (SIPConstants.BRANCH_MAGIC_COOKIE.toUpperCase())){ return; }
    */
   if (sipTransaction instanceof SIPClientTransaction) {
     synchronized (clientTransactionTable) {
       String key = sipRequest.getTransactionId();
       clientTransactionTable.put(key, sipTransaction);
     }
   } else {
     synchronized (serverTransactionTable) {
       String key = sipRequest.getTransactionId();
       serverTransactionTable.put(key, sipTransaction);
     }
   }
 }
  /**
   * Gets invoked by the parser as a callback on successful message parsing (i.e. no parser errors).
   *
   * @param sipMessage Mesage to process (this calls the application for processing the message).
   */
  public void processMessage(SIPMessage sipMessage) throws Exception {
    try {
      if (sipMessage.getFrom() == null
          || // sipMessage.getFrom().getTag() == null ||
          sipMessage.getTo() == null
          || sipMessage.getCallId() == null
          || sipMessage.getCSeq() == null
          || sipMessage.getViaHeaders() == null) {
        String badmsg = sipMessage.encode();
        if (LogWriter.needsLogging) {
          stack.logWriter.logMessage("bad message " + badmsg);
          stack.logWriter.logMessage(">>> Dropped Bad Msg");
        }
        stack.logBadMessage(badmsg);
        return;
      }

      ViaList viaList = sipMessage.getViaHeaders();
      // For a request
      // first via header tells where the message is coming from.
      // For response, this has already been recorded in the outgoing
      // message.
      if (sipMessage instanceof SIPRequest) {
        Via v = (Via) viaList.first();
        if (v.hasPort()) {
          this.peerPort = v.getPort();
        } else this.peerPort = 5061;
        this.peerProtocol = v.getTransport();
        try {
          this.peerAddress = mySock.getInetAddress();
          // Check to see if the received parameter matches
          // the peer address and tag it appropriately.
          // Bug fix by [email protected]
          // Should record host address not host name
          // bug fix by  Joost Yervante Damand
          if (!v.getSentBy().getInetAddress().equals(this.peerAddress)) {
            v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());
            // @@@ hagai
            v.setParameter(Via.RPORT, new Integer(this.peerPort).toString());
          }
        } catch (java.net.UnknownHostException ex) {
          // Could not resolve the sender address.
          if (LogWriter.needsLogging) {
            stack.logWriter.logMessage("Rejecting message -- could not resolve Via Address");
          }
          return;
        } catch (java.text.ParseException ex) {
          InternalErrorHandler.handleException(ex);
        }
        // Use this for outgoing messages as well.
        if (!this.isCached) {
          ((TLSMessageProcessor) this.messageProcessor).cacheMessageChannel(this);
          this.isCached = true;
          String key = IOHandler.makeKey(mySock.getInetAddress(), this.peerPort);
          stack.ioHandler.putSocket(key, mySock);
        }
      }

      // Foreach part of the request header, fetch it and process it

      long receptionTime = System.currentTimeMillis();
      //

      if (sipMessage instanceof SIPRequest) {
        // This is a request - process the request.
        SIPRequest sipRequest = (SIPRequest) sipMessage;
        // Create a new sever side request processor for this
        // message and let it handle the rest.

        if (LogWriter.needsLogging) {
          stack.logWriter.logMessage("----Processing Message---");
        }

        // Check for reasonable size - reject message
        // if it is too long.
        if (stack.getMaxMessageSize() > 0
            && sipRequest.getSize()
                    + (sipRequest.getContentLength() == null
                        ? 0
                        : sipRequest.getContentLength().getContentLength())
                > stack.getMaxMessageSize()) {
          SIPResponse sipResponse = sipRequest.createResponse(SIPResponse.MESSAGE_TOO_LARGE);
          byte[] resp = sipResponse.encodeAsBytes();
          this.sendMessage(resp, false);
          throw new Exception("Message size exceeded");
        }

        ServerRequestInterface sipServerRequest = stack.newSIPServerRequest(sipRequest, this);
        sipServerRequest.processRequest(sipRequest, this);
        if (this.stack.serverLog.needsLogging(ServerLog.TRACE_MESSAGES)) {
          if (sipServerRequest.getProcessingInfo() == null) {
            stack.serverLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                false,
                receptionTime);
          } else {
            this.stack.serverLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                sipServerRequest.getProcessingInfo(),
                false,
                receptionTime);
          }
        }
      } else {
        SIPResponse sipResponse = (SIPResponse) sipMessage;
        // This is a response message - process it.
        // Check the size of the response.
        // If it is too large dump it silently.
        if (stack.getMaxMessageSize() > 0
            && sipResponse.getSize()
                    + (sipResponse.getContentLength() == null
                        ? 0
                        : sipResponse.getContentLength().getContentLength())
                > stack.getMaxMessageSize()) {
          if (LogWriter.needsLogging) this.stack.logWriter.logMessage("Message size exceeded");
          return;
        }
        ServerResponseInterface sipServerResponse = stack.newSIPServerResponse(sipResponse, this);
        sipServerResponse.processResponse(sipResponse, this);
      }
    } finally {
    }
  }
Пример #4
0
  /**
   * Handles a new SIP request. It finds a server transaction to handle this message. If none
   * exists, it creates a new transaction.
   *
   * @param requestReceived Request to handle.
   * @param requestMessageChannel Channel that received message.
   * @return A server transaction.
   */
  protected ServerRequestInterface newSIPServerRequest(
      SIPRequest requestReceived, MessageChannel requestMessageChannel) {

    // Iterator through all server transactions
    Iterator<SIPServerTransaction> transactionIterator;
    // Next transaction in the set
    SIPServerTransaction nextTransaction;
    // Transaction to handle this request
    SIPServerTransaction currentTransaction;

    String key = requestReceived.getTransactionId();

    currentTransaction = (SIPServerTransaction) serverTransactionTable.get(key);
    if (currentTransaction == null
        || !currentTransaction.isMessagePartOfTransaction(requestReceived)) {

      // Loop through all server transactions
      synchronized (serverTransactions) {
        transactionIterator = serverTransactions.iterator();
        currentTransaction = null;
        while (transactionIterator.hasNext() && currentTransaction == null) {

          nextTransaction = (SIPServerTransaction) transactionIterator.next();

          // If this transaction should handle this request,
          if (nextTransaction.isMessagePartOfTransaction(requestReceived)) {
            // Mark this transaction as the one
            // to handle this message
            currentTransaction = nextTransaction;
          }
        }

        // If no transaction exists to handle this message
        if (currentTransaction == null) {
          currentTransaction = findPendingTransaction(requestReceived);
          if (currentTransaction != null) return currentTransaction;
          currentTransaction = createServerTransaction(requestMessageChannel);
          currentTransaction.setOriginalRequest(requestReceived);
          if (!isDialogCreated(requestReceived.getMethod())) {
            // Dialog is not created - can we find the state?
            // If so, then create a transaction and add it.
            String dialogId = requestReceived.getDialogId(true);
            SIPDialog dialog = getDialog(dialogId);
            // Sequence numbers are supposed to increment.
            // avoid processing old sequence numbers and
            // delivering the same request up to the
            // application if the request has already been seen.
            // Special handling applies to ACK processing.
            if (dialog != null
                && (requestReceived.getMethod().equals(Request.ACK)
                    || requestReceived.getCSeq().getSequenceNumber()
                        > dialog.getRemoteSequenceNumber())) {
              // Found a dialog.
              if (LogWriter.needsLogging)
                logWriter.logMessage("adding server transaction " + currentTransaction);
              serverTransactions.add(0, currentTransaction);
              addTransactionHash(currentTransaction);
              currentTransaction.startTransactionTimer();
              currentTransaction.isMapped = true;
            }
          } else {
            // Create the transaction but dont map it.
            String dialogId = requestReceived.getDialogId(true);
            SIPDialog dialog = getDialog(dialogId);
            // This is a dialog creating request that is part of an
            // existing dialog (eg. re-Invite). Re-invites get a non
            // null server transaction Id (unlike the original
            // invite).
            if (dialog != null
                && requestReceived.getCSeq().getSequenceNumber()
                    > dialog.getRemoteSequenceNumber()) {
              currentTransaction.map();
              if (LogWriter.needsLogging)
                logWriter.logMessage("adding server transaction " + currentTransaction);
              serverTransactions.add(0, currentTransaction);
              addTransactionHash(currentTransaction);
              currentTransaction.startTransactionTimer();
              currentTransaction.toListener = true;
            }
          }
        }
      }
    }

    // Set ths transaction's encapsulated request
    // interface from the superclass
    currentTransaction.setRequestInterface(
        super.newSIPServerRequest(requestReceived, currentTransaction));
    return currentTransaction;
  }
Пример #5
0
  /**
   * Actually proces the parsed message.
   *
   * @param sipMessage
   */
  public void processMessage(SIPMessage sipMessage) {

    if (sipMessage instanceof SIPRequest) {
      SIPRequest sipRequest = (SIPRequest) sipMessage;

      // This is a request - process it.
      // So far so good -- we will commit this message if
      // all processing is OK.
      if (sipStack.logWriter.isLoggingEnabled(ServerLog.TRACE_MESSAGES)) {

        this.sipStack.serverLog.logMessage(
            sipMessage,
            this.getPeerHostPort().toString(),
            this.getHost() + ":" + this.myPort,
            false,
            receptionTime);
      }
      ServerRequestInterface sipServerRequest = sipStack.newSIPServerRequest(sipRequest, this);
      // Drop it if there is no request returned
      if (sipServerRequest == null) {
        if (sipStack.isLoggingEnabled()) {
          this.sipStack.logWriter.logWarning("Null request interface returned -- dropping request");
        }

        return;
      }
      if (sipStack.isLoggingEnabled())
        this.sipStack.logWriter.logDebug(
            "About to process " + sipRequest.getFirstLine() + "/" + sipServerRequest);
      try {
        sipServerRequest.processRequest(sipRequest, this);
      } finally {
        if (sipServerRequest instanceof SIPTransaction) {
          SIPServerTransaction sipServerTx = (SIPServerTransaction) sipServerRequest;
          if (!sipServerTx.passToListener()) {
            ((SIPTransaction) sipServerRequest).releaseSem();
          }
        }
      }
      if (sipStack.isLoggingEnabled())
        this.sipStack.logWriter.logDebug(
            "Done processing " + sipRequest.getFirstLine() + "/" + sipServerRequest);

      // So far so good -- we will commit this message if
      // all processing is OK.

    } else {
      // Handle a SIP Reply message.
      SIPResponse sipResponse = (SIPResponse) sipMessage;
      try {
        sipResponse.checkHeaders();
      } catch (ParseException ex) {
        if (sipStack.isLoggingEnabled())
          sipStack.logWriter.logError(
              "Dropping Badly formatted response message >>> " + sipResponse);
        return;
      }
      ServerResponseInterface sipServerResponse = sipStack.newSIPServerResponse(sipResponse, this);
      if (sipServerResponse != null) {
        try {
          if (sipServerResponse instanceof SIPClientTransaction
              && !((SIPClientTransaction) sipServerResponse).checkFromTag(sipResponse)) {
            if (sipStack.isLoggingEnabled())
              sipStack.logWriter.logError(
                  "Dropping response message with invalid tag >>> " + sipResponse);
            return;
          }

          sipServerResponse.processResponse(sipResponse, this);
        } finally {
          if (sipServerResponse instanceof SIPTransaction
              && !((SIPTransaction) sipServerResponse).passToListener())
            ((SIPTransaction) sipServerResponse).releaseSem();
        }

        // Normal processing of message.
      } else {
        if (sipStack.isLoggingEnabled()) {
          this.sipStack.logWriter.logDebug("null sipServerResponse!");
        }
      }
    }
  }
Пример #6
0
  /**
   * Logs the specified message and details to the packet logging service if enabled.
   *
   * @param message the message to log
   * @param sender determines whether we are the origin of this message.
   */
  private void logPacket(SIPMessage message, boolean sender) {
    try {
      PacketLoggingService packetLogging = SipActivator.getPacketLogging();
      if (packetLogging == null
          || !packetLogging.isLoggingEnabled(PacketLoggingService.ProtocolName.SIP)
          /* Via not present in CRLF packet on TCP - causes NPE */
          || message.getTopmostVia() == null) return;

      String transport = message.getTopmostVia().getTransport();
      boolean isTransportUDP = transport.equalsIgnoreCase("UDP");

      byte[] srcAddr;
      int srcPort;
      byte[] dstAddr;
      int dstPort;

      // if addresses are not set use empty byte array with length
      // equals to the other address or just empty
      // byte array with length 4 (ipv4 0.0.0.0)
      if (sender) {
        if (!isTransportUDP) {
          InetSocketAddress localAddress =
              getLocalAddressForDestination(
                  message.getRemoteAddress(),
                  message.getRemotePort(),
                  message.getLocalAddress(),
                  transport);
          srcPort = localAddress.getPort();
          srcAddr = localAddress.getAddress().getAddress();
        } else {
          srcPort = message.getLocalPort();
          if (message.getLocalAddress() != null) srcAddr = message.getLocalAddress().getAddress();
          else if (message.getRemoteAddress() != null)
            srcAddr = new byte[message.getRemoteAddress().getAddress().length];
          else srcAddr = new byte[4];
        }

        dstPort = message.getRemotePort();
        if (message.getRemoteAddress() != null) dstAddr = message.getRemoteAddress().getAddress();
        else dstAddr = new byte[srcAddr.length];
      } else {
        if (!isTransportUDP) {
          InetSocketAddress dstAddress =
              getLocalAddressForDestination(
                  message.getRemoteAddress(),
                  message.getRemotePort(),
                  message.getLocalAddress(),
                  transport);
          dstPort = dstAddress.getPort();
          dstAddr = dstAddress.getAddress().getAddress();
        } else {
          dstPort = message.getLocalPort();
          if (message.getLocalAddress() != null) dstAddr = message.getLocalAddress().getAddress();
          else if (message.getRemoteAddress() != null)
            dstAddr = new byte[message.getRemoteAddress().getAddress().length];
          else dstAddr = new byte[4];
        }

        srcPort = message.getRemotePort();
        if (message.getRemoteAddress() != null) srcAddr = message.getRemoteAddress().getAddress();
        else srcAddr = new byte[dstAddr.length];
      }

      byte[] msg = null;
      if (message instanceof SIPRequest) {
        SIPRequest req = (SIPRequest) message;
        if (req.getMethod().equals(SIPRequest.MESSAGE)
            && message.getContentTypeHeader() != null
            && message.getContentTypeHeader().getContentType().equalsIgnoreCase("text")) {
          int len = req.getContentLength().getContentLength();

          if (len > 0) {
            SIPRequest newReq = (SIPRequest) req.clone();

            byte[] newContent = new byte[len];
            Arrays.fill(newContent, (byte) '.');
            newReq.setMessageContent(newContent);
            msg = newReq.toString().getBytes("UTF-8");
          }
        }
      }

      if (msg == null) {
        msg = message.toString().getBytes("UTF-8");
      }

      packetLogging.logPacket(
          PacketLoggingService.ProtocolName.SIP,
          srcAddr,
          srcPort,
          dstAddr,
          dstPort,
          isTransportUDP
              ? PacketLoggingService.TransportName.UDP
              : PacketLoggingService.TransportName.TCP,
          sender,
          msg);
    } catch (Throwable e) {
      logger.error("Cannot obtain message body", e);
    }
  }