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