sal_eXosip2.c 46.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
linphone
Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
19 20 21
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
22

Simon Morlat's avatar
Simon Morlat committed
23
#include "sal_eXosip2.h"
24

25
#include "offeranswer.h"
26

Simon Morlat's avatar
Simon Morlat committed
27 28
static void text_received(Sal *sal, eXosip_event_t *ev);

29 30 31 32 33 34 35 36
static void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
	void *data;
	while((data=osip_list_get(l,0))!=NULL){
		osip_list_remove(l,0);
		freefunc(data);
	}
}

37 38 39 40 41 42 43
void sal_get_default_local_ip(Sal *sal, int address_family,char *ip, size_t iplen){
	if (eXosip_guess_localip(address_family,ip,iplen)<0){
		/*default to something */
		strncpy(ip,address_family==AF_INET6 ? "::1" : "127.0.0.1",iplen);
		ms_error("Could not find default routable ip address !");
	}
}
Simon Morlat's avatar
Simon Morlat committed
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

static SalOp * sal_find_call(Sal *sal, int cid){
	const MSList *elem;
	SalOp *op;
	for(elem=sal->calls;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->cid==cid) return op;
	}
	return NULL;
}

static void sal_add_call(Sal *sal, SalOp *op){
	sal->calls=ms_list_append(sal->calls,op);
}

static void sal_remove_call(Sal *sal, SalOp *op){
	sal->calls=ms_list_remove(sal->calls, op);
}

Simon Morlat's avatar
Simon Morlat committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static SalOp * sal_find_register(Sal *sal, int rid){
	const MSList *elem;
	SalOp *op;
	for(elem=sal->registers;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->rid==rid) return op;
	}
	return NULL;
}

static void sal_add_register(Sal *sal, SalOp *op){
	sal->registers=ms_list_append(sal->registers,op);
}

static void sal_remove_register(Sal *sal, int rid){
	MSList *elem;
	SalOp *op;
	for(elem=sal->registers;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->rid==rid) {
			sal->registers=ms_list_remove_link(sal->registers,elem);
			return;
		}
	}
}
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static SalOp * sal_find_other(Sal *sal, osip_message_t *response){
	const MSList *elem;
	SalOp *op;
	osip_call_id_t *callid=osip_message_get_call_id(response);
	if (callid==NULL) {
		ms_error("There is no call-id in this response !");
		return NULL;
	}
	for(elem=sal->other_transactions;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (osip_call_id_match(callid,op->call_id)==0) return op;
	}
	return NULL;
}

static void sal_add_other(Sal *sal, SalOp *op, osip_message_t *request){
	osip_call_id_t *callid=osip_message_get_call_id(request);
	if (callid==NULL) {
		ms_error("There is no call id in the request !");
		return;
	}
	osip_call_id_clone(callid,&op->call_id);
	sal->other_transactions=ms_list_append(sal->other_transactions,op);
}

static void sal_remove_other(Sal *sal, SalOp *op){
	sal->other_transactions=ms_list_remove(sal->other_transactions,op);
}


120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static void sal_add_pending_auth(Sal *sal, SalOp *op){
	sal->pending_auths=ms_list_append(sal->pending_auths,op);
}


static void sal_remove_pending_auth(Sal *sal, SalOp *op){
	sal->pending_auths=ms_list_remove(sal->pending_auths,op);
}

void sal_exosip_fix_route(SalOp *op){
	if (sal_op_get_route(op)!=NULL){
		osip_route_t *rt=NULL;
		osip_uri_param_t *lr_param=NULL;
		
		osip_route_init(&rt);
		if (osip_route_parse(rt,sal_op_get_route(op))<0){
			ms_warning("Bad route  %s!",sal_op_get_route(op));
			sal_op_set_route(op,NULL);
		}else{
			/* check if the lr parameter is set , if not add it */
			osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
		  	if (lr_param==NULL){
				char *tmproute;
				osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
				osip_route_to_str(rt,&tmproute);
				sal_op_set_route(op,tmproute);
				osip_free(tmproute);
			}
		}
		osip_route_free(rt);
	}
}

Simon Morlat's avatar
Simon Morlat committed
153
SalOp * sal_op_new(Sal *sal){
154
	SalOp *op=ms_new(SalOp,1);
Simon Morlat's avatar
Simon Morlat committed
155
	__sal_op_init(op,sal);
Simon Morlat's avatar
Simon Morlat committed
156
	op->cid=op->did=op->tid=op->rid=op->nid=op->sid=-1;
157
	op->result=NULL;
Simon Morlat's avatar
Simon Morlat committed
158
	op->supports_session_timers=FALSE;
159 160 161 162
	op->sdp_offering=TRUE;
	op->pending_auth=NULL;
	op->sdp_answer=NULL;
	op->reinvite=FALSE;
163
	op->call_id=NULL;
164
	op->masquerade_via=FALSE;
165
	op->auto_answer_asked=FALSE;
166 167 168
	return op;
}

169 170 171 172 173
bool_t sal_call_autoanswer_asked(SalOp *op)
{
	return op->auto_answer_asked;
}

