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