基於GBT28181:SIP協議組件開發-----------第四篇SIP注冊流程eXosip2實現(一)


原創文章,引用請保證原文完整性,尊重作者勞動,原文地址http://www.cnblogs.com/qq1269122125/p/3945294.html。

上章節講解了利用自主開發的組件SIP組件libGBT28181SipComponent.so實現Linux 32平台的UAS和UAC,因為該組件采用很多新的技術,所以采用該組件效率無疑是很高的。但是對於想學習SIP協議,或者想了解eXosip2開發流程的程序員,是不能從根本上了解學習的。因為所有的功能都封裝在libGBT28181SipComponent.so中。本講將講解一個用eXosip2庫實現的Demo 程序。該Demo中包括UAS和UAC的實現。當然Demo實現比較粗糙,主要目的就是講解eXosip2庫的用法。該Demo講的也是注冊的過程,注冊的流程在上一節已經有了詳細的講解,再次不贅述。源代碼不多,直接上代碼。

一.源代碼UAS main.cpp

  1 /*
  2  ===============================================================
  3  GBT28181 SIP組件libGBT28181SipComponent.so注冊實現
  4  作者:程序人生
  5  博客地址:http://blog.csdn.net/hiwubihe
  6  QQ:1269122125
  7  注:請尊重原作者勞動成果,僅供學習使用,請勿盜用,違者必究!
  8  ================================================================
  9  */
 10 
 11 #include <iostream>
 12 #include <string>
 13 #include <sstream>
 14 #include <osipparser2/osip_message.h>
 15 #include <osipparser2/osip_parser.h>
 16 #include <osipparser2/osip_port.h>
 17 
 18 #include <eXosip2/eXosip.h>
 19 #include <eXosip2/eX_setup.h>
 20 #include <eXosip2/eX_register.h>
 21 #include <eXosip2/eX_options.h>
 22 #include <eXosip2/eX_message.h>
 23 #include <arpa/inet.h>
 24 #include <sys/types.h>
 25 #include <sys/socket.h>
 26 
 27 using namespace std;
 28 
 29 #define LISTEN_ADDR ("192.168.50.57")
 30 #define UASPORT (5060)
 31 
 32 
 33 //該系數是由UAS維護的,UAS在接收到UAC的未鑒權報文后,給UAC回復401,在該報文中必須要帶相關認證系數和認證方法
 34 //UAS賦值的認證隨機數
 35 #define NONCE "9bd055"
 36 //UAS默認加密算法
 37 #define ALGORITHTHM "MD5"
 38 
 39 
 40 //SIP From頭部
 41 class CSipFromHeader
 42 {
 43 public:
 44     CSipFromHeader()
 45     {
 46     }
 47     ~CSipFromHeader()
 48     {
 49     }
 50     void SetHeader(string addrCod, string addrI, string addrPor)
 51     {
 52         addrCode = addrCod;
 53         addrIp = addrI;
 54         addrPort = addrPor;
 55     }
 56     string GetFormatHeader()
 57     {
 58         std::stringstream stream;
 59         stream << "<sip: " << addrCode << "@" << addrIp << ":" << addrPort
 60                 << ">";
 61         return stream.str();
 62     }
 63     //主機名稱
 64     string GetRealName()
 65     {
 66         std::stringstream stream;
 67         stream << addrIp;
 68         return stream.str();
 69     }
 70 private:
 71     string addrCode;
 72     string addrIp;
 73     string addrPort;
 74 };
 75 
 76 //SIP Contract頭部
 77 class CContractHeader: public CSipFromHeader
 78 {
 79 public:
 80     CContractHeader()
 81     {
 82     }
 83     ~CContractHeader()
 84     {
 85     }
 86     void SetContractHeader(string addrCod, string addrI, string addrPor,
 87             int expire)
 88     {
 89         SetHeader(addrCod, addrI, addrPor);
 90         expires = expire;
 91     }
 92     string GetContractFormatHeader(bool bExpires)
 93     {
 94         if (!bExpires)
 95         {
 96             return GetFormatHeader();
 97         }
 98         else
 99         {
100             string sTmp = GetFormatHeader();
101             std::stringstream stream;
102             stream << ";" << "expires=" << expires;
103             sTmp += stream.str();
104             return sTmp;
105         }
106 
107     }
108 private:
109     int expires;
110 };
111 
112 struct SipContextInfo
113 {
114     //Sip層返回的請求的標志 響應時返回即可
115     int sipRequestId;
116     //維護一次注冊
117     string callId;
118     //消息所屬的功能方法名字符串
119     string method;
120     //地址編碼@域名或IP地址:連接端口,例如sip:1111@127.0.0.1:5060
121     CSipFromHeader from;
122     //地址編碼@域名或IP地址:連接端口,例如sip:1111@127.0.0.1:5060
123     CSipFromHeader proxy;
124     //地址編碼@域名或IP地址:連接端口,例如sip:1111@127.0.0.1:5060
125     CContractHeader contact;
126     //消息內容,一般為DDCP消息體XML文檔,或者具體協議幀要求的其他字符串文本
127     string content;
128     //響應狀態信息
129     string status;
130     //超時,時間單位為秒
131     int expires;
132 };
133 
134 struct SipAuthInfo
135 {
136     //平台主機名
137     string digestRealm;
138     //平台提供的隨機數
139     string nonce;
140     //用戶名
141     string userName;
142     //密碼
143     string response;
144     //“sip:平台地址”,不需要uac賦值
145     string uri;
146     //加密算法MD5
147     string algorithm;
148 };
149 
150 struct sipRegisterInfo
151 {
152     SipContextInfo baseInfo;
153     SipAuthInfo authInfo;
154     bool isAuthNull;
155 };
156 
157 void parserRegisterInfo(osip_message_t*request, int iReqId,
158         sipRegisterInfo &regInfo)
159 {
160     std::stringstream stream;
161     regInfo.baseInfo.method = request->sip_method;
162     regInfo.baseInfo.from.SetHeader(request->from->url->username,
163             request->from->url->host, request->from->url->port);
164     regInfo.baseInfo.proxy.SetHeader(request->to->url->username,
165             request->to->url->host, request->to->url->port);
166     //獲取expires
167     osip_header_t* header = NULL;
168     {
169         osip_message_header_get_byname(request, "expires", 
170                 0, &header);
171         if (NULL != header && NULL != header->hvalue)
172         {
173             regInfo.baseInfo.expires = atoi(header->hvalue);
174         }
175     }
176     //contact字段
177     osip_contact_t* contact = NULL;
178     osip_message_get_contact(request, 0, &contact);
179     if (NULL != contact)
180     {
181         regInfo.baseInfo.contact.SetContractHeader(contact->url->username,
182                 contact->url->host, contact->url->port,
183                 regInfo.baseInfo.expires);
184     }
185     //注冊返回 由發送方維護的請求ID 接收方接收后原樣返回即可
186     regInfo.baseInfo.sipRequestId = iReqId;
187     //CALL_ID
188     {
189         stream.str("");
190         stream << request->call_id->number;
191         regInfo.baseInfo.callId = stream.str();
192     }
193     //解析content消息
194     osip_body_t * body = NULL;
195     osip_message_get_body(request, 0, &body);
196     if (body != NULL)
197     {
198         stream.str("");
199         stream << body->body;
200         regInfo.baseInfo.content = stream.str();
201     }
202     //鑒權信息
203     osip_authorization_t* authentication = NULL;
204     {
205         osip_message_get_authorization(request, 0, &authentication);
206         if (NULL == authentication)
207         {
208             regInfo.isAuthNull = true;
209         }
210         else
211         {
212             regInfo.isAuthNull = false;
213             stream.str("");
214             stream << authentication->username;
215             regInfo.authInfo.userName = stream.str();
216             stream.str("");
217             stream << authentication->algorithm;
218             regInfo.authInfo.algorithm = stream.str();
219             stream.str("");
220             stream << authentication->realm;
221             regInfo.authInfo.digestRealm = stream.str();
222             stream.str("");
223             stream << authentication->nonce;
224             regInfo.authInfo.nonce = stream.str();
225             stream.str("");
226             stream << authentication->response;
227             regInfo.authInfo.response = stream.str();
228             stream.str("");
229             stream << authentication->uri;
230             regInfo.authInfo.uri = stream.str();
231         }
232     }
233     authentication = NULL;
234 }
235 
236 //打印接收到的響應報文
237 void printRegisterPkt( sipRegisterInfo&info)
238 {
239     cout<<"接收到報文:"<<endl;
240     cout<<"=============================================="
241             "=================="<<endl;
242     cout << "method:" << info.baseInfo.method << endl;
243     cout << "from:    " << info.baseInfo.from.GetFormatHeader() << endl;
244     cout << "to:" << info.baseInfo.proxy.GetFormatHeader() << endl;
245     cout << "contact:" << info.baseInfo.contact.GetContractFormatHeader(false)
246             << endl;
247 
248     //注冊返回 由發送方維護的請求ID 接收方接收后原樣返回即可
249     cout << "sipRequestId:" << info.baseInfo.sipRequestId << endl;
250     //CALL_ID
251     cout << "CallId:" << info.baseInfo.callId << endl;
252     //解析content消息
253     cout << "body:" << info.baseInfo.content << endl;
254     //獲取expires
255     cout << "expires:" << info.baseInfo.expires << endl;
256     //鑒權信息
257     if (info.isAuthNull)
258     {
259         cout << "當前報文未提供鑒權信息!!!" << endl;
260     }
261     else
262     {
263         cout << "當前報文鑒權信息如下:" << endl;
264         cout << "username:" << info.authInfo.userName << endl;
265         cout << "algorithm:" << info.authInfo.algorithm << endl;
266         cout << "Realm:" << info.authInfo.digestRealm << endl;
267         cout << "nonce:" << info.authInfo.nonce << endl;
268         cout << "response:" << info.authInfo.response << endl;
269         cout << "uri:" << info.authInfo.uri << endl;
270     }
271      cout<<"=================================================="
272              "=============="<<endl;
273     return;
274 }
275 void sendRegisterAnswer( sipRegisterInfo&info)
276 {
277     osip_message_t* answer = NULL;
278     int iStatus;
279     if (info.isAuthNull)
280     {
281         iStatus = 401;
282     }
283     else
284     {
285         iStatus = 200;
286     }eXosip_lock();
287     {
288         int result = ::eXosip_message_build_answer(info.baseInfo.sipRequestId,
289                 iStatus, &answer);
290         if (iStatus == 401)
291         {
292             //由SIP庫生成認證方法和認證參數發送客戶端
293             std::stringstream stream;
294             string nonce=NONCE;
295             string algorithm=ALGORITHTHM;
296             stream << "Digest realm=\"" << info.baseInfo.from.GetRealName() 
297                     << "\",nonce=\"" << nonce
298                     << "\",algorithm=" << algorithm;
299 
300             osip_message_set_header(answer, "WWW-Authenticate",
301                     stream.str().c_str());
302              cout<<"======================================================="
303                      "========="<<endl;
304              cout<<"發送401報文"<<endl;
305              cout<<"========================================================"
306                      "========"<<endl;
307         }
308         else if (iStatus == 200)
309         {
310             osip_message_set_header(answer, "Contact",
311                     info.baseInfo.contact.GetContractFormatHeader(true).c_str());
312              cout<<"========================================================="
313                      "======="<<endl;
314              cout<<"發送200報文"<<endl;
315              cout<<"=========================================================="
316                      "======"<<endl;
317             //string_t b = "<sip: 100110000101000000@192.168.31.18:5061>;expires=600";
318             //osip_message_set_header(answer, "Contact", b.c_str());
319         }
320         else
321         {
322             //Do nothing
323         }
324 
325         if (OSIP_SUCCESS != result)
326         {
327             ::eXosip_message_send_answer(info.baseInfo.sipRequestId, 400, NULL);
328         }
329         else
330         {
331             //發送消息體
332             ::eXosip_message_send_answer(info.baseInfo.sipRequestId, iStatus,
333                     answer);
334         }
335         if (0 == info.baseInfo.expires)
336         {
337             eXosip_register_remove(info.baseInfo.sipRequestId);
338         }
339     }eXosip_unlock();
340 }
341 void OnRegister(eXosip_event_t *osipEvent)
342 {
343     sipRegisterInfo regInfo;
344     parserRegisterInfo(osipEvent->request, osipEvent->tid, regInfo);
345     //打印報文
346     printRegisterPkt(regInfo);
347     //發送應答報文
348     sendRegisterAnswer(regInfo);
349 
350 }
351 
352 int main()
353 {
354 
355     int result = OSIP_SUCCESS;
356     // init exosip.
357     if (OSIP_SUCCESS != (result = eXosip_init()))
358     {
359         printf("eXosip_init failure.\n");
360         return 1;
361     }
362     cout << "eXosip_init success." << endl;
363     //
364     //        if (null_ptr != this->receiveSipMessageCallback || null_ptr
365     //                != this->sendSipMessageCallback)
366     //        {
367     //            if (OSIP_SUCCESS != (result = ::eXosip_set_cbsip_message(
368     //                    &Sip::MessageCallback)))
369     //            {
370     //                return;
371     //            }
372     //        }
373     eXosip_set_user_agent(NULL);
374 
375     if (OSIP_SUCCESS != eXosip_listen_addr(IPPROTO_UDP, NULL, UASPORT, AF_INET,
376             0))
377     {
378         printf("eXosip_listen_addr failure.\n");
379         return 1;
380     }
381 
382     if (OSIP_SUCCESS != eXosip_set_option(
383     EXOSIP_OPT_SET_IPV4_FOR_GATEWAY,
384             LISTEN_ADDR))
385     {
386         return -1;
387     }
388     //開啟循環消息,實際應用中可以開啟多線程同時接收信號
389     eXosip_event_t* osipEventPtr = NULL;
390 
391     while (true)
392     {
393         // Wait the osip event.
394         osipEventPtr = ::eXosip_event_wait(0, 200);// 0的單位是秒,500是毫秒
395         // If get nothing osip event,then continue the loop.
396         if (NULL == osipEventPtr)
397         {
398             continue;
399         }
400         // 事件處理
401 
402         switch (osipEventPtr->type)
403         {
404 
405         //需要繼續驗證REGISTER是什么類型
406         case EXOSIP_REGISTRATION_NEW:
407             OnRegister(osipEventPtr);
408             break;
409         case EXOSIP_MESSAGE_NEW:
410         {
411             if (!strncmp(osipEventPtr->request->sip_method, "REGISTER", 
412                     strlen("REGISTER")))
413             {
414                 OnRegister(osipEventPtr);
415             }
416             else if (!strncmp(osipEventPtr->request->sip_method, "MESSAGE",
417                     strlen("MESSAGE")))
418             {
419                 //
420             }
421         }
422             break;
423         default:
424             cout
425                     << "The sip event type that not be precessed.the event "
426                             "type is : "
427                     << osipEventPtr->type;
428             break;
429         }
430         //ProcessSipEvent(this->osipEventPtrParam);
431         eXosip_event_free(osipEventPtr);
432         osipEventPtr = NULL;
433     }
434 
435     return 0;
436 }

二.UAC源代碼

 參看下一篇 基於GBT28181:SIP協議組件開發-----------第四篇SIP注冊流程eXosip2實現(二)

三.測試效果

1.UAS啟動后,接受到報文效果

2.UAS發送401回復報文

3.UAS接受到帶鑒權報文

4.UAS回復200OK

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM