sal_impl.c 31.5 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
linphone
Copyright (C) 2012  Belledonne Communications, Grenoble, France

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

jehan's avatar
jehan committed
20
#include "sal_impl.h"
Yann Diorcet's avatar
Yann Diorcet committed
21

Simon Morlat's avatar
Simon Morlat committed
22 23 24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
jehan's avatar
jehan committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
rfc3323
4.2 Expressing Privacy Preferences
When a Privacy header is constructed, it MUST consist of either the
   value 'none', or one or more of the values 'user', 'header' and
   'session' (each of which MUST appear at most once) which MAY in turn
   be followed by the 'critical' indicator.
 */
void sal_op_set_privacy_from_message(SalOp* op,belle_sip_message_t* msg) {
	belle_sip_header_privacy_t* privacy = belle_sip_message_get_header_by_type(msg,belle_sip_header_privacy_t);
	if (!privacy) {
		sal_op_set_privacy(op,SalPrivacyNone);
	} else {
		belle_sip_list_t* privacy_list=belle_sip_header_privacy_get_privacy(privacy);
		sal_op_set_privacy(op,0);
		for (;privacy_list!=NULL;privacy_list=privacy_list->next) {
			char* privacy_value=(char*)privacy_list->data;
			if(strcmp(sal_privacy_to_string(SalPrivacyCritical),privacy_value) == 0)
				sal_op_set_privacy(op,sal_op_get_privacy(op)|SalPrivacyCritical);
			if(strcmp(sal_privacy_to_string(SalPrivacyHeader),privacy_value) == 0)
							sal_op_set_privacy(op,sal_op_get_privacy(op)|SalPrivacyHeader);
			if(strcmp(sal_privacy_to_string(SalPrivacyId),privacy_value) == 0)
							sal_op_set_privacy(op,sal_op_get_privacy(op)|SalPrivacyId);
			if(strcmp(sal_privacy_to_string(SalPrivacyNone),privacy_value) == 0) {
				sal_op_set_privacy(op,SalPrivacyNone);
				break;
			}
			if(strcmp(sal_privacy_to_string(SalPrivacySession),privacy_value) == 0)
							sal_op_set_privacy(op,sal_op_get_privacy(op)|SalPrivacySession);
			if(strcmp(sal_privacy_to_string(SalPrivacyUser),privacy_value) == 0)
										sal_op_set_privacy(op,sal_op_get_privacy(op)|SalPrivacyUser);
		}
	}
}
Simon Morlat's avatar
Simon Morlat committed
59 60
static void set_tls_properties(Sal *ctx);

Yann Diorcet's avatar
Yann Diorcet committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
void _belle_sip_log(belle_sip_log_level lev, const char *fmt, va_list args) {
	int ortp_level;
	switch(lev) {
		case BELLE_SIP_LOG_FATAL:
			ortp_level=ORTP_FATAL;
		break;
		case BELLE_SIP_LOG_ERROR:
			ortp_level=ORTP_ERROR;
		break;
		case BELLE_SIP_LOG_WARNING:
			ortp_level=ORTP_WARNING;
		break;
		case BELLE_SIP_LOG_MESSAGE:
			ortp_level=ORTP_MESSAGE;
		break;
		case BELLE_SIP_LOG_DEBUG:
jehan's avatar
jehan committed
77
		default:
jehan's avatar
jehan committed
78
			ortp_level=ORTP_DEBUG;
jehan's avatar
jehan committed
79
			break;
Yann Diorcet's avatar
Yann Diorcet committed
80 81 82 83 84 85
	}
	if (ortp_log_level_enabled(ortp_level)){
		ortp_logv(ortp_level,fmt,args);
	}
}

jehan's avatar
jehan committed
86 87 88
void sal_enable_logs(){
	belle_sip_set_log_level(BELLE_SIP_LOG_MESSAGE);
}
Simon Morlat's avatar
Simon Morlat committed
89

jehan's avatar
jehan committed
90 91 92
void sal_disable_logs() {
	belle_sip_set_log_level(BELLE_SIP_LOG_ERROR);
}
Simon Morlat's avatar
Simon Morlat committed
93

94
void sal_add_pending_auth(Sal *sal, SalOp *op){
95 96 97
	if (ms_list_find(sal->pending_auths,op)==NULL){
		sal->pending_auths=ms_list_append(sal->pending_auths,sal_op_ref(op));
	}
jehan's avatar
jehan committed
98 99
}

Simon Morlat's avatar
Simon Morlat committed
100
void sal_remove_pending_auth(Sal *sal, SalOp *op){
101 102 103 104
	if (ms_list_find(sal->pending_auths,op)){
		sal->pending_auths=ms_list_remove(sal->pending_auths,op);
		sal_op_unref(op);
	}
jehan's avatar
jehan committed
105 106
}

107
void sal_process_authentication(SalOp *op) {
Ghislain MARY's avatar
Ghislain MARY committed
108
	belle_sip_request_t* request=belle_sip_transaction_get_request((belle_sip_transaction_t*)op->pending_auth_transaction);
jehan's avatar
jehan committed
109
	bool_t is_within_dialog=FALSE;
110 111
	belle_sip_list_t* auth_list=NULL;
	belle_sip_auth_event_t* auth_event;
112 113 114 115
	belle_sip_response_t *response=belle_sip_transaction_get_response((belle_sip_transaction_t*)op->pending_auth_transaction);
	
	sal_add_pending_auth(op->base.root,op);
	
jehan's avatar
jehan committed
116
	if (op->dialog && belle_sip_dialog_get_state(op->dialog)==BELLE_SIP_DIALOG_CONFIRMED) {
117
		request = belle_sip_dialog_create_request_from(op->dialog,(const belle_sip_request_t *)request);
jehan's avatar
jehan committed
118 119 120 121 122
		is_within_dialog=TRUE;
	} else {
		belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_AUTHORIZATION);
		belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_PROXY_AUTHORIZATION);
	}
