Monday, July 26, 2010

Locating SIP Servers

Recently I implemented RFC3263 in one of my projects. In this post I will explain my findings. 
INTRODUCTION 
If we look at a typical SIP configuration also known as SIP “trapezoid”, we will observe that as part of the call flow: - 

  • proxy I needs to determine a SIP server for Domain B.
  • while sending responses back, in case proxy I fails after forwarding the request, proxy II needs to send responses to any backup of proxy I.
PROBLEM DEFINITION
Even if there is no proxy configured, if a UAC wants to send message to UAS, then it needs to determine the IPaddress, port and protocol if not given in the Request-URI. Also DNS lookup processing can be required at every hop i.e. whenever a UAC wants to forward a request to the next hop.
SIP URI Samples 
Typically a SIP uri is in the below format(s): -  
FORMAT1: sip:support@phonesystem.3cx.com
In this case ipaddress, port and protocol needs to be determined.
FORMAT2: sip:support@172.12.23.43:655321; transport=TCP
No DNS query is needed to send the request. But if sending of response fails to the Via address then DNS lookup is required.
Also choice of protocol is very important since SIP supports UDP, TCP, TLS over TCP and SCTP. Incase protocol is not mentioned, in live scenarios, there will be situation whereby UAC has its own list of supported protocols with respective priorities. UAC needs to find the list of supported protocols of the UAS with theirrespective priorities and then find an intersecting protocol b/w the two lists which UAS prefers the most.
Coming to the second aspect, since SIP transactions require some kind of human intervention in order to send responses (for e.g. generating 2xx or 403 response), it is common for sip elements to fail in middle of a transaction. Thus if proxy2 now wants to send back response to proxy1 which failed, then proxy2 needs to discover the backup for proxy1. 
NOTE: Protocol cannot be changed in such scenarios. Only port and the ipaddress need to be
determined.
Now let us see what all SIP UAC’s and UAS’s have to do for DNS processing.

UAC USAGE
  • The UAC uses either the SIP URI in the Request-URI or the first URI in Route header to determine the next hop. However it is to be noted that after applying these procedures, UAC will not rewrite the URI. The information will be use by the transport layer only to send the packet.
  • DNS query is done once per transaction i.e once the sip server has been contacted, all the retransmissions of the request, ACK for non 2xx responses and very importantly Cancel will be sent to the same address.
  • Ack to 2xx is a new transaction in itself and it also has considerations of Record route. If a UA didn’t record route, it will not receive the ACK. 
Rules for selecting the transport protocol:-
  • If the URI specifies the transport protocol, that transport is used. Otherwise
  • If target is numeric IP address, use UDP for a SIP URI and TCP for SIPS URI. Otherwise
  • If target is not numeric but port is specified, use UDP for SIP URI and TCP for SIPS URI. Otheriwse
  • If nothing is specified i.e no transport or port is specified and target is not numeric IP address, perform NAPTR query for the domain in the URI. The relevant records for transport selection are those with NAPTR service fields with values “ SIP+D2X” and “SIPS+D2X”, where X represents transport. Eg D2U for UDP, D2T for TCP, D2S for SCTP. These NAPTR records map domain to SRV record for contacting a server with the specific transport protocol in the NAPTR services field. The resource record will contain an empty regular expression and replacement value, which is the SRV record for that particular transport protocol. There is separate entry for each transport protocol supported.Consider if sip:user@example.com needs to be solved. NAPTR query for the domain returned the following NAPTR records:-
;          order pref flags service      regexp  replacement
 IN NAPTR 50   50  "s"  "SIPS+D2T"     ""  _sips._tcp.example.com.
 IN NAPTR 90   50  "s"  "SIP+D2T"      ""  _sip._tcp.example.com
 IN NAPTR 100  50  "s"  "SIP+D2U"      ""  _sip._udp.example.com.
This indicates that the server supports TLS over TCP, TCP, and UDP in the given preference. Since the client supports TCP and UDP, TCP will be used, targeted to a host determined by an SRV lookup of _sip._tcp.example.com. That lookup would return
   ;;          Priority Weight Port   Target
       IN SRV  0        1      5060   server1.example.com
       IN SRV  0        2      5060   server2.example.com 
If  no NAPTR records are found, the client constructs SRV queries for transport protocols it supports, and if no NAPTR records are found, the client constructs SRV queries for transport protocols it supports. If no SRV records are found, the client should use TCP for a SIPS URI, and UDP for a SIP URI.
Determining Port and IP Address 
  • If target is a numeric IP address, the client uses that address. If the URI also contains a port, it uses that port. If no port is specified, it uses the default port for the particular transport protocol. Otherwise.  
  • If the Target was not a numeric IP address, but a port is present in the uri, the client performs an A or AAAA record lookup of the domain name giving the list of IP addresses which can be contacted at specified port and protocol already determined. 
Server Usage
A SIP UAS normally sends the response on the connection the request arrived (TCP) or to the UrI and port present in VIA field. DNS query is required to provide failover support. 
Following rules are followed:- 
  • Examine the sent-by in the topmost VIA field. If it contains a numeric IP address, response will be sent to that address, take protocol from VIA field and port for sent-by. If port is not given, then use default port for the protocol. Otherwise  
  • If sent-by field contains domain name and port, query for A or AAAA records with that name. This will give a list of entries which can be tried one by one. Otherwise  
  • If domain name and no port is given in sent-by field, query SRV records for that domain name using “_sips” for TLS in VIA field, “_sip” for others. This will give list of addresses which can be tried one by one giving preference to the topmost. 
For outbound call
Host name
Protocol
Port number
Action
Numeric
UDP
Not present
Port no is 5060
FQDN
UDP
Present
AAAA query for ip address with the specified port.
FQDN
UDP
Not Present
Do SRV query.
If records found follow rfc 2782
If no records are found, then do AAAA query and use the IP address and default port of 5060.
Numeric
UDP
Present
No DNS
FQDN
Not present
Not Present
NAPTR Query