\section{Behavior of SIP User Agents}
\label{sec:ua}

This section describes the rules for user agent client and servers for
generating and processing requests and responses.

\subsection{Caller Issues Initial {\INVITE} Request}
\label{sec:ua-invitation}

When a user agent client desires to initiate a call, it formulates an
{\INVITE} request.  The \header{To} field in the request contains the
address of the callee, and remains unaltered as the request traverses
proxies.  The \header{Request-URI} contains the same address, but may be
rewritten by proxies.  The \header{From} field contains the address of
the caller.  It {\MUST} contain a \header{tag}.  A UAC {\MUST} add a
\header{Contact} header containing an address where it would like to be
contacted for transactions from the callee back to the caller.

If the UAC desires to end the call before a response is received to
the {\INVITE}, it {\SHOULD} send a {\CANCEL}. This {\CANCEL} will
normally result in a 487 response to be returned to the {\INVITE},
indicating successful cancellation. However, it is possible that the
{\CANCEL} and a 200 class response to the {\INVITE} ``pass on the
wire''. In this case, the UAC will receive a 2xx to the {\INVITE}. It
then terminates the call by following the procedures described in
Section \ref{sec:ua-sub}.

\subsection{Callee Issues Response}

When the initial {\INVITE} request is received at the callee, the callee
can accept, redirect, or reject the call.  In all of these cases, it
formulates a response.  The response {\MUST} copy the \header{To},
\header{From}, \header{Call-ID}, \header{CSeq} and \header{Via} fields
from the request.  Additionally, the responding UAS {\MUST} add the
\header{tag} parameter to the \header{To} field in the response.  Since
a request from a UAC may fork and arrive at multiple hosts, the
\header{tag} parameter serves to distinguish, at the UAC, multiple
responses from different UAS's. 

The UAS {\MUST} add a \header{Contact} header field in the response.  It
contains an address where the callee would like to be contacted for
subsequent transactions, including the {\ACK} for the current {\INVITE}. 
The UAS stores the values of the \header{To} and \header{From} field,
including tags.  These become the local and remote addresses of the
call leg, respectively.

If an {\ACK} never arrives for the 2xx to an initial {\INVITE}
transaction, the UAS considers the call down and {\SHOULD} send a
{\BYE}.

\subsection{Caller Receives Response to Initial Request}
\label{sec:ua-ack}

Multiple responses may arrive at the UAC for a single {\INVITE} request,
due to a forking proxy.  Each response is distinguished by the
``\header{tag}'' parameter in the \header{To} header field, and each
represents a distinct call leg, with a distinct call leg identifier. 
The call leg identifier is defined as the combination of the remote
address, local address, and \header{Call-ID}.  The local address is the
value of the \header{From} field, including the \header{tag}, in the 2xx
responses (they will all be the same).  The remote address is the value
of the \header{To} field, including the tag.  Each 2xx response to the
{\INVITE} will differ in the value of the tag in the \header{To} field.

The UAC {\MUST} generate an {\ACK} request for each distinct call leg
created by a 2xx. The \header{Request-URI} and \header{Route} headers
for the {\ACK} are constructed as described in Section
\ref{sec:rr:uasubsequent}. The \header{To} field in the {\ACK} {\MUST}
contain the remote address for the call leg (which includes the
tag). The \header{From} field in the {\ACK} {\MUST} contain the local
address for the call leg. The \header{Call-ID} {\MUST} contain the
\header{Call-ID} for the call leg. The \header{Via} header in the
{\ACK} {\MUST} be indentical to the one in the request being
acknowledged, including any \header{branch} parameter. The
\header{CSeq} number {\MUST} be the same as the {\INVITE} being
acknowledged, but the \header{CSeq} method {\MUST} be {\ACK}. The
\header{ACK} might possibly require a session description in the
body. See Section \ref{sec:sdp} for guidelines. 

If, after acknowledging any 2xx response to an {\INVITE}, the caller
does not want to continue with that call leg, then the caller {\MUST}
terminate the call-leg by sending a {\BYE} request as described in
Section \ref{sec:ua-sub}.

\subsection{Caller or Callee Generate Subsequent Requests}
\label{sec:ua-sub}

Once the call has been established, either the caller or callee {\MAY}
generate {\INVITE} or {\BYE} requests to change or terminate the
call. It {\MAY} initiate other requests as needed. A UA {\MUSTNOT}
initiate a new {\INVITE} transaction within a call leg while one is in
progress. A UA {\MUSTNOT} initiate a new regular transaction
while a regular transaction is in progress. However, a UA {\MAY} initiate a
regular transaction while an {\INVITE} transaction on the same
call leg is in progress. 

