Пример #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;
 }
  /**
   * Make a clone (deep copy) of this object. You can use this if you want to modify a request while
   * preserving the original
   *
   * @return a deep copy of this object.
   */
  public Object clone() {
    SIPRequest retval = (SIPRequest) super.clone();
    // Do not copy over the tx pointer -- this is only for internal
    // tracking.
    retval.transactionPointer = null;
    if (this.requestLine != null) retval.requestLine = (RequestLine) this.requestLine.clone();

    return retval;
  }
Пример #3
0
  /**
   * Performs strict router fix according to RFC3261 section 16.6 step 6
   *
   * <p>pre: top route header in request has no 'lr' parameter in URI post: request-URI added as
   * last route header, new req-URI = top-route-URI
   */
  public void fixStrictRouting(SIPRequest req) {

    RouteList routes = req.getRouteHeaders();
    Route first = (Route) routes.getFirst();
    SipUri firstUri = (SipUri) first.getAddress().getURI();
    routes.removeFirst();

    // Add request-URI as last Route entry
    AddressImpl addr = new AddressImpl();
    addr.setAddess(req.getRequestURI()); // don't clone it
    Route route = new Route(addr);

    routes.add(route); // as last one
    req.setRequestURI(firstUri);
    if (sipStack.isLoggingEnabled()) {
      sipStack.getStackLogger().logDebug("post: fixStrictRouting" + req);
    }
  }
