sal_eXosip2.c 60.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"
jehan's avatar
jehan committed
24
#include "private.h"
25
#include "offeranswer.h"
26

jehan's avatar
jehan committed
27
#ifdef ANDROID
28 29
// Necessary to make it linked
static void for_linker() { eXosip_transport_hook_register(NULL); }
jehan's avatar
jehan committed
30
#endif
31 32
static bool_t call_failure(Sal *sal, eXosip_event_t *ev);

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

35 36 37 38
static void masquerade_via(osip_message_t *msg, const char *ip, const char *port);
static bool_t fix_message_contact(SalOp *op, osip_message_t *request,osip_message_t *last_answer);
static void update_contact_from_response(SalOp *op, osip_message_t *response);

39
void _osip_list_set_empty(osip_list_t *l, void (*freefunc)(void*)){
40
	void *data;
Simon Morlat's avatar
Simon Morlat committed
41 42
	while(!osip_list_eol(l,0)) {
		data=osip_list_get(l,0);
43
		osip_list_remove(l,0);
Simon Morlat's avatar
Simon Morlat committed
44
		if (data) freefunc(data);
45 46 47
	}
}

48 49 50 51 52 53 54
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
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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;
		}
	}
}
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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;
}

116
void sal_add_other(Sal *sal, SalOp *op, osip_message_t *request){
117 118 119 120 121 122 123 124 125 126 127 128 129 130
	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);
}


131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
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
164
SalOp * sal_op_new(Sal *sal){
165
	SalOp *op=ms_new(SalOp,1);
Simon Morlat's avatar
Simon Morlat committed
166
	__sal_op_init(op,sal);
Simon Morlat's avatar
Simon Morlat committed
167
	op->cid=op->did=op->tid=op->rid=op->nid=op->sid=-1;
168
	op->result=NULL;
Simon Morlat's avatar
Simon Morlat committed
169
	op->supports_session_timers=FALSE;
170 171 172 173
	op->sdp_offering=TRUE;
	op->pending_auth=NULL;
	op->sdp_answer=NULL;
	op->reinvite=FALSE;
174
	op->call_id=NULL;
175
	op->replaces=NULL;
176
	op->referred_by=NULL;
177
	op->masquerade_via=FALSE;
178
	op->auto_answer_asked=FALSE;
jehan's avatar
jehan committed
179
	op->auth_info=NULL;
Simon Morlat's avatar
Simon Morlat committed
180
	op->terminated=FALSE;
181 182 183
	return op;
}

184 185 186 187 188
bool_t sal_call_autoanswer_asked(SalOp *op)
{
	return op->auto_answer_asked;
}

189
void sal_op_release(SalOp *op){
190 191 192 193
	if (op->sdp_answer)
		sdp_message_free(op->sdp_answer);
	if (op->pending_auth)
		eXosip_event_free(op->pending_auth);
194
	if (op->rid!=-1){
Simon Morlat's avatar
Simon Morlat committed
195
		sal_remove_register(op->base.root,op->rid);
196
		eXosip_register_remove(op->rid);
Simon Morlat's avatar
Simon Morlat committed
197
	}
198
	if (op->cid!=-1){
199
		ms_message("Cleaning cid %i",op->cid);
200
		sal_remove_call(op->base.root,op);
201
	}
202 203 204 205 206
	if (op->sid!=-1){
		sal_remove_out_subscribe(op->base.root,op);
	}
	if (op->nid!=-1){
		sal_remove_in_subscribe(op->base.root,op);
207 208 209
		if (op->call_id)
			osip_call_id_free(op->call_id);
		op->call_id=NULL;
210
	}
211 212 213
	if (op->pending_auth){
		sal_remove_pending_auth(op->base.root,op);
	}
214 215
	if (op->result)
		sal_media_description_unref(op->result);
216 217 218 219
	if (op->call_id){
		sal_remove_other(op->base.root,op);
		osip_call_id_free(op->call_id);
	}
220 221 222
	if (op->replaces){
		ms_free(op->replaces);
	}
223 224 225
	if (op->referred_by){
		ms_free(op->referred_by);
	}
jehan's avatar
jehan committed
226 227 228
	if (op->auth_info) {
		sal_auth_info_delete(op->auth_info);
	}
Simon Morlat's avatar
Simon Morlat committed
229
	__sal_op_free(op);
230 231
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
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);
	}
}


