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;
172 bool_t sal_call_autoanswer_asked(SalOp *op)
174 return op->auto_answer_asked;
177 void sal_op_release(SalOp *op){
179 sdp_message_free(op->sdp_answer);
180 if (op->pending_auth)
181 eXosip_event_free(op->pending_auth);
183 sal_remove_register(op->base.root,op->rid);
186 ms_message("Cleaning cid %i",op->cid);
187 sal_remove_call(op->base.root,op);
190 sal_remove_out_subscribe(op->base.root,op);
193 sal_remove_in_subscribe(op->base.root,op);
195 osip_call_id_free(op->call_id);
198 if (op->pending_auth){
199 sal_remove_pending_auth(op->base.root,op);
202 sal_media_description_unref(op->result);
204 sal_remove_other(op->base.root,op);
205 osip_call_id_free(op->call_id);
208 ms_free(op->replaces);
210 if (op->referred_by){
211 ms_free(op->referred_by);
214 sal_auth_info_delete(op->auth_info);
219 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
220 int ortp_level=ORTP_DEBUG;
226 ortp_level=ORTP_MESSAGE;
229 ortp_level=ORTP_WARNING;
233 ortp_level=ORTP_ERROR;
236 ortp_level=ORTP_FATAL;
238 case END_TRACE_LEVEL:
241 if (ortp_log_level_enabled(level)){
242 int len=strlen(chfr);
243 char *chfrdup=ortp_strdup(chfr);
244 /*need to remove endline*/
246 if (chfrdup[len-1]=='\n')
248 if (chfrdup[len-2]=='\r')
251 ortp_logv(ortp_level,chfrdup,ap);
258 static bool_t firsttime=TRUE;
261 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
266 sal->keepalive_period=30;
267 sal->double_reg=TRUE;
271 void sal_uninit(Sal* sal){
276 void sal_set_user_pointer(Sal *sal, void *user_data){
280 void *sal_get_user_pointer(const Sal *sal){
284 static void unimplemented_stub(){
285 ms_warning("Unimplemented SAL callback");
288 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
289 memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
290 if (ctx->callbacks.call_received==NULL)
291 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
292 if (ctx->callbacks.call_ringing==NULL)
293 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
294 if (ctx->callbacks.call_accepted==NULL)
295 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
296 if (ctx->callbacks.call_failure==NULL)
297 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
298 if (ctx->callbacks.call_terminated==NULL)
299 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
300 if (ctx->callbacks.call_released==NULL)
301 ctx->callbacks.call_released=(SalOnCallReleased)unimplemented_stub;
302 if (ctx->callbacks.call_updating==NULL)
303 ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
304 if (ctx->callbacks.auth_requested==NULL)
305 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
306 if (ctx->callbacks.auth_success==NULL)
307 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
308 if (ctx->callbacks.register_success==NULL)
309 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
310 if (ctx->callbacks.register_failure==NULL)
311 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
312 if (ctx->callbacks.dtmf_received==NULL)
313 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
314 if (ctx->callbacks.notify==NULL)
315 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
316 if (ctx->callbacks.notify_presence==NULL)
317 ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
318 if (ctx->callbacks.subscribe_received==NULL)
319 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
320 if (ctx->callbacks.text_received==NULL)
321 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
322 if (ctx->callbacks.internal_message==NULL)
323 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
324 if (ctx->callbacks.ping_reply==NULL)
325 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
328 int sal_unlisten_ports(Sal *ctx){
336 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
339 int proto=IPPROTO_UDP;
342 case SalTransportDatagram:
345 case SalTransportStream:
349 ms_warning("unexpected proto, using datagram");
353 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
354 version of eXosip, which is not the case*/
355 /*see if it looks like an IPv6 address*/
356 ipv6=strchr(addr,':')!=NULL;
357 eXosip_enable_ipv6(ipv6);
360 ms_fatal("SIP over TLS or DTLS is not supported yet.");
363 err=eXosip_listen_addr(proto, addr, port, ipv6 ? PF_INET6 : PF_INET, 0);
364 #ifdef HAVE_EXOSIP_GET_SOCKET
365 ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
367 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &ctx->keepalive_period);
372 ortp_socket_t sal_get_socket(Sal *ctx){
373 #ifdef HAVE_EXOSIP_GET_SOCKET
374 return eXosip_get_socket(IPPROTO_UDP);
376 ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
381 void sal_set_user_agent(Sal *ctx, const char *user_agent){
382 eXosip_set_user_agent(user_agent);
385 void sal_use_session_timers(Sal *ctx, int expires){
386 ctx->session_expires=expires;
389 void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
390 ctx->one_matching_codec=one_matching_codec;
393 MSList *sal_get_pending_auths(Sal *sal){
394 return ms_list_copy(sal->pending_auths);
397 void sal_use_double_registrations(Sal *ctx, bool_t enabled){
398 ctx->double_reg=enabled;
401 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
402 osip_via_t *via=NULL;
403 osip_generic_param_t *param=NULL;
404 const char *rport=NULL;
408 osip_message_get_via(msg,0,&via);
411 /* it is useless to do that with tcp since client socket might have a different port
412 than the server socket.
414 if (strcasecmp(via->protocol,"tcp")==0) return -1;
416 if (via->port && via->port[0]!='\0')
417 *rportval=atoi(via->port);
419 osip_via_param_get_byname(via,"rport",¶m);
422 if (rport && rport[0]!='\0') *rportval=atoi(rport);
427 osip_via_param_get_byname(via,"received",¶m);
428 if (param) *received=param->gvalue;
430 if (rport==NULL && *received==NULL) return -1;
434 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
438 sdp_message_to_str(msg,&sdp);
440 snprintf(clen,sizeof(clen),"%i",sdplen);
441 osip_message_set_body(sip,sdp,sdplen);
442 osip_message_set_content_type(sip,"application/sdp");
443 osip_message_set_content_length(sip,clen);
447 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
448 sdp_message_t *msg=media_description_to_sdp(desc);
450 ms_error("Fail to print sdp message !");
454 sdp_message_free(msg);
457 static void sdp_process(SalOp *h){
458 ms_message("Doing SDP offer/answer process");
460 sal_media_description_unref(h->result);
462 h->result=sal_media_description_new();
463 if (h->sdp_offering){
464 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
467 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result,h->base.root->one_matching_codec);
468 h->sdp_answer=media_description_to_sdp(h->result);
469 /*once we have generated the SDP answer, we modify the result description for processing by the upper layer.
470 It should contains media parameters constraint from the remote offer, not our response*/
471 strcpy(h->result->addr,h->base.remote_media->addr);
472 h->result->bandwidth=h->base.remote_media->bandwidth;
473 for(i=0;i<h->result->nstreams;++i){
474 if (h->result->streams[i].port>0){
475 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
476 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
477 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
478 h->result->streams[i].port=h->base.remote_media->streams[i].port;
485 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
487 sal_media_description_ref(desc);
488 if (h->base.local_media)
489 sal_media_description_unref(h->base.local_media);
490 h->base.local_media=desc;
494 int sal_call(SalOp *h, const char *from, const char *to){
496 osip_message_t *invite=NULL;
497 sal_op_set_from(h,from);
499 sal_exosip_fix_route(h);
500 err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
502 ms_error("Could not create call.");
505 osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
506 if (h->base.contact){
507 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
508 osip_message_set_contact(invite,h->base.contact);
510 if (h->base.root->session_expires!=0){
511 osip_message_set_header(invite, "Session-expires", "200");
512 osip_message_set_supported(invite, "timer");
514 if (h->base.local_media){
515 h->sdp_offering=TRUE;
516 set_sdp_from_desc(invite,h->base.local_media);
517 }else h->sdp_offering=FALSE;
519 osip_message_set_header(invite,"Replaces",h->replaces);
521 osip_message_set_header(invite,"Referred-By",h->referred_by);
525 err=eXosip_call_send_initial_invite(invite);
529 ms_error("Fail to send invite !");
532 sal_add_call(h->base.root,h);
537 int sal_call_notify_ringing(SalOp *h, bool_t early_media){
541 /*if early media send also 180 and 183 */
542 if (early_media && h->sdp_answer){
545 err=eXosip_call_build_answer(h->tid,180,&msg);
547 set_sdp(msg,h->sdp_answer);
548 eXosip_call_send_answer(h->tid,180,msg);
551 err=eXosip_call_build_answer(h->tid,183,&msg);
553 set_sdp(msg,h->sdp_answer);
554 eXosip_call_send_answer(h->tid,183,msg);
559 eXosip_call_send_answer(h->tid,180,NULL);
565 int sal_call_accept(SalOp * h){
567 const char *contact=sal_op_get_contact(h);
569 int err=eXosip_call_build_answer(h->tid,200,&msg);
570 if (err<0 || msg==NULL){
571 ms_error("Fail to build answer for call: err=%i",err);
574 if (h->base.root->session_expires!=0){
575 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
579 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
580 osip_message_set_contact(msg,contact);
583 if (h->base.local_media){
584 /*this is the case where we received an invite without SDP*/
585 if (h->sdp_offering) {
586 set_sdp_from_desc(msg,h->base.local_media);
589 set_sdp(msg,h->sdp_answer);
590 sdp_message_free(h->sdp_answer);
595 ms_error("You are accepting a call but not defined any media capabilities !");
597 eXosip_call_send_answer(h->tid,200,msg);
601 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
602 if (reason==SalReasonBusy){
604 eXosip_call_send_answer(h->tid,486,NULL);
607 else if (reason==SalReasonTemporarilyUnavailable){
609 eXosip_call_send_answer(h->tid,480,NULL);
611 }else if (reason==SalReasonDoNotDisturb){
613 eXosip_call_send_answer(h->tid,600,NULL);
615 }else if (reason==SalReasonMedia){
617 eXosip_call_send_answer(h->tid,415,NULL);
619 }else if (redirect!=NULL && reason==SalReasonRedirect){
622 if (strstr(redirect,"sip:")!=0) code=302;
625 eXosip_call_build_answer(h->tid,code,&msg);
626 osip_message_set_contact(msg,redirect);
627 eXosip_call_send_answer(h->tid,code,msg);
629 }else sal_call_terminate(h);
633 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
634 if (h->base.local_media && h->base.remote_media && !h->result){
640 int sal_call_set_referer(SalOp *h, SalOp *refered_call){
641 if (refered_call->replaces)
642 h->replaces=ms_strdup(refered_call->replaces);
643 if (refered_call->referred_by)
644 h->referred_by=ms_strdup(refered_call->referred_by);
648 int sal_ping(SalOp *op, const char *from, const char *to){
649 osip_message_t *options=NULL;
651 sal_op_set_from(op,from);
652 sal_op_set_to(op,to);
653 /*bug here: eXosip2 does not honor the route argument*/
654 eXosip_options_build_request (&options, sal_op_get_to(op),
655 sal_op_get_from(op),sal_op_get_route(op));
657 if (op->base.root->session_expires!=0){
658 osip_message_set_header(options, "Session-expires", "200");
659 osip_message_set_supported(options, "timer");
661 sal_add_other(sal_op_get_sal(op),op,options);
662 return eXosip_options_send_request(options);
667 int sal_call_accept_refer(SalOp *op){
668 osip_message_t *msg=NULL;
671 err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
674 osip_message_set_header(msg,(const char *)"event","refer");
675 osip_message_set_content_type(msg,"message/sipfrag");
676 osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
677 eXosip_call_send_request(op->did,msg);
681 ms_error("could not get a notify built\n");
687 int sal_call_refer(SalOp *h, const char *refer_to){
688 osip_message_t *msg=NULL;
691 eXosip_call_build_refer(h->did,refer_to, &msg);
692 if (msg) err=eXosip_call_send_request(h->did, msg);
698 int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
699 osip_message_t *msg=NULL;
700 char referto[256]={0};
703 if (eXosip_call_get_referto(other_call_h->did,referto,sizeof(referto)-1)!=0){
704 ms_error("eXosip_call_get_referto() failed for did=%i",other_call_h->did);
708 eXosip_call_build_refer(h->did,referto, &msg);
709 osip_message_set_header(msg,"Referred-By",h->base.from);
710 if (msg) err=eXosip_call_send_request(h->did, msg);
716 SalOp *sal_call_get_replaces(SalOp *h){
717 if (h->replaces!=NULL){
720 cid=eXosip_call_find_by_replaces(h->replaces);
723 SalOp *ret=sal_find_call(h->base.root,cid);
730 int sal_call_send_dtmf(SalOp *h, char dtmf){
731 osip_message_t *msg=NULL;
736 eXosip_call_build_info(h->did,&msg);
738 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
739 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
740 osip_message_set_content_type(msg,"application/dtmf-relay");
741 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
742 osip_message_set_content_length(msg,clen);
743 eXosip_call_send_request(h->did,msg);
749 static void push_auth_to_exosip(const SalAuthInfo *info){
751 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
752 else userid=info->userid;
753 ms_message("Authentication info for username [%s], id[%s], realm [%s] added to eXosip", info->username,userid, info->realm);
754 eXosip_add_authentication_info (info->username,userid,
755 info->password, NULL,info->realm);
758 * Just for symmetry ;-)
760 static void pop_auth_from_exosip() {
761 eXosip_clear_authentication_info();
764 int sal_call_terminate(SalOp *h){
766 if (h->auth_info) push_auth_to_exosip(h->auth_info);
768 err=eXosip_call_terminate(h->cid,h->did);
770 pop_auth_from_exosip();
772 ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
777 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
778 if (h->pending_auth){
779 push_auth_to_exosip(info);
781 eXosip_default_action(h->pending_auth);
783 ms_message("eXosip_default_action() done");
784 pop_auth_from_exosip();
785 eXosip_event_free(h->pending_auth);
786 sal_remove_pending_auth(sal_op_get_sal(h),h);
787 h->pending_auth=NULL;
788 if (h->auth_info) sal_auth_info_delete(h->auth_info); /*if already exist*/
789 h->auth_info=sal_auth_info_clone(info); /*store auth info for subsequent request*/
793 static void set_network_origin(SalOp *op, osip_message_t *req){
794 const char *received=NULL;
797 if (extract_received_rport(req,&received,&rport)!=0){
798 osip_via_t *via=NULL;
800 osip_message_get_via(req,0,&via);
801 received=osip_via_get_host(via);
802 tmp=osip_via_get_port(via);
803 if (tmp) rport=atoi(tmp);
805 snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
806 __sal_op_set_network_origin(op,origin);
809 static void set_remote_ua(SalOp* op, osip_message_t *req){
810 if (op->base.remote_ua==NULL){
811 osip_header_t *h=NULL;
812 osip_message_get_user_agent(req,0,&h);
814 op->base.remote_ua=ms_strdup(h->hvalue);
819 static void set_replaces(SalOp *op, osip_message_t *req){
820 osip_header_t *h=NULL;
823 ms_free(op->replaces);
826 osip_message_header_get_byname(req,"replaces",0,&h);
828 if (h->hvalue && h->hvalue[0]!='\0'){
829 op->replaces=ms_strdup(h->hvalue);
834 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
836 return sal_find_call(sal,ev->cid);
839 return sal_find_register(sal,ev->rid);
842 return sal_find_out_subscribe(sal,ev->sid);
844 if (ev->response) return sal_find_other(sal,ev->response);
848 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
849 SalOp *op=sal_op_new(sal);
850 osip_from_t *from,*to;
851 osip_call_info_t *call_info;
853 sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
855 set_network_origin(op,ev->request);
856 set_remote_ua(op,ev->request);
857 set_replaces(op,ev->request);
860 op->sdp_offering=FALSE;
861 op->base.remote_media=sal_media_description_new();
862 sdp_to_media_description(sdp,op->base.remote_media);
863 sdp_message_free(sdp);
864 }else op->sdp_offering=TRUE;
866 from=osip_message_get_from(ev->request);
867 to=osip_message_get_to(ev->request);
868 osip_from_to_str(from,&tmp);
869 sal_op_set_from(op,tmp);
871 osip_from_to_str(to,&tmp);
872 sal_op_set_to(op,tmp);
875 osip_message_get_call_info(ev->request,0,&call_info);
878 osip_call_info_to_str(call_info,&tmp);
879 if( strstr(tmp,"answer-after=") != NULL)
881 op->auto_answer_asked=TRUE;
882 ms_message("The caller asked to automatically answer the call(Emergency?)\n");
891 sal_add_call(op->base.root,op);
892 sal->callbacks.call_received(op);
895 static void handle_reinvite(Sal *sal, eXosip_event_t *ev){
896 SalOp *op=find_op(sal,ev);
898 osip_message_t *msg=NULL;
901 ms_warning("Reinvite for non-existing operation !");
906 sdp=eXosip_get_sdp_info(ev->request);
907 if (op->base.remote_media){
908 sal_media_description_unref(op->base.remote_media);
909 op->base.remote_media=NULL;
912 sal_media_description_unref(op->result);
916 op->sdp_offering=FALSE;
917 op->base.remote_media=sal_media_description_new();
918 sdp_to_media_description(sdp,op->base.remote_media);
919 sdp_message_free(sdp);
920 sal->callbacks.call_updating(op);
922 op->sdp_offering=TRUE;
924 eXosip_call_build_answer(ev->tid,200,&msg);
926 set_sdp_from_desc(msg,op->base.local_media);
927 eXosip_call_send_answer(ev->tid,200,msg);
934 static void handle_ack(Sal *sal, eXosip_event_t *ev){
935 SalOp *op=find_op(sal,ev);
939 ms_warning("ack for non-existing call !");
942 sdp=eXosip_get_sdp_info(ev->ack);
944 op->base.remote_media=sal_media_description_new();
945 sdp_to_media_description(sdp,op->base.remote_media);
947 sdp_message_free(sdp);
950 if (sdp) sal->callbacks.call_updating(op);
953 sal->callbacks.call_ack(op);
957 static void update_contact_from_response(SalOp *op, osip_message_t *response){
958 const char *received;
960 if (extract_received_rport(response,&received,&rport)==0){
961 const char *contact=sal_op_get_contact(op);
963 /*no contact given yet, use from instead*/
964 contact=sal_op_get_from(op);
967 SalAddress *addr=sal_address_new(contact);
969 sal_address_set_domain(addr,received);
970 sal_address_set_port_int(addr,rport);
971 tmp=sal_address_as_string(addr);
972 ms_message("Contact address updated to %s for this dialog",tmp);
973 sal_op_set_contact(op,tmp);
974 sal_address_destroy(addr);
980 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
981 SalOp *op=find_op(sal,ev);
984 ms_warning("This call has been canceled.");
986 eXosip_call_terminate(ev->cid,ev->did);
994 /* update contact if received and rport are set by the server
995 note: will only be used by remote for next INVITE, if any...*/
996 update_contact_from_response(op,ev->response);
1000 static void call_ringing(Sal *sal, eXosip_event_t *ev){
1002 SalOp *op=find_op(sal,ev);
1003 if (call_proceeding(sal, ev)==-1) return;
1005 set_remote_ua(op,ev->response);
1006 sdp=eXosip_get_sdp_info(ev->response);
1008 op->base.remote_media=sal_media_description_new();
1009 sdp_to_media_description(sdp,op->base.remote_media);
1010 sdp_message_free(sdp);
1011 if (op->base.local_media) sdp_process(op);
1013 sal->callbacks.call_ringing(op);
1016 static void call_accepted(Sal *sal, eXosip_event_t *ev){
1018 osip_message_t *msg=NULL;
1019 SalOp *op=find_op(sal,ev);
1020 const char *contact;
1023 ms_error("A closed call is accepted ?");
1028 set_remote_ua(op,ev->response);
1030 sdp=eXosip_get_sdp_info(ev->response);
1032 op->base.remote_media=sal_media_description_new();
1033 sdp_to_media_description(sdp,op->base.remote_media);
1034 sdp_message_free(sdp);
1035 if (op->base.local_media) sdp_process(op);
1037 eXosip_call_build_ack(ev->did,&msg);
1038 contact=sal_op_get_contact(op);
1040 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1041 osip_message_set_contact(msg,contact);
1043 if (op->sdp_answer){
1044 set_sdp(msg,op->sdp_answer);
1045 sdp_message_free(op->sdp_answer);
1046 op->sdp_answer=NULL;
1048 eXosip_call_send_ack(ev->did,msg);
1049 sal->callbacks.call_accepted(op);
1052 static void call_terminated(Sal *sal, eXosip_event_t *ev){
1054 SalOp *op=find_op(sal,ev);
1056 ms_warning("Call terminated for already closed call ?");
1060 osip_from_to_str(ev->request->from,&from);
1062 sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
1063 if (from) osip_free(from);
1066 static void call_released(Sal *sal, eXosip_event_t *ev){
1067 SalOp *op=find_op(sal,ev);
1069 ms_warning("No op associated to this call_released()");
1073 sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL, 487);
1075 sal->callbacks.call_released(op);
1078 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
1079 const char *prx_realm=NULL,*www_realm=NULL;
1080 osip_proxy_authenticate_t *prx_auth;
1081 osip_www_authenticate_t *www_auth;
1083 *username=osip_uri_get_username(resp->from->url);
1084 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
1085 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
1087 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
1089 www_realm=osip_www_authenticate_get_realm(www_auth);
1093 }else if (www_realm){
1101 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
1102 osip_authorization_t *auth=NULL;
1103 osip_proxy_authorization_t *prx_auth=NULL;
1105 *username=osip_uri_get_username(msg->from->url);
1106 osip_message_get_authorization(msg, 0, &auth);
1108 *realm=osip_authorization_get_realm(auth);
1111 osip_message_get_proxy_authorization(msg,0,&prx_auth);
1113 *realm=osip_proxy_authorization_get_realm(prx_auth);
1119 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
1120 if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
1121 if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
1125 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
1126 if (op->pending_auth){
1127 return get_auth_data(op->pending_auth,realm,username);
1132 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1134 const char *username,*realm;
1137 ms_warning("No operation associated with this authentication !");
1140 if (get_auth_data(ev,&realm,&username)==0){
1141 if (op->pending_auth!=NULL)
1142 eXosip_event_free(op->pending_auth);
1143 op->pending_auth=ev;
1144 sal_add_pending_auth (sal,op);
1145 sal->callbacks.auth_requested(op,realm,username);
1151 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1153 const char *username,*realm;
1156 ms_warning("No operation associated with this authentication_ok!");
1159 if (get_auth_data(ev,&realm,&username)==0){
1160 sal->callbacks.auth_success(op,realm,username);
1164 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1167 char* computedReason=NULL;
1168 const char *reason=NULL;
1169 SalError error=SalErrorUnknown;
1170 SalReason sr=SalReasonUnknown;
1173 op=(SalOp*)find_op(sal,ev);
1176 ms_warning("Call failure reported for a closed call, ignored.");
1181 code=osip_message_get_status_code(ev->response);
1182 reason=osip_message_get_reason_phrase(ev->response);
1183 osip_header_t *h=NULL;
1184 if (!osip_message_header_get_byname( ev->response
1188 computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1189 reason = computedReason;
1197 return process_authentication(sal,ev);
1200 error=SalErrorUnknown;
1203 error=SalErrorFailure;
1204 sr=SalReasonNotFound;
1207 error=SalErrorFailure;
1211 eXosip_default_action(ev);
1215 error=SalErrorFailure;
1216 sr=SalReasonTemporarilyUnavailable;
1218 error=SalErrorFailure;
1224 error=SalErrorFailure;
1225 sr=SalReasonDoNotDisturb;
1228 error=SalErrorFailure;
1229 sr=SalReasonDeclined;
1233 error=SalErrorFailure;
1234 sr=SalReasonUnknown;
1235 }else error=SalErrorNoResponse;
1237 sal->callbacks.call_failure(op,error,sr,reason,code);
1238 if (computedReason != NULL){
1239 ms_free(computedReason);
1244 /* Request remote side to send us VFU */
1245 void sal_call_send_vfu_request(SalOp *h){
1246 osip_message_t *msg=NULL;
1248 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1252 " <picture_fast_update></picture_fast_update>"
1260 eXosip_call_build_info(h->did,&msg);
1262 osip_message_set_body(msg,info_body,strlen(info_body));
1263 osip_message_set_content_type(msg,"application/media_control+xml");
1264 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(info_body));
1265 osip_message_set_content_length(msg,clen);
1266 eXosip_call_send_request(h->did,msg);
1267 ms_message("Sending VFU request !");
1272 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1273 SalOp *op=find_op(sal,ev);
1274 osip_body_t *body=NULL;
1277 ms_warning("media control xml received without operation context!");
1281 osip_message_get_body(ev->request,0,&body);
1282 if (body && body->body!=NULL &&
1283 strstr(body->body,"picture_fast_update")){
1284 osip_message_t *ans=NULL;
1285 ms_message("Receiving VFU request !");
1286 if (sal->callbacks.vfu_request){
1287 sal->callbacks.vfu_request(op);
1288 eXosip_call_build_answer(ev->tid,200,&ans);
1290 eXosip_call_send_answer(ev->tid,200,ans);
1294 /*in all other cases we must say it is not implemented.*/
1296 osip_message_t *ans=NULL;
1298 eXosip_call_build_answer(ev->tid,501,&ans);
1300 eXosip_call_send_answer(ev->tid,501,ans);
1305 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1306 SalOp *op=find_op(sal,ev);
1307 osip_body_t *body=NULL;
1310 ms_warning("media dtmf relay received without operation context!");
1314 osip_message_get_body(ev->request,0,&body);
1315 if (body && body->body!=NULL){
1316 osip_message_t *ans=NULL;
1317 const char *name=strstr(body->body,"Signal");
1318 if (name==NULL) name=strstr(body->body,"signal");
1320 ms_warning("Could not extract the dtmf name from the SIP INFO.");
1323 name+=strlen("signal");
1324 if (sscanf(name," = %1s",tmp)==1){
1325 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1326 if (sal->callbacks.dtmf_received != NULL)
1327 sal->callbacks.dtmf_received(op, tmp[0]);
1330 eXosip_call_build_answer(ev->tid,200,&ans);
1332 eXosip_call_send_answer(ev->tid,200,ans);
1336 static void fill_options_answer(osip_message_t *options){
1337 osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1338 osip_message_set_accept(options,"application/sdp");
1341 static void process_refer(Sal *sal, SalOp *op, eXosip_event_t *ev){
1342 osip_header_t *h=NULL;
1343 osip_message_t *ans=NULL;
1344 ms_message("Receiving REFER request !");
1345 osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1348 osip_from_t *from=NULL;
1350 osip_from_init(&from);
1352 if (osip_from_parse(from,h->hvalue)==0){
1354 osip_uri_header_t *uh=NULL;
1355 osip_header_t *referred_by=NULL;
1356 osip_uri_header_get_byname(&from->url->url_headers,(char*)"Replaces",&uh);
1357 if (uh!=NULL && uh->gvalue && uh->gvalue[0]!='\0'){
1358 ms_message("Found replaces in Refer-To");
1360 ms_free(op->replaces);
1362 op->replaces=ms_strdup(uh->gvalue);
1364 osip_message_header_get_byname(ev->request,"Referred-By",0,&referred_by);
1365 if (referred_by && referred_by->hvalue && referred_by->hvalue[0]!='\0'){
1366 if (op->referred_by)
1367 ms_free(op->referred_by);
1368 op->referred_by=ms_strdup(referred_by->hvalue);
1371 osip_uri_header_freelist(&from->url->url_headers);
1372 osip_from_to_str(from,&tmp);
1373 sal->callbacks.refer_received(sal,op,tmp);
1375 osip_from_free(from);
1378 eXosip_call_build_answer(ev->tid,202,&ans);
1380 eXosip_call_send_answer(ev->tid,202,ans);
1385 ms_warning("cannot do anything with the refer without destination\n");
1389 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1390 osip_message_t *ans=NULL;
1392 if (MSG_IS_INFO(ev->request)){
1393 osip_content_type_t *ct;
1394 ct=osip_message_get_content_type(ev->request);
1395 if (ct && ct->subtype){
1396 if (strcmp(ct->subtype,"media_control+xml")==0)
1397 process_media_control_xml(sal,ev);
1398 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1399 process_dtmf_relay(sal,ev);
1401 ms_message("Unhandled SIP INFO.");
1402 /*send an "Not implemented" answer*/
1404 eXosip_call_build_answer(ev->tid,501,&ans);
1406 eXosip_call_send_answer(ev->tid,501,ans);
1410 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1412 eXosip_call_build_answer(ev->tid,200,&ans);
1414 eXosip_call_send_answer(ev->tid,200,ans);
1417 }else if(MSG_IS_MESSAGE(ev->request)){
1418 /* SIP messages could be received into call */
1419 text_received(sal, ev);
1421 eXosip_call_build_answer(ev->tid,200,&ans);
1423 eXosip_call_send_answer(ev->tid,200,ans);
1425 }else if(MSG_IS_REFER(ev->request)){
1426 SalOp *op=find_op(sal,ev);
1428 ms_message("Receiving REFER request !");
1429 process_refer(sal,op,ev);
1430 }else if(MSG_IS_NOTIFY(ev->request)){
1431 osip_header_t *h=NULL;
1433 SalOp *op=find_op(sal,ev);
1435 ms_message("Receiving NOTIFY request !");
1436 osip_from_to_str(ev->request->from,&from);
1437 osip_message_header_get_byname(ev->request,"Event",0,&h);
1439 sal->callbacks.notify(op,from,h->hvalue);
1440 /*answer that we received the notify*/
1442 eXosip_call_build_answer(ev->tid,200,&ans);
1444 eXosip_call_send_answer(ev->tid,200,ans);
1447 }else if (MSG_IS_OPTIONS(ev->request)){
1449 eXosip_call_build_answer(ev->tid,200,&ans);
1451 fill_options_answer(ans);
1452 eXosip_call_send_answer(ev->tid,200,ans);
1456 }else ms_warning("call_message_new: No request ?");
1459 static void inc_update(Sal *sal, eXosip_event_t *ev){
1460 osip_message_t *msg=NULL;
1461 ms_message("Processing incoming UPDATE");
1463 eXosip_message_build_answer(ev->tid,200,&msg);
1465 eXosip_message_send_answer(ev->tid,200,msg);
1469 static bool_t comes_from_local_if(osip_message_t *msg){
1470 osip_via_t *via=NULL;
1471 osip_message_get_via(msg,0,&via);
1474 host=osip_via_get_host(via);
1475 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1476 osip_generic_param_t *param=NULL;
1477 osip_via_param_get_byname(via,"received",¶m);
1478 if (param==NULL) return TRUE;
1479 if (param->gvalue &&
1480 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1488 static void text_received(Sal *sal, eXosip_event_t *ev){
1489 osip_body_t *body=NULL;
1490 char *from=NULL,*msg;
1492 osip_message_get_body(ev->request,0,&body);
1494 ms_error("Could not get text message from SIP body");
1498 osip_from_to_str(ev->request->from,&from);
1499 sal->callbacks.text_received(sal,from,msg);
1505 static void other_request(Sal *sal, eXosip_event_t *ev){
1506 ms_message("in other_request");
1507 if (ev->request==NULL) return;
1508 if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1509 text_received(sal,ev);
1510 eXosip_message_send_answer(ev->tid,200,NULL);
1511 }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1512 osip_message_t *options=NULL;
1513 eXosip_options_build_answer(ev->tid,200,&options);
1514 fill_options_answer(options);
1515 eXosip_options_send_answer(ev->tid,200,options);
1516 }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1517 && comes_from_local_if(ev->request)) {
1518 eXosip_message_send_answer(ev->tid,200,NULL);
1519 ms_message("Receiving WAKEUP request !");
1520 sal->callbacks.internal_message(sal,"WAKEUP");
1521 }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1522 ms_message("Receiving REFER request !");
1523 if (comes_from_local_if(ev->request)) {
1524 process_refer(sal,NULL,ev);
1525 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1526 }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1531 osip_message_to_str(ev->request,&tmp,&msglen);
1533 ms_message("Unsupported request received:\n%s",tmp);
1536 /*answer with a 501 Not implemented*/
1537 eXosip_message_send_answer(ev->tid,501,NULL);
1541 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1542 osip_via_t *via=NULL;
1543 osip_message_get_via(msg,0,&via);
1545 osip_free(via->port);
1546 via->port=osip_strdup(port);
1547 osip_free(via->host);
1548 via->host=osip_strdup(ip);
1552 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1553 osip_message_t *msg;
1554 const char *received;
1556 osip_contact_t *ctt=NULL;
1560 Sal *sal=op->base.root;
1562 if (sal->double_reg==FALSE) return FALSE;
1564 if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1565 osip_message_get_contact(orig_request,0,&ctt);
1566 if (strcmp(ctt->url->host,received)==0){
1567 /*ip address matches, check ports*/
1568 const char *contact_port=ctt->url->port;
1569 if (contact_port==NULL || contact_port[0]=='\0')
1570 contact_port="5060";
1571 if (atoi(contact_port)==rport){
1572 ms_message("Register has up to date contact, doing nothing.");
1574 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1578 eXosip_register_build_register(op->rid,op->expires,&msg);
1581 ms_warning("Fail to create a contact updated register.");
1584 osip_message_get_contact(msg,0,&ctt);
1585 if (ctt->url->host!=NULL){
1586 osip_free(ctt->url->host);
1588 ctt->url->host=osip_strdup(received);
1589 if (ctt->url->port!=NULL){
1590 osip_free(ctt->url->port);
1592 snprintf(port,sizeof(port),"%i",rport);
1593 ctt->url->port=osip_strdup(port);
1594 if (op->masquerade_via) masquerade_via(msg,received,port);
1595 eXosip_register_send_register(op->rid,msg);
1597 osip_contact_to_str(ctt,&tmp);
1598 addr=sal_address_new(tmp);
1600 sal_address_clean(addr);
1601 tmp=sal_address_as_string(addr);
1602 sal_op_set_contact(op,tmp);
1603 sal_address_destroy(addr);
1604 ms_message("Resending new register with updated contact %s",tmp);
1609 static void registration_success(Sal *sal, eXosip_event_t *ev){
1610 SalOp *op=sal_find_register(sal,ev->rid);
1611 osip_header_t *h=NULL;
1614 ms_error("Receiving register response for unknown operation");
1617 osip_message_get_expires(ev->request,0,&h);
1618 if (h!=NULL && atoi(h->hvalue)!=0){
1620 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1621 sal->callbacks.register_success(op,registered);
1624 sal->callbacks.register_success(op,FALSE);
1628 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1630 const char *reason=NULL;
1631 SalOp *op=sal_find_register(sal,ev->rid);
1632 SalReason sr=SalReasonUnknown;
1633 SalError se=SalErrorUnknown;
1636 ms_error("Receiving register failure for unknown operation");
1640 status_code=osip_message_get_status_code(ev->response);
1641 reason=osip_message_get_reason_phrase(ev->response);
1643 switch(status_code){
1646 return process_authentication(sal,ev);
1648 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1649 in vias, such as ekiga.net
1650 On the opposite, freephonie.net bugs when via are masqueraded.
1652 op->masquerade_via=TRUE;
1654 /* if contact is up to date, process the failure, otherwise resend a new register with
1655 updated contact first, just in case the faillure is due to incorrect contact */
1656 if (ev->response && register_again_with_updated_contact(op,ev->request,ev->response))
1657 return TRUE; /*we are retrying with an updated contact*/
1658 if (status_code==403){
1660 sr=SalReasonForbidden;
1661 }else if (status_code==0){
1662 se=SalErrorNoResponse;
1664 sal->callbacks.register_failure(op,se,sr,reason);
1669 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1670 SalOp *op=find_op(sal,ev);
1673 ms_warning("other_request_reply(): Receiving response to unknown request.");
1677 update_contact_from_response(op,ev->response);
1678 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1679 sal->callbacks.ping_reply(op);
1683 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1684 ms_message("linphone process event get a message %d\n",ev->type);
1686 case EXOSIP_CALL_ANSWERED:
1687 ms_message("CALL_ANSWERED\n");
1688 call_accepted(sal,ev);
1689 authentication_ok(sal,ev);
1691 case EXOSIP_CALL_CLOSED:
1692 case EXOSIP_CALL_CANCELLED:
1693 ms_message("CALL_CLOSED or CANCELLED\n");
1694 call_terminated(sal,ev);
1696 case EXOSIP_CALL_TIMEOUT:
1697 case EXOSIP_CALL_NOANSWER:
1698 ms_message("CALL_TIMEOUT or NOANSWER\n");
1699 return call_failure(sal,ev);
1701 case EXOSIP_CALL_REQUESTFAILURE:
1702 case EXOSIP_CALL_GLOBALFAILURE:
1703 case EXOSIP_CALL_SERVERFAILURE:
1704 ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1705 return call_failure(sal,ev);
1707 case EXOSIP_CALL_RELEASED:
1708 ms_message("CALL_RELEASED\n");
1709 call_released(sal, ev);
1711 case EXOSIP_CALL_INVITE:
1712 ms_message("CALL_NEW\n");
1713 inc_new_call(sal,ev);
1715 case EXOSIP_CALL_REINVITE:
1716 handle_reinvite(sal,ev);
1718 case EXOSIP_CALL_ACK:
1719 ms_message("CALL_ACK");
1722 case EXOSIP_CALL_REDIRECTED:
1723 ms_message("CALL_REDIRECTED");
1724 eXosip_default_action(ev);
1726 case EXOSIP_CALL_PROCEEDING:
1727 ms_message("CALL_PROCEEDING");
1728 call_proceeding(sal,ev);
1730 case EXOSIP_CALL_RINGING:
1731 ms_message("CALL_RINGING");
1732 call_ringing(sal,ev);
1734 case EXOSIP_CALL_MESSAGE_NEW:
1735 ms_message("EXOSIP_CALL_MESSAGE_NEW");
1736 call_message_new(sal,ev);
1738 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1740 (ev->response->status_code==407 || ev->response->status_code==401)){
1741 return process_authentication(sal,ev);
1744 case EXOSIP_IN_SUBSCRIPTION_NEW:
1745 ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1746 sal_exosip_subscription_recv(sal,ev);
1748 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1749 ms_message("CALL_SUBSCRIPTION_NEW ");
1750 sal_exosip_in_subscription_closed(sal,ev);
1752 case EXOSIP_SUBSCRIPTION_UPDATE:
1753 ms_message("CALL_SUBSCRIPTION_UPDATE");
1755 case EXOSIP_SUBSCRIPTION_NOTIFY:
1756 ms_message("CALL_SUBSCRIPTION_NOTIFY");
1757 sal_exosip_notify_recv(sal,ev);
1759 case EXOSIP_SUBSCRIPTION_ANSWERED:
1760 ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1761 sal_exosip_subscription_answered(sal,ev);
1763 case EXOSIP_SUBSCRIPTION_CLOSED:
1764 ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1765 sal_exosip_subscription_closed(sal,ev);
1767 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE: /**< announce a request failure */
1768 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1769 return process_authentication(sal,ev);
1771 case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1772 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1773 sal_exosip_subscription_closed(sal,ev);
1775 case EXOSIP_REGISTRATION_FAILURE:
1776 ms_message("REGISTRATION_FAILURE\n");
1777 return registration_failure(sal,ev);
1779 case EXOSIP_REGISTRATION_SUCCESS:
1780 authentication_ok(sal,ev);
1781 registration_success(sal,ev);
1783 case EXOSIP_MESSAGE_NEW:
1784 other_request(sal,ev);
1786 case EXOSIP_MESSAGE_PROCEEDING:
1787 case EXOSIP_MESSAGE_ANSWERED:
1788 case EXOSIP_MESSAGE_REDIRECTED:
1789 case EXOSIP_MESSAGE_SERVERFAILURE:
1790 case EXOSIP_MESSAGE_GLOBALFAILURE:
1791 other_request_reply(sal,ev);
1793 case EXOSIP_MESSAGE_REQUESTFAILURE:
1795 switch (ev->response->status_code) {
1798 return process_authentication(sal,ev);
1800 eXosip_automatic_action ();
1805 other_request_reply(sal,ev);
1808 ms_message("Unhandled exosip event ! %i",ev->type);
1814 int sal_iterate(Sal *sal){
1816 while((ev=eXosip_event_wait(0,0))!=NULL){
1817 if (process_event(sal,ev))
1818 eXosip_event_free(ev);
1821 eXosip_automatic_refresh();
1826 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1827 osip_message_t *msg;
1828 sal_op_set_route(h,proxy);
1831 h->rid=eXosip_register_build_initial_register(from,proxy,sal_op_get_contact(h),expires,&msg);
1832 sal_add_register(h->base.root,h);
1835 eXosip_register_build_register(h->rid,expires,&msg);
1837 eXosip_register_send_register(h->rid,msg);
1843 int sal_unregister(SalOp *h){
1844 osip_message_t *msg=NULL;
1846 eXosip_register_build_register(h->rid,0,&msg);
1847 if (msg) eXosip_register_send_register(h->rid,msg);
1848 else ms_warning("Could not build unREGISTER !");
1853 SalAddress * sal_address_new(const char *uri){
1855 osip_from_init(&from);
1856 if (osip_from_parse(from,uri)!=0){
1857 osip_from_free(from);
1860 if (from->displayname!=NULL && from->displayname[0]=='"'){
1861 char *unquoted=osip_strdup_without_quote(from->displayname);
1862 osip_free(from->displayname);
1863 from->displayname=unquoted;
1865 return (SalAddress*)from;
1868 SalAddress * sal_address_clone(const SalAddress *addr){
1869 osip_from_t *ret=NULL;
1870 osip_from_clone((osip_from_t*)addr,&ret);
1871 return (SalAddress*)ret;
1874 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1876 const char *sal_address_get_scheme(const SalAddress *addr){
1877 const osip_from_t *u=(const osip_from_t*)addr;
1878 return null_if_empty(u->url->scheme);
1881 const char *sal_address_get_display_name(const SalAddress* addr){
1882 const osip_from_t *u=(const osip_from_t*)addr;
1883 return null_if_empty(u->displayname);
1886 const char *sal_address_get_username(const SalAddress *addr){
1887 const osip_from_t *u=(const osip_from_t*)addr;
1888 return null_if_empty(u->url->username);
1891 const char *sal_address_get_domain(const SalAddress *addr){
1892 const osip_from_t *u=(const osip_from_t*)addr;
1893 return null_if_empty(u->url->host);
1896 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1897 osip_from_t *u=(osip_from_t*)addr;
1898 if (u->displayname!=NULL){
1899 osip_free(u->displayname);
1900 u->displayname=NULL;
1902 if (display_name!=NULL && display_name[0]!='\0'){
1903 u->displayname=osip_strdup(display_name);
1907 void sal_address_set_username(SalAddress *addr, const char *username){
1908 osip_from_t *uri=(osip_from_t*)addr;
1909 if (uri->url->username!=NULL){
1910 osip_free(uri->url->username);
1911 uri->url->username=NULL;
1914 uri->url->username=osip_strdup(username);
1917 void sal_address_set_domain(SalAddress *addr, const char *host){
1918 osip_from_t *uri=(osip_from_t*)addr;
1919 if (uri->url->host!=NULL){
1920 osip_free(uri->url->host);
1921 uri->url->host=NULL;
1924 uri->url->host=osip_strdup(host);
1927 void sal_address_set_port(SalAddress *addr, const char *port){
1928 osip_from_t *uri=(osip_from_t*)addr;
1929 if (uri->url->port!=NULL){
1930 osip_free(uri->url->port);
1931 uri->url->port=NULL;
1934 uri->url->port=osip_strdup(port);
1937 void sal_address_set_port_int(SalAddress *uri, int port){
1940 /*this is the default, special case to leave the port field blank*/
1941 sal_address_set_port(uri,NULL);
1944 snprintf(tmp,sizeof(tmp),"%i",port);
1945 sal_address_set_port(uri,tmp);
1948 void sal_address_clean(SalAddress *addr){
1949 osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1950 osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
1953 char *sal_address_as_string(const SalAddress *u){
1955 osip_from_t *from=(osip_from_t *)u;
1956 char *old_displayname=NULL;
1957 /* hack to force use of quotes around the displayname*/
1958 if (from->displayname!=NULL
1959 && from->displayname[0]!='"'){
1960 old_displayname=from->displayname;
1961 from->displayname=osip_enquote(from->displayname);
1963 osip_from_to_str(from,&tmp);
1964 if (old_displayname!=NULL){
1965 ms_free(from->displayname);
1966 from->displayname=old_displayname;
1973 char *sal_address_as_string_uri_only(const SalAddress *u){
1974 char *tmp=NULL,*ret;
1975 osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
1980 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
1981 osip_uri_uparam_add (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
1984 void sal_address_destroy(SalAddress *u){
1985 osip_from_free((osip_from_t*)u);
1988 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
1989 ctx->keepalive_period=value;
1990 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
1992 unsigned int sal_get_keepalive_period(Sal *ctx) {
1993 return ctx->keepalive_period;
1996 const char * sal_address_get_port(const SalAddress *addr) {
1997 const osip_from_t *u=(const osip_from_t*)addr;
1998 return null_if_empty(u->url->port);
2001 int sal_address_get_port_int(const SalAddress *uri) {
2002 const char* port = sal_address_get_port(uri);
2011 * Send a re-Invite used to hold the current call
2013 int sal_call_hold(SalOp *h, bool_t holdon)
2017 osip_message_t *reinvite=NULL;
2018 if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL)
2020 osip_message_set_subject(reinvite,holdon ? "Phone call hold" : "Phone call resume" );
2021 osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2022 if (h->base.root->session_expires!=0){
2023 osip_message_set_header(reinvite, "Session-expires", "200");
2024 osip_message_set_supported(reinvite, "timer");
2026 //add something to say that the distant sip phone will be in sendonly/sendrecv mode
2027 if (h->base.local_media){
2028 h->sdp_offering=TRUE;
2029 sal_media_description_set_dir(h->base.local_media, holdon ? SalStreamSendOnly : SalStreamSendRecv);
2030 set_sdp_from_desc(reinvite,h->base.local_media);
2031 }else h->sdp_offering=FALSE;
2033 err = eXosip_call_send_request(h->did, reinvite);
2039 /* sends a reinvite. Local media description may have changed by application since call establishment*/
2040 int sal_call_update(SalOp *h){
2042 osip_message_t *reinvite=NULL;
2045 if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL){
2050 osip_message_set_subject(reinvite,osip_strdup("Phone call parameters updated"));
2051 osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2052 if (h->base.root->session_expires!=0){
2053 osip_message_set_header(reinvite, "Session-expires", "200");
2054 osip_message_set_supported(reinvite, "timer");
2056 if (h->base.local_media){
2057 h->sdp_offering=TRUE;
2058 set_sdp_from_desc(reinvite,h->base.local_media);
2059 }else h->sdp_offering=FALSE;
2061 err = eXosip_call_send_request(h->did, reinvite);