sal.c 25.3 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
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.
*/

20
/**
Simon Morlat's avatar
Simon Morlat committed
21
 This header files defines the Signaling Abstraction Layer.
22
 The purpose of this layer is too allow experiment different call signaling
Simon Morlat's avatar
Simon Morlat committed
23 24
 protocols and implementations under linphone, for example SIP, JINGLE...
**/
25 26 27
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Sylvain Berfini's avatar
Sylvain Berfini committed
28
#include "sal/sal.h"
29
#include "bellesip_sal/sal_impl.h"
30 31 32

#include <ctype.h>

33
const char* sal_transport_to_string(SalTransport transport) {
34 35 36 37 38 39 40 41
	switch (transport) {
		case SalTransportUDP:return "udp";
		case SalTransportTCP: return "tcp";
		case SalTransportTLS:return "tls";
		case SalTransportDTLS:return "dtls";
		default: {
			ms_fatal("Unexpected transport [%i]",transport);
			return NULL;
42
		}
43
	}
44
}
45

46
SalTransport sal_transport_parse(const char* param) {
jehan's avatar
jehan committed
47
	if (!param) return SalTransportUDP;
48 49 50 51 52 53
	if (strcasecmp("udp",param)==0) return SalTransportUDP;
	if (strcasecmp("tcp",param)==0) return SalTransportTCP;
	if (strcasecmp("tls",param)==0) return SalTransportTLS;
	if (strcasecmp("dtls",param)==0) return SalTransportDTLS;
	ms_error("Unknown transport type[%s], returning UDP", param);
	return SalTransportUDP;
54
}
55

Simon Morlat's avatar
Simon Morlat committed
56
SalMediaDescription *sal_media_description_new(){
57
	SalMediaDescription *md=ms_new0(SalMediaDescription,1);
58
	int i;
59
	md->refcount=1;
60
	for(i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
61
		md->streams[i].dir=SalStreamInactive;
62 63
		md->streams[i].rtp_port = 0;
		md->streams[i].rtcp_port = 0;
64
	}
Simon Morlat's avatar
Simon Morlat committed
65
	return md;
Simon Morlat's avatar
Simon Morlat committed
66 67
}

68
static void sal_media_description_destroy(SalMediaDescription *md){
Simon Morlat's avatar
Simon Morlat committed
69 70
	int i;
	for(i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;i++){
71 72
		ms_list_free_with_data(md->streams[i].payloads,(void (*)(void *))payload_type_destroy);
		ms_list_free_with_data(md->streams[i].already_assigned_payloads,(void (*)(void *))payload_type_destroy);
73
		md->streams[i].payloads=NULL;
74
		md->streams[i].already_assigned_payloads=NULL;
Simon Morlat's avatar
Simon Morlat committed
75 76 77 78
	}
	ms_free(md);
}

79
SalMediaDescription * sal_media_description_ref(SalMediaDescription *md){
80
	md->refcount++;
81
	return md;
82 83 84 85 86 87 88 89 90
}

void sal_media_description_unref(SalMediaDescription *md){
	md->refcount--;
	if (md->refcount==0){
		sal_media_description_destroy (md);
	}
}

91
SalStreamDescription *sal_media_description_find_stream(SalMediaDescription *md, SalMediaProto proto, SalStreamType type){
92
	int i;
93
	for(i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;++i){
94
		SalStreamDescription *ss=&md->streams[i];
95
		if (!sal_stream_description_active(ss)) continue;
96 97 98 99 100
		if (ss->proto==proto && ss->type==type) return ss;
	}
	return NULL;
}

101 102 103
unsigned int sal_media_description_nb_active_streams_of_type(SalMediaDescription *md, SalStreamType type) {
	unsigned int i;
	unsigned int nb = 0;
104
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; ++i) {
105
		if (!sal_stream_description_active(&md->streams[i])) continue;
106 107 108 109 110 111 112
		if (md->streams[i].type == type) nb++;
	}
	return nb;
}

SalStreamDescription * sal_media_description_get_active_stream_of_type(SalMediaDescription *md, SalStreamType type, unsigned int idx) {
	unsigned int i;
113
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; ++i) {
114
		if (!sal_stream_description_active(&md->streams[i])) continue;
115 116 117 118 119 120 121 122 123 124 125 126 127 128
		if (md->streams[i].type == type) {
			if (idx-- == 0) return &md->streams[i];
		}
	}
	return NULL;
}

SalStreamDescription * sal_media_description_find_secure_stream_of_type(SalMediaDescription *md, SalStreamType type) {
	SalStreamDescription *desc = sal_media_description_find_stream(md, SalProtoRtpSavpf, type);
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoRtpSavp, type);
	return desc;
}