270
Sal * sal_init(){
271
	static bool_t firsttime=TRUE;
272
	Sal *sal;
273 274 275 276
	if (firsttime){
		osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
		firsttime=FALSE;
	}
277
	eXosip_init();
278
	sal=ms_new0(Sal,1);
279
	sal->keepalive_period=30;
280
	sal->double_reg=TRUE;
281
	sal->use_rports=TRUE;
282
	sal->use_101=TRUE;
283
	sal->reuse_authorization=FALSE;
284
	sal->rootCa = 0;
285
	return sal;
286 287 288 289
}

void sal_uninit(Sal* sal){
	eXosip_quit();
290 291
	if (sal->rootCa)
		ms_free(sal->rootCa);
292 293 294
	ms_free(sal);
}

Simon Morlat's avatar
Simon Morlat committed
295 296 297 298 299 300 301 302
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;
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
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;
319 320
	if (ctx->callbacks.call_released==NULL)
		ctx->callbacks.call_released=(SalOnCallReleased)unimplemented_stub;
321 322
	if (ctx->callbacks.call_updating==NULL) 
		ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
323 324 325 326 327 328 329 330 331 332
	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;
333 334
	if (ctx->callbacks.notify==NULL)
		ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
335 336
	if (ctx->callbacks.notify_presence==NULL)
		ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
Simon Morlat's avatar
Simon Morlat committed
337 338 339 340
	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;
341 342
	if (ctx->callbacks.internal_message==NULL)
		ctx->callbacks.internal_message=(SalOnInternalMsg)unimplemented_stub;
343 344
	if (ctx->callbacks.ping_reply==NULL)
		ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
345
}
346

347 348 349 350
int sal_unlisten_ports(Sal *ctx){
	if (ctx->running){
		eXosip_quit();
		eXosip_init();
351
		ctx->running=FALSE;
352 353 354 355
	}
	return 0;
}

356 357 358 359
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;
jehan's avatar
jehan committed
360
	int keepalive = ctx->keepalive_period;
361
	
jehan's avatar
jehan committed
362
	switch (tr) {
363
	case SalTransportUDP:
jehan's avatar
jehan committed
364
		proto=IPPROTO_UDP;
jehan's avatar
jehan committed
365
		eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE, &keepalive);	
jehan's avatar
jehan committed
366
		break;
367
	case SalTransportTCP:
Pierre-Eric's avatar
Pierre-Eric committed
368
	case SalTransportTLS:
jehan's avatar
jehan committed
369
		proto= IPPROTO_TCP;
jehan's avatar
jehan committed
370
			keepalive=-1;	
371 372 373 374 375 376 377 378
		eXosip_set_option (EXOSIP_OPT_UDP_KEEP_ALIVE,&keepalive);

		if (ctx->rootCa) {
			eXosip_tls_ctx_t tlsCtx;
			memset(&tlsCtx, 0, sizeof(tlsCtx));
			snprintf(tlsCtx.root_ca_cert, sizeof(tlsCtx.client.cert), "%s", ctx->rootCa);
			eXosip_set_tls_ctx(&tlsCtx);
		}
jehan's avatar
jehan committed
379 380 381 382 383
		break;
	default:
		ms_warning("unexpected proto, using datagram");
	}

384 385 386 387
	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*/
388 389
	int use_rports = ctx->use_rports; // Copy char to int to avoid bad alignment
	eXosip_set_option(EXOSIP_OPT_USE_RPORT,&use_rports);
