proxy.c 60.7 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
*/
/*
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
20

aymeric's avatar
aymeric committed
21
#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 "mediastreamer2/mediastream.h"
26
#include "enum.h"
aymeric's avatar
aymeric committed
27

28 29
#include <ctype.h>

30
/*store current config related to server location*/
31 32 33 34
static void linphone_proxy_config_store_server_config(LinphoneProxyConfig* cfg) {
	if (cfg->saved_identity) linphone_address_destroy(cfg->saved_identity);
	if (cfg->identity_address)
		cfg->saved_identity = linphone_address_clone(cfg->identity_address);
35
	else
36
		cfg->saved_identity = NULL;
37

38 39 40
	if (cfg->saved_proxy) linphone_address_destroy(cfg->saved_proxy);
	if (cfg->reg_proxy)
		cfg->saved_proxy = linphone_address_new(cfg->reg_proxy);
41
	else
42
		cfg->saved_proxy = NULL;
43 44
}

45
LinphoneProxyConfigAddressComparisonResult linphone_proxy_config_address_equal(const LinphoneAddress *a, const LinphoneAddress *b) {
46
	if (a == NULL && b == NULL)
47
		return LinphoneProxyConfigAddressEqual;
48
	else if (!a || !b)
49
		return LinphoneProxyConfigAddressDifferent;
50

51 52
	if (linphone_address_equal(a,b))
		return LinphoneProxyConfigAddressEqual;
53 54
	if (linphone_address_weak_equal(a,b)) {
		/*also check both transport and uri */
55 56 57 58 59 60
		if (linphone_address_is_secure(a) == linphone_address_is_secure(b) && linphone_address_get_transport(a) == linphone_address_get_transport(b))
			return LinphoneProxyConfigAddressWeakEqual;
		else
			return LinphoneProxyConfigAddressDifferent;
	}
	return LinphoneProxyConfigAddressDifferent; /*either username, domain or port ar not equals*/
61 62
}

63 64
LinphoneProxyConfigAddressComparisonResult linphone_proxy_config_is_server_config_changed(const LinphoneProxyConfig* cfg) {
	LinphoneAddress *current_proxy=cfg->reg_proxy?linphone_address_new(cfg->reg_proxy):NULL;
65 66
	LinphoneProxyConfigAddressComparisonResult result_identity;
	LinphoneProxyConfigAddressComparisonResult result;
67

68
	result = linphone_proxy_config_address_equal(cfg->saved_identity,cfg->identity_address);
69
	if (result == LinphoneProxyConfigAddressDifferent) goto end;
70
	result_identity = result;
71

72
	result = linphone_proxy_config_address_equal(cfg->saved_proxy,current_proxy);
73
	if (result == LinphoneProxyConfigAddressDifferent) goto end;
74 75 76 77 78
	/** If the proxies are equal use the result of the difference between the identities,
	  * otherwise the result is weak-equal and so weak-equal must be returned even if the
	  * identities were equal.
	  */
	if (result == LinphoneProxyConfigAddressEqual) result = result_identity;
79

Simon Morlat's avatar
Simon Morlat committed
80 81 82
	end:
	if (current_proxy) linphone_address_destroy(current_proxy);
	return result;
83
}
84

smorlat's avatar
smorlat committed
85 86 87
void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
	MSList *elem;
	int i;
88
	if (!linphone_core_ready(lc)) return;
89

smorlat's avatar
smorlat committed
90 91 92 93
	for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
		LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
		linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
	}
94 95
	/*to ensure removed configs are erased:*/
	linphone_proxy_config_write_to_config_file(lc->config,NULL,i);
96
	lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy_config_index(lc));
smorlat's avatar
smorlat committed
97
}
aymeric's avatar
aymeric committed
98

99
static void linphone_proxy_config_init(LinphoneCore* lc, LinphoneProxyConfig *cfg) {
100 101 102 103
	const char *dial_prefix = lc ? lp_config_get_default_string(lc->config,"proxy","dial_prefix",NULL) : NULL;
	const char *identity = lc ? lp_config_get_default_string(lc->config, "proxy", "reg_identity", NULL) : NULL;
	const char *proxy = lc ? lp_config_get_default_string(lc->config, "proxy", "reg_proxy", NULL) : NULL;
	const char *route = lc ? lp_config_get_default_string(lc->config, "proxy", "reg_route", NULL) : NULL;
104
	const char *realm = lc ? lp_config_get_default_string(lc->config, "proxy", "realm", NULL) : NULL;
105
	const char *quality_reporting_collector = lc ? lp_config_get_default_string(lc->config, "proxy", "quality_reporting_collector", NULL) : NULL;
106 107
	const char *contact_params = lc ? lp_config_get_default_string(lc->config, "proxy", "contact_parameters", NULL) : NULL;
	const char *contact_uri_params = lc ? lp_config_get_default_string(lc->config, "proxy", "contact_uri_parameters", NULL) : NULL;
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	cfg->expires = lc ? lp_config_get_default_int(lc->config, "proxy", "reg_expires", 3600) : 3600;
	cfg->reg_sendregister = lc ? lp_config_get_default_int(lc->config, "proxy", "reg_sendregister", 1) : 1;
	cfg->dial_prefix = dial_prefix ? ms_strdup(dial_prefix) : NULL;
	cfg->dial_escape_plus = lc ? lp_config_get_default_int(lc->config, "proxy", "dial_escape_plus", 0) : 0;
	cfg->privacy = lc ? lp_config_get_default_int(lc->config, "proxy", "privacy", LinphonePrivacyDefault) : LinphonePrivacyDefault;
	cfg->identity_address = identity ? linphone_address_new(identity) : NULL;
	cfg->reg_identity = cfg->identity_address ? linphone_address_as_string(cfg->identity_address) : NULL;
	cfg->reg_proxy = proxy ? ms_strdup(proxy) : NULL;
	cfg->reg_route = route ? ms_strdup(route) : NULL;
	cfg->realm = realm ? ms_strdup(realm) : NULL;
	cfg->quality_reporting_enabled = lc ? lp_config_get_default_int(lc->config, "proxy", "quality_reporting_enabled", 0) : 0;
	cfg->quality_reporting_collector = quality_reporting_collector ? ms_strdup(quality_reporting_collector) : NULL;
	cfg->quality_reporting_interval = lc ? lp_config_get_default_int(lc->config, "proxy", "quality_reporting_interval", 0) : 0;
	cfg->contact_params = contact_params ? ms_strdup(contact_params) : NULL;
	cfg->contact_uri_params = contact_uri_params ? ms_strdup(contact_uri_params) : NULL;
	cfg->avpf_mode = lc ? lp_config_get_default_int(lc->config, "proxy", "avpf", LinphoneAVPFDefault) : LinphoneAVPFDefault;
	cfg->avpf_rr_interval = lc ? lp_config_get_default_int(lc->config, "proxy", "avpf_rr_interval", 5) : 5;
	cfg->publish_expires=-1;
aymeric's avatar
aymeric committed
127 128
}

129 130 131
LinphoneProxyConfig *linphone_proxy_config_new() {
	return linphone_core_create_proxy_config(NULL);
}
132

133
static void _linphone_proxy_config_destroy(LinphoneProxyConfig *cfg);
134 135 136 137 138 139 140 141 142 143

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneProxyConfig);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneProxyConfig, belle_sip_object_t,
	(belle_sip_object_destroy_t)_linphone_proxy_config_destroy,
	NULL, // clone
	NULL, // marshal
	FALSE
);

