]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2.c
38638716e5a78176e2005fcd1823484b94a9aec6
[linphone] / coreapi / sal_eXosip2.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "sal_eXosip2.h"
24 #include "private.h"
25 #include "offeranswer.h"
26
27 static bool_t call_failure(Sal *sal, eXosip_event_t *ev);
28
29 static void text_received(Sal *sal, eXosip_event_t *ev);
30
31 void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
32         void *data;
33         while((data=osip_list_get(l,0))!=NULL){
34                 osip_list_remove(l,0);
35                 freefunc(data);
36         }
37 }
38
39 void sal_get_default_local_ip(Sal *sal, int address_family,char *ip, size_t iplen){
40         if (eXosip_guess_localip(address_family,ip,iplen)<0){
41                 /*default to something */
42                 strncpy(ip,address_family==AF_INET6 ? "::1" : "127.0.0.1",iplen);
43                 ms_error("Could not find default routable ip address !");
44         }
45 }
46
47
48 static SalOp * sal_find_call(Sal *sal, int cid){
49         const MSList *elem;
50         SalOp *op;
51         for(elem=sal->calls;elem!=NULL;elem=elem->next){
52                 op=(SalOp*)elem->data;
53                 if (op->cid==cid) return op;
54         }
55         return NULL;
56 }
57
58 static void sal_add_call(Sal *sal, SalOp *op){
59         sal->calls=ms_list_append(sal->calls,op);
60 }
61
62 static void sal_remove_call(Sal *sal, SalOp *op){
63         sal->calls=ms_list_remove(sal->calls, op);
64 }
65
66 static SalOp * sal_find_register(Sal *sal, int rid){
67         const MSList *elem;
68         SalOp *op;
69         for(elem=sal->registers;elem!=NULL;elem=elem->next){
70                 op=(SalOp*)elem->data;
71                 if (op->rid==rid) return op;
72         }
73         return NULL;
74 }
75
76 static void sal_add_register(Sal *sal, SalOp *op){
77         sal->registers=ms_list_append(sal->registers,op);
78 }
79
80 static void sal_remove_register(Sal *sal, int rid){
81         MSList *elem;
82         SalOp *op;
83         for(elem=sal->registers;elem!=NULL;elem=elem->next){
84                 op=(SalOp*)elem->data;
85                 if (op->rid==rid) {
86                         sal->registers=ms_list_remove_link(sal->registers,elem);
87                         return;
88                 }
89         }
90 }
91
92 static SalOp * sal_find_other(Sal *sal, osip_message_t *response){
93         const MSList *elem;
94         SalOp *op;
95         osip_call_id_t *callid=osip_message_get_call_id(response);
96         if (callid==NULL) {
97                 ms_error("There is no call-id in this response !");
98                 return NULL;
99         }
100         for(elem=sal->other_transactions;elem!=NULL;elem=elem->next){
101                 op=(SalOp*)elem->data;
102                 if (osip_call_id_match(callid,op->call_id)==0) return op;
103         }
104         return NULL;
105 }
106
107 void sal_add_other(Sal *sal, SalOp *op, osip_message_t *request){
108         osip_call_id_t *callid=osip_message_get_call_id(request);
109         if (callid==NULL) {
110                 ms_error("There is no call id in the request !");
111                 return;
112         }
113         osip_call_id_clone(callid,&op->call_id);
114         sal->other_transactions=ms_list_append(sal->other_transactions,op);
115 }
116
117 static void sal_remove_other(Sal *sal, SalOp *op){
118         sal->other_transactions=ms_list_remove(sal->other_transactions,op);
119 }
120
121
122 static void sal_add_pending_auth(Sal *sal, SalOp *op){
123         sal->pending_auths=ms_list_append(sal->pending_auths,op);
124 }
125
126
127 static void sal_remove_pending_auth(Sal *sal, SalOp *op){
128         sal->pending_auths=ms_list_remove(sal->pending_auths,op);
129 }
130
131 void sal_exosip_fix_route(SalOp *op){
132         if (sal_op_get_route(op)!=NULL){
133                 osip_route_t *rt=NULL;
134                 osip_uri_param_t *lr_param=NULL;
135                 
136                 osip_route_init(&rt);
137                 if (osip_route_parse(rt,sal_op_get_route(op))<0){
138                         ms_warning("Bad route  %s!",sal_op_get_route(op));
139                         sal_op_set_route(op,NULL);
140                 }else{
141                         /* check if the lr parameter is set , if not add it */
142                         osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
143                         if (lr_param==NULL){
144                                 char *tmproute;
145                                 osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
146                                 osip_route_to_str(rt,&tmproute);
147                                 sal_op_set_route(op,tmproute);
148                                 osip_free(tmproute);
149                         }
150                 }
151                 osip_route_free(rt);
152         }
153 }
154
155 SalOp * sal_op_new(Sal *sal){
156         SalOp *op=ms_new(SalOp,1);
157         __sal_op_init(op,sal);
158         op->cid=op->did=op->tid=op->rid=op->nid=op->sid=-1;
159         op->result=NULL;
160         op->supports_session_timers=FALSE;
161         op->sdp_offering=TRUE;
162         op->pending_auth=NULL;
163         op->sdp_answer=NULL;
164         op->reinvite=FALSE;
165         op->call_id=NULL;
166         op->replaces=NULL;
167         op->referred_by=NULL;
168         op->masquerade_via=FALSE;
169         op->auto_answer_asked=FALSE;
170         op->auth_info=NULL;
171         return op;
172 }
173
174 bool_t sal_call_autoanswer_asked(SalOp *op)
175 {
176         return op->auto_answer_asked;
177 }
178
179 void sal_op_release(SalOp *op){
180         if (op->sdp_answer)
181                 sdp_message_free(op->sdp_answer);
182         if (op->pending_auth)
183                 eXosip_event_free(op->pending_auth);
184         if (op->rid!=-1){
185                 sal_remove_register(op->base.root,op->rid);
186         }
187         if (op->cid!=-1){
188                 ms_message("Cleaning cid %i",op->cid);
189                 sal_remove_call(op->base.root,op);
190         }
191         if (op->sid!=-1){
192                 sal_remove_out_subscribe(op->base.root,op);
193         }
194         if (op->nid!=-1){
195                 sal_remove_in_subscribe(op->base.root,op);
196                 if (op->call_id)
197                         osip_call_id_free(op->call_id);
198                 op->call_id=NULL;
199         }
200         if (op->pending_auth){
201                 sal_remove_pending_auth(op->base.root,op);
202         }
203         if (op->result)
204                 sal_media_description_unref(op->result);
205         if (op->call_id){
206                 sal_remove_other(op->base.root,op);
207                 osip_call_id_free(op->call_id);
208         }
209         if (op->replaces){
210                 ms_free(op->replaces);
211         }
212         if (op->referred_by){
213                 ms_free(op->referred_by);
214         }
215         if (op->auth_info) {
216                 sal_auth_info_delete(op->auth_info);
217         }
218         __sal_op_free(op);
219 }
220
221 static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
222         int ortp_level=ORTP_DEBUG;
223         switch(level){
224                 case OSIP_INFO1:
225                 case OSIP_INFO2:
226                 case OSIP_INFO3:
227                 case OSIP_INFO4:
228                         ortp_level=ORTP_MESSAGE;
229                         break;
230                 case OSIP_WARNING:
231                         ortp_level=ORTP_WARNING;
232                         break;
233                 case OSIP_ERROR:
234                 case OSIP_BUG:
235                         ortp_level=ORTP_ERROR;
236                         break;
237                 case OSIP_FATAL:
238                         ortp_level=ORTP_FATAL;
239                         break;
240                 case END_TRACE_LEVEL:
241                         break;
242         }
243         if (ortp_log_level_enabled(level)){
244                 int len=strlen(chfr);
245                 char *chfrdup=ortp_strdup(chfr);
246                 /*need to remove endline*/
247                 if (len>1){
248                         if (chfrdup[len-1]=='\n')
249                                 chfrdup[len-1]='\0';
250                         if (chfrdup[len-2]=='\r')
251                                 chfrdup[len-2]='\0';
252                 }
253                 ortp_logv(ortp_level,chfrdup,ap);
254                 ortp_free(chfrdup);
255         }
256 }
257
258
259 Sal * sal_init(){
260         static bool_t firsttime=TRUE;
261         Sal *sal;
262         if (firsttime){
263                 osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
264                 firsttime=FALSE;
265         }
266         eXosip_init();
267         sal=ms_new0(Sal,1);
268         sal->keepalive_period=30;
269         sal->double_reg=TRUE;
270         return sal;
271 }
272
273 void sal_uninit(Sal* sal){
274         eXosip_quit();
275         ms_free(sal);
276 }
277
278 void sal_set_user_pointer(Sal *sal, void *user_data){
279         sal->up=user_data;
280 }
281
282 void *sal_get_user_pointer(const Sal *sal){
283         return sal->up;
284 }
285
286 static void unimplemented_stub(){
287         ms_warning("Unimplemented SAL callback");
288 }
289
290 void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
291         memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
292         if (ctx->callbacks.call_received==NULL) 
293                 ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
294         if (ctx->callbacks.call_ringing==NULL) 
295                 ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
296         if (ctx->callbacks.call_accepted==NULL) 
297                 ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
298         if (ctx->callbacks.call_failure==NULL) 
299                 ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
300         if (ctx->callbacks.call_terminated==NULL) 
301                 ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
302         if (ctx->callbacks.call_released==NULL)
303                 ctx->callbacks.call_released=(SalOnCallReleased)unimplemented_stub;
304         if (ctx->callbacks.call_updating==NULL) 
305                 ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
306         if (ctx->callbacks.auth_requested==NULL) 
307                 ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
308         if (ctx->callbacks.auth_success==NULL) 
309                 ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
310         if (ctx->callbacks.register_success==NULL) 
311                 ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
312         if (ctx->callbacks.register_failure==NULL) 
313                 ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
314         if (ctx->callbacks.dtmf_received==NULL) 
315                 ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
316         if (ctx->callbacks.notify==NULL)
317                 ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
318         if (ctx->callbacks.notify_presence==NULL)
319                 ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
320         if (ctx->callbacks.subscribe_received==NULL)
321                 ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
322         if (ctx->callbacks.text_received==NULL)
323                 ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
324         if (ctx->callbacks.internal_message==NULL)
325                 ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
326         if (ctx->callbacks.ping_reply==NULL)
327                 ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
328 }
329
330 int sal_unlisten_ports(Sal *ctx){
331         if (ctx->running){
332                 eXosip_quit();
333                 eXosip_init();
334                 ctx->running=FALSE;
335         }
336         return 0;
337 }
338
339 int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
340         int err;
341         bool_t ipv6;
342         int proto=IPPROTO_UDP;
343         int keepalive = ctx->keepalive_period;
344         
345         switch (tr) {
346         case SalTransportDatagram:
347                 proto=IPPROTO_UDP;
348                 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &keepalive);      
349                 break;
350         case SalTransportStream:
351                 proto= IPPROTO_TCP;
352                         keepalive=-1;   
353                 eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE,&keepalive);       
354                 break;
355         default:
356                 ms_warning("unexpected proto, using datagram");
357         }
358
359         err=0;
360         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
361                                         version of eXosip, which is not the case*/
362         /*see if it looks like an IPv6 address*/
363         ipv6=strchr(addr,':')!=NULL;
364         eXosip_enable_ipv6(ipv6);
365
366         if (is_secure){
367                 ms_fatal("SIP over TLS or DTLS is not supported yet.");
368                 return -1;
369         }
370         err=eXosip_listen_addr(proto, addr, port, ipv6 ?  PF_INET6 : PF_INET, 0);
371 #ifdef HAVE_EXOSIP_GET_SOCKET
372         ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
373 #endif
374         
375         ctx->running=TRUE;
376         return err;
377 }
378
379 ortp_socket_t sal_get_socket(Sal *ctx){
380 #ifdef HAVE_EXOSIP_GET_SOCKET
381         return eXosip_get_socket(IPPROTO_UDP);
382 #else
383         ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
384         return -1;
385 #endif
386 }
387
388 void sal_set_user_agent(Sal *ctx, const char *user_agent){
389         eXosip_set_user_agent(user_agent);
390 }
391
392 void sal_use_session_timers(Sal *ctx, int expires){
393         ctx->session_expires=expires;
394 }
395
396 void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
397         ctx->one_matching_codec=one_matching_codec;
398 }
399
400 MSList *sal_get_pending_auths(Sal *sal){
401         return ms_list_copy(sal->pending_auths);
402 }
403
404 void sal_use_double_registrations(Sal *ctx, bool_t enabled){
405         ctx->double_reg=enabled;
406 }
407
408 static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
409         osip_via_t *via=NULL;
410         osip_generic_param_t *param=NULL;
411         const char *rport=NULL;
412
413         *rportval=5060;
414         *received=NULL;
415         osip_message_get_via(msg,0,&via);
416         if (!via) return -1;
417
418         /* it is useless to do that with tcp since client socket might have a different port
419                 than the server socket.
420         */
421         if (strcasecmp(via->protocol,"tcp")==0) return -1;
422         
423         if (via->port && via->port[0]!='\0')
424                 *rportval=atoi(via->port);
425         
426         osip_via_param_get_byname(via,"rport",&param);
427         if (param) {
428                 rport=param->gvalue;
429                 if (rport && rport[0]!='\0') *rportval=atoi(rport);
430                 else *rportval=5060;
431                 *received=via->host;
432         }
433         param=NULL;
434         osip_via_param_get_byname(via,"received",&param);
435         if (param) *received=param->gvalue;
436
437         if (rport==NULL && *received==NULL) return -1;
438         return 0;
439 }
440
441 static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
442         int sdplen;
443         char clen[10];
444         char *sdp=NULL;
445         sdp_message_to_str(msg,&sdp);
446         sdplen=strlen(sdp);
447         snprintf(clen,sizeof(clen),"%i",sdplen);
448         osip_message_set_body(sip,sdp,sdplen);
449         osip_message_set_content_type(sip,"application/sdp");
450         osip_message_set_content_length(sip,clen);
451         osip_free(sdp);
452 }
453
454 static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
455         sdp_message_t *msg=media_description_to_sdp(desc);
456         if (msg==NULL) {
457                 ms_error("Fail to print sdp message !");
458                 return;
459         }
460         set_sdp(sip,msg);
461         sdp_message_free(msg);
462 }
463
464 static void sdp_process(SalOp *h){
465         ms_message("Doing SDP offer/answer process");
466         if (h->result){
467                 sal_media_description_unref(h->result);
468         }
469         h->result=sal_media_description_new();
470         if (h->sdp_offering){   
471                 offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
472         }else{
473                 int i;
474                 offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result,h->base.root->one_matching_codec);
475                 h->sdp_answer=media_description_to_sdp(h->result);
476                 /*once we have generated the SDP answer, we modify the result description for processing by the upper layer.
477                  It should contains media parameters constraint from the remote offer, not our response*/
478                 strcpy(h->result->addr,h->base.remote_media->addr);
479                 h->result->bandwidth=h->base.remote_media->bandwidth;
480                 for(i=0;i<h->result->nstreams;++i){
481                         if (h->result->streams[i].port>0){
482                                 strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
483                                 h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
484                                 h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
485                                 h->result->streams[i].port=h->base.remote_media->streams[i].port;
486                         }
487                 }
488         }
489         
490 }
491
492 int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
493         if (desc)
494                 sal_media_description_ref(desc);
495         if (h->base.local_media)
496                 sal_media_description_unref(h->base.local_media);
497         h->base.local_media=desc;
498         return 0;
499 }
500
501 int sal_call(SalOp *h, const char *from, const char *to){
502         int err;
503         osip_message_t *invite=NULL;
504         sal_op_set_from(h,from);
505         sal_op_set_to(h,to);
506         sal_exosip_fix_route(h);
507         err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
508         if (err!=0){
509                 ms_error("Could not create call.");
510                 return -1;
511         }
512         osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
513         if (h->base.contact){
514                 _osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
515                 osip_message_set_contact(invite,h->base.contact);
516         }
517         if (h->base.root->session_expires!=0){
518                 osip_message_set_header(invite, "Session-expires", "200");
519                 osip_message_set_supported(invite, "timer");
520         }
521         if (h->base.local_media){
522                 h->sdp_offering=TRUE;
523                 set_sdp_from_desc(invite,h->base.local_media);
524         }else h->sdp_offering=FALSE;
525         if (h->replaces){
526                 osip_message_set_header(invite,"Replaces",h->replaces);
527                 if (h->referred_by)
528                         osip_message_set_header(invite,"Referred-By",h->referred_by);
529         }
530         
531         eXosip_lock();
532         err=eXosip_call_send_initial_invite(invite);
533         eXosip_unlock();
534         h->cid=err;
535         if (err<0){
536                 ms_error("Fail to send invite !");
537                 return -1;
538         }else{
539                 sal_add_call(h->base.root,h);
540         }
541         return 0;
542 }
543
544 int sal_call_notify_ringing(SalOp *h, bool_t early_media){
545         osip_message_t *msg;
546         int err;
547         
548         /*if early media send also 180 and 183 */
549         if (early_media && h->sdp_answer){
550                 msg=NULL;
551                 eXosip_lock();
552                 err=eXosip_call_build_answer(h->tid,180,&msg);
553                 if (msg){
554                         set_sdp(msg,h->sdp_answer);
555                         eXosip_call_send_answer(h->tid,180,msg);
556                 }
557                 msg=NULL;
558                 err=eXosip_call_build_answer(h->tid,183,&msg);
559                 if (msg){
560                         set_sdp(msg,h->sdp_answer);
561                         eXosip_call_send_answer(h->tid,183,msg);
562                 }
563                 eXosip_unlock();
564         }else{
565                 eXosip_lock();
566                 eXosip_call_send_answer(h->tid,180,NULL);
567                 eXosip_unlock();
568         }
569         return 0;
570 }
571
572 int sal_call_accept(SalOp * h){
573         osip_message_t *msg;
574         const char *contact=sal_op_get_contact(h);
575         /* sends a 200 OK */
576         int err=eXosip_call_build_answer(h->tid,200,&msg);
577         if (err<0 || msg==NULL){
578                 ms_error("Fail to build answer for call: err=%i",err);
579                 return -1;
580         }
581         if (h->base.root->session_expires!=0){
582                 if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
583         }
584
585         if (contact) {
586                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
587                 osip_message_set_contact(msg,contact);
588         }
589         
590         if (h->base.local_media){
591                 /*this is the case where we received an invite without SDP*/
592                 if (h->sdp_offering) {
593                         set_sdp_from_desc(msg,h->base.local_media);
594                 }else{
595                         if (h->sdp_answer){
596                                 set_sdp(msg,h->sdp_answer);
597                                 sdp_message_free(h->sdp_answer);
598                                 h->sdp_answer=NULL;
599                         }
600                 }
601         }else{
602                 ms_error("You are accepting a call but not defined any media capabilities !");
603         }
604         eXosip_call_send_answer(h->tid,200,msg);
605         return 0;
606 }
607
608 int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
609         if (reason==SalReasonBusy){
610                 eXosip_lock();
611                 eXosip_call_send_answer(h->tid,486,NULL);
612                 eXosip_unlock();
613         }
614         else if (reason==SalReasonTemporarilyUnavailable){
615                 eXosip_lock();
616                 eXosip_call_send_answer(h->tid,480,NULL);
617                 eXosip_unlock();
618         }else if (reason==SalReasonDoNotDisturb){
619                 eXosip_lock();
620                 eXosip_call_send_answer(h->tid,600,NULL);
621                 eXosip_unlock();
622         }else if (reason==SalReasonMedia){
623                 eXosip_lock();
624                 eXosip_call_send_answer(h->tid,415,NULL);
625                 eXosip_unlock();
626         }else if (redirect!=NULL && reason==SalReasonRedirect){
627                 osip_message_t *msg;
628                 int code;
629                 if (strstr(redirect,"sip:")!=0) code=302;
630                 else code=380;
631                 eXosip_lock();
632                 eXosip_call_build_answer(h->tid,code,&msg);
633                 osip_message_set_contact(msg,redirect);
634                 eXosip_call_send_answer(h->tid,code,msg);
635                 eXosip_unlock();
636         }else sal_call_terminate(h);
637         return 0;
638 }
639
640 SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
641         if (h->base.local_media && h->base.remote_media && !h->result){
642                 sdp_process(h);
643         }
644         return h->result;
645 }
646
647 int sal_call_set_referer(SalOp *h, SalOp *refered_call){
648         if (refered_call->replaces)
649                 h->replaces=ms_strdup(refered_call->replaces);
650         if (refered_call->referred_by)
651                 h->referred_by=ms_strdup(refered_call->referred_by);
652         return 0;
653 }
654
655 int sal_ping(SalOp *op, const char *from, const char *to){
656         osip_message_t *options=NULL;
657         
658         sal_op_set_from(op,from);
659         sal_op_set_to(op,to);
660         /*bug here: eXosip2 does not honor the route argument*/
661         eXosip_options_build_request (&options, sal_op_get_to(op),
662                         sal_op_get_from(op),sal_op_get_route(op));
663         if (options){
664                 if (op->base.root->session_expires!=0){
665                         osip_message_set_header(options, "Session-expires", "200");
666                         osip_message_set_supported(options, "timer");
667                 }
668                 sal_add_other(sal_op_get_sal(op),op,options);
669                 return eXosip_options_send_request(options);
670         }
671         return -1;
672 }
673
674 int sal_call_accept_refer(SalOp *op){
675         osip_message_t *msg=NULL;
676         int err=0;
677         eXosip_lock();
678         err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
679         if(msg != NULL)
680         {
681                 osip_message_set_header(msg,(const char *)"event","refer");
682                 osip_message_set_content_type(msg,"message/sipfrag");
683                 osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
684                 eXosip_call_send_request(op->did,msg);
685         }
686         else
687         {
688                 ms_error("could not get a notify built\n");
689         }
690         eXosip_unlock();
691         return err;
692 }
693
694 int sal_call_refer(SalOp *h, const char *refer_to){
695         osip_message_t *msg=NULL;
696         int err=0;
697         eXosip_lock();
698         eXosip_call_build_refer(h->did,refer_to, &msg);
699         if (msg) err=eXosip_call_send_request(h->did, msg);
700         else err=-1;
701         eXosip_unlock();
702         return err;
703 }
704
705 int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
706         osip_message_t *msg=NULL;
707         char referto[256]={0};
708         int err=0;
709         eXosip_lock();
710         if (eXosip_call_get_referto(other_call_h->did,referto,sizeof(referto)-1)!=0){
711                 ms_error("eXosip_call_get_referto() failed for did=%i",other_call_h->did);
712                 eXosip_unlock();
713                 return -1;
714         }
715         eXosip_call_build_refer(h->did,referto, &msg);
716         osip_message_set_header(msg,"Referred-By",h->base.from);
717         if (msg) err=eXosip_call_send_request(h->did, msg);
718         else err=-1;
719         eXosip_unlock();
720         return err;
721 }
722
723 SalOp *sal_call_get_replaces(SalOp *h){
724         if (h->replaces!=NULL){
725                 int cid;
726                 eXosip_lock();
727                 cid=eXosip_call_find_by_replaces(h->replaces);
728                 eXosip_unlock();
729                 if (cid>0){
730                         SalOp *ret=sal_find_call(h->base.root,cid);
731                         return ret;
732                 }
733         }
734         return NULL;
735 }
736
737 int sal_call_send_dtmf(SalOp *h, char dtmf){
738         osip_message_t *msg=NULL;
739         char dtmf_body[128];
740         char clen[10];
741
742         eXosip_lock();
743         eXosip_call_build_info(h->did,&msg);
744         if (msg){
745                 snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
746                 osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
747                 osip_message_set_content_type(msg,"application/dtmf-relay");
748                 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
749                 osip_message_set_content_length(msg,clen);              
750                 eXosip_call_send_request(h->did,msg);
751         }
752         eXosip_unlock();
753         return 0;
754 }
755
756 static void push_auth_to_exosip(const SalAuthInfo *info){
757         const char *userid;
758         if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
759         else userid=info->userid;
760         ms_message("Authentication info for username [%s], id[%s], realm [%s] added to eXosip", info->username,userid, info->realm);
761         eXosip_add_authentication_info (info->username,userid,
762                                   info->password, NULL,info->realm);
763 }
764 /*
765  * Just for symmetry ;-)
766  */
767 static void pop_auth_from_exosip() {
768         eXosip_clear_authentication_info();
769 }
770
771 int sal_call_terminate(SalOp *h){
772         int err;
773         if (h->auth_info) push_auth_to_exosip(h->auth_info);
774         eXosip_lock();
775         err=eXosip_call_terminate(h->cid,h->did);
776         eXosip_unlock();
777         pop_auth_from_exosip();
778         if (err!=0){
779                 ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
780         }
781         return 0;
782 }
783
784 void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
785         if (h->pending_auth){
786                 push_auth_to_exosip(info);
787                 eXosip_lock();
788                 eXosip_default_action(h->pending_auth);
789                 eXosip_unlock();
790                 ms_message("eXosip_default_action() done");
791                 pop_auth_from_exosip();
792                 eXosip_event_free(h->pending_auth);
793                 sal_remove_pending_auth(sal_op_get_sal(h),h);
794                 h->pending_auth=NULL;
795                 if (h->auth_info) sal_auth_info_delete(h->auth_info); /*if already exist*/
796                 h->auth_info=sal_auth_info_clone(info); /*store auth info for subsequent request*/
797         }
798 }
799 void sal_op_cancel_authentication(SalOp *h) {
800         if (h->rid >0) {
801                 sal_op_get_sal(h)->callbacks.register_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"));
802         } else if (h->cid >0) {
803                 sal_op_get_sal(h)->callbacks.call_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"),0);
804         } else {
805                 ms_warning("Auth failure not handled");
806         }
807
808 }
809 static void set_network_origin(SalOp *op, osip_message_t *req){
810         const char *received=NULL;
811         int rport=5060;
812         char origin[64];
813         if (extract_received_rport(req,&received,&rport)!=0){
814                 osip_via_t *via=NULL;
815                 char *tmp;
816                 osip_message_get_via(req,0,&via);
817                 received=osip_via_get_host(via);
818                 tmp=osip_via_get_port(via);
819                 if (tmp) rport=atoi(tmp);
820         }
821         snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
822         __sal_op_set_network_origin(op,origin);
823 }
824
825 static void set_remote_ua(SalOp* op, osip_message_t *req){
826         if (op->base.remote_ua==NULL){
827                 osip_header_t *h=NULL;
828                 osip_message_get_user_agent(req,0,&h);
829                 if (h){
830                         op->base.remote_ua=ms_strdup(h->hvalue);
831                 }
832         }
833 }
834
835 static void set_replaces(SalOp *op, osip_message_t *req){
836         osip_header_t *h=NULL;
837
838         if (op->replaces){
839                 ms_free(op->replaces);
840                 op->replaces=NULL;
841         }
842         osip_message_header_get_byname(req,"replaces",0,&h);
843         if (h){
844                 if (h->hvalue && h->hvalue[0]!='\0'){
845                         op->replaces=ms_strdup(h->hvalue);
846                 }
847         }
848 }
849
850 static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
851         if (ev->cid>0){
852                 return sal_find_call(sal,ev->cid);
853         }
854         if (ev->rid>0){
855                 return sal_find_register(sal,ev->rid);
856         }
857         if (ev->sid>0){
858                 return sal_find_out_subscribe(sal,ev->sid);
859         }
860         if (ev->nid>0){
861                 return sal_find_in_subscribe(sal,ev->nid);
862         }
863         if (ev->response) return sal_find_other(sal,ev->response);
864         return NULL;
865 }
866
867 static void inc_new_call(Sal *sal, eXosip_event_t *ev){
868         SalOp *op=sal_op_new(sal);
869         osip_from_t *from,*to;
870         osip_call_info_t *call_info;
871         char *tmp;
872         sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
873
874         set_network_origin(op,ev->request);
875         set_remote_ua(op,ev->request);
876         set_replaces(op,ev->request);
877         
878         if (sdp){
879                 op->sdp_offering=FALSE;
880                 op->base.remote_media=sal_media_description_new();
881                 sdp_to_media_description(sdp,op->base.remote_media);
882                 sdp_message_free(sdp);
883         }else op->sdp_offering=TRUE;
884
885         from=osip_message_get_from(ev->request);
886         to=osip_message_get_to(ev->request);
887         osip_from_to_str(from,&tmp);
888         sal_op_set_from(op,tmp);
889         osip_free(tmp);
890         osip_from_to_str(to,&tmp);
891         sal_op_set_to(op,tmp);
892         osip_free(tmp);
893
894         osip_message_get_call_info(ev->request,0,&call_info);
895         if(call_info)
896         {
897                 osip_call_info_to_str(call_info,&tmp);
898                 if( strstr(tmp,"answer-after=") != NULL)
899                 {
900                         op->auto_answer_asked=TRUE;
901                         ms_message("The caller asked to automatically answer the call(Emergency?)\n");
902                 }
903                 osip_free(tmp);
904         }
905
906         op->tid=ev->tid;
907         op->cid=ev->cid;
908         op->did=ev->did;
909         
910         sal_add_call(op->base.root,op);
911         sal->callbacks.call_received(op);
912 }
913
914 static void handle_reinvite(Sal *sal,  eXosip_event_t *ev){
915         SalOp *op=find_op(sal,ev);
916         sdp_message_t *sdp;
917         osip_message_t *msg=NULL;
918
919         if (op==NULL) {
920                 ms_warning("Reinvite for non-existing operation !");
921                 return;
922         }
923         op->reinvite=TRUE;
924         op->tid=ev->tid;
925         sdp=eXosip_get_sdp_info(ev->request);
926         if (op->base.remote_media){
927                 sal_media_description_unref(op->base.remote_media);
928                 op->base.remote_media=NULL;
929         }
930         if (op->result){
931                 sal_media_description_unref(op->result);
932                 op->result=NULL;
933         }
934         if (sdp){
935                 op->sdp_offering=FALSE;
936                 op->base.remote_media=sal_media_description_new();
937                 sdp_to_media_description(sdp,op->base.remote_media);
938                 sdp_message_free(sdp);
939                 sal->callbacks.call_updating(op);
940         }else {
941                 op->sdp_offering=TRUE;
942                 eXosip_lock();
943                 eXosip_call_build_answer(ev->tid,200,&msg);
944                 if (msg!=NULL){
945                         set_sdp_from_desc(msg,op->base.local_media);
946                         eXosip_call_send_answer(ev->tid,200,msg);
947                 }
948                 eXosip_unlock();
949         }
950         
951 }
952
953 static void handle_ack(Sal *sal,  eXosip_event_t *ev){
954         SalOp *op=find_op(sal,ev);
955         sdp_message_t *sdp;
956
957         if (op==NULL) {
958                 ms_warning("ack for non-existing call !");
959                 return;
960         }
961         sdp=eXosip_get_sdp_info(ev->ack);
962         if (sdp){
963                 op->base.remote_media=sal_media_description_new();
964                 sdp_to_media_description(sdp,op->base.remote_media);
965                 sdp_process(op);
966                 sdp_message_free(sdp);
967         }
968         if (op->reinvite){
969                 if (sdp) sal->callbacks.call_updating(op);
970                 op->reinvite=FALSE;
971         }else{
972                 sal->callbacks.call_ack(op);
973         }
974 }
975
976 static void update_contact_from_response(SalOp *op, osip_message_t *response){
977         const char *received;
978         int rport;
979         if (extract_received_rport(response,&received,&rport)==0){
980                 const char *contact=sal_op_get_contact(op);
981                 if (!contact){
982                         /*no contact given yet, use from instead*/
983                         contact=sal_op_get_from(op);
984                 }
985                 if (contact){
986                         SalAddress *addr=sal_address_new(contact);
987                         char *tmp;
988                         sal_address_set_domain(addr,received);
989                         sal_address_set_port_int(addr,rport);
990                         tmp=sal_address_as_string(addr);
991                         ms_message("Contact address updated to %s for this dialog",tmp);
992                         sal_op_set_contact(op,tmp);
993                         sal_address_destroy(addr);
994                         ms_free(tmp);
995                 }
996         }
997 }
998
999 static int call_proceeding(Sal *sal, eXosip_event_t *ev){
1000         SalOp *op=find_op(sal,ev);
1001         
1002         if (op==NULL) {
1003                 ms_warning("This call has been canceled.");
1004                 eXosip_lock();
1005                 eXosip_call_terminate(ev->cid,ev->did);
1006                 eXosip_unlock();
1007                 return -1;
1008         }
1009         if (ev->did>0)
1010                 op->did=ev->did;
1011         op->tid=ev->tid;
1012         
1013         /* update contact if received and rport are set by the server
1014          note: will only be used by remote for next INVITE, if any...*/
1015         update_contact_from_response(op,ev->response);
1016         return 0;
1017 }
1018
1019 static void call_ringing(Sal *sal, eXosip_event_t *ev){
1020         sdp_message_t *sdp;
1021         SalOp *op=find_op(sal,ev);
1022         if (call_proceeding(sal, ev)==-1) return;
1023
1024         set_remote_ua(op,ev->response);
1025         sdp=eXosip_get_sdp_info(ev->response);
1026         if (sdp){
1027                 op->base.remote_media=sal_media_description_new();
1028                 sdp_to_media_description(sdp,op->base.remote_media);
1029                 sdp_message_free(sdp);
1030                 if (op->base.local_media) sdp_process(op);
1031         }
1032         sal->callbacks.call_ringing(op);
1033 }
1034
1035 static void call_accepted(Sal *sal, eXosip_event_t *ev){
1036         sdp_message_t *sdp;
1037         osip_message_t *msg=NULL;
1038         SalOp *op=find_op(sal,ev);
1039         const char *contact;
1040         
1041         if (op==NULL){
1042                 ms_error("A closed call is accepted ?");
1043                 return;
1044         }
1045
1046         op->did=ev->did;
1047         set_remote_ua(op,ev->response);
1048
1049         sdp=eXosip_get_sdp_info(ev->response);
1050         if (sdp){
1051                 op->base.remote_media=sal_media_description_new();
1052                 sdp_to_media_description(sdp,op->base.remote_media);
1053                 sdp_message_free(sdp);
1054                 if (op->base.local_media) sdp_process(op);
1055         }
1056         eXosip_call_build_ack(ev->did,&msg);
1057         contact=sal_op_get_contact(op);
1058         if (contact) {
1059                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1060                 osip_message_set_contact(msg,contact);
1061         }
1062         if (op->sdp_answer){
1063                 set_sdp(msg,op->sdp_answer);
1064                 sdp_message_free(op->sdp_answer);
1065                 op->sdp_answer=NULL;
1066         }
1067         eXosip_call_send_ack(ev->did,msg);
1068         sal->callbacks.call_accepted(op);
1069 }
1070
1071 static void call_terminated(Sal *sal, eXosip_event_t *ev){
1072         char *from=NULL;
1073         SalOp *op=find_op(sal,ev);
1074         if (op==NULL){
1075                 ms_warning("Call terminated for already closed call ?");
1076                 return;
1077         }
1078         if (ev->request){
1079                 osip_from_to_str(ev->request->from,&from);
1080         }
1081         sal->callbacks.call_terminated(op,from!=NULL ? from : sal_op_get_from(op));
1082         if (from) osip_free(from);
1083 }
1084
1085 static void call_released(Sal *sal, eXosip_event_t *ev){
1086         SalOp *op=find_op(sal,ev);
1087         if (op==NULL){
1088                 ms_warning("No op associated to this call_released()");
1089                 return;
1090         }
1091         if (ev->response==NULL){
1092                 /* no response received so far */
1093                 call_failure(sal,ev);
1094         }
1095         sal->callbacks.call_released(op);
1096 }
1097
1098 static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
1099         const char *prx_realm=NULL,*www_realm=NULL;
1100         osip_proxy_authenticate_t *prx_auth;
1101         osip_www_authenticate_t *www_auth;
1102         
1103         *username=osip_uri_get_username(resp->from->url);
1104         prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
1105         www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
1106         if (prx_auth!=NULL)
1107                 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
1108         if (www_auth!=NULL)
1109                 www_realm=osip_www_authenticate_get_realm(www_auth);
1110
1111         if (prx_realm){
1112                 *realm=prx_realm;
1113         }else if (www_realm){
1114                 *realm=www_realm;
1115         }else{
1116                 return -1;
1117         }
1118         return 0;
1119 }
1120
1121 static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
1122         osip_authorization_t *auth=NULL;
1123         osip_proxy_authorization_t *prx_auth=NULL;
1124         
1125         *username=osip_uri_get_username(msg->from->url);
1126         osip_message_get_authorization(msg, 0, &auth);
1127         if (auth){
1128                 *realm=osip_authorization_get_realm(auth);
1129                 return 0;
1130         }
1131         osip_message_get_proxy_authorization(msg,0,&prx_auth);
1132         if (prx_auth){
1133                 *realm=osip_proxy_authorization_get_realm(prx_auth);
1134                 return 0;
1135         }
1136         return -1;
1137 }
1138
1139 static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
1140         if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
1141         if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
1142         return -1;
1143 }
1144
1145 int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
1146         if (op->pending_auth){
1147                 return get_auth_data(op->pending_auth,realm,username);
1148         }
1149         return -1;
1150 }
1151
1152 static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
1153         SalOp *op;
1154         const char *username,*realm;
1155         op=find_op(sal,ev);
1156         if (op==NULL){
1157                 ms_warning("No operation associated with this authentication !");
1158                 return TRUE;
1159         }
1160         if (get_auth_data(ev,&realm,&username)==0){
1161                 if (op->pending_auth!=NULL)
1162                         eXosip_event_free(op->pending_auth);
1163                 op->pending_auth=ev;
1164                 sal_add_pending_auth (sal,op);
1165                 sal->callbacks.auth_requested(op,realm,username);
1166                 return FALSE;
1167         }
1168         return TRUE;
1169 }
1170
1171 static void authentication_ok(Sal *sal, eXosip_event_t *ev){
1172         SalOp *op;
1173         const char *username,*realm;
1174         op=find_op(sal,ev);
1175         if (op==NULL){
1176                 ms_warning("No operation associated with this authentication_ok!");
1177                 return ;
1178         }
1179         if (get_auth_data(ev,&realm,&username)==0){
1180                 sal->callbacks.auth_success(op,realm,username);
1181         }
1182 }
1183
1184 static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
1185         SalOp *op;
1186         int code=0;
1187         char* computedReason=NULL;
1188         const char *reason=NULL;
1189         SalError error=SalErrorUnknown;
1190         SalReason sr=SalReasonUnknown;
1191         
1192
1193         op=(SalOp*)find_op(sal,ev);
1194
1195         if (op==NULL) {
1196                 ms_warning("Call failure reported for a closed call, ignored.");
1197                 return TRUE;
1198         }
1199
1200         if (ev->response){
1201                 code=osip_message_get_status_code(ev->response);
1202                 reason=osip_message_get_reason_phrase(ev->response);
1203                 osip_header_t *h=NULL;
1204                 if (!osip_message_header_get_byname(    ev->response
1205                                                                                         ,"Reason"
1206                                                                                         ,0
1207                                                                                         ,&h)) {
1208                         computedReason = ms_strdup_printf("%s %s",reason,osip_header_get_value(h));
1209                         reason = computedReason;
1210
1211                 }
1212         }
1213         switch(code)
1214         {
1215                 case 401:
1216                 case 407:
1217                         return process_authentication(sal,ev);
1218                         break;
1219                 case 400:
1220                         error=SalErrorUnknown;
1221                 break;
1222                 case 404:
1223                         error=SalErrorFailure;
1224                         sr=SalReasonNotFound;
1225                 break;
1226                 case 415:
1227                         error=SalErrorFailure;
1228                         sr=SalReasonMedia;
1229                 break;
1230                 case 422:
1231                         eXosip_default_action(ev);
1232                         return TRUE;
1233                 break;
1234                 case 480:
1235                         error=SalErrorFailure;
1236                         sr=SalReasonTemporarilyUnavailable;
1237                 case 486:
1238                         error=SalErrorFailure;
1239                         sr=SalReasonBusy;
1240                 break;
1241                 case 487:
1242                 break;
1243                 case 600:
1244                         error=SalErrorFailure;
1245                         sr=SalReasonDoNotDisturb;
1246                 break;
1247                 case 603:
1248                         error=SalErrorFailure;
1249                         sr=SalReasonDeclined;
1250                 break;
1251                 default:
1252                         if (code>0){
1253                                 error=SalErrorFailure;
1254                                 sr=SalReasonUnknown;
1255                         }else error=SalErrorNoResponse;
1256         }
1257         sal->callbacks.call_failure(op,error,sr,reason,code);
1258         if (computedReason != NULL){
1259                 ms_free(computedReason);
1260         }
1261         return TRUE;
1262 }
1263
1264 /* Request remote side to send us VFU */
1265 void sal_call_send_vfu_request(SalOp *h){
1266         osip_message_t *msg=NULL;
1267         char info_body[] =
1268                         "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1269                          "<media_control>"
1270                          "  <vc_primitive>"
1271                          "    <to_encoder>"
1272                          "      <picture_fast_update></picture_fast_update>"
1273                          "    </to_encoder>"
1274                          "  </vc_primitive>"
1275                          "</media_control>";
1276
1277         char clen[10];
1278
1279         eXosip_lock();
1280         eXosip_call_build_info(h->did,&msg);
1281         if (msg){
1282                 osip_message_set_body(msg,info_body,strlen(info_body));
1283                 osip_message_set_content_type(msg,"application/media_control+xml");
1284                 snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(info_body));
1285                 osip_message_set_content_length(msg,clen);
1286                 eXosip_call_send_request(h->did,msg);
1287                 ms_message("Sending VFU request !");
1288         }
1289         eXosip_unlock();
1290 }
1291
1292 static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1293         SalOp *op=find_op(sal,ev);
1294         osip_body_t *body=NULL;
1295
1296         if (op==NULL){
1297                 ms_warning("media control xml received without operation context!");
1298                 return ;
1299         }
1300         
1301         osip_message_get_body(ev->request,0,&body);
1302         if (body && body->body!=NULL &&
1303                 strstr(body->body,"picture_fast_update")){
1304                 osip_message_t *ans=NULL;
1305                 ms_message("Receiving VFU request !");
1306                 if (sal->callbacks.vfu_request){
1307                         sal->callbacks.vfu_request(op);
1308                         eXosip_call_build_answer(ev->tid,200,&ans);
1309                         if (ans)
1310                                 eXosip_call_send_answer(ev->tid,200,ans);
1311                         return;
1312                 }
1313         }
1314         /*in all other cases we must say it is not implemented.*/
1315         {
1316                 osip_message_t *ans=NULL;
1317                 eXosip_lock();
1318                 eXosip_call_build_answer(ev->tid,501,&ans);
1319                 if (ans)
1320                         eXosip_call_send_answer(ev->tid,501,ans);
1321                 eXosip_unlock();
1322         }
1323 }
1324
1325 static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1326         SalOp *op=find_op(sal,ev);
1327         osip_body_t *body=NULL;
1328
1329         if (op==NULL){
1330                 ms_warning("media dtmf relay received without operation context!");
1331                 return ;
1332         }
1333         
1334         osip_message_get_body(ev->request,0,&body);
1335         if (body && body->body!=NULL){
1336                 osip_message_t *ans=NULL;
1337                 const char *name=strstr(body->body,"Signal");
1338                 if (name==NULL) name=strstr(body->body,"signal");
1339                 if (name==NULL) {
1340                         ms_warning("Could not extract the dtmf name from the SIP INFO.");
1341                 }else{
1342                         char tmp[2];
1343                         name+=strlen("signal");
1344                         if (sscanf(name," = %1s",tmp)==1){
1345                                 ms_message("Receiving dtmf %s via SIP INFO.",tmp);
1346                                 if (sal->callbacks.dtmf_received != NULL)
1347                                         sal->callbacks.dtmf_received(op, tmp[0]);
1348                         }
1349                 }
1350                 eXosip_lock();
1351                 eXosip_call_build_answer(ev->tid,200,&ans);
1352                 if (ans)
1353                         eXosip_call_send_answer(ev->tid,200,ans);
1354                 eXosip_unlock();
1355         }
1356 }
1357
1358 static void fill_options_answer(osip_message_t *options){
1359         osip_message_set_allow(options,"INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, SUBSCRIBE, NOTIFY, INFO");
1360         osip_message_set_accept(options,"application/sdp");
1361 }
1362
1363 static void process_refer(Sal *sal, SalOp *op, eXosip_event_t *ev){
1364         osip_header_t *h=NULL;
1365         osip_message_t *ans=NULL;
1366         ms_message("Receiving REFER request !");
1367         osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
1368
1369         if (h){
1370                 osip_from_t *from=NULL;
1371                 char *tmp;
1372                 osip_from_init(&from);
1373         
1374                 if (osip_from_parse(from,h->hvalue)==0){
1375                         if (op ){
1376                                 osip_uri_header_t *uh=NULL;
1377                                 osip_header_t *referred_by=NULL;
1378                                 osip_uri_header_get_byname(&from->url->url_headers,(char*)"Replaces",&uh);
1379                                 if (uh!=NULL && uh->gvalue && uh->gvalue[0]!='\0'){
1380                                         ms_message("Found replaces in Refer-To");
1381                                         if (op->replaces){
1382                                                 ms_free(op->replaces);
1383                                         }
1384                                         op->replaces=ms_strdup(uh->gvalue);
1385                                 }
1386                                 osip_message_header_get_byname(ev->request,"Referred-By",0,&referred_by);
1387                                 if (referred_by && referred_by->hvalue && referred_by->hvalue[0]!='\0'){
1388                                         if (op->referred_by)
1389                                                 ms_free(op->referred_by);
1390                                         op->referred_by=ms_strdup(referred_by->hvalue);
1391                                 }
1392                         }
1393                         osip_uri_header_freelist(&from->url->url_headers);
1394                         osip_from_to_str(from,&tmp);
1395                         sal->callbacks.refer_received(sal,op,tmp);
1396                         osip_free(tmp);
1397                         osip_from_free(from);
1398                 }
1399                 eXosip_lock();
1400                 eXosip_call_build_answer(ev->tid,202,&ans);
1401                 if (ans)
1402                         eXosip_call_send_answer(ev->tid,202,ans);
1403                 eXosip_unlock();
1404         }
1405         else
1406         {
1407                 ms_warning("cannot do anything with the refer without destination\n");
1408         }
1409 }
1410
1411 static void call_message_new(Sal *sal, eXosip_event_t *ev){
1412         osip_message_t *ans=NULL;
1413         if (ev->request){
1414                 if (MSG_IS_INFO(ev->request)){
1415                         osip_content_type_t *ct;
1416                         ct=osip_message_get_content_type(ev->request);
1417                         if (ct && ct->subtype){
1418                                 if (strcmp(ct->subtype,"media_control+xml")==0)
1419                                         process_media_control_xml(sal,ev);
1420                                 else if (strcmp(ct->subtype,"dtmf-relay")==0)
1421                                         process_dtmf_relay(sal,ev);
1422                                 else {
1423                                         ms_message("Unhandled SIP INFO.");
1424                                         /*send an "Not implemented" answer*/
1425                                         eXosip_lock();
1426                                         eXosip_call_build_answer(ev->tid,501,&ans);
1427                                         if (ans)
1428                                                 eXosip_call_send_answer(ev->tid,501,ans);
1429                                         eXosip_unlock();
1430                                 }
1431                         }else{
1432                                 /*empty SIP INFO, probably to test we are alive. Send an empty answer*/
1433                                 eXosip_lock();
1434                                 eXosip_call_build_answer(ev->tid,200,&ans);
1435                                 if (ans)
1436                                         eXosip_call_send_answer(ev->tid,200,ans);
1437                                 eXosip_unlock();
1438                         }
1439                 }else if(MSG_IS_MESSAGE(ev->request)){
1440                         /* SIP messages could be received into call */
1441                         text_received(sal, ev);
1442                         eXosip_lock();
1443                         eXosip_call_build_answer(ev->tid,200,&ans);
1444                         if (ans)
1445                                 eXosip_call_send_answer(ev->tid,200,ans);
1446                         eXosip_unlock();
1447                 }else if(MSG_IS_REFER(ev->request)){
1448                         SalOp *op=find_op(sal,ev);
1449                         
1450                         ms_message("Receiving REFER request !");
1451                         process_refer(sal,op,ev);
1452                 }else if(MSG_IS_NOTIFY(ev->request)){
1453                         osip_header_t *h=NULL;
1454                         char *from=NULL;
1455                         SalOp *op=find_op(sal,ev);
1456
1457                         ms_message("Receiving NOTIFY request !");
1458                         osip_from_to_str(ev->request->from,&from);
1459                         osip_message_header_get_byname(ev->request,"Event",0,&h);
1460                         if(h)
1461                                 sal->callbacks.notify(op,from,h->hvalue);
1462                         /*answer that we received the notify*/
1463                         eXosip_lock();
1464                         eXosip_call_build_answer(ev->tid,200,&ans);
1465                         if (ans)
1466                                 eXosip_call_send_answer(ev->tid,200,ans);
1467                         eXosip_unlock();
1468                         osip_free(from);
1469                 }else if (MSG_IS_OPTIONS(ev->request)){
1470                         eXosip_lock();
1471                         eXosip_call_build_answer(ev->tid,200,&ans);
1472                         if (ans){
1473                                 fill_options_answer(ans);
1474                                 eXosip_call_send_answer(ev->tid,200,ans);
1475                         }
1476                         eXosip_unlock();
1477                 }
1478         }else ms_warning("call_message_new: No request ?");
1479 }
1480
1481 static void inc_update(Sal *sal, eXosip_event_t *ev){
1482         osip_message_t *msg=NULL;
1483         ms_message("Processing incoming UPDATE");
1484         eXosip_lock();
1485         eXosip_message_build_answer(ev->tid,200,&msg);
1486         if (msg!=NULL)
1487                 eXosip_message_send_answer(ev->tid,200,msg);
1488         eXosip_unlock();
1489 }
1490
1491 static bool_t comes_from_local_if(osip_message_t *msg){
1492         osip_via_t *via=NULL;
1493         osip_message_get_via(msg,0,&via);
1494         if (via){
1495                 const char *host;
1496                 host=osip_via_get_host(via);
1497                 if (strcmp(host,"127.0.0.1")==0 || strcmp(host,"::1")==0){
1498                         osip_generic_param_t *param=NULL;
1499                         osip_via_param_get_byname(via,"received",&param);
1500                         if (param==NULL) return TRUE;
1501                         if (param->gvalue &&
1502                                 (strcmp(param->gvalue,"127.0.0.1")==0 || strcmp(param->gvalue,"::1")==0)){
1503                                 return TRUE;
1504                         }
1505                 }
1506         }
1507         return FALSE;
1508 }
1509
1510 static void text_received(Sal *sal, eXosip_event_t *ev){
1511         osip_body_t *body=NULL;
1512         char *from=NULL,*msg;
1513         
1514         osip_message_get_body(ev->request,0,&body);
1515         if (body==NULL){
1516                 ms_error("Could not get text message from SIP body");
1517                 return;
1518         }
1519         msg=body->body;
1520         osip_from_to_str(ev->request->from,&from);
1521         sal->callbacks.text_received(sal,from,msg);
1522         osip_free(from);
1523 }
1524
1525
1526
1527 static void other_request(Sal *sal, eXosip_event_t *ev){
1528         ms_message("in other_request");
1529         if (ev->request==NULL) return;
1530         if (strcmp(ev->request->sip_method,"MESSAGE")==0){
1531                 text_received(sal,ev);
1532                 eXosip_message_send_answer(ev->tid,200,NULL);
1533         }else if (strcmp(ev->request->sip_method,"OPTIONS")==0){
1534                 osip_message_t *options=NULL;
1535                 eXosip_options_build_answer(ev->tid,200,&options);
1536                 fill_options_answer(options);
1537                 eXosip_options_send_answer(ev->tid,200,options);
1538         }else if (strcmp(ev->request->sip_method,"WAKEUP")==0
1539                 && comes_from_local_if(ev->request)) {
1540                 eXosip_message_send_answer(ev->tid,200,NULL);
1541                 ms_message("Receiving WAKEUP request !");
1542                 sal->callbacks.internal_message(sal,"WAKEUP");
1543         }else if (strncmp(ev->request->sip_method, "REFER", 5) == 0){
1544                 ms_message("Receiving REFER request !");
1545                 if (comes_from_local_if(ev->request)) {
1546                         process_refer(sal,NULL,ev);
1547                 }else ms_warning("Ignored REFER not coming from this local loopback interface.");
1548         }else if (strncmp(ev->request->sip_method, "UPDATE", 6) == 0){
1549                 inc_update(sal,ev);
1550     }else {
1551                 char *tmp=NULL;
1552                 size_t msglen=0;
1553                 osip_message_to_str(ev->request,&tmp,&msglen);
1554                 if (tmp){
1555                         ms_message("Unsupported request received:\n%s",tmp);
1556                         osip_free(tmp);
1557                 }
1558                 /*answer with a 501 Not implemented*/
1559                 eXosip_message_send_answer(ev->tid,501,NULL);
1560         }
1561 }
1562
1563 static void masquerade_via(osip_message_t *msg, const char *ip, const char *port){
1564         osip_via_t *via=NULL;
1565         osip_message_get_via(msg,0,&via);
1566         if (via){
1567                 osip_free(via->port);
1568                 via->port=osip_strdup(port);
1569                 osip_free(via->host);
1570                 via->host=osip_strdup(ip);
1571         }
1572 }
1573
1574 static bool_t register_again_with_updated_contact(SalOp *op, osip_message_t *orig_request, osip_message_t *last_answer){
1575         osip_message_t *msg;
1576         const char *received;
1577         int rport;
1578         osip_contact_t *ctt=NULL;
1579         char *tmp;
1580         char port[20];
1581         SalAddress *addr;
1582         Sal *sal=op->base.root;
1583
1584         if (sal->double_reg==FALSE) return FALSE;
1585         
1586         if (extract_received_rport(last_answer,&received,&rport)==-1) return FALSE;
1587         osip_message_get_contact(orig_request,0,&ctt);
1588         if (strcmp(ctt->url->host,received)==0){
1589                 /*ip address matches, check ports*/
1590                 const char *contact_port=ctt->url->port;
1591                 if (contact_port==NULL || contact_port[0]=='\0')
1592                         contact_port="5060";
1593                 if (atoi(contact_port)==rport){
1594                         ms_message("Register has up to date contact, doing nothing.");
1595                         return FALSE;
1596                 }else ms_message("ports do not match, need to update the register (%s <> %i)", contact_port,rport);
1597         }
1598         eXosip_lock();
1599         msg=NULL;
1600         eXosip_register_build_register(op->rid,op->expires,&msg);
1601         if (msg==NULL){
1602                 eXosip_unlock();
1603                 ms_warning("Fail to create a contact updated register.");
1604                 return FALSE;
1605         }
1606         osip_message_get_contact(msg,0,&ctt);
1607         if (ctt->url->host!=NULL){
1608                 osip_free(ctt->url->host);
1609         }
1610         ctt->url->host=osip_strdup(received);
1611         if (ctt->url->port!=NULL){
1612                 osip_free(ctt->url->port);
1613         }
1614         snprintf(port,sizeof(port),"%i",rport);
1615         ctt->url->port=osip_strdup(port);
1616         if (op->masquerade_via) masquerade_via(msg,received,port);
1617         eXosip_register_send_register(op->rid,msg);
1618         eXosip_unlock();
1619         osip_contact_to_str(ctt,&tmp);
1620         addr=sal_address_new(tmp);
1621         osip_free(tmp);
1622         sal_address_clean(addr);
1623         tmp=sal_address_as_string(addr);
1624         sal_op_set_contact(op,tmp);
1625         sal_address_destroy(addr);
1626         ms_message("Resending new register with updated contact %s",tmp);
1627         ms_free(tmp);
1628         return TRUE;
1629 }
1630
1631 static void registration_success(Sal *sal, eXosip_event_t *ev){
1632         SalOp *op=sal_find_register(sal,ev->rid);
1633         osip_header_t *h=NULL;
1634         bool_t registered;
1635         if (op==NULL){
1636                 ms_error("Receiving register response for unknown operation");
1637                 return;
1638         }
1639         osip_message_get_expires(ev->request,0,&h);
1640         if (h!=NULL && atoi(h->hvalue)!=0){
1641                 registered=TRUE;
1642                 if (!register_again_with_updated_contact(op,ev->request,ev->response)){
1643                         sal->callbacks.register_success(op,registered);
1644                 }
1645         }else {
1646                 sal->callbacks.register_success(op,FALSE);
1647         }
1648 }
1649
1650 static bool_t registration_failure(Sal *sal, eXosip_event_t *ev){
1651         int status_code=0;
1652         const char *reason=NULL;
1653         SalOp *op=sal_find_register(sal,ev->rid);
1654         SalReason sr=SalReasonUnknown;
1655         SalError se=SalErrorUnknown;
1656         
1657         if (op==NULL){
1658                 ms_error("Receiving register failure for unknown operation");
1659                 return TRUE;
1660         }
1661         if (ev->response){
1662                 status_code=osip_message_get_status_code(ev->response);
1663                 reason=osip_message_get_reason_phrase(ev->response);
1664         }
1665         switch(status_code){
1666                 case 401:
1667                 case 407:
1668                         return process_authentication(sal,ev);
1669                         break;
1670                 case 606: /*Not acceptable, workaround for proxies that don't like private addresses
1671                                  in vias, such as ekiga.net 
1672                                  On the opposite, freephonie.net bugs when via are masqueraded.
1673                                  */
1674                         op->masquerade_via=TRUE;
1675                 default:
1676                         /* if contact is up to date, process the failure, otherwise resend a new register with
1677                                 updated contact first, just in case the faillure is due to incorrect contact */
1678                         if (ev->response && register_again_with_updated_contact(op,ev->request,ev->response))
1679                                 return TRUE; /*we are retrying with an updated contact*/
1680                         if (status_code==403){
1681                                 se=SalErrorFailure;
1682                                 sr=SalReasonForbidden;
1683                         }else if (status_code==0){
1684                                 se=SalErrorNoResponse;
1685                         }
1686                         sal->callbacks.register_failure(op,se,sr,reason);
1687         }
1688         return TRUE;
1689 }
1690
1691 static void other_request_reply(Sal *sal,eXosip_event_t *ev){
1692         SalOp *op=find_op(sal,ev);
1693
1694         if (op==NULL){
1695                 ms_warning("other_request_reply(): Receiving response to unknown request.");
1696                 return;
1697         }
1698         if (ev->response){
1699                 update_contact_from_response(op,ev->response);
1700                 if (ev->request && strcmp(osip_message_get_method(ev->request),"OPTIONS")==0)
1701                         sal->callbacks.ping_reply(op);
1702         }
1703 }
1704
1705 static bool_t process_event(Sal *sal, eXosip_event_t *ev){
1706         ms_message("linphone process event get a message %d\n",ev->type);
1707         switch(ev->type){
1708                 case EXOSIP_CALL_ANSWERED:
1709                         ms_message("CALL_ANSWERED\n");
1710                         call_accepted(sal,ev);
1711                         authentication_ok(sal,ev);
1712                         break;
1713                 case EXOSIP_CALL_CLOSED:
1714                 case EXOSIP_CALL_CANCELLED:
1715                         ms_message("CALL_CLOSED or CANCELLED\n");
1716                         call_terminated(sal,ev);
1717                         break;
1718                 case EXOSIP_CALL_TIMEOUT:
1719                 case EXOSIP_CALL_NOANSWER:
1720                         ms_message("CALL_TIMEOUT or NOANSWER\n");
1721                         return call_failure(sal,ev);
1722                         break;
1723                 case EXOSIP_CALL_REQUESTFAILURE:
1724                 case EXOSIP_CALL_GLOBALFAILURE:
1725                 case EXOSIP_CALL_SERVERFAILURE:
1726                         ms_message("CALL_REQUESTFAILURE or GLOBALFAILURE or SERVERFAILURE\n");
1727                         return call_failure(sal,ev);
1728                         break;
1729                 case EXOSIP_CALL_RELEASED:
1730                         ms_message("CALL_RELEASED\n");
1731                         call_released(sal, ev);
1732                         break;
1733                 case EXOSIP_CALL_INVITE:
1734                         ms_message("CALL_NEW\n");
1735                         inc_new_call(sal,ev);
1736                         break;
1737                 case EXOSIP_CALL_REINVITE:
1738                         handle_reinvite(sal,ev);
1739                         break;
1740                 case EXOSIP_CALL_ACK:
1741                         ms_message("CALL_ACK");
1742                         handle_ack(sal,ev);
1743                         break;
1744                 case EXOSIP_CALL_REDIRECTED:
1745                         ms_message("CALL_REDIRECTED");
1746                         eXosip_default_action(ev);
1747                         break;
1748                 case EXOSIP_CALL_PROCEEDING:
1749                         ms_message("CALL_PROCEEDING");
1750                         call_proceeding(sal,ev);
1751                         break;
1752                 case EXOSIP_CALL_RINGING:
1753                         ms_message("CALL_RINGING");
1754                         call_ringing(sal,ev);
1755                         break;
1756                 case EXOSIP_CALL_MESSAGE_NEW:
1757                         ms_message("EXOSIP_CALL_MESSAGE_NEW");
1758                         call_message_new(sal,ev);
1759                         break;
1760                 case EXOSIP_CALL_MESSAGE_REQUESTFAILURE:
1761                         if (ev->response &&
1762                                 (ev->response->status_code==407 || ev->response->status_code==401)){
1763                                  return process_authentication(sal,ev);
1764                         }
1765                         break;
1766                 case EXOSIP_IN_SUBSCRIPTION_NEW:
1767                         ms_message("CALL_IN_SUBSCRIPTION_NEW ");
1768                         sal_exosip_subscription_recv(sal,ev);
1769                         break;
1770                 case EXOSIP_IN_SUBSCRIPTION_RELEASED:
1771                         ms_message("CALL_SUBSCRIPTION_NEW ");
1772                         sal_exosip_in_subscription_closed(sal,ev);
1773                         break;
1774                 case EXOSIP_SUBSCRIPTION_UPDATE:
1775                         ms_message("CALL_SUBSCRIPTION_UPDATE");
1776                         break;
1777                 case EXOSIP_SUBSCRIPTION_NOTIFY:
1778                         ms_message("CALL_SUBSCRIPTION_NOTIFY");
1779                         sal_exosip_notify_recv(sal,ev);
1780                         break;
1781                 case EXOSIP_SUBSCRIPTION_ANSWERED:
1782                         ms_message("EXOSIP_SUBSCRIPTION_ANSWERED, ev->sid=%i, ev->did=%i\n",ev->sid,ev->did);
1783                         sal_exosip_subscription_answered(sal,ev);
1784                         break;
1785                 case EXOSIP_SUBSCRIPTION_CLOSED:
1786                         ms_message("EXOSIP_SUBSCRIPTION_CLOSED\n");
1787                         sal_exosip_subscription_closed(sal,ev);
1788                         break;
1789                 case EXOSIP_SUBSCRIPTION_REQUESTFAILURE:   /**< announce a request failure      */
1790                         if (ev->response && (ev->response->status_code == 407 || ev->response->status_code == 401)){
1791                                 return process_authentication(sal,ev);
1792                         }
1793         case EXOSIP_SUBSCRIPTION_SERVERFAILURE:
1794                 case EXOSIP_SUBSCRIPTION_GLOBALFAILURE:
1795                         sal_exosip_subscription_closed(sal,ev);
1796                         break;
1797                 case EXOSIP_REGISTRATION_FAILURE:
1798                         ms_message("REGISTRATION_FAILURE\n");
1799                         return registration_failure(sal,ev);
1800                         break;
1801                 case EXOSIP_REGISTRATION_SUCCESS:
1802                         authentication_ok(sal,ev);
1803                         registration_success(sal,ev);
1804                         break;
1805                 case EXOSIP_MESSAGE_NEW:
1806                         other_request(sal,ev);
1807                         break;
1808                 case EXOSIP_MESSAGE_PROCEEDING:
1809                 case EXOSIP_MESSAGE_ANSWERED:
1810                 case EXOSIP_MESSAGE_REDIRECTED:
1811                 case EXOSIP_MESSAGE_SERVERFAILURE:
1812                 case EXOSIP_MESSAGE_GLOBALFAILURE:
1813                         other_request_reply(sal,ev);
1814                         break;
1815                 case EXOSIP_MESSAGE_REQUESTFAILURE:
1816                 case EXOSIP_NOTIFICATION_REQUESTFAILURE:
1817                         if (ev->response) {
1818                                 switch (ev->response->status_code) {
1819                                         case 407:
1820                                         case 401:
1821                                                 return process_authentication(sal,ev);
1822                                         case 412: {
1823                                                 eXosip_automatic_action ();
1824                                                 return 1;
1825                                         }
1826                                 }
1827                         }
1828                         other_request_reply(sal,ev);
1829                         break;
1830                 default:
1831                         ms_message("Unhandled exosip event ! %i",ev->type);
1832                         break;
1833         }
1834         return TRUE;
1835 }
1836
1837 int sal_iterate(Sal *sal){
1838         eXosip_event_t *ev;
1839         while((ev=eXosip_event_wait(0,0))!=NULL){
1840                 if (process_event(sal,ev))
1841                         eXosip_event_free(ev);
1842         }
1843         eXosip_lock();
1844         eXosip_automatic_refresh();
1845         eXosip_unlock();
1846         return 0;
1847 }
1848
1849 static void register_set_contact(osip_message_t *msg, const char *contact){
1850         osip_uri_param_t *param = NULL;
1851         osip_contact_t *ct=NULL;
1852         char *line=NULL;
1853         /*we get the line parameter choosed by exosip, and add it to our own contact*/
1854         osip_message_get_contact(msg,0,&ct);
1855         if (ct!=NULL){
1856                 osip_uri_uparam_get_byname(ct->url, "line", &param);
1857                 if (param && param->gvalue)
1858                         line=osip_strdup(param->gvalue);
1859         }
1860         _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
1861         osip_message_set_contact(msg,contact);
1862         osip_message_get_contact(msg,0,&ct);
1863         osip_uri_uparam_add(ct->url,osip_strdup("line"),line);
1864 }
1865
1866 int sal_register(SalOp *h, const char *proxy, const char *from, int expires){
1867         osip_message_t *msg;
1868         const char *contact=sal_op_get_contact(h);
1869         
1870         sal_op_set_route(h,proxy);
1871         if (h->rid==-1){
1872                 eXosip_lock();
1873                 h->rid=eXosip_register_build_initial_register(from,proxy,NULL,expires,&msg);
1874                 if (contact) register_set_contact(msg,contact);
1875                 sal_add_register(h->base.root,h);
1876         }else{
1877                 eXosip_lock();
1878                 eXosip_register_build_register(h->rid,expires,&msg);    
1879         }
1880         eXosip_register_send_register(h->rid,msg);
1881         eXosip_unlock();
1882         h->expires=expires;
1883         return 0;
1884 }
1885
1886 int sal_unregister(SalOp *h){
1887         osip_message_t *msg=NULL;
1888         eXosip_lock();
1889         eXosip_register_build_register(h->rid,0,&msg);
1890         if (msg) eXosip_register_send_register(h->rid,msg);
1891         else ms_warning("Could not build unREGISTER !");
1892         eXosip_unlock();
1893         return 0;
1894 }
1895
1896 SalAddress * sal_address_new(const char *uri){
1897         osip_from_t *from;
1898         osip_from_init(&from);
1899         if (osip_from_parse(from,uri)!=0){
1900                 osip_from_free(from);
1901                 return NULL;
1902         }
1903         if (from->displayname!=NULL && from->displayname[0]=='"'){
1904                 char *unquoted=osip_strdup_without_quote(from->displayname);
1905                 osip_free(from->displayname);
1906                 from->displayname=unquoted;
1907         }
1908         return (SalAddress*)from;
1909 }
1910
1911 SalAddress * sal_address_clone(const SalAddress *addr){
1912         osip_from_t *ret=NULL;
1913         osip_from_clone((osip_from_t*)addr,&ret);
1914         return (SalAddress*)ret;
1915 }
1916
1917 #define null_if_empty(s) (((s)!=NULL && (s)[0]!='\0') ? (s) : NULL )
1918
1919 const char *sal_address_get_scheme(const SalAddress *addr){
1920         const osip_from_t *u=(const osip_from_t*)addr;
1921         return null_if_empty(u->url->scheme);
1922 }
1923
1924 const char *sal_address_get_display_name(const SalAddress* addr){
1925         const osip_from_t *u=(const osip_from_t*)addr;
1926         return null_if_empty(u->displayname);
1927 }
1928
1929 const char *sal_address_get_username(const SalAddress *addr){
1930         const osip_from_t *u=(const osip_from_t*)addr;
1931         return null_if_empty(u->url->username);
1932 }
1933
1934 const char *sal_address_get_domain(const SalAddress *addr){
1935         const osip_from_t *u=(const osip_from_t*)addr;
1936         return null_if_empty(u->url->host);
1937 }
1938
1939 void sal_address_set_display_name(SalAddress *addr, const char *display_name){
1940         osip_from_t *u=(osip_from_t*)addr;
1941         if (u->displayname!=NULL){
1942                 osip_free(u->displayname);
1943                 u->displayname=NULL;
1944         }
1945         if (display_name!=NULL && display_name[0]!='\0'){
1946                 u->displayname=osip_strdup(display_name);
1947         }
1948 }
1949
1950 void sal_address_set_username(SalAddress *addr, const char *username){
1951         osip_from_t *uri=(osip_from_t*)addr;
1952         if (uri->url->username!=NULL){
1953                 osip_free(uri->url->username);
1954                 uri->url->username=NULL;
1955         }
1956         if (username)
1957                 uri->url->username=osip_strdup(username);
1958 }
1959
1960 void sal_address_set_domain(SalAddress *addr, const char *host){
1961         osip_from_t *uri=(osip_from_t*)addr;
1962         if (uri->url->host!=NULL){
1963                 osip_free(uri->url->host);
1964                 uri->url->host=NULL;
1965         }
1966         if (host)
1967                 uri->url->host=osip_strdup(host);
1968 }
1969
1970 void sal_address_set_port(SalAddress *addr, const char *port){
1971         osip_from_t *uri=(osip_from_t*)addr;
1972         if (uri->url->port!=NULL){
1973                 osip_free(uri->url->port);
1974                 uri->url->port=NULL;
1975         }
1976         if (port)
1977                 uri->url->port=osip_strdup(port);
1978 }
1979
1980 void sal_address_set_port_int(SalAddress *uri, int port){
1981         char tmp[12];
1982         if (port==5060){
1983                 /*this is the default, special case to leave the port field blank*/
1984                 sal_address_set_port(uri,NULL);
1985                 return;
1986         }
1987         snprintf(tmp,sizeof(tmp),"%i",port);
1988         sal_address_set_port(uri,tmp);
1989 }
1990
1991 void sal_address_clean(SalAddress *addr){
1992         osip_generic_param_freelist(& ((osip_from_t*)addr)->gen_params);
1993         osip_uri_param_freelist(& ((osip_from_t*)addr)->url->url_params);
1994 }
1995
1996 char *sal_address_as_string(const SalAddress *u){
1997         char *tmp,*ret;
1998         osip_from_t *from=(osip_from_t *)u;
1999         char *old_displayname=NULL;
2000         /* hack to force use of quotes around the displayname*/
2001         if (from->displayname!=NULL
2002             && from->displayname[0]!='"'){
2003                 old_displayname=from->displayname;
2004                 from->displayname=osip_enquote(from->displayname);
2005         }
2006         osip_from_to_str(from,&tmp);
2007         if (old_displayname!=NULL){
2008                 ms_free(from->displayname);
2009                 from->displayname=old_displayname;
2010         }
2011         ret=ms_strdup(tmp);
2012         osip_free(tmp);
2013         return ret;
2014 }
2015
2016 char *sal_address_as_string_uri_only(const SalAddress *u){
2017         char *tmp=NULL,*ret;
2018         osip_uri_to_str(((osip_from_t*)u)->url,&tmp);
2019         ret=ms_strdup(tmp);
2020         osip_free(tmp);
2021         return ret;
2022 }
2023 void sal_address_add_param(SalAddress *u,const char* name,const char* value) {
2024         osip_uri_uparam_add     (((osip_from_t*)u)->url,ms_strdup(name),ms_strdup(value));
2025 }
2026
2027 void sal_address_destroy(SalAddress *u){
2028         osip_from_free((osip_from_t*)u);
2029 }
2030
2031 void sal_set_keepalive_period(Sal *ctx,unsigned int value) {
2032         ctx->keepalive_period=value;
2033         eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &value);
2034 }
2035 unsigned int sal_get_keepalive_period(Sal *ctx) {
2036         return ctx->keepalive_period;
2037 }
2038
2039 const char * sal_address_get_port(const SalAddress *addr) {
2040         const osip_from_t *u=(const osip_from_t*)addr;
2041         return null_if_empty(u->url->port);
2042 }
2043
2044 int sal_address_get_port_int(const SalAddress *uri) {
2045         const char* port = sal_address_get_port(uri);
2046         if (port != NULL) {
2047                 return atoi(port);
2048         } else {
2049                 return 5060;
2050         }
2051 }
2052
2053 /* sends a reinvite. Local media description may have changed by application since call establishment*/
2054 int sal_call_update(SalOp *h, const char *subject){
2055         int err=0;
2056         osip_message_t *reinvite=NULL;
2057
2058         eXosip_lock();
2059         if(eXosip_call_build_request(h->did,"INVITE",&reinvite) != 0 || reinvite==NULL){
2060                 eXosip_unlock();
2061                 return -1;
2062         }
2063         eXosip_unlock();
2064         osip_message_set_subject(reinvite,subject);
2065         osip_message_set_allow(reinvite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
2066         if (h->base.root->session_expires!=0){
2067                 osip_message_set_header(reinvite, "Session-expires", "200");
2068                 osip_message_set_supported(reinvite, "timer");
2069         }
2070         if (h->base.local_media){
2071                 h->sdp_offering=TRUE;
2072                 set_sdp_from_desc(reinvite,h->base.local_media);
2073         }else h->sdp_offering=FALSE;
2074         eXosip_lock();
2075         err = eXosip_call_send_request(h->did, reinvite);
2076         eXosip_unlock();
2077         return err;
2078 }