SalStreamDescription * sal_media_description_find_best_stream(SalMediaDescription *md, SalStreamType type) {
129 130 131
	SalStreamDescription *desc = sal_media_description_find_stream(md, SalProtoUdpTlsRtpSavpf, type);
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoUdpTlsRtpSavp, type);
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoRtpSavpf, type);
132 133 134 135 136 137
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoRtpSavp, type);
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoRtpAvpf, type);
	if (desc == NULL) desc = sal_media_description_find_stream(md, SalProtoRtpAvp, type);
	return desc;
}

Simon Morlat's avatar
Simon Morlat committed
138
bool_t sal_media_description_empty(const SalMediaDescription *md){
139
	if (sal_media_description_get_nb_active_streams(md) > 0) return FALSE;
Simon Morlat's avatar
Simon Morlat committed
140 141 142 143 144
	return TRUE;
}

void sal_media_description_set_dir(SalMediaDescription *md, SalStreamDir stream_dir){
	int i;
145
	for(i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;++i){
Simon Morlat's avatar
Simon Morlat committed
146
		SalStreamDescription *ss=&md->streams[i];
147
		if (!sal_stream_description_active(ss)) continue;
Simon Morlat's avatar
Simon Morlat committed
148 149 150 151
		ss->dir=stream_dir;
	}
}

152 153 154
int sal_media_description_get_nb_active_streams(const SalMediaDescription *md) {
	int i;
	int nb = 0;
155
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
156 157 158 159 160
		if (sal_stream_description_active(&md->streams[i])) nb++;
	}
	return nb;
}

161 162 163 164 165 166
static bool_t is_null_address(const char *addr){
	return strcmp(addr,"0.0.0.0")==0 || strcmp(addr,"::0")==0;
}

/*check for the presence of at least one stream with requested direction */
static bool_t has_dir(const SalMediaDescription *md, SalStreamDir stream_dir){
Simon Morlat's avatar
Simon Morlat committed
167
	int i;
168
	
Simon Morlat's avatar
Simon Morlat committed
169
	/* we are looking for at least one stream with requested direction, inactive streams are ignored*/
170
	for(i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;++i){
Simon Morlat's avatar
Simon Morlat committed
171
		const SalStreamDescription *ss=&md->streams[i];
172
		if (!sal_stream_description_active(ss)) continue;
173 174 175
		if (ss->dir==stream_dir) {
			return TRUE;
		}
176
		/*compatibility check for phones that only used the null address and no attributes */
177
		if (ss->dir==SalStreamSendRecv && stream_dir==SalStreamSendOnly && (is_null_address(md->addr) || is_null_address(ss->rtp_addr))){
178
			return TRUE;
179
		}
180 181 182 183 184 185
	}
	return FALSE;
}

bool_t sal_media_description_has_dir(const SalMediaDescription *md, SalStreamDir stream_dir){
	if (stream_dir==SalStreamRecvOnly){
186
		return has_dir(md, SalStreamRecvOnly) && !(has_dir(md,SalStreamSendOnly) || has_dir(md,SalStreamSendRecv));
187
	}else if (stream_dir==SalStreamSendOnly){
188
		return has_dir(md, SalStreamSendOnly) && !(has_dir(md,SalStreamRecvOnly) || has_dir(md,SalStreamSendRecv));
189 190 191 192 193 194 195
	}else if (stream_dir==SalStreamSendRecv){
		return has_dir(md,SalStreamSendRecv);
	}else{
		/*SalStreamInactive*/
		if (has_dir(md,SalStreamSendOnly) || has_dir(md,SalStreamSendRecv)  || has_dir(md,SalStreamRecvOnly))
			return FALSE;
		else return TRUE;
Simon Morlat's avatar
Simon Morlat committed
196
	}
197
	return FALSE;
Simon Morlat's avatar
Simon Morlat committed
198
}
199

200 201 202 203 204
bool_t sal_stream_description_active(const SalStreamDescription *sd) {
	return (sd->rtp_port > 0);
}

bool_t sal_stream_description_has_avpf(const SalStreamDescription *sd) {
johan's avatar
johan committed
205
	return ((sd->proto == SalProtoRtpAvpf) || (sd->proto == SalProtoRtpSavpf) || (sd->proto == SalProtoUdpTlsRtpSavpf));
206 207 208 209 210 211
}

bool_t sal_stream_description_has_srtp(const SalStreamDescription *sd) {
	return ((sd->proto == SalProtoRtpSavp) || (sd->proto == SalProtoRtpSavpf));
}