390 391 392
	int dont_use_101 = !ctx->use_101; // Copy char to int to avoid bad alignment
	eXosip_set_option(EXOSIP_OPT_DONT_SEND_101,&dont_use_101);

393 394 395
	ipv6=strchr(addr,':')!=NULL;
	eXosip_enable_ipv6(ipv6);

Pierre-Eric's avatar
Pierre-Eric committed
396 397
	if (is_secure && tr == SalTransportUDP){
		ms_fatal("SIP over DTLS is not supported yet.");
398 399
		return -1;
	}
Pierre-Eric's avatar
Pierre-Eric committed
400
	err=eXosip_listen_addr(proto, addr, port, ipv6 ?  PF_INET6 : PF_INET, is_secure);
401 402 403
#ifdef HAVE_EXOSIP_GET_SOCKET
	ms_message("Exosip has socket number %i",eXosip_get_socket(proto));
#endif
jehan's avatar
jehan committed
404
	
405
	ctx->running=TRUE;
406 407 408
	return err;
}

409
ortp_socket_t sal_get_socket(Sal *ctx){
410 411 412 413 414 415
#ifdef HAVE_EXOSIP_GET_SOCKET
	return eXosip_get_socket(IPPROTO_UDP);
#else
	ms_warning("Sorry, eXosip does not have eXosip_get_socket() method");
	return -1;
#endif
416 417
}

418 419 420 421 422 423 424 425
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;
}

Simon Morlat's avatar
Simon Morlat committed
426 427 428 429
void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
	ctx->one_matching_codec=one_matching_codec;
}

430 431 432 433
MSList *sal_get_pending_auths(Sal *sal){
	return ms_list_copy(sal->pending_auths);
}

434 435 436 437
void sal_use_double_registrations(Sal *ctx, bool_t enabled){
	ctx->double_reg=enabled;
}

438 439 440
void sal_use_rport(Sal *ctx, bool_t use_rports){
	ctx->use_rports=use_rports;
}
441 442 443
void sal_use_101(Sal *ctx, bool_t use_101){
	ctx->use_101=use_101;
}
444

445 446 447 448 449 450
void sal_root_ca(Sal* ctx, const char* rootCa) {
	if (ctx->rootCa)
		ms_free(ctx->rootCa);
	ctx->rootCa = ms_strdup(rootCa);
}

451
static int extract_received_rport(osip_message_t *msg, const char **received, int *rportval,SalTransport* transport){
452 453
	osip_via_t *via=NULL;
	osip_generic_param_t *param=NULL;
454 455 456 457
	const char *rport=NULL;

	*rportval=5060;
	*received=NULL;
458 459
	osip_message_get_via(msg,0,&via);
	if (!via) return -1;
460

461
	*transport = sal_transport_parse(via->protocol);
462
	
463 464 465
	if (via->port && via->port[0]!='\0')
		*rportval=atoi(via->port);
	
466 467 468 469
	osip_via_param_get_byname(via,"rport",&param);
	if (param) {
		rport=param->gvalue;
		if (rport && rport[0]!='\0') *rportval=atoi(rport);
470
		*received=via->host;
471 472 473 474
	}
	param=NULL;
	osip_via_param_get_byname(via,"received",&param);
	if (param) *received=param->gvalue;
475 476

	if (rport==NULL && *received==NULL) return -1;
477 478
	return 0;
}
479 480

static void set_sdp(osip_message_t *sip,sdp_message_t *msg){
481 482
	int sdplen;
	char clen[10];
483 484
	char *sdp=NULL;
	sdp_message_to_str(msg,&sdp);
485 486 487 488 489 490 491 492
	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);
}