Пример #4
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);
     }
   }
 }
 /**
  * Create a new default SIPRequest from the original request. Warning: the newly created
  * SIPRequest, shares the headers of this request but we generate any new headers that we need to
  * modify so the original request is umodified. However, if you modify the shared headers after
  * this request is created, then the newly created request will also be modified. If you want to
  * modify the original request without affecting the returned Request make sure you clone it
  * before calling this method.
  *
  * <p>Only required headers are copied.
  *
  * <ul>
  *   <li>Contact headers are not included in the newly created request. Setting the appropriate
  *       sequence number is the responsibility of the caller.
  *   <li>RouteList is not copied for ACK and CANCEL
  *   <li>Note that we DO NOT copy the body of the argument into the returned header. We do not
  *       copy the content type header from the original request either. These have to be added
  *       seperately and the content length has to be correctly set if necessary the content length
  *       is set to 0 in the returned header.
  *   <li>Contact List is not copied from the original request.
  *   <li>RecordRoute List is not included from original request.
  *   <li>Via header is not included from the original request.
  * </ul>
  *
  * @param requestLine is the new request line.
  * @param switchHeaders is a boolean flag that causes to and from headers to switch (set this to
  *     true if you are the server of the transaction and are generating a BYE request). If the
  *     headers are switched, we generate new From and To headers otherwise we just use the
  *     incoming headers.
  * @return a new Default SIP Request which has the requestLine specified.
  */
 public SIPRequest createSIPRequest(RequestLine requestLine, boolean switchHeaders) {
   SIPRequest newRequest = new SIPRequest();
   newRequest.requestLine = requestLine;
   Iterator headerIterator = this.getHeaders();
   while (headerIterator.hasNext()) {
     SIPHeader nextHeader = (SIPHeader) headerIterator.next();
     // For BYE and cancel set the CSeq header to the
     // appropriate method.
     if (nextHeader instanceof CSeq) {
       CSeq newCseq = (CSeq) nextHeader.clone();
       nextHeader = newCseq;
       try {
         newCseq.setMethod(requestLine.getMethod());
       } catch (ParseException e) {
       }
     } else if (nextHeader instanceof ViaList) {
       Via via = (Via) (((ViaList) nextHeader).getFirst().clone());
       via.removeParameter("branch");
       nextHeader = via;
       // Cancel and ACK preserve the branch ID.
     } else if (nextHeader instanceof To) {
       To to = (To) nextHeader;
       if (switchHeaders) {
         nextHeader = new From(to);
         ((From) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) to.clone();
         ((To) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof From) {
       From from = (From) nextHeader;
       if (switchHeaders) {
         nextHeader = new To(from);
         ((To) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) from.clone();
         ((From) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof ContentLength) {
       ContentLength cl = (ContentLength) nextHeader.clone();
       try {
         cl.setContentLength(0);
       } catch (InvalidArgumentException e) {
       }
       nextHeader = cl;
     } else if (!(nextHeader instanceof CallID) && !(nextHeader instanceof MaxForwards)) {
       // Route is kept by dialog.
       // RR is added by the caller.
       // Contact is added by the Caller
       // Any extension headers must be added
       // by the caller.
       continue;
     }
     try {
       newRequest.attachHeader(nextHeader, false);
     } catch (SIPDuplicateHeaderException e) {
       e.printStackTrace();
     }
   }
   if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
     newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
   }
   return newRequest;
 }
  /**
   * Creates an ACK for non-2xx responses according to RFC3261 17.1.1.3
   *
   * @return A SIPRequest with an ACK method.
   * @throws SipException
   * @throws NullPointerException
   * @throws ParseException
   * @author jvb
   */
  public final SIPRequest createErrorAck(To responseToHeader) throws SipException, ParseException {

    /*
     * The ACK request constructed by the client transaction MUST contain values for the
     * Call-ID, From, and Request-URI that are equal to the values of those header fields in
     * the request passed to the transport by the client transaction (call this the "original
     * request"). The To header field in the ACK MUST equal the To header field in the
     * response being acknowledged, and therefore will usually differ from the To header field
     * in the original request by the addition of the tag parameter. The ACK MUST contain a
     * single Via header field, and this MUST be equal to the top Via header field of the
     * original request. The CSeq header field in the ACK MUST contain the same value for the
     * sequence number as was present in the original request, but the method parameter MUST
     * be equal to "ACK".
     */
    SIPRequest newRequest = new SIPRequest();
    newRequest.setRequestLine((RequestLine) this.requestLine.clone());
    newRequest.setMethod(Request.ACK);
    newRequest.setHeader((Header) this.callIdHeader.clone());
    newRequest.setHeader((Header) this.maxForwardsHeader.clone()); // ISSUE
    // 130
    // fix
    newRequest.setHeader((Header) this.fromHeader.clone());
    newRequest.setHeader((Header) responseToHeader.clone());
    newRequest.addFirst((Header) this.getTopmostVia().clone());
    newRequest.setHeader((Header) cSeqHeader.clone());
    newRequest.getCSeq().setMethod(Request.ACK);

    /*
     * If the INVITE request whose response is being acknowledged had Route header fields,
     * those header fields MUST appear in the ACK. This is to ensure that the ACK can be
     * routed properly through any downstream stateless proxies.
     */
    if (this.getRouteHeaders() != null) {
      newRequest.setHeader((SIPHeaderList) this.getRouteHeaders().clone());
    }
    if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
      newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
    }
    return newRequest;
  }
  /**
   * Creates a default ACK SIPRequest message for this original request. Note that the defaultACK
   * SIPRequest does not include the content of the original SIPRequest. If responseToHeader is null
   * then the toHeader of this request is used to construct the ACK. Note that tag fields are just
   * copied from the original SIP Request. Added by Jeff Keyser.
   *
   * @param responseToHeader To header to use for this request.
   * @return A SIPRequest with an ACK method.
   */
  public SIPRequest createAckRequest(To responseToHeader) {
    SIPRequest newRequest;
    Iterator headerIterator;
    SIPHeader nextHeader;

    newRequest = new SIPRequest();
    newRequest.setRequestLine((RequestLine) this.requestLine.clone());
    newRequest.setMethod(Request.ACK);
    headerIterator = getHeaders();
    while (headerIterator.hasNext()) {
      nextHeader = (SIPHeader) headerIterator.next();
      if (nextHeader instanceof RouteList) {
        // Ack and cancel do not get ROUTE headers.
        // Route header for ACK is assigned by the
        // Dialog if necessary.
        continue;
      } else if (nextHeader instanceof ProxyAuthorization) {
        // Remove proxy auth header.
        // Assigned by the Dialog if necessary.
        continue;
      } else if (nextHeader instanceof ContentLength) {
        // Adding content is responsibility of user.
        nextHeader = (SIPHeader) nextHeader.clone();
        try {
          ((ContentLength) nextHeader).setContentLength(0);
        } catch (InvalidArgumentException e) {
        }
      } else if (nextHeader instanceof ContentType) {
        // Content type header is removed since
        // content length is 0.
        continue;
      } else if (nextHeader instanceof CSeq) {
        // The CSeq header field in the
        // ACK MUST contain the same value for the
        // sequence number as was present in the
        // original request, but the method parameter
        // MUST be equal to "ACK".
        CSeq cseq = (CSeq) nextHeader.clone();
        try {
          cseq.setMethod(Request.ACK);
        } catch (ParseException e) {
        }
        nextHeader = cseq;
      } else if (nextHeader instanceof To) {
        if (responseToHeader != null) {
          nextHeader = responseToHeader;
        } else {
          nextHeader = (SIPHeader) nextHeader.clone();
        }
      } else if (nextHeader instanceof ContactList || nextHeader instanceof Expires) {
        // CONTACT header does not apply for ACK requests.
        continue;
      } else if (nextHeader instanceof ViaList) {
        // Bug reported by Gianluca Martinello
        // The ACK MUST contain a single Via header field,
        // and this MUST be equal to the top Via header
        // field of the original
        // request.

        nextHeader = (SIPHeader) ((ViaList) nextHeader).getFirst().clone();
      } else {
        nextHeader = (SIPHeader) nextHeader.clone();
      }

      try {
        newRequest.attachHeader(nextHeader, false);
      } catch (SIPDuplicateHeaderException e) {
        e.printStackTrace();
      }
    }
    if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
      newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
    }
    return newRequest;
  }
  /**
   * Creates a default SIPResquest message that would cancel this request. Note that tag assignment
   * and removal of is left to the caller (we use whatever tags are present in the original
   * request).
   *
   * @return A CANCEL SIPRequest constructed according to RFC3261 section 9.1
   * @throws SipException
   * @throws ParseException
   */
  public SIPRequest createCancelRequest() throws SipException {

    // see RFC3261 9.1

    // A CANCEL request SHOULD NOT be sent to cancel a request other than
    // INVITE

    if (!this.getMethod().equals(Request.INVITE))
      throw new SipException("Attempt to create CANCEL for " + this.getMethod());

    /*
     * The following procedures are used to construct a CANCEL request. The Request-URI,
     * Call-ID, To, the numeric part of CSeq, and From header fields in the CANCEL request
     * MUST be identical to those in the request being cancelled, including tags. A CANCEL
     * constructed by a client MUST have only a single Via header field value matching the top
     * Via value in the request being cancelled. Using the same values for these header fields
     * allows the CANCEL to be matched with the request it cancels (Section 9.2 indicates how
     * such matching occurs). However, the method part of the CSeq header field MUST have a
     * value of CANCEL. This allows it to be identified and processed as a transaction in its
     * own right (See Section 17).
     */
    SIPRequest cancel = new SIPRequest();
    cancel.setRequestLine((RequestLine) this.requestLine.clone());
    cancel.setMethod(Request.CANCEL);
    cancel.setHeader((Header) this.callIdHeader.clone());
    cancel.setHeader((Header) this.toHeader.clone());
    cancel.setHeader((Header) cSeqHeader.clone());
    try {
      cancel.getCSeq().setMethod(Request.CANCEL);
    } catch (ParseException e) {
      e.printStackTrace(); // should not happen
    }
    cancel.setHeader((Header) this.fromHeader.clone());

    cancel.addFirst((Header) this.getTopmostVia().clone());
    cancel.setHeader((Header) this.maxForwardsHeader.clone());

    /*
     * If the request being cancelled contains a Route header field, the CANCEL request MUST
     * include that Route header field's values.
     */
    if (this.getRouteHeaders() != null) {
      cancel.setHeader((SIPHeaderList) this.getRouteHeaders().clone());
    }
    if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
      cancel.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
    }
    return cancel;
  }