123
	if (belle_sip_provider_add_authorization(op->base.root->prov,request,response,&auth_list)) {
jehan's avatar
jehan committed
124 125
		if (is_within_dialog) {
			sal_op_send_request(op,request);
126 127
		} else {
			sal_op_resend_request(op,request);
jehan's avatar
jehan committed
128
		}
129
		sal_remove_pending_auth(op->base.root,op);
jehan's avatar
jehan committed
130 131
	}else {
		ms_message("No auth info found for [%s]",sal_op_get_from(op));
jehan's avatar
jehan committed
132 133 134
		if (is_within_dialog) {
			belle_sip_object_unref(request);
		}
135 136
		if (op->auth_info) sal_auth_info_delete(op->auth_info);
		auth_event=(belle_sip_auth_event_t*)(auth_list->data);
137
		op->auth_info=sal_auth_info_create(auth_event);
138
		belle_sip_list_free_with_data(auth_list,(void (*)(void*))belle_sip_auth_event_destroy);
jehan's avatar
jehan committed
139 140 141
	}

}
Simon Morlat's avatar
Simon Morlat committed
142

jehan's avatar
jehan committed
143 144 145
static void process_dialog_terminated(void *sal, const belle_sip_dialog_terminated_event_t *event){
	belle_sip_dialog_t* dialog =  belle_sip_dialog_terminated_get_dialog(event);
	SalOp* op = belle_sip_dialog_get_application_data(dialog);
146
	if (op && op->callbacks.process_dialog_terminated) {
jehan's avatar
jehan committed
147 148
		op->callbacks.process_dialog_terminated(op,event);
	} else {
149
		ms_error("sal process_dialog_terminated no op found for this dialog [%p], ignoring",dialog);
jehan's avatar
jehan committed
150
	}
jehan's avatar
jehan committed
151
}
Simon Morlat's avatar
Simon Morlat committed
152

jehan's avatar
jehan committed
153
static void process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){
jehan's avatar
jehan committed
154 155 156 157 158 159 160 161 162
	belle_sip_client_transaction_t*client_transaction;
	SalOp* op;
	if (belle_sip_object_is_instance_of(BELLE_SIP_OBJECT(belle_sip_io_error_event_get_source(event)),BELLE_SIP_TYPE_ID(belle_sip_client_transaction_t))) {
		client_transaction=BELLE_SIP_CLIENT_TRANSACTION(belle_sip_io_error_event_get_source(event));
		op = (SalOp*)belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction));
		if (op->callbacks.process_io_error) {
				op->callbacks.process_io_error(op,event);
		}
	} else {
163 164
		/*ms_error("sal process_io_error not implemented yet for non transaction");*/
		/*nop, because already handle at transaction layer*/
jehan's avatar
jehan committed
165
	}
jehan's avatar
jehan committed
166
}
Simon Morlat's avatar
Simon Morlat committed
167

jehan's avatar
jehan committed
168
static void process_request_event(void *sal, const belle_sip_request_event_t *event) {
jehan's avatar
jehan committed
169
	SalOp* op=NULL;
jehan's avatar
jehan committed
170 171 172 173
	belle_sip_request_t* req = belle_sip_request_event_get_request(event);
	belle_sip_dialog_t* dialog=belle_sip_request_event_get_dialog(event);
	belle_sip_header_address_t* origin_address;
	belle_sip_header_address_t* address;
jehan's avatar
jehan committed
174
	belle_sip_header_from_t* from_header;
jehan's avatar
jehan committed
175
	belle_sip_header_to_t* to;
jehan's avatar
jehan committed
176
	belle_sip_response_t* resp;
177
	belle_sip_header_t *evh;
178

jehan's avatar
jehan committed
179 180
	from_header=belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(req),belle_sip_header_from_t);

jehan's avatar
jehan committed
181 182 183 184
	if (dialog) {
		op=(SalOp*)belle_sip_dialog_get_application_data(dialog);
	} else if (strcmp("INVITE",belle_sip_request_get_method(req))==0) {
		op=sal_op_new((Sal*)sal);
jehan's avatar
jehan committed
185
		op->dir=SalOpDirIncoming;
jehan's avatar
jehan committed
186
		sal_op_call_fill_cbs(op);
187
	} else if (strcmp("SUBSCRIBE",belle_sip_request_get_method(req))==0 && (evh=belle_sip_message_get_header(BELLE_SIP_MESSAGE(req),"Event"))!=NULL) {
jehan's avatar
jehan committed
188 189
		op=sal_op_new((Sal*)sal);
		op->dir=SalOpDirIncoming;
190 191 192 193
		if (strncmp(belle_sip_header_get_unparsed_value(evh),"presence",strlen("presence"))==0){
			sal_op_presence_fill_cbs(op);
		}else
			sal_op_subscribe_fill_cbs(op);
jehan's avatar
jehan committed
194
	} else if (strcmp("MESSAGE",belle_sip_request_get_method(req))==0) {
195 196 197
		op=sal_op_new((Sal*)sal);
		op->dir=SalOpDirIncoming;
		sal_op_message_fill_cbs(op);
jehan's avatar
jehan committed
198
	} else if (strcmp("OPTIONS",belle_sip_request_get_method(req))==0) {
199 200 201
		resp=belle_sip_response_create_from_request(req,200);
		belle_sip_provider_send_response(((Sal*)sal)->prov,resp);
		return;
Simon Morlat's avatar
Simon Morlat committed
202
	}else if (strcmp("INFO",belle_sip_request_get_method(req))==0) {
203 204 205
		resp=belle_sip_response_create_from_request(req,481);/*INFO out of call dialogs are not allowed*/
		belle_sip_provider_send_response(((Sal*)sal)->prov,resp);
		return;
206
	}else {
jehan's avatar
jehan committed
207
		ms_error("sal process_request_event not implemented yet for method [%s]",belle_sip_request_get_method(req));
208
		resp=belle_sip_response_create_from_request(req,501);
jehan's avatar
jehan committed
209
		belle_sip_provider_send_response(((Sal*)sal)->prov,resp);
jehan's avatar
jehan committed
210
		return;
jehan's avatar
jehan committed
211 212 213
	}

	if (!op->base.from_address)  {
jehan's avatar
jehan committed
214 215
		address=belle_sip_header_address_create(belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(from_header))
														,belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(from_header)));
