SIP Registration Example

本文介绍SIP协议中注册过程的工作原理。重点讲述了Heisenberg如何通过发送REGISTER请求到SIP注册服务器来完成注册流程,包括请求和响应消息的具体格式、有效期限及更新机制等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this example, shown in Figure 2.3, Heisenberg sends a SIP REGISTER request to a type of SIP server known as a registrar server. The SIP registrar server receives the message uses the information in the request to update the database used by proxies to route SIP requests. Contained in the REGISTER message To header is the SIP URI address of Heisenberg. This is Heisenberg's "well-known" address, perhaps printed on his business card or published on a Web page or in a directory. Also contained in the REGISTER is a Contact URI, which represents the current device (and its IP address) that the user Heisenberg is currently using. The registrar binds the SIP URI of Heisenberg and the IP address of the device in a database that can be used, for example, by the proxy server in Figure 2.2 to locate Heisenberg. When a proxy server with access to the database receives an INVITE request addressed to Heisenberg's URI (i.e., an incoming call), the request will be proxied to the Contact URI of the currently registered device.

Click To expand
Figure 2.3: SIP registration example.

This registration has no real counterpart in the telephone network, but it is very similar to the registration a wireless phone performs when it is turned on. A cell phone sends its identity to the base station (BS), which then forwards the location and phone number of the cell phone to a home location register (HLR). When the mobile switching center (MSC) receives an incoming call, it consults the HLR to get the current location of the cell phone. Further aspects of SIP mobility are discussed in Chapter 9.

The REGISTER message sent by Heisenberg to the SIP registrar server has the form:

     REGISTER sip:registrar.munich.de SIP/2.0
     Via: SIP/2.0/UDP 200.201.202.203:5060;branch=z9hG4bKus19
     Max-Forwards: 70
     To: Werner Heisenberg <sip:werner.heisenberg@munich.de>
     From: Werner Heisenberg <sip:werner.heisenberg@munich.de>
      ;tag=3431
     Call-ID: 23@200.201.202.203
     CSeq: 1 REGISTER
     Contact: sip:werner.heisenberg@200.201.202.203
     Content-Length: 0

The Request-URI in the start line of the message contains the address of the registrar server. In a REGISTER request, the To header field contains the URI that is being registered, in this case sip:werner.heisenberg@munich.de. This results in the To and From header fields usually being the same, although an example of third-party registration is given in Section 4.1.2. The SIP URI in the Contact address is stored by the registrar.

The registrar server acknowledges the successful registration by sending a 200 OK response to Heisenberg. The response echoes the Contact information that has just been stored in the database and includes a To tag:

     SIP/2.0 200 OK
     Via: SIP/2.0/UDP 200.201.202.203:5060;branch=z9hG4bKus19
     To: Werner Heisenberg <sip:werner.heisenberg@munich.de>;tag=8771
     From: Werner Heisenberg <sip:werner.heisenberg@munich.de>
      ;tag=3431
     Call-ID: 23@200.201.202.203
     CSeq: 1 REGISTER
     Contact: <sip:werner.heisenberg@munich.de>;expires=3600
     Content-Length: 0

The Contact URI is returned along with an expires parameter, which indicates how long the registration is valid, which in this case is 1 hour (3,600 seconds). If Heisenberg wants the registration to be valid beyond that interval, he must send another REGISTER request within the expiration interval.

Registration is typically automatically performed on initialization of a SIP device and at regular intervals determined by the expiration interval chosen by the registrar server. Registration is an additive process—more than one device can be registered against a SIP URI. If more than one device is registered, a proxy may forward the request to either or both devices, either in a sequential or parallel search. Additional register operations can be used to clear registrations or retrieve a list of currently registered devices.

### 实现SIP电话呼叫的关键要素 为了实现基于SIP协议的电话呼叫功能,需理解并构建几个核心组件。这些组件包括但不限于注册服务器、代理服务器以及用户代理(UA)。用户代理分为两个部分:客户端和服务器端。客户端用于发起请求,而服务器端则响应来自其他用户的邀请。 #### 用户代理初始化与配置 在启动一次SIP呼叫之前,必须先完成用户代理的设置工作。此过程涉及定义本地地址、监听端口以及其他必要的参数。对于大多数应用而言,这部分可以通过调用库函数来简化操作: ```c // 初始化PJSUA-LIB库 pj_status_t status; status = pjsua_create(); if (status != PJ_SUCCESS) { // 错误处理... } pjsua_config cfg; pjsua_logging_config log_cfg; pjsua_init(&cfg, &log_cfg); ``` 上述代码片段展示了如何使用[PJProject](https://www.pjsip.org/)中的`pjsua-lib`库来进行基本的环境搭建[^2]。 #### 注册至SIP服务提供商 一旦用户代理被正确初始化之后,下一步就是将其注册到指定的服务商处。这一步骤允许设备告知网络自己当前的位置以便能够接收到来电通知。 ```c pjsua_acc_config acc_cfg; pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL); char sip_uri[] = "sip:user@domain.com"; pjsua_acc_set_registration(acc_id, PJ_TRUE, sip_uri); ``` 这段C语言代码说明了怎样通过API接口向特定URI执行注册动作[^3]。 #### 发起呼叫请求 当一切准备就绪后,就可以尝试拨打一通测试性的语音通话了。此时应构造一个INVITE消息并通过合适的传输方式发送出去给目标联系人。 ```c const char *uri = "sip:bob@example.com"; pjsua_call_setting callSetting; callSetting.aud_cnt = 1; /* Enable audio */ pjsua_call_make_call(acc_id, uri, &callSetting, NULL, NULL, &call_id); ``` 这里给出了创建一个新的呼叫实例的具体方法,并指定了要连接的目标号码作为目的地[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值