johan's avatar
johan committed
212 213 214 215
bool_t sal_stream_description_has_dtls(const SalStreamDescription *sd) {
	return ((sd->proto == SalProtoUdpTlsRtpSavp) || (sd->proto == SalProtoUdpTlsRtpSavpf));
}

216 217 218
bool_t sal_media_description_has_avpf(const SalMediaDescription *md) {
	int i;
	if (md->nb_streams == 0) return FALSE;
219
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
220 221 222 223 224 225 226 227 228
		if (!sal_stream_description_active(&md->streams[i])) continue;
		if (sal_stream_description_has_avpf(&md->streams[i]) != TRUE) return FALSE;
	}
	return TRUE;
}

bool_t sal_media_description_has_srtp(const SalMediaDescription *md) {
	int i;
	if (md->nb_streams == 0) return FALSE;
229
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
230 231 232 233 234 235
		if (!sal_stream_description_active(&md->streams[i])) continue;
		if (sal_stream_description_has_srtp(&md->streams[i]) != TRUE) return FALSE;
	}
	return TRUE;
}

johan's avatar
johan committed
236 237 238
bool_t sal_media_description_has_dtls(const SalMediaDescription *md) {
	int i;
	if (md->nb_streams == 0) return FALSE;
239
	for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++) {
johan's avatar
johan committed
240 241 242 243 244 245
		if (!sal_stream_description_active(&md->streams[i])) continue;
		if (sal_stream_description_has_dtls(&md->streams[i]) != TRUE) return FALSE;
	}
	return TRUE;
}

246 247 248 249 250 251 252 253 254 255 256 257 258
/*
static bool_t fmtp_equals(const char *p1, const char *p2){
	if (p1 && p2 && strcmp(p1,p2)==0) return TRUE;
	if (p1==NULL && p2==NULL) return TRUE;
	return FALSE;
}
*/

static bool_t payload_type_equals(const PayloadType *p1, const PayloadType *p2){
	if (p1->type!=p2->type) return FALSE;
	if (strcmp(p1->mime_type,p2->mime_type)!=0) return FALSE;
	if (p1->clock_rate!=p2->clock_rate) return FALSE;
	if (p1->channels!=p2->channels) return FALSE;
259
	if (payload_type_get_number(p1) != payload_type_get_number(p2)) return FALSE;
260 261 262 263 264
	/*
	 Do not compare fmtp right now: they are modified internally when the call is started
	*/
	/*
	if (!fmtp_equals(p1->recv_fmtp,p2->recv_fmtp) ||
265
		!fmtp_equals(p1->send_fmtp,p2->send_fmtp))
266 267 268 269 270
		return FALSE;
	*/
	return TRUE;
}

Simon Morlat's avatar
Simon Morlat committed
271 272 273 274
static bool_t is_recv_only(PayloadType *p){
	return (p->flags & PAYLOAD_TYPE_FLAG_CAN_RECV) && ! (p->flags & PAYLOAD_TYPE_FLAG_CAN_SEND);
}

275 276 277 278 279 280 281 282
static bool_t payload_list_equals(const MSList *l1, const MSList *l2){
	const MSList *e1,*e2;
	for(e1=l1,e2=l2;e1!=NULL && e2!=NULL; e1=e1->next,e2=e2->next){
		PayloadType *p1=(PayloadType*)e1->data;
		PayloadType *p2=(PayloadType*)e2->data;
		if (!payload_type_equals(p1,p2))
			return FALSE;
	}
Simon Morlat's avatar
Simon Morlat committed
283 284 285 286 287 288
	if (e1!=NULL){
		/*skip possible recv-only payloads*/
		for(;e1!=NULL && is_recv_only((PayloadType*)e1->data);e1=e1->next){
			ms_message("Skipping recv-only payload type...");
		}
	}
289 290 291 292 293 294 295
	if (e1!=NULL || e2!=NULL){
		/*means one list is longer than the other*/
		return FALSE;
	}
	return TRUE;
}