jehan's avatar
jehan committed
216 217 218
		sal_op_set_from_address(op,(SalAddress*)address);
		belle_sip_object_unref(address);
	}
jehan's avatar
jehan committed
219 220


jehan's avatar
jehan committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	if (!op->base.to_address) {
		to=belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(req),belle_sip_header_to_t);
		address=belle_sip_header_address_create(belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(to))
												,belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(to)));
		sal_op_set_to_address(op,(SalAddress*)address);
		belle_sip_object_unref(address);
	}

	if (!op->base.origin) {
		/*set origin uri*/
		origin_address=belle_sip_header_address_create(NULL,belle_sip_request_extract_origin(req));
		__sal_op_set_network_origin_address(op,(SalAddress*)origin_address);
		belle_sip_object_unref(origin_address);
	}
	if (!op->base.remote_ua) {
		sal_op_set_remote_ua(op,BELLE_SIP_MESSAGE(req));
	}

jehan's avatar
jehan committed
239 240 241
	if (!op->base.call_id) {
		op->base.call_id=ms_strdup(belle_sip_header_call_id_get_call_id(BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(req), belle_sip_header_call_id_t))));
	}
jehan's avatar
jehan committed
242 243 244 245
	/*It is worth noting that proxies can (and
   will) remove this header field*/
	sal_op_set_privacy_from_message(op,(belle_sip_message_t*)req);

Simon Morlat's avatar
Simon Morlat committed
246
	sal_op_assign_recv_headers(op,(belle_sip_message_t*)req);
jehan's avatar
jehan committed
247 248 249 250 251 252
	if (op->callbacks.process_request_event) {
		op->callbacks.process_request_event(op,event);
	} else {
		ms_error("sal process_request_event not implemented yet");
	}

jehan's avatar
jehan committed
253 254 255 256 257
}

static void process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
	belle_sip_client_transaction_t* client_transaction = belle_sip_response_event_get_client_transaction(event);
	belle_sip_response_t* response = belle_sip_response_event_get_response(event);
jehan's avatar
jehan committed
258
	int response_code = belle_sip_response_get_status_code(response);
259
	if (!client_transaction) {
260
		ms_warning("Discarding stateless response [%i]",response_code);
jehan's avatar
jehan committed
261
		return;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	} else {
		SalOp* op = (SalOp*)belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction));
		belle_sip_request_t* request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction));
		belle_sip_header_contact_t* original_contact;
		belle_sip_header_address_t* contact_address=NULL;
		belle_sip_header_via_t* via_header;
		belle_sip_uri_t* contact_uri;
		unsigned int contact_port;
		const char* received;
		int rport;
		bool_t contact_updated=FALSE;
		char* new_contact;

		if (op->state == SalOpStateTerminated) {
			belle_sip_message("Op is terminated, nothing to do with this [%i]",response_code);
			return;
		}
		if (!op->base.remote_ua) {
			sal_op_set_remote_ua(op,BELLE_SIP_MESSAGE(response));
jehan's avatar
jehan committed
281
		}
jehan's avatar
jehan committed
282 283 284
		if (!op->base.call_id) {
			op->base.call_id=ms_strdup(belle_sip_header_call_id_get_call_id(BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(response), belle_sip_header_call_id_t))));
		}
jehan's avatar
jehan committed
285

286 287
		sal_op_assign_recv_headers(op,(belle_sip_message_t*)response);
		
288 289
		if (op->callbacks.process_response_event) {

jehan's avatar
jehan committed
290 291 292 293 294
			if (op->base.root->nat_helper_enabled) {
				/*Fix contact if needed*/
				via_header= (belle_sip_header_via_t*)belle_sip_message_get_header(BELLE_SIP_MESSAGE(response),BELLE_SIP_VIA);
				received = belle_sip_header_via_get_received(via_header);
				rport = belle_sip_header_via_get_rport(via_header);
295 296 297 298 299
				if ((original_contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t))) {
					/*update contact with sent values in any cases*/
					contact_address=belle_sip_header_address_create(NULL,belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(original_contact)));
					sal_op_set_contact_address(op,(const SalAddress *)contact_address);
					belle_sip_object_unref(contact_address);
jehan's avatar
jehan committed
300
				}
jehan's avatar
jehan committed
301 302
				if (sal_op_get_contact(op)){
					if (received!=NULL || rport>0) {
jehan's avatar
jehan committed
303
						contact_address = BELLE_SIP_HEADER_ADDRESS(sal_address_clone(sal_op_get_contact_address(op)));
jehan's avatar
jehan committed
304 305 306 307
						contact_uri=belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact_address));
						if (received && strcmp(received,belle_sip_uri_get_host(contact_uri))!=0) {
							/*need to update host*/
							belle_sip_uri_set_host(contact_uri,received);
jehan's avatar
jehan committed
308 309
							contact_updated=TRUE;
						}
jehan's avatar
jehan committed
310 311 312 313
						contact_port =  belle_sip_uri_get_port(contact_uri);
						if (rport>0 && rport!=contact_port && (contact_port+rport)!=5060) {
							/*need to update port*/
							belle_sip_uri_set_port(contact_uri,rport);
jehan's avatar
jehan committed
314 315
							contact_updated=TRUE;
						}
