linphonecore.c 240 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
Simon Morlat's avatar
Simon Morlat committed
4
Copyright (C) 2010  Belledonne Communications SARL
aymeric's avatar
aymeric committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

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.
*/

#include "linphonecore.h"
smorlat's avatar
smorlat committed
22
#include "sipsetup.h"
aymeric's avatar
aymeric committed
23 24
#include "lpconfig.h"
#include "private.h"
25
#include "quality_reporting.h"
26
#include "lime.h"
27

28
#include <math.h>
29 30
#include <sys/types.h>
#include <sys/stat.h>
31
#include <ortp/telephonyevents.h>
32
#include <mediastreamer2/zrtp.h>
johan's avatar
johan committed
33
#include <mediastreamer2/dtls_srtp.h>
aymeric's avatar
aymeric committed
34
#include "mediastreamer2/mediastream.h"
35
#include "mediastreamer2/mseventqueue.h"
smorlat's avatar
smorlat committed
36
#include "mediastreamer2/msvolume.h"
37
#include "mediastreamer2/msequalizer.h"
38
#include "mediastreamer2/dtmfgen.h"
aymeric's avatar
aymeric committed
39

40
#ifdef INET6
aymeric's avatar
aymeric committed
41
#ifndef WIN32
42
#include <netdb.h>
aymeric's avatar
aymeric committed
43 44 45
#endif
#endif

46
#ifdef HAVE_CONFIG_H
47
#include "config.h"
48 49 50
#ifndef ANDROID /*on Android LIBLINPHONE version is passed from root Makefile*/
	#include "liblinphone_gitversion.h"
#endif
Ghislain MARY's avatar
Ghislain MARY committed
51 52 53
#else
#ifndef LIBLINPHONE_GIT_VERSION
#define LIBLINPHONE_GIT_VERSION "unknown"
54 55 56
#endif
#endif

57 58 59
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
60

61
#ifdef HAVE_ZLIB
62
#define COMPRESSED_LOG_COLLECTION_EXTENSION "gz"
63 64 65 66 67 68 69 70
#ifdef WIN32
#include <fcntl.h>
#include <io.h>
#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
#define SET_BINARY_MODE(file)
#endif
#include <zlib.h>
71
#else
72
#define COMPRESSED_LOG_COLLECTION_EXTENSION "txt"
73
#endif
74 75 76 77
#define LOG_COLLECTION_DEFAULT_PATH "."
#define LOG_COLLECTION_DEFAULT_PREFIX "linphone"
#define LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE (10 * 1024 * 1024)

78

smorlat's avatar
smorlat committed
79
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
80

81 82
#define ROOT_CA_FILE PACKAGE_DATA_DIR "/linphone/rootca.pem"

83 84 85 86 87 88 89
static const char *liblinphone_version=
#ifdef LIBLINPHONE_GIT_VERSION
	LIBLINPHONE_GIT_VERSION
#else
	LIBLINPHONE_VERSION
#endif
;
90
static OrtpLogFunc liblinphone_log_func = NULL;
91
static LinphoneLogCollectionState liblinphone_log_collection_state = LinphoneLogCollectionDisabled;
92
static char * liblinphone_log_collection_path = NULL;
93 94
static char * liblinphone_log_collection_prefix = NULL;
static int liblinphone_log_collection_max_file_size = LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE;
95
static ortp_mutex_t liblinphone_log_collection_mutex;
96
static bool_t liblinphone_serialize_logs = FALSE;
97
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
Simon Morlat's avatar
Simon Morlat committed
98 99
static void linphone_core_run_hooks(LinphoneCore *lc);
static void linphone_core_free_hooks(LinphoneCore *lc);
aymeric's avatar
aymeric committed
100 101

#include "enum.h"
102
#include "contact_providers_priv.h"
103

104

105
const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
aymeric's avatar
aymeric committed
106 107
static void toggle_video_preview(LinphoneCore *lc, bool_t val);

108
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
109
#define SOUNDS_PREFIX
110 111
#else
#define SOUNDS_PREFIX "Assets/Sounds/"
112
#endif
aymeric's avatar
aymeric committed
113
/* relative path where is stored local ring*/
114
#define LOCAL_RING SOUNDS_PREFIX "rings/oldphone.wav"
aymeric's avatar
aymeric committed
115
/* same for remote ring (ringback)*/
116 117
#define REMOTE_RING SOUNDS_PREFIX "ringback.wav"
#define HOLD_MUSIC SOUNDS_PREFIX "rings/toy-mono.wav"
118

aymeric's avatar
aymeric committed
119

Simon Morlat's avatar
Simon Morlat committed
120
extern SalCallbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
121 122 123 124 125 126 127 128 129 130 131 132

void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
{
  obj->_func=func;
  obj->_user_data=ud;
}

int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
	if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
	return 0;
}

133

134 135 136 137 138 139 140 141 142 143 144 145
/**
 * Returns TRUE if the LinphoneCall asked to autoanswer
 *
**/
bool_t linphone_call_asked_to_autoanswer(LinphoneCall *call){
	//return TRUE if the unique(for the moment) incoming call asked to be autoanswered
	if(call)
		return sal_call_autoanswer_asked(call->op);
	else
		return FALSE;
}

146 147
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
148 149
	if (call)  return linphone_call_get_duration(call);
	return -1;
150 151 152 153
}

const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call(lc);
Simon Morlat's avatar
Simon Morlat committed
154
	if (call==NULL) return NULL;
155
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
156 157
}

jehan's avatar
jehan committed
158 159
static void linphone_core_log_collection_handler(OrtpLogLevel level, const char *fmt, va_list args);

160
void linphone_core_set_log_handler(OrtpLogFunc logfunc) {
161 162 163 164 165
	if (ortp_logv_out == linphone_core_log_collection_handler) {
		ms_message("There is already a log collection handler, keep it");
		liblinphone_log_func = logfunc;
	} else
		ortp_set_log_handler(logfunc);
166 167 168 169 170 171 172 173
}

void linphone_core_set_log_file(FILE *file) {
	if (file == NULL) file = stdout;
	ortp_set_log_file(file);
}

void linphone_core_set_log_level(OrtpLogLevel loglevel) {
174 175 176 177
	linphone_core_set_log_level_mask(loglevel);
}

void linphone_core_set_log_level_mask(OrtpLogLevel loglevel) {
178
	ortp_set_log_level_mask(loglevel);
179 180 181 182 183
	if (loglevel == 0) {
		sal_disable_logs();
	} else {
		sal_enable_logs();
	}
184 185
}