174
void sal_op_release(SalOp *op){
175 176 177 178
	if (op->sdp_answer)
		sdp_message_free(op->sdp_answer);
	if (op->pending_auth)
		eXosip_event_free(op->pending_auth);
179
	if (op->rid!=-1){
Simon Morlat's avatar
Simon Morlat committed
180 181
		sal_remove_register(op->base.root,op->rid);
	}
182
	if (op->cid!=-1){
183
		ms_message("Cleaning cid %i",op->cid);
184
		sal_remove_call(op->base.root,op);
185
	}
186 187 188 189 190
	if (op->sid!=-1){
		sal_remove_out_subscribe(op->base.root,op);
	}
	if (op->nid!=-1){
		sal_remove_in_subscribe(op->base.root,op);
191 192 193
		if (op->call_id)
			osip_call_id_free(op->call_id);
		op->call_id=NULL;
194
	}
195 196 197
	if (op->pending_auth){
		sal_remove_pending_auth(op->base.root,op);
	}
198 199
	if (op->result)
		sal_media_description_unref(op->result);
200 201 202 203
	if (op->call_id){
		sal_remove_other(op->base.root,op);
		osip_call_id_free(op->call_id);
	}
Simon Morlat's avatar
Simon Morlat committed
204
	__sal_op_free(op);
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
	int ortp_level=ORTP_DEBUG;
	switch(level){
		case OSIP_INFO1:
		case OSIP_INFO2:
		case OSIP_INFO3:
		case OSIP_INFO4:
			ortp_level=ORTP_MESSAGE;
			break;
		case OSIP_WARNING:
			ortp_level=ORTP_WARNING;
			break;
		case OSIP_ERROR:
		case OSIP_BUG:
			ortp_level=ORTP_ERROR;
			break;
		case OSIP_FATAL:
			ortp_level=ORTP_FATAL;
			break;
		case END_TRACE_LEVEL:
			break;
	}
	if (ortp_log_level_enabled(level)){
		int len=strlen(chfr);
		char *chfrdup=ortp_strdup(chfr);
		/*need to remove endline*/
		if (len>1){
			if (chfrdup[len-1]=='\n')
				chfrdup[len-1]='\0';
			if (chfrdup[len-2]=='\r')
				chfrdup[len-2]='\0';
		}
		ortp_logv(ortp_level,chfrdup,ap);
		ortp_free(chfrdup);
	}
}


245
Sal * sal_init(){
246
	static bool_t firsttime=TRUE;
247
	Sal *sal;
248 249 250 251
	if (firsttime){
		osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
		firsttime=FALSE;
	}
252
	eXosip_init();
253 254 255
	sal=ms_new0(Sal,1);
	sal->sock=-1;
	return sal;
256 257 258 259 260 261 262
}

void sal_uninit(Sal* sal){
	eXosip_quit();
	ms_free(sal);
}

Simon Morlat's avatar
Simon Morlat committed
263 264 265 266 267 268 269 270
void sal_set_user_pointer(Sal *sal, void *user_data){
	sal->up=user_data;
}

void *sal_get_user_pointer(const Sal *sal){
	return sal->up;
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
static void unimplemented_stub(){
	ms_warning("Unimplemented SAL callback");
}

void sal_set_callbacks(Sal *ctx, const SalCallbacks *cbs){
	memcpy(&ctx->callbacks,cbs,sizeof(*cbs));
	if (ctx->callbacks.call_received==NULL) 
		ctx->callbacks.call_received=(SalOnCallReceived)unimplemented_stub;
	if (ctx->callbacks.call_ringing==NULL) 
		ctx->callbacks.call_ringing=(SalOnCallRinging)unimplemented_stub;
	if (ctx->callbacks.call_accepted==NULL) 
		ctx->callbacks.call_accepted=(SalOnCallAccepted)unimplemented_stub;
	if (ctx->callbacks.call_failure==NULL) 
		ctx->callbacks.call_failure=(SalOnCallFailure)unimplemented_stub;
	if (ctx->callbacks.call_terminated==NULL) 
		ctx->callbacks.call_terminated=(SalOnCallTerminated)unimplemented_stub;
	if (ctx->callbacks.call_updated==NULL) 
		ctx->callbacks.call_updated=(SalOnCallUpdated)unimplemented_stub;
	if (ctx->callbacks.auth_requested==NULL) 
		ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
	if (ctx->callbacks.auth_success==NULL) 
		ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
	if (ctx->callbacks.register_success==NULL) 
		ctx->callbacks.register_success=(SalOnRegisterSuccess)unimplemented_stub;
	if (ctx->callbacks.register_failure==NULL) 
		ctx->callbacks.register_failure=(SalOnRegisterFailure)unimplemented_stub;
	if (ctx->callbacks.dtmf_received==NULL) 
		ctx->callbacks.dtmf_received=(SalOnDtmfReceived)unimplemented_stub;
299 300
	if (ctx->callbacks.notify==NULL)
		ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
301 302
	if (ctx->callbacks.notify_presence==NULL)
		ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
Simon Morlat's avatar
Simon Morlat committed
303 304 305 306
	if (ctx->callbacks.subscribe_received==NULL)
		ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
	if (ctx->callbacks.text_received==NULL)
		ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
307 308
	if (ctx->callbacks.internal_message==NULL)
		ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
309 310
	if (ctx->callbacks.ping_reply==NULL)
		ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
311
}
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

static ortp_socket_t create_socket(int pf, int proto, const char *addr, int local_port){
	struct addrinfo hints;
	struct addrinfo *res=NULL;
	ortp_socket_t sock;
	int optval;
	char tmp[20];
	int err;
	
	sock=socket(pf,(proto==IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,0);
	if (sock<0) {
		ms_error("Fail to create socket");
		return -1;
	}
	snprintf(tmp,sizeof(tmp)-1,"%i",local_port);
	memset (&hints,0,sizeof(hints));
	hints.ai_family=(pf==PF_INET) ? AF_INET : AF_INET6;
	hints.ai_socktype=(proto==IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
	
	if ((err=getaddrinfo(addr,
	    	tmp,
	    	&hints,&res))<0) {
		ms_error("create_socket: getaddrinfo() failed: %s",gai_strerror(err));
		close_socket(sock);
		return -1;
	}
	if (bind(sock,(struct sockaddr*)res->ai_addr,res->ai_addrlen)<0){
		ms_error("Bind socket to localhost:%i failed: %s",local_port,getSocketError());
		freeaddrinfo(res);
		close_socket(sock);
		return -1;
	}
	freeaddrinfo(res);
	optval=1;
	if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
				(SOCKET_OPTION_VALUE)&optval, sizeof (optval))<0){
		ms_warning("Fail to set SO_REUSEADDR");
	}
	/*set_non_blocking_socket(sock);*/
	return sock;
}


356 357 358 359 360
int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure){
	int err;
	bool_t ipv6;
	int proto=IPPROTO_UDP;
	
361 362 363 364
	if (ctx->running){
		eXosip_quit();
		eXosip_init();
	}
365 366 367 368 369 370 371
	err=0;
	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
					version of eXosip, which is not the case*/
	/*see if it looks like an IPv6 address*/
	ipv6=strchr(addr,':')!=NULL;
	eXosip_enable_ipv6(ipv6);

372
	if (tr!=SalTransportDatagram || is_secure){
373 374 375
		ms_fatal("SIP over TCP or TLS or DTLS is not supported yet.");
		return -1;
	}
376 377 378 379
	ctx->sock=create_socket(ipv6 ?  PF_INET6 : PF_INET, proto, addr, port);
	if (ctx->sock==-1) return -1;
	ms_message("Exosip is given socket %i",ctx->sock);
	eXosip_set_socket(proto,ctx->sock,port);
380 381 382
	/* this is to properly initialize the udp transport layer of eXosip, as we don't use eXosip_listen_addr() */
	/* otherwise we have a bug with improper contacts in registers: <sip:user@host:;line=jIjijde68>*/
	eXosip_masquerade_contact(NULL,0);
383
	/*
384
	err=eXosip_listen_addr(proto, addr, port, ipv6 ?  PF_INET6 : PF_INET, 0);
385 386
	*/
	ctx->running=TRUE;
387 388 389
	return err;
}