jehan's avatar
jehan committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

						/*try to fix transport if needed (very unlikely)*/
						if (strcasecmp(belle_sip_header_via_get_transport(via_header),"UDP")!=0) {
							if (!belle_sip_uri_get_transport_param(contact_uri)
									||strcasecmp(belle_sip_uri_get_transport_param(contact_uri),belle_sip_header_via_get_transport(via_header))!=0) {
								belle_sip_uri_set_transport_param(contact_uri,belle_sip_header_via_get_transport_lowercase(via_header));
								contact_updated=TRUE;
							}
						} else {
							if (belle_sip_uri_get_transport_param(contact_uri)) {
								contact_updated=TRUE;
								belle_sip_uri_set_transport_param(contact_uri,NULL);
							}
						}
						if (contact_updated) {
							char* old_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(sal_op_get_contact_address(op)));
							new_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(contact_address));
							ms_message("Updating contact from [%s] to [%s] for [%p]",old_contact,new_contact,op);
							sal_op_set_contact_address(op,(const SalAddress *)contact_address);
							belle_sip_free(new_contact);
							belle_sip_free(old_contact);
						}
						if (contact_address)belle_sip_object_unref(contact_address);
jehan's avatar
jehan committed
339
					}
340
				}
jehan's avatar
jehan committed
341
			}
342 343
			
			/*handle authorization*/
344
			switch (response_code) {
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
				case 200: {
					break;
				}
				case 401:
				case 407:{
					
					/*belle_sip_transaction_set_application_data(BELLE_SIP_TRANSACTION(client_transaction),NULL);*//*remove op from trans*/
					if (op->state == SalOpStateTerminating && strcmp("BYE",belle_sip_request_get_method(request))!=0) {
						/*only bye are completed*/
						belle_sip_message("Op is in state terminating, nothing else to do ");
					} else {
						if (op->pending_auth_transaction){
							belle_sip_object_unref(op->pending_auth_transaction);
							op->pending_auth_transaction=NULL;
						}
						op->pending_auth_transaction=(belle_sip_client_transaction_t*)belle_sip_object_ref(client_transaction);
						sal_process_authentication(op);
						return;
363
					}
364
				}
jehan's avatar
jehan committed
365
			}
366 367 368 369
			op->callbacks.process_response_event(op,event);
		} else {
			ms_error("Unhandled event response [%p]",event);
		}
jehan's avatar
jehan committed
370 371 372 373
	}

}
static void process_timeout(void *user_ctx, const belle_sip_timeout_event_t *event) {
374
	belle_sip_client_transaction_t* client_transaction = belle_sip_timeout_event_get_client_transaction(event);
jehan's avatar
jehan committed
375
	SalOp* op = (SalOp*)belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction));
376
	if (op && op->callbacks.process_timeout) {
jehan's avatar
jehan committed
377
		op->callbacks.process_timeout(op,event);
378
	} else {
jehan's avatar
jehan committed
379 380 381 382
		ms_error("Unhandled event timeout [%p]",event);
	}
}
static void process_transaction_terminated(void *user_ctx, const belle_sip_transaction_terminated_event_t *event) {
383 384
	belle_sip_client_transaction_t* client_transaction = belle_sip_transaction_terminated_event_get_client_transaction(event);
	belle_sip_server_transaction_t* server_transaction = belle_sip_transaction_terminated_event_get_server_transaction(event);
385
	belle_sip_transaction_t* trans;
Sylvain Berfini's avatar
Sylvain Berfini committed
386 387
	SalOp* op;

388 389 390 391 392
	if(client_transaction)
		trans=BELLE_SIP_TRANSACTION(client_transaction);
	 else
		 trans=BELLE_SIP_TRANSACTION(server_transaction);

Sylvain Berfini's avatar
Sylvain Berfini committed
393
	op = (SalOp*)belle_sip_transaction_get_application_data(trans);
394 395 396 397
	if (op && op->callbacks.process_transaction_terminated) {
		op->callbacks.process_transaction_terminated(op,event);
	} else {
		ms_error("Unhandled transaction terminated [%p]",trans);
jehan's avatar
jehan committed
398
	}
jehan's avatar
jehan committed
399
	if (op && client_transaction) sal_op_unref(op); /*because every client transaction ref op*/
400

jehan's avatar
jehan committed
401
}
Simon Morlat's avatar
Simon Morlat committed
402

jehan's avatar
jehan committed
403
static void process_auth_requested(void *sal, belle_sip_auth_event_t *auth_event) {
jehan's avatar
jehan committed
404 405 406 407 408 409
	SalAuthInfo* auth_info = sal_auth_info_create(auth_event);
	((Sal*)sal)->callbacks.auth_requested(sal,auth_info);
	belle_sip_auth_event_set_passwd(auth_event,(const char*)auth_info->password);
	belle_sip_auth_event_set_ha1(auth_event,(const char*)auth_info->ha1);
	belle_sip_auth_event_set_userid(auth_event,(const char*)auth_info->userid);
	sal_auth_info_delete(auth_info);
jehan's avatar
jehan committed
410 411
	return;
}
Simon Morlat's avatar
Simon Morlat committed
412