186 187 188 189 190 191 192
static void linphone_core_log_collection_handler(OrtpLogLevel level, const char *fmt, va_list args) {
	const char *lname="undef";
	char *msg;
	char *log_filename1;
	char *log_filename2;
	FILE *log_file;
	struct timeval tp;
193
	struct tm *lt;
194 195 196 197
	time_t tt;
	struct stat statbuf;

	if (liblinphone_log_func != NULL) {
198
#ifndef WIN32
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
199 200 201 202
		va_list args_copy;
		va_copy(args_copy, args);
		liblinphone_log_func(level, fmt, args_copy);
		va_end(args_copy);
203 204 205 206 207
#else
		/* This works on 32 bits, luckily. */
		/* TODO: va_copy is available in Visual Studio 2013. */
		liblinphone_log_func(level, fmt, args);
#endif
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	}

	ortp_gettimeofday(&tp, NULL);
	tt = (time_t)tp.tv_sec;
	lt = localtime((const time_t*)&tt);
	switch(level){
		case ORTP_DEBUG:
			lname = "DEBUG";
			break;
		case ORTP_MESSAGE:
			lname = "MESSAGE";
			break;
		case ORTP_WARNING:
			lname = "WARNING";
			break;
		case ORTP_ERROR:
			lname = "ERROR";
			break;
		case ORTP_FATAL:
			lname = "FATAL";
			break;
		default:
			ortp_fatal("Bad level !");
	}
	msg = ortp_strdup_vprintf(fmt, args);

234 235 236 237 238 239
	log_filename1 = ortp_strdup_printf("%s/%s1.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
	log_filename2 = ortp_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
240
	ortp_mutex_lock(&liblinphone_log_collection_mutex);
241 242
	log_file = fopen(log_filename1, "a");
	fstat(fileno(log_file), &statbuf);
243
	if (statbuf.st_size > liblinphone_log_collection_max_file_size) {
244 245 246
		fclose(log_file);
		log_file = fopen(log_filename2, "a");
		fstat(fileno(log_file), &statbuf);
247
		if (statbuf.st_size > liblinphone_log_collection_max_file_size) {
248 249 250 251 252 253 254 255 256 257
			fclose(log_file);
			unlink(log_filename1);
			rename(log_filename2, log_filename1);
			log_file = fopen(log_filename2, "a");
		}
	}
	fprintf(log_file,"%i-%.2i-%.2i %.2i:%.2i:%.2i:%.3i %s %s\n",
		1900 + lt->tm_year, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int)(tp.tv_usec / 1000), lname, msg);
	fflush(log_file);
	fclose(log_file);
258
	ortp_mutex_unlock(&liblinphone_log_collection_mutex);
259 260 261 262 263 264

	ortp_free(log_filename1);
	ortp_free(log_filename2);
	ortp_free(msg);
}

265 266 267 268 269 270 271
const char * linphone_core_get_log_collection_path(void) {
	if (liblinphone_log_collection_path != NULL) {
		return liblinphone_log_collection_path;
	}
	return LOG_COLLECTION_DEFAULT_PATH;
}