If a UAS never receives an {\ACK} for a 2xx response to a
re-{\INVITE}, it {\SHOULD} generate a re-{\INVITE} itself in order to
synchronize the state of the session.

Regardless of whether the caller or callee is generating the new
request, the header fields in the request are set as follows.  For the
desired call leg, the \header{To} header field is set to the remote
address, and the \header{From} header field is set to the local
address (both including tags).  A UAC copies the tag from the final
response into the {\ACK}, but it {\MUSTNOT} copy the tag into any
subsequent requests unless the response was a 200-class response to an
{\INVITE} request.  The \header{To} field of {\CANCEL} requests always
contain exactly the same value as the request it is cancelling.

For an {\INVITE}, the \header{Contact} header field {\MAY} be different
than the \header{Contact} header field sent in a previous response or
request. 

The callee's requests use the caller's \header{To} header field value as
the \header{From} header value and the \header{From} header field value
as the \header{To} header field value.

The network destination and \header{Request-URI} of requests is
determined according to the following rules:

\begin{itemize}

\item If the response from the previous request contained a
\header{Record-Route} header field, the UAC sends the request to the
last entry in the list and removes that entry.  As described in
Section~\ref{sec:Record-Route}, the \header{Request-URI} is set to that
value.

\item Otherwise, if the response for the previous request contained a
\header{Contact} header field, the request is directed to the host and
port identified there. The \header{Request-URI} is set to the value of
the \header{Contact} header. The request does not contain a
\header{Route} header field in this case.

\item Otherwise, the \header{Request-URI} contains the same URL as the
\header{To} header.

\end{itemize}

If the UAC is configured with the address of an outbound proxy server,
the UAC sends the request there, independent of the
\header{Request-URI}.  The outbound proxy is \textbf{NOT} named in the
\header{Request-URI}.  If there is no outbound proxy server, the
\header{Request-URI} determines the network destination. 

If a UAC does not support DNS resolution or the full
\header{Record-Route}/\header{Route} mechanism, it {\MAY} send all
requests to a locally configured outbound proxy.  In that case, that
proxy behaves as described above.  The UAC {\MUST}, however, perform the
mapping of \header{Record-Route} to \header{Route} header fields and
{\MUST} include {\em all} \header{Route} header fields, i.e., the UAC
does not remove the first \header{Route} header field.

\subsection{Receiving Subsequent Requests}
\label{sec:subsequent}

When a request is received during a call, the following checks are made:
 
\begin{enumerate}
\item If the \header{Call-ID} is new, the request is for a new call,
regardless of the values of the \header{To} and \header{From} header
fields.

It is possible that the \header{To} header in an {\INVITE} request has a
tag, but the UAS believes this to be a new call.  This will occur if the
UAS crashed and rebooted in the middle of a call, and the UAC has sent
what it believes to be a re-{\INVITE}.  The UAS {\MAY} either accept or
reject the request.  Accepting the request provides robustness, so that
calls can persist even through crashes.  UAs wishing to support this
capability must choose monotonically increasing \header{CSeq} numbers
even across reboots.  This is because subsequent requests from the
crashed-and-rebooted UA towards the other UA need to have a
\header{CSeq} number higher than previous requests in that direction.

Note also that the crashed-and-rebooted UA will have lost any
\header{Route} headers which would need to be inserted into a subsequent
request.  Therefore, it is possible that the requests may not be
properly forwarded by proxies.

\motivation{RTP media agents allowing restarts need to be robust by
accepting out-of-range timestamps and sequence numbers.}

If the UAS wishes to reject the {\INVITE}, because it does not wish to
recreate the call, it {\MUST} respond to the request with a 481 status
code. A UAC receiving a 481 response for any mid-call request
({\INVITE} or otherwise) {\MUST} consider that call terminated.

\item If the \header{To}, \header{From}, \header{Call-ID}, 
\header{CSeq}, \header{Request-URI}, and branch-ID in the topmost
\header{Via} exactly match (including tags) those of any 
requests received previously, the request is a retransmission. 
 
\item If there was no match to the previous step, the \header{To} and
\header{From} fields are compared against existing call leg local and
remote addresses.  If there is a match, and the \header{CSeq} in the
request is higher than the last \header{CSeq} received on that leg, the
request is a new transaction for an existing call leg. It is possible
for the \header{CSeq} header to be higher than the previous by more
than one. This is not an error condition, and a UAS {\SHOULD} be
prepared to receive and process requests with \header{CSeq} values
more than one higher than the previous received request. A request on
an existing call leg with a lower \header{CSeq} {\MUST} be rejected. 

\end{enumerate}