493 494 495 496 497 498 499 500 501 502 503
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){
504
	ms_message("Doing SDP offer/answer process");
505
	if (h->result){
506
		sal_media_description_unref(h->result);
507 508 509 510 511 512
	}
	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;
Simon Morlat's avatar
Simon Morlat committed
513
		offer_answer_initiate_incoming(h->base.local_media,h->base.remote_media,h->result,h->base.root->one_matching_codec);
514
		h->sdp_answer=media_description_to_sdp(h->result);
Simon Morlat's avatar
Simon Morlat committed
515 516
		/*once we have generated the SDP answer, we modify the result description for processing by the upper layer.
		 It should contains media parameters constraint from the remote offer, not our response*/
517
		strcpy(h->result->addr,h->base.remote_media->addr);
518
		h->result->bandwidth=h->base.remote_media->bandwidth;
519 520 521 522 523 524 525 526 527 528 529 530
		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;
			}
		}
	}
	
}

531 532 533 534
int sal_call_is_offerer(const SalOp *h){
	return h->sdp_offering;
}

Simon Morlat's avatar
Simon Morlat committed
535
int sal_call_set_local_media_description(SalOp *h, SalMediaDescription *desc){
536 537 538 539
	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
540
	h->base.local_media=desc;
541 542 543
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
544
int sal_call(SalOp *h, const char *from, const char *to){
545
	int err;
Simon Morlat's avatar
Simon Morlat committed
546 547 548
	osip_message_t *invite=NULL;
	sal_op_set_from(h,from);
	sal_op_set_to(h,to);
549 550
	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
551 552 553 554
	if (err!=0){
		ms_error("Could not create call.");
		return -1;
	}
555
	osip_message_set_allow(invite, "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
556
	if (h->base.contact){
557
		_osip_list_set_empty(&invite->contacts,(void (*)(void*))osip_contact_free);
Simon Morlat's avatar
Simon Morlat committed
558
		osip_message_set_contact(invite,h->base.contact);
559
	}
Simon Morlat's avatar
Simon Morlat committed
560 561 562 563
	if (h->base.root->session_expires!=0){
		osip_message_set_header(invite, "Session-expires", "200");
		osip_message_set_supported(invite, "timer");
	}
564 565 566 567
	if (h->base.local_media){
		h->sdp_offering=TRUE;
		set_sdp_from_desc(invite,h->base.local_media);
	}else h->sdp_offering=FALSE;
568 569 570 571 572 573
	if (h->replaces){
		osip_message_set_header(invite,"Replaces",h->replaces);
		if (h->referred_by)
			osip_message_set_header(invite,"Referred-By",h->referred_by);
	}
	
574
	eXosip_lock();
Simon Morlat's avatar
Simon Morlat committed
575
	err=eXosip_call_send_initial_invite(invite);
576 577 578 579 580
	eXosip_unlock();
	h->cid=err;
	if (err<0){
		ms_error("Fail to send invite !");
		return -1;
581
	}else{
582
		sal_add_call(h->base.root,h);
583 584 585 586
	}
	return 0;
}

587 588 589 590 591 592 593
int sal_call_notify_ringing(SalOp *h, bool_t early_media){
	osip_message_t *msg;
	
	/*if early media send also 180 and 183 */
	if (early_media && h->sdp_answer){
		msg=NULL;
		eXosip_lock();
Simon Morlat's avatar
Simon Morlat committed
594
		eXosip_call_build_answer(h->tid,180,&msg);
595 596 597 598 599
		if (msg){
			set_sdp(msg,h->sdp_answer);
			eXosip_call_send_answer(h->tid,180,msg);
		}
		msg=NULL;
Simon Morlat's avatar
Simon Morlat committed
600
		eXosip_call_build_answer(h->tid,183,&msg);
601 602 603 604 605 606 607 608 609 610
		if (msg){
			set_sdp(msg,h->sdp_answer);
			eXosip_call_send_answer(h->tid,183,msg);
		}
		eXosip_unlock();
	}else{
		eXosip_lock();
		eXosip_call_send_answer(h->tid,180,NULL);
		eXosip_unlock();
	}
Simon Morlat's avatar
Simon Morlat committed
611 612 613
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
614 615
int sal_call_accept(SalOp * h){
	osip_message_t *msg;
Simon Morlat's avatar
Simon Morlat committed
616
	const char *contact=sal_op_get_contact(h);
Simon Morlat's avatar
Simon Morlat committed
617 618 619 620 621 622 623 624 625
	/* 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
626

627
	if (contact) {
628
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
629 630
		osip_message_set_contact(msg,contact);
	}
631 632 633 634 635 636
	
	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{
637
			if (h->sdp_answer){
638
				set_sdp(msg,h->sdp_answer);
639 640 641
				sdp_message_free(h->sdp_answer);
				h->sdp_answer=NULL;
			}
642 643 644 645 646
		}
	}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
647 648 649
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
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){
683 684 685 686
	if (h->base.local_media && h->base.remote_media && !h->result){
		sdp_process(h);
	}
	return h->result;
Simon Morlat's avatar
Simon Morlat committed
687 688
}

689 690 691 692 693 694 695 696
int sal_call_set_referer(SalOp *h, SalOp *refered_call){
	if (refered_call->replaces)
		h->replaces=ms_strdup(refered_call->replaces);
	if (refered_call->referred_by)
		h->referred_by=ms_strdup(refered_call->referred_by);
	return 0;
}

697 698 699 700 701
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);
Simon Morlat's avatar
Simon Morlat committed
702
	/*bug here: eXosip2 does not honor the route argument*/
703 704 705 706 707 708 709 710 711 712 713 714 715
	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;
}

716
int sal_call_accept_refer(SalOp *op){
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	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;
}

736
int sal_call_refer(SalOp *h, const char *refer_to){
Simon Morlat's avatar
Simon Morlat committed
737 738 739 740 741 742 743 744 745 746
	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;
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
int sal_call_refer_with_replaces(SalOp *h, SalOp *other_call_h){
	osip_message_t *msg=NULL;
	char referto[256]={0};
	int err=0;
	eXosip_lock();
	if (eXosip_call_get_referto(other_call_h->did,referto,sizeof(referto)-1)!=0){
		ms_error("eXosip_call_get_referto() failed for did=%i",other_call_h->did);
		eXosip_unlock();
		return -1;
	}
	eXosip_call_build_refer(h->did,referto, &msg);
	osip_message_set_header(msg,"Referred-By",h->base.from);
	if (msg) err=eXosip_call_send_request(h->did, msg);
	else err=-1;
	eXosip_unlock();
	return err;
}

765 766 767 768 769 770 771 772 773 774 775 776 777 778
SalOp *sal_call_get_replaces(SalOp *h){
	if (h->replaces!=NULL){
		int cid;
		eXosip_lock();
		cid=eXosip_call_find_by_replaces(h->replaces);
		eXosip_unlock();
		if (cid>0){
			SalOp *ret=sal_find_call(h->base.root,cid);
			return ret;
		}
	}
	return NULL;
}

Simon Morlat's avatar
Simon Morlat committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
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;
}

jehan's avatar
jehan committed
798 799 800 801 802 803 804 805
static void push_auth_to_exosip(const SalAuthInfo *info){
	const char *userid;
	if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
	else userid=info->userid;
	ms_message("Authentication info for username [%s], id[%s], realm [%s] added to eXosip", info->username,userid, info->realm);
	eXosip_add_authentication_info (info->username,userid,
                                  info->password, NULL,info->realm);
}
806
/*
jehan's avatar
jehan committed
807 808 809 810 811 812
 * Just for symmetry ;-)
 */
static void pop_auth_from_exosip() {
	eXosip_clear_authentication_info();
}

Simon Morlat's avatar
Simon Morlat committed
813
int sal_call_terminate(SalOp *h){
814
	int err;
jehan's avatar
jehan committed
815
	if (h->auth_info) push_auth_to_exosip(h->auth_info);
Simon Morlat's avatar
Simon Morlat committed
816
	eXosip_lock();
817
	err=eXosip_call_terminate(h->cid,h->did);
Simon Morlat's avatar
Simon Morlat committed
818
	eXosip_unlock();
819
	if (!h->base.root->reuse_authorization) pop_auth_from_exosip();
820 821 822
	if (err!=0){
		ms_warning("Exosip could not terminate the call: cid=%i did=%i", h->cid,h->did);
	}
Simon Morlat's avatar
Simon Morlat committed
823
	h->terminated=TRUE;
824 825 826 827
	return 0;
}

void sal_op_authenticate(SalOp *h, const SalAuthInfo *info){
828
    if (h->pending_auth){
jehan's avatar
jehan committed
829
		push_auth_to_exosip(info);
Simon Morlat's avatar
Simon Morlat committed
830
		
831
        /*FIXME exosip does not take into account this update register message*/
Simon Morlat's avatar
Simon Morlat committed
832
	/*
833
        if (fix_message_contact(h, h->pending_auth->request,h->pending_auth->response)) {
Simon Morlat's avatar
Simon Morlat committed
834
            
835
        };
Simon Morlat's avatar
Simon Morlat committed
836 837 838
	*/
		update_contact_from_response(h,h->pending_auth->response);
		eXosip_lock();
839 840
		eXosip_default_action(h->pending_auth);
		eXosip_unlock();
841
		ms_message("eXosip_default_action() done");
842
		if (!h->base.root->reuse_authorization) pop_auth_from_exosip();
843
		
jehan's avatar
jehan committed
844 845
		if (h->auth_info) sal_auth_info_delete(h->auth_info); /*if already exist*/
		h->auth_info=sal_auth_info_clone(info); /*store auth info for subsequent request*/
846 847
	}
}
jehan's avatar
jehan committed
848 849 850 851 852 853 854 855
void sal_op_cancel_authentication(SalOp *h) {
	if (h->rid >0) {
		sal_op_get_sal(h)->callbacks.register_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"));
	} else if (h->cid >0) {
		sal_op_get_sal(h)->callbacks.call_failure(h,SalErrorFailure, SalReasonForbidden,_("Authentication failure"),0);
	} else {
		ms_warning("Auth failure not handled");
	}
856

jehan's avatar
jehan committed
857
}
858 859 860
static void set_network_origin(SalOp *op, osip_message_t *req){
	const char *received=NULL;
	int rport=5060;
Simon Morlat's avatar
Simon Morlat committed
861
	char origin[64]={0};
862 863
    SalTransport transport;
	if (extract_received_rport(req,&received,&rport,&transport)!=0){
864 865 866 867 868 869 870
		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);
	}
871 872 873 874 875
    if (transport != SalTransportUDP) {
        snprintf(origin,sizeof(origin)-1,"sip:%s:%i",received,rport);
    } else {
       snprintf(origin,sizeof(origin)-1,"sip:%s:%i;transport=%s",received,rport,sal_transport_to_string(transport)); 
    }
876 877 878
	__sal_op_set_network_origin(op,origin);
}

879 880 881 882 883 884 885 886 887 888
static void set_remote_ua(SalOp* op, osip_message_t *req){
	if (op->base.remote_ua==NULL){
		osip_header_t *h=NULL;
		osip_message_get_user_agent(req,0,&h);
		if (h){
			op->base.remote_ua=ms_strdup(h->hvalue);
		}
	}
}

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
static void set_replaces(SalOp *op, osip_message_t *req){
	osip_header_t *h=NULL;

	if (op->replaces){
		ms_free(op->replaces);
		op->replaces=NULL;
	}
	osip_message_header_get_byname(req,"replaces",0,&h);
	if (h){
		if (h->hvalue && h->hvalue[0]!='\0'){
			op->replaces=ms_strdup(h->hvalue);
		}
	}
}

904
static SalOp *find_op(Sal *sal, eXosip_event_t *ev){
905
	if (ev->cid>0){
906
		return sal_find_call(sal,ev->cid);
907
	}
908 909 910
	if (ev->rid>0){
		return sal_find_register(sal,ev->rid);
	}
911 912 913
	if (ev->sid>0){
		return sal_find_out_subscribe(sal,ev->sid);
	}
Simon Morlat's avatar
Simon Morlat committed
914 915 916
	if (ev->nid>0){
		return sal_find_in_subscribe(sal,ev->nid);
	}
917 918 919 920
	if (ev->response) return sal_find_other(sal,ev->response);
	return NULL;
}

921 922 923
static void inc_new_call(Sal *sal, eXosip_event_t *ev){
	SalOp *op=sal_op_new(sal);
	osip_from_t *from,*to;
924
	osip_call_info_t *call_info;
925 926
	char *tmp;
	sdp_message_t *sdp=eXosip_get_sdp_info(ev->request);
927 928

	set_network_origin(op,ev->request);
929
	set_remote_ua(op,ev->request);
930
	set_replaces(op,ev->request);
931
	
932
	if (sdp){
933
		op->sdp_offering=FALSE;
934 935 936 937 938 939 940 941 942 943 944 945 946
		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);
947 948 949 950 951 952 953 954 955 956 957 958 959

	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);
	}

960 961 962 963
	op->tid=ev->tid;
	op->cid=ev->cid;
	op->did=ev->did;
	
964
	sal_add_call(op->base.root,op);
965 966 967 968
	sal->callbacks.call_received(op);
}