272
void linphone_core_set_log_collection_path(const char *path) {
273 274 275 276 277 278 279
	if (liblinphone_log_collection_path != NULL) {
		ms_free(liblinphone_log_collection_path);
		liblinphone_log_collection_path = NULL;
	}
	if (path != NULL) {
		liblinphone_log_collection_path = ms_strdup(path);
	}
280 281
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
const char * linphone_core_get_log_collection_prefix(void) {
	if (liblinphone_log_collection_prefix != NULL) {
		return liblinphone_log_collection_prefix;
	}
	return LOG_COLLECTION_DEFAULT_PREFIX;
}

void linphone_core_set_log_collection_prefix(const char *prefix) {
	if (liblinphone_log_collection_prefix != NULL) {
		ms_free(liblinphone_log_collection_prefix);
		liblinphone_log_collection_prefix = NULL;
	}
	if (prefix != NULL) {
		liblinphone_log_collection_prefix = ms_strdup(prefix);
	}
}

int linphone_core_get_log_collection_max_file_size(void) {
	return liblinphone_log_collection_max_file_size;
}

void linphone_core_set_log_collection_max_file_size(int size) {
	liblinphone_log_collection_max_file_size = size;
}

307 308 309 310 311 312 313 314
const char *linphone_core_get_log_collection_upload_server_url(LinphoneCore *core) {
	return lp_config_get_string(core->config, "misc", "log_collection_upload_server_url", NULL);
}

void linphone_core_set_log_collection_upload_server_url(LinphoneCore *core, const char *server_url) {
	lp_config_set_string(core->config, "misc", "log_collection_upload_server_url", server_url);
}

315 316 317 318
LinphoneLogCollectionState linphone_core_log_collection_enabled(void) {
	return liblinphone_log_collection_state;
}

319
void linphone_core_enable_log_collection(LinphoneLogCollectionState state) {
320 321
	if (liblinphone_log_collection_state == state) return;

322 323 324 325 326
	/* at first call of this function, set liblinphone_log_func to the current
	 * ortp log function */
	if( liblinphone_log_func == NULL ){
		liblinphone_log_func = ortp_logv_out;
	}
327 328
	liblinphone_log_collection_state = state;
	if (state != LinphoneLogCollectionDisabled) {
329
		ortp_mutex_init(&liblinphone_log_collection_mutex, NULL);
330 331 332 333 334
		if (state == LinphoneLogCollectionEnabledWithoutPreviousLogHandler) {
			liblinphone_log_func = NULL;
		} else {
			liblinphone_log_func = ortp_logv_out;
		}
335 336 337 338 339 340
		ortp_set_log_handler(linphone_core_log_collection_handler);
	} else {
		ortp_set_log_handler(liblinphone_log_func);
	}
}

jehan's avatar
jehan committed
341
static void clean_log_collection_upload_context(LinphoneCore *lc) {
342 343 344 345
	char *filename = ms_strdup_printf("%s/%s_log.%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
346
	unlink(filename);
347
	ms_free(filename);
jehan's avatar
jehan committed
348 349 350 351
	if (lc && lc->log_collection_upload_information) {
		ms_free(lc->log_collection_upload_information);
		lc->log_collection_upload_information=NULL;
	}
352 353
}

354 355 356 357
static void process_io_error_upload_log_collection(void *data, const belle_sip_io_error_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("I/O Error during log collection upload to %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "I/O Error");
jehan's avatar
jehan committed
358
	clean_log_collection_upload_context(core);
359 360 361 362 363 364
}

static void process_auth_requested_upload_log_collection(void *data, belle_sip_auth_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("Error during log collection upload: auth requested to connect %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "Auth requested");
jehan's avatar
jehan committed
365
	clean_log_collection_upload_context(core);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
}

/**
 * Callback called when posting a log collection file to server (following rcs5.1 recommendation)
 *
 * @param[in] bh The body handler
 * @param[in] msg The belle sip message
 * @param[in] data The user data associated with the handler, contains the LinphoneCore object
 * @param[in] offset The current position in the input buffer
 * @param[in] buffer The ouput buffer where to copy the data to be uploaded
 * @param[in,out] size The size in byte of the data requested, as output it will contain the effective copied size
 *
 */
static int log_collection_upload_on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, uint8_t *buffer, size_t *size) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* If we've not reach the end of file yet, fill the buffer with more data */
383
	if (offset < linphone_content_get_size(core->log_collection_upload_information)) {
384 385 386 387
		char *log_filename = ms_strdup_printf("%s/%s_log.%s",
			liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
			COMPRESSED_LOG_COLLECTION_EXTENSION);
388 389 390
#ifdef HAVE_ZLIB
		FILE *log_file = fopen(log_filename, "rb");
#else
391
		FILE *log_file = fopen(log_filename, "r");
392
#endif
393 394 395
		fseek(log_file, offset, SEEK_SET);
		*size = fread(buffer, 1, *size, log_file);
		fclose(log_file);
396
		ms_free(log_filename);
397 398 399 400 401 402 403 404 405 406 407
	}

	return BELLE_SIP_CONTINUE;
}

/**
 * Callback called during upload of a log collection to server.
 * It is just forwarding the call and some parameters to the vtable defined callback.
 */
static void log_collection_upload_on_progress(belle_sip_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, size_t total) {
	LinphoneCore *core = (LinphoneCore *)data;
408
	linphone_core_notify_log_collection_upload_progress_indication(core, offset, total);
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
}

/**
 * Callback function called when we have a response from server during the upload of the log collection to the server (rcs5.1 recommandation)
 * Note: The first post is empty and the server shall reply a 204 (No content) message, this will trigger a new post request to the server
 * to upload the file. The server response to this second post is processed by this same function
 *
 * @param[in] data The user-defined pointer associated with the request, it contains the LinphoneCore object
 * @param[in] event The response from server
 */
static void process_response_from_post_file_log_collection(void *data, const belle_http_response_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* Check the answer code */
	if (event->response) {
		int code = belle_http_response_get_status_code(event->response);
		if (code == 204) { /* This is the reply to the first post to the server - an empty file */
			/* Start uploading the file */
			belle_http_request_listener_callbacks_t cbs = { 0 };
			belle_http_request_listener_t *l;
			belle_generic_uri_t *uri;
			belle_http_request_t *req;
			belle_sip_multipart_body_handler_t *bh;
			char* ua;
			char *first_part_header;
			belle_sip_user_body_handler_t *first_part_bh;

			linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateInProgress, NULL);

			/* Temporary storage for the Content-disposition header value */
439
			first_part_header = belle_sip_strdup_printf("form-data; name=\"File\"; filename=\"%s\"", linphone_content_get_name(core->log_collection_upload_information));
440 441

			/* Create a user body handler to take care of the file and add the content disposition and content-type headers */
442
			first_part_bh = belle_sip_user_body_handler_new(linphone_content_get_size(core->log_collection_upload_information), NULL, NULL, log_collection_upload_on_send_body, core);
443 444
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh, belle_sip_header_create("Content-disposition", first_part_header));
			belle_sip_free(first_part_header);
445 446
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh,
				(belle_sip_header_t *)belle_sip_header_content_type_create(linphone_content_get_type(core->log_collection_upload_information), linphone_content_get_subtype(core->log_collection_upload_information)));
447 448 449 450 451

			/* Insert it in a multipart body handler which will manage the boundaries of multipart message */
			bh = belle_sip_multipart_body_handler_new(log_collection_upload_on_progress, core, (belle_sip_body_handler_t *)first_part_bh);
			ua = ms_strdup_printf("%s/%s", linphone_core_get_user_agent_name(), linphone_core_get_user_agent_version());
			uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
452
			req = belle_http_request_create("POST", uri, belle_sip_header_create("User-Agent", ua), NULL);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
			ms_free(ua);
			belle_sip_message_set_body_handler(BELLE_SIP_MESSAGE(req), BELLE_SIP_BODY_HANDLER(bh));
			cbs.process_response = process_response_from_post_file_log_collection;
			cbs.process_io_error = process_io_error_upload_log_collection;
			cbs.process_auth_requested = process_auth_requested_upload_log_collection;
			l = belle_http_request_listener_create_from_callbacks(&cbs, core);
			belle_http_provider_send_request(core->http_provider, req, l);
		}
		if (code == 200) { /* The file has been uploaded correctly, get the server reply */
			xmlDocPtr xmlMessageBody;
			xmlNodePtr cur;
			xmlChar *file_url = NULL;
			const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
			xmlMessageBody = xmlParseDoc((const xmlChar *)body);
			cur = xmlDocGetRootElement(xmlMessageBody);
			if (cur != NULL) {
				cur = cur->xmlChildrenNode;
				while (cur != NULL) {
					if (!xmlStrcmp(cur->name, (const xmlChar *)"file-info")) { /* we found a file info node, check it has a type="file" attribute */
						xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type");
						if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) { /* this is the node we are looking for */
							cur = cur->xmlChildrenNode; /* now loop on the content of the file-info node */
							while (cur != NULL) {
								if (!xmlStrcmp(cur->name, (const xmlChar *)"data")) {
									file_url = 	xmlGetProp(cur, (const xmlChar *)"url");
								}
								cur=cur->next;
							}
							xmlFree(typeAttribute);
							break;
						}
						xmlFree(typeAttribute);
					}
					cur = cur->next;
				}
			}
			if (file_url != NULL) {
				linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateDelivered, (const char *)file_url);
			}
jehan's avatar
jehan committed
492
			clean_log_collection_upload_context(core);
493 494 495 496
		}
	}
}

497
#ifdef HAVE_ZLIB
498 499 500
#define COMPRESS_FILE_PTR gzFile
#define COMPRESS_OPEN gzopen
#define COMPRESS_CLOSE gzclose
501
#else
502 503 504 505
#define COMPRESS_FILE_PTR FILE*
#define COMPRESS_OPEN fopen
#define COMPRESS_CLOSE fclose
#endif
506 507 508 509

/**
 * If zlib is not available the two log files are simply concatenated.
 */
