exosip注册/认证的例子程序
#include <eXosip2/eXosip.h> #include <sys/socket.h> #include <osip2/osip_mt.h> #include <osip2/osip.h> #include <unistd.h> void process_event(); void Register(); int m_nregid=0;//注册id int main() { int i=0; i=eXosip_init(); if(i!=0) return -1; printf("========= %s\n",eXosip_get_version()); unsigned short port=7000;//本地sip端口 i=eXosip_listen_addr(17,NULL,port,AF_INET,0);//17是udp,不想过多的引入头文件,故用数字表示一下 if(i!=0) { printf("listen fail!\n"); eXosip_quit(); return -1; } Register(); process_event(); sleep(5000); getchar(); return 0; } void process_event() { eXosip_event *event; for(;;) { if (!(event = eXosip_event_wait(0, 50))) continue ; eXosip_lock(); eXosip_automatic_action(); eXosip_unlock(); printf("event->type ====== %d\n",event->type); if(event->type==EXOSIP_CALL_ACK) { } else if(event->type==EXOSIP_CALL_ANSWERED) { } else if(event->type==EXOSIP_REGISTRATION_FAILURE) { if(event->response && event->response->status_code==401) { printf("====== 401 =%d\n",event->rid); #if 0 osip_message_t *reg = NULL; eXosip_lock(); osip_www_authenticate_t *dest = NULL; osip_message_get_www_authenticate(event->response,0,&dest); if(dest == NULL) continue; char realm[256]; eXosip_clear_authentication_info(); strcpy(realm,osip_www_authenticate_get_realm(dest)); eXosip_add_authentication_info("022000000110000","022000000110000","12345678", "MD5",realm); eXosip_register_build_register(event->rid, 3600, ®); if(reg==NULL) { printf("eXosip_register_build_register failed!\n"); continue; } printf("authenticate=%s ver=%s\n",realm,reg->sip_version); eXosip_register_send_register(event->rid,reg); eXosip_unlock(); #endif } } else if(event->type==EXOSIP_REGISTRATION_SUCCESS) { printf("register sccess!\n"); } eXosip_event_free(event); } } void Register() { printf("1 ====== Register()\n"); char fromuser[256]; char proxy[256]; char route[256]; sprintf(fromuser,"sip:%s@%s","022000000110000","192.168.66.117"); sprintf(proxy,"sip:%s@%s","022000000110000","192.168.66.244"); sprintf(route,"<sip:%s:%d;lr>","192.168.66.244",35060); eXosip_clear_authentication_info(); osip_message_t *reg = NULL; m_nregid = eXosip_register_build_initial_register(fromuser, proxy, NULL,3600,®); //提前输入了验证信息,在消息为401处,用eXosip_automatic_action()自动处理 eXosip_add_authentication_info("022000000110000","022000000110000","12345678", "MD5",NULL); if(reg==NULL) return; osip_message_set_route(reg,route); if(eXosip_register_send_register(m_nregid, reg)!=0) return ; }
posted on 2017-10-12 21:27 zhuxian2009 阅读(2757) 评论(0) 编辑 收藏 举报