296 297
int sal_stream_description_equals(const SalStreamDescription *sd1, const SalStreamDescription *sd2) {
	int result = SAL_MEDIA_DESCRIPTION_UNCHANGED;
298
	int i;
299 300 301 302

	/* A different proto should result in SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED but the encryption change
	   needs a stream restart for now, so use SAL_MEDIA_DESCRIPTION_CODEC_CHANGED */
	if (sd1->proto != sd2->proto) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
303 304
	for (i = 0; i < SAL_CRYPTO_ALGO_MAX; i++) {
		if ((sd1->crypto[i].tag != sd2->crypto[i].tag)
305 306 307 308 309
			|| (sd1->crypto[i].algo != sd2->crypto[i].algo)){
			result|=SAL_MEDIA_DESCRIPTION_CRYPTO_POLICY_CHANGED;
		}
		if ((strncmp(sd1->crypto[i].master_key, sd2->crypto[i].master_key, sizeof(sd1->crypto[i].master_key) - 1))) {
			result |= SAL_MEDIA_DESCRIPTION_CRYPTO_KEYS_CHANGED;
310 311
		}
	}
312 313 314

	if (sd1->type != sd2->type) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
	if (strcmp(sd1->rtp_addr, sd2->rtp_addr) != 0) result |= SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED;
315 316
	if (sd1->rtp_addr[0]!='\0' && sd2->rtp_addr[0]!='\0' && ms_is_multicast(sd1->rtp_addr) != ms_is_multicast(sd2->rtp_addr))
			result |= SAL_MEDIA_DESCRIPTION_NETWORK_XXXCAST_CHANGED;
317 318 319 320 321 322 323 324 325 326 327
	if (sd1->rtp_port != sd2->rtp_port) {
		if ((sd1->rtp_port == 0) || (sd2->rtp_port == 0)) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
		else result |= SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED;
	}
	if (strcmp(sd1->rtcp_addr, sd2->rtcp_addr) != 0) result |= SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED;
	if (sd1->rtcp_port != sd2->rtcp_port) result |= SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED;
	if (!payload_list_equals(sd1->payloads, sd2->payloads)) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
	if (sd1->bandwidth != sd2->bandwidth) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
	if (sd1->ptime != sd2->ptime) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
	if (sd1->dir != sd2->dir) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;

jehan's avatar
jehan committed
328 329 330 331
	/*DTLS*/
	if (sd1->dtls_role != sd2->dtls_role) result |= SAL_MEDIA_DESCRIPTION_CRYPTO_KEYS_CHANGED;
	if (strcmp(sd1->dtls_fingerprint, sd2->dtls_fingerprint) != 0) result |= SAL_MEDIA_DESCRIPTION_CRYPTO_KEYS_CHANGED;

332
	return result;
333 334
}

335 336
int sal_media_description_equals(const SalMediaDescription *md1, const SalMediaDescription *md2) {
	int result = SAL_MEDIA_DESCRIPTION_UNCHANGED;
337
	int i;
338 339

	if (strcmp(md1->addr, md2->addr) != 0) result |= SAL_MEDIA_DESCRIPTION_NETWORK_CHANGED;
340 341
	if (md1->addr[0]!='\0' && md2->addr[0]!='\0' && ms_is_multicast(md1->addr) != ms_is_multicast(md2->addr))
		result |= SAL_MEDIA_DESCRIPTION_NETWORK_XXXCAST_CHANGED;
342
	if (md1->nb_streams != md2->nb_streams) result |= SAL_MEDIA_DESCRIPTION_STREAMS_CHANGED;
343
	if (md1->bandwidth != md2->bandwidth) result |= SAL_MEDIA_DESCRIPTION_CODEC_CHANGED;
344
	for(i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_STREAMS; ++i){
345
		if (!sal_stream_description_active(&md1->streams[i]) && !sal_stream_description_active(&md2->streams[i])) continue;
346
		result |= sal_stream_description_equals(&md1->streams[i], &md2->streams[i]);
347
	}
348
	return result;
349
}
jehan's avatar
jehan committed
350 351 352 353 354 355 356 357
static void assign_address(SalAddress** address, const char *value){
	if (*address){
		sal_address_destroy(*address);
		*address=NULL;
	}
	if (value)
		*address=sal_address_new(value);
}
358

Simon Morlat's avatar
Simon Morlat committed
359 360 361 362 363 364 365 366
static void assign_string(char **str, const char *arg){
	if (*str){
		ms_free(*str);
		*str=NULL;
	}
	if (arg)
		*str=ms_strdup(arg);
}
jehan's avatar
jehan committed
367

jehan's avatar
jehan committed
368
void sal_op_set_contact_address(SalOp *op, const SalAddress *address){
369 370
	if (((SalOpBase*)op)->contact_address) sal_address_destroy(((SalOpBase*)op)->contact_address);
	((SalOpBase*)op)->contact_address=address?sal_address_clone(address):NULL;
jehan's avatar
jehan committed
371 372 373 374
}
const SalAddress* sal_op_get_contact_address(const SalOp *op) {
	return ((SalOpBase*)op)->contact_address;
}
375 376 377 378 379 380

const SalAddress*sal_op_get_remote_contact_address(const SalOp* op)
{
	return ((SalOpBase*)op)->remote_contact_address;
}