510
static int compress_file(FILE *input_file, COMPRESS_FILE_PTR output_file) {
511
	char buffer[131072]; /* 128kB */
512
	int bytes;
513

514
	while ((bytes = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
515 516 517 518 519 520 521
		if (bytes < 0) return bytes;
#ifdef HAVE_ZLIB
		bytes = gzwrite(output_file, buffer, bytes);
#else
		bytes = fwrite(buffer, 1, bytes, output_file);
#endif
		if (bytes < 0) return bytes;
522
	}
523
	return 0;
524 525
}

526
static int prepare_log_collection_file_to_upload(const char *filename) {
527 528 529
	char *input_filename = NULL;
	char *output_filename = NULL;
	FILE *input_file = NULL;
530 531
	COMPRESS_FILE_PTR output_file = NULL;
	int ret = 0;
532

533
	ortp_mutex_lock(&liblinphone_log_collection_mutex);
534 535
	output_filename = ms_strdup_printf("%s/%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename);
536
	output_file = COMPRESS_OPEN(output_filename, "w");
537
	if (output_file == NULL) goto error;
538 539 540
	input_filename = ms_strdup_printf("%s/%s1.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
541 542 543 544 545
	input_file = fopen(input_filename, "r");
	if (input_file == NULL) goto error;
	ret = compress_file(input_file, output_file);
	if (ret < 0) goto error;
	fclose(input_file);
546
	ms_free(input_filename);
547 548 549
	input_filename = ms_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
550 551 552 553 554
	input_file = fopen(input_filename, "r");
	if (input_file != NULL) {
		ret = compress_file(input_file, output_file);
		if (ret < 0) goto error;
	}
555

556 557
error:
	if (input_file != NULL) fclose(input_file);
558
	if (output_file != NULL) COMPRESS_CLOSE(output_file);
559 560
	if (input_filename != NULL) ms_free(input_filename);
	if (output_filename != NULL) ms_free(output_filename);
561
	ortp_mutex_unlock(&liblinphone_log_collection_mutex);
562 563 564 565 566
	return ret;
}

static size_t get_size_of_file_to_upload(const char *filename) {
	struct stat statbuf;
567 568
	char *output_filename = ms_strdup_printf("%s/%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename);
569 570 571
	FILE *output_file = fopen(output_filename, "rb");
	fstat(fileno(output_file), &statbuf);
	fclose(output_file);
572
	ms_free(output_filename);
573
	return statbuf.st_size;
574 575
}

576
void linphone_core_upload_log_collection(LinphoneCore *core) {
577
	if ((core->log_collection_upload_information == NULL) && (linphone_core_get_log_collection_upload_server_url(core) != NULL) && (liblinphone_log_collection_state != LinphoneLogCollectionDisabled)) {
578 579 580 581 582
		/* open a transaction with the server and send an empty request(RCS5.1 section 3.5.4.8.3.1) */
		belle_http_request_listener_callbacks_t cbs = { 0 };
		belle_http_request_listener_t *l;
		belle_generic_uri_t *uri;
		belle_http_request_t *req;
583
		char *name;
584 585 586

		core->log_collection_upload_information = (LinphoneContent *)malloc(sizeof(LinphoneContent));
		memset(core->log_collection_upload_information, 0, sizeof(LinphoneContent));
587
#ifdef HAVE_ZLIB
588 589
		linphone_content_set_type(core->log_collection_upload_information, "application");
		linphone_content_set_subtype(core->log_collection_upload_information, "gzip");
590
#else
591 592
		linphone_content_set_type(core->log_collection_upload_information, "text");
		linphone_content_set_subtype(core->log_collection_upload_information,"plain");
593
#endif
594
		name = ms_strdup_printf("%s_log.%s",
595 596
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
			COMPRESSED_LOG_COLLECTION_EXTENSION);
597 598 599
		linphone_content_set_name(core->log_collection_upload_information, name);
		if (prepare_log_collection_file_to_upload(name) < 0) return;
		linphone_content_set_size(core->log_collection_upload_information, get_size_of_file_to_upload(name));
600 601 602 603 604 605 606
		uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
		req = belle_http_request_create("POST", uri, NULL, NULL, NULL);
		cbs.process_response = process_response_from_post_file_log_collection;
		cbs.process_io_error = process_io_error_upload_log_collection;
		cbs.process_auth_requested = process_auth_requested_upload_log_collection;
		l = belle_http_request_listener_create_from_callbacks(&cbs, core);
		belle_http_provider_send_request(core->http_provider, req, l);
607
		ms_free(name);
608 609 610
	}
}

611
char * linphone_core_compress_log_collection() {
612 613 614 615 616 617 618 619 620 621 622 623 624 625
	char *filename = NULL;
	if (liblinphone_log_collection_state == LinphoneLogCollectionDisabled) return NULL;
	filename = ms_strdup_printf("%s_log.%s",
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
	if (prepare_log_collection_file_to_upload(filename) < 0) {
		ms_free(filename);
		return NULL;
	}
	ms_free(filename);
	return ms_strdup_printf("%s/%s_log.%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
626 627
}

628
void linphone_core_reset_log_collection() {
629 630
	char *filename;
	ortp_mutex_lock(&liblinphone_log_collection_mutex);
jehan's avatar
jehan committed
631
	clean_log_collection_upload_context(NULL);
632 633 634
	filename = ms_strdup_printf("%s/%s1.log",
			liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
635 636
	unlink(filename);
	ms_free(filename);
637 638 639
	filename = ms_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
640 641 642 643 644
	unlink(filename);
	ms_free(filename);
	ortp_mutex_unlock(&liblinphone_log_collection_mutex);
}

645 646 647 648
/**
 * Enable logs in supplied FILE*.
 *
 * @ingroup misc
649
 * @deprecated Use #linphone_core_set_log_file and #linphone_core_set_log_level instead.
650 651
 *
 * @param file a C FILE* where to fprintf logs. If null stdout is used.
652
 *
653
**/
aymeric's avatar
aymeric committed
654 655 656 657
void linphone_core_enable_logs(FILE *file){
	if (file==NULL) file=stdout;
	ortp_set_log_file(file);
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
jehan's avatar
jehan committed
658
	sal_enable_logs();
aymeric's avatar
aymeric committed
659 660
}

661 662 663 664
/**
 * Enable logs through the user's supplied log callback.
 *
 * @ingroup misc
665
 * @deprecated Use #linphone_core_set_log_handler and #linphone_core_set_log_level instead.
666 667 668
 *
 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
 *            	  typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
669
 *
670
**/
aymeric's avatar
aymeric committed
671 672
void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
673
	linphone_core_set_log_handler(logfunc);
jehan's avatar
jehan committed
674
	sal_enable_logs();
aymeric's avatar
aymeric committed
675 676
}

677 678 679 680
/**
 * Entirely disable logging.
 *
 * @ingroup misc
681
 * @deprecated Use #linphone_core_set_log_level instead.
682
**/
683
void linphone_core_disable_logs(void){
aymeric's avatar
aymeric committed
684
	ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
jehan's avatar
jehan committed
685
	sal_disable_logs();
aymeric's avatar
aymeric committed
686 687
}

688 689 690 691
void linphone_core_serialize_logs(void) {
	liblinphone_serialize_logs = TRUE;
}

aymeric's avatar
aymeric committed
692

693
static void net_config_read (LinphoneCore *lc)
aymeric's avatar
aymeric committed
694 695 696 697 698
{
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;

699
	lc->net_conf.nat_address_ip = NULL;
aymeric's avatar
aymeric committed
700 701 702 703 704 705 706 707 708 709
	tmp=lp_config_get_int(config,"net","download_bw",0);
	linphone_core_set_download_bandwidth(lc,tmp);
	tmp=lp_config_get_int(config,"net","upload_bw",0);
	linphone_core_set_upload_bandwidth(lc,tmp);
	linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
	tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
	if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
	linphone_core_set_nat_address(lc,tmpstr);
	tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
	lc->net_conf.nat_sdp_only=tmp;
710
	tmp=lp_config_get_int(lc->config,"net","mtu",1300);
aymeric's avatar
aymeric committed
711
	linphone_core_set_mtu(lc,tmp);
712 713 714 715 716
	tmp=lp_config_get_int(lc->config,"net","download_ptime",-1);
	if (tmp !=-1 && linphone_core_get_download_ptime(lc) !=0) {
		/*legacy parameter*/
		linphone_core_set_download_ptime(lc,tmp);
	}
717 718
	tmp = lp_config_get_int(lc->config, "net", "dns_srv_enabled", 1);
	linphone_core_enable_dns_srv(lc, tmp);
719 720 721

	/* This is to filter out unsupported firewall policies */
	linphone_core_set_firewall_policy(lc, linphone_core_get_firewall_policy(lc));
aymeric's avatar
aymeric committed
722 723
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
static void build_sound_devices_table(LinphoneCore *lc){
	const char **devices;
	const char **old;
	int ndev;
	int i;
	const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
	ndev=ms_list_size(elem);
	devices=ms_malloc((ndev+1)*sizeof(const char *));
	for (i=0;elem!=NULL;elem=elem->next,i++){
		devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
	}
	devices[ndev]=NULL;
	old=lc->sound_conf.cards;
	lc->sound_conf.cards=devices;
	if (old!=NULL) ms_free(old);
}
aymeric's avatar
aymeric committed
740

741
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
742
{
743
	int tmp;
aymeric's avatar
aymeric committed
744 745
	const char *tmpbuf;
	const char *devid;
746
#ifdef __linux
aymeric's avatar
aymeric committed
747 748 749
	/*alsadev let the user use custom alsa device within linphone*/
	devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
	if (devid){
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
		MSSndCard* card;
		const char* delim=",";
		size_t l=strlen(devid);
		char* d=malloc(l+1);
		char* i;
		memcpy(d,devid,l+1);
		for (l=0,i=strpbrk(d+l,delim);i;i=strpbrk(d+l,delim)){
			char s=*i;
			*i='\0';
			card=ms_alsa_card_new_custom(d+l,d+l);
			ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
			*i=s;
			l=i-d+1;
		}
		if(d[l]!='\0') {
			card=ms_alsa_card_new_custom(d+l,d+l);
			ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
		}
		free(d);
aymeric's avatar
aymeric committed
769
	}
770
	tmp=lp_config_get_int(lc->config,"sound","alsa_forced_rate",-1);
771
	if (tmp>0) ms_alsa_card_set_forced_sample_rate(tmp);
aymeric's avatar
aymeric committed
772 773
#endif
	/* retrieve all sound devices */
774 775
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
776 777
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
778

aymeric's avatar
aymeric committed
779 780
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
781

aymeric's avatar
aymeric committed
782 783
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
784

aymeric's avatar
aymeric committed
785 786 787 788 789 790 791 792 793 794
/*
	tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
	linphone_core_set_play_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
	linphone_core_set_ring_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
	linphone_core_set_rec_level(lc,tmp);
	tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
	linphone_core_set_sound_source(lc,tmpbuf[0]);
*/
795

aymeric's avatar
aymeric committed
796 797
	tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
798
	if (ortp_file_exist(tmpbuf)==-1) {
799
		ms_warning("%s does not exist",tmpbuf);
aymeric's avatar
aymeric committed
800 801 802 803 804 805 806
		tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
		tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	}
	linphone_core_set_ring(lc,tmpbuf);
807

808
	tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
809
	tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
810
	if (ortp_file_exist(tmpbuf)==-1){
811
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
812 813 814
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
815
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
816
	}
817
	linphone_core_set_ringback(lc,tmpbuf);
818 819

	linphone_core_set_play_file(lc,lp_config_get_string(lc->config,"sound","hold_music",PACKAGE_SOUND_DIR "/" HOLD_MUSIC));
aymeric's avatar
aymeric committed
820
	lc->sound_conf.latency=0;
821
#ifndef __ios
Simon Morlat's avatar
Simon Morlat committed
822
	tmp=TRUE;
823
#else
Simon Morlat's avatar
Simon Morlat committed
824
	tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
825
#endif
Simon Morlat's avatar
Simon Morlat committed
826
	tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
827
	linphone_core_enable_echo_cancellation(lc,tmp);
smorlat's avatar
smorlat committed
828 829
	linphone_core_enable_echo_limiter(lc,
		lp_config_get_int(lc->config,"sound","echolimiter",0));
830 831
	linphone_core_enable_agc(lc,
		lp_config_get_int(lc->config,"sound","agc",0));
832

833 834
	linphone_core_set_playback_gain_db (lc,lp_config_get_float(lc->config,"sound","playback_gain_db",0));
	linphone_core_set_mic_gain_db (lc,lp_config_get_float(lc->config,"sound","mic_gain_db",0));
835 836

	linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
837 838 839

	/*just parse requested stream feature once at start to print out eventual errors*/
	linphone_core_get_audio_features(lc);
840

841
	_linphone_core_set_tone(lc,LinphoneReasonBusy,LinphoneToneBusy,NULL);
aymeric's avatar
aymeric committed
842 843
}

844 845
static void certificates_config_read(LinphoneCore *lc)
{
846
	const char *rootca;
847
#ifdef __linux
848
	rootca=lp_config_get_string(lc->config,"sip","root_ca", "/etc/ssl/certs");
849
#else
850
	rootca=lp_config_get_string(lc->config,"sip","root_ca", ROOT_CA_FILE);
851
#endif
852
	linphone_core_set_root_ca(lc,rootca);
853
	linphone_core_verify_server_certificates(lc,lp_config_get_int(lc->config,"sip","verify_server_certs",TRUE));
854
	linphone_core_verify_server_cn(lc,lp_config_get_int(lc->config,"sip","verify_server_cn",TRUE));
855 856
}

857
static void sip_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
858 859 860
{
	char *contact;
	const char *tmpstr;
861
	LCSipTransports tr;
aymeric's avatar
aymeric committed
862 863
	int i,tmp;
	int ipv6;
864 865 866 867

	if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
		sal_use_session_timers(lc->sal,200);
	}
aymeric's avatar
aymeric committed
868

869
	sal_use_no_initial_route(lc->sal,lp_config_get_int(lc->config,"sip","use_no_initial_route",0));
870 871
	sal_use_rport(lc->sal,lp_config_get_int(lc->config,"sip","use_rport",1));

aymeric's avatar
aymeric committed
872 873 874 875 876
	ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
	if (ipv6==-1){
		ipv6=0;
	}
	linphone_core_enable_ipv6(lc,ipv6);
877
	memset(&tr,0,sizeof(tr));
878

879 880 881 882
	tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",5060);
	tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",5060);
	/*we are not listening inbound connection for tls, port has no meaning*/
	tr.tls_port=lp_config_get_int(lc->config,"sip","sip_tls_port",LC_SIP_TRANSPORT_RANDOM);
883

884
	certificates_config_read(lc);
885 886
	/*setting the dscp must be done before starting the transports, otherwise it is not taken into effect*/
	sal_set_dscp(lc->sal,linphone_core_get_sip_dscp(lc));
887
	/*start listening on ports*/
888
	linphone_core_set_sip_transports(lc,&tr);
889

aymeric's avatar
aymeric committed
890 891
	tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
	if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
smorlat's avatar
smorlat committed
892 893
		const char *hostname=NULL;
		const char *username=NULL;
894
#ifdef HAVE_GETENV
smorlat's avatar
smorlat committed
895 896
		hostname=getenv("HOST");
		username=getenv("USER");
aymeric's avatar
aymeric committed
897
		if (hostname==NULL) hostname=getenv("HOSTNAME");
898
#endif /*HAVE_GETENV*/
aymeric's avatar
aymeric committed
899 900 901 902 903 904 905 906 907 908 909 910
		if (hostname==NULL)
			hostname="unknown-host";
		if (username==NULL){
			username="toto";
		}
		contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
		linphone_core_set_primary_contact(lc,contact);
		ms_free(contact);
	}

	tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
	linphone_core_set_guess_hostname(lc,tmp);
911

Simon Morlat's avatar
Simon Morlat committed
912 913
	tmp=lp_config_get_int(lc->config,"sip","lime",FALSE);
	linphone_core_enable_lime(lc,tmp);
914

915
	tmp=lp_config_get_int(lc->config,"sip","inc_timeout",30);
aymeric's avatar
aymeric committed
916 917
	linphone_core_set_inc_timeout(lc,tmp);

Yann Diorcet's avatar
Yann Diorcet committed
918 919
	tmp=lp_config_get_int(lc->config,"sip","in_call_timeout",0);
	linphone_core_set_in_call_timeout(lc,tmp);
920

921 922
	tmp=lp_config_get_int(lc->config,"sip","delayed_timeout",4);
	linphone_core_set_delayed_timeout(lc,tmp);
Yann Diorcet's avatar
Yann Diorcet committed
923

aymeric's avatar
aymeric committed
924 925
	/* get proxies config */
	for(i=0;; i++){
jehan's avatar
jehan committed
926
		LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc,i);
aymeric's avatar
aymeric committed
927 928
		if (cfg!=NULL){
			linphone_core_add_proxy_config(lc,cfg);
929
			linphone_proxy_config_unref(cfg);
aymeric's avatar
aymeric committed
930 931 932 933 934 935 936
		}else{
			break;
		}
	}
	/* get the default proxy */
	tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
	linphone_core_set_default_proxy_index(lc,tmp);
937

aymeric's avatar
aymeric committed
938 939 940 941 942
	/* read authentication information */
	for(i=0;; i++){
		LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
		if (ai!=NULL){
			linphone_core_add_auth_info(lc,ai);
Simon Morlat's avatar
Simon Morlat committed
943
			linphone_auth_info_destroy(ai);
aymeric's avatar
aymeric committed
944 945 946 947
		}else{
			break;
		}
	}
948 949
	/*this is to filter out unsupported encryption schemes*/
	linphone_core_set_media_encryption(lc,linphone_core_get_media_encryption(lc));
950

951
	/*for tuning or test*/
aymeric's avatar
aymeric committed
952
	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
953
	lc->sip_conf.register_only_when_network_is_up=
954
		lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
955 956
	lc->sip_conf.register_only_when_upnp_is_ok=
		lp_config_get_int(lc->config,"sip","register_only_when_upnp_is_ok",1);
957
	lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",0);
958
	lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
959
	lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
960 961
	lc->sip_conf.tcp_tls_keepalive=lp_config_get_int(lc->config,"sip","tcp_tls_keepalive",0);
	linphone_core_enable_keep_alive(lc, (lc->sip_conf.keepalive_period > 0));
962
	sal_use_one_matching_codec_policy(lc->sal,lp_config_get_int(lc->config,"sip","only_one_codec",0));
963
	sal_use_dates(lc->sal,lp_config_get_int(lc->config,"sip","put_date",0));
964
	sal_enable_sip_update_method(lc->sal,lp_config_get_int(lc->config,"sip","sip_update",1));
965
	lc->sip_conf.vfu_with_info=lp_config_get_int(lc->config,"sip","vfu_with_info",1);
966
	linphone_core_set_sip_transport_timeout(lc, lp_config_get_int(lc->config, "sip", "transport_timeout", 63000));
967
	sal_set_supported_tags(lc->sal,lp_config_get_string(lc->config,"sip","supported","replaces, outbound"));
968
	lc->sip_conf.save_auth_info = lp_config_get_int(lc->config, "sip", "save_auth_info", 1);
aymeric's avatar
aymeric committed
969 970
}

971
static void rtp_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
972
{
973
	int min_port, max_port;
aymeric's avatar
aymeric committed
974 975
	int jitt_comp;
	int nortp_timeout;
Simon Morlat's avatar
Simon Morlat committed
976
	bool_t rtp_no_xmit_on_audio_mute;
977
	bool_t adaptive_jitt_comp_enabled;
jehan's avatar
jehan committed
978 979
	const char* tmp;
	int tmp_int;
Simon Morlat's avatar
Simon Morlat committed
980

981 982 983 984 985 986 987 988
	if (lp_config_get_range(lc->config, "rtp", "audio_rtp_port", &min_port, &max_port, 7078, 7078) == TRUE) {
		if (min_port <= 0) min_port = 1;
		if (max_port > 65535) max_port = 65535;
		linphone_core_set_audio_port_range(lc, min_port, max_port);
	} else {
		min_port = lp_config_get_int(lc->config, "rtp", "audio_rtp_port", 7078);
		linphone_core_set_audio_port(lc, min_port);
	}
989

990 991 992 993 994 995 996 997
	if (lp_config_get_range(lc->config, "rtp", "video_rtp_port", &min_port, &max_port, 9078, 9078) == TRUE) {
		if (min_port <= 0) min_port = 1;
		if (max_port > 65535) max_port = 65535;
		linphone_core_set_video_port_range(lc, min_port, max_port);
	} else {
		min_port = lp_config_get_int(lc->config, "rtp", "video_rtp_port", 9078);
		linphone_core_set_video_port(lc, min_port);
	}
998

aymeric's avatar
aymeric committed
999
	jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
1000
	linphone_core_set_audio_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
1001
	jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
1002
	if (jitt_comp==0) jitt_comp=60;
1003
	linphone_core_set_video_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
1004
	nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
1005
	linphone_core_set_nortp_timeout(lc,nortp_timeout);
Simon Morlat's avatar
Simon Morlat committed
1006
	rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
1007
	linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);
1008 1009 1010 1011
	adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "audio_adaptive_jitt_comp_enabled", TRUE);
	linphone_core_enable_audio_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
	adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "video_adaptive_jitt_comp_enabled", TRUE);
	linphone_core_enable_video_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
1012
	lc->rtp_conf.disable_upnp = lp_config_get_int(lc->config, "rtp", "disable_upnp", FALSE);
1013
	linphone_core_set_avpf_mode(lc,lp_config_get_int(lc->config,"rtp","avpf",0));
jehan's avatar
jehan committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	if ((tmp=lp_config_get_string(lc->config,"rtp","audio_multicast_addr",NULL)))
		linphone_core_set_audio_multicast_addr(lc,tmp);
	else
		lc->rtp_conf.audio_multicast_addr=ms_strdup("224.1.2.3");
	if ((tmp_int=lp_config_get_int(lc->config,"rtp","audio_multicast_enabled",-1)) >-1)
		linphone_core_enable_audio_multicast(lc,tmp_int);
	if ((tmp_int=lp_config_get_int(lc->config,"rtp","audio_multicast_ttl",-1))>0)
			linphone_core_set_audio_multicast_ttl(lc,tmp_int);
	else
		lc->rtp_conf.audio_multicast_ttl=1;/*local network*/
	if ((tmp=lp_config_get_string(lc->config,"rtp","video_multicast_addr",NULL)))
		linphone_core_set_video_multicast_addr(lc,tmp);
	else
		lc->rtp_conf.video_multicast_addr=ms_strdup("224.1.2.3");
	if ((tmp_int=lp_config_get_int(lc->config,"rtp","video_multicast_ttl",-1))>-1)
		linphone_core_set_video_multicast_ttl(lc,tmp_int);
	else
		lc->rtp_conf.video_multicast_ttl=1;/*local network*/
	if ((tmp_int=lp_config_get_int(lc->config,"rtp","video_multicast_enabled",-1)) >0)
		linphone_core_enable_video_multicast(lc,tmp_int);
aymeric's avatar
aymeric committed
1034 1035
}

1036
static PayloadType * find_payload(const MSList *default_list, const char *mime_type, int clock_rate, int channels, const char *recv_fmtp){
1037 1038
	PayloadType *candidate=NULL;
	PayloadType *it;
1039
	const MSList *elem;
1040

1041 1042
	for(elem=default_list;elem!=NULL;elem=elem->next){
		it=(PayloadType*)elem->data;
1043
		if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
1044 1045
			&& (clock_rate==it->clock_rate || clock_rate<=0)
			&& (channels==it->channels || channels<=0) ){
1046
			if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
1047 1048
				(recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
1049
				if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
1050
				return it;
1051 1052 1053 1054 1055
			}else {
				if (candidate){
					if (it->recv_fmtp==NULL) candidate=it;
				}else candidate=it;
			}
1056 1057
		}
	}
1058 1059 1060
	if (candidate && recv_fmtp){
		payload_type_set_recv_fmtp(candidate,recv_fmtp);
	}
1061 1062
	return candidate;
}
aymeric's avatar
aymeric committed
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
static PayloadType* find_payload_type_from_list(const char* type, int rate, int channels, const MSList* from) {
	const MSList *elem;
	for(elem=from;elem!=NULL;elem=elem->next){
		PayloadType *pt=(PayloadType*)elem->data;
		if ((strcasecmp(type, payload_type_get_mime(pt)) == 0)
			&& (rate == LINPHONE_FIND_PAYLOAD_IGNORE_RATE || rate==pt->clock_rate)
			&& (channels == LINPHONE_FIND_PAYLOAD_IGNORE_CHANNELS || channels==pt->channels)) {
			return pt;
		}
	}
	return NULL;
}

