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