jehan's avatar
jehan committed
413
Sal * sal_init(){
Simon Morlat's avatar
Simon Morlat committed
414
	belle_sip_listener_callbacks_t listener_callbacks;
jehan's avatar
jehan committed
415
	Sal * sal=ms_new0(Sal,1);
jehan's avatar
jehan committed
416
	sal->nat_helper_enabled=TRUE;
jehan's avatar
jehan committed
417
	sal->user_agent=belle_sip_header_user_agent_new();
jehan's avatar
jehan committed
418
#if defined(PACKAGE_NAME) && defined(LINPHONE_VERSION)
jehan's avatar
jehan committed
419
	belle_sip_header_user_agent_add_product(sal->user_agent, PACKAGE_NAME "/" LINPHONE_VERSION);
jehan's avatar
jehan committed
420
#endif
421
	sal_append_stack_string_to_user_agent(sal);
jehan's avatar
jehan committed
422
	belle_sip_object_ref(sal->user_agent);
Yann Diorcet's avatar
Yann Diorcet committed
423
	belle_sip_set_log_handler(_belle_sip_log);
jehan's avatar
jehan committed
424 425
	sal->stack = belle_sip_stack_new(NULL);
	sal->prov = belle_sip_stack_create_provider(sal->stack,NULL);
Simon Morlat's avatar
Simon Morlat committed
426 427 428 429 430 431 432 433 434 435
	memset(&listener_callbacks,0,sizeof(listener_callbacks));
	listener_callbacks.process_dialog_terminated=process_dialog_terminated;
	listener_callbacks.process_io_error=process_io_error;
	listener_callbacks.process_request_event=process_request_event;
	listener_callbacks.process_response_event=process_response_event;
	listener_callbacks.process_timeout=process_timeout;
	listener_callbacks.process_transaction_terminated=process_transaction_terminated;
	listener_callbacks.process_auth_requested=process_auth_requested;
	sal->listener=belle_sip_listener_create_from_callbacks(&listener_callbacks,sal);
	belle_sip_provider_add_sip_listener(sal->prov,sal->listener);
Simon Morlat's avatar
Simon Morlat committed
436 437
	sal->tls_verify=TRUE;
	sal->tls_verify_cn=TRUE;
jehan's avatar
jehan committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	return sal;
}
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;
}

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_released==NULL)
		ctx->callbacks.call_released=(SalOnCallReleased)unimplemented_stub;
	if (ctx->callbacks.call_updating==NULL)
		ctx->callbacks.call_updating=(SalOnCallUpdating)unimplemented_stub;
	if (ctx->callbacks.auth_requested_legacy==NULL)
		ctx->callbacks.auth_requested_legacy=(SalOnAuthRequestedLegacy)unimplemented_stub;
jehan's avatar
jehan committed
470 471 472 473
#ifdef USE_BELLESIP
	if (ctx->callbacks.auth_failure==NULL)
		ctx->callbacks.auth_failure=(SalOnAuthFailure)unimplemented_stub;
#else
jehan's avatar
jehan committed
474 475
	if (ctx->callbacks.auth_success==NULL)
		ctx->callbacks.auth_success=(SalOnAuthSuccess)unimplemented_stub;
jehan's avatar
jehan committed
476
#endif
jehan's avatar
jehan committed
477 478 479 480 481 482 483 484 485 486
	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;
	if (ctx->callbacks.notify==NULL)
		ctx->callbacks.notify=(SalOnNotify)unimplemented_stub;
	if (ctx->callbacks.subscribe_received==NULL)
		ctx->callbacks.subscribe_received=(SalOnSubscribeReceived)unimplemented_stub;
487 488
	if (ctx->callbacks.subscribe_closed==NULL)
		ctx->callbacks.subscribe_closed=(SalOnSubscribeClosed)unimplemented_stub;
489 490
	if (ctx->callbacks.parse_presence_requested==NULL)
		ctx->callbacks.parse_presence_requested=(SalOnParsePresenceRequested)unimplemented_stub;
491 492 493 494
	if (ctx->callbacks.notify_presence==NULL)
		ctx->callbacks.notify_presence=(SalOnNotifyPresence)unimplemented_stub;
	if (ctx->callbacks.subscribe_presence_received==NULL)
		ctx->callbacks.subscribe_presence_received=(SalOnSubscribePresenceReceived)unimplemented_stub;
jehan's avatar
jehan committed
495 496 497 498 499 500
	if (ctx->callbacks.text_received==NULL)
		ctx->callbacks.text_received=(SalOnTextReceived)unimplemented_stub;
	if (ctx->callbacks.ping_reply==NULL)
		ctx->callbacks.ping_reply=(SalOnPingReply)unimplemented_stub;
	if (ctx->callbacks.auth_requested==NULL)
		ctx->callbacks.auth_requested=(SalOnAuthRequested)unimplemented_stub;
Simon Morlat's avatar
Simon Morlat committed
501 502
	if (ctx->callbacks.info_received==NULL)
		ctx->callbacks.info_received=(SalOnInfoReceived)unimplemented_stub;
jehan's avatar
jehan committed
503 504 505 506 507
}



void sal_uninit(Sal* sal){
jehan's avatar
jehan committed
508
	belle_sip_object_unref(sal->user_agent);
jehan's avatar
jehan committed
509 510
	belle_sip_object_unref(sal->prov);
	belle_sip_object_unref(sal->stack);
Simon Morlat's avatar
Simon Morlat committed
511
	belle_sip_object_unref(sal->listener);
Simon Morlat's avatar
Simon Morlat committed
512
	if (sal->root_ca) ms_free(sal->root_ca);
jehan's avatar
jehan committed
513 514 515 516
	ms_free(sal);
	return ;
};

jehan's avatar
jehan committed
517
int sal_add_listen_port(Sal *ctx, SalAddress* addr){
jehan's avatar
jehan committed
518
	int result;
jehan's avatar
jehan committed
519 520 521 522
	belle_sip_listening_point_t* lp = belle_sip_stack_create_listening_point(ctx->stack
																			,sal_address_get_domain(addr)
																			,sal_address_get_port_int(addr)
																			,sal_transport_to_string(sal_address_get_transport(addr)));
jehan's avatar
jehan committed
523
	if (lp) {
jehan's avatar
jehan committed
524
		belle_sip_listening_point_set_keep_alive(lp,ctx->keep_alive);
jehan's avatar
jehan committed
525
		result = belle_sip_provider_add_listening_point(ctx->prov,lp);
Simon Morlat's avatar
Simon Morlat committed
526
		set_tls_properties(ctx);
jehan's avatar
jehan committed
527 528 529 530 531
	} else {
		return -1;
	}
	return result;
}
Simon Morlat's avatar
Simon Morlat committed
532

