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