织梦CMS - 轻松建站从此开始!

罗索

osip2/eXosip2调试笔记

jackyhwei 发布于 2011-01-17 16:48 点击:次 
软件版本: libosip2-3.3.0.tar.gz libeXosip2-3.3.0.tar.gz ./configure --prefix=/opt/sip/target --disable-static make make install 测试代码: 代码来源:http://blog.csdn.net/bat603/archive/2006/11/15/1386277.aspx
TAG:

软件版本:

libosip2-3.3.0.tar.gz
libeXosip2-3.3.0.tar.gz
./configure --prefix=/opt/sip/target --disable-static
make
make install

测试代码:


代码来源:http://blog.csdn.net/bat603/archive/2006/11/15/1386277.aspx

1、UAS

//*******************UAS*******************//

  1. #include <eXosip2/eXosip.h> 
  2. #include <osip2/osip_mt.h> 
  3. #include <stdio.h> 
  4. #include <stdlib.h> 
  5. #include <netinet/in.h> 
  6. #include <sys/socket.h> 
  7. #include <sys/types.h> 
  8.  
  9. int 
  10. main (int argc, char *argv[]) 
  11.   eXosip_event_t *je = NULL; 
  12.   osip_message_t *ack = NULL; 
  13.   osip_message_t *invite = NULL; 
  14.   osip_message_t *answer = NULL; 
  15.   sdp_message_t *remote_sdp = NULL; 
  16.  
  17.   int call_id, dialog_id; 
  18.   int i,j; 
  19.   int id; 
  20.   char *sour_call = "sip:133@192.168.0.133"
  21.   char *dest_call = "sip:140@192.168.0.140:5060"
  22.  
  23.   char command; 
  24.   char tmp[4096]; 
  25.   char localip[128]; 
  26.  
  27.   int pos = 0; 
  28.  
  29.   //初始化sip 
  30.   i = eXosip_init (); 
  31.   if (i != 0) 
  32.     { 
  33.       printf ("Can't initialize eXosip!\n"); 
  34.       return -1; 
  35.     } 
  36.   else 
  37.     { 
  38.       printf ("eXosip_init successfully!\n"); 
  39.     } 
  40.  
  41.   i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5060, AF_INET, 0); 
  42.   if (i != 0) 
  43.     { 
  44.       eXosip_quit (); 
  45. fprintf (stderr, "eXosip_listen_addr error!\nCouldn't initialize transport layer!\n"); 
  46.     } 
  47.  
  48.   for(;;) 
  49.     { 
  50.       //侦听是否有消息到来 
  51.       je = eXosip_event_wait (0,50); 
  52.  
  53.       //协议栈带有此语句,具体作用未知 
  54.       eXosip_lock (); 
  55.       eXosip_default_action (je); 
  56.       eXosip_automatic_refresh (); 
  57.       eXosip_unlock (); 
  58.  
  59.       if (je == NULL)//没有接收到消息 
  60.     continue
  61.       // printf ("the cid is %s, did is %s\n", je->did, je->cid); 
  62.       switch (je->type) 
  63.     { 
  64.     case EXOSIP_MESSAGE_NEW://新的消息到来 
  65.       printf (" EXOSIP_MESSAGE_NEW!\n"); 
  66.       if (MSG_IS_MESSAGE (je->request))//如果接受到的消息类型是MESSAGE 
  67.         { 
  68.       { 
  69.         osip_body_t *body; 
  70.         osip_message_get_body (je->request, 0, &body); 
  71.         printf ("I get the msg is: %s\n", body->body); 
  72.         //printf ("the cid is %s, did is %s\n", je->did, je->cid); 
  73.       } 
  74.       //按照规则,需要回复200 OK信息 
  75.       eXosip_message_build_answer (je->tid, 200,&answer); 
  76.       eXosip_message_send_answer (je->tid, 200,answer); 
  77.         } 
  78.       break
  79.     case EXOSIP_CALL_INVITE: 
  80.       //得到接收到消息的具体信息 
  81.             printf ("Received a INVITE msg from %s:%s, UserName is %s
  82. , password is %s\n",je->request->req_uri->host, 
  83.           je->request->req_uri->port, je->request->req_uri->username
  84. , je->request->req_uri->password); 
  85.       //得到消息体,认为该消息就是SDP格式. 
  86.       remote_sdp = eXosip_get_remote_sdp (je->did); 
  87.       call_id = je->cid; 
  88.       dialog_id = je->did; 
  89.       
  90.       eXosip_lock (); 
  91.       eXosip_call_send_answer (je->tid, 180, NULL); 
  92.       i = eXosip_call_build_answer (je->tid, 200, &answer); 
  93.       if (i != 0) 
  94.         { 
  95.           printf ("This request msg is invalid!Cann't response!\n"); 
  96.           eXosip_call_send_answer (je->tid, 400, NULL); 
  97.         } 
  98.       else 
  99.         { 
  100.           snprintf (tmp, 4096, 
  101.                "v=0\r\n" 
  102.                "o=anonymous 0 0 IN IP4 0.0.0.0\r\n" 
  103.                "t=1 10\r\n" 
  104.                "a=username:rainfish\r\n" 
  105.                "a=password:123\r\n"); 
  106.           
  107.           //设置回复的SDP消息体,下一步计划分析消息体 
  108.           //没有分析消息体,直接回复原来的消息,这一块做的不好。 
  109.           osip_message_set_body (answer, tmp, strlen(tmp)); 
  110.           osip_message_set_content_type (answer, "application/sdp"); 
  111.           
  112.           eXosip_call_send_answer (je->tid, 200, answer); 
  113.           printf ("send 200 over!\n"); 
  114.         } 
  115.       eXosip_unlock (); 
  116.       
  117.       //显示出在sdp消息体中的 attribute 的内容,里面计划存放我们的信息 
  118.       printf ("the INFO is :\n"); 
  119.       while (!osip_list_eol (remote_sdp->a_attributes, pos)) 
  120.         { 
  121.           sdp_attribute_t *at; 
  122.           
  123.           at = (sdp_attribute_t *) osip_list_get (remote_sdp->a_attributes, pos); 
  124.           printf ("%s : %s\n", at->a_att_field, at->a_att_value);
  125. //这里解释了为什么在SDP消息体中属性a里面存放必须是两列 
  126.           
  127.           pos ++; 
  128.         } 
  129.       break
  130.     case EXOSIP_CALL_ACK: 
  131.       printf ("ACK recieved!\n"); 
  132.       // printf ("the cid is %s, did is %s\n", je->did, je->cid); 
  133.       break
  134.     case EXOSIP_CALL_CLOSED: 
  135.       printf ("the remote hold the session!\n"); 
  136.       // eXosip_call_build_ack(dialog_id, &ack); 
  137.       //eXosip_call_send_ack(dialog_id, ack); 
  138.       i = eXosip_call_build_answer (je->tid, 200, &answer); 
  139.       if (i != 0) 
  140.         { 
  141.           printf ("This request msg is invalid!Cann't response!\n"); 
  142.           eXosip_call_send_answer (je->tid, 400, NULL); 
  143.         
  144.           } 
  145.       else 
  146.         { 
  147.           eXosip_call_send_answer (je->tid, 200, answer); 
  148.           printf ("bye send 200 over!\n"); 
  149.         }        
  150.       break
  151.  
  152.     case EXOSIP_CALL_MESSAGE_NEW://至于该类型和EXOSIP_MESSAGE_NEW的区别,源代码这么解释的 
  153.     /* 
  154.     /* request related events within calls (except INVITE) */ 
  155.           EXOSIP_CALL_MESSAGE_NEW,            /**< announce new incoming request. */ 
  156.     /* response received for request outside calls */ 
  157.             EXOSIP_MESSAGE_NEW,
  158. /**< announce new incoming request. */ 
  159. 我也不是很明白,理解是: EXOSIP_CALL_MESSAGE_NEW是一个呼叫中的新
  160. 的消息到来,比如ring trying都算,所以在接受到后必须判断 
  161. 该消息类型,EXOSIP_MESSAGE_NEW而是表示不是呼叫内的消息到来。 
  162. 该解释有不妥地方,仅供参考。 
  163.     */ 
  164.       printf(" EXOSIP_CALL_MESSAGE_NEW\n"); 
  165.       if (MSG_IS_INFO(je->request))//如果传输的是INFO方法 
  166.       { 
  167.                    eXosip_lock (); 
  168.               i = eXosip_call_build_answer (je->tid, 200, &answer); 
  169.               if (i == 0) 
  170.                 { 
  171.                   eXosip_call_send_answer (je->tid, 200, answer); 
  172.                 } 
  173.               eXosip_unlock (); 
  174.           { 
  175.         osip_body_t *body; 
  176.         osip_message_get_body (je->request, 0, &body); 
  177.         printf ("the body is %s\n", body->body); 
  178.           } 
  179.       } 
  180.       break
  181.     default
  182.       printf ("Could not parse the msg!\n"); 
  183.     } 
  184.     } 

 >>  gcc uastest.c -o uastest -L/opt/sip/target/lib/ -I/opt/sip/target/include/ -leXosip2