static bool_t get_codec(LinphoneCore *lc, SalStreamType type, int index, PayloadType **ret){
aymeric's avatar
aymeric committed
1078 1079
	char codeckey[50];
	const char *mime,*fmtp;
1080
	int rate,channels,enabled;
aymeric's avatar
aymeric committed
1081
	PayloadType *pt;
1082
	LpConfig *config=lc->config;
1083

1084
	*ret=NULL;
1085
	snprintf(codeckey,50,"%s_codec_%i",type==SalAudio ? "audio" : "video", index);
aymeric's avatar
aymeric committed
1086
	mime=lp_config_get_string(config,codeckey,"mime",NULL);
1087
	if (mime==NULL || strlen(mime)==0 ) return FALSE;
1088

aymeric's avatar
aymeric committed
1089 1090
	rate=lp_config_get_int(config,codeckey,"rate",8000);
	fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
1091
	channels=lp_config_get_int(config,codeckey,"channels",0);
aymeric's avatar
aymeric committed
1092
	enabled=lp_config_get_int(config,codeckey,"enabled",1);
1093 1094 1095 1096 1097 1098 1099
	if (!ms_filter_codec_supported(mime)){
		ms_warning("Codec %s/%i read from conf is not supported by mediastreamer2, ignored.",mime,rate);
		return TRUE;
	}
	pt=find_payload(type==SalAudio ? lc->default_audio_codecs : lc->default_video_codecs,mime,rate,channels,fmtp);
	if (!pt){
		MSList **default_list=(type==SalAudio) ? &lc->default_audio_codecs :  &lc->default_video_codecs;
1100 1101
		if (type==SalAudio)
			ms_warning("Codec %s/%i/%i read from conf is not in the default list.",mime,rate,channels);
1102
		else
1103
			ms_warning("Codec %s/%i read from conf is not in the default list.",mime,rate);
1104 1105 1106 1107 1108
		pt=payload_type_new();
		pt->type=(type==SalAudio) ? PAYLOAD_AUDIO_PACKETIZED :  PAYLOAD_VIDEO;
		pt->mime_type=ortp_strdup(mime);
		pt->clock_rate=rate;
		pt->channels=channels;
1109
		payload_type_set_number(pt,-1); /*dynamic assignment*/
1110 1111 1112 1113
		payload_type_set_recv_fmtp(pt,fmtp);
		*default_list=ms_list_append(*default_list, pt);
	}
	if (enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
1114
	else pt->flags&=~PAYLOAD_TYPE_ENABLED;
1115 1116 1117 1118
	*ret=pt;
	return TRUE;
}

1119 1120 1121 1122 1123 1124
/*this function merges the payload types from the codec default list with the list read from configuration file.
 * If a new codec becomes supported in Liblinphone or if the list from configuration file is empty or incomplete, all the supported codecs are added
 * automatically. This 'l' list is entirely destroyed and rewritten.*/
static MSList *add_missing_codecs(const MSList *default_list, MSList *l){
	const MSList *elem;
	MSList *newlist;
1125

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	for(elem=default_list; elem!=NULL; elem=elem->next){
		MSList *elem2=ms_list_find(l,elem->data);
		if (!elem2){
			PayloadType *pt=(PayloadType*)elem->data;
			/*this codec from default list should be inserted in the list*/
			if (!elem->prev){
				l=ms_list_prepend(l,pt);
			}else{
				const MSList *after=ms_list_find(l,elem->prev->data);
				l=ms_list_insert(l, after->next, pt);
1136
			}
1137 1138
			ms_message("Supported codec %s/%i fmtp=%s automatically added to codec list.", pt->mime_type,
				   pt->clock_rate, pt->recv_fmtp ? pt->recv_fmtp : "");
1139 1140
		}
	}
1141 1142 1143
	newlist=ms_list_copy_with_data(l,(void *(*)(void*))payload_type_clone);
	ms_list_free(l);
	return newlist;
aymeric's avatar
aymeric committed
1144 1145
}

1146 1147
static MSList *codec_append_if_new(MSList *l, PayloadType *pt){
	MSList *elem;
1148
	for (elem=l;elem!=NULL;elem=elem->next){
1149 1150 1151 1152 1153 1154 1155 1156
		PayloadType *ept=(PayloadType*)elem->data;
		if (pt==ept)
			return l;
	}
	l=ms_list_append(l,pt);
	return l;
}

1157
static void codecs_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
1158 1159 1160 1161 1162
{
	int i;
	PayloadType *pt;
	MSList *audio_codecs=NULL;
	MSList *video_codecs=NULL;
1163

1164 1165
	lc->codecs_conf.dyn_pt=96;
	lc->codecs_conf.telephone_event_pt=lp_config_get_int(lc->config,"misc","telephone_event_pt",101);
1166

1167
	for (i=0;get_codec(lc,SalAudio,i,&pt);i++){
1168
		if (pt){
1169
			audio_codecs=codec_append_if_new(audio_codecs, pt);
1170
		}
aymeric's avatar
aymeric committed
1171
	}
1172
	audio_codecs=add_missing_codecs(lc->default_audio_codecs,audio_codecs);
1173

1174
	for (i=0;get_codec(lc,SalVideo,i,&pt);i++){
1175
		if (pt){
1176
			video_codecs=codec_append_if_new(video_codecs, pt);
1177
		}
aymeric's avatar
aymeric committed
1178
	}
1179
	video_codecs=add_missing_codecs(lc->default_video_codecs,video_codecs);
aymeric's avatar
aymeric committed
1180 1181
	linphone_core_set_audio_codecs(lc,audio_codecs);
	linphone_core_set_video_codecs(lc,video_codecs);
1182
	linphone_core_update_allocated_audio_bandwidth(lc);
aymeric's avatar
aymeric committed
1183 1184
}

1185
static void build_video_devices_table(LinphoneCore *lc){
aymeric's avatar
aymeric committed
1186 1187
	const MSList *elem;
	int i;
1188 1189 1190 1191
	int ndev;
	const char **devices;
	if (lc->video_conf.cams)
		ms_free(lc->video_conf.cams);
aymeric's avatar
aymeric committed
1192 1193 1194 1195 1196 1197 1198 1199 1200
	/* retrieve all video devices */
	elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
	ndev=ms_list_size(elem);
	devices=ms_malloc((ndev+1)*sizeof(const char *));
	for (i=0;elem!=NULL;elem=elem->next,i++){
		devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
	}
	devices[ndev]=NULL;
	lc->video_conf.cams=devices;
1201 1202 1203 1204
}

static void video_config_read(LinphoneCore *lc){
#ifdef VIDEO_ENABLED
1205
	int capture, display, self_view, reuse_source;
Simon Morlat's avatar
Simon Morlat committed
1206
	int automatic_video=1;
1207
#endif
1208 1209
	const char *str;
#ifdef VIDEO_ENABLED
1210
	LinphoneVideoPolicy vpol;
1211
	memset(&vpol, 0, sizeof(LinphoneVideoPolicy));
1212
#endif
1213
	build_video_devices_table(lc);
aymeric's avatar
aymeric committed
1214 1215 1216 1217

	str=lp_config_get_string(lc->config,"video","device",NULL);
	if (str && str[0]==0) str=NULL;
	linphone_core_set_video_device(lc,str);
1218

smorlat's avatar
smorlat committed
1219 1220
	linphone_core_set_preferred_video_size_by_name(lc,
		lp_config_get_string(lc->config,"video","size","cif"));
1221

1222 1223
	linphone_core_set_preview_video_size_by_name(lc,
		lp_config_get_string(lc->config,"video","preview_size",NULL));
1224

1225
	linphone_core_set_preferred_framerate(lc,lp_config_get_float(lc->config,"video","framerate",0));
smorlat's avatar
smorlat committed
1226

1227
#ifdef VIDEO_ENABLED
Simon Morlat's avatar
Simon Morlat committed
1228 1229 1230
#if defined(ANDROID) || defined(__ios)
	automatic_video=0;
#endif
1231 1232 1233
	capture=lp_config_get_int(lc->config,"video","capture",1);
	display=lp_config_get_int(lc->config,"video","display",1);
	self_view=lp_config_get_int(lc->config,"video","self_view",1);
1234
	reuse_source=lp_config_get_int(lc->config,"video","reuse_source",0);
Simon Morlat's avatar
Simon Morlat committed
1235 1236