jehan's avatar
jehan committed
533 534 535 536 537 538 539 540 541 542
int sal_listen_port(Sal *ctx, const char *addr, int port, SalTransport tr, int is_secure) {
	SalAddress* sal_addr = sal_address_new(NULL);
	int result;
	sal_address_set_domain(sal_addr,addr);
	sal_address_set_port_int(sal_addr,port);
	sal_address_set_transport(sal_addr,tr);
	result = sal_add_listen_port(ctx,sal_addr);
	sal_address_destroy(sal_addr);
	return result;
}
jehan's avatar
jehan committed
543 544 545 546 547 548 549 550 551 552 553 554 555
static void remove_listening_point(belle_sip_listening_point_t* lp,belle_sip_provider_t* prov) {
	belle_sip_provider_remove_listening_point(prov,lp);
}
int sal_unlisten_ports(Sal *ctx){
	const belle_sip_list_t * lps = belle_sip_provider_get_listening_points(ctx->prov);
	belle_sip_list_t * tmp_list = belle_sip_list_copy(lps);
	belle_sip_list_for_each2 (tmp_list,(void (*)(void*,void*))remove_listening_point,ctx->prov);
	belle_sip_list_free(tmp_list);

	ms_message("sal_unlisten_ports done");
	return 0;
}
ortp_socket_t sal_get_socket(Sal *ctx){
jehan's avatar
jehan committed
556
	ms_warning("sal_get_socket is deprecated");
jehan's avatar
jehan committed
557 558 559
	return -1;
}
void sal_set_user_agent(Sal *ctx, const char *user_agent){
jehan's avatar
jehan committed
560 561
	belle_sip_header_user_agent_set_products(ctx->user_agent,NULL);
	belle_sip_header_user_agent_add_product(ctx->user_agent,user_agent);
jehan's avatar
jehan committed
562 563
	return ;
}
564 565 566 567 568 569 570

void sal_append_stack_string_to_user_agent(Sal *ctx) {
	char stack_string[64];
	snprintf(stack_string, sizeof(stack_string) - 1, "(belle-sip/%s)", belle_sip_version_to_string());
	belle_sip_header_user_agent_add_product(ctx->user_agent, stack_string);
}

jehan's avatar
jehan committed
571 572
/*keepalive period in ms*/
void sal_set_keepalive_period(Sal *ctx,unsigned int value){
jehan's avatar
jehan committed
573
	const belle_sip_list_t* iterator;
jehan's avatar
jehan committed
574
	belle_sip_listening_point_t* lp;
jehan's avatar
jehan committed
575 576
	ctx->keep_alive=value;
	for (iterator=belle_sip_provider_get_listening_points(ctx->prov);iterator!=NULL;iterator=iterator->next) {
jehan's avatar
jehan committed
577 578 579 580
		lp=(belle_sip_listening_point_t*)iterator->data;
		if (ctx->use_tcp_tls_keep_alive || strcasecmp(belle_sip_listening_point_get_transport(lp),"udp")==0) {
			belle_sip_listening_point_set_keep_alive(lp,ctx->keep_alive);
		}
jehan's avatar
jehan committed
581
	}
jehan's avatar
jehan committed
582 583
	return ;
}
Ghislain MARY's avatar
Ghislain MARY committed
584
int sal_enable_tunnel(Sal *ctx, void *tunnelclient) {
585 586
#ifdef TUNNEL_ENABLED
	belle_sip_listening_point_t *lp;
Ghislain MARY's avatar
Ghislain MARY committed
587 588 589
	int result;

	sal_unlisten_ports(ctx);
590
	lp = belle_sip_tunnel_listening_point_new(ctx->stack, tunnelclient);
Ghislain MARY's avatar
Ghislain MARY committed
591 592 593 594 595 596
	if (lp == NULL) return -1;

	belle_sip_listening_point_set_keep_alive(lp, ctx->keep_alive);
	result = belle_sip_provider_add_listening_point(ctx->prov, lp);
	set_tls_properties(ctx);
	return result;
597 598 599
#else
	return 0;
#endif
Ghislain MARY's avatar
Ghislain MARY committed
600 601
}
void sal_disable_tunnel(Sal *ctx) {
602
#ifdef TUNNEL_ENABLED
Ghislain MARY's avatar
Ghislain MARY committed
603
	sal_unlisten_ports(ctx);
604
#endif
Ghislain MARY's avatar
Ghislain MARY committed
605
}
jehan's avatar
jehan committed
606 607 608 609 610
/**
 * returns keepalive period in ms
 * 0 desactiaved
 * */
unsigned int sal_get_keepalive_period(Sal *ctx){
jehan's avatar
jehan committed
611
	return ctx->keep_alive;
jehan's avatar
jehan committed
612 613
}
void sal_use_session_timers(Sal *ctx, int expires){
jehan's avatar
jehan committed
614
	ctx->session_expires=expires;
jehan's avatar
jehan committed
615 616 617
	return ;
}
void sal_use_double_registrations(Sal *ctx, bool_t enabled){
jehan's avatar
jehan committed
618
	ms_warning("sal_use_double_registrations is deprecated");
jehan's avatar
jehan committed
619 620 621
	return ;
}
void sal_reuse_authorization(Sal *ctx, bool_t enabled){
jehan's avatar
jehan committed
622
	ms_warning("sal_reuse_authorization is deprecated");
jehan's avatar
jehan committed
623 624 625
	return ;
}
void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec){
jehan's avatar
jehan committed
626
	ctx->one_matching_codec=one_matching_codec;
jehan's avatar
jehan committed
627 628
}
void sal_use_rport(Sal *ctx, bool_t use_rports){
jehan's avatar
jehan committed
629 630
	belle_sip_provider_enable_rport(ctx->prov,use_rports);
	ms_message("Sal use rport [%s]",use_rports?"enabled":"disabled");
jehan's avatar
jehan committed
631 632 633
	return ;
}
void sal_use_101(Sal *ctx, bool_t use_101){
jehan's avatar
jehan committed
634
	ms_warning("sal_use_101 is deprecated");
jehan's avatar
jehan committed
635 636
	return ;
}
Simon Morlat's avatar
Simon Morlat committed
637 638 639 640 641 642 643 644 645 646

