]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2.c
fix unauthicated notifies
[linphone] / coreapi / sal_eXosip2.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
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.
9
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.
14
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.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "sal_eXosip2.h"
24
25 #include "offeranswer.h"
26
27 static void text_received(Sal *sal, eXosip_event_t *ev);
28
29 void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
30         void *data;
31         while((data=osip_list_get(l,0))!=NULL){
32                 osip_list_remove(l,0);
33                 freefunc(data);
34         }
35 }
36
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 !");
42         }
43 }
44
45
46 static SalOp * sal_find_call(Sal *sal, int cid){
47         const MSList *elem;
48         SalOp *op;
49         for(elem=sal->calls;elem!=NULL;elem=elem->next){
50                 op=(SalOp*)elem->data;
51                 if (op->cid==cid) return op;
52         }
53         return NULL;
54 }
55
56 static void sal_add_call(Sal *sal, SalOp *op){
57         sal->calls=ms_list_append(sal->calls,op);
58 }
59
60 static void sal_remove_call(Sal *sal, SalOp *op){
61         sal->calls=ms_list_remove(sal->calls, op);
62 }
63
64 static SalOp * sal_find_register(Sal *sal, int rid){
65         const MSList *elem;
66         SalOp *op;
67         for(elem=sal->registers;elem!=NULL;elem=elem->next){
68                 op=(SalOp*)elem->data;
69                 if (op->rid==rid) return op;
70         }
71         return NULL;
72 }
73
74 static void sal_add_register(Sal *sal, SalOp *op){
75         sal->registers=ms_list_append(sal->registers,op);
76 }
77
78 static void sal_remove_register(Sal *sal, int rid){
79         MSList *elem;
80         SalOp *op;
81         for(elem=sal->registers;elem!=NULL;elem=elem->next){
82                 op=(SalOp*)elem->data;
83                 if (op->rid==rid) {
84                         sal->registers=ms_list_remove_link(sal->registers,elem);
85                         return;
86                 }
87         }
88 }
89
90 static SalOp * sal_find_other(Sal *sal, osip_message_t *response){
91         const MSList *elem;
92         SalOp *op;
93         osip_call_id_t *callid=osip_message_get_call_id(response);
94         if (callid==NULL) {
95                 ms_error("There is no call-id in this response !");
96                 return NULL;
97         }
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;
101         }
102         return NULL;
103 }
104
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);
107         if (callid==NULL) {
108                 ms_error("There is no call id in the request !");
109                 return;
110         }
111         osip_call_id_clone(callid,&op->call_id);
112         sal->other_transactions=ms_list_append(sal->other_transactions,op);
113 }
114
115 static void sal_remove_other(Sal *sal, SalOp *op){
116         sal->other_transactions=ms_list_remove(sal->other_transactions,op);
117 }
118
119
120 static void sal_add_pending_auth(Sal *sal, SalOp *op){
121         sal->pending_auths=ms_list_append(sal->pending_auths,op);
122 }
123
124
125 static void sal_remove_pending_auth(Sal *sal, SalOp *op){
126         sal->pending_auths=ms_list_remove(sal->pending_auths,op);
127 }
128
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;
133                 
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);
138                 }else{
139                         /* check if the lr parameter is set , if not add it */
140                         osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
141                         if (lr_param==NULL){
142                                 char *tmproute;
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);
146                                 osip_free(tmproute);
147                         }
148                 }
149                 osip_route_free(rt);
150         }
151 }
152
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;
157         op->result=NULL;
158         op->supports_session_timers=FALSE;
159         op->sdp_offering=TRUE;
160         op->pending_auth=NULL;
161         op->sdp_answer=NULL;
162         op->reinvite=FALSE;
163         op->call_id=NULL;
164         op->replaces=NULL;
165         op->referred_by=NULL;
166         op->masquerade_via=FALSE;
167         op->auto_answer_asked=FALSE;
168         op->auth_info=NULL;
169         return op;
170 }
171
172 bool_t sal_call_autoanswer_asked(SalOp *op)
173 {
174         return op->auto_answer_asked;
175 }
176
177 void sal_op_release(SalOp *op){
178         if (op->sdp_answer)
179                 sdp_message_free(op->sdp_answer);
180         if (op->pending_auth)
181                 eXosip_event_free(op->pending_auth);
182         if (op->rid!=-1){
183                 sal_remove_register(op->base.root,op->rid);
184         }
185         if (op->cid!=-1){
186                 ms_message("Cleaning cid %i",op->cid);
187                 sal_remove_call(op->base.root,op);
188         }
189         if (op->sid!=-1){
190                 sal_remove_out_subscribe(op->base.root,op);
191         }
192         if (op->nid!=-1){
193                 sal_remove_in_subscribe(op->base.root,op);
194                 if (op->call_id)
195                         osip_call_id_free(op->call_id);
196                 op->call_id=NULL;
197         }
198         if (op->pending_auth){
199                 sal_remove_pending_auth(op->base.root,op);
200         }
201         if (op->result)
202                 sal_media_description_unref(op->result);
203         if (op->call_id){
204                 sal_remove_other(op->base.root,op);
205                 osip_call_id_free(op->call_id);
206         }
207         if (op->replaces){
208                 ms_free(op->replaces);
209         }
210         if (op->referred_by){
211                 ms_free(op->referred_by);
212         }
213         if (op->auth_info) {
214                 sal_auth_info_delete(op->auth_info);
215         }
216         __sal_op_free(op);
217 }
218
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;
221         switch(level){
222                 case OSIP_INFO1:
223                 case OSIP_INFO2:
224                 case OSIP_INFO3:
225                 case OSIP_INFO4:
226                         ortp_level=ORTP_MESSAGE;
227                         break;
228                 case OSIP_WARNING:
229                         ortp_level=ORTP_WARNING;
230                         break;
231                 case OSIP_ERROR:
232                 case OSIP_BUG:
233                         ortp_level=ORTP_ERROR;
234                         break;
235                 case OSIP_FATAL:
236                         ortp_level=ORTP_FATAL;
237                         break;
238                 case END_TRACE_LEVEL:
239                         break;
240         }
241         if (ortp_log_level_enabled(level)){
242                 int len=strlen(chfr);
243                 char *chfrdup=ortp_strdup(chfr);
244                 /*need to remove endline*/
245                 if (len>1){
246                         if (chfrdup[len-1]=='\n')
247                                 chfrdup[len-1]='\0';
248                         if (chfrdup[len-2]=='\r')
249                                 chfrdup[len-2]='\0';
250                 }
251                 ortp_logv(ortp_level,chfrdup,ap);
252                 ortp_free(chfrdup);
253         }
254 }
255
256
257 Sal * sal_init(){
258         static bool_t firsttime=TRUE;
259         Sal *sal;
260         if (firsttime){
261                 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
262                 firsttime=FALSE;
263         }
264         eXosip_init();
265         sal=ms_new0(Sal,1);
266         sal->keepalive_period=30;
267         sal->double_reg=TRUE;
268         return sal;
269 }
270
271 void sal_uninit(Sal* sal){
272         eXosip_quit();
273         ms_free(sal);
274 }
275
276 void sal_set_user_pointer(Sal *sal, void *user_data){
277         sal->up=user_data;
278 }
279
280 void *sal_get_user_pointer(const Sal *sal){
281         return sal->up;
282 }
283
284 static void unimplemented_stub(){
285         ms_warning("Unimplemented SAL callback");
286 }
287
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;
326 }
327
328 int sal_unlisten_ports(Sal *ctx){
329         if (ctx->running){
330                 eXosip_quit();
331                 eXosip_init();
332         }
333         return 0;
334 }
335
336 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
337         int err;
338         bool_t ipv6;
339         int proto=IPPROTO_UDP;
340         
341         switch (tr) {
342         case SalTransportDatagram:
343                 proto=IPPROTO_UDP;
344                 break;
345         case SalTransportStream:
346                 proto= IPPROTO_TCP;
347                 break;
348         default:
349                 ms_warning("unexpected proto, using datagram");
350         }
351
352         err=0;
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);
358
359         if (is_secure){
360                 ms_fatal("SIP over TLS or DTLS is not supported yet.");
361                 return -1;
362         }
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));
366 #endif
367         eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &ctx->keepalive_period);
368         ctx->running=TRUE;
369         return err;
370 }
371
372 ortp_socket_t sal_get_socket(Sal *ctx){
373 #ifdef HAVE_EXOSIP_GET_SOCKET
374         return eXosip_get_socket(IPPROTO_UDP);
375 #else
376         ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
377         return -1;
378 #endif
379 }
380
381 void sal_set_user_agent(Sal *ctx, const char *user_agent){
382         eXosip_set_user_agent(user_agent);
383 }
384
385 void sal_use_session_timers(Sal *ctx, int expires){
386         ctx->session_expires=expires;
387 }
388
389 void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
390         ctx->one_matching_codec=one_matching_codec;
391 }
392
393 MSList *sal_get_pending_auths(Sal *sal){
394         return ms_list_copy(sal->pending_auths);
395 }
396
397 void sal_use_double_registrations(Sal *ctx, bool_t enabled){
398         ctx->double_reg=enabled;
399 }
400
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;
405
406         *rportval=5060;
407         *received=NULL;
408         osip_message_get_via(msg,0,&via);
409         if (!via) return -1;
410
411         /* it is useless to do that with tcp since client socket might have a different port
412                 than the server socket.
413         */
414         if (strcasecmp(via->protocol,"tcp")==0) return -1;
415         
416         if (via->port && via->port[0]!='\0')
417                 *rportval=atoi(via->port);
418         
419         osip_via_param_get_byname(via,"rport",&param);
420         if (param) {
421                 rport=param->gvalue;
422                 if (rport && rport[0]!='\0') *rportval=atoi(rport);
423                 else *rportval=5060;
424                 *received=via->host;
425         }
426         param=NULL;
427         osip_via_param_get_byname(via,"received",&param);
428         if (param) *received=param->gvalue;
429
430         if (rport==NULL && *received==NULL) return -1;
431         return 0;
432 }
433
434 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
435         int sdplen;
436         char clen[10];
437         char *sdp=NULL;
438         sdp_message_to_str(msg,&sdp);
439         sdplen=strlen(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);
444         osip_free(sdp);
445 }
446
447 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
448         sdp_message_t *msg=media_description_to_sdp(desc);
449         if (msg==NULL) {
450                 ms_error("Fail to print sdp message !");
451                 return;
452         }
453         set_sdp(sip,msg);
454         sdp_message_free(msg);
455 }
456
457 static void sdp_process(SalOp *h){
458         ms_message("Doing SDP offer/answer process");
459         if (h->result){
460                 sal_media_description_unref(h->result);
461         }
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);
465         }else{
466                 int i;
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;
479                         }
480                 }
481         }
482         
483 }
484
485 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
486         if (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;
491         return 0;
492 }
493
494 int sal_call(SalOp *h, const char *from, const char *to){
495         int err;
496         osip_message_t *invite=NULL;
497         sal_op_set_from(h,from);
498         sal_op_set_to(h,to);
499         sal_exosip_fix_route(h);
500         err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
501         if (err!=0){
502                 ms_error("Could not create call.");
503                 return -1;
504         }
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);
509         }
510         if (h->base.root->session_expires!=0){
511                 osip_message_set_header(invite, "Session-expires", "200");
512                 osip_message_set_supported(invite, "timer");
513         }
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;
518         if (h->replaces){
519                 osip_message_set_header(invite,"Replaces",h->replaces);
520                 if (h->referred_by)
521                         osip_message_set_header(invite,"Referred-By",h->referred_by);
522         }
523         
524         eXosip_lock();
525         err=eXosip_call_send_initial_invite(invite);
526         eXosip_unlock();
527         h->cid=err;
528         if (err<0){
529                 ms_error("Fail to send invite !");
530                 return -1;
531         }else{
532                 sal_add_call(h->base.root,h);
533         }
534         return 0;
535 }
536
537 int sal_call_notify_ringing(SalOp *h, bool_t early_media){
538         osip_message_t *msg;
539         int err;
540         
541         /*if early media send also 180 and 183 */
542         if (early_media && h->sdp_answer){
543                 msg=NULL;
544                 eXosip_lock();
545                 err=eXosip_call_build_answer(h->tid,180,&msg);
546                 if (msg){
547                         set_sdp(msg,h->sdp_answer);
548                         eXosip_call_send_answer(h->tid,180,msg);
549                 }
550                 msg=NULL;
551                 err=eXosip_call_build_answer(h->tid,183,&msg);
552                 if (msg){
553                         set_sdp(msg,h->sdp_answer);
554                         eXosip_call_send_answer(h->tid,183,msg);
555                 }
556                 eXosip_unlock();
557         }else{
558                 eXosip_lock();
559                 eXosip_call_send_answer(h->tid,180,NULL);
560                 eXosip_unlock();
561         }
562         return 0;
563 }
564
565 int sal_call_accept(SalOp * h){
566         osip_message_t *msg;
567         const char *contact=sal_op_get_contact(h);
568         /* sends a 200 OK */
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);
572                 return -1;
573         }
574         if (h->base.root->session_expires!=0){
575                 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
576         }
577
578         if (contact) {
579                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
580                 osip_message_set_contact(msg,contact);
581         }
582         
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);
587                 }else{
588                         if (h->sdp_answer){
589                                 set_sdp(msg,h->sdp_answer);
590                                 sdp_message_free(h->sdp_answer);
591                                 h->sdp_answer=NULL;
592                         }
593                 }
594         }else{
595                 ms_error("You are accepting a call but not defined any media capabilities !");
596         }
597         eXosip_call_send_answer(h->tid,200,msg);
598         return 0;
599 }
600
601 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
602         if (reason==SalReasonBusy){
603                 eXosip_lock();
604                 eXosip_call_send_answer(h->tid,486,NULL);
605                 eXosip_unlock();
606         }
607         else if (reason==SalReasonTemporarilyUnavailable){
608                 eXosip_lock();
609                 eXosip_call_send_answer(h->tid,480,NULL);
610                 eXosip_unlock();
611         }else if (reason==SalReasonDoNotDisturb){
612                 eXosip_lock();
613                 eXosip_call_send_answer(h->tid,600,NULL);
614                 eXosip_unlock();
615         }else if (reason==SalReasonMedia){
616                 eXosip_lock();
617                 eXosip_call_send_answer(h->tid,415,NULL);
618                 eXosip_unlock();
619         }else if (redirect!=NULL && reason==SalReasonRedirect){
620                 osip_message_t *msg;
621                 int code;
622                 if (strstr(redirect,"sip:")!=0) code=302;
623                 else code=380;
624                 eXosip_lock();
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);
628                 eXosip_unlock();
629         }else sal_call_terminate(h);
630         return 0;
631 }
632
633 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
634         if (h->base.local_media && h->base.remote_media && !h->result){
635                 sdp_process(h);
636         }
637         return h->result;
638 }
639
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);
645         return 0;
646 }
647
648 int sal_ping(SalOp *op, const char *from, const char *to){
649         osip_message_t *options=NULL;
650         
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));
656         if (options){
657                 if (op->base.root->session_expires!=0){
658                         osip_message_set_header(options, "Session-expires", "200");
659                         osip_message_set_supported(options, "timer");
660                 }
661                 sal_add_other(sal_op_get_sal(op),op,options);
662                 return eXosip_options_send_request(options);
663         }
664         return -1;
665 }
666
667 int sal_call_accept_refer(SalOp *op){
668         osip_message_t *msg=NULL;
669         int err=0;
670         eXosip_lock();
671         err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
672         if(msg != NULL)
673         {
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);
678         }
679         else
680         {
681                 ms_error("could not get a notify built\n");
682         }
683         eXosip_unlock();
684         return err;
685 }
686
687 int sal_call_refer(SalOp *h, const char *refer_to){
688         osip_message_t *msg=NULL;
689         int err=0;
690         eXosip_lock();
691         eXosip_call_build_refer(h->did,refer_to, &msg);
692         if (msg) err=eXosip_call_send_request(h->did, msg);
693         else err=-1;
694         eXosip_unlock();
695         return err;
696 }
697
698 int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
699         osip_message_t *msg=NULL;
700         char referto[256]={0};
701         int err=0;
702         eXosip_lock();
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);
705                 eXosip_unlock();
706                 return -1;
707         }
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);
711         else err=-1;
712         eXosip_unlock();
713         return err;
714 }
715
716 SalOp *sal_call_get_replaces(SalOp *h){
717         if (h->replaces!=NULL){
718                 int cid;
719                 eXosip_lock();
720                 cid=eXosip_call_find_by_replaces(h->replaces);
721                 eXosip_unlock();
722                 if (cid>0){
723                         SalOp *ret=sal_find_call(h->base.root,cid);
724                         return ret;
725                 }
726         }
727         return NULL;
728 }
729
730 int sal_call_send_dtmf(SalOp *h, char dtmf){
731         osip_message_t *msg=NULL;
732         char dtmf_body[128];
733         char clen[10];
734
735         eXosip_lock();
736         eXosip_call_build_info(h->did,&msg);
737         if (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);
744         }
745         eXosip_unlock();
746         return 0;
747 }
748
749 static void push_auth_to_exosip(const SalAuthInfo *info){
750         const char *userid;
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);
756 }
757 /*
758  * Just for symmetry ;-)
759  */
760 static void pop_auth_from_exosip() {
761         eXosip_clear_authentication_info();
762 }
763
764 int sal_call_terminate(SalOp *h){
765         int err;
766         if (h->auth_info) push_auth_to_exosip(h->auth_info);
767         eXosip_lock();
768         err=eXosip_call_terminate(h->cid,h->did);
769         eXosip_unlock();
770         pop_auth_from_exosip();
771         if (err!=0){
772                 ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
773         }
774         return 0;
775 }
776
777 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
778         if (h->pending_auth){
779                 push_auth_to_exosip(info);
780                 eXosip_lock();
781                 eXosip_default_action(h->pending_auth);
782                 eXosip_unlock();
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*/
790         }
791 }
792
793 static void set_network_origin(SalOp *op, osip_message_t *req){
794         const char *received=NULL;
795         int rport=5060;
796         char origin[64];
797         if (extract_received_rport(req,&received,&rport)!=0){
798                 osip_via_t *via=NULL;
799                 char *tmp;
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);
804         }
805         snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
806         __sal_op_set_network_origin(op,origin);
807 }
808
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);
813                 if (h){
814                         op->base.remote_ua=ms_strdup(h->hvalue);
815                 }
816         }
817 }
818
819 static void set_replaces(SalOp *op, osip_message_t *req){
820         osip_header_t *h=NULL;
821
822         if (op->replaces){
823                 ms_free(op->replaces);
824                 op->replaces=NULL;
825         }
826         osip_message_header_get_byname(req,"replaces",0,&h);
827         if (h){
828                 if (h->hvalue && h->hvalue[0]!='\0'){
829                         op->replaces=ms_strdup(h->hvalue);
830                 }
831         }
832 }
833
834 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
835         if (ev->cid>0){
836                 return sal_find_call(sal,ev->cid);
837         }
838         if (ev->rid>0){
839                 return sal_find_register(sal,ev->rid);
840         }
841         if (ev->sid>0){
842                 return sal_find_out_subscribe(sal,ev->sid);
843         }
844         if (ev->nid>0){
845                 return sal_find_in_subscribe(sal,ev->nid);
846         }
847         if (ev->response) return sal_find_other(sal,ev->response);
848         return NULL;
849 }
850
851 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
852         SalOp *op=sal_op_new(sal);
853         osip_from_t *from,*to;
854         osip_call_info_t *call_info;
855         char *tmp;
856         sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
857
858         set_network_origin(op,ev->request);
859         set_remote_ua(op,ev->request);
860         set_replaces(op,ev->request);
861         
862         if (sdp){
863                 op->sdp_offering=FALSE;
864                 op->base.remote_media=sal_media_description_new();
865                 sdp_to_media_description(sdp,op->base.remote_media);
866                 sdp_message_free(sdp);
867         }else op->sdp_offering=TRUE;
868
869         from=osip_message_get_from(ev->request);
870         to=osip_message_get_to(ev->request);
871         osip_from_to_str(from,&tmp);
872         sal_op_set_from(op,tmp);
873         osip_free(tmp);
874         osip_from_to_str(to,&tmp);
875         sal_op_set_to(op,tmp);
876         osip_free(tmp);
877
878         osip_message_get_call_info(ev->request,0,&call_info);
879         if(call_info)
880         {
881                 osip_call_info_to_str(call_info,&tmp);
882                 if( strstr(tmp,"answer-after=") != NULL)
883                 {
884                         op->auto_answer_asked=TRUE;
885                         ms_message("The caller asked to automatically answer the call(Emergency?)\n");
886                 }
887                 osip_free(tmp);
888         }
889
890         op->tid=ev->tid;
891         op->cid=ev->cid;
892         op->did=ev->did;
893         
894         sal_add_call(op->base.root,op);
895         sal->callbacks.call_received(op);
896 }
897
898 static void handle_reinvite(Sal *sal,  eXosip_event_t *ev){
899         SalOp *op=find_op(sal,ev);
900         sdp_message_t *sdp;
901         osip_message_t *msg=NULL;
902
903         if (op==NULL) {
904                 ms_warning("Reinvite for non-existing operation !");
905                 return;
906         }
907         op->reinvite=TRUE;
908         op->tid=ev->tid;
909         sdp=eXosip_get_sdp_info(ev->request);
910         if (op->base.remote_media){
911                 sal_media_description_unref(op->base.remote_media);
912                 op->base.remote_media=NULL;
913         }
914         if (op->result){
915                 sal_media_description_unref(op->result);
916                 op->result=NULL;
917         }
918         if (sdp){
919                 op->sdp_offering=FALSE;
920                 op->base.remote_media=sal_media_description_new();
921                 sdp_to_media_description(sdp,op->base.remote_media);
922                 sdp_message_free(sdp);
923                 sal->callbacks.call_updating(op);
924         }else {
925                 op->sdp_offering=TRUE;
926                 eXosip_lock();
927                 eXosip_call_build_answer(ev->tid,200,&msg);
928                 if (msg!=NULL){
929                         set_sdp_from_desc(msg,op->base.local_media);
930                         eXosip_call_send_answer(ev->tid,200,msg);
931                 }
932                 eXosip_unlock();
933         }
934         
935 }
936
937 static void handle_ack(Sal *sal,  eXosip_event_t *ev){
938         SalOp *op=find_op(sal,ev);
939         sdp_message_t *sdp;
940
941         if (op==NULL) {
942                 ms_warning("ack for non-existing call !");
943                 return;
944         }
945         sdp=eXosip_get_sdp_info(ev->ack);
946         if (sdp){
947                 op->base.remote_media=sal_media_description_new();
948                 sdp_to_media_description(sdp,op->base.remote_media);
949                 sdp_process(op);
950                 sdp_message_free(sdp);
951         }
952         if (op->reinvite){
953                 if (sdp) sal->callbacks.call_updating(op);
954                 op->reinvite=FALSE;
955         }else{
956                 sal->callbacks.call_ack(op);
957         }
958 }
959
960 static void update_contact_from_response(SalOp *op, osip_message_t *response){
961         const char *received;
962         int rport;
963         if (extract_received_rport(response,&received,&rport)==0){
964                 const char *contact=sal_op_get_contact(op);
965                 if (!contact){
966                         /*no contact given yet, use from instead*/
967                         contact=sal_op_get_from(op);
968                 }
969                 if (contact){
970                         SalAddress *addr=sal_address_new(contact);
971                         char *tmp;
972                         sal_address_set_domain(addr,received);
973                         sal_address_set_port_int(addr,rport);
974                         tmp=sal_address_as_string(addr);
975                         ms_message("Contact address updated to %s for this dialog",tmp);
976                         sal_op_set_contact(op,tmp);
977                         sal_address_destroy(addr);
978                         ms_free(tmp);
979                 }
980         }
981 }
982
983 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
984         SalOp *op=find_op(sal,ev);
985         
986         if (op==NULL) {
987                 ms_warning("This call has been canceled.");
988                 eXosip_lock();
989                 eXosip_call_terminate(ev->cid,ev->did);
990                 eXosip_unlock();
991                 return -1;
992         }
993         if (ev->did>0)
994                 op->did=ev->did;
995         op->tid=ev->tid;
996         
997         /* update contact if received and rport are set by the server
998          note: will only be used by remote for next INVITE, if any...*/
999         update_contact_from_response(op,ev->response);
1000         return 0;
1001 }
1002
1003 static void call_ringing(Sal *sal, eXosip_event_t *ev){
1004         sdp_message_t *sdp;
1005         SalOp *op=find_op(sal,ev);
1006         if (call_proceeding(sal, ev)==-1) return;
1007
1008         set_remote_ua(op,ev->response);
1009         sdp=eXosip_get_sdp_info(ev->response);
1010         if (sdp){
1011                 op->base.remote_media=sal_media_description_new();
1012                 sdp_to_media_description(sdp,op->base.remote_media);
1013                 sdp_message_free(sdp);
1014                 if (op->base.local_media) sdp_process(op);
1015         }
1016         sal->callbacks.call_ringing(op);
1017 }
1018
1019 static void call_accepted(Sal *sal, eXosip_event_t *ev){
1020         sdp_message_t *sdp;
1021         osip_message_t *msg=NULL;
1022         SalOp *op=find_op(sal,ev);
1023         const char *contact;
1024         
1025         if (op==NULL){
1026                 ms_error("A closed call is accepted ?");
1027                 return;
1028         }
1029
1030         op->did=ev->did;
1031         set_remote_ua(op,ev->response);
1032
1033         sdp=eXosip_get_sdp_info(ev->response);
1034         if (sdp){
1035                 op->base.remote_media=sal_media_description_new();
1036                 sdp_to_media_description(sdp,op->base.remote_media);
1037                 sdp_message_free(sdp);
1038                 if (op->base.local_media) sdp_process(op);
1039         }
1040         eXosip_call_build_ack(ev->did,&msg);
1041         contact=sal_op_get_contact(op);
1042         if (contact) {
1043                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1044                 osip_message_set_contact(msg,contact);
1045         }
1046         if (op->sdp_answer){
1047                 set_sdp(msg,op->sdp_answer);
1048                 sdp_message_free(op->sdp_answer);
1049                 op->sdp_answer=NULL;
1050         }
1051         eXosip_call_send_ack(ev->did,msg);
1052         sal->callbacks.call_accepted(op);
1053 }
1054
1055 static void call_terminated(Sal *sal, eXosip_event_t *ev){
1056         char *from=NULL;
1057         SalOp *op=find_op(sal,ev);
1058         if (op==NULL){
1059                 ms_warning("Call terminated for already closed call ?");
1060                 return;
1061         }
1062         if (ev->request){
1063                 osip_from_to_str(ev->request->from,&from);
1064         }
1065         sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
1066         if (from) osip_free(from);
1067 }
1068
1069 static void call_released(Sal *sal, eXosip_event_t *ev){
1070         SalOp *op=find_op(sal,ev);
1071         if (op==NULL){
1072                 ms_warning("No op associated to this call_released()");
1073                 return;
1074         }
1075         if (op->did==-1) {
1076                 sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL, 487);
1077         }
1078         sal->callbacks.call_released(op);
1079 }
1080
1081 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
1082         const char *prx_realm=NULL,*www_realm=NULL;
1083         osip_proxy_authenticate_t *prx_auth;
1084         osip_www_authenticate_t *www_auth;
1085         
1086         *username=osip_uri_get_username(resp->from->url);
1087         prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
1088         www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
1089         if (prx_auth!=NULL)
1090                 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
1091         if (www_auth!=NULL)
1092                 www_realm=osip_www_authenticate_get_realm(www_auth);
1093
1094         if (prx_realm){
1095                 *realm=prx_realm;
1096         }else if (www_realm){
1097                 *realm=www_realm;
1098         }else{
1099                 return -1;
1100         }
1101         return 0;
1102 }
1103
1104 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
1105         osip_authorization_t *auth=NULL;
1106         osip_proxy_authorization_t *prx_auth=NULL;
1107         
1108         *username=osip_uri_get_username(msg->from->url);
1109         osip_message_get_authorization(msg, 0, &auth);
1110         if (auth){
1111                 *realm=osip_authorization_get_realm(auth);
1112                 return 0;
1113         }
1114         osip_message_get_proxy_authorization(msg,0,&prx_auth);
1115         if (prx_auth){
1116                 *realm=osip_proxy_authorization_get_realm(prx_auth);
1117                 return 0;
1118         }
1119         return -1;
1120 }
1121
1122 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
1123         if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
1124         if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
1125         return -1;
1126 }
1127
1128 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
1129         if (op->pending_auth){
1130                 return get_auth_data(op->pending_auth,realm,username);
1131         }
1132         return -1;
1133 }
1134
1135 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1136         SalOp *op;
1137         const char *username,*realm;
1138         op=find_op(sal,ev);
1139         if (op==NULL){
1140                 ms_warning("No operation associated with this authentication !");
1141                 return TRUE;
1142         }
1143         if (get_auth_data(ev,&realm,&username)==0){
1144                 if (op->pending_auth!=NULL)
1145                         eXosip_event_free(op->pending_auth);
1146                 op->pending_auth=ev;
1147                 sal_add_pending_auth (sal,op);
1148                 sal->callbacks.auth_requested(op,realm,username);
1149                 return FALSE;
1150         }
1151         return TRUE;
1152 }
1153
1154 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1155         SalOp *op;
1156         const char *username,*realm;
1157         op=find_op(sal,ev);
1158         if (op==NULL){
1159                 ms_warning("No operation associated with this authentication_ok!");
1160                 return ;
1161         }
1162         if (get_auth_data(ev,&realm,&username)==0){
1163                 sal->callbacks.auth_success(op,realm,username);
1164         }
1165 }
1166
1167 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1168         SalOp *op;
1169         int code=0;
1170         char* computedReason=NULL;
1171         const char *reason=NULL;
1172         SalError error=SalErrorUnknown;
1173         SalReason sr=SalReasonUnknown;
1174         
1175
1176         op=(SalOp*)find_op(sal,ev);
1177
1178         if (op==NULL) {
1179                 ms_warning("Call failure reported for a closed call, ignored.");
1180                 return TRUE;
1181         }
1182
1183         if (ev->response){
1184                 code=osip_message_get_status_code(ev->response);
1185                 reason=osip_message_get_reason_phrase(ev->response);
1186                 osip_header_t *h=NULL;
1187                 if (!osip_message_header_get_byname(    ev->response
1188                                                                                         ,"Reason"
1189                                                                                         ,0
1190                                                                                         ,&h)) {
1191                         computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1192                         reason = computedReason;
1193
1194                 }
1195         }
1196         switch(code)
1197         {
1198                 case 401:
1199                 case 407:
1200                         return process_authentication(sal,ev);
1201                         break;
1202                 case 400:
1203                         error=SalErrorUnknown;
1204                 break;
1205                 case 404:
1206                         error=SalErrorFailure;
1207                         sr=SalReasonNotFound;
1208                 break;
1209                 case 415:
1210                         error=SalErrorFailure;
1211                         sr=SalReasonMedia;
1212                 break;
1213                 case 422:
1214                         eXosip_default_action(ev);
1215                         return TRUE;
1216                 break;
1217                 case 480:
1218                         error=SalErrorFailure;
1219                         sr=SalReasonTemporarilyUnavailable;
1220                 case 486:
1221                         error=SalErrorFailure;
1222                         sr=SalReasonBusy;
1223                 break;
1224                 case 487:
1225                 break;
1226                 case 600:
1227                         error=SalErrorFailure;
1228                         sr=SalReasonDoNotDisturb;
1229                 break;
1230                 case 603:
1231                         error=SalErrorFailure;
1232                         sr=SalReasonDeclined;
1233                 break;
1234                 default:
1235                         if (code>0){
1236                                 error=SalErrorFailure;
1237                                 sr=SalReasonUnknown;
1238                         }else error=SalErrorNoResponse;
1239         }
1240         sal->callbacks.call_failure(op,error,sr,reason,code);
1241         if (computedReason != NULL){
1242                 ms_free(computedReason);
1243         }
1244         return TRUE;
1245 }
1246
1247 /* Request remote side to send us VFU */
1248 void sal_call_send_vfu_request(SalOp *h){
1249         osip_message_t *msg=NULL;
1250         char info_body[] =
1251                         "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1252                          "<media_control>"
1253                          "  <vc_primitive>"
1254                          "    <to_encoder>"
1255                          "      <picture_fast_update></picture_fast_update>"
1256                          "    </to_encoder>"
1257                          "  </vc_primitive>"
1258                          "</media_control>";
1259
1260         char clen[10];
1261
1262         eXosip_lock();
1263         eXosip_call_build_info(h->did,&msg);
1264         if (msg){
1265                 osip_message_set_body(msg,info_body,strlen(info_body));
1266                 osip_message_set_content_type(msg,"application/media_control+xml");
1267                 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(info_body));
1268                 osip_message_set_content_length(msg,clen);
1269                 eXosip_call_send_request(h->did,msg);
1270                 ms_message("Sending VFU request !");
1271         }
1272         eXosip_unlock();
1273 }
1274
1275 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1276         SalOp *op=find_op(sal,ev);
1277         osip_body_t *body=NULL;
1278
1279         if (op==NULL){
1280                 ms_warning("media control xml received without operation context!");
1281                 return ;
1282         }
1283         
1284         osip_message_get_body(ev->request,0,&body);
1285         if (body && body->body!=NULL &&
1286                 strstr(body->body,"picture_fast_update")){
1287                 osip_message_t *ans=NULL;
1288                 ms_message("Receiving VFU request !");
1289                 if (sal->callbacks.vfu_request){
1290                         sal->callbacks.vfu_request(op);
1291                         eXosip_call_build_answer(ev->tid,200,&ans);
1292                         if (ans)
1293                                 eXosip_call_send_answer(ev->tid,200,ans);
1294                         return;
1295                 }
1296         }
1297         /*in all other cases we must say it is not implemented.*/
1298         {
1299                 osip_message_t *ans=NULL;
1300                 eXosip_lock();
1301                 eXosip_call_build_answer(ev->tid,501,&ans);
1302                 if (ans)
1303                         eXosip_call_send_answer(ev->tid,501,ans);
1304                 eXosip_unlock();
1305         }
1306 }
1307
1308 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1309         SalOp *op=find_op(sal,ev);
1310         osip_body_t *body=NULL;
1311
1312         if (op==NULL){
1313                 ms_warning("media dtmf relay received without operation context!");
1314                 return ;
1315         }
1316         
1317         osip_message_get_body(ev->request,0,&body);
1318         if (body && body->body!=NULL){
1319                 osip_message_t *ans=NULL;
1320                 const char *name=strstr(body->body,"Signal");
1321                 if (name==NULL) name=strstr(body->body,"signal");
1322                 if (name==NULL) {
1323                         ms_warning("Could not extract the dtmf name from the SIP INFO.");
1324                 }else{
1325                         char tmp[2];
1326                         name+=strlen("signal");
1327                         if (sscanf(name," = %1s",tmp)==1){
1328                                 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1329                                 if (sal->callbacks.dtmf_received != NULL)
1330                                         sal->callbacks.dtmf_received(op, tmp[0]);
1331                         }
1332                 }
1333                 eXosip_call_build_answer(ev->tid,200,&ans);
1334                 if (ans)
1335                         eXosip_call_send_answer(ev->tid,200,ans);
1336         }
1337 }
1338
1339 static void fill_options_answer(osip_message_t *options){
1340         osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1341         osip_message_set_accept(options,"application/sdp");
1342 }
1343
1344 static void process_refer(Sal *sal, SalOp *op, eXosip_event_t *ev){
1345         osip_header_t *h=NULL;
1346         osip_message_t *ans=NULL;
1347         ms_message("Receiving REFER request !");
1348         osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1349
1350         if (h){
1351                 osip_from_t *from=NULL;
1352                 char *tmp;
1353                 osip_from_init(&from);
1354         
1355                 if (osip_from_parse(from,h->hvalue)==0){
1356                         if (op ){
1357                                 osip_uri_header_t *uh=NULL;
1358                                 osip_header_t *referred_by=NULL;
1359                                 osip_uri_header_get_byname(&from->url->url_headers,(char*)"Replaces",&uh);
1360                                 if (uh!=NULL && uh->gvalue && uh->gvalue[0]!='\0'){
1361                                         ms_message("Found replaces in Refer-To");
1362                                         if (op->replaces){
1363                                                 ms_free(op->replaces);
1364                                         }
1365                                         op->replaces=ms_strdup(uh->gvalue);
1366                                 }
1367                                 osip_message_header_get_byname(ev->request,"Referred-By",0,&referred_by);
1368                                 if (referred_by && referred_by->hvalue && referred_by->hvalue[0]!='\0'){
1369                                         if (op->referred_by)
1370                                                 ms_free(op->referred_by);
1371                                         op->referred_by=ms_strdup(referred_by->hvalue);
1372                                 }
1373                         }
1374                         osip_uri_header_freelist(&from->url->url_headers);
1375                         osip_from_to_str(from,&tmp);
1376                         sal->callbacks.refer_received(sal,op,tmp);
1377                         osip_free(tmp);
1378                         osip_from_free(from);
1379                 }
1380                 eXosip_lock();
1381                 eXosip_call_build_answer(ev->tid,202,&ans);
1382                 if (ans)
1383                         eXosip_call_send_answer(ev->tid,202,ans);
1384                 eXosip_unlock();
1385         }
1386         else
1387         {
1388                 ms_warning("cannot do anything with the refer without destination\n");
1389         }
1390 }
1391
1392 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1393         osip_message_t *ans=NULL;
1394         if (ev->request){
1395                 if (MSG_IS_INFO(ev->request)){
1396                         osip_content_type_t *ct;
1397                         ct=osip_message_get_content_type(ev->request);
1398                         if (ct && ct->subtype){
1399                                 if (strcmp(ct->subtype,"media_control+xml")==0)
1400                                         process_media_control_xml(sal,ev);
1401                                 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1402                                         process_dtmf_relay(sal,ev);
1403                                 else {
1404                                         ms_message("Unhandled SIP INFO.");
1405                                         /*send an "Not implemented" answer*/
1406                                         eXosip_lock();
1407                                         eXosip_call_build_answer(ev->tid,501,&ans);
1408                                         if (ans)
1409                                                 eXosip_call_send_answer(ev->tid,501,ans);
1410                                         eXosip_unlock();
1411                                 }
1412                         }else{
1413                                 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1414                                 eXosip_lock();
1415                                 eXosip_call_build_answer(ev->tid,200,&ans);
1416                                 if (ans)
1417                                         eXosip_call_send_answer(ev->tid,200,ans);
1418                                 eXosip_unlock();
1419                         }
1420                 }else if(MSG_IS_MESSAGE(ev->request)){
1421                         /* SIP messages could be received into call */
1422                         text_received(sal, ev);
1423                         eXosip_lock();
1424                         eXosip_call_build_answer(ev->tid,200,&ans);
1425                         if (ans)
1426                                 eXosip_call_send_answer(ev->tid,200,ans);
1427                         eXosip_unlock();
1428                 }else if(MSG_IS_REFER(ev->request)){
1429                         SalOp *op=find_op(sal,ev);
1430                         
1431                         ms_message("Receiving REFER request !");
1432                         process_refer(sal,op,ev);
1433                 }else if(MSG_IS_NOTIFY(ev->request)){
1434                         osip_header_t *h=NULL;
1435                         char *from=NULL;
1436                         SalOp *op=find_op(sal,ev);
1437
1438                         ms_message("Receiving NOTIFY request !");
1439                         osip_from_to_str(ev->request->from,&from);
1440                         osip_message_header_get_byname(ev->request,"Event",0,&h);
1441                         if(h)
1442                                 sal->callbacks.notify(op,from,h->hvalue);
1443                         /*answer that we received the notify*/
1444                         eXosip_lock();
1445                         eXosip_call_build_answer(ev->tid,200,&ans);
1446                         if (ans)
1447                                 eXosip_call_send_answer(ev->tid,200,ans);
1448                         eXosip_unlock();
1449                         osip_free(from);
1450                 }else if (MSG_IS_OPTIONS(ev->request)){
1451                         eXosip_lock();
1452                         eXosip_call_build_answer(ev->tid,200,&ans);
1453                         if (ans){
1454                                 fill_options_answer(ans);
1455                                 eXosip_call_send_answer(ev->tid,200,ans);
1456                         }
1457                         eXosip_unlock();
1458                 }
1459         }else ms_warning("call_message_new: No request ?");
1460 }
1461
1462 static void inc_update(Sal *sal, eXosip_event_t *ev){
1463         osip_message_t *msg=NULL;
1464         ms_message("Processing incoming UPDATE");
1465         eXosip_lock();
1466         eXosip_message_build_answer(ev->tid,200,&msg);
1467         if (msg!=NULL)
1468                 eXosip_message_send_answer(ev->tid,200,msg);
1469         eXosip_unlock();
1470 }
1471
1472 static bool_t comes_from_local_if(osip_message_t *msg){
1473         osip_via_t *via=NULL;
1474         osip_message_get_via(msg,0,&via);
1475         if (via){
1476                 const char *host;
1477                 host=osip_via_get_host(via);
1478                 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1479                         osip_generic_param_t *param=NULL;
1480                         osip_via_param_get_byname(via,"received",&param);
1481                         if (param==NULL) return TRUE;
1482                         if (param->gvalue &&
1483                                 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1484                                 return TRUE;
1485                         }
1486                 }
1487         }
1488         return FALSE;
1489 }
1490
1491 static void text_received(Sal *sal, eXosip_event_t *ev){
1492         osip_body_t *body=NULL;
1493         char *from=NULL,*msg;
1494         
1495         osip_message_get_body(ev->request,0,&body);
1496         if (body==NULL){
1497                 ms_error("Could not get text message from SIP body");
1498                 return;
1499         }
1500         msg=body->body;
1501         osip_from_to_str(ev->request->from,&from);
1502         sal->callbacks.text_received(sal,from,msg);
1503         osip_free(from);
1504 }
1505
1506
1507
1508 static void other_request(Sal *sal, eXosip_event_t *ev){
1509         ms_message("in other_request");
1510         if (ev->request==NULL) return;
1511         if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1512                 text_received(sal,ev);
1513                 eXosip_message_send_answer(ev->tid,200,NULL);
1514         }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1515                 osip_message_t *options=NULL;
1516                 eXosip_options_build_answer(ev->tid,200,&options);
1517                 fill_options_answer(options);
1518                 eXosip_options_send_answer(ev->tid,200,options);
1519         }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1520                 && comes_from_local_if(ev->request)) {
1521                 eXosip_message_send_answer(ev->tid,200,NULL);
1522                 ms_message("Receiving WAKEUP request !");
1523                 sal->callbacks.internal_message(sal,"WAKEUP");
1524         }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1525                 ms_message("Receiving REFER request !");
1526                 if (comes_from_local_if(ev->request)) {
1527                         process_refer(sal,NULL,ev);
1528                 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1529         }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1530                 inc_update(sal,ev);
1531     }else {
1532                 char *tmp=NULL;
1533                 size_t msglen=0;
1534                 osip_message_to_str(ev->request,&tmp,&msglen);
1535                 if (tmp){
1536                         ms_message("Unsupported request received:\n%s",tmp);
1537                         osip_free(tmp);
1538                 }
1539                 /*answer with a 501 Not implemented*/
1540                 eXosip_message_send_answer(ev->tid,501,NULL);
1541         }
1542 }
1543
1544 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1545         osip_via_t *via=NULL;
1546         osip_message_get_via(msg,0,&via);
1547         if (via){
1548                 osip_free(via->port);
1549                 via->port=osip_strdup(port);
1550                 osip_free(via->host);
1551                 via->host=osip_strdup(ip);
1552         }
1553 }
1554
1555 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1556         osip_message_t *msg;
1557         const char *received;
1558         int rport;
1559         osip_contact_t *ctt=NULL;
1560         char *tmp;
1561         char port[20];
1562         SalAddress *addr;
1563         Sal *sal=op->base.root;
1564
1565         if (sal->double_reg==FALSE) return FALSE;
1566         
1567         if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1568         osip_message_get_contact(orig_request,0,&ctt);
1569         if (strcmp(ctt->url->host,received)==0){
1570                 /*ip address matches, check ports*/
1571                 const char *contact_port=ctt->url->port;
1572                 if (contact_port==NULL || contact_port[0]=='\0')
1573                         contact_port="5060";
1574                 if (atoi(contact_port)==rport){
1575                         ms_message("Register has up to date contact, doing nothing.");
1576                         return FALSE;
1577                 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1578         }
1579         eXosip_lock();
1580         msg=NULL;
1581         eXosip_register_build_register(op->rid,op->expires,&msg);
1582         if (msg==NULL){
1583                 eXosip_unlock();
1584                 ms_warning("Fail to create a contact updated register.");
1585                 return FALSE;
1586         }
1587         osip_message_get_contact(msg,0,&ctt);
1588         if (ctt->url->host!=NULL){
1589                 osip_free(ctt->url->host);
1590         }
1591         ctt->url->host=osip_strdup(received);
1592         if (ctt->url->port!=NULL){
1593                 osip_free(ctt->url->port);
1594         }
1595         snprintf(port,sizeof(port),"%i",rport);
1596         ctt->url->port=osip_strdup(port);
1597         if (op->masquerade_via) masquerade_via(msg,received,port);
1598         eXosip_register_send_register(op->rid,msg);
1599         eXosip_unlock();
1600         osip_contact_to_str(ctt,&tmp);
1601         addr=sal_address_new(tmp);
1602         osip_free(tmp);
1603         sal_address_clean(addr);
1604         tmp=sal_address_as_string(addr);
1605         sal_op_set_contact(op,tmp);
1606         sal_address_destroy(addr);
1607         ms_message("Resending new register with updated contact %s",tmp);
1608         ms_free(tmp);
1609         return TRUE;
1610 }
1611
1612 static void registration_success(Sal *sal, eXosip_event_t *ev){
1613         SalOp *op=sal_find_register(sal,ev->rid);
1614         osip_header_t *h=NULL;
1615         bool_t registered;
1616         if (op==NULL){
1617                 ms_error("Receiving register response for unknown operation");
1618                 return;
1619         }
1620         osip_message_get_expires(ev->request,0,&h);
1621         if (h!=NULL && atoi(h->hvalue)!=0){
1622                 registered=TRUE;
1623                 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1624                         sal->callbacks.register_success(op,registered);
1625                 }
1626         }else {
1627                 sal->callbacks.register_success(op,FALSE);
1628         }
1629 }
1630
1631 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1632         int status_code=0;
1633         const char *reason=NULL;
1634         SalOp *op=sal_find_register(sal,ev->rid);
1635         SalReason sr=SalReasonUnknown;
1636         SalError se=SalErrorUnknown;
1637         
1638         if (op==NULL){
1639                 ms_error("Receiving register failure for unknown operation");
1640                 return TRUE;
1641         }
1642         if (ev->response){
1643                 status_code=osip_message_get_status_code(ev->response);
1644                 reason=osip_message_get_reason_phrase(ev->response);
1645         }
1646         switch(status_code){
1647                 case 401:
1648                 case 407:
1649                         return process_authentication(sal,ev);
1650                         break;
1651                 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1652                                  in vias, such as ekiga.net 
1653                                  On the opposite, freephonie.net bugs when via are masqueraded.
1654                                  */
1655                         op->masquerade_via=TRUE;
1656                 default:
1657                         /* if contact is up to date, process the failure, otherwise resend a new register with
1658                                 updated contact first, just in case the faillure is due to incorrect contact */
1659                         if (ev->response && register_again_with_updated_contact(op,ev->request,ev->response))
1660                                 return TRUE; /*we are retrying with an updated contact*/
1661                         if (status_code==403){
1662                                 se=SalErrorFailure;
1663                                 sr=SalReasonForbidden;
1664                         }else if (status_code==0){
1665                                 se=SalErrorNoResponse;
1666                         }
1667                         sal->callbacks.register_failure(op,se,sr,reason);
1668         }
1669         return TRUE;
1670 }
1671
1672 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1673         SalOp *op=find_op(sal,ev);
1674
1675         if (op==NULL){
1676                 ms_warning("other_request_reply(): Receiving response to unknown request.");
1677                 return;
1678         }
1679         if (ev->response){
1680                 update_contact_from_response(op,ev->response);
1681                 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1682                         sal->callbacks.ping_reply(op);
1683         }
1684 }
1685
1686 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1687         ms_message("linphone process event get a message %d\n",ev->type);
1688         switch(ev->type){
1689                 case EXOSIP_CALL_ANSWERED:
1690                         ms_message("CALL_ANSWERED\n");
1691                         call_accepted(sal,ev);
1692                         authentication_ok(sal,ev);
1693                         break;
1694                 case EXOSIP_CALL_CLOSED:
1695                 case EXOSIP_CALL_CANCELLED:
1696                         ms_message("CALL_CLOSED or CANCELLED\n");
1697                         call_terminated(sal,ev);
1698                         break;
1699                 case EXOSIP_CALL_TIMEOUT:
1700                 case EXOSIP_CALL_NOANSWER:
1701                         ms_message("CALL_TIMEOUT or NOANSWER\n");
1702                         return call_failure(sal,ev);
1703                         break;
1704                 case EXOSIP_CALL_REQUESTFAILURE:
1705                 case EXOSIP_CALL_GLOBALFAILURE:
1706                 case EXOSIP_CALL_SERVERFAILURE:
1707                         ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1708                         return call_failure(sal,ev);
1709                         break;
1710                 case EXOSIP_CALL_RELEASED:
1711                         ms_message("CALL_RELEASED\n");
1712                         call_released(sal, ev);
1713                         break;
1714                 case EXOSIP_CALL_INVITE:
1715                         ms_message("CALL_NEW\n");
1716                         inc_new_call(sal,ev);
1717                         break;
1718                 case EXOSIP_CALL_REINVITE:
1719                         handle_reinvite(sal,ev);
1720                         break;
1721                 case EXOSIP_CALL_ACK:
1722                         ms_message("CALL_ACK");
1723                         handle_ack(sal,ev);
1724                         break;
1725                 case EXOSIP_CALL_REDIRECTED:
1726                         ms_message("CALL_REDIRECTED");
1727                         eXosip_default_action(ev);
1728                         break;
1729                 case EXOSIP_CALL_PROCEEDING:
1730                         ms_message("CALL_PROCEEDING");
1731                         call_proceeding(sal,ev);
1732                         break;
1733                 case EXOSIP_CALL_RINGING:
1734                         ms_message("CALL_RINGING");
1735                         call_ringing(sal,ev);
1736                         break;
1737                 case EXOSIP_CALL_MESSAGE_NEW:
1738                         ms_message("EXOSIP_CALL_MESSAGE_NEW");
1739                         call_message_new(sal,ev);
1740                         break;
1741                 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1742                         if (ev->response &&
1743                                 (ev->response->status_code==407 || ev->response->status_code==401)){
1744                                  return process_authentication(sal,ev);
1745                         }
1746                         break;
1747                 case EXOSIP_IN_SUBSCRIPTION_NEW:
1748                         ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1749                         sal_exosip_subscription_recv(sal,ev);
1750                         break;
1751                 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1752                         ms_message("CALL_SUBSCRIPTION_NEW ");
1753                         sal_exosip_in_subscription_closed(sal,ev);
1754                         break;
1755                 case EXOSIP_SUBSCRIPTION_UPDATE:
1756                         ms_message("CALL_SUBSCRIPTION_UPDATE");
1757                         break;
1758                 case EXOSIP_SUBSCRIPTION_NOTIFY:
1759                         ms_message("CALL_SUBSCRIPTION_NOTIFY");
1760                         sal_exosip_notify_recv(sal,ev);
1761                         break;
1762                 case EXOSIP_SUBSCRIPTION_ANSWERED:
1763                         ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1764                         sal_exosip_subscription_answered(sal,ev);
1765                         break;
1766                 case EXOSIP_SUBSCRIPTION_CLOSED:
1767                         ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1768                         sal_exosip_subscription_closed(sal,ev);
1769                         break;
1770                 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE:   /**< announce a request failure      */
1771                         if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1772                                 return process_authentication(sal,ev);
1773                         }
1774         case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1775                 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1776                         sal_exosip_subscription_closed(sal,ev);
1777                         break;
1778                 case EXOSIP_REGISTRATION_FAILURE:
1779                         ms_message("REGISTRATION_FAILURE\n");
1780                         return registration_failure(sal,ev);
1781                         break;
1782                 case EXOSIP_REGISTRATION_SUCCESS:
1783                         authentication_ok(sal,ev);
1784                         registration_success(sal,ev);
1785                         break;
1786                 case EXOSIP_MESSAGE_NEW:
1787                         other_request(sal,ev);
1788                         break;
1789                 case EXOSIP_MESSAGE_PROCEEDING:
1790                 case EXOSIP_MESSAGE_ANSWERED:
1791                 case EXOSIP_MESSAGE_REDIRECTED:
1792                 case EXOSIP_MESSAGE_SERVERFAILURE:
1793                 case EXOSIP_MESSAGE_GLOBALFAILURE:
1794                         other_request_reply(sal,ev);
1795                         break;
1796                 case EXOSIP_MESSAGE_REQUESTFAILURE:
1797                 case EXOSIP_NOTIFICATION_REQUESTFAILURE:
1798                         if (ev->response) {
1799                                 switch (ev->response->status_code) {
1800                                         case 407:
1801                                         case 401:
1802                                                 return process_authentication(sal,ev);
1803                                         case 412: {
1804                                                 eXosip_automatic_action ();
1805                                                 return 1;
1806                                         }
1807                                 }
1808                         }
1809                         other_request_reply(sal,ev);
1810                         break;
1811                 default:
1812                         ms_message("Unhandled exosip event ! %i",ev->type);
1813                         break;
1814         }
1815         return TRUE;
1816 }
1817
1818 int sal_iterate(Sal *sal){
1819         eXosip_event_t *ev;
1820         while((ev=eXosip_event_wait(0,0))!=NULL){
1821                 if (process_event(sal,ev))
1822                         eXosip_event_free(ev);
1823         }
1824         eXosip_lock();
1825         eXosip_automatic_refresh();
1826         eXosip_unlock();
1827         return 0;
1828 }
1829
1830 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1831         osip_message_t *msg;
1832         sal_op_set_route(h,proxy);
1833         if (h->rid==-1){
1834                 eXosip_lock();
1835                 h->rid=eXosip_register_build_initial_register(from,proxy,sal_op_get_contact(h),expires,&msg);
1836                 sal_add_register(h->base.root,h);
1837         }else{
1838                 eXosip_lock();
1839                 eXosip_register_build_register(h->rid,expires,&msg);    
1840         }
1841         eXosip_register_send_register(h->rid,msg);
1842         eXosip_unlock();
1843         h->expires=expires;
1844         return 0;
1845 }
1846
1847 int sal_unregister(SalOp *h){
1848         osip_message_t *msg=NULL;
1849         eXosip_lock();
1850         eXosip_register_build_register(h->rid,0,&msg);
1851         if (msg) eXosip_register_send_register(h->rid,msg);
1852         else ms_warning("Could not build unREGISTER !");
1853         eXosip_unlock();
1854         return 0;
1855 }
1856
1857 SalAddress * sal_address_new(const char *uri){
1858         osip_from_t *from;
1859         osip_from_init(&from);
1860         if (osip_from_parse(from,uri)!=0){
1861                 osip_from_free(from);
1862                 return NULL;
1863         }
1864         if (from->displayname!=NULL && from->displayname[0]=='"'){
1865                 char *unquoted=osip_strdup_without_quote(from->displayname);
1866                 osip_free(from->displayname);
1867                 from->displayname=unquoted;
1868         }
1869         return (SalAddress*)from;
1870 }
1871
1872 SalAddress * sal_address_clone(const SalAddress *addr){
1873         osip_from_t *ret=NULL;
1874         osip_from_clone((osip_from_t*)addr,&ret);
1875         return (SalAddress*)ret;
1876 }
1877
1878 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1879
1880 const char *sal_address_get_scheme(const SalAddress *addr){
1881         const osip_from_t *u=(const osip_from_t*)addr;
1882         return null_if_empty(u->url->scheme);
1883 }
1884
1885 const char *sal_address_get_display_name(const SalAddress* addr){
1886         const osip_from_t *u=(const osip_from_t*)addr;
1887         return null_if_empty(u->displayname);
1888 }
1889
1890 const char *sal_address_get_username(const SalAddress *addr){
1891         const osip_from_t *u=(const osip_from_t*)addr;
1892         return null_if_empty(u->url->username);
1893 }
1894
1895 const char *sal_address_get_domain(const SalAddress *addr){
1896         const osip_from_t *u=(const osip_from_t*)addr;
1897         return null_if_empty(u->url->host);
1898 }
1899
1900 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1901         osip_from_t *u=(osip_from_t*)addr;
1902         if (u->displayname!=NULL){
1903                 osip_free(u->displayname);
1904                 u->displayname=NULL;
1905         }
1906         if (display_name!=NULL && display_name[0]!='\0'){
1907                 u->displayname=osip_strdup(display_name);
1908         }
1909 }
1910
1911 void sal_address_set_username(SalAddress *addr, const char *username){
1912         osip_from_t *uri=(osip_from_t*)addr;
1913         if (uri->url->username!=NULL){
1914                 osip_free(uri->url->username);
1915                 uri->url->username=NULL;
1916         }
1917         if (username)
1918                 uri->url->username=osip_strdup(username);
1919 }
1920
1921 void sal_address_set_domain(SalAddress *addr, const char *host){
1922         osip_from_t *uri=(osip_from_t*)addr;
1923         if (uri->url->host!=NULL){
1924                 osip_free(uri->url->host);
1925                 uri->url->host=NULL;
1926         }
1927         if (host)
1928                 uri->url->host=osip_strdup(host);
1929 }
1930
1931 void sal_address_set_port(SalAddress *addr, const char *port){
1932         osip_from_t *uri=(osip_from_t*)addr;
1933         if (uri->url->port!=NULL){
1934                 osip_free(uri->url->port);
1935                 uri->url->port=NULL;
1936         }
1937         if (port)
1938                 uri->url->port=osip_strdup(port);
1939 }
1940
1941 void sal_address_set_port_int(SalAddress *uri, int port){
1942         char tmp[12];
1943         if (port==5060){
1944                 /*this is the default, special case to leave the port field blank*/
1945                 sal_address_set_port(uri,NULL);
1946                 return;
1947         }
1948         snprintf(tmp,sizeof(tmp),"%i",port);
1949         sal_address_set_port(uri,tmp);
1950 }
1951
1952 void sal_address_clean(SalAddress *addr){
1953         osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1954         osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
1955 }
1956
1957 char *sal_address_as_string(const SalAddress *u){
1958         char *tmp,*ret;
1959         osip_from_t *from=(osip_from_t *)u;
1960         char *old_displayname=NULL;
1961         /* hack to force use of quotes around the displayname*/
1962         if (from->displayname!=NULL
1963             && from->displayname[0]!='"'){
1964                 old_displayname=from->displayname;
1965                 from->displayname=osip_enquote(from->displayname);
1966         }
1967         osip_from_to_str(from,&tmp);
1968         if (old_displayname!=NULL){
1969                 ms_free(from->displayname);
1970                 from->displayname=old_displayname;
1971         }
1972         ret=ms_strdup(tmp);
1973         osip_free(tmp);
1974         return ret;
1975 }
1976
1977 char *sal_address_as_string_uri_only(const SalAddress *u){
1978         char *tmp=NULL,*ret;
1979         osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
1980         ret=ms_strdup(tmp);
1981         osip_free(tmp);
1982         return ret;
1983 }
1984 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
1985         osip_uri_uparam_add     (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
1986 }
1987
1988 void sal_address_destroy(SalAddress *u){
1989         osip_from_free((osip_from_t*)u);
1990 }
1991
1992 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
1993         ctx->keepalive_period=value;
1994         eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
1995 }
1996 unsigned int sal_get_keepalive_period(Sal *ctx) {
1997         return ctx->keepalive_period;
1998 }
1999
2000 const char * sal_address_get_port(const SalAddress *addr) {
2001         const osip_from_t *u=(const osip_from_t*)addr;
2002         return null_if_empty(u->url->port);
2003 }
2004
2005 int sal_address_get_port_int(const SalAddress *uri) {
2006         const char* port = sal_address_get_port(uri);
2007         if (port != NULL) {
2008                 return atoi(port);
2009         } else {
2010                 return 5060;
2011         }
2012 }
2013
2014 /*
2015  * Send a re-Invite used to hold the current call
2016 */
2017 int sal_call_hold(SalOp *h, bool_t holdon)
2018 {
2019         int err=0;
2020
2021         osip_message_t *reinvite=NULL;
2022         if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL)
2023                 return -1;
2024         osip_message_set_subject(reinvite,holdon ? "Phone call hold" : "Phone call resume" );   
2025         osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2026         if (h->base.root->session_expires!=0){
2027                 osip_message_set_header(reinvite, "Session-expires", "200");
2028                 osip_message_set_supported(reinvite, "timer");
2029         }
2030         //add something to say that the distant sip phone will be in sendonly/sendrecv mode
2031         if (h->base.local_media){
2032                 h->sdp_offering=TRUE;
2033                 sal_media_description_set_dir(h->base.local_media, holdon ? SalStreamSendOnly : SalStreamSendRecv);
2034                 set_sdp_from_desc(reinvite,h->base.local_media);
2035         }else h->sdp_offering=FALSE;
2036         eXosip_lock();
2037         err = eXosip_call_send_request(h->did, reinvite);
2038         eXosip_unlock();
2039         
2040         return err;
2041 }
2042
2043 /* sends a reinvite. Local media description may have changed by application since call establishment*/
2044 int sal_call_update(SalOp *h){
2045         int err=0;
2046         osip_message_t *reinvite=NULL;
2047
2048         eXosip_lock();
2049         if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != OSIP_SUCCESS || reinvite==NULL){
2050                 eXosip_unlock();
2051                 return -1;
2052         }
2053         eXosip_unlock();
2054         osip_message_set_subject(reinvite,osip_strdup("Phone call parameters updated"));
2055         osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2056         if (h->base.root->session_expires!=0){
2057                 osip_message_set_header(reinvite, "Session-expires", "200");
2058                 osip_message_set_supported(reinvite, "timer");
2059         }
2060         if (h->base.local_media){
2061                 h->sdp_offering=TRUE;
2062                 set_sdp_from_desc(reinvite,h->base.local_media);
2063         }else h->sdp_offering=FALSE;
2064         eXosip_lock();
2065         err = eXosip_call_send_request(h->did, reinvite);
2066         eXosip_unlock();
2067         return err;
2068 }