390 391 392 393
ortp_socket_t sal_get_socket(Sal *ctx){
	return ctx->sock;
}

394 395 396 397 398 399 400 401
void sal_set_user_agent(Sal *ctx, const char *user_agent){
	eXosip_set_user_agent(user_agent);
}

void sal_use_session_timers(Sal *ctx, int expires){
	ctx->session_expires=expires;
}

402 403 404 405
MSList *sal_get_pending_auths(Sal *sal){
	return ms_list_copy(sal->pending_auths);
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval){
	osip_via_t *via=NULL;
	osip_generic_param_t *param=NULL;
	const char *rport;
	
	osip_message_get_via(msg,0,&via);
	if (!via) return -1;
	osip_via_param_get_byname(via,"rport",&param);
	if (param) {
		rport=param->gvalue;
		if (rport && rport[0]!='\0') *rportval=atoi(rport);
		else *rportval=5060;
	}
	param=NULL;
	osip_via_param_get_byname(via,"received",&param);
	if (param) *received=param->gvalue;
	else return -1;
	return 0;
}
425 426

static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
427 428
	int sdplen;
	char clen[10];
429 430
	char *sdp=NULL;
	sdp_message_to_str(msg,&sdp);
431 432 433 434 435 436 437 438
	sdplen=strlen(sdp);
	snprintf(clen,sizeof(clen),"%i",sdplen);
	osip_message_set_body(sip,sdp,sdplen);
	osip_message_set_content_type(sip,"application/sdp");
	osip_message_set_content_length(sip,clen);
	osip_free(sdp);
}

439 440 441 442 443 444 445 446 447 448 449
static void set_sdp_from_desc(osip_message_t *sip, const SalMediaDescription *desc){
	sdp_message_t *msg=media_description_to_sdp(desc);
	if (msg==NULL) {
		ms_error("Fail to print sdp message !");
		return;
	}
	set_sdp(sip,msg);
	sdp_message_free(msg);
}

static void sdp_process(SalOp *h){
450
	ms_message("Doing SDP offer/answer process");
451
	if (h->result){
452
		sal_media_description_unref(h->result);
453 454 455 456 457 458 459 460 461
	}
	h->result=sal_media_description_new();
	if (h->sdp_offering){	
		offer_answer_initiate_outgoing(h->base.local_media,h->base.remote_media,h->result);
	}else{
		int i;
		offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result);
		h->sdp_answer=media_description_to_sdp(h->result);
		strcpy(h->result->addr,h->base.remote_media->addr);
462
		h->result->bandwidth=h->base.remote_media->bandwidth;
463 464 465 466 467 468 469 470 471 472 473 474
		for(i=0;i<h->result->nstreams;++i){
			if (h->result->streams[i].port>0){
				strcpy(h->result->streams[i].addr,h->base.remote_media->streams[i].addr);
				h->result->streams[i].ptime=h->base.remote_media->streams[i].ptime;
				h->result->streams[i].bandwidth=h->base.remote_media->streams[i].bandwidth;
				h->result->streams[i].port=h->base.remote_media->streams[i].port;
			}
		}
	}
	
}

Simon Morlat's avatar
Simon Morlat committed
475
int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
476 477 478 479
	if (desc)
		sal_media_description_ref(desc);
	if (h->base.local_media)
		sal_media_description_unref(h->base.local_media);