144
LinphoneProxyConfig * linphone_core_create_proxy_config(LinphoneCore *lc) {
145 146 147
	LinphoneProxyConfig *cfg = belle_sip_object_new(LinphoneProxyConfig);
	linphone_proxy_config_init(lc,cfg);
	return cfg;
148 149
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
void _linphone_proxy_config_release_ops(LinphoneProxyConfig *cfg){
	if (cfg->op) {
		sal_op_release(cfg->op);
		cfg->op=NULL;
	}
	if (cfg->publish_op){
		sal_op_release(cfg->publish_op);
		cfg->publish_op=NULL;
	}
}

void _linphone_proxy_config_destroy(LinphoneProxyConfig *cfg){
	if (cfg->reg_proxy!=NULL) ms_free(cfg->reg_proxy);
	if (cfg->reg_identity!=NULL) ms_free(cfg->reg_identity);
	if (cfg->identity_address!=NULL) linphone_address_destroy(cfg->identity_address);
	if (cfg->reg_route!=NULL) ms_free(cfg->reg_route);
	if (cfg->quality_reporting_collector!=NULL) ms_free(cfg->quality_reporting_collector);
	if (cfg->ssctx!=NULL) sip_setup_context_free(cfg->ssctx);
	if (cfg->realm!=NULL) ms_free(cfg->realm);
	if (cfg->type!=NULL) ms_free(cfg->type);
	if (cfg->dial_prefix!=NULL) ms_free(cfg->dial_prefix);
	if (cfg->contact_params) ms_free(cfg->contact_params);
	if (cfg->contact_uri_params) ms_free(cfg->contact_uri_params);
	if (cfg->saved_proxy!=NULL) linphone_address_destroy(cfg->saved_proxy);
	if (cfg->saved_identity!=NULL) linphone_address_destroy(cfg->saved_identity);
	if (cfg->sent_headers!=NULL) sal_custom_header_free(cfg->sent_headers);
176
	if (cfg->pending_contact) linphone_address_unref(cfg->pending_contact);
177
	_linphone_proxy_config_release_ops(cfg);
178 179 180 181 182 183
}

void linphone_proxy_config_destroy(LinphoneProxyConfig *cfg) {
	belle_sip_object_unref(cfg);
}

184 185 186 187 188
void _linphone_proxy_config_release(LinphoneProxyConfig *cfg) {
	_linphone_proxy_config_release_ops(cfg);
	belle_sip_object_unref(cfg);
}

189 190 191 192 193 194 195
LinphoneProxyConfig *linphone_proxy_config_ref(LinphoneProxyConfig *cfg) {
	belle_sip_object_ref(cfg);
	return cfg;
}

void linphone_proxy_config_unref(LinphoneProxyConfig *cfg) {
	belle_sip_object_unref(cfg);
aymeric's avatar
aymeric committed
196 197
}

198 199
bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *cfg){
	return cfg->state == LinphoneRegistrationOk;
smorlat's avatar
smorlat committed
200 201
}

202
int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *cfg, const char *server_addr){
203 204
	LinphoneAddress *addr=NULL;
	char *modified=NULL;
205

206 207
	if (cfg->reg_proxy!=NULL) ms_free(cfg->reg_proxy);
	cfg->reg_proxy=NULL;
208

aymeric's avatar
aymeric committed
209
	if (server_addr!=NULL && strlen(server_addr)>0){
210
		if (strstr(server_addr,"sip:")==NULL && strstr(server_addr,"sips:")==NULL){
211 212 213
			modified=ms_strdup_printf("sip:%s",server_addr);
			addr=linphone_address_new(modified);
			ms_free(modified);
214
		}
215 216
		if (addr==NULL)
			addr=linphone_address_new(server_addr);
217
		if (addr){
218
			cfg->reg_proxy=linphone_address_as_string(addr);
219
			linphone_address_destroy(addr);
aymeric's avatar
aymeric committed
220 221
		}else{
			ms_warning("Could not parse %s",server_addr);
222
			return -1;
aymeric's avatar
aymeric committed
223 224 225 226 227
		}
	}
	return 0;
}

228 229 230

int linphone_proxy_config_set_identity_address(LinphoneProxyConfig *cfg, const LinphoneAddress *addr){
	if (!addr || linphone_address_get_username(addr)==NULL){
231 232
		char* as_string = addr ? linphone_address_as_string(addr) : ms_strdup("NULL");
		ms_warning("Invalid sip identity: %s", as_string);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		ms_free(as_string);
		return -1;
	}
	if (cfg->identity_address != NULL) {
		linphone_address_destroy(cfg->identity_address);
	}
	cfg->identity_address=linphone_address_clone(addr);

	if (cfg->reg_identity!=NULL) {
		ms_free(cfg->reg_identity);
	}
	cfg->reg_identity= linphone_address_as_string(cfg->identity_address);
	return 0;
}

int linphone_proxy_config_set_identity(LinphoneProxyConfig *cfg, const char *identity){
aymeric's avatar
aymeric committed
249
	if (identity!=NULL && strlen(identity)>0){
250 251
		LinphoneAddress *addr=linphone_address_new(identity);
		int ret=linphone_proxy_config_set_identity_address(cfg, addr);
252
		if (addr) linphone_address_destroy(addr);
253
		return ret;
aymeric's avatar
aymeric committed
254
	}
255
	return -1;
smorlat's avatar
smorlat committed
256 257 258
}

const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
259
	return cfg->identity_address ? linphone_address_get_domain(cfg->identity_address) : NULL;
aymeric's avatar
aymeric committed
260 261
}

262
int linphone_proxy_config_set_route(LinphoneProxyConfig *cfg, const char *route)
aymeric's avatar
aymeric committed
263
{
264 265 266
	if (cfg->reg_route!=NULL){
		ms_free(cfg->reg_route);
		cfg->reg_route=NULL;
aymeric's avatar
aymeric committed
267
	}
268
	if (route!=NULL && route[0] !='\0'){
269 270
		SalAddress *addr;
		char *tmp;
271
		/*try to prepend 'sip:' */
272
		if (strstr(route,"sip:")==NULL && strstr(route,"sips:")==NULL){
273 274 275 276 277
			tmp=ms_strdup_printf("sip:%s",route);
		}else tmp=ms_strdup(route);
		addr=sal_address_new(tmp);
		if (addr!=NULL){
			sal_address_destroy(addr);
278 279
			cfg->reg_route=tmp;
			return 0;
280 281
		}else{
			ms_free(tmp);
282
			return -1;
283
		}
284 285
	} else {
		return 0;
286
	}
aymeric's avatar
aymeric committed
287 288
}

289 290
bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *cfg){
	if (cfg->reg_proxy==NULL){
291 292
		if (lc)
			linphone_core_notify_display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
aymeric's avatar
aymeric committed
293 294 295
						" followed by a hostname."));
		return FALSE;
	}
296
	if (cfg->identity_address==NULL){
297 298
		if (lc)
			linphone_core_notify_display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
aymeric's avatar
aymeric committed
299 300 301 302 303 304
					"sip:username@proxydomain, such as sip:alice@example.net"));
		return FALSE;
	}
	return TRUE;
}

305 306
void linphone_proxy_config_enableregister(LinphoneProxyConfig *cfg, bool_t val){
	cfg->reg_sendregister=val;
aymeric's avatar
aymeric committed
307 308
}

309
void linphone_proxy_config_set_expires(LinphoneProxyConfig *cfg, int val){
310
	if (val<0) val=600;
311
	cfg->expires=val;
aymeric's avatar
aymeric committed
312 313
}

314 315
void linphone_proxy_config_enable_publish(LinphoneProxyConfig *cfg, bool_t val){
	cfg->publish=val;
aymeric's avatar
aymeric committed
316
}
317

318 319
void linphone_proxy_config_pause_register(LinphoneProxyConfig *cfg){
	if (cfg->op) sal_op_stop_refreshing(cfg->op);
320 321
}

322 323 324 325 326 327
void linphone_proxy_config_edit(LinphoneProxyConfig *cfg){
	if (cfg->publish && cfg->publish_op){
			/*unpublish*/
			sal_publish_presence(cfg->publish_op,NULL,NULL,0,(SalPresenceModel *)NULL);
			sal_op_release(cfg->publish_op);
			cfg->publish_op=NULL;
aymeric's avatar
aymeric committed
328
	}
329
	/*store current config related to server location*/
330
	linphone_proxy_config_store_server_config(cfg);
331 332

	/*stop refresher in any case*/
333
	linphone_proxy_config_pause_register(cfg);
aymeric's avatar
aymeric committed
334 335
}

