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