int uac_key_distribute(const char *peer_id, sip_entity *sip_target) { eXosip_event_t *g_event; osip_header_t * subject; char to[100]; char from[100]; //key_nego 1 //snprintf(to, 50,"sip:%s@%s:%s",device_info.server_id,device_info.server_ip,device_info.server_port); //char peer_id[CHARLEN]="user2"; snprintf(to, 50,"sip:%s@%s:%s",peer_id,device_info.server_ip,device_info.server_port); snprintf(from, 50,"sip:%s@%s:%s",device_info.ipc_id,device_info.ipc_ip,device_info.ipc_port); //uac_send_noSessionMessage(to,from, NULL,"peer userid:user2\n","KEY_DISTRIBUTE1\n"); sessionId id; sip_entity sip_server; memset(&sip_server,0,sizeof(sip_server)); sprintf(sip_server.ip, "%s", device_info.server_ip); sip_server.port=atoi(device_info.server_port); sprintf(sip_server.username, "%s", peer_id); alter_message invite_message; memset(&invite_message,0,sizeof(alter_message)); invite_message.body="peer userid:user2\n"; invite_message.content_type=CONTENT_CODE; invite_message.subject="KEY_DISTRIBUTE1\n"; uac_sendInvite(&id,&sip_server,&invite_message); /*------------------finish send the invite request----------------------*/ //printf("uac_sendInvite sucess\n"); uac_waitfor(&id,EXOSIP_CALL_ANSWERED,&g_event); if(g_event==NULL) { printf("no response\n\n"); return 0; } id.cid=g_event->cid; id.did=g_event->did; if(g_event->type!= EXOSIP_CALL_ANSWERED )//&& g_event->type!=EXOSIP_CALL_MESSAGE_ANSWERED) { printf("g_event->type:%d\n",g_event->type); printf("g_event->cid:%d\n",g_event->cid); printf("not the right response\n"); return 0; } /*------------------finish receive the response----------------------*/ osip_message_t *ack = NULL; eXosip_call_build_ack (id.did, &ack); if(eXosip_call_send_ack (id.did, ack)) { printf("send_ack error\n"); return 0; } /*------------------finish send the ack response----------------------*/ printf("before HandleP2PKeyDistribution_request\n"); if(HandleP2PKeyDistribution_request(g_event)<1) { printf("HandleP2PKeyDistribution_request error\n"); id.cid=g_event->cid; id.did=g_event->did; memcpy(id.call_id,g_event->request->call_id->number,sizeof(id.call_id)); eXosip_event_free (g_event); uac_bye(id); return 0; } id.cid=g_event->cid; id.did=g_event->did; memcpy(id.call_id,g_event->request->call_id->number,sizeof(id.call_id)); eXosip_event_free (g_event); uac_bye(id); int LinkNum; LinkNum=getSecureLinkNum(&Securelinks, p2pcc->peer_id); if(LinkNum<0) { printf("No secure link information with %s!\n ", p2pcc->peer_id); return 0; } memcpy(sip_target->ip,Securelinks.links[LinkNum].partner_ip/*"192.168.17.127"*/ ,sizeof(sip_target->ip)); sip_target->port=5063;//this will modify later memcpy(sip_target->username,p2pcc->peer_id,sizeof(sip_target->username)); printf("uac_key_distribute-----finished\n"); return 1; }
int uac_key_nego() { eXosip_event_t *g_event; osip_header_t * subject; char to[100]; char from[100]; //key_nego 1 snprintf(to, 50,"sip:%s@%s:%s",device_info.ipc_id,device_info.server_ip,device_info.server_port); snprintf(from, 50,"sip:%s@%s:%s",device_info.ipc_id,device_info.server_ip,device_info.server_port); //uac_send_noSessionMessage(to,from, NULL,"this is no KEY_NAGO1 message","KEY_NAGO1\n"); sessionId id; sip_entity target; memset(&target,0,sizeof(target)); sprintf(target.ip, "%s", device_info.server_ip); target.port=atoi(device_info.server_port); sprintf(target.username, "%s", device_info.ipc_id); alter_message invite_message; memset(&invite_message,0,sizeof(alter_message)); invite_message.body="this is KEY_NAGO1 message"; invite_message.content_type=CONTENT_CODE; invite_message.subject="KEY_NAGO1\n"; uac_sendInvite(&id,&target,&invite_message); //uac_sendInvite(&id,to,"this is no KEY_NAGO1 message","text/code","KEY_NAGO1\n"); //printf("uac_sendInvite sucess\n"); uac_waitfor(&id,EXOSIP_CALL_ANSWERED,&g_event); if(g_event==NULL) { printf("no response\n\n"); return 0; } id.cid=g_event->cid; id.did=g_event->did; if(g_event->type!= EXOSIP_CALL_ANSWERED )//&& g_event->type!=EXOSIP_CALL_MESSAGE_ANSWERED) { //if(g_event->response ) //printf("g_event->response->message:\n"); //if(g_event->response->call_id) //printf("g_event->response->call_id->number:%s\n",g_event->response->call_id->number); printf("g_event->type:%d\n",g_event->type); printf("g_event->cid:%d\n",g_event->cid); printf("not the right response\n"); return 0; } osip_message_t *ack = NULL; eXosip_call_build_ack (id.did, &ack); if(eXosip_call_send_ack (id.did, ack)!=0) { printf("send_ack error\n"); return 0; } osip_message_get_subject(g_event->response,0,&subject); if(subject==NULL) { printf("no subject\n"); return 0; } //printf("subject->hvalue:%s\n",subject->hvalue); if(!strcmp(subject->hvalue,"KEY_NAGO2")) { //do something handle the KEY_NAGO2 osip_body_t *body; osip_message_get_body (g_event->response, 0, &body);//body UnicastKeyNegoRequ *unicast_key_nego_requ_packet_c=(UnicastKeyNegoRequ*)malloc (sizeof(UnicastKeyNegoRequ)*2); if(body->length < sizeof(UnicastKeyNegoRequ)*2) { printf("not valid length"); free(unicast_key_nego_requ_packet_c); return 0; } memcpy(unicast_key_nego_requ_packet_c,body->body, sizeof(UnicastKeyNegoRequ)*2); decodeFromChar(unicast_key_nego_requ_packet_c,sizeof(UnicastKeyNegoRequ)*2); if(HandleUnicastKeyNegoRequest(RegisterCon, unicast_key_nego_requ_packet_c)<1) { printf("HandleUnicastKeyNegoRequest error\n"); free(unicast_key_nego_requ_packet_c); return 0; } id.cid=g_event->cid; id.did=g_event->did; osip_message_t *ack = NULL; printf("id.cid:%d id.did:%d",id.cid,id.did); //eXosip_call_build_ack (id.did, &ack); //eXosip_call_send_ack (id.cid, ack); free(unicast_key_nego_requ_packet_c); eXosip_event_free (g_event); } else { printf("not KEY_NAGO2\n"); printf("g_event->cid:%d\n",g_event->cid); eXosip_event_free (g_event); return 0; } g_event=NULL; UnicastKeyNegoResp *unicast_key_nego_resp_packet_c=(UnicastKeyNegoResp*)malloc (sizeof(UnicastKeyNegoResp)*2); if(ProcessUnicastKeyNegoResponse(RegisterCon, unicast_key_nego_resp_packet_c)<1) { printf("ProcessUnicastKeyNegoResponse error\n"); free(unicast_key_nego_resp_packet_c); return 0; } codeToChar(unicast_key_nego_resp_packet_c,sizeof(UnicastKeyNegoResp)*2); //key_nego 3 alter_message key_nego_message; memset(&key_nego_message,0,sizeof(alter_message)); key_nego_message.body=unicast_key_nego_resp_packet_c; key_nego_message.method_type=METHODMESSAGE; key_nego_message.content_type=CONTENT_CODE; key_nego_message.subject="KEY_NAGO3"; if(uac_send_message(id,&key_nego_message)!=0) { printf("uac_send_message error\n"); free(unicast_key_nego_resp_packet_c); return 0; } free(unicast_key_nego_resp_packet_c); if(!uac_waitfor(&id,EXOSIP_CALL_MESSAGE_ANSWERED,&g_event)) { printf("g_event->type:%d\n",g_event->type); printf("g_event->cid:%d\n",g_event->cid); printf("not the right response\n"); return 0; } if(g_event==NULL) { printf("no response\n\n"); return 0; } osip_message_get_subject(g_event->response,0,&subject); //printf("subject->hvalue:%s",subject->hvalue); if(!strcmp(subject->hvalue,"KEY_NAGO4")) { //do something handle the KEY_NAGO 4 osip_body_t *body; osip_message_get_body (g_event->response, 0, &body); UnicastKeyNegoConfirm *unicast_key_nego_confirm_packet_c=(UnicastKeyNegoConfirm*)malloc (sizeof(UnicastKeyNegoConfirm)*2); if(body->length < sizeof(UnicastKeyNegoConfirm)*2) { printf("not valid length"); free(unicast_key_nego_confirm_packet_c); eXosip_event_free (g_event); return 0; } memcpy(unicast_key_nego_confirm_packet_c,body->body, sizeof(UnicastKeyNegoConfirm)*2); //free(body); decodeFromChar(unicast_key_nego_confirm_packet_c,sizeof(UnicastKeyNegoConfirm)*2); if(HandleUnicastKeyNegoConfirm(RegisterCon, unicast_key_nego_confirm_packet_c)<1) { printf("HandleUnicastKeyNegoConfirm error\n"); free(unicast_key_nego_confirm_packet_c); eXosip_event_free (g_event); return 0; } free(unicast_key_nego_confirm_packet_c); eXosip_event_free (g_event); uac_bye(id); //return 1; } else { printf("not KEY_NAGO4\n"); //printf("g_event->cid:%d\n",g_event->cid); eXosip_event_free (g_event); uac_bye(id); return 0; } //if it is NVR , it will wait for IPC access //if(!strcmp(device_info.ipc_port,"5063")) //{ //user_type=NVR; //printf("user_type is NVR\n"); //} /* if(user_type==NVR) { eXosip_event_t *event; uac_waitfor(NULL, EXOSIP_MESSAGE_NEW,&event); if(event==NULL) { printf("not the right response\n"); return 0; } if(HandleP2PKeyDistribution_request(event)<1) { printf("HandleP2PKeyDistribution_request error\n"); return 0; } } */ printf("uac_key_nego-----finished\n"); return 1; }
int eXosip_call_send_ack (int did, osip_message_t * ack) { eXosip_dialog_t *jd = NULL; eXosip_call_t *jc = NULL; int i; osip_route_t *route; char *host; int port; if (did <= 0) return OSIP_BADPARAMETER; if (did > 0) { eXosip_call_dialog_find (did, &jc, &jd); } if (jc == NULL || jd == NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: No call here?\n")); if (ack != NULL) osip_message_free (ack); return OSIP_NOTFOUND; } if (ack == NULL) { i = eXosip_call_build_ack (did, &ack); if (i != 0) { return i; } } osip_message_get_route (ack, 0, &route); if (route != NULL) { osip_uri_param_t *lr_param = NULL; osip_uri_uparam_get_byname (route->url, "lr", &lr_param); if (lr_param == NULL) route = NULL; } if (route != NULL) { port = 5060; if (route->url->port != NULL) port = osip_atoi (route->url->port); host = route->url->host; } else { /* search for maddr parameter */ osip_uri_param_t *maddr_param = NULL; osip_uri_uparam_get_byname (ack->req_uri, "maddr", &maddr_param); host = NULL; if (maddr_param != NULL && maddr_param->gvalue != NULL) host = maddr_param->gvalue; port = 5060; if (ack->req_uri->port != NULL) port = osip_atoi (ack->req_uri->port); if (host == NULL) host = ack->req_uri->host; } i = cb_snd_message (NULL, ack, host, port, -1); if (jd->d_ack != NULL) osip_message_free (jd->d_ack); jd->d_ack = ack; if (i < 0) return i; /* TODO: could be 1 for icmp... */ return OSIP_SUCCESS; }
int csenn_eXosip_invit(sessionId * id, char * to, char * sdpMessage, char * responseSdp) { osip_message_t *invite; int i;// optionnal route header char to_[100]; snprintf (to_, 100,"sip:%s", to); char from_[100]; snprintf (from_, 100, "sip:%s:%s", device_info.ipc_ip ,device_info.ipc_port ); //snprintf (tmp, 4096, ""); /*i = eXosip_call_build_initial_invite (&invite, "sip:[email protected]:5060", "sip:[email protected]:5060", NULL, "34020000001320000001:1,34020000001180000002:1" );*/ i = eXosip_call_build_initial_invite (&invite, to_, from_, NULL, "This is a call for a conversation" ); //i = eXosip_call_build_initial_invite (&invite,"<sip:[email protected]>", "<sip:[email protected]>",NULL, "This is a call for a conversation" ); if (i != 0) { return -1; } //osip_message_set_supported (invite, "100rel"); { char tmp[4096]; char localip[128]; eXosip_guess_localip (AF_INET, localip, 128); localip[128]=device_info.ipc_ip; i=osip_message_set_body (invite, sdpMessage, strlen (sdpMessage)); i=osip_message_set_content_type (invite, "APPLICATION/SDP"); } eXosip_lock (); i = eXosip_call_send_initial_invite (invite); if (i > 0) { //eXosip_call_set_reference (i, "ssss"); } eXosip_unlock (); int flag1 = 1; while (flag1) { eXosip_event_t *je; je = eXosip_event_wait (0, 1000); if (je == NULL) { printf ("No response or the time is over!\n"); break; } switch (je->type) { case EXOSIP_CALL_INVITE: printf ("a new invite reveived!\n"); break; case EXOSIP_CALL_PROCEEDING: printf ("proceeding!\n"); break; case EXOSIP_CALL_RINGING: printf ("ringing!\n"); //printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); break; case EXOSIP_CALL_ANSWERED: printf ("ok! connected!\n"); printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); id->cid=je->cid; id->did=je->did; osip_body_t *body; osip_message_get_body (je->response, 0, &body); //printf ("I get the msg is: %s\n", body->body); //(*responseSdp)=(char *)malloc (body->length*sizeof(char)); if(body!=NULL) snprintf (responseSdp, body->length,"%s", body->body); //response a ack osip_message_t *ack = NULL; eXosip_call_build_ack (je->did, &ack); eXosip_call_send_ack (je->did, ack); flag1 = 0; break; case EXOSIP_CALL_CLOSED: printf ("the other sid closed!\n"); break; case EXOSIP_CALL_ACK: printf ("ACK received!\n"); break; default: printf ("other response!\n"); break; } eXosip_event_free (je); } return 0; }
int main (int argc, char *argv[]) { eXosip_event_t *je; osip_message_t *reg = NULL; osip_message_t *invite = NULL; osip_message_t *ack = NULL; osip_message_t *info = NULL; osip_message_t *message = NULL; int call_id, dialog_id; int i,flag; int flag1 = 1; int id; char *identity = "sip:[email protected]"; char *registerer = "sip:192.168.44.100:5060"; char *source_call = "sip:[email protected]"; char *dest_call = "sip:[email protected]:5060"; char command; char tmp[4096]; char localip[128]; printf("r 向服务器注册\n\n"); printf("c 取消注册\n\n"); printf("i 发起呼叫请求\n\n"); printf("h 挂断\n\n"); printf("q 退出程序\n\n"); printf("s 执行方法INFO\n\n"); printf("m 执行方法MESSAGE\n\n"); //初始化 i = eXosip_init (); if (i != 0) { printf ("Couldn't initialize eXosip!\n"); return -1; } else { printf ("eXosip_init successfully!\n"); } i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5060, AF_INET, 0); if (i != 0) { eXosip_quit (); fprintf (stderr, "Couldn't initialize transport layer!\n"); return -1; } flag = 1; while (flag) { printf ("please input the comand:\n"); scanf ("%c", &command); getchar (); switch (command) { case 'r': printf ("This modal isn't commpleted!\n"); break; case 'i':/* INVITE */ i = eXosip_call_build_initial_invite (&invite, dest_call, source_call, NULL, "This si a call for a conversation"); if (i != 0) { printf ("Intial INVITE failed!\n"); break; } //符合SDP格式,其中属性a是自定义格式,也就是说可以存放自己的信息,但是只能是两列,比如帐户信息 //但是经测试,格式:v o t必不可少,原因未知,估计是协议栈在传输时需要检查的 snprintf (tmp, 4096, "v=0\r\n" "o=anonymous 0 0 IN IP4 0.0.0.0\r\n" "t=1 10\r\n" "a=username:rainfish\r\n" "a=password:123\r\n"); osip_message_set_body (invite, tmp, strlen(tmp)); osip_message_set_content_type (invite, "application/sdp"); eXosip_lock (); i = eXosip_call_send_initial_invite (invite); eXosip_unlock (); flag1 = 1; while (flag1) { je = eXosip_event_wait (0, 200); if (je == NULL) { printf ("No response or the time is over!\n"); break; } switch (je->type) { case EXOSIP_CALL_INVITE: printf ("a new invite reveived!\n"); break; case EXOSIP_CALL_PROCEEDING: printf ("proceeding!\n"); break; case EXOSIP_CALL_RINGING: printf ("ringing!\n"); // call_id = je->cid; // dialog_id = je->did; printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); break; case EXOSIP_CALL_ANSWERED: printf ("ok! connected!\n"); call_id = je->cid; dialog_id = je->did; printf ("call_id is %d, dialog_id is %d \n", je->cid, je->did); eXosip_call_build_ack (je->did, &ack); eXosip_call_send_ack (je->did, ack); flag1 = 0; break; case EXOSIP_CALL_CLOSED: printf ("the other sid closed!\n"); break; case EXOSIP_CALL_ACK: printf ("ACK received!\n"); break; default: printf ("other response!\n"); break; } eXosip_event_free (je); } break; case 'h': printf ("Holded !\n"); eXosip_lock (); eXosip_call_terminate (call_id, dialog_id); eXosip_unlock (); break; case 'c': printf ("This modal isn't commpleted!\n"); break; case 's': //传输INFO方法 eXosip_call_build_info (dialog_id, &info); snprintf (tmp , 4096, "hello,rainfish"); osip_message_set_body (info, tmp, strlen(tmp)); //格式可以任意设定,text/plain代表文本信息 osip_message_set_content_type (info, "text/plain"); eXosip_call_send_request (dialog_id, info); break; case 'm': //传输MESSAGE方法,也就是即时消息,和INFO方法相比,我认为主要区别,是MESSAGE不用建立连接,直接传输信息,而INFO必须 //在建立INVITE的基础上传输。 printf ("the mothed :MESSAGE\n"); eXosip_message_build_request (&message, "MESSAGE", dest_call, source_call, NULL); snprintf (tmp, 4096, "hellor rainfish"); osip_message_set_body (message, tmp, strlen(tmp)); //假设格式是xml osip_message_set_content_type (message, "text/xml"); eXosip_message_send_request (message); break; case 'q': eXosip_quit (); printf ("Exit the setup!\n"); flag = 0; break; } } return (0); }