336 337 338
void linphone_proxy_config_apply(LinphoneProxyConfig *cfg,LinphoneCore *lc){
	cfg->lc=lc;
	linphone_proxy_config_done(cfg);
aymeric's avatar
aymeric committed
339
}
Simon Morlat's avatar
Simon Morlat committed
340

341 342 343 344 345 346 347 348 349 350 351 352 353
void linphone_proxy_config_stop_refreshing(LinphoneProxyConfig * cfg){
	LinphoneAddress *contact_addr=NULL;
	if (	cfg->op
			&& cfg->state == LinphoneRegistrationOk
			&& (contact_addr = (LinphoneAddress*)sal_op_get_contact_address(cfg->op))
			&& linphone_address_get_transport(contact_addr) != LinphoneTransportUdp /*with udp, there is a risk of port reuse, so I prefer to not do anything for now*/) {
		/*need to save current contact in order to reset is later*/
		linphone_address_ref(contact_addr);
		if (cfg->pending_contact)
			linphone_address_unref(cfg->pending_contact);
		cfg->pending_contact=contact_addr;

	}
354 355 356
	if (cfg->publish_op){
		sal_op_release(cfg->publish_op);
		cfg->publish_op=NULL;
357
	}
358 359 360
	if (cfg->op){
		sal_op_release(cfg->op);
		cfg->op=NULL;
361 362 363
	}
}

364
LinphoneAddress *guess_contact_for_register(LinphoneProxyConfig *cfg){
365
	LinphoneAddress *ret=NULL;
366
	LinphoneAddress *proxy=linphone_address_new(cfg->reg_proxy);
367
	const char *host;
368

369
	if (proxy==NULL) return NULL;
370
	host=linphone_address_get_domain(proxy);
371
	if (host!=NULL){
372 373
		int localport = -1;
		const char *localip = NULL;
374
		LinphoneAddress *contact=linphone_address_clone(cfg->identity_address);
375

376
		linphone_address_clean(contact);
377

378
		if (cfg->contact_params) {
379
			// We want to add a list of contacts params to the linphone address
380
			sal_address_set_params(contact,cfg->contact_params);
381
		}
382 383
		if (cfg->contact_uri_params){
			sal_address_set_uri_params(contact,cfg->contact_uri_params);
384
		}
385
#ifdef BUILD_UPNP
386 387 388 389
		if (cfg->lc->upnp != NULL && linphone_core_get_firewall_policy(cfg->lc)==LinphonePolicyUseUpnp &&
			linphone_upnp_context_get_state(cfg->lc->upnp) == LinphoneUpnpStateOk) {
			localip = linphone_upnp_context_get_external_ipaddress(cfg->lc->upnp);
			localport = linphone_upnp_context_get_external_port(cfg->lc->upnp);
390
		}
391
#endif //BUILD_UPNP
392
		linphone_address_set_port(contact,localport);
393 394 395 396
		linphone_address_set_domain(contact,localip);
		linphone_address_set_display_name(contact,NULL);

		ret=contact;
397
	}
398
	linphone_address_destroy(proxy);
399 400
	return ret;
}
401

402 403 404 405
void _linphone_proxy_config_unregister(LinphoneProxyConfig *obj) {
	if (obj->op && (obj->state == LinphoneRegistrationOk ||
					(obj->state == LinphoneRegistrationProgress && obj->expires != 0))) {
		sal_unregister(obj->op);
406 407
	}
}
408

409 410 411
static void linphone_proxy_config_register(LinphoneProxyConfig *cfg){
	if (cfg->reg_sendregister){
		LinphoneAddress* proxy=linphone_address_new(cfg->reg_proxy);
Simon Morlat's avatar
Simon Morlat committed
412
		char* proxy_string;
413
		char * from = linphone_address_as_string(cfg->identity_address);
414
		LinphoneAddress *contact;
415
		ms_message("LinphoneProxyConfig [%p] about to register (LinphoneCore version: %s)",cfg,linphone_core_get_version());
416 417
		proxy_string=linphone_address_as_string_uri_only(proxy);
		linphone_address_destroy(proxy);
418 419 420
		if (cfg->op)
			sal_op_release(cfg->op);
		cfg->op=sal_op_new(cfg->lc->sal);
421

422
		linphone_configure_op(cfg->lc, cfg->op, cfg->identity_address, cfg->sent_headers, FALSE);
423

424 425
		if ((contact=guess_contact_for_register(cfg))) {
			sal_op_set_contact_address(cfg->op,contact);
Simon Morlat's avatar
Simon Morlat committed
426
			linphone_address_destroy(contact);
427
		}
428

429
		sal_op_set_user_pointer(cfg->op,cfg);
430

431 432 433 434 435 436

		if (sal_register(cfg->op,proxy_string, cfg->reg_identity, cfg->expires, cfg->pending_contact)==0) {
			if (cfg->pending_contact) {
				linphone_address_unref(cfg->pending_contact);
				cfg->pending_contact=NULL;
			}
437
			linphone_proxy_config_set_state(cfg,LinphoneRegistrationProgress,"Registration in progress");
438
		} else {
439
			linphone_proxy_config_set_state(cfg,LinphoneRegistrationFailed,"Registration failed");
440
		}
441
		ms_free(proxy_string);
442
		ms_free(from);
443
	} else {
444
		/* unregister if registered*/
445 446
		if (cfg->state == LinphoneRegistrationProgress) {
			linphone_proxy_config_set_state(cfg,LinphoneRegistrationCleared,"Registration cleared");
447
		}
448
		_linphone_proxy_config_unregister(cfg);
aymeric's avatar
aymeric committed
449
	}
smorlat's avatar
smorlat committed
450 451
}

452 453 454 455
void linphone_proxy_config_refresh_register(LinphoneProxyConfig *cfg){
	if (cfg->reg_sendregister && cfg->op && cfg->state!=LinphoneRegistrationProgress){
		if (sal_register_refresh(cfg->op,cfg->expires) == 0) {
			linphone_proxy_config_set_state(cfg,LinphoneRegistrationProgress, "Refresh registration");
456
		}
457 458 459
	}
}

460 461 462 463 464 465

void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
	if (cfg->dial_prefix!=NULL){
		ms_free(cfg->dial_prefix);
		cfg->dial_prefix=NULL;
	}
466
	if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
467 468 469 470 471 472 473 474 475 476 477 478 479
}

const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
	return cfg->dial_prefix;
}

void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
	cfg->dial_escape_plus=val;
}

bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
	return cfg->dial_escape_plus;
}
480

481 482
void linphone_proxy_config_enable_quality_reporting(LinphoneProxyConfig *cfg, bool_t val){
	cfg->quality_reporting_enabled = val;
483 484
}

485
bool_t linphone_proxy_config_quality_reporting_enabled(LinphoneProxyConfig *cfg){
486
	return cfg->quality_reporting_enabled;
487 488
}

489
void linphone_proxy_config_set_quality_reporting_interval(LinphoneProxyConfig *cfg, uint8_t interval) {
490
	cfg->quality_reporting_interval = interval;
491 492 493 494 495 496
}

int linphone_proxy_config_get_quality_reporting_interval(LinphoneProxyConfig *cfg) {
	return cfg->quality_reporting_interval;
}

497
void linphone_proxy_config_set_quality_reporting_collector(LinphoneProxyConfig *cfg, const char *collector){
498 499
	if (collector!=NULL && strlen(collector)>0){
		LinphoneAddress *addr=linphone_address_new(collector);
500
		if (!addr){
501
			ms_error("Invalid SIP collector URI: %s. Quality reporting will be DISABLED.",collector);
502
		} else {
503
			if (cfg->quality_reporting_collector != NULL){
504
				ms_free(cfg->quality_reporting_collector);
505
			}
506
			cfg->quality_reporting_collector = ms_strdup(collector);
507 508 509
		}

		if (addr){
510 511 512 513 514
			linphone_address_destroy(addr);
		}
	}
}

