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