/// <summary> /// Raises <b>Reinvite</b> event. /// </summary> /// <param name="reinvite">Re-INVITE server transaction.</param> private void OnReinvite(SIP_ServerTransaction reinvite) { if (Reinvite != null) { Reinvite(this, new EventArgs()); } }
/// <summary> /// Creates new SIP server transaction for specified request. /// </summary> /// <param name="flow">SIP data flow which is used to receive request.</param> /// <param name="request">SIP request.</param> /// <returns>Returns added server transaction.</returns> /// <exception cref="ObjectDisposedException">Is raised when this class is Disposed and this method is accessed.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>flow</b> or <b>request</b> is null reference.</exception> public SIP_ServerTransaction CreateServerTransaction(SIP_Flow flow, SIP_Request request) { if (m_IsDisposed) { throw new ObjectDisposedException(GetType().Name); } if (flow == null) { throw new ArgumentNullException("flow"); } if (request == null) { throw new ArgumentNullException("request"); } lock (m_pServerTransactions) { SIP_ServerTransaction transaction = new SIP_ServerTransaction(m_pStack, flow, request); m_pServerTransactions.Add(transaction.Key, transaction); transaction.StateChanged += delegate { if (transaction.State == SIP_TransactionState.Terminated) { lock (m_pClientTransactions) { m_pServerTransactions.Remove(transaction.Key); } } }; return(transaction); } }
/// <summary> /// Matches CANCEL requst to SIP server non-CANCEL transaction. Returns null if no match. /// </summary> /// <param name="cancelRequest">SIP CANCEL request.</param> /// <returns>Returns CANCEL matching server transaction or null if no match.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>cancelTransaction</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when <b>cancelTransaction</b> has invalid.</exception> public SIP_ServerTransaction MatchCancelToTransaction(SIP_Request cancelRequest) { if (cancelRequest == null) { throw new ArgumentNullException("cancelRequest"); } if (cancelRequest.RequestLine.Method != SIP_Methods.CANCEL) { throw new ArgumentException("Argument 'cancelRequest' is not SIP CANCEL request."); } SIP_ServerTransaction retVal = null; // NOTE: There we don't add '-CANCEL' because we want to get CANCEL matching transaction, not CANCEL // transaction itself. string key = cancelRequest.Via.GetTopMostValue().Branch + '-' + cancelRequest.Via.GetTopMostValue().SentBy; lock (m_pServerTransactions) { m_pServerTransactions.TryGetValue(key, out retVal); } return(retVal); }
/// <summary> /// Default constructor. /// </summary> /// <param name="stack">Reference to SIP stack.</param> /// <param name="flow">SIP data flow.</param> /// <param name="request">Recieved request.</param> /// <param name="dialog">SIP dialog which received request.</param> /// <param name="transaction">SIP server transaction which must be used to send response back to request maker.</param> internal SIP_RequestReceivedEventArgs(SIP_Stack stack, SIP_Flow flow, SIP_Request request, SIP_Dialog dialog, SIP_ServerTransaction transaction) { m_pStack = stack; m_pFlow = flow; m_pRequest = request; m_pDialog = dialog; m_pTransaction = transaction; }
/// <summary> /// Default constructor. /// </summary> /// <param name="stack">Reference to SIP stack.</param> /// <param name="flow">SIP data flow.</param> /// <param name="request">Recieved request.</param> /// <param name="dialog">SIP dialog which received request.</param> /// <param name="transaction">SIP server transaction which must be used to send response back to request maker.</param> internal SIP_RequestReceivedEventArgs(SIP_Stack stack, SIP_Flow flow, SIP_Request request, SIP_Dialog dialog, SIP_ServerTransaction transaction) { m_pStack = stack; m_pFlow = flow; m_pRequest = request; m_pDialog = dialog; m_pTransaction = transaction; }
/// <summary> /// Default constructor. /// </summary> /// <param name="transaction">Server transaction.</param> /// <param name="response">SIP response.</param> /// <exception cref="ArgumentNullException">Is raised when any of the arguments is null.</exception> public SIP_ResponseSentEventArgs(SIP_ServerTransaction transaction, SIP_Response response) { if (transaction == null) { throw new ArgumentNullException("transaction"); } if (response == null) { throw new ArgumentNullException("response"); } m_pTransaction = transaction; m_pResponse = response; }
/// <summary> /// Default constructor. /// </summary> /// <param name="transaction">Server transaction.</param> /// <param name="response">SIP response.</param> /// <exception cref="ArgumentNullException">Is raised when any of the arguments is null.</exception> public SIP_ResponseSentEventArgs(SIP_ServerTransaction transaction, SIP_Response response) { if (transaction == null) { throw new ArgumentNullException("transaction"); } if (response == null) { throw new ArgumentNullException("response"); } m_pTransaction = transaction; m_pResponse = response; }
/// <summary> /// Matches SIP request to server transaction. If not matching transaction found, returns null. /// </summary> /// <param name="request">SIP request to match.</param> /// <returns>Returns matching transaction or null if no match.</returns> internal SIP_ServerTransaction MatchServerTransaction(SIP_Request request) { /* RFC 3261 17.2.3 Matching Requests to Server Transactions. * This matching rule applies to both INVITE and non-INVITE transactions. * * 1. the branch parameter in the request is equal to the one in the top Via header * field of the request that created the transaction, and * * 2. the sent-by value in the top Via of the request is equal to the * one in the request that created the transaction, and * * 3. the method of the request matches the one that created the transaction, except * for ACK, where the method of the request that created the transaction is INVITE. * * Internal implementation notes: * Inernally we use branch + '-' + sent-by for non-CANCEL and for CANCEL * branch + '-' + sent-by + '-' CANCEL. This is because method matching is actually * needed for CANCEL only (CANCEL shares cancelable transaction branch ID). */ SIP_ServerTransaction retVal = null; /* * We use branch and sent-by as indexing key for transaction, the only special what we need to * do is to handle CANCEL, because it has same branch as transaction to be canceled. * For avoiding key collision, we add branch + '-' + 'sent-by' + CANCEL for cancel index key. * ACK has also same branch, but we won't do transaction for ACK, so it isn't problem. */ string key = request.Via.GetTopMostValue().Branch + '-' + request.Via.GetTopMostValue().SentBy; if (request.RequestLine.Method == SIP_Methods.CANCEL) { key += "-CANCEL"; } lock (m_pServerTransactions) { m_pServerTransactions.TryGetValue(key, out retVal); } // Don't match ACK for terminated transaction, in that case ACK must be passed to "core". if (retVal != null && request.RequestLine.Method == SIP_Methods.ACK && retVal.State == SIP_TransactionState.Terminated) { retVal = null; } return(retVal); }
/// <summary> /// Ensures that specified request has matching server transaction. If server transaction doesn't exist, /// it will be created, otherwise existing transaction will be returned. /// </summary> /// <param name="flow">SIP data flow which is used to receive request.</param> /// <param name="request">SIP request.</param> /// <returns>Returns matching transaction.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>flow</b> or <b>request</b> is null.</exception> /// <exception cref="InvalidOperationException">Is raised when request.Method is ACK request.</exception> public SIP_ServerTransaction EnsureServerTransaction(SIP_Flow flow, SIP_Request request) { if (flow == null) { throw new ArgumentNullException("flow"); } if (request == null) { throw new ArgumentNullException("request"); } if (request.RequestLine.Method == SIP_Methods.ACK) { throw new InvalidOperationException( "ACK request is transaction less request, can't create transaction for it."); } /* * We use branch and sent-by as indexing key for transaction, the only special what we need to * do is to handle CANCEL, because it has same branch as transaction to be canceled. * For avoiding key collision, we add branch + '-' + 'sent-by' + CANCEL for cancel index key. * ACK has also same branch, but we won't do transaction for ACK, so it isn't problem. */ string key = request.Via.GetTopMostValue().Branch + '-' + request.Via.GetTopMostValue().SentBy; if (request.RequestLine.Method == SIP_Methods.CANCEL) { key += "-CANCEL"; } lock (m_pServerTransactions) { SIP_ServerTransaction retVal = null; m_pServerTransactions.TryGetValue(key, out retVal); // We don't have transaction, create it. if (retVal == null) { retVal = CreateServerTransaction(flow, request); } return(retVal); } }
/// <summary> /// Raises <b>Reinvite</b> event. /// </summary> /// <param name="reinvite">Re-INVITE server transaction.</param> private void OnReinvite(SIP_ServerTransaction reinvite) { if (Reinvite != null) { Reinvite(this, new EventArgs()); } }
/// <summary> /// Creates new SIP server transaction for specified request. /// </summary> /// <param name="flow">SIP data flow which is used to receive request.</param> /// <param name="request">SIP request.</param> /// <returns>Returns added server transaction.</returns> /// <exception cref="ObjectDisposedException">Is raised when this class is Disposed and this method is accessed.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>flow</b> or <b>request</b> is null reference.</exception> public SIP_ServerTransaction CreateServerTransaction(SIP_Flow flow, SIP_Request request) { if (m_IsDisposed) { throw new ObjectDisposedException(GetType().Name); } if (flow == null) { throw new ArgumentNullException("flow"); } if (request == null) { throw new ArgumentNullException("request"); } lock (m_pServerTransactions) { SIP_ServerTransaction transaction = new SIP_ServerTransaction(m_pStack, flow, request); m_pServerTransactions.Add(transaction.Key, transaction); transaction.StateChanged += delegate { if (transaction.State == SIP_TransactionState.Terminated) { lock (m_pClientTransactions) { m_pServerTransactions.Remove(transaction.Key); } } }; return transaction; } }
/// <summary> /// Sends specified response back to request maker using RFC 3261 18. rules. /// </summary> /// <param name="transaction">SIP server transaction which response to send.</param> /// <param name="response">SIP response.</param> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when stack ahs not been started and this method is accessed.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>transaction</b> or <b>response</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> /// <exception cref="SIP_TransportException">Is raised when <b>response</b> sending has failed.</exception> internal void SendResponse(SIP_ServerTransaction transaction, SIP_Response response) { if (transaction == null) { throw new ArgumentNullException("transaction"); } // NOTE: all other paramter / state validations are done in SendResponseInternal. SendResponseInternal(transaction, response, null); }
/// <summary> /// Sends response to request maker using RFC 3261 18. rules. /// </summary> /// <param name="transaction">Owner server transaction. Can be null if stateless response sending.</param> /// <param name="response">SIP response to send.</param> /// <param name="localEP">Local IP end point to use for sending resposne. Value null means system will allocate it.</param> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when stack ahs not been started and this method is accessed.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>response</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> /// <exception cref="SIP_TransportException">Is raised when <b>response</b> sending has failed.</exception> private void SendResponseInternal(SIP_ServerTransaction transaction, SIP_Response response, IPEndPoint localEP) { if (m_IsDisposed) { throw new ObjectDisposedException(GetType().Name); } if (!m_IsRunning) { throw new InvalidOperationException("Stack has not been started."); } if (response == null) { throw new ArgumentNullException("response"); } /* RFC 3261 18.2.2. The server transport uses the value of the top Via header field in order to determine where to send a response. It MUST follow the following process: o If the "sent-protocol" is a reliable transport protocol such as TCP or SCTP, or TLS over those, the response MUST be sent using the existing connection to the source of the original request that created the transaction, if that connection is still open. This requires the server transport to maintain an association between server transactions and transport connections. If that connection is no longer open, the server SHOULD open a connection to the IP address in the "received" parameter, if present, using the port in the "sent-by" value, or the default port for that transport, if no port is specified. If that connection attempt fails, the server SHOULD use the procedures in [4] for servers in order to determine the IP address and port to open the connection and send the response to. o Otherwise, if the Via header field value contains a "maddr" parameter, the response MUST be forwarded to the address listed there, using the port indicated in "sent-by", or port 5060 if none is present. If the address is a multicast address, the response SHOULD be sent using the TTL indicated in the "ttl" parameter, or with a TTL of 1 if that parameter is not present. o Otherwise (for unreliable unicast transports), if the top Via has a "received" parameter, the response MUST be sent to the address in the "received" parameter, using the port indicated in the "sent-by" value, or using port 5060 if none is specified explicitly. If this fails, for example, elicits an ICMP "port unreachable" response, the procedures of Section 5 of [4] SHOULD be used to determine where to send the response. o Otherwise, if it is not receiver-tagged, the response MUST be sent to the address indicated by the "sent-by" value, using the procedures in Section 5 of [4]. */ /* RFC 3581 4. (Adds new processing between RFC 3261 18.2.2. bullet 2 and 3) When a server attempts to send a response, it examines the topmost Via header field value of that response. If the "sent-protocol" component indicates an unreliable unicast transport protocol, such as UDP, and there is no "maddr" parameter, but there is both a "received" parameter and an "rport" parameter, the response MUST be sent to the IP address listed in the "received" parameter, and the port in the "rport" parameter. The response MUST be sent from the same address and port that the corresponding request was received on. This effectively adds a new processing step between bullets two and three in Section 18.2.2 of SIP [1]. The response must be sent from the same address and port that the request was received on in order to traverse symmetric NATs. When a server is listening for requests on multiple ports or interfaces, it will need to remember the one on which the request was received. For a stateful proxy, storing this information for the duration of the transaction is not an issue. However, a stateless proxy does not store state between a request and its response, and therefore cannot remember the address and port on which a request was received. To properly implement this specification, a stateless proxy can encode the destination address and port of a request into the Via header field value that it inserts. When the response arrives, it can extract this information and use it to forward the response. */ SIP_t_ViaParm via = response.Via.GetTopMostValue(); if (via == null) { throw new ArgumentException("Argument 'response' does not contain required Via: header field."); } // TODO: If transport is not supported. //throw new SIP_TransportException("Not supported transport '" + via.ProtocolTransport + "'."); string logID = Guid.NewGuid().ToString(); string transactionID = transaction == null ? "" : transaction.ID; // Try to get local IP end point which we should use to send response back. if (transaction != null && transaction.Request.LocalEndPoint != null) { localEP = transaction.Request.LocalEndPoint; } // TODO: no "localEP" at moment // TODO: Stateless should use flowID instead. // Our stateless proxy add 'localEP' parameter to Via: if so normally we can get it from there. else if (via.Parameters["localEP"] != null) { localEP = Net_Utils.ParseIPEndPoint(via.Parameters["localEP"].Value); } byte[] responseData = response.ToByteData(); #region Try existing flow first /* First try active flow to send response, thats not 100% as RFC says, but works better in any case. RFC says that for TCP and TLS only, we do it for any transport. */ if (transaction != null) { try { SIP_Flow flow = transaction.Flow; flow.Send(response); if (m_pStack.Logger != null) { m_pStack.Logger.AddWrite(logID, null, 0, "Response [flowReuse=true; transactionID='" + transactionID + "'; method='" + response.CSeq.RequestMethod + "'; cseq='" + response.CSeq.SequenceNumber + "'; " + "transport='" + flow.Transport + "'; size='" + responseData.Length + "'; statusCode='" + response.StatusCode + "'; " + "reason='" + response.ReasonPhrase + "'; sent '" + flow.LocalEP + "' -> '" + flow.RemoteEP + "'.", localEP, flow.RemoteEP, responseData); } return; } catch { // Do nothing, processing will continue. } } #endregion #region Reliable TCP,TLS, ... if (SIP_Utils.IsReliableTransport(via.ProtocolTransport)) { // Get original request remote end point. IPEndPoint remoteEP = null; if (transaction != null && transaction.Request.RemoteEndPoint != null) { remoteEP = transaction.Request.RemoteEndPoint; } else if (via.Received != null) { remoteEP = new IPEndPoint(via.Received, via.SentBy.Port == -1 ? 5060 : via.SentBy.Port); } #region If original request connection alive, use it try { SIP_Flow flow = null; // Statefull if (transaction != null) { if (transaction.Request.Flow != null && !transaction.Request.Flow.IsDisposed) { flow = transaction.Request.Flow; } } // Stateless else { string flowID = via.Parameters["connectionID"].Value; if (flowID != null) { flow = m_pFlowManager[flowID]; } } if (flow != null) { flow.Send(response); if (m_pStack.Logger != null) { m_pStack.Logger.AddWrite(logID, null, 0, "Response [flowReuse=true; transactionID='" + transactionID + "'; method='" + response.CSeq.RequestMethod + "'; cseq='" + response.CSeq.SequenceNumber + "'; " + "transport='" + flow.Transport + "'; size='" + responseData.Length + "'; statusCode='" + response.StatusCode + "'; " + "reason='" + response.ReasonPhrase + "'; sent '" + flow.RemoteEP + "' -> '" + flow.LocalEP + "'.", localEP, remoteEP, responseData); } return; } } catch { // Do nothing, processing will continue. // Override RFC, if there is any existing connection and it gives error, try always RFC 3261 18.2.2(recieved) and 3265 5. } #endregion #region Send RFC 3261 18.2.2(recieved) if (remoteEP != null) { try { SendResponseToHost(logID, transactionID, null, remoteEP.Address.ToString(), remoteEP.Port, via.ProtocolTransport, response); } catch { // Do nothing, processing will continue -> "RFC 3265 5.". } } #endregion #region Send RFC 3265 5. SendResponse_RFC_3263_5(logID, transactionID, localEP, response); #endregion } #endregion #region UDP Via: maddr parameter else if (via.Maddr != null) { throw new SIP_TransportException( "Sending responses to multicast address(Via: 'maddr') is not supported."); } #endregion #region RFC 3581 4. UDP Via: received and rport parameters else if (via.Maddr == null && via.Received != null && via.RPort > 0) { SendResponseToHost(logID, transactionID, localEP, via.Received.ToString(), via.RPort, via.ProtocolTransport, response); } #endregion #region UDP Via: received parameter else if (via.Received != null) { SendResponseToHost(logID, transactionID, localEP, via.Received.ToString(), via.SentByPortWithDefault, via.ProtocolTransport, response); } #endregion #region UDP else { SendResponse_RFC_3263_5(logID, transactionID, localEP, response); } #endregion }