static void handle_reinvite(Sal *sal,  eXosip_event_t *ev){
969
	SalOp *op=find_op(sal,ev);
970 971 972 973 974 975 976 977 978 979 980
	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){
981
		sal_media_description_unref(op->base.remote_media);
982 983
		op->base.remote_media=NULL;
	}
984 985 986
	if (op->result){
		sal_media_description_unref(op->result);
		op->result=NULL;
987
	}
988 989 990 991 992
	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);
993
		sal->callbacks.call_updating(op);
994 995
	}else {
		op->sdp_offering=TRUE;
996 997 998 999 1000 1001 1002
		eXosip_lock();
		eXosip_call_build_answer(ev->tid,200,&msg);
		if (msg!=NULL){
			set_sdp_from_desc(msg,op->base.local_media);
			eXosip_call_send_answer(ev->tid,200,msg);
		}
		eXosip_unlock();
1003
	}
1004
	
1005 1006 1007
}

static void handle_ack(Sal *sal,  eXosip_event_t *ev){
1008
	SalOp *op=find_op(sal,ev);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	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){
1023
		if (sdp) sal->callbacks.call_updating(op);
1024 1025 1026 1027 1028 1029
		op->reinvite=FALSE;
	}else{
		sal->callbacks.call_ack(op);
	}
}

1030
static void update_contact_from_response(SalOp *op, osip_message_t *response){
1031 1032
	const char *received;
	int rport;
Simon Morlat's avatar
Simon Morlat committed
1033
	SalTransport transport;
1034
	if (extract_received_rport(response,&received,&rport,&transport)==0){
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		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);
Simon Morlat's avatar
Simon Morlat committed
1045 1046
			if (transport!=SalTransportUDP)
				sal_address_set_transport(addr,transport);
1047
			tmp=sal_address_as_string(addr);
1048
			ms_message("Contact address updated to %s",tmp);
1049
			sal_op_set_contact(op,tmp);
Simon Morlat's avatar
Simon Morlat committed
1050
			sal_address_destroy(addr);
1051 1052 1053
			ms_free(tmp);
		}
	}