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