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