static void set_tls_properties(Sal *ctx){
	belle_sip_listening_point_t *lp=belle_sip_provider_get_listening_point(ctx->prov,"TLS");
	if (lp){
		belle_sip_tls_listening_point_t *tlp=BELLE_SIP_TLS_LISTENING_POINT(lp);
		int verify_exceptions=0;
		
		if (!ctx->tls_verify) verify_exceptions=BELLE_SIP_TLS_LISTENING_POINT_BADCERT_ANY_REASON;
		else if (!ctx->tls_verify_cn) verify_exceptions=BELLE_SIP_TLS_LISTENING_POINT_BADCERT_CN_MISMATCH;
		
jehan's avatar
jehan committed
647
		belle_sip_tls_listening_point_set_root_ca(tlp,ctx->root_ca); /*root_ca might be NULL */
Simon Morlat's avatar
Simon Morlat committed
648 649 650 651
		belle_sip_tls_listening_point_set_verify_exceptions(tlp,verify_exceptions);
	}
}

jehan's avatar
jehan committed
652
void sal_set_root_ca(Sal* ctx, const char* rootCa){
Simon Morlat's avatar
Simon Morlat committed
653 654 655 656 657 658 659
	if (ctx->root_ca){
		ms_free(ctx->root_ca);
		ctx->root_ca=NULL;
	}
	if (rootCa)
		ctx->root_ca=ms_strdup(rootCa);
	set_tls_properties(ctx);
jehan's avatar
jehan committed
660 661
	return ;
}
Simon Morlat's avatar
Simon Morlat committed
662

jehan's avatar
jehan committed
663
void sal_verify_server_certificates(Sal *ctx, bool_t verify){
Simon Morlat's avatar
Simon Morlat committed
664 665
	ctx->tls_verify=verify;
	set_tls_properties(ctx);
jehan's avatar
jehan committed
666 667
	return ;
}
Simon Morlat's avatar
Simon Morlat committed
668

669
void sal_verify_server_cn(Sal *ctx, bool_t verify){
Simon Morlat's avatar
Simon Morlat committed
670 671
	ctx->tls_verify_cn=verify;
	set_tls_properties(ctx);
672 673 674 675
	return ;
}

void sal_use_tcp_tls_keepalive(Sal *ctx, bool_t enabled) {
jehan's avatar
jehan committed
676
	ctx->use_tcp_tls_keep_alive=enabled;
677
}
jehan's avatar
jehan committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691

int sal_iterate(Sal *sal){
	belle_sip_stack_sleep(sal->stack,0);
	return 0;
}
MSList * sal_get_pending_auths(Sal *sal){
	return ms_list_copy(sal->pending_auths);
}

#define payload_type_set_number(pt,n)	(pt)->user_data=(void*)((long)n);
#define payload_type_get_number(pt)		((int)(long)(pt)->user_data)

/*misc*/
void sal_get_default_local_ip(Sal *sal, int address_family, char *ip, size_t iplen){
jehan's avatar
jehan committed
692
	strncpy(ip,address_family==AF_INET6 ? "::1" : "127.0.0.1",iplen);
Simon Morlat's avatar
Simon Morlat committed
693
	ms_error("sal_get_default_local_ip() is deprecated.");
jehan's avatar
jehan committed
694
}
695 696

const char *sal_get_root_ca(Sal* ctx) {
Simon Morlat's avatar
Simon Morlat committed
697
	return ctx->root_ca;
698
}
Simon Morlat's avatar
Simon Morlat committed
699

700
int sal_reset_transports(Sal *ctx){
jehan's avatar
jehan committed
701
	ms_message("Reseting transports");
jehan's avatar
jehan committed
702 703
	belle_sip_provider_clean_channels(ctx->prov);
	return 0;
704
}
Simon Morlat's avatar
Simon Morlat committed
705

706
void sal_set_dscp(Sal *ctx, int dscp){
Simon Morlat's avatar
Simon Morlat committed
707
	belle_sip_stack_set_default_dscp(ctx->stack,dscp);
708
}
Simon Morlat's avatar
Simon Morlat committed
709

710 711 712
void  sal_set_send_error(Sal *sal,int value) {
	 belle_sip_stack_set_send_error(sal->stack,value);
}
jehan's avatar
jehan committed
713 714 715
void  sal_set_recv_error(Sal *sal,int value) {
	 belle_sip_provider_set_recv_error(sal->prov,value);
}
jehan's avatar
jehan committed
716 717 718 719 720 721 722
void sal_nat_helper_enable(Sal *sal,bool_t enable) {
	sal->nat_helper_enabled=enable;
	ms_message("Sal nat helper [%s]",enable?"enabled":"disabled");
}
bool_t sal_nat_helper_enabled(Sal *sal) {
	return sal->nat_helper_enabled;
}
jehan's avatar
jehan committed
723 724 725 726 727 728
void sal_set_dns_timeout(Sal* sal,int timeout) {
	belle_sip_stack_set_dns_timeout(sal->stack, timeout);
}
int sal_get_dns_timeout(const Sal* sal)  {
	return belle_sip_stack_get_dns_timeout(sal->stack);
}
729

730 731 732 733 734 735 736 737
void sal_set_dns_user_hosts_file(Sal *sal, const char *hosts_file) {
	belle_sip_stack_set_dns_user_hosts_file(sal->stack, hosts_file);
}