515 516
const char *linphone_proxy_config_get_quality_reporting_collector(const LinphoneProxyConfig *cfg){
	return cfg->quality_reporting_collector;
517 518 519
}


520 521 522 523 524 525
/*
 * http://en.wikipedia.org/wiki/Telephone_numbering_plan
 * http://en.wikipedia.org/wiki/Telephone_numbers_in_Europe
 */
typedef struct dial_plan{
	const char *country;
526
	const char* iso_country_code; /* ISO 3166-1 alpha-2 code, ex: FR for France*/
527 528 529
	char  ccc[8]; /*country calling code*/
	int nnl; /*maximum national number length*/
	const char * icp; /*international call prefix, ex: 00 in europe*/
530

531 532 533
}dial_plan_t;

static dial_plan_t const dial_plans[]={
534
	//Country					, iso country code, e164 country calling code, number length, international usual prefix
jehan's avatar
jehan committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	{"Afghanistan"                  ,"AF"		, "93"      , 9		, "00"  },
	{"Albania"                      ,"AL"		, "355"     , 9		, "00"  },
	{"Algeria"                      ,"DZ"		, "213"     , 9		, "00"  },
	{"American Samoa"               ,"AS"		, "1"       , 10	, "011"	},
	{"Andorra"                      ,"AD"		, "376"     , 6		, "00"  },
	{"Angola"                       ,"AO"		, "244"     , 9		, "00"  },
	{"Anguilla"                     ,"AI"		, "1"       , 10	, "011" },
	{"Antigua and Barbuda"          ,"AG"		, "1"       , 10	, "011"	},
	{"Argentina"                    ,"AR"		, "54"      , 10	, "00"  },
	{"Armenia"                      ,"AM"		, "374"     , 8		, "00"  },
	{"Aruba"                        ,"AW"		, "297"     , 7		, "011"	},
	{"Australia"                    ,"AU"		, "61"      , 9	    , "0011"},
	{"Austria"                      ,"AT"		, "43"      , 10	, "00"  },
	{"Azerbaijan"                   ,"AZ"       , "994"     , 9		, "00"  },
	{"Bahamas"                      ,"BS"		, "1"       , 10    , "011"	},
	{"Bahrain"                      ,"BH"		, "973"     , 8     , "00"  },
	{"Bangladesh"                   ,"BD"		, "880"     , 10    , "00"  },
	{"Barbados"                     ,"BB"		, "1"       , 10    , "011"	},
553
	{"Belarus"                      ,"BY"		, "375"     , 9     , "00"  },
jehan's avatar
jehan committed
554 555 556 557 558 559 560 561
	{"Belgium"                      ,"BE"		, "32"      , 9     , "00"  },
	{"Belize"                       ,"BZ"		, "501"     , 7     , "00"  },
	{"Benin"                        ,"BJ"		, "229"     , 8     , "00"	},
	{"Bermuda"                      ,"BM"		, "1"       , 10    , "011" },
	{"Bhutan"                       ,"BT"		, "975"     , 8     , "00"  },
	{"Bolivia"                      ,"BO"		, "591"     , 8     , "00"	},
	{"Bosnia and Herzegovina"       ,"BA"		, "387"     , 8     , "00"  },
	{"Botswana"                     ,"BW"		, "267"     , 8     , "00"  },
562
	{"Brazil"                       ,"BR"		, "55"      , 10	, "00"  },
jehan's avatar
jehan committed
563 564 565 566
	{"Brunei Darussalam"            ,"BN"		, "673"     , 7		, "00"	},
	{"Bulgaria"                     ,"BG"		, "359"     , 9		, "00"  },
	{"Burkina Faso"                 ,"BF"		, "226"     , 8		, "00"  },
	{"Burundi"                      ,"BI"		, "257"     , 8     , "011" },
567
	{"Cambodia"                     ,"KH"		, "855"     , 9		, "00"  },
jehan's avatar
jehan committed
568 569 570 571 572 573 574 575
	{"Cameroon"                     ,"CM"		, "237"     , 8		, "00"  },
	{"Canada"                       ,"CA"		, "1"       , 10	, "011" },
	{"Cape Verde"                   ,"CV"		, "238"     , 7		, "00"	},
	{"Cayman Islands"               ,"KY"		, "1"       , 10	, "011" },
	{"Central African Republic"     ,"CF"		, "236"     , 8     , "00"  },
	{"Chad"                         ,"TD"		, "235"     , 8		, "00"	},
	{"Chile"                        ,"CL"		, "56"      , 9	    , "00"  },
	{"China"                        ,"CN"		, "86"      , 11	, "00"  },
576 577 578 579 580 581
	{"Colombia"                     ,"CO"       , "57"      , 10	, "00"  },
	{"Comoros"                      ,"KM"		, "269"     , 7     , "00"	},
	{"Congo"                        ,"CG"		, "242"     , 9		, "00"	},
	{"Congo Democratic Republic"	,"CD"		, "243"     , 9		, "00"  },
	{"Cook Islands"                 ,"CK"		, "682"     , 5		, "00"  },
	{"Costa Rica"                   ,"CR"		, "506"     , 8     , "00"	},
582
	{"Cote d'Ivoire"	            ,"AD"		, "225"     , 8     , "00"  },
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	{"Croatia"                      ,"HR"		, "385"     , 9		, "00"  },
	{"Cuba"                         ,"CU"		, "53"      , 8     , "119" },
	{"Cyprus"                       ,"CY"		, "357"     , 8     , "00"	},
	{"Czech Republic"               ,"CZ"		, "420"     , 9     , "00"  },
	{"Denmark"                      ,"DK"		, "45"      , 8		, "00"  },
	{"Djibouti"                     ,"DJ"		, "253"     , 8		, "00"	},
	{"Dominica"                     ,"DM"		, "1"       , 10	, "011" },
	{"Dominican Republic"	        ,"DO"		, "1"       , 10	, "011" },
	{"Ecuador"                      ,"EC"       , "593"     , 9		, "00"  },
	{"Egypt"                        ,"EG"		, "20"      , 10	, "00"	},
	{"El Salvador"                  ,"SV"		, "503"     , 8		, "00"	},
	{"Equatorial Guinea"            ,"GQ"		, "240"     , 9		, "00"  },
	{"Eritrea"                      ,"ER"		, "291"     , 7		, "00"  },
	{"Estonia"                      ,"EE"		, "372"     , 8     , "00"	},
	{"Ethiopia"                     ,"ET"		, "251"     , 9     , "00"  },
	{"Falkland Islands"	            ,"FK"		, "500"     , 5		, "00"  },
	{"Faroe Islands"	            ,"FO"		, "298"     , 6     , "00"  },
	{"Fiji"                         ,"FJ"		, "679"     , 7     , "00"	},
	{"Finland"                      ,"FI"		, "358"     , 9     , "00"  },
	{"France"                       ,"FR"		, "33"      , 9		, "00"	},
	{"French Guiana"				,"GF"		, "594"     , 9		, "00"	},
	{"French Polynesia"             ,"PF"		, "689"     , 6	    , "00"  },
	{"Gabon"                        ,"GA"		, "241"     , 8     , "00"  },
	{"Gambia"                       ,"GM"       , "220"     , 7		, "00"  },
	{"Georgia"                      ,"GE"		, "995"     , 9     , "00"	},
	{"Germany"                      ,"DE"		, "49"      , 11	, "00"	},
	{"Ghana"                        ,"GH"		, "233"     , 9		, "00"  },
	{"Gibraltar"                    ,"GI"		, "350"     , 8		, "00"  },
	{"Greece"                       ,"GR"		, "30"      ,10     , "00"	},
	{"Greenland"                    ,"GL"		, "299"     , 6		, "00"  },
	{"Grenada"                      ,"GD"		, "1"       , 10	, "011" },
	{"Guadeloupe"                   ,"GP"		, "590"     , 9     , "00"  },
	{"Guam"                         ,"GU"		, "1"       , 10	, "011"	},
	{"Guatemala"                    ,"GT"		, "502"     , 8     , "00"  },
	{"Guinea"                       ,"GN"		, "224"     , 8		, "00"  },
	{"Guinea-Bissau"				,"GW"		, "245"     , 7		, "00"	},
	{"Guyana"                       ,"GY"		, "592"     , 7	    , "001" },
	{"Haiti"                        ,"HT"		, "509"     , 8     , "00"  },
	{"Honduras"                     ,"HN"       , "504"     , 8		, "00"  },
	{"Hong Kong"                    ,"HK"		, "852"     , 8     , "001"	},
	{"Hungary"                      ,"HU"		, "36"      , 9     , "00"  },
	{"Iceland"                      ,"IS"		, "354"     , 9     , "00"  },
	{"India"                        ,"IN"		, "91"      , 10    , "00"  },
	{"Indonesia"                    ,"ID"		, "62"      , 10	, "001"	},
	{"Iran"                         ,"IR"		, "98"      , 10	, "00"	},
	{"Iraq"                         ,"IQ"		, "964"     , 10	, "00"  },
	{"Ireland"                      ,"IE"		, "353"     , 9		, "00"  },
	{"Israel"                       ,"IL"		, "972"     , 9     , "00"	},
	{"Italy"                        ,"IT"		, "39"      , 10	, "00"  },
632
/*	{"Jersey"                       ,"JE"		, "44"      , 10	, "00"	},*/
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	{"Jamaica"                      ,"JM"		, "1"       , 10	, "011" },
	{"Japan"                        ,"JP"		, "81"      , 10	, "010" },
	{"Jordan"                       ,"JO"		, "962"     , 9     , "00"	},
	{"Kazakhstan"                   ,"KZ"		, "7"       , 10    , "00"  },
	{"Kenya"                        ,"KE"		, "254"     , 9		, "000" },
	{"Kiribati"                     ,"KI"		, "686"     , 5		, "00"	},
	{"Korea, North"                 ,"KP"		, "850"     , 12	, "99"  },
	{"Korea, South"                 ,"KR"       , "82"      , 12	, "001" },
	{"Kuwait"                       ,"KW"		, "965"     , 8     , "00"	},
	{"Kyrgyzstan"                   ,"KG"		, "996"     , 9     , "00"  },
	{"Laos"                         ,"LA"		, "856"     , 10    , "00"  },
	{"Latvia"                       ,"LV"		, "371"     , 8     , "00"	},
	{"Lebanon"                      ,"LB"		, "961"     , 7     , "00"	},
	{"Lesotho"                      ,"LS"		, "266"     , 8		, "00"	},
	{"Liberia"                      ,"LR"		, "231"     , 8		, "00"  },
	{"Libya"                        ,"LY"		, "218"     , 8		, "00"  },
	{"Liechtenstein"                ,"LI"		, "423"     , 7     , "00"	},
	{"Lithuania"                    ,"LT"		, "370"     , 8		, "00"  },
	{"Luxembourg"                   ,"LU"		, "352"     , 9		, "00"  },
	{"Macau"                        ,"MO"		, "853"     , 8     , "00"  },
	{"Macedonia"                    ,"MK"		, "389"     , 8     , "00"	},
	{"Madagascar"                   ,"MG"		, "261"     , 9     , "00"  },
	{"Malawi"                       ,"MW"		, "265"     , 9		, "00"  },
	{"Malaysia"                     ,"MY"		, "60"      , 9		, "00"	},
	{"Maldives"                     ,"MV"		, "960"     , 7	    , "00"  },
	{"Mali"                         ,"ML"		, "223"     , 8     , "00"  },
	{"Malta"                        ,"MT"       , "356"     , 8		, "00"  },
	{"Marshall Islands"				,"MH"		, "692"     , 7     , "011"	},
	{"Martinique"                   ,"MQ"		, "596"     , 9     , "00"  },
	{"Mauritania"                   ,"MR"		, "222"     , 8     , "00"  },
	{"Mauritius"                    ,"MU"		, "230"     , 7     , "00"	},
	{"Mayotte Island"               ,"YT"		, "262"     , 9     , "00"	},
	{"Mexico"                       ,"MX"		, "52"      , 10	, "00"	},
	{"Micronesia"                   ,"FM"		, "691"     , 7		, "011" },
	{"Moldova"                      ,"MD"		, "373"     , 8		, "00"  },
	{"Monaco"                       ,"MC"		, "377"     , 8     , "00"	},
	{"Mongolia"                     ,"MN"		, "976"     , 8     , "001" },
	{"Montenegro"                   ,"ME"		, "382"     , 8		, "00"  },
	{"Montserrat"                   ,"MS"		, "664"     , 10	, "011" },
	{"Morocco"                      ,"MA"		, "212"     , 9     , "00"	},
	{"Mozambique"                   ,"MZ"		, "258"     , 9     , "00"  },
	{"Myanmar"                      ,"MM"		, "95"      , 8		, "00"  },
	{"Namibia"                      ,"NA"		, "264"     , 9		, "00"	},
	{"Nauru"                        ,"NR"		, "674"     , 7	    , "00"  },
	{"Nepal"                        ,"NP"		, "43"      , 10	, "00"  },
	{"Netherlands"                  ,"NL"       , "31"      , 9		, "00"  },
	{"New Caledonia"				,"NC"		, "687"     , 6     , "00"	},
	{"New Zealand"                  ,"NZ"		, "64"      , 10	, "00"  },
	{"Nicaragua"                    ,"NI"		, "505"     , 8     , "00"  },
	{"Niger"                        ,"NE"		, "227"     , 8     , "00"	},
	{"Nigeria"                      ,"NG"		, "234"     , 10	, "009"	},
	{"Niue"                         ,"NU"		, "683"     , 4		, "00"	},
	{"Norfolk Island"	            ,"NF"		, "672"     , 5		, "00"  },
	{"Northern Mariana Islands"	    ,"MP"		, "1"       , 10	, "011" },
	{"Norway"                       ,"NO"		, "47"      , 8     , "00"	},
	{"Oman"                         ,"OM"		, "968"     , 8		, "00"  },
	{"Pakistan"                     ,"PK"		, "92"      , 10	, "00"  },
	{"Palau"                        ,"PW"		, "680"     , 7     , "011" },
	{"Palestine"                    ,"PS"		, "970"     , 9     , "00"	},
	{"Panama"                       ,"PA"		, "507"     , 8     , "00"  },
	{"Papua New Guinea"	            ,"PG"		, "675"     , 8		, "00"  },
	{"Paraguay"                     ,"PY"		, "595"     , 9		, "00"	},
	{"Peru"                         ,"PE"		, "51"      , 9	    , "00"  },
	{"Philippines"                  ,"PH"		, "63"      , 10	, "00"  },
	{"Poland"                       ,"PL"       , "48"      , 9		, "00"  },
	{"Portugal"                     ,"PT"		, "351"     , 9     , "00"	},
	{"Puerto Rico"                  ,"PR"		, "1"       , 10	, "011" },
	{"Qatar"                        ,"QA"		, "974"     , 8     , "00"  },
	{"R�union Island"				,"RE"		, "262"     , 9     , "011"	},
	{"Romania"                      ,"RO"		, "40"      , 9     , "00"	},
	{"Russian Federation"           ,"RU"		, "7"       , 10	, "8"	},
	{"Rwanda"                       ,"RW"		, "250"     , 9		, "00"  },
	{"Saint Helena"                 ,"SH"		, "290"     , 4		, "00"  },
	{"Saint Kitts and Nevis"		,"KN"		, "1"       , 10	, "011"	},
	{"Saint Lucia"                  ,"LC"		, "1"       , 10	, "011" },
	{"Saint Pierre and Miquelon"    ,"PM"		, "508"     , 6		, "00"  },
	{"Saint Vincent and the Grenadines","VC"	, "1"       , 10	, "011" },
	{"Samoa"                        ,"WS"		, "685"     , 7     , "0"	},
	{"San Marino"                   ,"SM"		, "378"     , 10	, "00"  },
712
	{"Sao Tome and Principe"        ,"ST"		, "239"     , 7		, "00"  },
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	{"Saudi Arabia"                 ,"SA"		, "966"     , 9		, "00"	},
	{"Senegal"                      ,"SN"		, "221"     , 9	    , "00"  },
	{"Serbia"                       ,"RS"		, "381"     , 9     , "00"  },
	{"Seychelles"                   ,"SC"       , "248"     , 7		, "00"  },
	{"Sierra Leone"                 ,"SL"		, "232"     , 8     , "00"	},
	{"Singapore"                    ,"SG"		, "65"      , 8     , "001" },
	{"Slovakia"                     ,"SK"		, "421"     , 9     , "00"  },
	{"Slovenia"                     ,"SI"		, "386"     , 8     , "00"	},
	{"Solomon Islands"              ,"SB"		, "677"     , 7     , "00"	},
	{"Somalia"                      ,"SO"		, "252"     , 8		, "00"	},
	{"South Africa"                 ,"ZA"		, "27"      , 9		, "00"  },
	{"Spain"                        ,"ES"		, "34"      , 9		, "00"  },
	{"Sri Lanka"                    ,"LK"		, "94"      , 9     , "00"	},
	{"Sudan"                        ,"SD"		, "249"     , 9		, "00"  },
	{"Suriname"                     ,"SR"		, "597"     , 7		, "00"  },
	{"Swaziland"                    ,"SZ"		, "268"     , 8     , "00"  },
	{"Sweden"                       ,"SE"		, "1"       , 9     , "00"	},
	{"Switzerland"                  ,"XK"		, "41"      , 9		, "00"	},
	{"Syria"                        ,"SY"		, "963"     , 9		, "00"  },
	{"Taiwan"                       ,"TW"		, "886"     , 9		, "810"	},
	{"Tajikistan"                   ,"TJ"		, "992"     , 9	    , "002" },
	{"Tanzania"                     ,"TZ"		, "255"     , 9     , "000" },
	{"Thailand"                     ,"TH"       , "66"      , 9		, "001" },
	{"Togo"                         ,"TG"		, "228"     , 8     , "00"	},
	{"Tokelau"                      ,"TK"		, "690"     , 4     , "00"  },
	{"Tonga"                        ,"TO"		, "676"     , 5     , "00"  },
	{"Trinidad and Tobago"			,"TT"		, "1"       , 10    , "011"	},
	{"Tunisia"                      ,"TN"		, "216"     , 8     , "00"	},
	{"Turkey"                       ,"TR"		, "90"      , 10	, "00"	},
	{"Turkmenistan"                 ,"TM"		, "993"     , 8		, "00"  },
	{"Turks and Caicos Islands"	    ,"TC"		, "1"       , 7		, "0"   },
	{"Tuvalu"                       ,"TV"		, "688"     , 5     , "00"	},
	{"Uganda"                       ,"UG"		, "256"     , 9     , "000" },
	{"Ukraine"                      ,"UA"		, "380"     , 9		, "00"  },
	{"United Arab Emirates"	        ,"AE"		, "971"     , 9     , "00"  },
748 749
	{"United Kingdom"               ,"GB"		, "44"      , 10	, "00"	},
/*	{"United Kingdom"               ,"UK"		, "44"      , 10	, "00"	},*/
750 751 752 753 754 755 756 757 758 759
	{"United States"                ,"US"		, "1"       , 10	, "011" },
	{"Uruguay"                      ,"UY"		, "598"     , 8		, "00"  },
	{"Uzbekistan"                   ,"UZ"		, "998"     , 9		, "8"	},
	{"Vanuatu"                      ,"VU"		, "678"     , 7	    , "00"  },
	{"Venezuela"                    ,"VE"		, "58"      , 10	, "00"  },
	{"Vietnam"                      ,"VN"		, "84"      , 9     , "00"  },
	{"Wallis and Futuna"	        ,"WF"		, "681"     , 5		, "00"  },
	{"Yemen"                        ,"YE"		, "967"     , 9     , "00"  },
	{"Zambia"                       ,"ZM"		, "260"     , 9     , "00"	},
	{"Zimbabwe"                     ,"ZW"		, "263"     , 9     , "00"  },
jehan's avatar
jehan committed
760
	{NULL                           ,NULL       ,  ""       , 0     , NULL	}
761
};
762 763
static dial_plan_t most_common_dialplan={ "generic" ,"", "", 10, "00"};

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
int linphone_dial_plan_lookup_ccc_from_e164(const char* e164) {
	dial_plan_t* dial_plan;
	dial_plan_t* elected_dial_plan=NULL;
	unsigned int found;
	unsigned int i=0;
	if (e164[1]=='1') {
		/*USA case*/
		return 1;
	}
	do {
		found=0;
		i++;
		for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
			if (strncmp(dial_plan->ccc,&e164[1],i) == 0) {
				elected_dial_plan=dial_plan;
				found++;
			}
		}
782
	} while ((found>1 || found==0) && i < sizeof(dial_plan->ccc));
