SIP Proxy Connection and Registration

The AnyFirewall™ Engine can be used to establish a connection to a SIP proxy, i.e., to register with a SIP proxy using UDP, TCP, or TLS as transport protocols. For this purpose, the AnyFirewall™ Engine determines the available connection options to the SIP proxy, e.g., whether UDP can be used to connect to the SIP proxy or whether an HTTP tunnel must be setup.

Before connecting to the SIP proxy to send or receive SIP messages, it is necessary to create a channel. Channels represent communication endpoints and behave similar to sockets for an application developer. Once the channel is created, it is necessary to call Connect. Connecting a channel means setting up the internal data structures, detecting the public IP address and port and allocating ports on the AnyFirewall™ Server.

You must detect the public port in order to populate the SIP REGISTER message with the correct Contact field parameters.
The following code snippet contains the necessary function calls to create a channel and REGISTER with a SIP proxy. The sample code creates a channel for the communication with the SIP proxy and connects to the SIP proxy and exchanges SIP messages with the SIP proxy.
m_iSipChannel = m_pAFEngine->Create(serverStoreID, AF_CHANNEL_UDP, 0, 0, 0);
if (m_iSipChannel < 0)
{
  // Handle errors here
}
m_bSipTransportUdp = true;
// Connect to the SIP proxy
// This function determines the best possible connection method:
// direct UDP if the firewall does not block UDP traffic,
// TCP or HTTP tunneling will be used otherwise.
if (!m_pAFEngine->Connect(m_iSipChannel, m_pAFEngine->CreateHost(
                                AF_HOST_PUBLIC,
                                sSipProxyAddress,
                                iSipProxyPort,
                                AF_PROTOCOL_UDP,
                                NULL)
                          ))
{
  // Handle errors here
  m_pAFEngine->Close(m_iSipChannel);
}
// Retrieve the description that will be used in communicating with the
// server. This information is placed in the Contact field of the
// REGISTER message.
string sRegIPPort = m_pAFEngine->GetLocalAddress(m_iSipChannel);
if (m_sMyRaddr.empty())
{
  m_sMyRaddr = m_pAFEngine->GetHostAddress(sRegIPPort);
}
if (m_iMyRport <= 0)
{
  m_iMyRport = m_pAFEngine->GetHostPort(sRegIPPort);
}
// Create the SIP REGISTER message using the parameters obtained from
// calling the AFE API GetLocalAddress()
string sRegMsg = m_pSipUtil->BuildRegisterRequest(m_sUsername,
                                m_sMyRaddr,
                                m_iMyRport,
                                m_sSipProxyDomain,
                                m_iTransportType
                          );
// Send the SIP REGISTER message
if (m_pAFEngine->Send(m_iSipChannel, sRegMsg.c_str(),
                               (int)sRegMsg.length(),
                               AF_NON_BLOCKING) < 0
                          )
{
  // Handle the errors
}
iRet = m_pAFEngine->Recv(m_iSipChannel,
                              sSIPRegisterResponse,
                              SIPRegisterResponseLength,
                              1000
                         );


The channel used to connect to the SIP proxy is created using the method Create. This function creates a channel of type AF_CHANNEL_UDP. This channel type is used when connecting to servers or clients that support UDP.

The host description, generated by CreateHost in this case, defines that the SIP proxy should be contacted using UDP (AF_PROTOCOL_UDP). The other protocols that can be used in this context are TCP (AF_PROTOCOL_TCP) or TLS/SSL (AF_PROTOCOL_TLS).

The method Connect initiates the connection to the SIP proxy. The method determines whether the SIP proxy can be directly accessed. If this is not possible, it will try to relay SIP message through the AnyFirewall™ Server instead of attempting a direct connection (this behavior can be changed using channel option EAfOptionRelayFallbackTimeout). In many cases, SIP proxies only support UDP. In these situations, it is impossible to create a direct connection from a client in a UDP-blocked environment. Examples are corporate networks where only an HTTP proxy can be used to connect to the public Internet. In order to overcome this problem, the AnyFirewall™ Engine allocates a UDP port on the AnyFirewall™ Server and relays the SIP messages through the AnyFirewall™ Server. The AnyFirewall™ Server then translates the SIP messages received from the AnyFirewall™ Engine from TCP to UDP and forwards them to the SIP proxy using UDP. In the same way, messages received from the SIP proxy will be translated from UDP to TCP and forwarded to the AnyFirewall™ Engine.