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"
28 // Necessary to make it linked
29 static void for_linker() { eXosip_transport_hook_register(NULL); }
31 static bool_t call_failure(Sal *sal, eXosip_event_t *ev);
33 static void text_received(Sal *sal, eXosip_event_t *ev);
35 void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
37 while(!osip_list_eol(l,0)) {
38 data=osip_list_get(l,0);
39 osip_list_remove(l,0);
40 if (data) freefunc(data);
44 void sal_get_default_local_ip(Sal *sal, int address_family,char *ip, size_t iplen){
45 if (eXosip_guess_localip(address_family,ip,iplen)<0){
46 /*default to something */
47 strncpy(ip,address_family==AF_INET6 ? "::1" : "127.0.0.1",iplen);
48 ms_error("Could not find default routable ip address !");
53 static SalOp * sal_find_call(Sal *sal, int cid){
56 for(elem=sal->calls;elem!=NULL;elem=elem->next){
57 op=(SalOp*)elem->data;
58 if (op->cid==cid) return op;
63 static void sal_add_call(Sal *sal, SalOp *op){
64 sal->calls=ms_list_append(sal->calls,op);
67 static void sal_remove_call(Sal *sal, SalOp *op){
68 sal->calls=ms_list_remove(sal->calls, op);
71 static SalOp * sal_find_register(Sal *sal, int rid){
74 for(elem=sal->registers;elem!=NULL;elem=elem->next){
75 op=(SalOp*)elem->data;
76 if (op->rid==rid) return op;
81 static void sal_add_register(Sal *sal, SalOp *op){
82 sal->registers=ms_list_append(sal->registers,op);
85 static void sal_remove_register(Sal *sal, int rid){
88 for(elem=sal->registers;elem!=NULL;elem=elem->next){
89 op=(SalOp*)elem->data;
91 sal->registers=ms_list_remove_link(sal->registers,elem);
97 static SalOp * sal_find_other(Sal *sal, osip_message_t *response){
100 osip_call_id_t *callid=osip_message_get_call_id(response);
102 ms_error("There is no call-id in this response !");
105 for(elem=sal->other_transactions;elem!=NULL;elem=elem->next){
106 op=(SalOp*)elem->data;
107 if (osip_call_id_match(callid,op->call_id)==0) return op;
112 void sal_add_other(Sal *sal, SalOp *op, osip_message_t *request){
113 osip_call_id_t *callid=osip_message_get_call_id(request);
115 ms_error("There is no call id in the request !");
118 osip_call_id_clone(callid,&op->call_id);
119 sal->other_transactions=ms_list_append(sal->other_transactions,op);
122 static void sal_remove_other(Sal *sal, SalOp *op){
123 sal->other_transactions=ms_list_remove(sal->other_transactions,op);
127 static void sal_add_pending_auth(Sal *sal, SalOp *op){
128 sal->pending_auths=ms_list_append(sal->pending_auths,op);
132 static void sal_remove_pending_auth(Sal *sal, SalOp *op){
133 sal->pending_auths=ms_list_remove(sal->pending_auths,op);
136 void sal_exosip_fix_route(SalOp *op){
137 if (sal_op_get_route(op)!=NULL){
138 osip_route_t *rt=NULL;
139 osip_uri_param_t *lr_param=NULL;
141 osip_route_init(&rt);
142 if (osip_route_parse(rt,sal_op_get_route(op))<0){
143 ms_warning("Bad route %s!",sal_op_get_route(op));
144 sal_op_set_route(op,NULL);
146 /* check if the lr parameter is set , if not add it */
147 osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
150 osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
151 osip_route_to_str(rt,&tmproute);
152 sal_op_set_route(op,tmproute);
160 SalOp * sal_op_new(Sal *sal){
161 SalOp *op=ms_new(SalOp,1);
162 __sal_op_init(op,sal);
163 op->cid=op->did=op->tid=op->rid=op->nid=op->sid=-1;
165 op->supports_session_timers=FALSE;
166 op->sdp_offering=TRUE;
167 op->pending_auth=NULL;
172 op->referred_by=NULL;
173 op->masquerade_via=FALSE;
174 op->auto_answer_asked=FALSE;
176 op->terminated=FALSE;
180 bool_t sal_call_autoanswer_asked(SalOp *op)
182 return op->auto_answer_asked;
185 void sal_op_release(SalOp *op){
187 sdp_message_free(op->sdp_answer);
188 if (op->pending_auth)
189 eXosip_event_free(op->pending_auth);
191 sal_remove_register(op->base.root,op->rid);
194 ms_message("Cleaning cid %i",op->cid);
195 sal_remove_call(op->base.root,op);
198 sal_remove_out_subscribe(op->base.root,op);
201 sal_remove_in_subscribe(op->base.root,op);
203 osip_call_id_free(op->call_id);
206 if (op->pending_auth){
207 sal_remove_pending_auth(op->base.root,op);
210 sal_media_description_unref(op->result);
212 sal_remove_other(op->base.root,op);
213 osip_call_id_free(op->call_id);
216 ms_free(op->replaces);
218 if (op->referred_by){
219 ms_free(op->referred_by);
222 sal_auth_info_delete(op->auth_info);
227 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
228 int ortp_level=ORTP_DEBUG;
234 ortp_level=ORTP_MESSAGE;
237 ortp_level=ORTP_WARNING;
241 ortp_level=ORTP_ERROR;
244 ortp_level=ORTP_FATAL;
246 case END_TRACE_LEVEL:
249 if (ortp_log_level_enabled(level)){
250 int len=strlen(chfr);
251 char *chfrdup=ortp_strdup(chfr);
252 /*need to remove endline*/
254 if (chfrdup[len-1]=='\n')
256 if (chfrdup[len-2]=='\r')
259 ortp_logv(ortp_level,chfrdup,ap);
266 static bool_t firsttime=TRUE;
269 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
274 sal->keepalive_period=30;
275 sal->double_reg=TRUE;
276 sal->use_rports=TRUE;
281 void sal_uninit(Sal* sal){
286 void sal_set_user_pointer(Sal *sal, void *user_data){
290 void *sal_get_user_pointer(const Sal *sal){
294 static void unimplemented_stub(){
295 ms_warning("Unimplemented SAL callback");
298 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
299 memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
300 if (ctx->callbacks.call_received==NULL)
301 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
302 if (ctx->callbacks.call_ringing==NULL)
303 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
304 if (ctx->callbacks.call_accepted==NULL)
305 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
306 if (ctx->callbacks.call_failure==NULL)
307 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
308 if (ctx->callbacks.call_terminated==NULL)
309 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
310 if (ctx->callbacks.call_released==NULL)
311 ctx->callbacks.call_released=(SalOnCallReleased)unimplemented_stub;
312 if (ctx->callbacks.call_updating==NULL)
313 ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
314 if (ctx->callbacks.auth_requested==NULL)
315 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
316 if (ctx->callbacks.auth_success==NULL)
317 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
318 if (ctx->callbacks.register_success==NULL)
319 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
320 if (ctx->callbacks.register_failure==NULL)
321 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
322 if (ctx->callbacks.dtmf_received==NULL)
323 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
324 if (ctx->callbacks.notify==NULL)
325 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
326 if (ctx->callbacks.notify_presence==NULL)
327 ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
328 if (ctx->callbacks.subscribe_received==NULL)
329 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
330 if (ctx->callbacks.text_received==NULL)
331 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
332 if (ctx->callbacks.internal_message==NULL)
333 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
334 if (ctx->callbacks.ping_reply==NULL)
335 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
338 int sal_unlisten_ports(Sal *ctx){
347 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
350 int proto=IPPROTO_UDP;
351 int keepalive = ctx->keepalive_period;
354 case SalTransportDatagram:
356 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &keepalive);
358 case SalTransportStream:
361 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE,&keepalive);
364 ms_warning("unexpected proto, using datagram");
368 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
369 version of eXosip, which is not the case*/
370 /*see if it looks like an IPv6 address*/
371 int use_rports = ctx->use_rports; // Copy char to int to avoid bad alignment
372 eXosip_set_option(EXOSIP_OPT_USE_RPORT,&use_rports);
373 int dont_use_101 = !ctx->use_101; // Copy char to int to avoid bad alignment
374 eXosip_set_option(EXOSIP_OPT_DONT_SEND_101,&dont_use_101);
376 ipv6=strchr(addr,':')!=NULL;
377 eXosip_enable_ipv6(ipv6);
380 ms_fatal("SIP over TLS or DTLS is not supported yet.");
383 err=eXosip_listen_addr(proto, addr, port, ipv6 ? PF_INET6 : PF_INET, 0);
384 #ifdef HAVE_EXOSIP_GET_SOCKET
385 ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
392 ortp_socket_t sal_get_socket(Sal *ctx){
393 #ifdef HAVE_EXOSIP_GET_SOCKET
394 return eXosip_get_socket(IPPROTO_UDP);
396 ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
401 void sal_set_user_agent(Sal *ctx, const char *user_agent){
402 eXosip_set_user_agent(user_agent);
405 void sal_use_session_timers(Sal *ctx, int expires){
406 ctx->session_expires=expires;
409 void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
410 ctx->one_matching_codec=one_matching_codec;
413 MSList *sal_get_pending_auths(Sal *sal){
414 return ms_list_copy(sal->pending_auths);
417 void sal_use_double_registrations(Sal *ctx, bool_t enabled){
418 ctx->double_reg=enabled;
421 void sal_use_rport(Sal *ctx, bool_t use_rports){
422 ctx->use_rports=use_rports;
424 void sal_use_101(Sal *ctx, bool_t use_101){
425 ctx->use_101=use_101;
427 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
428 osip_via_t *via=NULL;
429 osip_generic_param_t *param=NULL;
430 const char *rport=NULL;
434 osip_message_get_via(msg,0,&via);
437 /* it is useless to do that with tcp since client socket might have a different port
438 than the server socket.
440 if (strcasecmp(via->protocol,"tcp")==0) return -1;
442 if (via->port && via->port[0]!='\0')
443 *rportval=atoi(via->port);
445 osip_via_param_get_byname(via,"rport",¶m);
448 if (rport && rport[0]!='\0') *rportval=atoi(rport);
452 osip_via_param_get_byname(via,"received",¶m);
453 if (param) *received=param->gvalue;
455 if (rport==NULL && *received==NULL) return -1;
459 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
463 sdp_message_to_str(msg,&sdp);
465 snprintf(clen,sizeof(clen),"%i",sdplen);
466 osip_message_set_body(sip,sdp,sdplen);
467 osip_message_set_content_type(sip,"application/sdp");
468 osip_message_set_content_length(sip,clen);
472 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
473 sdp_message_t *msg=media_description_to_sdp(desc);
475 ms_error("Fail to print sdp message !");
479 sdp_message_free(msg);
482 static void sdp_process(SalOp *h){
483 ms_message("Doing SDP offer/answer process");
485 sal_media_description_unref(h->result);
487 h->result=sal_media_description_new();
488 if (h->sdp_offering){
489 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
492 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result,h->base.root->one_matching_codec);
493 h->sdp_answer=media_description_to_sdp(h->result);
494 /*once we have generated the SDP answer, we modify the result description for processing by the upper layer.
495 It should contains media parameters constraint from the remote offer, not our response*/
496 strcpy(h->result->addr,h->base.remote_media->addr);
497 h->result->bandwidth=h->base.remote_media->bandwidth;
498 for(i=0;i<h->result->nstreams;++i){
499 if (h->result->streams[i].port>0){
500 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
501 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
502 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
503 h->result->streams[i].port=h->base.remote_media->streams[i].port;
510 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
512 sal_media_description_ref(desc);
513 if (h->base.local_media)
514 sal_media_description_unref(h->base.local_media);
515 h->base.local_media=desc;
519 int sal_call(SalOp *h, const char *from, const char *to){
521 osip_message_t *invite=NULL;
522 sal_op_set_from(h,from);
524 sal_exosip_fix_route(h);
525 err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
527 ms_error("Could not create call.");
530 osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
531 if (h->base.contact){
532 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
533 osip_message_set_contact(invite,h->base.contact);
535 if (h->base.root->session_expires!=0){
536 osip_message_set_header(invite, "Session-expires", "200");
537 osip_message_set_supported(invite, "timer");
539 if (h->base.local_media){
540 h->sdp_offering=TRUE;
541 set_sdp_from_desc(invite,h->base.local_media);
542 }else h->sdp_offering=FALSE;
544 osip_message_set_header(invite,"Replaces",h->replaces);
546 osip_message_set_header(invite,"Referred-By",h->referred_by);
550 err=eXosip_call_send_initial_invite(invite);
554 ms_error("Fail to send invite !");
557 sal_add_call(h->base.root,h);
562 int sal_call_notify_ringing(SalOp *h, bool_t early_media){
566 /*if early media send also 180 and 183 */
567 if (early_media && h->sdp_answer){
570 err=eXosip_call_build_answer(h->tid,180,&msg);
572 set_sdp(msg,h->sdp_answer);
573 eXosip_call_send_answer(h->tid,180,msg);
576 err=eXosip_call_build_answer(h->tid,183,&msg);
578 set_sdp(msg,h->sdp_answer);
579 eXosip_call_send_answer(h->tid,183,msg);
584 eXosip_call_send_answer(h->tid,180,NULL);
590 int sal_call_accept(SalOp * h){
592 const char *contact=sal_op_get_contact(h);
594 int err=eXosip_call_build_answer(h->tid,200,&msg);
595 if (err<0 || msg==NULL){
596 ms_error("Fail to build answer for call: err=%i",err);
599 if (h->base.root->session_expires!=0){
600 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
604 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
605 osip_message_set_contact(msg,contact);
608 if (h->base.local_media){
609 /*this is the case where we received an invite without SDP*/
610 if (h->sdp_offering) {
611 set_sdp_from_desc(msg,h->base.local_media);
614 set_sdp(msg,h->sdp_answer);
615 sdp_message_free(h->sdp_answer);
620 ms_error("You are accepting a call but not defined any media capabilities !");
622 eXosip_call_send_answer(h->tid,200,msg);
626 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
627 if (reason==SalReasonBusy){
629 eXosip_call_send_answer(h->tid,486,NULL);
632 else if (reason==SalReasonTemporarilyUnavailable){
634 eXosip_call_send_answer(h->tid,480,NULL);
636 }else if (reason==SalReasonDoNotDisturb){
638 eXosip_call_send_answer(h->tid,600,NULL);
640 }else if (reason==SalReasonMedia){
642 eXosip_call_send_answer(h->tid,415,NULL);
644 }else if (redirect!=NULL && reason==SalReasonRedirect){
647 if (strstr(redirect,"sip:")!=0) code=302;
650 eXosip_call_build_answer(h->tid,code,&msg);
651 osip_message_set_contact(msg,redirect);
652 eXosip_call_send_answer(h->tid,code,msg);
654 }else sal_call_terminate(h);
658 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
659 if (h->base.local_media && h->base.remote_media && !h->result){
665 int sal_call_set_referer(SalOp *h, SalOp *refered_call){
666 if (refered_call->replaces)
667 h->replaces=ms_strdup(refered_call->replaces);
668 if (refered_call->referred_by)
669 h->referred_by=ms_strdup(refered_call->referred_by);
673 int sal_ping(SalOp *op, const char *from, const char *to){
674 osip_message_t *options=NULL;
676 sal_op_set_from(op,from);
677 sal_op_set_to(op,to);
678 /*bug here: eXosip2 does not honor the route argument*/
679 eXosip_options_build_request (&options, sal_op_get_to(op),
680 sal_op_get_from(op),sal_op_get_route(op));
682 if (op->base.root->session_expires!=0){
683 osip_message_set_header(options, "Session-expires", "200");
684 osip_message_set_supported(options, "timer");
686 sal_add_other(sal_op_get_sal(op),op,options);
687 return eXosip_options_send_request(options);
692 int sal_call_accept_refer(SalOp *op){
693 osip_message_t *msg=NULL;
696 err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
699 osip_message_set_header(msg,(const char *)"event","refer");
700 osip_message_set_content_type(msg,"message/sipfrag");
701 osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
702 eXosip_call_send_request(op->did,msg);
706 ms_error("could not get a notify built\n");
712 int sal_call_refer(SalOp *h, const char *refer_to){
713 osip_message_t *msg=NULL;
716 eXosip_call_build_refer(h->did,refer_to, &msg);
717 if (msg) err=eXosip_call_send_request(h->did, msg);
723 int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
724 osip_message_t *msg=NULL;
725 char referto[256]={0};
728 if (eXosip_call_get_referto(other_call_h->did,referto,sizeof(referto)-1)!=0){
729 ms_error("eXosip_call_get_referto() failed for did=%i",other_call_h->did);
733 eXosip_call_build_refer(h->did,referto, &msg);
734 osip_message_set_header(msg,"Referred-By",h->base.from);
735 if (msg) err=eXosip_call_send_request(h->did, msg);
741 SalOp *sal_call_get_replaces(SalOp *h){
742 if (h->replaces!=NULL){
745 cid=eXosip_call_find_by_replaces(h->replaces);
748 SalOp *ret=sal_find_call(h->base.root,cid);
755 int sal_call_send_dtmf(SalOp *h, char dtmf){
756 osip_message_t *msg=NULL;
761 eXosip_call_build_info(h->did,&msg);
763 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
764 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
765 osip_message_set_content_type(msg,"application/dtmf-relay");
766 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
767 osip_message_set_content_length(msg,clen);
768 eXosip_call_send_request(h->did,msg);
774 static void push_auth_to_exosip(const SalAuthInfo *info){
776 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
777 else userid=info->userid;
778 ms_message("Authentication info for username [%s], id[%s], realm [%s] added to eXosip", info->username,userid, info->realm);
779 eXosip_add_authentication_info (info->username,userid,
780 info->password, NULL,info->realm);
783 * Just for symmetry ;-)
785 static void pop_auth_from_exosip() {
786 eXosip_clear_authentication_info();
789 int sal_call_terminate(SalOp *h){
791 if (h->auth_info) push_auth_to_exosip(h->auth_info);
793 err=eXosip_call_terminate(h->cid,h->did);
795 pop_auth_from_exosip();
797 ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
803 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
804 if (h->pending_auth){
805 push_auth_to_exosip(info);
807 eXosip_default_action(h->pending_auth);
809 ms_message("eXosip_default_action() done");
810 pop_auth_from_exosip();
812 if (h->auth_info) sal_auth_info_delete(h->auth_info); /*if already exist*/
813 h->auth_info=sal_auth_info_clone(info); /*store auth info for subsequent request*/
816 void sal_op_cancel_authentication(SalOp *h) {
818 sal_op_get_sal(h)->callbacks.register_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"));
819 } else if (h->cid >0) {
820 sal_op_get_sal(h)->callbacks.call_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"),0);
822 ms_warning("Auth failure not handled");
826 static void set_network_origin(SalOp *op, osip_message_t *req){
827 const char *received=NULL;
830 if (extract_received_rport(req,&received,&rport)!=0){
831 osip_via_t *via=NULL;
833 osip_message_get_via(req,0,&via);
834 received=osip_via_get_host(via);
835 tmp=osip_via_get_port(via);
836 if (tmp) rport=atoi(tmp);
838 snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
839 __sal_op_set_network_origin(op,origin);
842 static void set_remote_ua(SalOp* op, osip_message_t *req){
843 if (op->base.remote_ua==NULL){
844 osip_header_t *h=NULL;
845 osip_message_get_user_agent(req,0,&h);
847 op->base.remote_ua=ms_strdup(h->hvalue);
852 static void set_replaces(SalOp *op, osip_message_t *req){
853 osip_header_t *h=NULL;
856 ms_free(op->replaces);
859 osip_message_header_get_byname(req,"replaces",0,&h);
861 if (h->hvalue && h->hvalue[0]!='\0'){
862 op->replaces=ms_strdup(h->hvalue);
867 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
869 return sal_find_call(sal,ev->cid);
872 return sal_find_register(sal,ev->rid);
875 return sal_find_out_subscribe(sal,ev->sid);
878 return sal_find_in_subscribe(sal,ev->nid);
880 if (ev->response) return sal_find_other(sal,ev->response);
884 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
885 SalOp *op=sal_op_new(sal);
886 osip_from_t *from,*to;
887 osip_call_info_t *call_info;
889 sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
891 set_network_origin(op,ev->request);
892 set_remote_ua(op,ev->request);
893 set_replaces(op,ev->request);
896 op->sdp_offering=FALSE;
897 op->base.remote_media=sal_media_description_new();
898 sdp_to_media_description(sdp,op->base.remote_media);
899 sdp_message_free(sdp);
900 }else op->sdp_offering=TRUE;
902 from=osip_message_get_from(ev->request);
903 to=osip_message_get_to(ev->request);
904 osip_from_to_str(from,&tmp);
905 sal_op_set_from(op,tmp);
907 osip_from_to_str(to,&tmp);
908 sal_op_set_to(op,tmp);
911 osip_message_get_call_info(ev->request,0,&call_info);
914 osip_call_info_to_str(call_info,&tmp);
915 if( strstr(tmp,"answer-after=") != NULL)
917 op->auto_answer_asked=TRUE;
918 ms_message("The caller asked to automatically answer the call(Emergency?)\n");
927 sal_add_call(op->base.root,op);
928 sal->callbacks.call_received(op);
931 static void handle_reinvite(Sal *sal, eXosip_event_t *ev){
932 SalOp *op=find_op(sal,ev);
934 osip_message_t *msg=NULL;
937 ms_warning("Reinvite for non-existing operation !");
942 sdp=eXosip_get_sdp_info(ev->request);
943 if (op->base.remote_media){
944 sal_media_description_unref(op->base.remote_media);
945 op->base.remote_media=NULL;
948 sal_media_description_unref(op->result);
952 op->sdp_offering=FALSE;
953 op->base.remote_media=sal_media_description_new();
954 sdp_to_media_description(sdp,op->base.remote_media);
955 sdp_message_free(sdp);
956 sal->callbacks.call_updating(op);
958 op->sdp_offering=TRUE;
960 eXosip_call_build_answer(ev->tid,200,&msg);
962 set_sdp_from_desc(msg,op->base.local_media);
963 eXosip_call_send_answer(ev->tid,200,msg);
970 static void handle_ack(Sal *sal, eXosip_event_t *ev){
971 SalOp *op=find_op(sal,ev);
975 ms_warning("ack for non-existing call !");
978 sdp=eXosip_get_sdp_info(ev->ack);
980 op->base.remote_media=sal_media_description_new();
981 sdp_to_media_description(sdp,op->base.remote_media);
983 sdp_message_free(sdp);
986 if (sdp) sal->callbacks.call_updating(op);
989 sal->callbacks.call_ack(op);
993 static void update_contact_from_response(SalOp *op, osip_message_t *response){
994 const char *received;
996 if (extract_received_rport(response,&received,&rport)==0){
997 const char *contact=sal_op_get_contact(op);
999 /*no contact given yet, use from instead*/
1000 contact=sal_op_get_from(op);
1003 SalAddress *addr=sal_address_new(contact);
1005 sal_address_set_domain(addr,received);
1006 sal_address_set_port_int(addr,rport);
1007 tmp=sal_address_as_string(addr);
1008 ms_message("Contact address updated to %s for this dialog",tmp);
1009 sal_op_set_contact(op,tmp);
1010 sal_address_destroy(addr);
1016 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
1017 SalOp *op=find_op(sal,ev);
1019 if (op==NULL || op->terminated==TRUE) {
1020 ms_warning("This call has been canceled.");
1022 eXosip_call_terminate(ev->cid,ev->did);
1030 /* update contact if received and rport are set by the server
1031 note: will only be used by remote for next INVITE, if any...*/
1032 update_contact_from_response(op,ev->response);
1036 static void call_ringing(Sal *sal, eXosip_event_t *ev){
1038 SalOp *op=find_op(sal,ev);
1039 if (call_proceeding(sal, ev)==-1) return;
1041 set_remote_ua(op,ev->response);
1042 sdp=eXosip_get_sdp_info(ev->response);
1044 op->base.remote_media=sal_media_description_new();
1045 sdp_to_media_description(sdp,op->base.remote_media);
1046 sdp_message_free(sdp);
1047 if (op->base.local_media) sdp_process(op);
1049 sal->callbacks.call_ringing(op);
1052 static void call_accepted(Sal *sal, eXosip_event_t *ev){
1054 osip_message_t *msg=NULL;
1055 SalOp *op=find_op(sal,ev);
1056 const char *contact;
1058 if (op==NULL || op->terminated==TRUE) {
1059 ms_warning("This call has been already terminated.");
1061 eXosip_call_terminate(ev->cid,ev->did);
1067 set_remote_ua(op,ev->response);
1069 sdp=eXosip_get_sdp_info(ev->response);
1071 op->base.remote_media=sal_media_description_new();
1072 sdp_to_media_description(sdp,op->base.remote_media);
1073 sdp_message_free(sdp);
1074 if (op->base.local_media) sdp_process(op);
1076 eXosip_call_build_ack(ev->did,&msg);
1077 contact=sal_op_get_contact(op);
1079 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1080 osip_message_set_contact(msg,contact);
1082 if (op->sdp_answer){
1083 set_sdp(msg,op->sdp_answer);
1084 sdp_message_free(op->sdp_answer);
1085 op->sdp_answer=NULL;
1087 eXosip_call_send_ack(ev->did,msg);
1088 sal->callbacks.call_accepted(op);
1091 static void call_terminated(Sal *sal, eXosip_event_t *ev){
1093 SalOp *op=find_op(sal,ev);
1095 ms_warning("Call terminated for already closed call ?");
1099 osip_from_to_str(ev->request->from,&from);
1101 sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
1102 if (from) osip_free(from);
1103 op->terminated=TRUE;
1106 static void call_released(Sal *sal, eXosip_event_t *ev){
1107 SalOp *op=find_op(sal,ev);
1109 ms_warning("No op associated to this call_released()");
1112 if (!op->terminated){
1113 /* no response received so far */
1114 call_failure(sal,ev);
1116 sal->callbacks.call_released(op);
1119 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
1120 const char *prx_realm=NULL,*www_realm=NULL;
1121 osip_proxy_authenticate_t *prx_auth;
1122 osip_www_authenticate_t *www_auth;
1124 *username=osip_uri_get_username(resp->from->url);
1125 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
1126 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
1128 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
1130 www_realm=osip_www_authenticate_get_realm(www_auth);
1134 }else if (www_realm){
1142 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
1143 osip_authorization_t *auth=NULL;
1144 osip_proxy_authorization_t *prx_auth=NULL;
1146 *username=osip_uri_get_username(msg->from->url);
1147 osip_message_get_authorization(msg, 0, &auth);
1149 *realm=osip_authorization_get_realm(auth);
1152 osip_message_get_proxy_authorization(msg,0,&prx_auth);
1154 *realm=osip_proxy_authorization_get_realm(prx_auth);
1160 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
1161 if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
1162 if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
1166 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
1167 if (op->pending_auth){
1168 return get_auth_data(op->pending_auth,realm,username);
1173 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1175 const char *username,*realm;
1178 ms_warning("No operation associated with this authentication !");
1181 if (get_auth_data(ev,&realm,&username)==0){
1182 if (op->pending_auth!=NULL){
1183 eXosip_event_free(op->pending_auth);
1184 op->pending_auth=ev;
1186 op->pending_auth=ev;
1187 sal_add_pending_auth(sal,op);
1190 sal->callbacks.auth_requested(op,realm,username);
1196 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1198 const char *username,*realm;
1201 ms_warning("No operation associated with this authentication_ok!");
1204 if (op->pending_auth){
1205 eXosip_event_free(op->pending_auth);
1206 sal_remove_pending_auth(sal,op);
1207 op->pending_auth=NULL;
1209 if (get_auth_data(ev,&realm,&username)==0){
1210 sal->callbacks.auth_success(op,realm,username);
1214 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1217 char* computedReason=NULL;
1218 const char *reason=NULL;
1219 SalError error=SalErrorUnknown;
1220 SalReason sr=SalReasonUnknown;
1223 op=(SalOp*)find_op(sal,ev);
1226 ms_warning("Call failure reported for a closed call, ignored.");
1231 code=osip_message_get_status_code(ev->response);
1232 reason=osip_message_get_reason_phrase(ev->response);
1233 osip_header_t *h=NULL;
1234 if (!osip_message_header_get_byname( ev->response
1238 computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1239 reason = computedReason;
1247 return process_authentication(sal,ev);
1250 error=SalErrorUnknown;
1253 error=SalErrorFailure;
1254 sr=SalReasonNotFound;
1257 error=SalErrorFailure;
1261 eXosip_default_action(ev);
1265 error=SalErrorFailure;
1266 sr=SalReasonTemporarilyUnavailable;
1268 error=SalErrorFailure;
1274 error=SalErrorFailure;
1275 sr=SalReasonDoNotDisturb;
1278 error=SalErrorFailure;
1279 sr=SalReasonDeclined;
1283 error=SalErrorFailure;
1284 sr=SalReasonUnknown;
1285 }else error=SalErrorNoResponse;
1287 op->terminated=TRUE;
1288 sal->callbacks.call_failure(op,error,sr,reason,code);
1289 if (computedReason != NULL){
1290 ms_free(computedReason);
1295 /* Request remote side to send us VFU */
1296 void sal_call_send_vfu_request(SalOp *h){
1297 osip_message_t *msg=NULL;
1299 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1303 " <picture_fast_update></picture_fast_update>"
1311 eXosip_call_build_info(h->did,&msg);
1313 osip_message_set_body(msg,info_body,strlen(info_body));
1314 osip_message_set_content_type(msg,"application/media_control+xml");
1315 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(info_body));
1316 osip_message_set_content_length(msg,clen);
1317 eXosip_call_send_request(h->did,msg);
1318 ms_message("Sending VFU request !");
1323 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1324 SalOp *op=find_op(sal,ev);
1325 osip_body_t *body=NULL;
1328 ms_warning("media control xml received without operation context!");
1332 osip_message_get_body(ev->request,0,&body);
1333 if (body && body->body!=NULL &&
1334 strstr(body->body,"picture_fast_update")){
1335 osip_message_t *ans=NULL;
1336 ms_message("Receiving VFU request !");
1337 if (sal->callbacks.vfu_request){
1338 sal->callbacks.vfu_request(op);
1339 eXosip_call_build_answer(ev->tid,200,&ans);
1341 eXosip_call_send_answer(ev->tid,200,ans);
1345 /*in all other cases we must say it is not implemented.*/
1347 osip_message_t *ans=NULL;
1349 eXosip_call_build_answer(ev->tid,501,&ans);
1351 eXosip_call_send_answer(ev->tid,501,ans);
1356 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1357 SalOp *op=find_op(sal,ev);
1358 osip_body_t *body=NULL;
1361 ms_warning("media dtmf relay received without operation context!");
1365 osip_message_get_body(ev->request,0,&body);
1366 if (body && body->body!=NULL){
1367 osip_message_t *ans=NULL;
1368 const char *name=strstr(body->body,"Signal");
1369 if (name==NULL) name=strstr(body->body,"signal");
1371 ms_warning("Could not extract the dtmf name from the SIP INFO.");
1374 name+=strlen("signal");
1375 if (sscanf(name," = %1s",tmp)==1){
1376 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1377 if (sal->callbacks.dtmf_received != NULL)
1378 sal->callbacks.dtmf_received(op, tmp[0]);
1382 eXosip_call_build_answer(ev->tid,200,&ans);
1384 eXosip_call_send_answer(ev->tid,200,ans);
1389 static void fill_options_answer(osip_message_t *options){
1390 osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1391 osip_message_set_accept(options,"application/sdp");
1394 static void process_refer(Sal *sal, SalOp *op, eXosip_event_t *ev){
1395 osip_header_t *h=NULL;
1396 osip_message_t *ans=NULL;
1397 ms_message("Receiving REFER request !");
1398 osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1401 osip_from_t *from=NULL;
1403 osip_from_init(&from);
1405 if (osip_from_parse(from,h->hvalue)==0){
1407 osip_uri_header_t *uh=NULL;
1408 osip_header_t *referred_by=NULL;
1409 osip_uri_header_get_byname(&from->url->url_headers,(char*)"Replaces",&uh);
1410 if (uh!=NULL && uh->gvalue && uh->gvalue[0]!='\0'){
1411 ms_message("Found replaces in Refer-To");
1413 ms_free(op->replaces);
1415 op->replaces=ms_strdup(uh->gvalue);
1417 osip_message_header_get_byname(ev->request,"Referred-By",0,&referred_by);
1418 if (referred_by && referred_by->hvalue && referred_by->hvalue[0]!='\0'){
1419 if (op->referred_by)
1420 ms_free(op->referred_by);
1421 op->referred_by=ms_strdup(referred_by->hvalue);
1424 osip_uri_header_freelist(&from->url->url_headers);
1425 osip_from_to_str(from,&tmp);
1426 sal->callbacks.refer_received(sal,op,tmp);
1428 osip_from_free(from);
1431 eXosip_call_build_answer(ev->tid,202,&ans);
1433 eXosip_call_send_answer(ev->tid,202,ans);
1438 ms_warning("cannot do anything with the refer without destination\n");
1442 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1443 osip_message_t *ans=NULL;
1445 if (MSG_IS_INFO(ev->request)){
1446 osip_content_type_t *ct;
1447 ct=osip_message_get_content_type(ev->request);
1448 if (ct && ct->subtype){
1449 if (strcmp(ct->subtype,"media_control+xml")==0)
1450 process_media_control_xml(sal,ev);
1451 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1452 process_dtmf_relay(sal,ev);
1454 ms_message("Unhandled SIP INFO.");
1455 /*send an "Not implemented" answer*/
1457 eXosip_call_build_answer(ev->tid,501,&ans);
1459 eXosip_call_send_answer(ev->tid,501,ans);
1463 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1465 eXosip_call_build_answer(ev->tid,200,&ans);
1467 eXosip_call_send_answer(ev->tid,200,ans);
1470 }else if(MSG_IS_MESSAGE(ev->request)){
1471 /* SIP messages could be received into call */
1472 text_received(sal, ev);
1474 eXosip_call_build_answer(ev->tid,200,&ans);
1476 eXosip_call_send_answer(ev->tid,200,ans);
1478 }else if(MSG_IS_REFER(ev->request)){
1479 SalOp *op=find_op(sal,ev);
1481 ms_message("Receiving REFER request !");
1482 process_refer(sal,op,ev);
1483 }else if(MSG_IS_NOTIFY(ev->request)){
1484 osip_header_t *h=NULL;
1486 SalOp *op=find_op(sal,ev);
1488 ms_message("Receiving NOTIFY request !");
1489 osip_from_to_str(ev->request->from,&from);
1490 osip_message_header_get_byname(ev->request,"Event",0,&h);
1492 sal->callbacks.notify(op,from,h->hvalue);
1493 /*answer that we received the notify*/
1495 eXosip_call_build_answer(ev->tid,200,&ans);
1497 eXosip_call_send_answer(ev->tid,200,ans);
1500 }else if (MSG_IS_OPTIONS(ev->request)){
1502 eXosip_call_build_answer(ev->tid,200,&ans);
1504 fill_options_answer(ans);
1505 eXosip_call_send_answer(ev->tid,200,ans);
1509 }else ms_warning("call_message_new: No request ?");
1512 static void inc_update(Sal *sal, eXosip_event_t *ev){
1513 osip_message_t *msg=NULL;
1514 ms_message("Processing incoming UPDATE");
1516 eXosip_message_build_answer(ev->tid,200,&msg);
1518 eXosip_message_send_answer(ev->tid,200,msg);
1522 static bool_t comes_from_local_if(osip_message_t *msg){
1523 osip_via_t *via=NULL;
1524 osip_message_get_via(msg,0,&via);
1527 host=osip_via_get_host(via);
1528 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1529 osip_generic_param_t *param=NULL;
1530 osip_via_param_get_byname(via,"received",¶m);
1531 if (param==NULL) return TRUE;
1532 if (param->gvalue &&
1533 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1541 static void text_received(Sal *sal, eXosip_event_t *ev){
1542 osip_body_t *body=NULL;
1543 char *from=NULL,*msg;
1545 osip_message_get_body(ev->request,0,&body);
1547 ms_error("Could not get text message from SIP body");
1551 osip_from_to_str(ev->request->from,&from);
1552 sal->callbacks.text_received(sal,from,msg);
1558 static void other_request(Sal *sal, eXosip_event_t *ev){
1559 ms_message("in other_request");
1560 if (ev->request==NULL) return;
1561 if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1562 text_received(sal,ev);
1563 eXosip_message_send_answer(ev->tid,200,NULL);
1564 }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1565 osip_message_t *options=NULL;
1566 eXosip_options_build_answer(ev->tid,200,&options);
1567 fill_options_answer(options);
1568 eXosip_options_send_answer(ev->tid,200,options);
1569 }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1570 && comes_from_local_if(ev->request)) {
1571 eXosip_message_send_answer(ev->tid,200,NULL);
1572 ms_message("Receiving WAKEUP request !");
1573 sal->callbacks.internal_message(sal,"WAKEUP");
1574 }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1575 ms_message("Receiving REFER request !");
1576 if (comes_from_local_if(ev->request)) {
1577 process_refer(sal,NULL,ev);
1578 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1579 }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1584 osip_message_to_str(ev->request,&tmp,&msglen);
1586 ms_message("Unsupported request received:\n%s",tmp);
1589 /*answer with a 501 Not implemented*/
1590 eXosip_message_send_answer(ev->tid,501,NULL);
1594 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1595 osip_via_t *via=NULL;
1596 osip_message_get_via(msg,0,&via);
1598 osip_free(via->port);
1599 via->port=osip_strdup(port);
1600 osip_free(via->host);
1601 via->host=osip_strdup(ip);
1605 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1606 osip_message_t *msg;
1607 const char *received;
1609 osip_contact_t *ctt=NULL;
1613 Sal *sal=op->base.root;
1615 if (sal->double_reg==FALSE) return FALSE;
1617 if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1618 osip_message_get_contact(orig_request,0,&ctt);
1619 if (strcmp(ctt->url->host,received)==0){
1620 /*ip address matches, check ports*/
1621 const char *contact_port=ctt->url->port;
1622 if (contact_port==NULL || contact_port[0]=='\0')
1623 contact_port="5060";
1624 if (atoi(contact_port)==rport){
1625 ms_message("Register has up to date contact, doing nothing.");
1627 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1631 eXosip_register_build_register(op->rid,op->expires,&msg);
1634 ms_warning("Fail to create a contact updated register.");
1637 osip_message_get_contact(msg,0,&ctt);
1638 if (ctt->url->host!=NULL){
1639 osip_free(ctt->url->host);
1641 ctt->url->host=osip_strdup(received);
1642 if (ctt->url->port!=NULL){
1643 osip_free(ctt->url->port);
1645 snprintf(port,sizeof(port),"%i",rport);
1646 ctt->url->port=osip_strdup(port);
1647 if (op->masquerade_via) masquerade_via(msg,received,port);
1648 eXosip_register_send_register(op->rid,msg);
1650 osip_contact_to_str(ctt,&tmp);
1651 addr=sal_address_new(tmp);
1653 sal_address_clean(addr);
1654 tmp=sal_address_as_string(addr);
1655 sal_op_set_contact(op,tmp);
1656 sal_address_destroy(addr);
1657 ms_message("Resending new register with updated contact %s",tmp);
1662 static void registration_success(Sal *sal, eXosip_event_t *ev){
1663 SalOp *op=sal_find_register(sal,ev->rid);
1664 osip_header_t *h=NULL;
1667 ms_error("Receiving register response for unknown operation");
1670 osip_message_get_expires(ev->request,0,&h);
1671 if (h!=NULL && atoi(h->hvalue)!=0){
1673 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1674 sal->callbacks.register_success(op,registered);
1677 sal->callbacks.register_success(op,FALSE);
1681 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1683 const char *reason=NULL;
1684 SalOp *op=sal_find_register(sal,ev->rid);
1685 SalReason sr=SalReasonUnknown;
1686 SalError se=SalErrorUnknown;
1689 ms_error("Receiving register failure for unknown operation");
1693 status_code=osip_message_get_status_code(ev->response);
1694 reason=osip_message_get_reason_phrase(ev->response);
1696 switch(status_code){
1699 return process_authentication(sal,ev);
1701 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1702 in vias, such as ekiga.net
1703 On the opposite, freephonie.net bugs when via are masqueraded.
1705 op->masquerade_via=TRUE;
1707 /* if contact is up to date, process the failure, otherwise resend a new register with
1708 updated contact first, just in case the faillure is due to incorrect contact */
1709 if (ev->response && register_again_with_updated_contact(op,ev->request,ev->response))
1710 return TRUE; /*we are retrying with an updated contact*/
1711 if (status_code==403){
1713 sr=SalReasonForbidden;
1714 }else if (status_code==0){
1715 se=SalErrorNoResponse;
1717 sal->callbacks.register_failure(op,se,sr,reason);
1722 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1723 SalOp *op=find_op(sal,ev);
1726 ms_warning("other_request_reply(): Receiving response to unknown request.");
1730 update_contact_from_response(op,ev->response);
1731 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1732 sal->callbacks.ping_reply(op);
1736 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1737 ms_message("linphone process event get a message %d\n",ev->type);
1739 case EXOSIP_CALL_ANSWERED:
1740 ms_message("CALL_ANSWERED\n");
1741 call_accepted(sal,ev);
1742 authentication_ok(sal,ev);
1744 case EXOSIP_CALL_CLOSED:
1745 case EXOSIP_CALL_CANCELLED:
1746 ms_message("CALL_CLOSED or CANCELLED\n");
1747 call_terminated(sal,ev);
1749 case EXOSIP_CALL_TIMEOUT:
1750 case EXOSIP_CALL_NOANSWER:
1751 ms_message("CALL_TIMEOUT or NOANSWER\n");
1752 return call_failure(sal,ev);
1754 case EXOSIP_CALL_REQUESTFAILURE:
1755 case EXOSIP_CALL_GLOBALFAILURE:
1756 case EXOSIP_CALL_SERVERFAILURE:
1757 ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1758 return call_failure(sal,ev);
1760 case EXOSIP_CALL_RELEASED:
1761 ms_message("CALL_RELEASED\n");
1762 call_released(sal, ev);
1764 case EXOSIP_CALL_INVITE:
1765 ms_message("CALL_NEW\n");
1766 inc_new_call(sal,ev);
1768 case EXOSIP_CALL_REINVITE:
1769 handle_reinvite(sal,ev);
1771 case EXOSIP_CALL_ACK:
1772 ms_message("CALL_ACK");
1775 case EXOSIP_CALL_REDIRECTED:
1776 ms_message("CALL_REDIRECTED");
1777 eXosip_default_action(ev);
1779 case EXOSIP_CALL_PROCEEDING:
1780 ms_message("CALL_PROCEEDING");
1781 call_proceeding(sal,ev);
1783 case EXOSIP_CALL_RINGING:
1784 ms_message("CALL_RINGING");
1785 call_ringing(sal,ev);
1787 case EXOSIP_CALL_MESSAGE_NEW:
1788 ms_message("EXOSIP_CALL_MESSAGE_NEW");
1789 call_message_new(sal,ev);
1791 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1793 (ev->response->status_code==407 || ev->response->status_code==401)){
1794 return process_authentication(sal,ev);
1797 case EXOSIP_IN_SUBSCRIPTION_NEW:
1798 ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1799 sal_exosip_subscription_recv(sal,ev);
1801 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1802 ms_message("CALL_SUBSCRIPTION_NEW ");
1803 sal_exosip_in_subscription_closed(sal,ev);
1805 case EXOSIP_SUBSCRIPTION_UPDATE:
1806 ms_message("CALL_SUBSCRIPTION_UPDATE");
1808 case EXOSIP_SUBSCRIPTION_NOTIFY:
1809 ms_message("CALL_SUBSCRIPTION_NOTIFY");
1810 sal_exosip_notify_recv(sal,ev);
1812 case EXOSIP_SUBSCRIPTION_ANSWERED:
1813 ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1814 sal_exosip_subscription_answered(sal,ev);
1816 case EXOSIP_SUBSCRIPTION_CLOSED:
1817 ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1818 sal_exosip_subscription_closed(sal,ev);
1820 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE: /**< announce a request failure */
1821 if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1822 return process_authentication(sal,ev);
1824 case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1825 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1826 sal_exosip_subscription_closed(sal,ev);
1828 case EXOSIP_REGISTRATION_FAILURE:
1829 ms_message("REGISTRATION_FAILURE\n");
1830 return registration_failure(sal,ev);
1832 case EXOSIP_REGISTRATION_SUCCESS:
1833 authentication_ok(sal,ev);
1834 registration_success(sal,ev);
1836 case EXOSIP_MESSAGE_NEW:
1837 other_request(sal,ev);
1839 case EXOSIP_MESSAGE_PROCEEDING:
1840 case EXOSIP_MESSAGE_ANSWERED:
1841 case EXOSIP_MESSAGE_REDIRECTED:
1842 case EXOSIP_MESSAGE_SERVERFAILURE:
1843 case EXOSIP_MESSAGE_GLOBALFAILURE:
1844 other_request_reply(sal,ev);
1846 case EXOSIP_MESSAGE_REQUESTFAILURE:
1847 case EXOSIP_NOTIFICATION_REQUESTFAILURE:
1849 switch (ev->response->status_code) {
1852 return process_authentication(sal,ev);
1854 eXosip_automatic_action ();
1859 other_request_reply(sal,ev);
1862 ms_message("Unhandled exosip event ! %i",ev->type);
1868 int sal_iterate(Sal *sal){
1870 while((ev=eXosip_event_wait(0,0))!=NULL){
1871 if (process_event(sal,ev))
1872 eXosip_event_free(ev);
1875 eXosip_automatic_refresh();
1880 static void register_set_contact(osip_message_t *msg, const char *contact){
1881 osip_uri_param_t *param = NULL;
1882 osip_contact_t *ct=NULL;
1884 /*we get the line parameter choosed by exosip, and add it to our own contact*/
1885 osip_message_get_contact(msg,0,&ct);
1887 osip_uri_uparam_get_byname(ct->url, "line", ¶m);
1888 if (param && param->gvalue)
1889 line=osip_strdup(param->gvalue);
1891 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1892 osip_message_set_contact(msg,contact);
1893 osip_message_get_contact(msg,0,&ct);
1894 osip_uri_uparam_add(ct->url,osip_strdup("line"),line);
1897 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1898 osip_message_t *msg;
1899 const char *contact=sal_op_get_contact(h);
1901 sal_op_set_route(h,proxy);
1904 h->rid=eXosip_register_build_initial_register(from,proxy,NULL,expires,&msg);
1906 if (contact) register_set_contact(msg,contact);
1907 sal_add_register(h->base.root,h);
1909 ms_error("Could not build initial register.");
1915 eXosip_register_build_register(h->rid,expires,&msg);
1917 eXosip_register_send_register(h->rid,msg);
1923 int sal_register_refresh(SalOp *op, int expires){
1924 osip_message_t *msg=NULL;
1925 const char *contact=sal_op_get_contact(op);
1928 ms_error("Unexistant registration context, not possible to refresh.");
1932 eXosip_register_build_register(op->rid,expires,&msg);
1934 if (contact) register_set_contact(msg,contact);
1935 eXosip_register_send_register(op->rid,msg);
1936 }else ms_error("Could not build REGISTER refresh message.");
1942 int sal_unregister(SalOp *h){
1943 osip_message_t *msg=NULL;
1945 eXosip_register_build_register(h->rid,0,&msg);
1946 if (msg) eXosip_register_send_register(h->rid,msg);
1947 else ms_warning("Could not build unREGISTER !");
1952 SalAddress * sal_address_new(const char *uri){
1954 osip_from_init(&from);
1956 // Remove front spaces
1957 while (uri[0]==' ') {
1961 if (osip_from_parse(from,uri)!=0){
1962 osip_from_free(from);
1965 if (from->displayname!=NULL && from->displayname[0]=='"'){
1966 char *unquoted=osip_strdup_without_quote(from->displayname);
1967 osip_free(from->displayname);
1968 from->displayname=unquoted;
1970 return (SalAddress*)from;
1973 SalAddress * sal_address_clone(const SalAddress *addr){
1974 osip_from_t *ret=NULL;
1975 osip_from_clone((osip_from_t*)addr,&ret);
1976 return (SalAddress*)ret;
1979 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1981 const char *sal_address_get_scheme(const SalAddress *addr){
1982 const osip_from_t *u=(const osip_from_t*)addr;
1983 return null_if_empty(u->url->scheme);
1986 const char *sal_address_get_display_name(const SalAddress* addr){
1987 const osip_from_t *u=(const osip_from_t*)addr;
1988 return null_if_empty(u->displayname);
1991 const char *sal_address_get_username(const SalAddress *addr){
1992 const osip_from_t *u=(const osip_from_t*)addr;
1993 return null_if_empty(u->url->username);
1996 const char *sal_address_get_domain(const SalAddress *addr){
1997 const osip_from_t *u=(const osip_from_t*)addr;
1998 return null_if_empty(u->url->host);
2001 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
2002 osip_from_t *u=(osip_from_t*)addr;
2003 if (u->displayname!=NULL){
2004 osip_free(u->displayname);
2005 u->displayname=NULL;
2007 if (display_name!=NULL && display_name[0]!='\0'){
2008 u->displayname=osip_strdup(display_name);
2012 void sal_address_set_username(SalAddress *addr, const char *username){
2013 osip_from_t *uri=(osip_from_t*)addr;
2014 if (uri->url->username!=NULL){
2015 osip_free(uri->url->username);
2016 uri->url->username=NULL;
2019 uri->url->username=osip_strdup(username);
2022 void sal_address_set_domain(SalAddress *addr, const char *host){
2023 osip_from_t *uri=(osip_from_t*)addr;
2024 if (uri->url->host!=NULL){
2025 osip_free(uri->url->host);
2026 uri->url->host=NULL;
2029 uri->url->host=osip_strdup(host);
2032 void sal_address_set_port(SalAddress *addr, const char *port){
2033 osip_from_t *uri=(osip_from_t*)addr;
2034 if (uri->url->port!=NULL){
2035 osip_free(uri->url->port);
2036 uri->url->port=NULL;
2039 uri->url->port=osip_strdup(port);
2042 void sal_address_set_port_int(SalAddress *uri, int port){
2045 /*this is the default, special case to leave the port field blank*/
2046 sal_address_set_port(uri,NULL);
2049 snprintf(tmp,sizeof(tmp),"%i",port);
2050 sal_address_set_port(uri,tmp);
2053 void sal_address_clean(SalAddress *addr){
2054 osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
2055 osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
2058 char *sal_address_as_string(const SalAddress *u){
2060 osip_from_t *from=(osip_from_t *)u;
2061 char *old_displayname=NULL;
2062 /* hack to force use of quotes around the displayname*/
2063 if (from->displayname!=NULL
2064 && from->displayname[0]!='"'){
2065 old_displayname=from->displayname;
2066 from->displayname=osip_enquote(from->displayname);
2068 osip_from_to_str(from,&tmp);
2069 if (old_displayname!=NULL){
2070 ms_free(from->displayname);
2071 from->displayname=old_displayname;
2078 char *sal_address_as_string_uri_only(const SalAddress *u){
2079 char *tmp=NULL,*ret;
2080 osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
2085 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
2086 osip_uri_uparam_add (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
2089 void sal_address_destroy(SalAddress *u){
2090 osip_from_free((osip_from_t*)u);
2093 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
2094 ctx->keepalive_period=value;
2095 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
2097 unsigned int sal_get_keepalive_period(Sal *ctx) {
2098 return ctx->keepalive_period;
2101 const char * sal_address_get_port(const SalAddress *addr) {
2102 const osip_from_t *u=(const osip_from_t*)addr;
2103 return null_if_empty(u->url->port);
2106 int sal_address_get_port_int(const SalAddress *uri) {
2107 const char* port = sal_address_get_port(uri);
2115 /* sends a reinvite. Local media description may have changed by application since call establishment*/
2116 int sal_call_update(SalOp *h, const char *subject){
2118 osip_message_t *reinvite=NULL;
2121 if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != 0 || reinvite==NULL){
2126 osip_message_set_subject(reinvite,subject);
2127 osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2128 if (h->base.root->session_expires!=0){
2129 osip_message_set_header(reinvite, "Session-expires", "200");
2130 osip_message_set_supported(reinvite, "timer");
2132 if (h->base.local_media){
2133 h->sdp_offering=TRUE;
2134 set_sdp_from_desc(reinvite,h->base.local_media);
2135 }else h->sdp_offering=FALSE;
2137 err = eXosip_call_send_request(h->did, reinvite);