783 784 785 786 787 788 789
	if (found==1) {
		return atoi(elected_dial_plan->ccc);
	} else {
		return -1; /*not found */
	}

}
790 791 792 793 794 795 796 797 798
int linphone_dial_plan_lookup_ccc_from_iso(const char* iso) {
	dial_plan_t* dial_plan;
	for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
		if (strcmp(iso, dial_plan->iso_country_code)==0) {
			return atoi(dial_plan->ccc);
		}
	}
	return -1;
}
799

800
static bool_t lookup_dial_plan_by_ccc(const char *ccc, dial_plan_t *plan){
801 802 803 804
	int i;
	for(i=0;dial_plans[i].country!=NULL;++i){
		if (strcmp(ccc,dial_plans[i].ccc)==0){
			*plan=dial_plans[i];
805
			return TRUE;
806 807 808 809 810
		}
	}
	/*else return a generic "most common" dial plan*/
	*plan=most_common_dialplan;
	strcpy(plan->ccc,ccc);
811
	return FALSE;
812
}
813

814
bool_t linphone_proxy_config_is_phone_number(LinphoneProxyConfig *proxy, const char *username){
815 816
	const char *p;
	for(p=username;*p!='\0';++p){
817
		if (isdigit(*p) ||
818 819 820 821
			*p==' ' ||
			*p=='.' ||
			*p=='-' ||
			*p==')' ||
822 823
			*p=='(' ||
			*p=='/' ||
824
			*p=='+' ||
825 826
			(unsigned char)*p==0xca || (unsigned char)*p==0xc2 || (unsigned char)*p==0xa0 // non-breakable space (iOS uses it to format contacts phone number)
			) {
827
			continue;
828
		}
829
		return FALSE;
830 831 832 833
	}
	return TRUE;
}