jehan's avatar
jehan committed
381 382 383 384 385 386 387 388 389
#define SET_PARAM(op,name) \
		char* name##_string=NULL; \
		assign_address(&((SalOpBase*)op)->name##_address,name); \
		if (((SalOpBase*)op)->name##_address) { \
			name##_string=sal_address_as_string(((SalOpBase*)op)->name##_address); \
		}\
		assign_string(&((SalOpBase*)op)->name,name##_string); \
		if(name##_string) ms_free(name##_string);

390

Simon Morlat's avatar
Simon Morlat committed
391
void sal_op_set_route(SalOp *op, const char *route){
392 393 394 395 396 397 398 399 400 401 402 403
	char* route_string=(void *)0;
	SalOpBase* op_base = (SalOpBase*)op;
	if (op_base->route_addresses) {
		ms_list_for_each(op_base->route_addresses,(void (*)(void *))sal_address_destroy);
		op_base->route_addresses=ms_list_free(op_base->route_addresses);
	}
	if (route) {
		op_base->route_addresses=ms_list_append(NULL,NULL);
		assign_address((SalAddress**)&(op_base->route_addresses->data),route);
		route_string=sal_address_as_string((SalAddress*)op_base->route_addresses->data); \
	}
	assign_string(&op_base->route,route_string); \
Simon Morlat's avatar
Simon Morlat committed
404
	if(route_string) ms_free(route_string);
jehan's avatar
jehan committed
405
}
406 407
const MSList* sal_op_get_route_addresses(const SalOp *op) {
	return ((SalOpBase*)op)->route_addresses;
jehan's avatar
jehan committed
408 409 410 411 412
}
void sal_op_set_route_address(SalOp *op, const SalAddress *address){
	char* address_string=sal_address_as_string(address); /*can probably be optimized*/
	sal_op_set_route(op,address_string);
	ms_free(address_string);
Simon Morlat's avatar
Simon Morlat committed
413
}
414 415 416
void sal_op_add_route_address(SalOp *op, const SalAddress *address){
	SalOpBase* op_base = (SalOpBase*)op;
	if (op_base->route_addresses) {
jehan's avatar
jehan committed
417
		op_base->route_addresses=ms_list_append(op_base->route_addresses,(void*)sal_address_clone(address));
418 419
	} else {
		sal_op_set_route_address(op,address);
420 421
	}
}
422 423 424 425 426 427 428
void sal_op_set_realm(SalOp *op, const char *realm){
	SalOpBase* op_base = (SalOpBase*)op;
	if (op_base->realm != NULL){
		ms_free(op_base->realm);
	}
	op_base->realm = ms_strdup(realm);
}
Simon Morlat's avatar
Simon Morlat committed
429
void sal_op_set_from(SalOp *op, const char *from){
jehan's avatar
jehan committed
430 431 432 433 434 435
	SET_PARAM(op,from);
}
void sal_op_set_from_address(SalOp *op, const SalAddress *from){
	char* address_string=sal_address_as_string(from); /*can probably be optimized*/
	sal_op_set_from(op,address_string);
	ms_free(address_string);
Simon Morlat's avatar
Simon Morlat committed
436 437
}
void sal_op_set_to(SalOp *op, const char *to){
jehan's avatar
jehan committed
438 439 440 441 442 443
	SET_PARAM(op,to);
}
void sal_op_set_to_address(SalOp *op, const SalAddress *to){
	char* address_string=sal_address_as_string(to); /*can probably be optimized*/
	sal_op_set_to(op,address_string);
	ms_free(address_string);
Simon Morlat's avatar
Simon Morlat committed
444 445 446 447 448 449
}

void sal_op_set_user_pointer(SalOp *op, void *up){
	((SalOpBase*)op)->user_pointer=up;
}

450 451 452 453
Sal *sal_op_get_sal(const SalOp *op){
	return ((SalOpBase*)op)->root;
}

Simon Morlat's avatar
Simon Morlat committed
454 455 456
const char *sal_op_get_from(const SalOp *op){
	return ((SalOpBase*)op)->from;
}
jehan's avatar
jehan committed
457 458 459
const SalAddress *sal_op_get_from_address(const SalOp *op){
	return ((SalOpBase*)op)->from_address;
}
Simon Morlat's avatar
Simon Morlat committed
460 461 462 463 464

const char *sal_op_get_to(const SalOp *op){
	return ((SalOpBase*)op)->to;
}

jehan's avatar
jehan committed
465 466 467
const SalAddress *sal_op_get_to_address(const SalOp *op){
	return ((SalOpBase*)op)->to_address;
}
468

Simon Morlat's avatar
Simon Morlat committed
469
const char *sal_op_get_route(const SalOp *op){
470 471 472
#ifdef BELLE_SIP
ms_fatal("sal_op_get_route not supported, use sal_op_get_route_addresses instead");
#endif
Simon Morlat's avatar
Simon Morlat committed
473 474 475
	return ((SalOpBase*)op)->route;
}

476 477 478 479
const char *sal_op_get_remote_ua(const SalOp *op){
	return ((SalOpBase*)op)->remote_ua;
}

Simon Morlat's avatar
Simon Morlat committed
480 481 482 483
void *sal_op_get_user_pointer(const SalOp *op){
	return ((SalOpBase*)op)->user_pointer;
}

484 485 486 487
const char *sal_op_get_proxy(const SalOp *op){
	return ((SalOpBase*)op)->route;
}

488 489 490
const char *sal_op_get_network_origin(const SalOp *op){
	return ((SalOpBase*)op)->origin;
}
jehan's avatar
jehan committed
491 492 493
const char* sal_op_get_call_id(const SalOp *op) {
	return  ((SalOpBase*)op)->call_id;
}
494
char* sal_op_get_dialog_id(const SalOp *op) {
495
	if (op->dialog != NULL) {
496
		return ms_strdup_printf("%s;to-tag=%s;from-tag=%s", ((SalOpBase*)op)->call_id,
497 498 499 500
			belle_sip_dialog_get_remote_tag(op->dialog), belle_sip_dialog_get_local_tag(op->dialog));
	}
	return NULL;

501
}
Simon Morlat's avatar
Simon Morlat committed
502 503 504 505 506
void __sal_op_init(SalOp *b, Sal *sal){
	memset(b,0,sizeof(SalOpBase));
	((SalOpBase*)b)->root=sal;
}

507
void __sal_op_set_network_origin(SalOp *op, const char *origin){
jehan's avatar
jehan committed
508
	SET_PARAM(op,origin);
509 510
}

511 512
void __sal_op_set_remote_contact(SalOp *op, const char* remote_contact){
	SET_PARAM(op,remote_contact);
513
}
514

jehan's avatar
jehan committed
515 516 517 518 519
void __sal_op_set_network_origin_address(SalOp *op, SalAddress *origin){
	char* address_string=sal_address_as_string(origin); /*can probably be optimized*/
	__sal_op_set_network_origin(op,address_string);
	ms_free(address_string);
}
520

Simon Morlat's avatar
Simon Morlat committed
521 522
void __sal_op_free(SalOp *op){
	SalOpBase *b=(SalOpBase *)op;
Simon Morlat's avatar
Simon Morlat committed
523 524 525 526 527 528 529 530
	if (b->from_address){
		sal_address_destroy(b->from_address);
		b->from_address=NULL;
	}
	if (b->to_address){
		sal_address_destroy(b->to_address);
		b->to_address=NULL;
	}
531

Simon Morlat's avatar
Simon Morlat committed
532 533 534 535
	if (b->service_route){
		sal_address_destroy(b->service_route);
		b->service_route=NULL;
	}
536

Simon Morlat's avatar
Simon Morlat committed
537 538 539 540
	if (b->origin_address){
		sal_address_destroy(b->origin_address);
		b->origin_address=NULL;
	}
541

Simon Morlat's avatar
Simon Morlat committed
542 543 544 545 546 547 548 549 550 551 552 553
	if (b->from) {
		ms_free(b->from);
		b->from=NULL;
	}
	if (b->to) {
		ms_free(b->to);
		b->to=NULL;
	}
	if (b->route) {
		ms_free(b->route);
		b->route=NULL;
	}
554 555 556 557
	if (b->realm) {
		ms_free(b->realm);
		b->realm=NULL;
	}
558 559 560
	if (b->contact_address) {
		sal_address_destroy(b->contact_address);
	}
561 562 563 564
	if (b->origin){
		ms_free(b->origin);
		b->origin=NULL;
	}
565 566 567 568
	if (b->remote_ua){
		ms_free(b->remote_ua);
		b->remote_ua=NULL;
	}
569 570 571 572
	if (b->remote_contact){
		ms_free(b->remote_contact);
		b->remote_contact=NULL;
	}
573 574 575
	if (b->remote_contact_address){
		sal_address_destroy(b->remote_contact_address);
	}
Simon Morlat's avatar
Simon Morlat committed
576
	if (b->local_media)
577
		sal_media_description_unref(b->local_media);
Simon Morlat's avatar
Simon Morlat committed
578
	if (b->remote_media)
579
		sal_media_description_unref(b->remote_media);
jehan's avatar
jehan committed
580 581
	if (b->call_id)
		ms_free((void*)b->call_id);
jehan's avatar
jehan committed
582 583 584
	if (b->service_route) {
		sal_address_destroy(b->service_route);
	}
Simon Morlat's avatar
Simon Morlat committed
585 586 587 588
	if (b->route_addresses){
		ms_list_for_each(b->route_addresses,(void (*)(void*)) sal_address_destroy);
		b->route_addresses=ms_list_free(b->route_addresses);
	}
Simon Morlat's avatar
Simon Morlat committed
589 590 591 592
	if (b->recv_custom_headers)
		sal_custom_header_free(b->recv_custom_headers);
	if (b->sent_custom_headers)
		sal_custom_header_free(b->sent_custom_headers);
Simon Morlat's avatar
Simon Morlat committed
593 594
	ms_free(op);
}
595

jehan's avatar
jehan committed
596 597 598
SalAuthInfo* sal_auth_info_new() {
	return ms_new0(SalAuthInfo,1);
}
599

jehan's avatar
jehan committed
600 601 602 603 604
SalAuthInfo* sal_auth_info_clone(const SalAuthInfo* auth_info) {
	SalAuthInfo* new_auth_info=sal_auth_info_new();
	new_auth_info->username=auth_info->username?ms_strdup(auth_info->username):NULL;
	new_auth_info->userid=auth_info->userid?ms_strdup(auth_info->userid):NULL;
	new_auth_info->realm=auth_info->realm?ms_strdup(auth_info->realm):NULL;
605
	new_auth_info->domain=auth_info->realm?ms_strdup(auth_info->domain):NULL;
jehan's avatar
jehan committed
606 607 608
	new_auth_info->password=auth_info->password?ms_strdup(auth_info->password):NULL;
	return new_auth_info;
}
609

610
void sal_auth_info_delete(SalAuthInfo* auth_info) {
jehan's avatar
jehan committed
611 612 613
	if (auth_info->username) ms_free(auth_info->username);
	if (auth_info->userid) ms_free(auth_info->userid);
	if (auth_info->realm) ms_free(auth_info->realm);
614
	if (auth_info->domain) ms_free(auth_info->domain);
jehan's avatar
jehan committed
615
	if (auth_info->password) ms_free(auth_info->password);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
616
	if (auth_info->ha1) ms_free(auth_info->ha1);
617 618
	if (auth_info->certificates) sal_certificates_chain_delete(auth_info->certificates);
	if (auth_info->key) sal_signing_key_delete(auth_info->key);
619
	ms_free(auth_info);
jehan's avatar
jehan committed
620
}
621

622 623


jehan's avatar
jehan committed
624 625
const char* sal_stream_type_to_string(SalStreamType type) {
	switch (type) {
626 627 628
	case SalAudio: return "audio";
	case SalVideo: return "video";
	case SalText: return "text";
jehan's avatar
jehan committed
629 630 631 632
	default: return "other";
	}
}

633 634 635 636 637
const char *sal_stream_description_get_type_as_string(const SalStreamDescription *desc){
	if (desc->type==SalOther) return desc->typeother;
	else return sal_stream_type_to_string(desc->type);
}

jehan's avatar
jehan committed
638 639 640 641
const char* sal_media_proto_to_string(SalMediaProto type) {
	switch (type) {
	case SalProtoRtpAvp:return "RTP/AVP";
	case SalProtoRtpSavp:return "RTP/SAVP";
johan's avatar
johan committed
642
	case SalProtoUdpTlsRtpSavp:return "UDP/TLS/RTP/SAVP";
643 644
	case SalProtoRtpAvpf:return "RTP/AVPF";
	case SalProtoRtpSavpf:return "RTP/SAVPF";
johan's avatar
johan committed
645
	case SalProtoUdpTlsRtpSavpf:return "UDP/TLS/RTP/SAVPF";
jehan's avatar
jehan committed
646 647 648 649
	default: return "unknown";
	}
}

650 651 652 653 654
const char *sal_stream_description_get_proto_as_string(const SalStreamDescription *desc){
	if (desc->proto==SalProtoOther) return desc->proto_other;
	else return sal_media_proto_to_string(desc->proto);
}

jehan's avatar
jehan committed
655 656 657 658 659 660 661 662 663

const char* sal_stream_dir_to_string(SalStreamDir type) {
	switch (type) {
	case SalStreamSendRecv:return "sendrecv";
	case SalStreamSendOnly:return "sendonly";
	case SalStreamRecvOnly:return "recvonly";
	case SalStreamInactive:return "inative";
	default: return "unknown";
	}
jehan's avatar
jehan committed
664

jehan's avatar
jehan committed
665
}
jehan's avatar
jehan committed
666 667 668 669 670 671 672 673 674

const char* sal_reason_to_string(const SalReason reason) {
	switch (reason) {
	case SalReasonDeclined : return "SalReasonDeclined";
	case SalReasonBusy: return "SalReasonBusy";
	case SalReasonRedirect: return "SalReasonRedirect";
	case SalReasonTemporarilyUnavailable: return "SalReasonTemporarilyUnavailable";
	case SalReasonNotFound: return "SalReasonNotFound";
	case SalReasonDoNotDisturb: return "SalReasonDoNotDisturb";
675
	case SalReasonUnsupportedContent: return "SalReasonUnsupportedContent";
jehan's avatar
jehan committed
676 677
	case SalReasonForbidden: return "SalReasonForbidden";
	case SalReasonUnknown: return "SalReasonUnknown";
678
	case SalReasonServiceUnavailable: return "SalReasonServiceUnavailable";
679
	case SalReasonNotAcceptable: return "SalReasonNotAcceptable";
jehan's avatar
jehan committed
680 681 682
	default: return "Unkown reason";
	}
}
jehan's avatar
jehan committed
683 684 685 686 687 688 689 690 691
const SalAddress* sal_op_get_service_route(const SalOp *op) {
	return ((SalOpBase*)op)->service_route;
}
void sal_op_set_service_route(SalOp *op,const SalAddress* service_route) {
	if (((SalOpBase*)op)->service_route)
		sal_address_destroy(((SalOpBase*)op)->service_route);

	((SalOpBase*)op)->service_route=service_route?sal_address_clone(service_route):NULL;
}
jehan's avatar
jehan committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

const char* sal_presence_status_to_string(const SalPresenceStatus status) {
	switch (status) {
	case SalPresenceOffline: return "SalPresenceOffline";
	case SalPresenceOnline: return "SalPresenceOnline";
	case SalPresenceBusy: return "SalPresenceBusy";
	case SalPresenceBerightback: return "SalPresenceBerightback";
	case SalPresenceAway: return "SalPresenceAway";
	case SalPresenceOnthephone: return "SalPresenceOnthephone";
	case SalPresenceOuttolunch: return "SalPresenceOuttolunch";
	case SalPresenceDonotdisturb: return "SalPresenceDonotdisturb";
	case SalPresenceMoved: return "SalPresenceMoved";
	case SalPresenceAltService: return "SalPresenceAltService";
	default : return "unknown";
	}

}
jehan's avatar
jehan committed
709 710 711 712 713 714 715 716 717 718 719
const char* sal_privacy_to_string(SalPrivacy privacy) {
	switch(privacy) {
	case SalPrivacyUser: return "user";
	case SalPrivacyHeader: return "header";
	case SalPrivacySession: return "session";
	case SalPrivacyId: return "id";
	case SalPrivacyNone: return "none";
	case SalPrivacyCritical: return "critical";
	default: return NULL;
	}
}
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

static void remove_trailing_spaces(char *line){
	int i;
	for(i=strlen(line)-1;i>=0;--i){
		if (isspace(line[i])) line[i]='\0';
		else break;
	}
}

static int line_get_value(const char *input, const char *key, char *value, size_t value_size, int *read){
	const char *end=strchr(input,'\n');
	char line[256]={0};
	char key_candidate[256];
	char *equal;
	size_t len;
	if (!end) len=strlen(input);
	else len=end +1 -input;
	*read=len;
	strncpy(line,input,MIN(len,sizeof(line)));
	equal=strchr(line,'=');
	if (!equal) return FALSE;
	*equal='\0';
	if (sscanf(line,"%s",key_candidate)!=1) return FALSE;
	if (strcasecmp(key,key_candidate)==0){
		equal++;
		remove_trailing_spaces(equal);
		strncpy(value,equal,value_size-1);
		value[value_size-1]='\0';
		return TRUE;
	}
	return FALSE;
}

int sal_lines_get_value(const char *data, const char *key, char *value, size_t value_size){
	int read=0;
755

756 757 758 759 760 761 762 763 764
	do{
		if (line_get_value(data,key,value,value_size,&read))
			return TRUE;
		data+=read;
	}while(read!=0);
	return FALSE;
}

int sal_body_has_type(const SalBody *body, const char *type, const char *subtype){
765
	return body->type && body->subtype
766 767 768 769
		&& strcmp(body->type,type)==0
		&& strcmp(body->subtype,subtype)==0;
}

770 771 772
belle_sip_stack_t *sal_get_belle_sip_stack(Sal *sal) {
	return sal->stack;
}
773

774 775 776
char* sal_op_get_public_uri(SalOp *op) {
	if (op && op->refresher) {
		return belle_sip_refresher_get_public_uri(op->refresher);
777 778 779
	}
	return NULL;
}