Пример #9
0
  /**
   * Return addresses for default proxy to forward the request to. The list is organized in the
   * following priority. If the requestURI refers directly to a host, the host and port information
   * are extracted from it and made the next hop on the list. If the default route has been
   * specified, then it is used to construct the next element of the list. <code>
   * RouteHeader firstRoute = (RouteHeader) req.getHeader( RouteHeader.NAME );
   * if (firstRoute!=null) {
   *   URI uri = firstRoute.getAddress().getURI();
   *    if (uri.isSIPUri()) {
   *       SipURI nextHop = (SipURI) uri;
   *       if ( nextHop.hasLrParam() ) {
   *           // OK, use it
   *       } else {
   *           nextHop = fixStrictRouting( req );        <--- Here, make the modifications as per RFC3261
   *       }
   *   } else {
   *       // error: non-SIP URI not allowed in Route headers
   *       throw new SipException( "Request has Route header with non-SIP URI" );
   *   }
   * } else if (outboundProxy!=null) {
   *   // use outbound proxy for nextHop
   * } else if ( req.getRequestURI().isSipURI() ) {
   *   // use request URI for nextHop
   * }
   *
   * </code>
   *
   * @param request is the sip request to route.
   */
  public Hop getNextHop(Request request) throws SipException {

    SIPRequest sipRequest = (SIPRequest) request;

    RequestLine requestLine = sipRequest.getRequestLine();
    if (requestLine == null) {
      return defaultRoute;
    }
    javax.sip.address.URI requestURI = requestLine.getUri();
    if (requestURI == null) throw new IllegalArgumentException("Bad message: Null requestURI");

    RouteList routes = sipRequest.getRouteHeaders();

    /*
     * In case the topmost Route header contains no 'lr' parameter (which
     * means the next hop is a strict router), the implementation will
     * perform 'Route Information Postprocessing' as described in RFC3261
     * section 16.6 step 6 (also known as "Route header popping"). That is,
     * the following modifications will be made to the request:
     *
     * The implementation places the Request-URI into the Route header field
     * as the last value.
     *
     * The implementation then places the first Route header field value
     * into the Request-URI and removes that value from the Route header
     * field.
     *
     * Subsequently, the request URI will be used as next hop target
     */

    if (routes != null) {

      // to send the request through a specified hop the application is
      // supposed to prepend the appropriate Route header which.
      Route route = (Route) routes.getFirst();
      URI uri = route.getAddress().getURI();
      if (uri.isSipURI()) {
        SipURI sipUri = (SipURI) uri;
        if (!sipUri.hasLrParam()) {

          fixStrictRouting(sipRequest);
          if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Route post processing fixed strict routing");
        }

        Hop hop = createHop(sipUri, request);
        if (sipStack.isLoggingEnabled())
          sipStack.getStackLogger().logDebug("NextHop based on Route:" + hop);
        return hop;
      } else {
        throw new SipException("First Route not a SIP URI");
      }

    } else if (requestURI.isSipURI() && ((SipURI) requestURI).getMAddrParam() != null) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using request URI maddr to route the request = " + hop.toString());

      // JvB: don't remove it!
      // ((SipURI) requestURI).removeParameter("maddr");

      return hop;

    } else if (defaultRoute != null) {
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using outbound proxy to route the request = " + defaultRoute.toString());
      return defaultRoute;
    } else if (requestURI.isSipURI()) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (hop != null && sipStack.isLoggingEnabled())
        sipStack.getStackLogger().logDebug("Used request-URI for nextHop = " + hop.toString());
      else if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug("returning null hop -- loop detected");
      }
      return hop;

    } else {
      // The internal router should never be consulted for non-sip URIs.
      InternalErrorHandler.handleException(
          "Unexpected non-sip URI", this.sipStack.getStackLogger());
      return null;
    }
  }
Пример #10
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;
  }