834
//remove anything but [0-9] and +
835 836 837 838 839 840 841 842 843 844 845 846 847
static char *flatten_number(const char *number){
	char *result=ms_malloc0(strlen(number)+1);
	char *w=result;
	const char *r;
	for(r=number;*r!='\0';++r){
		if (*r=='+' || isdigit(*r)){
			*w++=*r;
		}
	}
	*w++='\0';
	return result;
}

848
static char* replace_plus_with_icp(char *phone, const char* icp){
849 850 851
	return (icp && phone[0]=='+') ? ms_strdup_printf("%s%s", icp, phone+1) : ms_strdup(phone);
}

852
static char* replace_icp_with_plus(char *phone, const char *icp){
853 854
	return (strstr(phone, icp) == phone) ?  ms_strdup_printf("+%s", phone+strlen(icp)) : ms_strdup(phone);
}
855

856
bool_t linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
857 858
	char * normalized_phone = linphone_proxy_config_normalize_phone_number(proxy, username);
	const char * output = normalized_phone ? normalized_phone : username;
859
	memset(result, 0, result_len);
860 861 862
	memcpy(result, output, MIN(strlen(output) + 1, result_len));
	ms_free(normalized_phone);
	return output != username;
863 864
}

865 866 867 868 869 870
char* linphone_proxy_config_normalize_phone_number(LinphoneProxyConfig *proxy, const char *username) {
	LinphoneProxyConfig *tmpproxy = proxy ? proxy : linphone_proxy_config_new();
	char* result = NULL;
	if (linphone_proxy_config_is_phone_number(tmpproxy, username)){
		dial_plan_t dialplan = {0};
		char * flatten=flatten_number(username);
871
		ms_debug("Flattened number is '%s' for '%s'",flatten, username);
872 873 874 875

		/*if proxy has a dial prefix, modify phonenumber accordingly*/
		if (tmpproxy->dial_prefix!=NULL && tmpproxy->dial_prefix[0]!='\0'){
			lookup_dial_plan_by_ccc(tmpproxy->dial_prefix,&dialplan);
876
			ms_debug("Using dial plan '%s'",dialplan.country);
877 878
			/* the number already starts with + or international prefix*/
			if (flatten[0]=='+'||strstr(flatten,dialplan.icp)==flatten){
879
				ms_debug("Prefix already present.");
880
				if (tmpproxy->dial_escape_plus) {
881
					result = replace_plus_with_icp(flatten,dialplan.icp);
882
				} else {
883
					result = replace_icp_with_plus(flatten,dialplan.icp);
884 885 886
				}
			}else{
				/*0. keep at most national number significant digits */
887
				char* flatten_start = flatten + MAX(0, (int)strlen(flatten) - (int)dialplan.nnl);
888
				ms_debug("Prefix not present. Keeping at most %d digits: %s", dialplan.nnl, flatten_start);
889

890 891 892 893 894 895 896
				/*1. First prepend international calling prefix or +*/
				/*2. Second add prefix*/
				/*3. Finally add user digits */
				result = ms_strdup_printf("%s%s%s"
											, tmpproxy->dial_escape_plus ? dialplan.icp : "+"
											, dialplan.ccc
											, flatten_start);
897
				ms_debug("Prepended prefix resulted in %s", result);
898 899 900 901 902 903 904 905
			}
		}
		if (result==NULL) {
			result = flatten;
		} else {
			ms_free(flatten);
		}
	}
906
	if (proxy==NULL) linphone_proxy_config_destroy(tmpproxy);
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
	return result;
}