问题及其解决:

编译问题:

uastest.c: In function `main':
uastest.c:118: incompatible type for argument 1 of `osip_list_eol'
uastest.c:122: incompatible type for argument 1 of `osip_list_get'

解决方法:

The osip_list_* interface has been changed in latest versions of

osip and osip_list_t in structures are not pointer any more.

I think Updating is just a matter of adding "&" in front of
remote_sdp->m_medias and to other list accessed.

运行问题:

[root@localhost linphonenew]# ./uastest
./uastest: error while loading shared libraries: libosipparser2.so.4: cannot open shared object file: No such file or directory

解决方法:

#libosipparser2相关库文件复制到usr、lib目录下面

cp /opt/sip/target/lib/libosipp* /usr/lib

2、uac
 

  1. #include <eXosip2/eXosip.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include <netinet/in.h> 
  5. #include <sys/socket.h> 
  6. #include <sys/types.h> 
  7.  
  8. int 
  9. main (int argc, char *argv[]) 
  10.   eXosip_event_t *je; 
  11.   osip_message_t *reg = NULL; 
  12.   osip_message_t *invite = NULL; 
  13.   osip_message_t *ack = NULL; 
  14.   osip_message_t *info = NULL; 
  15.   osip_message_t *message = NULL; 
  16.  
  17.   int call_id, dialog_id; 
  18.   int i,flag; 
  19.   int flag1 = 1; 
  20.   int id; 
  21.   
  22.   char *identity = "sip:140@192.168.0.140"
  23.   char *registerer = "sip:192.168.0.133:5060"
  24.   char *source_call = "sip:140@192.168.0.140"
  25.   char *dest_call = "sip:133@192.168.0.133:5060"
  26.   
  27.   char command; 
  28.   char tmp[4096]; 
  29.   char localip[128]; 
  30.  
  31.   printf("r     向服务器注册\n\n"); 
  32.   printf("c     取消注册\n\n"); 
  33.   printf("i     发起呼叫请求\n\n"); 
  34.   printf("h     挂断\n\n"); 
  35.   printf("q     退出程序\n\n"); 
  36.   printf("s     执行方法INFO\n\n"); 
  37.   printf("m     执行方法MESSAGE\n\n"); 
  38.   //初始化 
  39.   i = eXosip_init (); 
  40.   if (i != 0) 
  41.     { 
  42.       printf ("Couldn't initialize eXosip!\n"); 
  43.       return -1; 
  44.     } 
  45.   else 
  46.     { 
  47.       printf ("eXosip_init successfully!\n"); 
  48.     } 
  49.  
  50.   i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5060, AF_INET, 0); 
  51.   if (i != 0) 
  52.     { 
  53.       eXosip_quit (); 
  54.       fprintf (stderr, "Couldn't initialize transport layer!\n"); 
  55.       return -1; 
  56.     } 
  57.   flag = 1; 
  58.   while (flag) 
  59.     { 
  60.       printf ("please input the comand:\n"); 
  61.       
  62.       scanf ("%c", &command); 
  63.       getchar (); 
  64.       
  65.       switch (command) 
  66.     { 
  67.     case 'r'
  68.       printf ("This modal isn't commpleted!\n"); 
  69.       break
  70.     case 'i':/* INVITE */ 
  71.       i = eXosip_call_build_initial_invite (&invite, dest_call
  72. , source_call, NULL, "This si a call for a conversation"); 
  73.       if (i != 0) 
  74.         { 
  75.           printf ("Intial INVITE failed!\n"); 
  76.           break
  77.         } 
  78. //符合SDP格式,其中属性a是自定义格式,也就是说可以存放自己的信息,但是只能是两列,比如帐户信息 
  79. //但是经测试,格式:v o t必不可少,原因未知,估计是协议栈在传输时需要检查的 
  80.       snprintf (tmp, 4096, 
  81.             "v=0\r\n" 
  82.             "o=anonymous 0 0 IN IP4 0.0.0.0\r\n" 
  83.             "t=1 10\r\n" 
  84.             "a=username:rainfish\r\n" 
  85.             "a=password:123\r\n"); 
  86.       osip_message_set_body (invite, tmp, strlen(tmp)); 
  87.       osip_message_set_content_type (invite, "application/sdp"); 
  88.       
  89.       eXosip_lock (); 
  90.       i = eXosip_call_send_initial_invite (invite); 
  91.       eXosip_unlock (); 
  92.       flag1 = 1; 
  93.       while (flag1) 
  94.         { 
  95.           je = eXosip_event_wait (0, 200); 
  96.           
  97.           if (je == NULL) 
  98.         { 
  99.           printf ("No response or the time is over!\n"); 
  100.           break
  101.         } 
  102.           
  103.         switch (je->type) 
  104.         { 
  105.         case EXOSIP_CALL_INVITE: 
  106.           printf ("a new invite reveived!\n"); 
  107.           break
  108.         case EXOSIP_CALL_PROCEEDING: 
  109.           printf ("proceeding!\n"); 
  110.           break
  111.         case EXOSIP_CALL_RINGING: 
  112.           printf ("ringing!\n"); 
  113.           // call_id = je->cid; 
  114.           // dialog_id = je->did; 
  115.           printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); 
  116.           break
  117.         case EXOSIP_CALL_ANSWERED: 
  118.           printf ("ok! connected!\n"); 
  119.           call_id = je->cid; 
  120.           dialog_id = je->did; 
  121.           printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); 
  122.  
  123.           eXosip_call_build_ack (je->did, &ack); 
  124.           eXosip_call_send_ack (je->did, ack); 
  125.           flag1 = 0; 
  126.           break
  127.         case EXOSIP_CALL_CLOSED: 
  128.           printf ("the other sid closed!\n"); 
  129.           break
  130.         case EXOSIP_CALL_ACK: 
  131.           printf ("ACK received!\n"); 
  132.           break
  133.         default
  134.           printf ("other response!\n"); 
  135.           break
  136.         } 
  137.           eXosip_event_free (je); 
  138.          
  139.         } 
  140.       break
  141.     case 'h'
  142.       printf ("Holded !\n"); 
  143.       
  144.       eXosip_lock (); 
  145.       eXosip_call_terminate (call_id, dialog_id); 
  146.       eXosip_unlock (); 
  147.       break
  148.     case 'c'
  149.       printf ("This modal isn't commpleted!\n"); 
  150.       break
  151.     case 's'
  152.     //传输INFO方法 
  153.       eXosip_call_build_info (dialog_id, &info); 
  154.       snprintf (tmp , 4096, 
  155.             "hello,rainfish"); 
  156.       osip_message_set_body (info, tmp, strlen(tmp)); 
  157.       //格式可以任意设定,text/plain代表文本信息 
  158.       osip_message_set_content_type (info, "text/plain"); 
  159.       eXosip_call_send_request (dialog_id, info); 
  160.       break
  161.     case 'm'
  162.     //传输MESSAGE方法,也就是即时消息,和INFO方法相比,我认为主要区别,
  163.     //是MESSAGE不用建立连接,直接传输信息,而INFO必须在建立INVITE的基础上传输。 
  164.       printf ("the mothed :MESSAGE\n"); 
  165.       eXosip_message_build_request (&message, "MESSAGE", dest_call, source_call, NULL); 
  166.       snprintf (tmp, 4096, 
  167.             "hellor rainfish"); 
  168.       osip_message_set_body (message, tmp, strlen(tmp)); 
  169.       //假设格式是xml 
  170.       osip_message_set_content_type (message, "text/xml"); 
  171.       eXosip_message_send_request (message); 
  172.       break
  173.     case 'q'
  174.       eXosip_quit (); 
  175.       printf ("Exit the setup!\n"); 
  176.       flag = 0; 
  177.       break
  178.     } 
  179.     } 
  180.   return (0); 

 >>  gcc uastest.c -o uastest -L/opt/sip/target/lib/ -I/opt/sip/target/include/ -leXosip2

3\ 单机测试指导

本机ip:192.168.3.168

uac修改:

line23-26改为:

  1. char *identity="sip:140@192.168.3.168";   //UAC1,端口是5060 
  2. char *registar="sip:133@192.168.3.168:5061"//UAS,端口是5061 
  3. char *source_call="sip:140@192.168.3.168"
  4. char *dest_call="sip:133@192.168.3.168:5061"

uas修改:

line44改为

  1. i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5061, AF_INET, 0); 

其他:

http://hi.baidu.com/yangkaiii/blog/item/e2946f44ecdeaa86b3b7dc5d.html

http://hi.baidu.com/yangkaiii/blog/item/e46a59dde1ad5e305882dd5c.html

4\ 呼叫彩E

本机ip:192.168.3.168

彩E:192.168.3.167

line80到85改为:

  1. snprintf (tmp, 4096,         
  2. "v=0\r\n" 
  3. "o=- 6 2 IN IP4 192.168.3.46\r\n" 
  4. "s=CounterPath X-Lite 3.0\r\n" 
  5. "c=IN IP4 192.168.3.46\r\n" 
  6. "t=0 0\r\n" 
  7. "m=audio 52386 RTP/AVP 107 0 8 101\r\n" 
  8. "a=alt:1 1 : 0+Y3WBhV SIknnQeS 192.168.3.46 52386\r\n" 
  9. "a=fmtp:101 0-15\r\n" 
  10. "a=rtpmap:107 BV32/16000\r\n" 
  11. "a=rtpmap:101 telephone-event/8000\r\n" 
  12. "a=sendrecv\r\n");  

line26:

  1. char *dest_call="sip:111@192.168.3.167:5060"

注意:111的用户名不能就是该彩E的账号,不能随便换成其他的121之类,否则得到busy之类的应答信息。

来源

(bloghome)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201101/10770.html]
本文出处:CSDN博客 作者:bloghome
顶一下
(9)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容