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