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