3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "sal_eXosip2.h"
25 #include "offeranswer.h"
27 static void text_received(Sal *sal, eXosip_event_t *ev);
29 static void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
31 while((data=osip_list_get(l,0))!=NULL){
32 osip_list_remove(l,0);
37 void sal_get_default_local_ip(Sal *sal, int address_family,char *ip, size_t iplen){
38 if (eXosip_guess_localip(address_family,ip,iplen)<0){
39 /*default to something */
40 strncpy(ip,address_family==AF_INET6 ? "::1" : "127.0.0.1",iplen);
41 ms_error("Could not find default routable ip address !");
46 static SalOp * sal_find_call(Sal *sal, int cid){
49 for(elem=sal->calls;elem!=NULL;elem=elem->next){
50 op=(SalOp*)elem->data;
51 if (op->cid==cid) return op;
56 static void sal_add_call(Sal *sal, SalOp *op){
57 sal->calls=ms_list_append(sal->calls,op);
60 static void sal_remove_call(Sal *sal, SalOp *op){
61 sal->calls=ms_list_remove(sal->calls, op);
64 static SalOp * sal_find_register(Sal *sal, int rid){
67 for(elem=sal->registers;elem!=NULL;elem=elem->next){
68 op=(SalOp*)elem->data;
69 if (op->rid==rid) return op;
74 static void sal_add_register(Sal *sal, SalOp *op){
75 sal->registers=ms_list_append(sal->registers,op);
78 static void sal_remove_register(Sal *sal, int rid){
81 for(elem=sal->registers;elem!=NULL;elem=elem->next){
82 op=(SalOp*)elem->data;
84 sal->registers=ms_list_remove_link(sal->registers,elem);
90 static SalOp * sal_find_other(Sal *sal, osip_message_t *response){
93 osip_call_id_t *callid=osip_message_get_call_id(response);
95 ms_error("There is no call-id in this response !");
98 for(elem=sal->other_transactions;elem!=NULL;elem=elem->next){
99 op=(SalOp*)elem->data;
100 if (osip_call_id_match(callid,op->call_id)==0) return op;
105 static void sal_add_other(Sal *sal, SalOp *op, osip_message_t *request){
106 osip_call_id_t *callid=osip_message_get_call_id(request);
108 ms_error("There is no call id in the request !");
111 osip_call_id_clone(callid,&op->call_id);
112 sal->other_transactions=ms_list_append(sal->other_transactions,op);
115 static void sal_remove_other(Sal *sal, SalOp *op){
116 sal->other_transactions=ms_list_remove(sal->other_transactions,op);
120 static void sal_add_pending_auth(Sal *sal, SalOp *op){
121 sal->pending_auths=ms_list_append(sal->pending_auths,op);
125 static void sal_remove_pending_auth(Sal *sal, SalOp *op){
126 sal->pending_auths=ms_list_remove(sal->pending_auths,op);
129 void sal_exosip_fix_route(SalOp *op){
130 if (sal_op_get_route(op)!=NULL){
131 osip_route_t *rt=NULL;
132 osip_uri_param_t *lr_param=NULL;
134 osip_route_init(&rt);
135 if (osip_route_parse(rt,sal_op_get_route(op))<0){
136 ms_warning("Bad route %s!",sal_op_get_route(op));
137 sal_op_set_route(op,NULL);
139 /* check if the lr parameter is set , if not add it */
140 osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
143 osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
144 osip_route_to_str(rt,&tmproute);
145 sal_op_set_route(op,tmproute);
153 SalOp * sal_op_new(Sal *sal){
154 SalOp *op=ms_new(SalOp,1);
155 __sal_op_init(op,sal);
156 op->cid=op->did=op->tid=op->rid=op->nid=op->sid=-1;
158 op->supports_session_timers=FALSE;
159 op->sdp_offering=TRUE;
160 op->pending_auth=NULL;
164 op->masquerade_via=FALSE;
165 op->auto_answer_asked=FALSE;
169 bool_t sal_call_autoanswer_asked(SalOp *op)
171 return op->auto_answer_asked;
174 void sal_op_release(SalOp *op){
176 sdp_message_free(op->sdp_answer);
177 if (op->pending_auth)
178 eXosip_event_free(op->pending_auth);
180 sal_remove_register(op->base.root,op->rid);
183 ms_message("Cleaning cid %i",op->cid);
184 sal_remove_call(op->base.root,op);
187 sal_remove_out_subscribe(op->base.root,op);
190 sal_remove_in_subscribe(op->base.root,op);
192 osip_call_id_free(op->call_id);
195 if (op->pending_auth){
196 sal_remove_pending_auth(op->base.root,op);
199 sal_media_description_unref(op->result);
201 sal_remove_other(op->base.root,op);
202 osip_call_id_free(op->call_id);
207 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
208 int ortp_level=ORTP_DEBUG;
214 ortp_level=ORTP_MESSAGE;
217 ortp_level=ORTP_WARNING;
221 ortp_level=ORTP_ERROR;
224 ortp_level=ORTP_FATAL;
226 case END_TRACE_LEVEL:
229 if (ortp_log_level_enabled(level)){
230 int len=strlen(chfr);
231 char *chfrdup=ortp_strdup(chfr);
232 /*need to remove endline*/
234 if (chfrdup[len-1]=='\n')
236 if (chfrdup[len-2]=='\r')
239 ortp_logv(ortp_level,chfrdup,ap);
246 static bool_t firsttime=TRUE;
249 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
254 sal->keepalive_period=30;
258 void sal_uninit(Sal* sal){
263 void sal_set_user_pointer(Sal *sal, void *user_data){
267 void *sal_get_user_pointer(const Sal *sal){
271 static void unimplemented_stub(){
272 ms_warning("Unimplemented SAL callback");
275 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
276 memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
277 if (ctx->callbacks.call_received==NULL)
278 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
279 if (ctx->callbacks.call_ringing==NULL)
280 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
281 if (ctx->callbacks.call_accepted==NULL)
282 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
283 if (ctx->callbacks.call_failure==NULL)
284 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
285 if (ctx->callbacks.call_terminated==NULL)
286 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
287 if (ctx->callbacks.call_updated==NULL)
288 ctx->callbacks.call_updated=(SalOnCallUpdated)unimplemented_stub;
289 if (ctx->callbacks.auth_requested==NULL)
290 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
291 if (ctx->callbacks.auth_success==NULL)
292 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
293 if (ctx->callbacks.register_success==NULL)
294 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
295 if (ctx->callbacks.register_failure==NULL)
296 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
297 if (ctx->callbacks.dtmf_received==NULL)
298 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
299 if (ctx->callbacks.notify==NULL)
300 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
301 if (ctx->callbacks.notify_presence==NULL)
302 ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
303 if (ctx->callbacks.subscribe_received==NULL)
304 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
305 if (ctx->callbacks.text_received==NULL)
306 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
307 if (ctx->callbacks.internal_message==NULL)
308 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
309 if (ctx->callbacks.ping_reply==NULL)
310 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
313 int sal_unlisten_ports(Sal *ctx){
321 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
324 int proto=IPPROTO_UDP;
327 case SalTransportDatagram:
330 case SalTransportStream:
334 ms_warning("unexpected proto, using datagram");
338 eXosip_set_option(13,&err); /*13=EXOSIP_OPT_SRV_WITH_NAPTR, as it is an enum value, we can't use it unless we are sure of the
339 version of eXosip, which is not the case*/
340 /*see if it looks like an IPv6 address*/
341 ipv6=strchr(addr,':')!=NULL;
342 eXosip_enable_ipv6(ipv6);
345 ms_fatal("SIP over TLS or DTLS is not supported yet.");
348 err=eXosip_listen_addr(proto, addr, port, ipv6 ? PF_INET6 : PF_INET, 0);
349 #ifdef HAVE_EXOSIP_GET_SOCKET
350 ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
352 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &ctx->keepalive_period);
357 ortp_socket_t sal_get_socket(Sal *ctx){
358 #ifdef HAVE_EXOSIP_GET_SOCKET
359 return eXosip_get_socket(IPPROTO_UDP);
361 ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
366 void sal_set_user_agent(Sal *ctx, const char *user_agent){
367 eXosip_set_user_agent(user_agent);
370 void sal_use_session_timers(Sal *ctx, int expires){
371 ctx->session_expires=expires;
374 MSList *sal_get_pending_auths(Sal *sal){
375 return ms_list_copy(sal->pending_auths);
378 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
379 osip_via_t *via=NULL;
380 osip_generic_param_t *param=NULL;
381 const char *rport=NULL;
385 osip_message_get_via(msg,0,&via);
388 /* it is useless to do that with tcp since client socket might have a different port
389 than the server socket.
391 if (strcasecmp(via->protocol,"tcp")==0) return -1;
393 if (via->port && via->port[0]!='\0')
394 *rportval=atoi(via->port);
396 osip_via_param_get_byname(via,"rport",¶m);
399 if (rport && rport[0]!='\0') *rportval=atoi(rport);
404 osip_via_param_get_byname(via,"received",¶m);
405 if (param) *received=param->gvalue;
407 if (rport==NULL && *received==NULL) return -1;
411 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
415 sdp_message_to_str(msg,&sdp);
417 snprintf(clen,sizeof(clen),"%i",sdplen);
418 osip_message_set_body(sip,sdp,sdplen);
419 osip_message_set_content_type(sip,"application/sdp");
420 osip_message_set_content_length(sip,clen);
424 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
425 sdp_message_t *msg=media_description_to_sdp(desc);
427 ms_error("Fail to print sdp message !");
431 sdp_message_free(msg);
434 static void sdp_process(SalOp *h){
435 ms_message("Doing SDP offer/answer process");
437 sal_media_description_unref(h->result);
439 h->result=sal_media_description_new();
440 if (h->sdp_offering){
441 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
444 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result);
445 h->sdp_answer=media_description_to_sdp(h->result);
446 strcpy(h->result->addr,h->base.remote_media->addr);
447 h->result->bandwidth=h->base.remote_media->bandwidth;
448 for(i=0;i<h->result->nstreams;++i){
449 if (h->result->streams[i].port>0){
450 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
451 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
452 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
453 h->result->streams[i].port=h->base.remote_media->streams[i].port;
460 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
462 sal_media_description_ref(desc);
463 if (h->base.local_media)
464 sal_media_description_unref(h->base.local_media);
465 h->base.local_media=desc;
469 int sal_call(SalOp *h, const char *from, const char *to){
471 osip_message_t *invite=NULL;
472 sal_op_set_from(h,from);
474 sal_exosip_fix_route(h);
475 err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
477 ms_error("Could not create call.");
480 osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
481 if (h->base.contact){
482 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
483 osip_message_set_contact(invite,h->base.contact);
485 if (h->base.root->session_expires!=0){
486 osip_message_set_header(invite, "Session-expires", "200");
487 osip_message_set_supported(invite, "timer");
489 if (h->base.local_media){
490 h->sdp_offering=TRUE;
491 set_sdp_from_desc(invite,h->base.local_media);
492 }else h->sdp_offering=FALSE;
494 err=eXosip_call_send_initial_invite(invite);
498 ms_error("Fail to send invite !");
501 sal_add_call(h->base.root,h);
506 int sal_call_notify_ringing(SalOp *h){
508 eXosip_call_send_answer(h->tid,180,NULL);
513 int sal_call_accept(SalOp * h){
515 const char *contact=sal_op_get_contact(h);
517 int err=eXosip_call_build_answer(h->tid,200,&msg);
518 if (err<0 || msg==NULL){
519 ms_error("Fail to build answer for call: err=%i",err);
522 if (h->base.root->session_expires!=0){
523 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
527 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
528 osip_message_set_contact(msg,contact);
531 if (h->base.local_media){
532 /*this is the case where we received an invite without SDP*/
533 if (h->sdp_offering) {
534 set_sdp_from_desc(msg,h->base.local_media);
537 set_sdp(msg,h->sdp_answer);
538 sdp_message_free(h->sdp_answer);
543 ms_error("You are accepting a call but not defined any media capabilities !");
545 eXosip_call_send_answer(h->tid,200,msg);
549 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
550 if (reason==SalReasonBusy){
552 eXosip_call_send_answer(h->tid,486,NULL);
555 else if (reason==SalReasonTemporarilyUnavailable){
557 eXosip_call_send_answer(h->tid,480,NULL);
559 }else if (reason==SalReasonDoNotDisturb){
561 eXosip_call_send_answer(h->tid,600,NULL);
563 }else if (reason==SalReasonMedia){
565 eXosip_call_send_answer(h->tid,415,NULL);
567 }else if (redirect!=NULL && reason==SalReasonRedirect){
570 if (strstr(redirect,"sip:")!=0) code=302;
573 eXosip_call_build_answer(h->tid,code,&msg);
574 osip_message_set_contact(msg,redirect);
575 eXosip_call_send_answer(h->tid,code,msg);
577 }else sal_call_terminate(h);
581 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
582 if (h->base.local_media && h->base.remote_media && !h->result){
588 int sal_ping(SalOp *op, const char *from, const char *to){
589 osip_message_t *options=NULL;
591 sal_op_set_from(op,from);
592 sal_op_set_to(op,to);
593 eXosip_options_build_request (&options, sal_op_get_to(op),
594 sal_op_get_from(op),sal_op_get_route(op));
596 if (op->base.root->session_expires!=0){
597 osip_message_set_header(options, "Session-expires", "200");
598 osip_message_set_supported(options, "timer");
600 sal_add_other(sal_op_get_sal(op),op,options);
601 return eXosip_options_send_request(options);
606 int sal_refer_accept(SalOp *op){
607 osip_message_t *msg=NULL;
610 err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
613 osip_message_set_header(msg,(const char *)"event","refer");
614 osip_message_set_content_type(msg,"message/sipfrag");
615 osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
616 eXosip_call_send_request(op->did,msg);
620 ms_error("could not get a notify built\n");
626 int sal_refer(SalOp *h, const char *refer_to){
627 osip_message_t *msg=NULL;
630 eXosip_call_build_refer(h->did,refer_to, &msg);
631 if (msg) err=eXosip_call_send_request(h->did, msg);
637 int sal_call_send_dtmf(SalOp *h, char dtmf){
638 osip_message_t *msg=NULL;
643 eXosip_call_build_info(h->did,&msg);
645 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
646 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
647 osip_message_set_content_type(msg,"application/dtmf-relay");
648 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
649 osip_message_set_content_length(msg,clen);
650 eXosip_call_send_request(h->did,msg);
656 int sal_call_terminate(SalOp *h){
658 eXosip_call_terminate(h->cid,h->did);
660 sal_remove_call(h->base.root,h);
665 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
666 if (h->pending_auth){
668 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
669 else userid=info->userid;
670 ms_message("Authentication info for %s %s added to eXosip", info->username,info->realm);
671 eXosip_add_authentication_info (info->username,userid,
672 info->password, NULL,info->realm);
674 eXosip_default_action(h->pending_auth);
676 ms_message("eXosip_default_action() done");
677 eXosip_clear_authentication_info();
678 eXosip_event_free(h->pending_auth);
679 sal_remove_pending_auth(sal_op_get_sal(h),h);
680 h->pending_auth=NULL;
684 static void set_network_origin(SalOp *op, osip_message_t *req){
685 const char *received=NULL;
688 if (extract_received_rport(req,&received,&rport)!=0){
689 osip_via_t *via=NULL;
691 osip_message_get_via(req,0,&via);
692 received=osip_via_get_host(via);
693 tmp=osip_via_get_port(via);
694 if (tmp) rport=atoi(tmp);
696 snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
697 __sal_op_set_network_origin(op,origin);
700 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
702 return sal_find_call(sal,ev->cid);
705 return sal_find_register(sal,ev->rid);
707 if (ev->response) return sal_find_other(sal,ev->response);
711 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
712 SalOp *op=sal_op_new(sal);
713 osip_from_t *from,*to;
714 osip_call_info_t *call_info;
716 sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
718 set_network_origin(op,ev->request);
721 op->sdp_offering=FALSE;
722 op->base.remote_media=sal_media_description_new();
723 sdp_to_media_description(sdp,op->base.remote_media);
724 sdp_message_free(sdp);
725 }else op->sdp_offering=TRUE;
727 from=osip_message_get_from(ev->request);
728 to=osip_message_get_to(ev->request);
729 osip_from_to_str(from,&tmp);
730 sal_op_set_from(op,tmp);
732 osip_from_to_str(to,&tmp);
733 sal_op_set_to(op,tmp);
736 osip_message_get_call_info(ev->request,0,&call_info);
739 osip_call_info_to_str(call_info,&tmp);
740 if( strstr(tmp,"answer-after=") != NULL)
742 op->auto_answer_asked=TRUE;
743 ms_message("The caller asked to automatically answer the call(Emergency?)\n");
752 sal_add_call(op->base.root,op);
753 sal->callbacks.call_received(op);
756 static void handle_reinvite(Sal *sal, eXosip_event_t *ev){
757 SalOp *op=find_op(sal,ev);
759 osip_message_t *msg=NULL;
762 ms_warning("Reinvite for non-existing operation !");
767 sdp=eXosip_get_sdp_info(ev->request);
768 if (op->base.remote_media){
769 sal_media_description_unref(op->base.remote_media);
770 op->base.remote_media=NULL;
773 eXosip_call_build_answer(ev->tid,200,&msg);
775 if (msg==NULL) return;
776 if (op->base.root->session_expires!=0){
777 if (op->supports_session_timers) osip_message_set_supported(msg, "timer");
779 if (op->base.contact){
780 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
781 osip_message_set_contact(msg,op->base.contact);
784 op->sdp_offering=FALSE;
785 op->base.remote_media=sal_media_description_new();
786 sdp_to_media_description(sdp,op->base.remote_media);
787 sdp_message_free(sdp);
789 if (op->sdp_answer!=NULL){
790 set_sdp(msg,op->sdp_answer);
791 sdp_message_free(op->sdp_answer);
795 op->sdp_offering=TRUE;
796 set_sdp_from_desc(msg,op->base.local_media);
799 eXosip_call_send_answer(ev->tid,200,msg);
803 static void handle_ack(Sal *sal, eXosip_event_t *ev){
804 SalOp *op=find_op(sal,ev);
808 ms_warning("ack for non-existing call !");
811 sdp=eXosip_get_sdp_info(ev->ack);
813 op->base.remote_media=sal_media_description_new();
814 sdp_to_media_description(sdp,op->base.remote_media);
816 sdp_message_free(sdp);
819 sal->callbacks.call_updated(op);
822 sal->callbacks.call_ack(op);
826 static void update_contact_from_response(SalOp *op, osip_message_t *response){
827 const char *received;
829 if (extract_received_rport(response,&received,&rport)==0){
830 const char *contact=sal_op_get_contact(op);
832 /*no contact given yet, use from instead*/
833 contact=sal_op_get_from(op);
836 SalAddress *addr=sal_address_new(contact);
838 sal_address_set_domain(addr,received);
839 sal_address_set_port_int(addr,rport);
840 tmp=sal_address_as_string(addr);
841 ms_message("Contact address updated to %s for this dialog",tmp);
842 sal_op_set_contact(op,tmp);
848 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
849 SalOp *op=find_op(sal,ev);
852 ms_warning("This call has been canceled.");
854 eXosip_call_terminate(ev->cid,ev->did);
861 /* update contact if received and rport are set by the server
862 note: will only be used by remote for next INVITE, if any...*/
863 update_contact_from_response(op,ev->response);
867 static void call_ringing(Sal *sal, eXosip_event_t *ev){
869 SalOp *op=find_op(sal,ev);
870 if (call_proceeding(sal, ev)==-1) return;
872 sdp=eXosip_get_sdp_info(ev->response);
874 op->base.remote_media=sal_media_description_new();
875 sdp_to_media_description(sdp,op->base.remote_media);
876 sdp_message_free(sdp);
877 if (op->base.local_media) sdp_process(op);
879 sal->callbacks.call_ringing(op);
882 static void call_accepted(Sal *sal, eXosip_event_t *ev){
884 osip_message_t *msg=NULL;
885 SalOp *op=find_op(sal,ev);
889 ms_error("A closed call is accepted ?");
895 sdp=eXosip_get_sdp_info(ev->response);
897 op->base.remote_media=sal_media_description_new();
898 sdp_to_media_description(sdp,op->base.remote_media);
899 sdp_message_free(sdp);
900 if (op->base.local_media) sdp_process(op);
902 eXosip_call_build_ack(ev->did,&msg);
903 contact=sal_op_get_contact(op);
905 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
906 osip_message_set_contact(msg,contact);
909 set_sdp(msg,op->sdp_answer);
910 sdp_message_free(op->sdp_answer);
913 eXosip_call_send_ack(ev->did,msg);
914 sal->callbacks.call_accepted(op);
917 static void call_terminated(Sal *sal, eXosip_event_t *ev){
919 SalOp *op=find_op(sal,ev);
921 ms_warning("Call terminated for already closed call ?");
925 osip_from_to_str(ev->request->from,&from);
927 sal_remove_call(sal,op);
929 sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
930 if (from) osip_free(from);
933 static void call_released(Sal *sal, eXosip_event_t *ev){
934 SalOp *op=find_op(sal,ev);
936 ms_warning("No op associated to this call_released()");
941 sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL);
944 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
945 const char *prx_realm=NULL,*www_realm=NULL;
946 osip_proxy_authenticate_t *prx_auth;
947 osip_www_authenticate_t *www_auth;
949 *username=osip_uri_get_username(resp->from->url);
950 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
951 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
953 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
955 www_realm=osip_www_authenticate_get_realm(www_auth);
959 }else if (www_realm){
967 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
968 osip_authorization_t *auth=NULL;
969 osip_proxy_authorization_t *prx_auth=NULL;
971 *username=osip_uri_get_username(msg->from->url);
972 osip_message_get_authorization(msg, 0, &auth);
974 *realm=osip_authorization_get_realm(auth);
977 osip_message_get_proxy_authorization(msg,0,&prx_auth);
979 *realm=osip_proxy_authorization_get_realm(prx_auth);
985 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
986 if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
987 if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
991 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
992 if (op->pending_auth){
993 return get_auth_data(op->pending_auth,realm,username);
998 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1000 const char *username,*realm;
1003 ms_warning("No operation associated with this authentication !");
1006 if (get_auth_data(ev,&realm,&username)==0){
1007 if (op->pending_auth!=NULL)
1008 eXosip_event_free(op->pending_auth);
1009 op->pending_auth=ev;
1010 sal_add_pending_auth (sal,op);
1011 sal->callbacks.auth_requested(op,realm,username);
1017 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1019 const char *username,*realm;
1022 ms_warning("No operation associated with this authentication_ok!");
1025 if (get_auth_data(ev,&realm,&username)==0){
1026 sal->callbacks.auth_success(op,realm,username);
1030 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1033 char* computedReason=NULL;
1034 const char *reason=NULL;
1035 SalError error=SalErrorUnknown;
1036 SalReason sr=SalReasonUnknown;
1039 op=(SalOp*)find_op(sal,ev);
1042 ms_warning("Call failure reported for a closed call, ignored.");
1047 code=osip_message_get_status_code(ev->response);
1048 reason=osip_message_get_reason_phrase(ev->response);
1049 osip_header_t *h=NULL;
1050 if (!osip_message_header_get_byname( ev->response
1054 computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1055 reason = computedReason;
1063 return process_authentication(sal,ev);
1066 error=SalErrorUnknown;
1069 error=SalErrorFailure;
1070 sr=SalReasonNotFound;
1073 error=SalErrorFailure;
1077 eXosip_default_action(ev);
1081 error=SalErrorFailure;
1082 sr=SalReasonTemporarilyUnavailable;
1084 error=SalErrorFailure;
1090 error=SalErrorFailure;
1091 sr=SalReasonDoNotDisturb;
1094 error=SalErrorFailure;
1095 sr=SalReasonDeclined;
1099 error=SalErrorFailure;
1100 sr=SalReasonUnknown;
1101 }else error=SalErrorNoResponse;
1103 sal->callbacks.call_failure(op,error,sr,reason);
1104 if (computedReason != NULL){
1105 ms_free(computedReason);
1111 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1112 SalOp *op=find_op(sal,ev);
1113 osip_body_t *body=NULL;
1116 ms_warning("media control xml received without operation context!");
1120 osip_message_get_body(ev->request,0,&body);
1121 if (body && body->body!=NULL &&
1122 strstr(body->body,"picture_fast_update")){
1123 osip_message_t *ans=NULL;
1124 ms_message("Receiving VFU request !");
1125 if (sal->callbacks.vfu_request){
1126 sal->callbacks.vfu_request(op);
1127 eXosip_call_build_answer(ev->tid,200,&ans);
1129 eXosip_call_send_answer(ev->tid,200,ans);
1134 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1135 SalOp *op=find_op(sal,ev);
1136 osip_body_t *body=NULL;
1139 ms_warning("media dtmf relay received without operation context!");
1143 osip_message_get_body(ev->request,0,&body);
1144 if (body && body->body!=NULL){
1145 osip_message_t *ans=NULL;
1146 const char *name=strstr(body->body,"Signal");
1147 if (name==NULL) name=strstr(body->body,"signal");
1149 ms_warning("Could not extract the dtmf name from the SIP INFO.");
1152 name+=strlen("signal");
1153 if (sscanf(name," = %1s",tmp)==1){
1154 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1155 if (sal->callbacks.dtmf_received != NULL)
1156 sal->callbacks.dtmf_received(op, tmp[0]);
1159 eXosip_call_build_answer(ev->tid,200,&ans);
1161 eXosip_call_send_answer(ev->tid,200,ans);
1165 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1166 osip_message_t *ans=NULL;
1168 if (MSG_IS_INFO(ev->request)){
1169 osip_content_type_t *ct;
1170 ct=osip_message_get_content_type(ev->request);
1171 if (ct && ct->subtype){
1172 if (strcmp(ct->subtype,"media_control+xml")==0)
1173 process_media_control_xml(sal,ev);
1174 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1175 process_dtmf_relay(sal,ev);
1177 ms_message("Unhandled SIP INFO.");
1178 /*send an "Not implemented" answer*/
1180 eXosip_call_build_answer(ev->tid,501,&ans);
1182 eXosip_call_send_answer(ev->tid,501,ans);
1186 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1188 eXosip_call_build_answer(ev->tid,200,&ans);
1190 eXosip_call_send_answer(ev->tid,200,ans);
1194 if(MSG_IS_MESSAGE(ev->request)){
1195 /* SIP messages could be received into call */
1196 text_received(sal, ev);
1198 eXosip_call_build_answer(ev->tid,200,&ans);
1200 eXosip_call_send_answer(ev->tid,200,ans);
1203 if(MSG_IS_REFER(ev->request)){
1204 osip_header_t *h=NULL;
1205 SalOp *op=find_op(sal,ev);
1207 ms_message("Receiving REFER request !");
1208 osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1210 eXosip_call_build_answer(ev->tid,202,&ans);
1212 eXosip_call_send_answer(ev->tid,202,ans);
1215 sal->callbacks.refer_received(sal,op,h->hvalue);
1219 ms_warning("cannot do anything with the refer without destination\n");
1222 if(MSG_IS_NOTIFY(ev->request)){
1223 osip_header_t *h=NULL;
1225 SalOp *op=find_op(sal,ev);
1227 ms_message("Receiving NOTIFY request !");
1228 osip_from_to_str(ev->request->from,&from);
1229 osip_message_header_get_byname(ev->request,"Event",0,&h);
1231 sal->callbacks.notify(op,from,h->hvalue);
1232 /*answer that we received the notify*/
1234 eXosip_call_build_answer(ev->tid,200,&ans);
1236 eXosip_call_send_answer(ev->tid,200,ans);
1240 }else ms_warning("call_message_new: No request ?");
1243 static void inc_update(Sal *sal, eXosip_event_t *ev){
1244 osip_message_t *msg=NULL;
1245 ms_message("Processing incoming UPDATE");
1247 eXosip_message_build_answer(ev->tid,200,&msg);
1249 eXosip_message_send_answer(ev->tid,200,msg);
1253 static bool_t comes_from_local_if(osip_message_t *msg){
1254 osip_via_t *via=NULL;
1255 osip_message_get_via(msg,0,&via);
1258 host=osip_via_get_host(via);
1259 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1260 osip_generic_param_t *param=NULL;
1261 osip_via_param_get_byname(via,"received",¶m);
1262 if (param==NULL) return TRUE;
1263 if (param->gvalue &&
1264 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1272 static void text_received(Sal *sal, eXosip_event_t *ev){
1273 osip_body_t *body=NULL;
1274 char *from=NULL,*msg;
1276 osip_message_get_body(ev->request,0,&body);
1278 ms_error("Could not get text message from SIP body");
1282 osip_from_to_str(ev->request->from,&from);
1283 sal->callbacks.text_received(sal,from,msg);
1287 static void other_request(Sal *sal, eXosip_event_t *ev){
1288 ms_message("in other_request");
1289 if (ev->request==NULL) return;
1290 if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1291 text_received(sal,ev);
1292 eXosip_message_send_answer(ev->tid,200,NULL);
1293 }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1294 osip_message_t *options=NULL;
1295 eXosip_options_build_answer(ev->tid,200,&options);
1296 osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1297 osip_message_set_accept(options,"application/sdp");
1298 eXosip_options_send_answer(ev->tid,200,options);
1299 }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1300 && comes_from_local_if(ev->request)) {
1301 eXosip_message_send_answer(ev->tid,200,NULL);
1302 ms_message("Receiving WAKEUP request !");
1303 sal->callbacks.internal_message(sal,"WAKEUP");
1304 }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1305 ms_message("Receiving REFER request !");
1306 if (comes_from_local_if(ev->request)) {
1307 osip_header_t *h=NULL;
1308 osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1309 eXosip_message_send_answer(ev->tid,200,NULL);
1311 sal->callbacks.refer_received(sal,NULL,h->hvalue);
1313 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1314 }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1319 osip_message_to_str(ev->request,&tmp,&msglen);
1321 ms_message("Unsupported request received:\n%s",tmp);
1324 /*answer with a 501 Not implemented*/
1325 eXosip_message_send_answer(ev->tid,501,NULL);
1329 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1330 osip_via_t *via=NULL;
1331 osip_message_get_via(msg,0,&via);
1333 osip_free(via->port);
1334 via->port=osip_strdup(port);
1335 osip_free(via->host);
1336 via->host=osip_strdup(ip);
1340 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1341 osip_message_t *msg;
1342 const char *received;
1344 osip_contact_t *ctt=NULL;
1349 if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1350 osip_message_get_contact(orig_request,0,&ctt);
1351 if (strcmp(ctt->url->host,received)==0){
1352 /*ip address matches, check ports*/
1353 const char *contact_port=ctt->url->port;
1354 if (contact_port==NULL || contact_port[0]=='\0')
1355 contact_port="5060";
1356 if (atoi(contact_port)==rport){
1357 ms_message("Register has up to date contact, doing nothing.");
1359 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1363 eXosip_register_build_register(op->rid,op->expires,&msg);
1366 ms_warning("Fail to create a contact updated register.");
1369 osip_message_get_contact(msg,0,&ctt);
1370 if (ctt->url->host!=NULL){
1371 osip_free(ctt->url->host);
1373 ctt->url->host=osip_strdup(received);
1374 if (ctt->url->port!=NULL){
1375 osip_free(ctt->url->port);
1377 snprintf(port,sizeof(port),"%i",rport);
1378 ctt->url->port=osip_strdup(port);
1379 if (op->masquerade_via) masquerade_via(msg,received,port);
1380 eXosip_register_send_register(op->rid,msg);
1382 osip_contact_to_str(ctt,&tmp);
1383 addr=sal_address_new(tmp);
1385 sal_address_clean(addr);
1386 tmp=sal_address_as_string(addr);
1387 sal_op_set_contact(op,tmp);
1388 sal_address_destroy(addr);
1389 ms_message("Resending new register with updated contact %s",tmp);
1394 static void registration_success(Sal *sal, eXosip_event_t *ev){
1395 SalOp *op=sal_find_register(sal,ev->rid);
1396 osip_header_t *h=NULL;
1399 ms_error("Receiving register response for unknown operation");
1402 osip_message_get_expires(ev->request,0,&h);
1403 if (h!=NULL && atoi(h->hvalue)!=0){
1405 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1406 sal->callbacks.register_success(op,registered);
1408 }else registered=FALSE;
1411 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1413 const char *reason=NULL;
1414 SalOp *op=sal_find_register(sal,ev->rid);
1415 SalReason sr=SalReasonUnknown;
1416 SalError se=SalErrorUnknown;
1419 ms_error("Receiving register failure for unknown operation");
1423 status_code=osip_message_get_status_code(ev->response);
1424 reason=osip_message_get_reason_phrase(ev->response);
1426 switch(status_code){
1429 return process_authentication(sal,ev);
1431 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1432 in vias, such as ekiga.net
1433 On the opposite, freephonie.net bugs when via are masqueraded.
1435 op->masquerade_via=TRUE;
1437 /* if contact is up to date, process the failure, otherwise resend a new register with
1438 updated contact first, just in case the faillure is due to incorrect contact */
1439 if (register_again_with_updated_contact(op,ev->request,ev->response))
1440 return TRUE; /*we are retrying with an updated contact*/
1441 if (status_code==403){
1443 sr=SalReasonForbidden;
1444 }else if (status_code==0){
1445 se=SalErrorNoResponse;
1447 sal->callbacks.register_failure(op,se,sr,reason);
1452 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1453 SalOp *op=find_op(sal,ev);
1456 ms_warning("other_request_reply(): Receiving response to unknown request.");
1460 update_contact_from_response(op,ev->response);
1461 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1462 sal->callbacks.ping_reply(op);
1466 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1467 ms_message("linphone process event get a message %d\n",ev->type);
1469 case EXOSIP_CALL_ANSWERED:
1470 ms_message("CALL_ANSWERED\n");
1471 call_accepted(sal,ev);
1472 authentication_ok(sal,ev);
1474 case EXOSIP_CALL_CLOSED:
1475 case EXOSIP_CALL_CANCELLED:
1476 ms_message("CALL_CLOSED or CANCELLED\n");
1477 call_terminated(sal,ev);
1479 case EXOSIP_CALL_TIMEOUT:
1480 case EXOSIP_CALL_NOANSWER:
1481 ms_message("CALL_TIMEOUT or NOANSWER\n");
1482 return call_failure(sal,ev);
1484 case EXOSIP_CALL_REQUESTFAILURE:
1485 case EXOSIP_CALL_GLOBALFAILURE:
1486 case EXOSIP_CALL_SERVERFAILURE:
1487 ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1488 return call_failure(sal,ev);
1490 case EXOSIP_CALL_RELEASED:
1491 ms_message("CALL_RELEASED\n");
1492 call_released(sal, ev);
1494 case EXOSIP_CALL_INVITE:
1495 ms_message("CALL_NEW\n");
1496 inc_new_call(sal,ev);
1498 case EXOSIP_CALL_REINVITE:
1499 handle_reinvite(sal,ev);
1501 case EXOSIP_CALL_ACK:
1502 ms_message("CALL_ACK");
1505 case EXOSIP_CALL_REDIRECTED:
1506 ms_message("CALL_REDIRECTED");
1507 eXosip_default_action(ev);
1509 case EXOSIP_CALL_PROCEEDING:
1510 ms_message("CALL_PROCEEDING");
1511 call_proceeding(sal,ev);
1513 case EXOSIP_CALL_RINGING:
1514 ms_message("CALL_RINGING");
1515 call_ringing(sal,ev);
1517 case EXOSIP_CALL_MESSAGE_NEW:
1518 ms_message("EXOSIP_CALL_MESSAGE_NEW");
1519 call_message_new(sal,ev);
1521 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1522 if (ev->did<0 && ev->response &&
1523 (ev->response->status_code==407 || ev->response->status_code==401)){
1524 return process_authentication(sal,ev);
1527 case EXOSIP_IN_SUBSCRIPTION_NEW:
1528 ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1529 sal_exosip_subscription_recv(sal,ev);
1531 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1532 ms_message("CALL_SUBSCRIPTION_NEW ");
1533 sal_exosip_in_subscription_closed(sal,ev);
1535 case EXOSIP_SUBSCRIPTION_UPDATE:
1536 ms_message("CALL_SUBSCRIPTION_UPDATE");
1538 case EXOSIP_SUBSCRIPTION_NOTIFY:
1539 ms_message("CALL_SUBSCRIPTION_NOTIFY");
1540 sal_exosip_notify_recv(sal,ev);
1542 case EXOSIP_SUBSCRIPTION_ANSWERED:
1543 ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1544 sal_exosip_subscription_answered(sal,ev);
1546 case EXOSIP_SUBSCRIPTION_CLOSED:
1547 ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1548 sal_exosip_subscription_closed(sal,ev);
1550 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE: /**< announce a request failure */
1551 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1552 return process_authentication(sal,ev);
1554 case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1555 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1556 sal_exosip_subscription_closed(sal,ev);
1558 case EXOSIP_REGISTRATION_FAILURE:
1559 ms_message("REGISTRATION_FAILURE\n");
1560 return registration_failure(sal,ev);
1562 case EXOSIP_REGISTRATION_SUCCESS:
1563 authentication_ok(sal,ev);
1564 registration_success(sal,ev);
1566 case EXOSIP_MESSAGE_NEW:
1567 other_request(sal,ev);
1569 case EXOSIP_MESSAGE_PROCEEDING:
1570 case EXOSIP_MESSAGE_ANSWERED:
1571 case EXOSIP_MESSAGE_REDIRECTED:
1572 case EXOSIP_MESSAGE_SERVERFAILURE:
1573 case EXOSIP_MESSAGE_GLOBALFAILURE:
1574 other_request_reply(sal,ev);
1576 case EXOSIP_MESSAGE_REQUESTFAILURE:
1577 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1578 return process_authentication(sal,ev);
1580 other_request_reply(sal,ev);
1583 ms_message("Unhandled exosip event ! %i",ev->type);
1589 int sal_iterate(Sal *sal){
1591 while((ev=eXosip_event_wait(0,0))!=NULL){
1592 if (process_event(sal,ev))
1593 eXosip_event_free(ev);
1596 eXosip_automatic_refresh();
1601 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1602 osip_message_t *msg;
1603 sal_op_set_route(h,proxy);
1606 h->rid=eXosip_register_build_initial_register(from,proxy,sal_op_get_contact(h),expires,&msg);
1607 sal_add_register(h->base.root,h);
1610 eXosip_register_build_register(h->rid,expires,&msg);
1612 eXosip_register_send_register(h->rid,msg);
1618 int sal_unregister(SalOp *h){
1619 osip_message_t *msg=NULL;
1621 eXosip_register_build_register(h->rid,0,&msg);
1622 if (msg) eXosip_register_send_register(h->rid,msg);
1623 else ms_warning("Could not build unREGISTER !");
1628 SalAddress * sal_address_new(const char *uri){
1630 osip_from_init(&from);
1631 if (osip_from_parse(from,uri)!=0){
1632 osip_from_free(from);
1635 if (from->displayname!=NULL && from->displayname[0]=='"'){
1636 char *unquoted=osip_strdup_without_quote(from->displayname);
1637 osip_free(from->displayname);
1638 from->displayname=unquoted;
1640 return (SalAddress*)from;
1643 SalAddress * sal_address_clone(const SalAddress *addr){
1644 osip_from_t *ret=NULL;
1645 osip_from_clone((osip_from_t*)addr,&ret);
1646 return (SalAddress*)ret;
1649 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1651 const char *sal_address_get_scheme(const SalAddress *addr){
1652 const osip_from_t *u=(const osip_from_t*)addr;
1653 return null_if_empty(u->url->scheme);
1656 const char *sal_address_get_display_name(const SalAddress* addr){
1657 const osip_from_t *u=(const osip_from_t*)addr;
1658 return null_if_empty(u->displayname);
1661 const char *sal_address_get_username(const SalAddress *addr){
1662 const osip_from_t *u=(const osip_from_t*)addr;
1663 return null_if_empty(u->url->username);
1666 const char *sal_address_get_domain(const SalAddress *addr){
1667 const osip_from_t *u=(const osip_from_t*)addr;
1668 return null_if_empty(u->url->host);
1671 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1672 osip_from_t *u=(osip_from_t*)addr;
1673 if (u->displayname!=NULL){
1674 osip_free(u->displayname);
1675 u->displayname=NULL;
1677 if (display_name!=NULL && display_name[0]!='\0'){
1678 u->displayname=osip_strdup(display_name);
1682 void sal_address_set_username(SalAddress *addr, const char *username){
1683 osip_from_t *uri=(osip_from_t*)addr;
1684 if (uri->url->username!=NULL){
1685 osip_free(uri->url->username);
1686 uri->url->username=NULL;
1689 uri->url->username=osip_strdup(username);
1692 void sal_address_set_domain(SalAddress *addr, const char *host){
1693 osip_from_t *uri=(osip_from_t*)addr;
1694 if (uri->url->host!=NULL){
1695 osip_free(uri->url->host);
1696 uri->url->host=NULL;
1699 uri->url->host=osip_strdup(host);
1702 void sal_address_set_port(SalAddress *addr, const char *port){
1703 osip_from_t *uri=(osip_from_t*)addr;
1704 if (uri->url->port!=NULL){
1705 osip_free(uri->url->port);
1706 uri->url->port=NULL;
1709 uri->url->port=osip_strdup(port);
1712 void sal_address_set_port_int(SalAddress *uri, int port){
1715 /*this is the default, special case to leave the port field blank*/
1716 sal_address_set_port(uri,NULL);
1719 snprintf(tmp,sizeof(tmp),"%i",port);
1720 sal_address_set_port(uri,tmp);
1723 void sal_address_clean(SalAddress *addr){
1724 osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1725 osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
1728 char *sal_address_as_string(const SalAddress *u){
1730 osip_from_t *from=(osip_from_t *)u;
1731 char *old_displayname=NULL;
1732 /* hack to force use of quotes around the displayname*/
1733 if (from->displayname!=NULL
1734 && from->displayname[0]!='"'){
1735 old_displayname=from->displayname;
1736 from->displayname=osip_enquote(from->displayname);
1738 osip_from_to_str(from,&tmp);
1739 if (old_displayname!=NULL){
1740 ms_free(from->displayname);
1741 from->displayname=old_displayname;
1748 char *sal_address_as_string_uri_only(const SalAddress *u){
1749 char *tmp=NULL,*ret;
1750 osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
1755 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
1756 osip_uri_uparam_add (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
1759 void sal_address_destroy(SalAddress *u){
1760 osip_from_free((osip_from_t*)u);
1763 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
1764 ctx->keepalive_period=value;
1765 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
1767 const char * sal_address_get_port(const SalAddress *addr) {
1768 const osip_from_t *u=(const osip_from_t*)addr;
1769 return null_if_empty(u->url->port);
1771 int sal_address_get_port_int(const SalAddress *uri) {
1772 const char* port = sal_address_get_port(uri);