static LinphoneAddress* _linphone_core_destroy_addr_if_not_sip( LinphoneAddress* addr ){
	if( linphone_address_is_sip(addr) ) {
		return addr;
	} else {
		linphone_address_destroy(addr);
		return NULL;
	}
}

LinphoneAddress* linphone_proxy_config_normalize_sip_uri(LinphoneProxyConfig *proxy, const char *username) {
	enum_lookup_res_t *enumres=NULL;
	char *enum_domain=NULL;
	char *tmpurl;
	LinphoneAddress *uri;

925
	if (!username || *username=='\0') return NULL;
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

	if (is_enum(username,&enum_domain)){
		if (proxy) {
			linphone_core_notify_display_status(proxy->lc,_("Looking for telephone number destination..."));
		}
		if (enum_lookup(enum_domain,&enumres)<0){
			if (proxy) {
				linphone_core_notify_display_status(proxy->lc,_("Could not resolve this number."));
			}
			ms_free(enum_domain);
			return NULL;
		}
		ms_free(enum_domain);
		tmpurl=enumres->sip_address[0];
		uri=linphone_address_new(tmpurl);
		enum_lookup_res_free(enumres);
		return _linphone_core_destroy_addr_if_not_sip(uri);
	}
	/* check if we have a "sip:" or a "sips:" */
	if ( (strstr(username,"sip:")==NULL) && (strstr(username,"sips:")==NULL) ){
		/* this doesn't look like a true sip uri */
		if (strchr(username,'@')!=NULL){
			/* seems like sip: is missing !*/
			tmpurl=ms_strdup_printf("sip:%s",username);
			uri=linphone_address_new(tmpurl);
			ms_free(tmpurl);
			if (uri){
				return _linphone_core_destroy_addr_if_not_sip(uri);
			}
		}

		if (proxy!=NULL){
958
			/* append the proxy domain suffix but remove any custom parameters/headers */
959
			LinphoneAddress *uri=linphone_address_clone(linphone_proxy_config_get_identity_address(proxy));
960
			linphone_address_clean(uri);
961 962
			if (uri==NULL){
				return NULL;
963
			} else {
964
				char* normalized_phone = linphone_proxy_config_normalize_phone_number(proxy,username);
965
				linphone_address_set_display_name(uri,NULL);
966 967
				linphone_address_set_username(uri,normalized_phone ? normalized_phone : username);
				ms_free(normalized_phone);
968
				return _linphone_core_destroy_addr_if_not_sip(uri);
969
			}
970 971 972
		} else {
			return NULL;
		}
973 974 975 976 977 978 979 980 981
	}
	uri=linphone_address_new(username);
	if (uri!=NULL){
		return _linphone_core_destroy_addr_if_not_sip(uri);
	}

	return NULL;
}

smorlat's avatar
smorlat committed
982 983 984
/**
 * Commits modification made to the proxy configuration.
**/
985
int linphone_proxy_config_done(LinphoneProxyConfig *cfg)
smorlat's avatar
smorlat committed
986
{
987 988
	LinphoneProxyConfigAddressComparisonResult res;

989
	if (!linphone_proxy_config_check(cfg->lc,cfg))
990 991 992
		return -1;

	/*check if server address as changed*/
993
	res = linphone_proxy_config_is_server_config_changed(cfg);
994
	if (res != LinphoneProxyConfigAddressEqual) {
995
		/* server config has changed, need to unregister from previous first*/
996
		if (cfg->op) {
997
			if (res == LinphoneProxyConfigAddressDifferent) {
998
				_linphone_proxy_config_unregister(cfg);
999
			}
1000 1001 1002
			sal_op_set_user_pointer(cfg->op,NULL); /*we don't want to receive status for this un register*/
			sal_op_unref(cfg->op); /*but we keep refresher to handle authentication if needed*/
			cfg->op=NULL;
1003 1004
		}
	}
1005 1006
	cfg->commit=TRUE;
	linphone_proxy_config_write_all_to_config_file(cfg->lc);
aymeric's avatar
aymeric committed
1007 1008 1009
	return 0;
}

1010 1011 1012 1013
const char* linphone_proxy_config_get_realm(const LinphoneProxyConfig *cfg)
{
	return cfg?cfg->realm:NULL;
}
aymeric's avatar
aymeric committed
1014 1015 1016 1017 1018
void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
{
	if (cfg->realm!=NULL) {
		ms_free(cfg->realm);
	}
1019
	cfg->realm=ms_strdup(realm);
aymeric's avatar
aymeric committed
1020 1021
}