Simon Morlat's avatar
Simon Morlat committed
480
	h->base.local_media=desc;
481 482 483
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
484
int sal_call(SalOp *h, const char *from, const char *to){
485
	int err;
Simon Morlat's avatar
Simon Morlat committed
486 487 488
	osip_message_t *invite=NULL;
	sal_op_set_from(h,from);
	sal_op_set_to(h,to);
489 490
	sal_exosip_fix_route(h);
	err=eXosip_call_build_initial_invite(&invite,to,from,sal_op_get_route(h),"Phone call");
Simon Morlat's avatar
Simon Morlat committed
491 492 493 494
	if (err!=0){
		ms_error("Could not create call.");
		return -1;
	}
495
	if (h->base.contact){
496
		_osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
Simon Morlat's avatar
Simon Morlat committed
497
		osip_message_set_contact(invite,h->base.contact);
498
	}
Simon Morlat's avatar
Simon Morlat committed
499 500 501 502
	if (h->base.root->session_expires!=0){
		osip_message_set_header(invite, "Session-expires", "200");
		osip_message_set_supported(invite, "timer");
	}
503 504 505 506
	if (h->base.local_media){
		h->sdp_offering=TRUE;
		set_sdp_from_desc(invite,h->base.local_media);
	}else h->sdp_offering=FALSE;
507
	eXosip_lock();
Simon Morlat's avatar
Simon Morlat committed
508
	err=eXosip_call_send_initial_invite(invite);
509 510 511 512 513
	eXosip_unlock();
	h->cid=err;
	if (err<0){
		ms_error("Fail to send invite !");
		return -1;
514
	}else{
515
		sal_add_call(h->base.root,h);
516 517 518 519
	}
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
520 521 522 523 524 525 526
int sal_call_notify_ringing(SalOp *h){
	eXosip_lock();
	eXosip_call_send_answer(h->tid,180,NULL);
	eXosip_unlock();
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
527 528
int sal_call_accept(SalOp * h){
	osip_message_t *msg;
Simon Morlat's avatar
Simon Morlat committed
529
	const char *contact=sal_op_get_contact(h);
Simon Morlat's avatar
Simon Morlat committed
530 531 532 533 534 535 536 537 538
	/* sends a 200 OK */
	int err=eXosip_call_build_answer(h->tid,200,&msg);
	if (err<0 || msg==NULL){
		ms_error("Fail to build answer for call: err=%i",err);
		return -1;
	}
	if (h->base.root->session_expires!=0){
		if (h->supports_session_timers) osip_message_set_supported(msg, "timer");
	}
Simon Morlat's avatar
Simon Morlat committed
539

540
	if (contact) {
541
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
542 543
		osip_message_set_contact(msg,contact);
	}
544 545 546 547 548 549 550 551 552 553 554 555 556
	
	if (h->base.local_media){
		/*this is the case where we received an invite without SDP*/
		if (h->sdp_offering) {
			set_sdp_from_desc(msg,h->base.local_media);
		}else{
			if (h->sdp_answer)
				set_sdp(msg,h->sdp_answer);
		}
	}else{
		ms_error("You are accepting a call but not defined any media capabilities !");
	}
	eXosip_call_send_answer(h->tid,200,msg);
Simon Morlat's avatar
Simon Morlat committed
557 558 559
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
int sal_call_decline(SalOp *h, SalReason reason, const char *redirect){
	if (reason==SalReasonBusy){
		eXosip_lock();
		eXosip_call_send_answer(h->tid,486,NULL);
		eXosip_unlock();
	}
	else if (reason==SalReasonTemporarilyUnavailable){
		eXosip_lock();
		eXosip_call_send_answer(h->tid,480,NULL);
		eXosip_unlock();
	}else if (reason==SalReasonDoNotDisturb){
		eXosip_lock();
		eXosip_call_send_answer(h->tid,600,NULL);
		eXosip_unlock();
	}else if (reason==SalReasonMedia){
		eXosip_lock();
		eXosip_call_send_answer(h->tid,415,NULL);
		eXosip_unlock();
	}else if (redirect!=NULL && reason==SalReasonRedirect){
		osip_message_t *msg;
		int code;
		if (strstr(redirect,"sip:")!=0) code=302;
		else code=380;
		eXosip_lock();
		eXosip_call_build_answer(h->tid,code,&msg);
		osip_message_set_contact(msg,redirect);
		eXosip_call_send_answer(h->tid,code,msg);
		eXosip_unlock();
	}else sal_call_terminate(h);
	return 0;
}

SalMediaDescription * sal_call_get_final_media_description(SalOp *h){
593 594 595 596
	if (h->base.local_media && h->base.remote_media && !h->result){
		sdp_process(h);
	}
	return h->result;
Simon Morlat's avatar
Simon Morlat committed
597 598
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
int sal_ping(SalOp *op, const char *from, const char *to){
	osip_message_t *options=NULL;
	
	sal_op_set_from(op,from);
	sal_op_set_to(op,to);
	eXosip_options_build_request (&options, sal_op_get_to(op),
			sal_op_get_from(op),sal_op_get_route(op));
	if (options){
		if (op->base.root->session_expires!=0){
			osip_message_set_header(options, "Session-expires", "200");
			osip_message_set_supported(options, "timer");
		}
		sal_add_other(sal_op_get_sal(op),op,options);
		return eXosip_options_send_request(options);
	}
	return -1;
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
int sal_refer_accept(SalOp *op){
	osip_message_t *msg=NULL;
	int err=0;
	eXosip_lock();
	err = eXosip_call_build_notify(op->did,EXOSIP_SUBCRSTATE_ACTIVE,&msg);
	if(msg != NULL)
	{
		osip_message_set_header(msg,(const char *)"event","refer");
		osip_message_set_content_type(msg,"message/sipfrag");
		osip_message_set_body(msg,"SIP/2.0 100 Trying",sizeof("SIP/2.0 100 Trying"));
		eXosip_call_send_request(op->did,msg);
	}
	else
	{
		ms_error("could not get a notify built\n");
	}
	eXosip_unlock();
	return err;
}

Simon Morlat's avatar
Simon Morlat committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
int sal_refer(SalOp *h, const char *refer_to){
	osip_message_t *msg=NULL;
	int err=0;
	eXosip_lock();
	eXosip_call_build_refer(h->did,refer_to, &msg);
	if (msg) err=eXosip_call_send_request(h->did, msg);
	else err=-1;
	eXosip_unlock();
	return err;
}

int sal_call_send_dtmf(SalOp *h, char dtmf){
	osip_message_t *msg=NULL;
	char dtmf_body[128];
	char clen[10];

	eXosip_lock();
	eXosip_call_build_info(h->did,&msg);
	if (msg){
		snprintf(dtmf_body, sizeof(dtmf_body), "Signal=%c\r\nDuration=250\r\n", dtmf);
		osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
		osip_message_set_content_type(msg,"application/dtmf-relay");
		snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
		osip_message_set_content_length(msg,clen);		
		eXosip_call_send_request(h->did,msg);
	}
	eXosip_unlock();
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
667 668 669
int sal_call_terminate(SalOp *h){
	eXosip_lock();
	eXosip_call_terminate(h->cid,h->did);
Simon Morlat's avatar
Simon Morlat committed
670
	eXosip_unlock();
671
	sal_remove_call(h->base.root,h);
Simon Morlat's avatar
Simon Morlat committed
672
	h->cid=-1;
673 674 675 676 677 678 679 680
	return 0;
}

void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
	if (h->pending_auth){
		const char *userid;
		if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
		else userid=info->userid;
681
		ms_message("Authentication info for %s %s added to eXosip", info->username,info->realm);
682 683
		eXosip_add_authentication_info (info->username,userid,
                                      info->password, NULL,info->realm);
Simon Morlat's avatar
Simon Morlat committed
684
		eXosip_lock();
685 686
		eXosip_default_action(h->pending_auth);
		eXosip_unlock();
687
		ms_message("eXosip_default_action() done");
Simon Morlat's avatar
Simon Morlat committed
688
		eXosip_clear_authentication_info();
689
		eXosip_event_free(h->pending_auth);
690
		sal_remove_pending_auth(sal_op_get_sal(h),h);
691 692 693 694
		h->pending_auth=NULL;
	}
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
static void set_network_origin(SalOp *op, osip_message_t *req){
	const char *received=NULL;
	int rport=5060;
	char origin[64];
	if (extract_received_rport(req,&received,&rport)!=0){
		osip_via_t *via=NULL;
		char *tmp;
		osip_message_get_via(req,0,&via);
		received=osip_via_get_host(via);
		tmp=osip_via_get_port(via);
		if (tmp) rport=atoi(tmp);
	}
	snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
	__sal_op_set_network_origin(op,origin);
}

711
static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
712
	if (ev->cid>0){
713
		return sal_find_call(sal,ev->cid);
714
	}
715 716 717 718 719 720 721
	if (ev->rid>0){
		return sal_find_register(sal,ev->rid);
	}
	if (ev->response) return sal_find_other(sal,ev->response);
	return NULL;
}

722 723 724
static void inc_new_call(Sal *sal, eXosip_event_t *ev){
	SalOp *op=sal_op_new(sal);
	osip_from_t *from,*to;
725
	osip_call_info_t *call_info;
726 727
	char *tmp;
	sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
728 729 730

	set_network_origin(op,ev->request);
	
731
	if (sdp){
732
		op->sdp_offering=FALSE;
733 734 735 736 737 738 739 740 741 742 743 744 745
		op->base.remote_media=sal_media_description_new();
		sdp_to_media_description(sdp,op->base.remote_media);
		sdp_message_free(sdp);
	}else op->sdp_offering=TRUE;

	from=osip_message_get_from(ev->request);
	to=osip_message_get_to(ev->request);
	osip_from_to_str(from,&tmp);
	sal_op_set_from(op,tmp);
	osip_free(tmp);
	osip_from_to_str(to,&tmp);
	sal_op_set_to(op,tmp);
	osip_free(tmp);
746 747 748 749 750 751 752 753 754 755 756 757 758

	osip_message_get_call_info(ev->request,0,&call_info);
	if(call_info)
	{
		osip_call_info_to_str(call_info,&tmp);
		if( strstr(tmp,"answer-after=") != NULL)
		{
			op->auto_answer_asked=TRUE;
			ms_message("The caller asked to automatically answer the call(Emergency?)\n");
		}
		osip_free(tmp);
	}

759 760 761 762
	op->tid=ev->tid;
	op->cid=ev->cid;
	op->did=ev->did;
	
763
	sal_add_call(op->base.root,op);
764 765 766 767
	sal->callbacks.call_received(op);
}

static void handle_reinvite(Sal *sal,  eXosip_event_t *ev){
768
	SalOp *op=find_op(sal,ev);
769 770 771 772 773 774 775 776 777 778 779
	sdp_message_t *sdp;
	osip_message_t *msg=NULL;

	if (op==NULL) {
		ms_warning("Reinvite for non-existing operation !");
		return;
	}
	op->reinvite=TRUE;
	op->tid=ev->tid;
	sdp=eXosip_get_sdp_info(ev->request);
	if (op->base.remote_media){
780
		sal_media_description_unref(op->base.remote_media);
781 782 783 784 785 786 787 788 789
		op->base.remote_media=NULL;
	}
	eXosip_lock();
	eXosip_call_build_answer(ev->tid,200,&msg);
	eXosip_unlock();
	if (msg==NULL) return;
	if (op->base.root->session_expires!=0){
		if (op->supports_session_timers) osip_message_set_supported(msg, "timer");
	}
790
	if (op->base.contact){
791
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
792 793
		osip_message_set_contact(msg,op->base.contact);
	}
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	if (sdp){
		op->sdp_offering=FALSE;
		op->base.remote_media=sal_media_description_new();
		sdp_to_media_description(sdp,op->base.remote_media);
		sdp_message_free(sdp);
		sdp_process(op);
		set_sdp(msg,op->sdp_answer);
	}else {
		op->sdp_offering=TRUE;
		set_sdp_from_desc(msg,op->base.local_media);
	}
	eXosip_lock();
	eXosip_call_send_answer(ev->tid,200,msg);
	eXosip_unlock();
}

static void handle_ack(Sal *sal,  eXosip_event_t *ev){
811
	SalOp *op=find_op(sal,ev);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	sdp_message_t *sdp;

	if (op==NULL) {
		ms_warning("ack for non-existing call !");
		return;
	}
	sdp=eXosip_get_sdp_info(ev->ack);
	if (sdp){
		op->base.remote_media=sal_media_description_new();
		sdp_to_media_description(sdp,op->base.remote_media);
		sdp_process(op);
		sdp_message_free(sdp);
	}
	if (op->reinvite){
		sal->callbacks.call_updated(op);
		op->reinvite=FALSE;
	}else{
		sal->callbacks.call_ack(op);
	}
}

833
static void update_contact_from_response(SalOp *op, osip_message_t *response){
834 835
	const char *received;
	int rport;
836
	if (extract_received_rport(response,&received,&rport)==0){
837 838 839 840 841 842 843 844 845 846 847
		const char *contact=sal_op_get_contact(op);
		if (!contact){
			/*no contact given yet, use from instead*/
			contact=sal_op_get_from(op);
		}
		if (contact){
			SalAddress *addr=sal_address_new(contact);
			char *tmp;
			sal_address_set_domain(addr,received);
			sal_address_set_port_int(addr,rport);
			tmp=sal_address_as_string(addr);
848
			ms_message("Contact address updated to %s for this dialog",tmp);
849 850 851 852
			sal_op_set_contact(op,tmp);
			ms_free(tmp);
		}
	}
853 854 855
}

static int call_proceeding(Sal *sal, eXosip_event_t *ev){
856
	SalOp *op=find_op(sal,ev);
857 858 859 860 861 862 863 864 865 866 867 868 869 870
	
	if (op==NULL) {
		ms_warning("This call has been canceled.");
		eXosip_lock();
		eXosip_call_terminate(ev->cid,ev->did);
		eXosip_unlock();
		return -1;
	}
	op->did=ev->did;
	op->tid=ev->tid;
	
	/* update contact if received and rport are set by the server
	 note: will only be used by remote for next INVITE, if any...*/
	update_contact_from_response(op,ev->response);
871 872 873 874 875
	return 0;
}

static void call_ringing(Sal *sal, eXosip_event_t *ev){
	sdp_message_t *sdp;
Simon Morlat's avatar
Simon Morlat committed
876
	SalOp *op=find_op(sal,ev);
877
	if (call_proceeding(sal, ev)==-1) return;
Simon Morlat's avatar
Simon Morlat committed
878
	
879 880 881 882 883 884 885 886 887 888 889 890 891
	sdp=eXosip_get_sdp_info(ev->response);
	if (sdp){
		op->base.remote_media=sal_media_description_new();
		sdp_to_media_description(sdp,op->base.remote_media);
		sdp_message_free(sdp);
		if (op->base.local_media) sdp_process(op);
	}
	sal->callbacks.call_ringing(op);
}

static void call_accepted(Sal *sal, eXosip_event_t *ev){
	sdp_message_t *sdp;
	osip_message_t *msg=NULL;
892
	SalOp *op=find_op(sal,ev);
893
	const char *contact;
894
	
895 896 897 898 899 900 901 902 903 904 905 906
	if (op==NULL){
		ms_error("A closed call is accepted ?");
		return;
	}
	sdp=eXosip_get_sdp_info(ev->response);
	if (sdp){
		op->base.remote_media=sal_media_description_new();
		sdp_to_media_description(sdp,op->base.remote_media);
		sdp_message_free(sdp);
		if (op->base.local_media) sdp_process(op);
	}
	eXosip_call_build_ack(ev->did,&msg);
907 908 909 910 911
	contact=sal_op_get_contact(op);
	if (contact) {
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
		osip_message_set_contact(msg,contact);
	}
912 913 914 915 916 917 918
	if (op->sdp_answer)
			set_sdp(msg,op->sdp_answer);
	eXosip_call_send_ack(ev->did,msg);
	sal->callbacks.call_accepted(op);
}

static void call_terminated(Sal *sal, eXosip_event_t *ev){
Simon Morlat's avatar
Simon Morlat committed
919
	char *from;
920
	SalOp *op=find_op(sal,ev);
921 922 923 924
	if (op==NULL){
		ms_warning("Call terminated for already closed call ?");
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
925
	osip_from_to_str(ev->request->from,&from);
926
	sal_remove_call(sal,op);
Simon Morlat's avatar
Simon Morlat committed
927 928 929
	op->cid=-1;
	sal->callbacks.call_terminated(op,from);
	osip_free(from);
930 931 932
}

static void call_released(Sal *sal, eXosip_event_t *ev){
933
	SalOp *op=find_op(sal,ev);
934
	if (op==NULL){
935
		ms_warning("No op associated to this call_released()");
936 937
		return;
	}
938 939 940
	op->cid=-1;
	if (op->did==-1) 
		sal->callbacks.call_failure(op,SalErrorNoResponse,SalReasonUnknown,NULL);
941 942
}

943
static int get_auth_data_from_response(osip_message_t *resp, const char **realm, const char **username){
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
	const char *prx_realm=NULL,*www_realm=NULL;
	osip_proxy_authenticate_t *prx_auth;
	osip_www_authenticate_t *www_auth;
	
	*username=osip_uri_get_username(resp->from->url);
	prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
	www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
	if (prx_auth!=NULL)
		prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
	if (www_auth!=NULL)
		www_realm=osip_www_authenticate_get_realm(www_auth);

	if (prx_realm){
		*realm=prx_realm;
	}else if (www_realm){
		*realm=www_realm;
	}else{
		return -1;
	}
	return 0;
}

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
static int get_auth_data_from_request(osip_message_t *msg, const char **realm, const char **username){
	osip_authorization_t *auth=NULL;
	osip_proxy_authorization_t *prx_auth=NULL;
	
	*username=osip_uri_get_username(msg->from->url);
	osip_message_get_authorization(msg, 0, &auth);
	if (auth){
		*realm=osip_authorization_get_realm(auth);
		return 0;
	}
	osip_message_get_proxy_authorization(msg,0,&prx_auth);
	if (prx_auth){
		*realm=osip_proxy_authorization_get_realm(prx_auth);
		return 0;
	}
	return -1;
}

static int get_auth_data(eXosip_event_t *ev, const char **realm, const char **username){
	if (ev->response && get_auth_data_from_response(ev->response,realm,username)==0) return 0;
	if (ev->request && get_auth_data_from_request(ev->request,realm,username)==0) return 0;
	return -1;
}

990 991 992 993 994 995 996
int sal_op_get_auth_requested(SalOp *op, const char **realm, const char **username){
	if (op->pending_auth){
		return get_auth_data(op->pending_auth,realm,username);
	}
	return -1;
}

997 998 999
static bool_t process_authentication(Sal *sal, eXosip_event_t *ev){
	SalOp *op;
	const char *username,*realm;
Simon Morlat's avatar
Simon Morlat committed
1000
	op=find_op(sal,ev);
1001 1002 1003 1004 1005 1006 1007 1008
	if (op==NULL){
		ms_warning("No operation associated with this authentication !");
		return TRUE;
	}
	if (get_auth_data(ev,&realm,&username)==0){
		if (op->pending_auth!=NULL)
			eXosip_event_free(op->pending_auth);
		op->pending_auth=ev;
1009
		sal_add_pending_auth (sal,op);
1010 1011 1012 1013 1014 1015 1016 1017 1018
		sal->callbacks.auth_requested(op,realm,username);
		return FALSE;
	}
	return TRUE;
}

static void authentication_ok(Sal *sal, eXosip_event_t *ev){
	SalOp *op;
	const char *username,*realm;
Simon Morlat's avatar
Simon Morlat committed
1019
	op=find_op(sal,ev);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	if (op==NULL){
		ms_warning("No operation associated with this authentication_ok!");
		return ;
	}
	if (get_auth_data(ev,&realm,&username)==0){
		sal->callbacks.auth_success(op,realm,username);
	}
}

static bool_t call_failure(Sal *sal, eXosip_event_t *ev){
	SalOp *op;
	int code=0;
	const char *reason=NULL;
	SalError error=SalErrorUnknown;
	SalReason sr=SalReasonUnknown;
	
1036
	op=(SalOp*)find_op(sal,ev);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

	if (op==NULL) {
		ms_warning("Call failure reported for a closed call, ignored.");
		return TRUE;
	}

	if (ev->response){
		code=osip_message_get_status_code(ev->response);
		reason=osip_message_get_reason_phrase(ev->response);
	}
	switch(code)
	{
		case 401:
		case 407:
			return process_authentication(sal,ev);
			break;
		case 400:
			error=SalErrorUnknown;
		break;
		case 404:
			error=SalErrorFailure;
			sr=SalReasonNotFound;
		break;
		case 415:
Simon Morlat's avatar
Simon Morlat committed
1061 1062
			error=SalErrorFailure;
			sr=SalReasonMedia;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		break;
		case 422:
			eXosip_default_action(ev);
			return TRUE;
		break;
		case 480:
			error=SalErrorFailure;
			sr=SalReasonTemporarilyUnavailable;
		case 486:
			error=SalErrorFailure;
			sr=SalReasonBusy;
		break;
		case 487:
		break;
		case 600:
			error=SalErrorFailure;
			sr=SalReasonDoNotDisturb;
		break;
		case 603:
			error=SalErrorFailure;
			sr=SalReasonDeclined;
		break;
		default:
			if (code>0){
				error=SalErrorFailure;
				sr=SalReasonUnknown;
			}else error=SalErrorNoResponse;
	}
Simon Morlat's avatar
Simon Morlat committed
1091
	sal->callbacks.call_failure(op,error,sr,reason);
1092 1093 1094 1095 1096
	return TRUE;
}


static void process_media_control_xml(Sal *sal, eXosip_event_t *ev){
1097
	SalOp *op=find_op(sal,ev);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	osip_body_t *body=NULL;

	if (op==NULL){
		ms_warning("media control xml received without operation context!");
		return ;
	}
	
	osip_message_get_body(ev->request,0,&body);
	if (body && body->body!=NULL &&
		strstr(body->body,"picture_fast_update")){
		osip_message_t *ans=NULL;
		ms_message("Receiving VFU request !");
		if (sal->callbacks.vfu_request){
			sal->callbacks.vfu_request(op);
			eXosip_call_build_answer(ev->tid,200,&ans);
			if (ans)
				eXosip_call_send_answer(ev->tid,200,ans);
		}
	}
}

static void process_dtmf_relay(Sal *sal, eXosip_event_t *ev){
1120
	SalOp *op=find_op(sal,ev);
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	osip_body_t *body=NULL;

	if (op==NULL){
		ms_warning("media dtmf relay received without operation context!");
		return ;
	}
	
	osip_message_get_body(ev->request,0,&body);
	if (body && body->body!=NULL){
		osip_message_t *ans=NULL;
		const char *name=strstr(body->body,"Signal");
		if (name==NULL) name=strstr(body->body,"signal");
		if (name==NULL) {
			ms_warning("Could not extract the dtmf name from the SIP INFO.");
		}else{
			char tmp[2];
			name+=strlen("signal");
			if (sscanf(name," = %1s",tmp)==1){
				ms_message("Receiving dtmf %s via SIP INFO.",tmp);
				if (sal->callbacks.dtmf_received != NULL)
					sal->callbacks.dtmf_received(op, tmp[0]);
			}
		}
		eXosip_call_build_answer(ev->tid,200,&ans);
		if (ans)
			eXosip_call_send_answer(ev->tid,200,ans);
	}
}

static void call_message_new(Sal *sal, eXosip_event_t *ev){
	osip_message_t *ans=NULL;
	if (ev->request){
		if (MSG_IS_INFO(ev->request)){
			osip_content_type_t *ct;
			ct=osip_message_get_content_type(ev->request);
			if (ct && ct->subtype){
				if (strcmp(ct->subtype,"media_control+xml")==0)
					process_media_control_xml(sal,ev);
				else if (strcmp(ct->subtype,"dtmf-relay")==0)
					process_dtmf_relay(sal,ev);
				else {
					ms_message("Unhandled SIP INFO.");
					/*send an "Not implemented" answer*/
					eXosip_lock();
					eXosip_call_build_answer(ev->tid,501,&ans);
					if (ans)
						eXosip_call_send_answer(ev->tid,501,ans);
					eXosip_unlock();
				}
			}else{
				/*empty SIP INFO, probably to test we are alive. Send an empty answer*/
				eXosip_lock();
				eXosip_call_build_answer(ev->tid,200,&ans);
				if (ans)
					eXosip_call_send_answer(ev->tid,200,ans);
				eXosip_unlock();
			}
		}
Simon Morlat's avatar
Simon Morlat committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
		if(MSG_IS_MESSAGE(ev->request)){
			/* SIP messages could be received into call */
			text_received(sal, ev);
			eXosip_lock();
			eXosip_call_build_answer(ev->tid,200,&ans);
			if (ans)
				eXosip_call_send_answer(ev->tid,200,ans);
			eXosip_unlock();
		}
		if(MSG_IS_REFER(ev->request)){
			osip_header_t *h=NULL;
1190 1191
			SalOp *op=find_op(sal,ev);
			
Simon Morlat's avatar
Simon Morlat committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
			ms_message("Receiving REFER request !");
			osip_message_header_get_byname(ev->request,"Refer-To",0,&h);
			eXosip_lock();
			eXosip_call_build_answer(ev->tid,202,&ans);
			if (ans)
				eXosip_call_send_answer(ev->tid,202,ans);
			eXosip_unlock();
			if (h){
				sal->callbacks.refer_received(sal,op,h->hvalue);
			}
1202 1203 1204 1205
			else
			{
				ms_warning("cannot do anything with the refer without destination\n");
			}
Simon Morlat's avatar
Simon Morlat committed
1206 1207 1208
		}
		if(MSG_IS_NOTIFY(ev->request)){
			osip_header_t *h=NULL;
1209 1210 1211
			char *from=NULL;
			SalOp *op=find_op(sal,ev);

Simon Morlat's avatar
Simon Morlat committed
1212
			ms_message("Receiving NOTIFY request !");
1213
			osip_from_to_str(ev->request->from,&from);
Simon Morlat's avatar
Simon Morlat committed
1214
			osip_message_header_get_byname(ev->request,"Event",0,&h);
1215 1216 1217
			if(h)
				sal->callbacks.notify(op,from,h->hvalue);
			/*answer that we received the notify*/
Simon Morlat's avatar
Simon Morlat committed
1218 1219 1220 1221 1222
			eXosip_lock();
			eXosip_call_build_answer(ev->tid,200,&ans);
			if (ans)
				eXosip_call_send_answer(ev->tid,200,ans);
			eXosip_unlock();
1223
			osip_free(from);
Simon Morlat's avatar
Simon Morlat committed
1224
		}
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263