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