1022
int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy, LinphonePresenceModel *presence){
1023
	int err=0;
1024

1025 1026
	if (proxy->state==LinphoneRegistrationOk || proxy->state==LinphoneRegistrationCleared){
		if (proxy->publish_op==NULL){
1027
			const LinphoneAddress *to=linphone_proxy_config_get_identity_address(proxy);
1028
			proxy->publish_op=sal_op_new(proxy->lc->sal);
1029 1030 1031 1032

			linphone_configure_op(proxy->lc, proxy->publish_op,
				to, NULL, FALSE);

1033
			if (lp_config_get_int(proxy->lc->config,"sip","publish_msg_with_contact",0)){
1034
				sal_op_set_contact_address(proxy->publish_op,linphone_proxy_config_get_identity_address(proxy));
1035 1036
			}
		}
1037 1038 1039 1040 1041
		err=sal_publish_presence(proxy->publish_op
									,NULL
									,NULL
									,linphone_proxy_config_get_publish_expires(proxy)
									,(SalPresenceModel *)presence);
1042
	}else proxy->send_publish=TRUE; /*otherwise do not send publish if registration is in progress, this will be done later*/
1043
	return err;
aymeric's avatar
aymeric committed
1044 1045
}

1046 1047
const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *cfg){
	return cfg->reg_route;
1048 1049
}

1050 1051
const LinphoneAddress *linphone_proxy_config_get_identity_address(const LinphoneProxyConfig *cfg){
	return cfg->identity_address;
1052 1053
}

1054 1055
const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *cfg){
	return cfg->reg_identity;
1056 1057
}

1058 1059
bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *cfg){
	return cfg->publish;
1060 1061
}

1062 1063
const char *linphone_proxy_config_get_server_addr(const LinphoneProxyConfig *cfg){
	return cfg->reg_proxy;
1064 1065 1066
}

/**
1067
 * @return the duration of registration.
1068
**/
1069 1070
int linphone_proxy_config_get_expires(const LinphoneProxyConfig *cfg){
	return cfg->expires;
1071 1072
}

1073 1074 1075 1076 1077 1078 1079 1080
bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *cfg){
	return cfg->reg_sendregister;
}

void linphone_proxy_config_set_contact_parameters(LinphoneProxyConfig *cfg, const char *contact_params){
	if (cfg->contact_params) {
		ms_free(cfg->contact_params);
		cfg->contact_params=NULL;
1081 1082
	}
	if (contact_params){
1083
		cfg->contact_params=ms_strdup(contact_params);
1084 1085 1086
	}
}

1087 1088 1089 1090
void linphone_proxy_config_set_contact_uri_parameters(LinphoneProxyConfig *cfg, const char *contact_uri_params){
	if (cfg->contact_uri_params) {
		ms_free(cfg->contact_uri_params);
		cfg->contact_uri_params=NULL;
1091 1092
	}
	if (contact_uri_params){
1093
		cfg->contact_uri_params=ms_strdup(contact_uri_params);
1094 1095 1096
	}
}

1097 1098
const char *linphone_proxy_config_get_contact_parameters(const LinphoneProxyConfig *cfg){
	return cfg->contact_params;
1099 1100
}

1101 1102
const char *linphone_proxy_config_get_contact_uri_parameters(const LinphoneProxyConfig *cfg){
	return cfg->contact_uri_params;
1103 1104
}

1105 1106
struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *cfg){
	return cfg->lc;
1107
}
smorlat's avatar
smorlat committed
1108

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
const char *linphone_proxy_config_get_custom_header(LinphoneProxyConfig *cfg, const char *header_name){
	const SalCustomHeader *ch;
	if (!cfg->op) return NULL;
	ch = sal_op_get_recv_custom_header(cfg->op);
	return sal_custom_header_find(ch, header_name);
}

void linphone_proxy_config_set_custom_header(LinphoneProxyConfig *cfg, const char *header_name, const char *header_value){
	cfg->sent_headers=sal_custom_header_append(cfg->sent_headers, header_name, header_value);
}

aymeric's avatar
aymeric committed
1120
int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1121 1122 1123
	if (!linphone_proxy_config_check(lc,cfg)) {
		return -1;
	}
1124 1125 1126 1127
	if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
		ms_warning("ProxyConfig already entered, ignored.");
		return 0;
	}
1128
	lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)linphone_proxy_config_ref(cfg));
aymeric's avatar
aymeric committed
1129 1130 1131 1132 1133
	linphone_proxy_config_apply(cfg,lc);
	return 0;
}

void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1134 1135
	/* check this proxy config is in the list before doing more*/
	if (ms_list_find(lc->sip_conf.proxies,cfg)==NULL){
1136
		ms_error("linphone_core_remove_proxy_config: LinphoneProxyConfig [%p] is not known by LinphoneCore (programming error?)",cfg);
1137 1138
		return;
	}
1139
	lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,cfg);
aymeric's avatar
aymeric committed
1140
	/* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
1141
	lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,cfg);
1142 1143 1144 1145 1146

	if (lc->default_proxy==cfg){
		lc->default_proxy=NULL;
	}

1147
	cfg->deletion_date=ms_time(NULL);
1148
	if (cfg->state==LinphoneRegistrationOk){
1149
		/* UNREGISTER */
1150
		linphone_proxy_config_edit(cfg);
1151 1152
		linphone_proxy_config_enable_register(cfg,FALSE);
		linphone_proxy_config_done(cfg);
1153
		linphone_proxy_config_update(cfg);
1154 1155
	} else if (cfg->state != LinphoneRegistrationNone) {
		linphone_proxy_config_set_state(cfg, LinphoneRegistrationNone,"Registration disabled");
1156
	}
1157
	linphone_proxy_config_write_all_to_config_file(lc);
aymeric's avatar
aymeric committed
1158
}
1159

1160
void linphone_core_clear_proxy_config(LinphoneCore *lc){
1161 1162
	MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
	MSList* copy=list;
1163
	for(;list!=NULL;list=list->next){
1164 1165
		linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
	}
1166
	ms_list_free(copy);
1167
	linphone_proxy_config_write_all_to_config_file(lc);
1168
}
1169

1170
int linphone_core_get_default_proxy_config_index(LinphoneCore *lc) {
1171 1172 1173 1174 1175 1176 1177 1178
	int pos = -1;
	if (lc->default_proxy != NULL) {
		pos = ms_list_position(lc->sip_conf.proxies, ms_list_find(lc->sip_conf.proxies, (void *)lc->default_proxy));
	}
	return pos;
}

void linphone_core_set_default_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *config){
aymeric's avatar
aymeric committed
1179 1180 1181 1182 1183 1184 1185 1186 1187
	/* check if this proxy is in our list */
	if (config!=NULL){
		if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
			ms_warning("Bad proxy address: it is not in the list !");
			lc->default_proxy=NULL;
			return ;
		}
	}
	lc->default_proxy=config;
1188
	if (linphone_core_ready(lc))
1189
		lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy_config_index(lc));
1190
}
aymeric's avatar
aymeric committed
1191 1192 1193 1194 1195 1196 1197 1198

void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
	if (index<0) linphone_core_set_default_proxy(lc,NULL);
	else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
}

int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
	if (config!=NULL) *config=lc->default_proxy;
1199 1200 1201 1202 1203
	return linphone_core_get_default_proxy_config_index(lc);
}

LinphoneProxyConfig * linphone_core_get_default_proxy_config(LinphoneCore *lc) {
	return lc->default_proxy;
aymeric's avatar
aymeric committed
1204 1205 1206 1207 1208 1209
}

const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
	return lc->sip_conf.proxies;
}

1210
void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *cfg, int index)
aymeric's avatar
aymeric committed
1211 1212 1213 1214 1215
{
	char key[50];

	sprintf(key,"proxy_%i",index);
	lp_config_clean_section(config,key);
1216
	if (cfg==NULL){
aymeric's avatar
aymeric committed
1217 1218
		return;
	}
1219 1220
	if (cfg->type!=NULL){
		lp_config_set_string(config,key,"type",cfg->type);
smorlat's avatar
smorlat committed
1221
	}
1222 1223
	if (cfg->reg_proxy!=NULL){
		lp_config_set_string(config,key,"reg_proxy",cfg->reg_proxy);
aymeric's avatar
aymeric committed
1224
	}
Gautier Pelloux-Prayer's avatar