]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2.c
8e1a4bbfd150cdc9493a453235d164ae81b5e7c0
[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                 if (op->call_id)
162                         osip_call_id_free(op->call_id);
163                 op->call_id=NULL;
164         }
165         if (op->pending_auth){
166                 sal_remove_pending_auth(op->base.root,op);
167         }
168         if (op->result)
169                 sal_media_description_unref(op->result);
170         if (op->call_id){
171                 sal_remove_other(op->base.root,op);
172                 osip_call_id_free(op->call_id);
173         }
174         __sal_op_free(op);
175 }
176
177 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
178         int ortp_level=ORTP_DEBUG;
179         switch(level){
180                 case OSIP_INFO1:
181                 case OSIP_INFO2:
182                 case OSIP_INFO3:
183                 case OSIP_INFO4:
184                         ortp_level=ORTP_MESSAGE;
185                         break;
186                 case OSIP_WARNING:
187                         ortp_level=ORTP_WARNING;
188                         break;
189                 case OSIP_ERROR:
190                 case OSIP_BUG:
191                         ortp_level=ORTP_ERROR;
192                         break;
193                 case OSIP_FATAL:
194                         ortp_level=ORTP_FATAL;
195                         break;
196                 case END_TRACE_LEVEL:
197                         break;
198         }
199         if (ortp_log_level_enabled(level)){
200                 int len=strlen(chfr);
201                 char *chfrdup=ortp_strdup(chfr);
202                 /*need to remove endline*/
203                 if (len>1){
204                         if (chfrdup[len-1]=='\n')
205                                 chfrdup[len-1]='\0';
206                         if (chfrdup[len-2]=='\r')
207                                 chfrdup[len-2]='\0';
208                 }
209                 ortp_logv(ortp_level,chfrdup,ap);
210                 ortp_free(chfrdup);
211         }
212 }
213
214
215 Sal * sal_init(){
216         static bool_t firsttime=TRUE;
217         if (firsttime){
218                 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
219                 firsttime=FALSE;
220         }
221         eXosip_init();
222         return ms_new0(Sal,1);
223 }
224
225 void sal_uninit(Sal* sal){
226         eXosip_quit();
227         ms_free(sal);
228 }
229
230 void sal_set_user_pointer(Sal *sal, void *user_data){
231         sal->up=user_data;
232 }
233
234 void *sal_get_user_pointer(const Sal *sal){
235         return sal->up;
236 }
237
238 static void unimplemented_stub(){
239         ms_warning("Unimplemented SAL callback");
240 }
241
242 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
243         memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
244         if (ctx->callbacks.call_received==NULL) 
245                 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
246         if (ctx->callbacks.call_ringing==NULL) 
247                 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
248         if (ctx->callbacks.call_accepted==NULL) 
249                 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
250         if (ctx->callbacks.call_failure==NULL) 
251                 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
252         if (ctx->callbacks.call_terminated==NULL) 
253                 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
254         if (ctx->callbacks.call_updated==NULL) 
255                 ctx->callbacks.call_updated=(SalOnCallUpdated)unimplemented_stub;
256         if (ctx->callbacks.auth_requested==NULL) 
257                 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
258         if (ctx->callbacks.auth_success==NULL) 
259                 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
260         if (ctx->callbacks.register_success==NULL) 
261                 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
262         if (ctx->callbacks.register_failure==NULL) 
263                 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
264         if (ctx->callbacks.dtmf_received==NULL) 
265                 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
266         if (ctx->callbacks.notify==NULL)
267                 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
268         if (ctx->callbacks.subscribe_received==NULL)
269                 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
270         if (ctx->callbacks.text_received==NULL)
271                 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
272         if (ctx->callbacks.internal_message==NULL)
273                 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
274         if (ctx->callbacks.ping_reply==NULL)
275                 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
276 }
277
278 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
279         int err;
280         bool_t ipv6;
281         int proto=IPPROTO_UDP;
282         
283         if (ctx->running) eXosip_quit();
284         eXosip_init();
285         err=0;
286         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
287                                         version of eXosip, which is not the case*/
288         /*see if it looks like an IPv6 address*/
289         ipv6=strchr(addr,':')!=NULL;
290         eXosip_enable_ipv6(ipv6);
291
292         if (tr!=SalTransportDatagram || is_secure){
293                 ms_fatal("SIP over TCP or TLS or DTLS is not supported yet.");
294                 return -1;
295         }
296         
297         err=eXosip_listen_addr(proto, addr, port, ipv6 ?  PF_INET6 : PF_INET, 0);
298         return err;
299 }
300
301 void sal_set_user_agent(Sal *ctx, const char *user_agent){
302         eXosip_set_user_agent(user_agent);
303 }
304
305 void sal_use_session_timers(Sal *ctx, int expires){
306         ctx->session_expires=expires;
307 }
308
309 MSList *sal_get_pending_auths(Sal *sal){
310         return ms_list_copy(sal->pending_auths);
311 }
312
313 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
314         osip_via_t *via=NULL;
315         osip_generic_param_t *param=NULL;
316         const char *rport;
317         
318         osip_message_get_via(msg,0,&via);
319         if (!via) return -1;
320         osip_via_param_get_byname(via,"rport",&param);
321         if (param) {
322                 rport=param->gvalue;
323                 if (rport && rport[0]!='\0') *rportval=atoi(rport);
324                 else *rportval=5060;
325         }
326         param=NULL;
327         osip_via_param_get_byname(via,"received",&param);
328         if (param) *received=param->gvalue;
329         else return -1;
330         return 0;
331 }
332
333 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
334         int sdplen;
335         char clen[10];
336         char *sdp=NULL;
337         sdp_message_to_str(msg,&sdp);
338         sdplen=strlen(sdp);
339         snprintf(clen,sizeof(clen),"%i",sdplen);
340         osip_message_set_body(sip,sdp,sdplen);
341         osip_message_set_content_type(sip,"application/sdp");
342         osip_message_set_content_length(sip,clen);
343         osip_free(sdp);
344 }
345
346 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
347         sdp_message_t *msg=media_description_to_sdp(desc);
348         if (msg==NULL) {
349                 ms_error("Fail to print sdp message !");
350                 return;
351         }
352         set_sdp(sip,msg);
353         sdp_message_free(msg);
354 }
355
356 static void sdp_process(SalOp *h){
357         ms_message("Doing SDP offer/answer process");
358         if (h->result){
359                 sal_media_description_unref(h->result);
360         }
361         h->result=sal_media_description_new();
362         if (h->sdp_offering){   
363                 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
364         }else{
365                 int i;
366                 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result);
367                 h->sdp_answer=media_description_to_sdp(h->result);
368                 strcpy(h->result->addr,h->base.remote_media->addr);
369                 for(i=0;i<h->result->nstreams;++i){
370                         if (h->result->streams[i].port>0){
371                                 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
372                                 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
373                                 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
374                                 h->result->streams[i].port=h->base.remote_media->streams[i].port;
375                         }
376                 }
377         }
378         
379 }
380
381 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
382         if (desc)
383                 sal_media_description_ref(desc);
384         if (h->base.local_media)
385                 sal_media_description_unref(h->base.local_media);
386         h->base.local_media=desc;
387         return 0;
388 }
389
390 int sal_call(SalOp *h, const char *from, const char *to){
391         int err;
392         osip_message_t *invite=NULL;
393         sal_op_set_from(h,from);
394         sal_op_set_to(h,to);
395         sal_exosip_fix_route(h);
396         err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
397         if (err!=0){
398                 ms_error("Could not create call.");
399                 return -1;
400         }
401         if (h->base.contact){
402                 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
403                 osip_message_set_contact(invite,h->base.contact);
404         }
405         if (h->base.root->session_expires!=0){
406                 osip_message_set_header(invite, "Session-expires", "200");
407                 osip_message_set_supported(invite, "timer");
408         }
409         if (h->base.local_media){
410                 h->sdp_offering=TRUE;
411                 set_sdp_from_desc(invite,h->base.local_media);
412         }else h->sdp_offering=FALSE;
413         eXosip_lock();
414         err=eXosip_call_send_initial_invite(invite);
415         eXosip_unlock();
416         h->cid=err;
417         if (err<0){
418                 ms_error("Fail to send invite !");
419                 return -1;
420         }else{
421                 eXosip_call_set_reference(h->cid,h);
422         }
423         return 0;
424 }
425
426 int sal_call_notify_ringing(SalOp *h){
427         eXosip_lock();
428         eXosip_call_send_answer(h->tid,180,NULL);
429         eXosip_unlock();
430         return 0;
431 }
432
433 int sal_call_accept(SalOp * h){
434         osip_message_t *msg;
435         const char *contact=sal_op_get_contact(h);
436         /* sends a 200 OK */
437         int err=eXosip_call_build_answer(h->tid,200,&msg);
438         if (err<0 || msg==NULL){
439                 ms_error("Fail to build answer for call: err=%i",err);
440                 return -1;
441         }
442         if (h->base.root->session_expires!=0){
443                 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
444         }
445
446         if (contact) {
447                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
448                 osip_message_set_contact(msg,contact);
449         }
450         
451         if (h->base.local_media){
452                 /*this is the case where we received an invite without SDP*/
453                 if (h->sdp_offering) {
454                         set_sdp_from_desc(msg,h->base.local_media);
455                 }else{
456                         if (h->sdp_answer)
457                                 set_sdp(msg,h->sdp_answer);
458                 }
459         }else{
460                 ms_error("You are accepting a call but not defined any media capabilities !");
461         }
462         eXosip_call_send_answer(h->tid,200,msg);
463         return 0;
464 }
465
466 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
467         if (reason==SalReasonBusy){
468                 eXosip_lock();
469                 eXosip_call_send_answer(h->tid,486,NULL);
470                 eXosip_unlock();
471         }
472         else if (reason==SalReasonTemporarilyUnavailable){
473                 eXosip_lock();
474                 eXosip_call_send_answer(h->tid,480,NULL);
475                 eXosip_unlock();
476         }else if (reason==SalReasonDoNotDisturb){
477                 eXosip_lock();
478                 eXosip_call_send_answer(h->tid,600,NULL);
479                 eXosip_unlock();
480         }else if (reason==SalReasonMedia){
481                 eXosip_lock();
482                 eXosip_call_send_answer(h->tid,415,NULL);
483                 eXosip_unlock();
484         }else if (redirect!=NULL && reason==SalReasonRedirect){
485                 osip_message_t *msg;
486                 int code;
487                 if (strstr(redirect,"sip:")!=0) code=302;
488                 else code=380;
489                 eXosip_lock();
490                 eXosip_call_build_answer(h->tid,code,&msg);
491                 osip_message_set_contact(msg,redirect);
492                 eXosip_call_send_answer(h->tid,code,msg);
493                 eXosip_unlock();
494         }else sal_call_terminate(h);
495         return 0;
496 }
497
498 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
499         if (h->base.local_media && h->base.remote_media && !h->result){
500                 sdp_process(h);
501         }
502         return h->result;
503 }
504
505 int sal_ping(SalOp *op, const char *from, const char *to){
506         osip_message_t *options=NULL;
507         
508         sal_op_set_from(op,from);
509         sal_op_set_to(op,to);
510         eXosip_options_build_request (&options, sal_op_get_to(op),
511                         sal_op_get_from(op),sal_op_get_route(op));
512         if (options){
513                 if (op->base.root->session_expires!=0){
514                         osip_message_set_header(options, "Session-expires", "200");
515                         osip_message_set_supported(options, "timer");
516                 }
517                 sal_add_other(sal_op_get_sal(op),op,options);
518                 return eXosip_options_send_request(options);
519         }
520         return -1;
521 }
522
523 int sal_refer(SalOp *h, const char *refer_to){
524         osip_message_t *msg=NULL;
525         int err=0;
526         eXosip_lock();
527         eXosip_call_build_refer(h->did,refer_to, &msg);
528         if (msg) err=eXosip_call_send_request(h->did, msg);
529         else err=-1;
530         eXosip_unlock();
531         return err;
532 }
533
534 int sal_call_send_dtmf(SalOp *h, char dtmf){
535         osip_message_t *msg=NULL;
536         char dtmf_body[128];
537         char clen[10];
538
539         eXosip_lock();
540         eXosip_call_build_info(h->did,&msg);
541         if (msg){
542                 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
543                 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
544                 osip_message_set_content_type(msg,"application/dtmf-relay");
545                 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
546                 osip_message_set_content_length(msg,clen);              
547                 eXosip_call_send_request(h->did,msg);
548         }
549         eXosip_unlock();
550         return 0;
551 }
552
553 int sal_call_terminate(SalOp *h){
554         eXosip_lock();
555         eXosip_call_terminate(h->cid,h->did);
556         eXosip_call_set_reference(h->cid,NULL);
557         eXosip_unlock();
558         return 0;
559 }
560
561 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
562         if (h->pending_auth){
563                 const char *userid;
564                 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
565                 else userid=info->userid;
566                 ms_message("Authentication info for %s %s added to eXosip", info->username,info->realm);
567                 eXosip_add_authentication_info (info->username,userid,
568                                       info->password, NULL,info->realm);
569                 eXosip_lock();
570                 eXosip_default_action(h->pending_auth);
571                 eXosip_unlock();
572                 ms_message("eXosip_default_action() done");
573                 eXosip_clear_authentication_info();
574                 eXosip_event_free(h->pending_auth);
575                 sal_remove_pending_auth(sal_op_get_sal(h),h);
576                 h->pending_auth=NULL;
577         }
578 }
579
580 static void set_network_origin(SalOp *op, osip_message_t *req){
581         const char *received=NULL;
582         int rport=5060;
583         char origin[64];
584         if (extract_received_rport(req,&received,&rport)!=0){
585                 osip_via_t *via=NULL;
586                 char *tmp;
587                 osip_message_get_via(req,0,&via);
588                 received=osip_via_get_host(via);
589                 tmp=osip_via_get_port(via);
590                 if (tmp) rport=atoi(tmp);
591         }
592         snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
593         __sal_op_set_network_origin(op,origin);
594 }
595
596 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
597         if (ev->cid>0)
598                 return (SalOp*)eXosip_call_get_reference(ev->cid);;
599         if (ev->rid>0){
600                 return sal_find_register(sal,ev->rid);
601         }
602         if (ev->response) return sal_find_other(sal,ev->response);
603         return NULL;
604 }
605
606 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
607         SalOp *op=sal_op_new(sal);
608         osip_from_t *from,*to;
609         char *tmp;
610         sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
611
612         set_network_origin(op,ev->request);
613         
614         if (sdp){
615                 op->sdp_offering=FALSE;
616                 op->base.remote_media=sal_media_description_new();
617                 sdp_to_media_description(sdp,op->base.remote_media);
618                 sdp_message_free(sdp);
619         }else op->sdp_offering=TRUE;
620
621         from=osip_message_get_from(ev->request);
622         to=osip_message_get_to(ev->request);
623         osip_from_to_str(from,&tmp);
624         sal_op_set_from(op,tmp);
625         osip_free(tmp);
626         osip_from_to_str(to,&tmp);
627         sal_op_set_to(op,tmp);
628         osip_free(tmp);
629         
630         op->tid=ev->tid;
631         op->cid=ev->cid;
632         op->did=ev->did;
633         
634         eXosip_call_set_reference(op->cid,op);
635         sal->callbacks.call_received(op);
636 }
637
638 static void handle_reinvite(Sal *sal,  eXosip_event_t *ev){
639         SalOp *op=find_op(sal,ev);
640         sdp_message_t *sdp;
641         osip_message_t *msg=NULL;
642
643         if (op==NULL) {
644                 ms_warning("Reinvite for non-existing operation !");
645                 return;
646         }
647         op->reinvite=TRUE;
648         op->tid=ev->tid;
649         sdp=eXosip_get_sdp_info(ev->request);
650         if (op->base.remote_media){
651                 sal_media_description_unref(op->base.remote_media);
652                 op->base.remote_media=NULL;
653         }
654         eXosip_lock();
655         eXosip_call_build_answer(ev->tid,200,&msg);
656         eXosip_unlock();
657         if (msg==NULL) return;
658         if (op->base.root->session_expires!=0){
659                 if (op->supports_session_timers) osip_message_set_supported(msg, "timer");
660         }
661         if (op->base.contact){
662                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
663                 osip_message_set_contact(msg,op->base.contact);
664         }
665         if (sdp){
666                 op->sdp_offering=FALSE;
667                 op->base.remote_media=sal_media_description_new();
668                 sdp_to_media_description(sdp,op->base.remote_media);
669                 sdp_message_free(sdp);
670                 sdp_process(op);
671                 set_sdp(msg,op->sdp_answer);
672         }else {
673                 op->sdp_offering=TRUE;
674                 set_sdp_from_desc(msg,op->base.local_media);
675         }
676         eXosip_lock();
677         eXosip_call_send_answer(ev->tid,200,msg);
678         eXosip_unlock();
679 }
680
681 static void handle_ack(Sal *sal,  eXosip_event_t *ev){
682         SalOp *op=find_op(sal,ev);
683         sdp_message_t *sdp;
684
685         if (op==NULL) {
686                 ms_warning("ack for non-existing call !");
687                 return;
688         }
689         sdp=eXosip_get_sdp_info(ev->ack);
690         if (sdp){
691                 op->base.remote_media=sal_media_description_new();
692                 sdp_to_media_description(sdp,op->base.remote_media);
693                 sdp_process(op);
694                 sdp_message_free(sdp);
695         }
696         if (op->reinvite){
697                 sal->callbacks.call_updated(op);
698                 op->reinvite=FALSE;
699         }else{
700                 sal->callbacks.call_ack(op);
701         }
702 }
703
704 static void update_contact_from_response(SalOp *op, osip_message_t *response){
705         const char *received;
706         int rport;
707         if (extract_received_rport(response,&received,&rport)==0){
708                 const char *contact=sal_op_get_contact(op);
709                 if (!contact){
710                         /*no contact given yet, use from instead*/
711                         contact=sal_op_get_from(op);
712                 }
713                 if (contact){
714                         SalAddress *addr=sal_address_new(contact);
715                         char *tmp;
716                         sal_address_set_domain(addr,received);
717                         sal_address_set_port_int(addr,rport);
718                         tmp=sal_address_as_string(addr);
719                         ms_message("Contact address updated to %s for this dialog",tmp);
720                         sal_op_set_contact(op,tmp);
721                         ms_free(tmp);
722                 }
723         }
724 }
725
726 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
727         SalOp *op=find_op(sal,ev);
728         
729         if (op==NULL) {
730                 ms_warning("This call has been canceled.");
731                 eXosip_lock();
732                 eXosip_call_terminate(ev->cid,ev->did);
733                 eXosip_unlock();
734                 return -1;
735         }
736         op->did=ev->did;
737         op->tid=ev->tid;
738         
739         /* update contact if received and rport are set by the server
740          note: will only be used by remote for next INVITE, if any...*/
741         update_contact_from_response(op,ev->response);
742         return 0;
743 }
744
745 static void call_ringing(Sal *sal, eXosip_event_t *ev){
746         sdp_message_t *sdp;
747         SalOp *op;
748         if (call_proceeding(sal, ev)==-1) return;
749         op=(SalOp*)ev->external_reference;
750         sdp=eXosip_get_sdp_info(ev->response);
751         if (sdp){
752                 op->base.remote_media=sal_media_description_new();
753                 sdp_to_media_description(sdp,op->base.remote_media);
754                 sdp_message_free(sdp);
755                 if (op->base.local_media) sdp_process(op);
756         }
757         sal->callbacks.call_ringing(op);
758 }
759
760 static void call_accepted(Sal *sal, eXosip_event_t *ev){
761         sdp_message_t *sdp;
762         osip_message_t *msg=NULL;
763         SalOp *op=find_op(sal,ev);
764         const char *contact;
765         
766         if (op==NULL){
767                 ms_error("A closed call is accepted ?");
768                 return;
769         }
770         sdp=eXosip_get_sdp_info(ev->response);
771         if (sdp){
772                 op->base.remote_media=sal_media_description_new();
773                 sdp_to_media_description(sdp,op->base.remote_media);
774                 sdp_message_free(sdp);
775                 if (op->base.local_media) sdp_process(op);
776         }
777         eXosip_call_build_ack(ev->did,&msg);
778         contact=sal_op_get_contact(op);
779         if (contact) {
780                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
781                 osip_message_set_contact(msg,contact);
782         }
783         if (op->sdp_answer)
784                         set_sdp(msg,op->sdp_answer);
785         eXosip_call_send_ack(ev->did,msg);
786         sal->callbacks.call_accepted(op);
787 }
788
789 static void call_terminated(Sal *sal, eXosip_event_t *ev){
790         char *from;
791         SalOp *op=find_op(sal,ev);
792         if (op==NULL){
793                 ms_warning("Call terminated for already closed call ?");
794                 return;
795         }
796         osip_from_to_str(ev->request->from,&from);
797         eXosip_call_set_reference(ev->cid,NULL);
798         op->cid=-1;
799         sal->callbacks.call_terminated(op,from);
800         osip_free(from);
801 }
802
803 static void call_released(Sal *sal, eXosip_event_t *ev){
804         SalOp *op=find_op(sal,ev);
805         if (op==NULL){
806                 return;
807         }
808         op->cid=-1;
809         if (op->did==-1) 
810                 sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL);
811 }
812
813 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
814         const char *prx_realm=NULL,*www_realm=NULL;
815         osip_proxy_authenticate_t *prx_auth;
816         osip_www_authenticate_t *www_auth;
817         
818         *username=osip_uri_get_username(resp->from->url);
819         prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
820         www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
821         if (prx_auth!=NULL)
822                 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
823         if (www_auth!=NULL)
824                 www_realm=osip_www_authenticate_get_realm(www_auth);
825
826         if (prx_realm){
827                 *realm=prx_realm;
828         }else if (www_realm){
829                 *realm=www_realm;
830         }else{
831                 return -1;
832         }
833         return 0;
834 }
835
836 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
837         osip_authorization_t *auth=NULL;
838         osip_proxy_authorization_t *prx_auth=NULL;
839         
840         *username=osip_uri_get_username(msg->from->url);
841         osip_message_get_authorization(msg, 0, &auth);
842         if (auth){
843                 *realm=osip_authorization_get_realm(auth);
844                 return 0;
845         }
846         osip_message_get_proxy_authorization(msg,0,&prx_auth);
847         if (prx_auth){
848                 *realm=osip_proxy_authorization_get_realm(prx_auth);
849                 return 0;
850         }
851         return -1;
852 }
853
854 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
855         if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
856         if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
857         return -1;
858 }
859
860 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
861         if (op->pending_auth){
862                 return get_auth_data(op->pending_auth,realm,username);
863         }
864         return -1;
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 void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1139         osip_via_t *via=NULL;
1140         osip_message_get_via(msg,0,&via);
1141         if (via){
1142                 osip_free(via->port);
1143                 via->port=osip_strdup(port);
1144                 osip_free(via->host);
1145                 via->host=osip_strdup(ip);
1146         }
1147 }
1148
1149 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1150         osip_message_t *msg;
1151         const char *received;
1152         int rport;
1153         osip_contact_t *ctt=NULL;
1154         char *tmp;
1155         char port[20];
1156         
1157         if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1158         osip_message_get_contact(orig_request,0,&ctt);
1159         if (strcmp(ctt->url->host,received)==0){
1160                 /*ip address matches, check ports*/
1161                 const char *contact_port=ctt->url->port;
1162                 if (contact_port==NULL || contact_port[0]=='\0')
1163                         contact_port="5060";
1164                 if (atoi(contact_port)==rport){
1165                         ms_message("Register has up to date contact, doing nothing.");
1166                         return FALSE;
1167                 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1168         }
1169         eXosip_lock();
1170         msg=NULL;
1171         eXosip_register_build_register(op->rid,op->expires,&msg);
1172         if (msg==NULL){
1173                 eXosip_unlock();
1174                 ms_warning("Fail to create a contact updated register.");
1175                 return FALSE;
1176         }
1177         osip_message_get_contact(msg,0,&ctt);
1178         if (ctt->url->host!=NULL){
1179                 osip_free(ctt->url->host);
1180         }
1181         ctt->url->host=osip_strdup(received);
1182         if (ctt->url->port!=NULL){
1183                 osip_free(ctt->url->port);
1184         }
1185         snprintf(port,sizeof(port),"%i",rport);
1186         ctt->url->port=osip_strdup(port);
1187         masquerade_via(msg,received,port);
1188         eXosip_register_send_register(op->rid,msg);
1189         eXosip_unlock();
1190         osip_contact_to_str(ctt,&tmp);
1191         sal_op_set_contact(op,tmp);
1192         ms_message("Resending new register with updated contact %s",tmp);
1193         ms_free(tmp);
1194         return TRUE;
1195 }
1196
1197 static void registration_success(Sal *sal, eXosip_event_t *ev){
1198         SalOp *op=sal_find_register(sal,ev->rid);
1199         osip_header_t *h=NULL;
1200         bool_t registered;
1201         if (op==NULL){
1202                 ms_error("Receiving register response for unknown operation");
1203                 return;
1204         }
1205         osip_message_get_expires(ev->request,0,&h);
1206         if (h!=NULL && atoi(h->hvalue)!=0){
1207                 registered=TRUE;
1208                 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1209                         sal->callbacks.register_success(op,registered);
1210                 }
1211         }else registered=FALSE;
1212 }
1213
1214 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1215         int status_code=0;
1216         const char *reason=NULL;
1217         SalOp *op=sal_find_register(sal,ev->rid);
1218         SalReason sr=SalReasonUnknown;
1219         SalError se=SalErrorUnknown;
1220         
1221         if (op==NULL){
1222                 ms_error("Receiving register failure for unknown operation");
1223                 return TRUE;
1224         }
1225         if (ev->response){
1226                 status_code=osip_message_get_status_code(ev->response);
1227                 reason=osip_message_get_reason_phrase(ev->response);
1228         }else return TRUE;
1229         switch(status_code){
1230                 case 401:
1231                 case 407:
1232                         return process_authentication(sal,ev);
1233                         break;
1234                 default:
1235                         /* if contact is up to date, process the failure, otherwise resend a new register with
1236                                 updated contact first, just in case the faillure is due to incorrect contact */
1237                         if (register_again_with_updated_contact(op,ev->request,ev->response))
1238                                 return TRUE; /*we are retrying with an updated contact*/
1239                         if (status_code==403){
1240                                 se=SalErrorFailure;
1241                                 sr=SalReasonForbidden;
1242                         }else if (status_code==0){
1243                                 se=SalErrorNoResponse;
1244                         }
1245                         sal->callbacks.register_failure(op,se,sr,reason);
1246         }
1247         return TRUE;
1248 }
1249
1250 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1251         SalOp *op=find_op(sal,ev);
1252
1253         if (op==NULL){
1254                 ms_warning("other_request_reply(): Receiving response to unknown request.");
1255                 return;
1256         }
1257         if (ev->response){
1258                 update_contact_from_response(op,ev->response);
1259                 sal->callbacks.ping_reply(op);
1260         }
1261 }
1262
1263 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1264         switch(ev->type){
1265                 case EXOSIP_CALL_ANSWERED:
1266                         ms_message("CALL_ANSWERED\n");
1267                         call_accepted(sal,ev);
1268                         authentication_ok(sal,ev);
1269                         break;
1270                 case EXOSIP_CALL_CLOSED:
1271                 case EXOSIP_CALL_CANCELLED:
1272                         ms_message("CALL_CLOSED or CANCELLED\n");
1273                         call_terminated(sal,ev);
1274                         break;
1275                 case EXOSIP_CALL_TIMEOUT:
1276                 case EXOSIP_CALL_NOANSWER:
1277                         ms_message("CALL_TIMEOUT or NOANSWER\n");
1278                         return call_failure(sal,ev);
1279                         break;
1280                 case EXOSIP_CALL_REQUESTFAILURE:
1281                 case EXOSIP_CALL_GLOBALFAILURE:
1282                 case EXOSIP_CALL_SERVERFAILURE:
1283                         ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1284                         return call_failure(sal,ev);
1285                         break;
1286                 case EXOSIP_CALL_INVITE:
1287                         ms_message("CALL_NEW\n");
1288                         inc_new_call(sal,ev);
1289                         break;
1290                 case EXOSIP_CALL_REINVITE:
1291                         handle_reinvite(sal,ev);
1292                         break;
1293                 case EXOSIP_CALL_ACK:
1294                         ms_message("CALL_ACK");
1295                         handle_ack(sal,ev);
1296                         break;
1297                 case EXOSIP_CALL_REDIRECTED:
1298                         ms_message("CALL_REDIRECTED");
1299                         eXosip_default_action(ev);
1300                         break;
1301                 case EXOSIP_CALL_PROCEEDING:
1302                         ms_message("CALL_PROCEEDING");
1303                         call_proceeding(sal,ev);
1304                         break;
1305                 case EXOSIP_CALL_RINGING:
1306                         ms_message("CALL_RINGING");
1307                         call_ringing(sal,ev);
1308                         break;
1309                 case EXOSIP_CALL_MESSAGE_NEW:
1310                         ms_message("EXOSIP_CALL_MESSAGE_NEW");
1311                         call_message_new(sal,ev);
1312                         break;
1313                 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1314                         if (ev->did<0 && ev->response &&
1315                                 (ev->response->status_code==407 || ev->response->status_code==401)){
1316                                  return process_authentication(sal,ev);
1317                         }
1318                         break;
1319                 case EXOSIP_IN_SUBSCRIPTION_NEW:
1320                         ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1321                         sal_exosip_subscription_recv(sal,ev);
1322                         break;
1323                 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1324                         ms_message("CALL_SUBSCRIPTION_NEW ");
1325                         sal_exosip_in_subscription_closed(sal,ev);
1326                         break;
1327                 case EXOSIP_SUBSCRIPTION_UPDATE:
1328                         ms_message("CALL_SUBSCRIPTION_UPDATE");
1329                         break;
1330                 case EXOSIP_SUBSCRIPTION_NOTIFY:
1331                         ms_message("CALL_SUBSCRIPTION_NOTIFY");
1332                         sal_exosip_notify_recv(sal,ev);
1333                         break;
1334                 case EXOSIP_SUBSCRIPTION_ANSWERED:
1335                         ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1336                         sal_exosip_subscription_answered(sal,ev);
1337                         break;
1338                 case EXOSIP_SUBSCRIPTION_CLOSED:
1339                         ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1340                         sal_exosip_subscription_closed(sal,ev);
1341                         break;
1342                 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE:   /**< announce a request failure      */
1343                         if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1344                                 return process_authentication(sal,ev);
1345                         }
1346         case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1347                 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1348                         sal_exosip_subscription_closed(sal,ev);
1349                         break;
1350                 case EXOSIP_CALL_RELEASED:
1351                         ms_message("CALL_RELEASED\n");
1352                         call_released(sal, ev);
1353                         break;
1354                 case EXOSIP_REGISTRATION_FAILURE:
1355                         ms_message("REGISTRATION_FAILURE\n");
1356                         return registration_failure(sal,ev);
1357                         break;
1358                 case EXOSIP_REGISTRATION_SUCCESS:
1359                         authentication_ok(sal,ev);
1360                         registration_success(sal,ev);
1361                         break;
1362                 case EXOSIP_MESSAGE_NEW:
1363                         other_request(sal,ev);
1364                         break;
1365                 case EXOSIP_MESSAGE_PROCEEDING:
1366                 case EXOSIP_MESSAGE_ANSWERED:
1367                 case EXOSIP_MESSAGE_REDIRECTED:
1368                 case EXOSIP_MESSAGE_SERVERFAILURE:
1369                 case EXOSIP_MESSAGE_GLOBALFAILURE:
1370                         other_request_reply(sal,ev);
1371                         break;
1372                 case EXOSIP_MESSAGE_REQUESTFAILURE:
1373                         if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1374                                 return process_authentication(sal,ev);
1375                         }
1376                         break;
1377                 default:
1378                         ms_message("Unhandled exosip event ! %i",ev->type);
1379                         break;
1380         }
1381         return TRUE;
1382 }
1383
1384 int sal_iterate(Sal *sal){
1385         eXosip_event_t *ev;
1386         while((ev=eXosip_event_wait(0,0))!=NULL){
1387                 if (process_event(sal,ev))
1388                         eXosip_event_free(ev);
1389         }
1390         eXosip_lock();
1391         eXosip_automatic_refresh();
1392         eXosip_unlock();
1393         return 0;
1394 }
1395
1396 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1397         osip_message_t *msg;
1398         sal_op_set_route(h,proxy);
1399         if (h->rid==-1){
1400                 eXosip_lock();
1401                 h->rid=eXosip_register_build_initial_register(from,proxy,sal_op_get_contact(h),expires,&msg);
1402                 sal_add_register(h->base.root,h);
1403         }else{
1404                 eXosip_lock();
1405                 eXosip_register_build_register(h->rid,expires,&msg);    
1406         }
1407         eXosip_register_send_register(h->rid,msg);
1408         eXosip_unlock();
1409         h->expires=expires;
1410         return 0;
1411 }
1412
1413 int sal_unregister(SalOp *h){
1414         osip_message_t *msg=NULL;
1415         eXosip_lock();
1416         eXosip_register_build_register(h->rid,0,&msg);
1417         if (msg) eXosip_register_send_register(h->rid,msg);
1418         else ms_warning("Could not build unREGISTER !");
1419         eXosip_unlock();
1420         return 0;
1421 }
1422
1423
1424
1425 SalAddress * sal_address_new(const char *uri){
1426         osip_from_t *from;
1427         osip_from_init(&from);
1428         if (osip_from_parse(from,uri)!=0){
1429                 osip_from_free(from);
1430                 return NULL;
1431         }
1432         return (SalAddress*)from;
1433 }
1434
1435 SalAddress * sal_address_clone(const SalAddress *addr){
1436         osip_from_t *ret=NULL;
1437         osip_from_clone((osip_from_t*)addr,&ret);
1438         return (SalAddress*)ret;
1439 }
1440
1441 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1442
1443 const char *sal_address_get_scheme(const SalAddress *addr){
1444         const osip_from_t *u=(const osip_from_t*)addr;
1445         return null_if_empty(u->url->scheme);
1446 }
1447
1448 const char *sal_address_get_display_name(const SalAddress* addr){
1449         const osip_from_t *u=(const osip_from_t*)addr;
1450         return null_if_empty(u->displayname);
1451 }
1452
1453 const char *sal_address_get_username(const SalAddress *addr){
1454         const osip_from_t *u=(const osip_from_t*)addr;
1455         return null_if_empty(u->url->username);
1456 }
1457
1458 const char *sal_address_get_domain(const SalAddress *addr){
1459         const osip_from_t *u=(const osip_from_t*)addr;
1460         return null_if_empty(u->url->host);
1461 }
1462
1463 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1464         osip_from_t *u=(osip_from_t*)addr;
1465         if (u->displayname!=NULL){
1466                 osip_free(u->displayname);
1467                 u->displayname=NULL;
1468         }
1469         if (display_name!=NULL)
1470                 u->displayname=osip_strdup(display_name);
1471 }
1472
1473 void sal_address_set_username(SalAddress *addr, const char *username){
1474         osip_from_t *uri=(osip_from_t*)addr;
1475         if (uri->url->username!=NULL){
1476                 osip_free(uri->url->username);
1477                 uri->url->username=NULL;
1478         }
1479         if (username)
1480                 uri->url->username=osip_strdup(username);
1481 }
1482
1483 void sal_address_set_domain(SalAddress *addr, const char *host){
1484         osip_from_t *uri=(osip_from_t*)addr;
1485         if (uri->url->host!=NULL){
1486                 osip_free(uri->url->host);
1487                 uri->url->host=NULL;
1488         }
1489         if (host)
1490                 uri->url->host=osip_strdup(host);
1491 }
1492
1493 void sal_address_set_port(SalAddress *addr, const char *port){
1494         osip_from_t *uri=(osip_from_t*)addr;
1495         if (uri->url->port!=NULL){
1496                 osip_free(uri->url->port);
1497                 uri->url->port=NULL;
1498         }
1499         if (port)
1500                 uri->url->port=osip_strdup(port);
1501 }
1502
1503 void sal_address_set_port_int(SalAddress *uri, int port){
1504         char tmp[12];
1505         if (port==5060){
1506                 /*this is the default, special case to leave the port field blank*/
1507                 sal_address_set_port(uri,NULL);
1508                 return;
1509         }
1510         snprintf(tmp,sizeof(tmp),"%i",port);
1511         sal_address_set_port(uri,tmp);
1512 }
1513
1514 void sal_address_clean(SalAddress *addr){
1515         osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1516 }
1517
1518 char *sal_address_as_string(const SalAddress *u){
1519         char *tmp,*ret;
1520         osip_from_to_str((osip_from_t*)u,&tmp);
1521         ret=ms_strdup(tmp);
1522         osip_free(tmp);
1523         return ret;
1524 }
1525
1526 char *sal_address_as_string_uri_only(const SalAddress *u){
1527         char *tmp=NULL,*ret;
1528         osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
1529         ret=ms_strdup(tmp);
1530         osip_free(tmp);
1531         return ret;
1532 }
1533
1534 void sal_address_destroy(SalAddress *u){
1535         osip_from_free((osip_from_t*)u);
1536 }
1537