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 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 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;
165 op->referred_by=NULL;
166 op->masquerade_via=FALSE;
167 op->auto_answer_asked=FALSE;
171 bool_t sal_call_autoanswer_asked(SalOp *op)
173 return op->auto_answer_asked;
176 void sal_op_release(SalOp *op){
178 sdp_message_free(op->sdp_answer);
179 if (op->pending_auth)
180 eXosip_event_free(op->pending_auth);
182 sal_remove_register(op->base.root,op->rid);
185 ms_message("Cleaning cid %i",op->cid);
186 sal_remove_call(op->base.root,op);
189 sal_remove_out_subscribe(op->base.root,op);
192 sal_remove_in_subscribe(op->base.root,op);
194 osip_call_id_free(op->call_id);
197 if (op->pending_auth){
198 sal_remove_pending_auth(op->base.root,op);
201 sal_media_description_unref(op->result);
203 sal_remove_other(op->base.root,op);
204 osip_call_id_free(op->call_id);
207 ms_free(op->replaces);
209 if (op->referred_by){
210 ms_free(op->referred_by);
215 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
216 int ortp_level=ORTP_DEBUG;
222 ortp_level=ORTP_MESSAGE;
225 ortp_level=ORTP_WARNING;
229 ortp_level=ORTP_ERROR;
232 ortp_level=ORTP_FATAL;
234 case END_TRACE_LEVEL:
237 if (ortp_log_level_enabled(level)){
238 int len=strlen(chfr);
239 char *chfrdup=ortp_strdup(chfr);
240 /*need to remove endline*/
242 if (chfrdup[len-1]=='\n')
244 if (chfrdup[len-2]=='\r')
247 ortp_logv(ortp_level,chfrdup,ap);
254 static bool_t firsttime=TRUE;
257 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
262 sal->keepalive_period=30;
266 void sal_uninit(Sal* sal){
271 void sal_set_user_pointer(Sal *sal, void *user_data){
275 void *sal_get_user_pointer(const Sal *sal){
279 static void unimplemented_stub(){
280 ms_warning("Unimplemented SAL callback");
283 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
284 memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
285 if (ctx->callbacks.call_received==NULL)
286 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
287 if (ctx->callbacks.call_ringing==NULL)
288 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
289 if (ctx->callbacks.call_accepted==NULL)
290 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
291 if (ctx->callbacks.call_failure==NULL)
292 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
293 if (ctx->callbacks.call_terminated==NULL)
294 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
295 if (ctx->callbacks.call_updating==NULL)
296 ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
297 if (ctx->callbacks.auth_requested==NULL)
298 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
299 if (ctx->callbacks.auth_success==NULL)
300 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
301 if (ctx->callbacks.register_success==NULL)
302 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
303 if (ctx->callbacks.register_failure==NULL)
304 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
305 if (ctx->callbacks.dtmf_received==NULL)
306 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
307 if (ctx->callbacks.notify==NULL)
308 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
309 if (ctx->callbacks.notify_presence==NULL)
310 ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
311 if (ctx->callbacks.subscribe_received==NULL)
312 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
313 if (ctx->callbacks.text_received==NULL)
314 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
315 if (ctx->callbacks.internal_message==NULL)
316 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
317 if (ctx->callbacks.ping_reply==NULL)
318 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
321 int sal_unlisten_ports(Sal *ctx){
329 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
332 int proto=IPPROTO_UDP;
335 case SalTransportDatagram:
338 case SalTransportStream:
342 ms_warning("unexpected proto, using datagram");
346 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
347 version of eXosip, which is not the case*/
348 /*see if it looks like an IPv6 address*/
349 ipv6=strchr(addr,':')!=NULL;
350 eXosip_enable_ipv6(ipv6);
353 ms_fatal("SIP over TLS or DTLS is not supported yet.");
356 err=eXosip_listen_addr(proto, addr, port, ipv6 ? PF_INET6 : PF_INET, 0);
357 #ifdef HAVE_EXOSIP_GET_SOCKET
358 ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
360 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &ctx->keepalive_period);
365 ortp_socket_t sal_get_socket(Sal *ctx){
366 #ifdef HAVE_EXOSIP_GET_SOCKET
367 return eXosip_get_socket(IPPROTO_UDP);
369 ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
374 void sal_set_user_agent(Sal *ctx, const char *user_agent){
375 eXosip_set_user_agent(user_agent);
378 void sal_use_session_timers(Sal *ctx, int expires){
379 ctx->session_expires=expires;
382 MSList *sal_get_pending_auths(Sal *sal){
383 return ms_list_copy(sal->pending_auths);
386 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
387 osip_via_t *via=NULL;
388 osip_generic_param_t *param=NULL;
389 const char *rport=NULL;
393 osip_message_get_via(msg,0,&via);
396 /* it is useless to do that with tcp since client socket might have a different port
397 than the server socket.
399 if (strcasecmp(via->protocol,"tcp")==0) return -1;
401 if (via->port && via->port[0]!='\0')
402 *rportval=atoi(via->port);
404 osip_via_param_get_byname(via,"rport",¶m);
407 if (rport && rport[0]!='\0') *rportval=atoi(rport);
412 osip_via_param_get_byname(via,"received",¶m);
413 if (param) *received=param->gvalue;
415 if (rport==NULL && *received==NULL) return -1;
419 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
423 sdp_message_to_str(msg,&sdp);
425 snprintf(clen,sizeof(clen),"%i",sdplen);
426 osip_message_set_body(sip,sdp,sdplen);
427 osip_message_set_content_type(sip,"application/sdp");
428 osip_message_set_content_length(sip,clen);
432 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
433 sdp_message_t *msg=media_description_to_sdp(desc);
435 ms_error("Fail to print sdp message !");
439 sdp_message_free(msg);
442 static void sdp_process(SalOp *h){
443 ms_message("Doing SDP offer/answer process");
445 sal_media_description_unref(h->result);
447 h->result=sal_media_description_new();
448 if (h->sdp_offering){
449 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
452 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result);
453 h->sdp_answer=media_description_to_sdp(h->result);
454 strcpy(h->result->addr,h->base.remote_media->addr);
455 h->result->bandwidth=h->base.remote_media->bandwidth;
456 for(i=0;i<h->result->nstreams;++i){
457 if (h->result->streams[i].port>0){
458 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
459 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
460 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
461 h->result->streams[i].port=h->base.remote_media->streams[i].port;
468 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
470 sal_media_description_ref(desc);
471 if (h->base.local_media)
472 sal_media_description_unref(h->base.local_media);
473 h->base.local_media=desc;
477 int sal_call(SalOp *h, const char *from, const char *to){
479 osip_message_t *invite=NULL;
480 sal_op_set_from(h,from);
482 sal_exosip_fix_route(h);
483 err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
485 ms_error("Could not create call.");
488 osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
489 if (h->base.contact){
490 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
491 osip_message_set_contact(invite,h->base.contact);
493 if (h->base.root->session_expires!=0){
494 osip_message_set_header(invite, "Session-expires", "200");
495 osip_message_set_supported(invite, "timer");
497 if (h->base.local_media){
498 h->sdp_offering=TRUE;
499 set_sdp_from_desc(invite,h->base.local_media);
500 }else h->sdp_offering=FALSE;
502 osip_message_set_header(invite,"Replaces",h->replaces);
504 osip_message_set_header(invite,"Referred-By",h->referred_by);
508 err=eXosip_call_send_initial_invite(invite);
512 ms_error("Fail to send invite !");
515 sal_add_call(h->base.root,h);
520 int sal_call_notify_ringing(SalOp *h, bool_t early_media){
524 /*if early media send also 180 and 183 */
525 if (early_media && h->sdp_answer){
528 err=eXosip_call_build_answer(h->tid,180,&msg);
530 set_sdp(msg,h->sdp_answer);
531 eXosip_call_send_answer(h->tid,180,msg);
534 err=eXosip_call_build_answer(h->tid,183,&msg);
536 set_sdp(msg,h->sdp_answer);
537 eXosip_call_send_answer(h->tid,183,msg);
542 eXosip_call_send_answer(h->tid,180,NULL);
548 int sal_call_accept(SalOp * h){
550 const char *contact=sal_op_get_contact(h);
552 int err=eXosip_call_build_answer(h->tid,200,&msg);
553 if (err<0 || msg==NULL){
554 ms_error("Fail to build answer for call: err=%i",err);
557 if (h->base.root->session_expires!=0){
558 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
562 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
563 osip_message_set_contact(msg,contact);
566 if (h->base.local_media){
567 /*this is the case where we received an invite without SDP*/
568 if (h->sdp_offering) {
569 set_sdp_from_desc(msg,h->base.local_media);
572 set_sdp(msg,h->sdp_answer);
573 sdp_message_free(h->sdp_answer);
578 ms_error("You are accepting a call but not defined any media capabilities !");
580 eXosip_call_send_answer(h->tid,200,msg);
584 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
585 if (reason==SalReasonBusy){
587 eXosip_call_send_answer(h->tid,486,NULL);
590 else if (reason==SalReasonTemporarilyUnavailable){
592 eXosip_call_send_answer(h->tid,480,NULL);
594 }else if (reason==SalReasonDoNotDisturb){
596 eXosip_call_send_answer(h->tid,600,NULL);
598 }else if (reason==SalReasonMedia){
600 eXosip_call_send_answer(h->tid,415,NULL);
602 }else if (redirect!=NULL && reason==SalReasonRedirect){
605 if (strstr(redirect,"sip:")!=0) code=302;
608 eXosip_call_build_answer(h->tid,code,&msg);
609 osip_message_set_contact(msg,redirect);
610 eXosip_call_send_answer(h->tid,code,msg);
612 }else sal_call_terminate(h);
616 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
617 if (h->base.local_media && h->base.remote_media && !h->result){
623 int sal_call_set_referer(SalOp *h, SalOp *refered_call){
624 if (refered_call->replaces)
625 h->replaces=ms_strdup(refered_call->replaces);
626 if (refered_call->referred_by)
627 h->referred_by=ms_strdup(refered_call->referred_by);
631 int sal_ping(SalOp *op, const char *from, const char *to){
632 osip_message_t *options=NULL;
634 sal_op_set_from(op,from);
635 sal_op_set_to(op,to);
636 eXosip_options_build_request (&options, sal_op_get_to(op),
637 sal_op_get_from(op),sal_op_get_route(op));
639 if (op->base.root->session_expires!=0){
640 osip_message_set_header(options, "Session-expires", "200");
641 osip_message_set_supported(options, "timer");
643 sal_add_other(sal_op_get_sal(op),op,options);
644 return eXosip_options_send_request(options);
649 int sal_call_accept_refer(SalOp *op){
650 osip_message_t *msg=NULL;
653 err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
656 osip_message_set_header(msg,(const char *)"event","refer");
657 osip_message_set_content_type(msg,"message/sipfrag");
658 osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
659 eXosip_call_send_request(op->did,msg);
663 ms_error("could not get a notify built\n");
669 int sal_call_refer(SalOp *h, const char *refer_to){
670 osip_message_t *msg=NULL;
673 eXosip_call_build_refer(h->did,refer_to, &msg);
674 if (msg) err=eXosip_call_send_request(h->did, msg);
680 int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
681 osip_message_t *msg=NULL;
682 char referto[256]={0};
685 if (eXosip_call_get_referto(other_call_h->did,referto,sizeof(referto)-1)!=0){
686 ms_error("eXosip_call_get_referto() failed for did=%i",other_call_h->did);
690 eXosip_call_build_refer(h->did,referto, &msg);
691 osip_message_set_header(msg,"Referred-By",h->base.from);
692 if (msg) err=eXosip_call_send_request(h->did, msg);
698 SalOp *sal_call_get_replaces(SalOp *h){
699 if (h->replaces!=NULL){
702 cid=eXosip_call_find_by_replaces(h->replaces);
705 SalOp *ret=sal_find_call(h->base.root,cid);
712 int sal_call_send_dtmf(SalOp *h, char dtmf){
713 osip_message_t *msg=NULL;
718 eXosip_call_build_info(h->did,&msg);
720 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
721 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
722 osip_message_set_content_type(msg,"application/dtmf-relay");
723 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
724 osip_message_set_content_length(msg,clen);
725 eXosip_call_send_request(h->did,msg);
731 int sal_call_terminate(SalOp *h){
734 err=eXosip_call_terminate(h->cid,h->did);
737 ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
739 sal_remove_call(h->base.root,h);
744 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
745 if (h->pending_auth){
747 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
748 else userid=info->userid;
749 ms_message("Authentication info for %s %s added to eXosip", info->username,info->realm);
750 eXosip_add_authentication_info (info->username,userid,
751 info->password, NULL,info->realm);
753 eXosip_default_action(h->pending_auth);
755 ms_message("eXosip_default_action() done");
756 eXosip_clear_authentication_info();
757 eXosip_event_free(h->pending_auth);
758 sal_remove_pending_auth(sal_op_get_sal(h),h);
759 h->pending_auth=NULL;
763 static void set_network_origin(SalOp *op, osip_message_t *req){
764 const char *received=NULL;
767 if (extract_received_rport(req,&received,&rport)!=0){
768 osip_via_t *via=NULL;
770 osip_message_get_via(req,0,&via);
771 received=osip_via_get_host(via);
772 tmp=osip_via_get_port(via);
773 if (tmp) rport=atoi(tmp);
775 snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
776 __sal_op_set_network_origin(op,origin);
779 static void set_remote_ua(SalOp* op, osip_message_t *req){
780 if (op->base.remote_ua==NULL){
781 osip_header_t *h=NULL;
782 osip_message_get_user_agent(req,0,&h);
784 op->base.remote_ua=ms_strdup(h->hvalue);
789 static void set_replaces(SalOp *op, osip_message_t *req){
790 osip_header_t *h=NULL;
793 ms_free(op->replaces);
796 osip_message_header_get_byname(req,"replaces",0,&h);
798 if (h->hvalue && h->hvalue[0]!='\0'){
799 op->replaces=ms_strdup(h->hvalue);
804 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
806 return sal_find_call(sal,ev->cid);
809 return sal_find_register(sal,ev->rid);
812 return sal_find_out_subscribe(sal,ev->sid);
814 if (ev->response) return sal_find_other(sal,ev->response);
818 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
819 SalOp *op=sal_op_new(sal);
820 osip_from_t *from,*to;
821 osip_call_info_t *call_info;
823 sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
825 set_network_origin(op,ev->request);
826 set_remote_ua(op,ev->request);
827 set_replaces(op,ev->request);
830 op->sdp_offering=FALSE;
831 op->base.remote_media=sal_media_description_new();
832 sdp_to_media_description(sdp,op->base.remote_media);
833 sdp_message_free(sdp);
834 }else op->sdp_offering=TRUE;
836 from=osip_message_get_from(ev->request);
837 to=osip_message_get_to(ev->request);
838 osip_from_to_str(from,&tmp);
839 sal_op_set_from(op,tmp);
841 osip_from_to_str(to,&tmp);
842 sal_op_set_to(op,tmp);
845 osip_message_get_call_info(ev->request,0,&call_info);
848 osip_call_info_to_str(call_info,&tmp);
849 if( strstr(tmp,"answer-after=") != NULL)
851 op->auto_answer_asked=TRUE;
852 ms_message("The caller asked to automatically answer the call(Emergency?)\n");
861 sal_add_call(op->base.root,op);
862 sal->callbacks.call_received(op);
865 static void handle_reinvite(Sal *sal, eXosip_event_t *ev){
866 SalOp *op=find_op(sal,ev);
868 osip_message_t *msg=NULL;
871 ms_warning("Reinvite for non-existing operation !");
876 sdp=eXosip_get_sdp_info(ev->request);
877 if (op->base.remote_media){
878 sal_media_description_unref(op->base.remote_media);
879 op->base.remote_media=NULL;
882 sal_media_description_unref(op->result);
886 op->sdp_offering=FALSE;
887 op->base.remote_media=sal_media_description_new();
888 sdp_to_media_description(sdp,op->base.remote_media);
889 sdp_message_free(sdp);
890 sal->callbacks.call_updating(op);
892 op->sdp_offering=TRUE;
894 eXosip_call_build_answer(ev->tid,200,&msg);
896 set_sdp_from_desc(msg,op->base.local_media);
897 eXosip_call_send_answer(ev->tid,200,msg);
904 static void handle_ack(Sal *sal, eXosip_event_t *ev){
905 SalOp *op=find_op(sal,ev);
909 ms_warning("ack for non-existing call !");
912 sdp=eXosip_get_sdp_info(ev->ack);
914 op->base.remote_media=sal_media_description_new();
915 sdp_to_media_description(sdp,op->base.remote_media);
917 sdp_message_free(sdp);
920 if (sdp) sal->callbacks.call_updating(op);
923 sal->callbacks.call_ack(op);
927 static void update_contact_from_response(SalOp *op, osip_message_t *response){
928 const char *received;
930 if (extract_received_rport(response,&received,&rport)==0){
931 const char *contact=sal_op_get_contact(op);
933 /*no contact given yet, use from instead*/
934 contact=sal_op_get_from(op);
937 SalAddress *addr=sal_address_new(contact);
939 sal_address_set_domain(addr,received);
940 sal_address_set_port_int(addr,rport);
941 tmp=sal_address_as_string(addr);
942 ms_message("Contact address updated to %s for this dialog",tmp);
943 sal_op_set_contact(op,tmp);
949 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
950 SalOp *op=find_op(sal,ev);
953 ms_warning("This call has been canceled.");
955 eXosip_call_terminate(ev->cid,ev->did);
963 /* update contact if received and rport are set by the server
964 note: will only be used by remote for next INVITE, if any...*/
965 update_contact_from_response(op,ev->response);
969 static void call_ringing(Sal *sal, eXosip_event_t *ev){
971 SalOp *op=find_op(sal,ev);
972 if (call_proceeding(sal, ev)==-1) return;
974 set_remote_ua(op,ev->response);
975 sdp=eXosip_get_sdp_info(ev->response);
977 op->base.remote_media=sal_media_description_new();
978 sdp_to_media_description(sdp,op->base.remote_media);
979 sdp_message_free(sdp);
980 if (op->base.local_media) sdp_process(op);
982 sal->callbacks.call_ringing(op);
985 static void call_accepted(Sal *sal, eXosip_event_t *ev){
987 osip_message_t *msg=NULL;
988 SalOp *op=find_op(sal,ev);
992 ms_error("A closed call is accepted ?");
997 set_remote_ua(op,ev->response);
999 sdp=eXosip_get_sdp_info(ev->response);
1001 op->base.remote_media=sal_media_description_new();
1002 sdp_to_media_description(sdp,op->base.remote_media);
1003 sdp_message_free(sdp);
1004 if (op->base.local_media) sdp_process(op);
1006 eXosip_call_build_ack(ev->did,&msg);
1007 contact=sal_op_get_contact(op);
1009 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1010 osip_message_set_contact(msg,contact);
1012 if (op->sdp_answer){
1013 set_sdp(msg,op->sdp_answer);
1014 sdp_message_free(op->sdp_answer);
1015 op->sdp_answer=NULL;
1017 eXosip_call_send_ack(ev->did,msg);
1018 sal->callbacks.call_accepted(op);
1021 static void call_terminated(Sal *sal, eXosip_event_t *ev){
1023 SalOp *op=find_op(sal,ev);
1025 ms_warning("Call terminated for already closed call ?");
1029 osip_from_to_str(ev->request->from,&from);
1031 sal_remove_call(sal,op);
1033 sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
1034 if (from) osip_free(from);
1037 static void call_released(Sal *sal, eXosip_event_t *ev){
1038 SalOp *op=find_op(sal,ev);
1040 ms_warning("No op associated to this call_released()");
1045 sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL, 487);
1048 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
1049 const char *prx_realm=NULL,*www_realm=NULL;
1050 osip_proxy_authenticate_t *prx_auth;
1051 osip_www_authenticate_t *www_auth;
1053 *username=osip_uri_get_username(resp->from->url);
1054 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
1055 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
1057 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
1059 www_realm=osip_www_authenticate_get_realm(www_auth);
1063 }else if (www_realm){
1071 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
1072 osip_authorization_t *auth=NULL;
1073 osip_proxy_authorization_t *prx_auth=NULL;
1075 *username=osip_uri_get_username(msg->from->url);
1076 osip_message_get_authorization(msg, 0, &auth);
1078 *realm=osip_authorization_get_realm(auth);
1081 osip_message_get_proxy_authorization(msg,0,&prx_auth);
1083 *realm=osip_proxy_authorization_get_realm(prx_auth);
1089 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
1090 if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
1091 if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
1095 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
1096 if (op->pending_auth){
1097 return get_auth_data(op->pending_auth,realm,username);
1102 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1104 const char *username,*realm;
1107 ms_warning("No operation associated with this authentication !");
1110 if (get_auth_data(ev,&realm,&username)==0){
1111 if (op->pending_auth!=NULL)
1112 eXosip_event_free(op->pending_auth);
1113 op->pending_auth=ev;
1114 sal_add_pending_auth (sal,op);
1115 sal->callbacks.auth_requested(op,realm,username);
1121 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1123 const char *username,*realm;
1126 ms_warning("No operation associated with this authentication_ok!");
1129 if (get_auth_data(ev,&realm,&username)==0){
1130 sal->callbacks.auth_success(op,realm,username);
1134 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1137 char* computedReason=NULL;
1138 const char *reason=NULL;
1139 SalError error=SalErrorUnknown;
1140 SalReason sr=SalReasonUnknown;
1143 op=(SalOp*)find_op(sal,ev);
1146 ms_warning("Call failure reported for a closed call, ignored.");
1151 code=osip_message_get_status_code(ev->response);
1152 reason=osip_message_get_reason_phrase(ev->response);
1153 osip_header_t *h=NULL;
1154 if (!osip_message_header_get_byname( ev->response
1158 computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1159 reason = computedReason;
1167 return process_authentication(sal,ev);
1170 error=SalErrorUnknown;
1173 error=SalErrorFailure;
1174 sr=SalReasonNotFound;
1177 error=SalErrorFailure;
1181 eXosip_default_action(ev);
1185 error=SalErrorFailure;
1186 sr=SalReasonTemporarilyUnavailable;
1188 error=SalErrorFailure;
1194 error=SalErrorFailure;
1195 sr=SalReasonDoNotDisturb;
1198 error=SalErrorFailure;
1199 sr=SalReasonDeclined;
1203 error=SalErrorFailure;
1204 sr=SalReasonUnknown;
1205 }else error=SalErrorNoResponse;
1207 sal->callbacks.call_failure(op,error,sr,reason,code);
1208 if (computedReason != NULL){
1209 ms_free(computedReason);
1215 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1216 SalOp *op=find_op(sal,ev);
1217 osip_body_t *body=NULL;
1220 ms_warning("media control xml received without operation context!");
1224 osip_message_get_body(ev->request,0,&body);
1225 if (body && body->body!=NULL &&
1226 strstr(body->body,"picture_fast_update")){
1227 osip_message_t *ans=NULL;
1228 ms_message("Receiving VFU request !");
1229 if (sal->callbacks.vfu_request){
1230 sal->callbacks.vfu_request(op);
1231 eXosip_call_build_answer(ev->tid,200,&ans);
1233 eXosip_call_send_answer(ev->tid,200,ans);
1237 /*in all other cases we must say it is not implemented.*/
1239 osip_message_t *ans=NULL;
1241 eXosip_call_build_answer(ev->tid,501,&ans);
1243 eXosip_call_send_answer(ev->tid,501,ans);
1248 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1249 SalOp *op=find_op(sal,ev);
1250 osip_body_t *body=NULL;
1253 ms_warning("media dtmf relay received without operation context!");
1257 osip_message_get_body(ev->request,0,&body);
1258 if (body && body->body!=NULL){
1259 osip_message_t *ans=NULL;
1260 const char *name=strstr(body->body,"Signal");
1261 if (name==NULL) name=strstr(body->body,"signal");
1263 ms_warning("Could not extract the dtmf name from the SIP INFO.");
1266 name+=strlen("signal");
1267 if (sscanf(name," = %1s",tmp)==1){
1268 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1269 if (sal->callbacks.dtmf_received != NULL)
1270 sal->callbacks.dtmf_received(op, tmp[0]);
1273 eXosip_call_build_answer(ev->tid,200,&ans);
1275 eXosip_call_send_answer(ev->tid,200,ans);
1279 static void fill_options_answer(osip_message_t *options){
1280 osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1281 osip_message_set_accept(options,"application/sdp");
1284 static void process_refer(Sal *sal, SalOp *op, eXosip_event_t *ev){
1285 osip_header_t *h=NULL;
1286 osip_message_t *ans=NULL;
1287 ms_message("Receiving REFER request !");
1288 osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1291 osip_from_t *from=NULL;
1293 osip_from_init(&from);
1295 if (osip_from_parse(from,h->hvalue)==0){
1297 osip_uri_header_t *uh=NULL;
1298 osip_header_t *referred_by=NULL;
1299 osip_uri_header_get_byname(&from->url->url_headers,(char*)"Replaces",&uh);
1300 if (uh!=NULL && uh->gvalue && uh->gvalue[0]!='\0'){
1301 ms_message("Found replaces in Refer-To");
1303 ms_free(op->replaces);
1305 op->replaces=ms_strdup(uh->gvalue);
1307 osip_message_header_get_byname(ev->request,"Referred-By",0,&referred_by);
1308 if (referred_by && referred_by->hvalue && referred_by->hvalue[0]!='\0'){
1309 if (op->referred_by)
1310 ms_free(op->referred_by);
1311 op->referred_by=ms_strdup(referred_by->hvalue);
1314 osip_uri_header_freelist(&from->url->url_headers);
1315 osip_from_to_str(from,&tmp);
1316 sal->callbacks.refer_received(sal,op,tmp);
1318 osip_from_free(from);
1321 eXosip_call_build_answer(ev->tid,202,&ans);
1323 eXosip_call_send_answer(ev->tid,202,ans);
1328 ms_warning("cannot do anything with the refer without destination\n");
1332 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1333 osip_message_t *ans=NULL;
1335 if (MSG_IS_INFO(ev->request)){
1336 osip_content_type_t *ct;
1337 ct=osip_message_get_content_type(ev->request);
1338 if (ct && ct->subtype){
1339 if (strcmp(ct->subtype,"media_control+xml")==0)
1340 process_media_control_xml(sal,ev);
1341 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1342 process_dtmf_relay(sal,ev);
1344 ms_message("Unhandled SIP INFO.");
1345 /*send an "Not implemented" answer*/
1347 eXosip_call_build_answer(ev->tid,501,&ans);
1349 eXosip_call_send_answer(ev->tid,501,ans);
1353 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1355 eXosip_call_build_answer(ev->tid,200,&ans);
1357 eXosip_call_send_answer(ev->tid,200,ans);
1360 }else if(MSG_IS_MESSAGE(ev->request)){
1361 /* SIP messages could be received into call */
1362 text_received(sal, ev);
1364 eXosip_call_build_answer(ev->tid,200,&ans);
1366 eXosip_call_send_answer(ev->tid,200,ans);
1368 }else if(MSG_IS_REFER(ev->request)){
1369 SalOp *op=find_op(sal,ev);
1371 ms_message("Receiving REFER request !");
1372 process_refer(sal,op,ev);
1373 }else if(MSG_IS_NOTIFY(ev->request)){
1374 osip_header_t *h=NULL;
1376 SalOp *op=find_op(sal,ev);
1378 ms_message("Receiving NOTIFY request !");
1379 osip_from_to_str(ev->request->from,&from);
1380 osip_message_header_get_byname(ev->request,"Event",0,&h);
1382 sal->callbacks.notify(op,from,h->hvalue);
1383 /*answer that we received the notify*/
1385 eXosip_call_build_answer(ev->tid,200,&ans);
1387 eXosip_call_send_answer(ev->tid,200,ans);
1390 }else if (MSG_IS_OPTIONS(ev->request)){
1392 eXosip_call_build_answer(ev->tid,200,&ans);
1394 fill_options_answer(ans);
1395 eXosip_call_send_answer(ev->tid,200,ans);
1399 }else ms_warning("call_message_new: No request ?");
1402 static void inc_update(Sal *sal, eXosip_event_t *ev){
1403 osip_message_t *msg=NULL;
1404 ms_message("Processing incoming UPDATE");
1406 eXosip_message_build_answer(ev->tid,200,&msg);
1408 eXosip_message_send_answer(ev->tid,200,msg);
1412 static bool_t comes_from_local_if(osip_message_t *msg){
1413 osip_via_t *via=NULL;
1414 osip_message_get_via(msg,0,&via);
1417 host=osip_via_get_host(via);
1418 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1419 osip_generic_param_t *param=NULL;
1420 osip_via_param_get_byname(via,"received",¶m);
1421 if (param==NULL) return TRUE;
1422 if (param->gvalue &&
1423 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1431 static void text_received(Sal *sal, eXosip_event_t *ev){
1432 osip_body_t *body=NULL;
1433 char *from=NULL,*msg;
1435 osip_message_get_body(ev->request,0,&body);
1437 ms_error("Could not get text message from SIP body");
1441 osip_from_to_str(ev->request->from,&from);
1442 sal->callbacks.text_received(sal,from,msg);
1448 static void other_request(Sal *sal, eXosip_event_t *ev){
1449 ms_message("in other_request");
1450 if (ev->request==NULL) return;
1451 if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1452 text_received(sal,ev);
1453 eXosip_message_send_answer(ev->tid,200,NULL);
1454 }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1455 osip_message_t *options=NULL;
1456 eXosip_options_build_answer(ev->tid,200,&options);
1457 fill_options_answer(options);
1458 eXosip_options_send_answer(ev->tid,200,options);
1459 }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1460 && comes_from_local_if(ev->request)) {
1461 eXosip_message_send_answer(ev->tid,200,NULL);
1462 ms_message("Receiving WAKEUP request !");
1463 sal->callbacks.internal_message(sal,"WAKEUP");
1464 }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1465 ms_message("Receiving REFER request !");
1466 if (comes_from_local_if(ev->request)) {
1467 process_refer(sal,NULL,ev);
1468 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1469 }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1474 osip_message_to_str(ev->request,&tmp,&msglen);
1476 ms_message("Unsupported request received:\n%s",tmp);
1479 /*answer with a 501 Not implemented*/
1480 eXosip_message_send_answer(ev->tid,501,NULL);
1484 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1485 osip_via_t *via=NULL;
1486 osip_message_get_via(msg,0,&via);
1488 osip_free(via->port);
1489 via->port=osip_strdup(port);
1490 osip_free(via->host);
1491 via->host=osip_strdup(ip);
1495 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1496 osip_message_t *msg;
1497 const char *received;
1499 osip_contact_t *ctt=NULL;
1504 if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1505 osip_message_get_contact(orig_request,0,&ctt);
1506 if (strcmp(ctt->url->host,received)==0){
1507 /*ip address matches, check ports*/
1508 const char *contact_port=ctt->url->port;
1509 if (contact_port==NULL || contact_port[0]=='\0')
1510 contact_port="5060";
1511 if (atoi(contact_port)==rport){
1512 ms_message("Register has up to date contact, doing nothing.");
1514 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1518 eXosip_register_build_register(op->rid,op->expires,&msg);
1521 ms_warning("Fail to create a contact updated register.");
1524 osip_message_get_contact(msg,0,&ctt);
1525 if (ctt->url->host!=NULL){
1526 osip_free(ctt->url->host);
1528 ctt->url->host=osip_strdup(received);
1529 if (ctt->url->port!=NULL){
1530 osip_free(ctt->url->port);
1532 snprintf(port,sizeof(port),"%i",rport);
1533 ctt->url->port=osip_strdup(port);
1534 if (op->masquerade_via) masquerade_via(msg,received,port);
1535 eXosip_register_send_register(op->rid,msg);
1537 osip_contact_to_str(ctt,&tmp);
1538 addr=sal_address_new(tmp);
1540 sal_address_clean(addr);
1541 tmp=sal_address_as_string(addr);
1542 sal_op_set_contact(op,tmp);
1543 sal_address_destroy(addr);
1544 ms_message("Resending new register with updated contact %s",tmp);
1549 static void registration_success(Sal *sal, eXosip_event_t *ev){
1550 SalOp *op=sal_find_register(sal,ev->rid);
1551 osip_header_t *h=NULL;
1554 ms_error("Receiving register response for unknown operation");
1557 osip_message_get_expires(ev->request,0,&h);
1558 if (h!=NULL && atoi(h->hvalue)!=0){
1560 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1561 sal->callbacks.register_success(op,registered);
1564 sal->callbacks.register_success(op,FALSE);
1568 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1570 const char *reason=NULL;
1571 SalOp *op=sal_find_register(sal,ev->rid);
1572 SalReason sr=SalReasonUnknown;
1573 SalError se=SalErrorUnknown;
1576 ms_error("Receiving register failure for unknown operation");
1580 status_code=osip_message_get_status_code(ev->response);
1581 reason=osip_message_get_reason_phrase(ev->response);
1583 switch(status_code){
1586 return process_authentication(sal,ev);
1588 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1589 in vias, such as ekiga.net
1590 On the opposite, freephonie.net bugs when via are masqueraded.
1592 op->masquerade_via=TRUE;
1594 /* if contact is up to date, process the failure, otherwise resend a new register with
1595 updated contact first, just in case the faillure is due to incorrect contact */
1596 if (ev->response && register_again_with_updated_contact(op,ev->request,ev->response))
1597 return TRUE; /*we are retrying with an updated contact*/
1598 if (status_code==403){
1600 sr=SalReasonForbidden;
1601 }else if (status_code==0){
1602 se=SalErrorNoResponse;
1604 sal->callbacks.register_failure(op,se,sr,reason);
1609 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1610 SalOp *op=find_op(sal,ev);
1613 ms_warning("other_request_reply(): Receiving response to unknown request.");
1617 update_contact_from_response(op,ev->response);
1618 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1619 sal->callbacks.ping_reply(op);
1623 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1624 ms_message("linphone process event get a message %d\n",ev->type);
1626 case EXOSIP_CALL_ANSWERED:
1627 ms_message("CALL_ANSWERED\n");
1628 call_accepted(sal,ev);
1629 authentication_ok(sal,ev);
1631 case EXOSIP_CALL_CLOSED:
1632 case EXOSIP_CALL_CANCELLED:
1633 ms_message("CALL_CLOSED or CANCELLED\n");
1634 call_terminated(sal,ev);
1636 case EXOSIP_CALL_TIMEOUT:
1637 case EXOSIP_CALL_NOANSWER:
1638 ms_message("CALL_TIMEOUT or NOANSWER\n");
1639 return call_failure(sal,ev);
1641 case EXOSIP_CALL_REQUESTFAILURE:
1642 case EXOSIP_CALL_GLOBALFAILURE:
1643 case EXOSIP_CALL_SERVERFAILURE:
1644 ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1645 return call_failure(sal,ev);
1647 case EXOSIP_CALL_RELEASED:
1648 ms_message("CALL_RELEASED\n");
1649 call_released(sal, ev);
1651 case EXOSIP_CALL_INVITE:
1652 ms_message("CALL_NEW\n");
1653 inc_new_call(sal,ev);
1655 case EXOSIP_CALL_REINVITE:
1656 handle_reinvite(sal,ev);
1658 case EXOSIP_CALL_ACK:
1659 ms_message("CALL_ACK");
1662 case EXOSIP_CALL_REDIRECTED:
1663 ms_message("CALL_REDIRECTED");
1664 eXosip_default_action(ev);
1666 case EXOSIP_CALL_PROCEEDING:
1667 ms_message("CALL_PROCEEDING");
1668 call_proceeding(sal,ev);
1670 case EXOSIP_CALL_RINGING:
1671 ms_message("CALL_RINGING");
1672 call_ringing(sal,ev);
1674 case EXOSIP_CALL_MESSAGE_NEW:
1675 ms_message("EXOSIP_CALL_MESSAGE_NEW");
1676 call_message_new(sal,ev);
1678 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1679 if (ev->did<0 && ev->response &&
1680 (ev->response->status_code==407 || ev->response->status_code==401)){
1681 return process_authentication(sal,ev);
1684 case EXOSIP_IN_SUBSCRIPTION_NEW:
1685 ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1686 sal_exosip_subscription_recv(sal,ev);
1688 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1689 ms_message("CALL_SUBSCRIPTION_NEW ");
1690 sal_exosip_in_subscription_closed(sal,ev);
1692 case EXOSIP_SUBSCRIPTION_UPDATE:
1693 ms_message("CALL_SUBSCRIPTION_UPDATE");
1695 case EXOSIP_SUBSCRIPTION_NOTIFY:
1696 ms_message("CALL_SUBSCRIPTION_NOTIFY");
1697 sal_exosip_notify_recv(sal,ev);
1699 case EXOSIP_SUBSCRIPTION_ANSWERED:
1700 ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1701 sal_exosip_subscription_answered(sal,ev);
1703 case EXOSIP_SUBSCRIPTION_CLOSED:
1704 ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1705 sal_exosip_subscription_closed(sal,ev);
1707 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE: /**< announce a request failure */
1708 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1709 return process_authentication(sal,ev);
1711 case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1712 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1713 sal_exosip_subscription_closed(sal,ev);
1715 case EXOSIP_REGISTRATION_FAILURE:
1716 ms_message("REGISTRATION_FAILURE\n");
1717 return registration_failure(sal,ev);
1719 case EXOSIP_REGISTRATION_SUCCESS:
1720 authentication_ok(sal,ev);
1721 registration_success(sal,ev);
1723 case EXOSIP_MESSAGE_NEW:
1724 other_request(sal,ev);
1726 case EXOSIP_MESSAGE_PROCEEDING:
1727 case EXOSIP_MESSAGE_ANSWERED:
1728 case EXOSIP_MESSAGE_REDIRECTED:
1729 case EXOSIP_MESSAGE_SERVERFAILURE:
1730 case EXOSIP_MESSAGE_GLOBALFAILURE:
1731 other_request_reply(sal,ev);
1733 case EXOSIP_MESSAGE_REQUESTFAILURE:
1734 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1735 return process_authentication(sal,ev);
1737 other_request_reply(sal,ev);
1740 ms_message("Unhandled exosip event ! %i",ev->type);
1746 int sal_iterate(Sal *sal){
1748 while((ev=eXosip_event_wait(0,0))!=NULL){
1749 if (process_event(sal,ev))
1750 eXosip_event_free(ev);
1753 eXosip_automatic_refresh();
1758 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1759 osip_message_t *msg;
1760 sal_op_set_route(h,proxy);
1763 h->rid=eXosip_register_build_initial_register(from,proxy,sal_op_get_contact(h),expires,&msg);
1764 sal_add_register(h->base.root,h);
1767 eXosip_register_build_register(h->rid,expires,&msg);
1769 eXosip_register_send_register(h->rid,msg);
1775 int sal_unregister(SalOp *h){
1776 osip_message_t *msg=NULL;
1778 eXosip_register_build_register(h->rid,0,&msg);
1779 if (msg) eXosip_register_send_register(h->rid,msg);
1780 else ms_warning("Could not build unREGISTER !");
1785 SalAddress * sal_address_new(const char *uri){
1787 osip_from_init(&from);
1788 if (osip_from_parse(from,uri)!=0){
1789 osip_from_free(from);
1792 if (from->displayname!=NULL && from->displayname[0]=='"'){
1793 char *unquoted=osip_strdup_without_quote(from->displayname);
1794 osip_free(from->displayname);
1795 from->displayname=unquoted;
1797 return (SalAddress*)from;
1800 SalAddress * sal_address_clone(const SalAddress *addr){
1801 osip_from_t *ret=NULL;
1802 osip_from_clone((osip_from_t*)addr,&ret);
1803 return (SalAddress*)ret;
1806 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1808 const char *sal_address_get_scheme(const SalAddress *addr){
1809 const osip_from_t *u=(const osip_from_t*)addr;
1810 return null_if_empty(u->url->scheme);
1813 const char *sal_address_get_display_name(const SalAddress* addr){
1814 const osip_from_t *u=(const osip_from_t*)addr;
1815 return null_if_empty(u->displayname);
1818 const char *sal_address_get_username(const SalAddress *addr){
1819 const osip_from_t *u=(const osip_from_t*)addr;
1820 return null_if_empty(u->url->username);
1823 const char *sal_address_get_domain(const SalAddress *addr){
1824 const osip_from_t *u=(const osip_from_t*)addr;
1825 return null_if_empty(u->url->host);
1828 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1829 osip_from_t *u=(osip_from_t*)addr;
1830 if (u->displayname!=NULL){
1831 osip_free(u->displayname);
1832 u->displayname=NULL;
1834 if (display_name!=NULL && display_name[0]!='\0'){
1835 u->displayname=osip_strdup(display_name);
1839 void sal_address_set_username(SalAddress *addr, const char *username){
1840 osip_from_t *uri=(osip_from_t*)addr;
1841 if (uri->url->username!=NULL){
1842 osip_free(uri->url->username);
1843 uri->url->username=NULL;
1846 uri->url->username=osip_strdup(username);
1849 void sal_address_set_domain(SalAddress *addr, const char *host){
1850 osip_from_t *uri=(osip_from_t*)addr;
1851 if (uri->url->host!=NULL){
1852 osip_free(uri->url->host);
1853 uri->url->host=NULL;
1856 uri->url->host=osip_strdup(host);
1859 void sal_address_set_port(SalAddress *addr, const char *port){
1860 osip_from_t *uri=(osip_from_t*)addr;
1861 if (uri->url->port!=NULL){
1862 osip_free(uri->url->port);
1863 uri->url->port=NULL;
1866 uri->url->port=osip_strdup(port);
1869 void sal_address_set_port_int(SalAddress *uri, int port){
1872 /*this is the default, special case to leave the port field blank*/
1873 sal_address_set_port(uri,NULL);
1876 snprintf(tmp,sizeof(tmp),"%i",port);
1877 sal_address_set_port(uri,tmp);
1880 void sal_address_clean(SalAddress *addr){
1881 osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1882 osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
1885 char *sal_address_as_string(const SalAddress *u){
1887 osip_from_t *from=(osip_from_t *)u;
1888 char *old_displayname=NULL;
1889 /* hack to force use of quotes around the displayname*/
1890 if (from->displayname!=NULL
1891 && from->displayname[0]!='"'){
1892 old_displayname=from->displayname;
1893 from->displayname=osip_enquote(from->displayname);
1895 osip_from_to_str(from,&tmp);
1896 if (old_displayname!=NULL){
1897 ms_free(from->displayname);
1898 from->displayname=old_displayname;
1905 char *sal_address_as_string_uri_only(const SalAddress *u){
1906 char *tmp=NULL,*ret;
1907 osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
1912 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
1913 osip_uri_uparam_add (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
1916 void sal_address_destroy(SalAddress *u){
1917 osip_from_free((osip_from_t*)u);
1920 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
1921 ctx->keepalive_period=value;
1922 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
1925 const char * sal_address_get_port(const SalAddress *addr) {
1926 const osip_from_t *u=(const osip_from_t*)addr;
1927 return null_if_empty(u->url->port);
1930 int sal_address_get_port_int(const SalAddress *uri) {
1931 const char* port = sal_address_get_port(uri);
1940 * Send a re-Invite used to hold the current call
1942 int sal_call_hold(SalOp *h, bool_t holdon)
1946 osip_message_t *reinvite=NULL;
1947 if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL)
1949 osip_message_set_subject(reinvite,osip_strdup("Phone Call Hold"));
1950 osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
1951 if (h->base.root->session_expires!=0){
1952 osip_message_set_header(reinvite, "Session-expires", "200");
1953 osip_message_set_supported(reinvite, "timer");
1955 //add something to say that the distant sip phone will be in sendonly/sendrecv mode
1956 if (h->base.local_media){
1957 h->sdp_offering=TRUE;
1958 sal_media_description_set_dir(h->base.local_media, holdon ? SalStreamSendOnly : SalStreamSendRecv);
1959 set_sdp_from_desc(reinvite,h->base.local_media);
1960 }else h->sdp_offering=FALSE;
1962 err = eXosip_call_send_request(h->did, reinvite);
1968 /* sends a reinvite. Local media description may have changed by application since call establishment*/
1969 int sal_call_update(SalOp *h){
1971 osip_message_t *reinvite=NULL;
1974 if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL){
1979 osip_message_set_subject(reinvite,osip_strdup("Phone call parameters updated"));
1980 osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
1981 if (h->base.root->session_expires!=0){
1982 osip_message_set_header(reinvite, "Session-expires", "200");
1983 osip_message_set_supported(reinvite, "timer");
1985 if (h->base.local_media){
1986 h->sdp_offering=TRUE;
1987 set_sdp_from_desc(reinvite,h->base.local_media);
1988 }else h->sdp_offering=FALSE;
1990 err = eXosip_call_send_request(h->did, reinvite);