const char * sal_get_dns_user_hosts_file(const Sal *sal) {
	return belle_sip_stack_get_dns_user_hosts_file(sal->stack);
}

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
SalAuthInfo* sal_auth_info_create(belle_sip_auth_event_t* event) {
	SalAuthInfo* auth_info = sal_auth_info_new();
	auth_info->realm = ms_strdup(belle_sip_auth_event_get_realm(event)) ;
	auth_info->username = ms_strdup(belle_sip_auth_event_get_username(event)) ;
	return auth_info;
}
const char* sal_op_type_to_string(const SalOpType_t type) {
	switch(type) {
	case SalOpRegister: return "SalOpRegister";
	case SalOpCall: return "SalOpCall";
	case SalOpMessage: return "SalOpMessage";
	case SalOpPresence: return "SalOpPresence";
	default:
		return "SalOpUnknown";
	}
}
754 755 756 757 758 759 760 761

void sal_expire_old_registration_contacts(Sal *ctx, bool_t enabled){
	ms_warning("sal_expire_old_registration_contacts not implemented ");
}

void sal_use_dates(Sal *ctx, bool_t enabled){
	ctx->use_dates=enabled;
}
762 763 764 765

int sal_auth_compute_ha1(const char* userid,const char* realm,const char* password, char ha1[33]) {
	return belle_sip_auth_helper_compute_ha1(userid, realm, password, ha1);
}
Simon Morlat's avatar
Simon Morlat committed
766 767 768 769 770 771 772 773 774


SalCustomHeader *sal_custom_header_append(SalCustomHeader *ch, const char *name, const char *value){
	belle_sip_message_t *msg=(belle_sip_message_t*)ch;
	belle_sip_header_t *h;
	char *tmp=ms_strdup_printf("%s: %s\r\n",name,value);
	
	if (msg==NULL){
		msg=(belle_sip_message_t*)belle_sip_request_new();
Simon Morlat's avatar
Simon Morlat committed
775
		belle_sip_object_ref(msg);
Simon Morlat's avatar
Simon Morlat committed
776 777 778 779 780 781 782 783 784 785 786 787
	}
	h=BELLE_SIP_HEADER(belle_sip_header_extension_parse(tmp));
	ms_free(tmp);
	if (h==NULL){
		belle_sip_error("Fail to parse extension header.");
		return (SalCustomHeader*)msg;
	}
	belle_sip_message_add_header(msg,h);
	return (SalCustomHeader*)msg;
}

const char *sal_custom_header_find(const SalCustomHeader *ch, const char *name){
Simon Morlat's avatar
Simon Morlat committed
788 789 790 791 792 793 794 795 796
	if (ch){
		belle_sip_header_t *h=belle_sip_message_get_header((belle_sip_message_t*)ch,name);
		
		if (h){
			if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(h,belle_sip_header_extension_t)){
				return belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(h));
			}else{
				return belle_sip_header_get_unparsed_value(h);
			}
Simon Morlat's avatar
Simon Morlat committed
797 798 799 800 801 802
		}
	}
	return NULL;
}

void sal_custom_header_free(SalCustomHeader *ch){
Simon Morlat's avatar
Simon Morlat committed
803
	if (ch==NULL) return;
Simon Morlat's avatar
Simon Morlat committed
804 805 806 807
	belle_sip_object_unref((belle_sip_message_t*)ch);
}

SalCustomHeader *sal_custom_header_clone(const SalCustomHeader *ch){
Simon Morlat's avatar
Simon Morlat committed
808
	if (ch==NULL) return NULL;
Simon Morlat's avatar
Simon Morlat committed
809 810 811
	return (SalCustomHeader*)belle_sip_object_ref((belle_sip_message_t*)ch);
}

Simon Morlat's avatar
Simon Morlat committed
812
const SalCustomHeader *sal_op_get_recv_custom_header(SalOp *op){
Simon Morlat's avatar
Simon Morlat committed
813
	SalOpBase *b=(SalOpBase *)op;
Simon Morlat's avatar
Simon Morlat committed
814
	return b->recv_custom_headers;
Simon Morlat's avatar
Simon Morlat committed
815 816
}

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
void sal_set_uuid(Sal *sal, const char *uuid){
	if (sal->uuid){
		belle_sip_free(sal->uuid);
		sal->uuid=NULL;
	}
	if (uuid)
		sal->uuid=belle_sip_strdup(uuid);
}

typedef struct {
    unsigned int time_low;
    unsigned short time_mid;
    unsigned short time_hi_and_version;
    unsigned char clock_seq_hi_and_reserved;
    unsigned char clock_seq_low;
    unsigned char node[6];
} sal_uuid_t;
Simon Morlat's avatar
Simon Morlat committed
834

Simon Morlat's avatar
Simon Morlat committed
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
int sal_create_uuid(Sal*ctx, char *uuid, size_t len){
	sal_uuid_t uuid_struct;
	int i;
	int written;
	
	if (len==0) return -1;
	/*create an UUID as described in RFC4122, 4.4 */
	belle_sip_random_bytes((unsigned char*)&uuid_struct, sizeof(sal_uuid_t));
	uuid_struct.clock_seq_hi_and_reserved&=~(1<<6);
	uuid_struct.clock_seq_hi_and_reserved|=1<<7;
	uuid_struct.time_hi_and_version&=~(0xf<<12);
	uuid_struct.time_hi_and_version|=4<<12;
	
	written=snprintf(uuid,len,"%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", uuid_struct.time_low, uuid_struct.time_mid,
			uuid_struct.time_hi_and_version, uuid_struct.clock_seq_hi_and_reserved,
			uuid_struct.clock_seq_low);
	if (written>len+13){
		ms_error("sal_create_uuid(): buffer is too short !");
		return -1;
	}
	for (i = 0; i < 6; i++)
		written+=snprintf(uuid+written,len-written,"%2.2x", uuid_struct.node[i]);
	uuid[len-1]='\0';
	sal_set_uuid(ctx,uuid);
	return 0;
}
Simon Morlat's avatar
Simon Morlat committed
862

863 864 865 866 867
belle_sip_response_t* sal_create_response_from_request ( Sal* sal, belle_sip_request_t* req, int code ) {
	belle_sip_response_t *resp=belle_sip_response_create_from_request(req,code);
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(sal->user_agent));
	return resp;
}