proxy.c 23.9 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 20 21
/*
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.
 */
 
#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"
aymeric's avatar
aymeric committed
26

27 28 29 30

#include <ctype.h>


smorlat's avatar
smorlat committed
31 32 33 34 35 36 37 38
void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
	MSList *elem;
	int i;
	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);
	}
}
aymeric's avatar
aymeric committed
39 40 41 42 43 44

void linphone_proxy_config_init(LinphoneProxyConfig *obj){
	memset(obj,0,sizeof(LinphoneProxyConfig));
	obj->expires=3600;
}

smorlat's avatar
smorlat committed
45 46 47 48 49 50 51 52
/**
 * @addtogroup proxies
 * @{
**/

/**
 * Creates an empty proxy config.
**/
aymeric's avatar
aymeric committed
53 54 55 56 57 58 59
LinphoneProxyConfig *linphone_proxy_config_new(){
	LinphoneProxyConfig *obj=NULL;
	obj=ms_new(LinphoneProxyConfig,1);
	linphone_proxy_config_init(obj);
	return obj;
}

smorlat's avatar
smorlat committed
60 61 62 63 64 65
/**
 * Destroys a proxy config.
 * 
 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
 * linphone_core_remove_proxy_config() must not be freed.
**/
aymeric's avatar
aymeric committed
66 67 68 69
void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
	if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
	if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
	if (obj->reg_route!=NULL) ms_free(obj->reg_route);
smorlat's avatar
smorlat committed
70
	if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
smorlat's avatar
smorlat committed
71
	if (obj->realm!=NULL) ms_free(obj->realm);
smorlat's avatar
smorlat committed
72
	if (obj->type!=NULL) ms_free(obj->type);
73
	if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
74
	if (obj->op) sal_op_release(obj->op);
Simon Morlat's avatar
Simon Morlat committed
75
	if (obj->publish_op) sal_op_release(obj->publish_op);
aymeric's avatar
aymeric committed
76 77
}

smorlat's avatar
smorlat committed
78 79 80
/**
 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
**/
smorlat's avatar
smorlat committed
81 82 83 84
bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
	return obj->registered;
}

smorlat's avatar
smorlat committed
85 86 87 88 89 90 91 92
/**
 * Sets the proxy address
 *
 * Examples of valid sip proxy address are:
 * - IP address: sip:87.98.157.38
 * - IP address with port: sip:87.98.157.38:5062
 * - hostnames : sip:sip.example.net
**/
aymeric's avatar
aymeric committed
93
int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
94 95
	LinphoneAddress *addr=NULL;
	char *modified=NULL;
96
	
aymeric's avatar
aymeric committed
97 98
	if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
	obj->reg_proxy=NULL;
99
	
aymeric's avatar
aymeric committed
100
	if (server_addr!=NULL && strlen(server_addr)>0){
101 102 103 104
		if (strstr(server_addr,"sip:")==NULL){
			modified=ms_strdup_printf("sip:%s",server_addr);
			addr=linphone_address_new(modified);
			ms_free(modified);
105
		}
106 107
		if (addr==NULL)
			addr=linphone_address_new(server_addr);
108
		if (addr){
Simon Morlat's avatar
Simon Morlat committed
109
			obj->reg_proxy=linphone_address_as_string_uri_only(addr);
110
			linphone_address_destroy(addr);
aymeric's avatar
aymeric committed
111 112
		}else{
			ms_warning("Could not parse %s",server_addr);
113
			return -1;
aymeric's avatar
aymeric committed
114 115 116 117 118
		}
	}
	return 0;
}

smorlat's avatar
smorlat committed
119 120 121 122 123 124 125 126 127
/**
 * Sets the user identity as a SIP address.
 *
 * This identity is normally formed with display name, username and domain, such 
 * as:
 * Alice <sip:alice@example.net>
 * The REGISTER messages will have from and to set to this identity.
 *
**/
128 129
int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
	LinphoneAddress *addr;
aymeric's avatar
aymeric committed
130
	if (identity!=NULL && strlen(identity)>0){
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		addr=linphone_address_new(identity);
		if (!addr || linphone_address_get_username(addr)==NULL){
			ms_warning("Invalid sip identity: %s",identity);
			if (addr)
				linphone_address_destroy(addr);
			return -1;
		}else{
			if (obj->reg_identity!=NULL) {
				ms_free(obj->reg_identity);
				obj->reg_identity=NULL;
			}
			obj->reg_identity=ms_strdup(identity);
			if (obj->realm){
				ms_free(obj->realm);
			}
			obj->realm=ms_strdup(linphone_address_get_domain(addr));
			linphone_address_destroy(addr);
			return 0;
aymeric's avatar
aymeric committed
149 150
		}
	}
151
	return -1;
smorlat's avatar
smorlat committed
152 153 154 155
}

const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
	return cfg->realm;
aymeric's avatar
aymeric committed
156 157
}

smorlat's avatar
smorlat committed
158 159 160 161 162
/**
 * Sets a SIP route.
 * When a route is set, all outgoing calls will go to the route's destination if this proxy
 * is the default one (see linphone_core_set_default_proxy() ).
**/
163
int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
aymeric's avatar
aymeric committed
164
{
165 166
	if (obj->reg_route!=NULL){
		ms_free(obj->reg_route);
aymeric's avatar
aymeric committed
167 168
		obj->reg_route=NULL;
	}
Simon Morlat's avatar
Simon Morlat committed
169 170 171 172 173 174
	if (route!=NULL){
		/*try to prepend 'sip:' */
		if (strstr(route,"sip:")==NULL){
			obj->reg_route=ms_strdup_printf("sip:%s",route);
		}else obj->reg_route=ms_strdup(route);
	}
175
	return 0;
aymeric's avatar
aymeric committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
	if (obj->reg_proxy==NULL){
		if (lc->vtable.display_warning)
			lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
						" followed by a hostname."));
		return FALSE;
	}
	if (obj->reg_identity==NULL){
		if (lc->vtable.display_warning)
			lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
					"sip:username@proxydomain, such as sip:alice@example.net"));
		return FALSE;
	}
	return TRUE;
}

smorlat's avatar
smorlat committed
194 195 196
/**
 * Indicates whether a REGISTER request must be sent to the proxy.
**/
aymeric's avatar
aymeric committed
197 198 199 200
void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
	obj->reg_sendregister=val;
}

smorlat's avatar
smorlat committed
201 202 203
/**
 * Sets the registration expiration time in seconds.
**/
aymeric's avatar
aymeric committed
204 205 206 207 208 209 210 211 212
void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
	if (val<=0) val=600;
	obj->expires=val;
}

void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
	obj->publish=val;
}

smorlat's avatar
smorlat committed
213 214 215 216 217 218 219 220 221
/**
 * Starts editing a proxy configuration.
 *
 * Because proxy configuration must be consistent, applications MUST
 * call linphone_proxy_config_edit() before doing any attempts to modify
 * proxy configuration (such as identity, proxy address and so on).
 * Once the modifications are done, then the application must call
 * linphone_proxy_config_done() to commit the changes.
**/
aymeric's avatar
aymeric committed
222 223 224 225
void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
	if (obj->reg_sendregister){
		/* unregister */
		if (obj->registered) {
226
			sal_unregister(obj->op);
aymeric's avatar
aymeric committed
227 228 229 230 231 232 233 234 235 236 237
			obj->registered=FALSE;
		}
	}
}

void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
{
	obj->lc=lc;
	linphone_proxy_config_done(obj);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251
static char *guess_contact_for_register(LinphoneProxyConfig *obj){
	LinphoneAddress *proxy=linphone_address_new(obj->reg_proxy);
	char *ret=NULL;
	const char *host;
	if (proxy==NULL) return NULL;
	host=linphone_address_get_domain (proxy);
	if (host!=NULL){
		LinphoneAddress *contact;
		char localip[LINPHONE_IPADDR_SIZE];
		
		linphone_core_get_local_ip(obj->lc,host,localip);
		contact=linphone_address_new(obj->reg_identity);
		linphone_address_set_domain (contact,localip);
		linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
252 253 254 255 256 257 258
		linphone_address_set_display_name(contact,NULL);
		LCSipTransports tr;
		linphone_core_get_sip_transports(obj->lc,&tr);
		if (tr.udp_port <= 0 && tr.tcp_port>0) {
			sal_address_add_param(contact,"transport","tcp");
		}
		ret=linphone_address_as_string(contact);
259 260 261 262 263 264
		linphone_address_destroy(contact);
	}
	linphone_address_destroy (proxy);
	return ret;
}

smorlat's avatar
smorlat committed
265
static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
aymeric's avatar
aymeric committed
266
	const char *id_str;
267

aymeric's avatar
aymeric committed
268 269 270
	if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
	else id_str=linphone_core_get_primary_contact(obj->lc);
	if (obj->reg_sendregister){
271
		char *contact;
272 273 274
		if (obj->op)
			sal_op_release(obj->op);
		obj->op=sal_op_new(obj->lc->sal);
275 276 277
		contact=guess_contact_for_register(obj);
		sal_op_set_contact(obj->op,contact);
		ms_free(contact);
Simon Morlat's avatar
Simon Morlat committed
278
		sal_op_set_user_pointer(obj->op,obj);
279 280
		if (sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)==0) {
			linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress,"Registration in progress");
281
		} else {
282
			linphone_proxy_config_set_state(obj,LinphoneRegistrationFailed,"Registration failed");
283
		}
aymeric's avatar
aymeric committed
284
	}
smorlat's avatar
smorlat committed
285 286
}

287 288 289 290 291 292 293 294 295 296 297

/**
 * Sets a dialing prefix to be automatically prepended when inviting a number with 
 * #linphone_core_invite.
 *
**/
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;
	}
298
	if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
}

/**
 * Returns dialing prefix.
 *
 * 
**/
const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
	return cfg->dial_prefix;
}

/**
 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
 * #linphone_core_invite ).
 *
**/
void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
	cfg->dial_escape_plus=val;
}

/**
 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
 * #linphone_core_invite ).
 *
**/
bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
	return cfg->dial_escape_plus;
}


static bool_t is_a_phone_number(const char *username){
	const char *p;
	for(p=username;*p!='\0';++p){
		if (isdigit(*p) || 
		    *p==' ' ||
		    *p=='-' ||
		    *p==')' ||
			*p=='(' ||
			*p=='/' ||
			*p=='+') continue;
		else return FALSE;
	}
	return TRUE;
}

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

static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
	int i=0;
	
	if (escape_plus && src[0]=='+' && destlen>2){
		dest[0]='0';
		dest[1]='0';
		src++;
		i=2;
	}
	
Simon Morlat's avatar
Simon Morlat committed
367
	for(;(i<destlen-1) && *src!='\0';++i){
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		dest[i]=*src;
		src++;
	}
	dest[i]='\0';
}


static char *append_prefix(const char *number, const char *prefix){
	char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
	strcpy(res,prefix);
	return strcat(res,number);
}

int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
	char *flatten;
	int numlen;
	if (is_a_phone_number(username)){
		flatten=flatten_number(username);
		ms_message("Flattened number is '%s'",flatten);
		numlen=strlen(flatten);
Simon Morlat's avatar
Simon Morlat committed
388
		if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
389 390 391 392 393
			ms_message("No need to add a prefix");
			/* prefix is already there */
			copy_result(flatten,result,result_len,proxy->dial_escape_plus);
			ms_free(flatten);
			return 0;
Simon Morlat's avatar
Simon Morlat committed
394
		}else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
			char *prefixed;
			int skipped=0;
			ms_message("Need to prefix with %s",proxy->dial_prefix);
			if (numlen==10){
				/*remove initial number before prepending prefix*/
				skipped=1;
			}
			prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
			ms_free(flatten);
			copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
			ms_free(prefixed);
		}
	}else strncpy(result,username,result_len);
	return 0;
}

smorlat's avatar
smorlat committed
411 412 413
/**
 * Commits modification made to the proxy configuration.
**/
smorlat's avatar
smorlat committed
414 415 416
int linphone_proxy_config_done(LinphoneProxyConfig *obj)
{
	if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
smorlat's avatar
smorlat committed
417
	obj->commit=TRUE;
smorlat's avatar
smorlat committed
418
	linphone_proxy_config_write_all_to_config_file(obj->lc);
aymeric's avatar
aymeric committed
419 420 421 422 423 424 425 426 427 428 429 430 431
	return 0;
}

void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
{
	if (cfg->realm!=NULL) {
		ms_free(cfg->realm);
		cfg->realm=NULL;
	}
	if (realm!=NULL) cfg->realm=ms_strdup(realm);
}

int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
432 433 434 435 436
			       LinphoneOnlineStatus presence_mode){
	int err;
	SalOp *op=sal_op_new(proxy->lc->sal);
	err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
	    linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
Simon Morlat's avatar
Simon Morlat committed
437 438 439
	if (proxy->publish_op!=NULL)
		sal_op_release(proxy->publish_op);
	proxy->publish_op=op;
440
	return err;
aymeric's avatar
aymeric committed
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
/**
 * Returns the route set for this proxy configuration.
**/
const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
	return obj->reg_route;
}

/**
 * Returns the SIP identity that belongs to this proxy configuration.
 *
 * The SIP identity is a SIP address (Display Name <sip:username@domain> )
**/
const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
	return obj->reg_identity;
}

/**
 * Returns TRUE if PUBLISH request is enabled for this proxy.
**/
bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
	return obj->publish;
}

/**
 * Returns the proxy's SIP address.
**/
const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
	return obj->reg_proxy;
}

/**
 * Returns the duration of registration.
**/
int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
	return obj->expires;
}

/**
 * Returns TRUE if registration to the proxy is enabled.
**/
bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
	return obj->reg_sendregister;
}

struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
	return obj->lc;
}
smorlat's avatar
smorlat committed
490 491 492 493 494

/**
 * Add a proxy configuration.
 * This will start registration on the proxy, if registration is enabled.
**/
aymeric's avatar
aymeric committed
495 496
int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
	if (!linphone_proxy_config_check(lc,cfg)) return -1;
497 498 499 500
	if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
		ms_warning("ProxyConfig already entered, ignored.");
		return 0;
	}
aymeric's avatar
aymeric committed
501 502 503 504 505
	lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
	linphone_proxy_config_apply(cfg,lc);
	return 0;
}

smorlat's avatar
smorlat committed
506 507 508 509 510 511
/**
 * Removes a proxy configuration.
 *
 * LinphoneCore will then automatically unregister and place the proxy configuration
 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
**/
aymeric's avatar
aymeric committed
512 513 514 515
void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
	lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
	/* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
	lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
516
	cfg->deletion_date=ms_time(NULL);
aymeric's avatar
aymeric committed
517 518 519 520 521 522
	/* this will unREGISTER */
	linphone_proxy_config_edit(cfg);
	if (lc->default_proxy==cfg){
		lc->default_proxy=NULL;
	}
}
smorlat's avatar
smorlat committed
523 524 525 526 527 528
/**
 * Erase all proxies from config.
 *
 * @ingroup proxy
**/
void linphone_core_clear_proxy_config(LinphoneCore *lc){
smorlat's avatar
smorlat committed
529
	MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
smorlat's avatar
smorlat committed
530
	for(;list!=NULL;list=list->next){
smorlat's avatar
smorlat committed
531 532 533
		linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
	}
	ms_list_free(list);
smorlat's avatar
smorlat committed
534
}
smorlat's avatar
smorlat committed
535 536 537 538 539 540 541
/**
 * Sets the default proxy.
 *
 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
 * Toggling it as default will make LinphoneCore use the identity associated with
 * the proxy configuration in all incoming and outgoing calls.
**/
aymeric's avatar
aymeric committed
542 543 544 545 546 547 548 549 550 551 552
void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
	/* 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;
	
smorlat's avatar
smorlat committed
553
}	
aymeric's avatar
aymeric committed
554 555 556 557 558 559

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

smorlat's avatar
smorlat committed
560 561 562
/**
 * Returns the default proxy configuration, that is the one used to determine the current identity.
**/
aymeric's avatar
aymeric committed
563 564 565 566 567 568 569 570 571
int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
	int pos=-1;
	if (config!=NULL) *config=lc->default_proxy;
	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;
}

smorlat's avatar
smorlat committed
572 573 574
/**
 * Returns an unmodifiable list of entered proxy configurations.
**/
aymeric's avatar
aymeric committed
575 576 577 578 579 580 581 582 583 584 585 586 587
const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
	return lc->sip_conf.proxies;
}

void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
{
	char key[50];

	sprintf(key,"proxy_%i",index);
	lp_config_clean_section(config,key);
	if (obj==NULL){
		return;
	}
smorlat's avatar
smorlat committed
588 589 590
	if (obj->type!=NULL){
		lp_config_set_string(config,key,"type",obj->type);
	}
aymeric's avatar
aymeric committed
591 592 593 594 595 596 597 598 599 600 601 602
	if (obj->reg_proxy!=NULL){
		lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
	}
	if (obj->reg_route!=NULL){
		lp_config_set_string(config,key,"reg_route",obj->reg_route);
	}
	if (obj->reg_identity!=NULL){
		lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
	}
	lp_config_set_int(config,key,"reg_expires",obj->expires);
	lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
	lp_config_set_int(config,key,"publish",obj->publish);
603 604
	lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
	lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
aymeric's avatar
aymeric committed
605 606
}

smorlat's avatar
smorlat committed
607 608


aymeric's avatar
aymeric committed
609 610 611
LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
{
	const char *tmp;
smorlat's avatar
smorlat committed
612 613
	const char *identity;
	const char *proxy;
aymeric's avatar
aymeric committed
614 615 616 617 618 619 620 621
	LinphoneProxyConfig *cfg;
	char key[50];
	
	sprintf(key,"proxy_%i",index);

	if (!lp_config_has_section(config,key)){
		return NULL;
	}
smorlat's avatar
smorlat committed
622 623 624 625

	cfg=linphone_proxy_config_new();

	identity=lp_config_get_string(config,key,"reg_identity",NULL);	
smorlat's avatar
smorlat committed
626
	proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
smorlat's avatar
smorlat committed
627 628 629
	
	linphone_proxy_config_set_identity(cfg,identity);
	linphone_proxy_config_set_server_addr(cfg,proxy);
smorlat's avatar
smorlat committed
630
	
aymeric's avatar
aymeric committed
631 632
	tmp=lp_config_get_string(config,key,"reg_route",NULL);
	if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
smorlat's avatar
smorlat committed
633

aymeric's avatar
aymeric committed
634 635 636 637
	linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
	linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
	
	linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
638 639 640

	linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
	linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
aymeric's avatar
aymeric committed
641
	
smorlat's avatar
smorlat committed
642 643
	tmp=lp_config_get_string(config,key,"type",NULL);
	if (tmp!=NULL && strlen(tmp)>0) 
smorlat's avatar
smorlat committed
644
		linphone_proxy_config_set_sip_setup(cfg,tmp);
smorlat's avatar
smorlat committed
645

aymeric's avatar
aymeric committed
646 647
	return cfg;
}
smorlat's avatar
smorlat committed
648

smorlat's avatar
smorlat committed
649
static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
smorlat's avatar
smorlat committed
650
	SipSetupContext *ssc;
smorlat's avatar
smorlat committed
651
	SipSetup *ss=sip_setup_lookup(cfg->type);
smorlat's avatar
smorlat committed
652 653
	LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
	unsigned int caps;
smorlat's avatar
smorlat committed
654
	if (!ss) return ;
smorlat's avatar
smorlat committed
655
	ssc=sip_setup_context_new(ss,cfg);
smorlat's avatar
smorlat committed
656
	cfg->ssctx=ssc;
smorlat's avatar
smorlat committed
657 658 659 660
	if (cfg->reg_identity==NULL){
		ms_error("Invalid identity for this proxy configuration.");
		return;
	}
smorlat's avatar
smorlat committed
661 662 663 664 665 666 667
	caps=sip_setup_context_get_capabilities(ssc);
	if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
		if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
			if (lc->vtable.display_warning){
				char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
				lc->vtable.display_warning(lc,tmp);
				ms_free(tmp);
668
			}
smorlat's avatar
smorlat committed
669
			return;
smorlat's avatar
smorlat committed
670 671
		}
	}
smorlat's avatar
smorlat committed
672 673 674 675 676 677 678 679 680
	if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
		char proxy[256];
		if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
			linphone_proxy_config_set_server_addr(cfg,proxy);
		}else{
			ms_error("Could not retrieve proxy uri !");
		}
	}
	
smorlat's avatar
smorlat committed
681 682
}

683 684 685 686 687 688 689 690
SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
	if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
	if (cfg->type!=NULL){
		return sip_setup_lookup(cfg->type);
	}
	return NULL;
}

smorlat's avatar
smorlat committed
691
void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
692
	LinphoneCore *lc=cfg->lc;
smorlat's avatar
smorlat committed
693 694 695 696
	if (cfg->commit){
		if (cfg->type && cfg->ssctx==NULL){
			linphone_proxy_config_activate_sip_setup(cfg);
		}
697 698
		if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
			linphone_proxy_config_register(cfg);
Simon Morlat's avatar
Simon Morlat committed
699 700 701
		if (cfg->publish && cfg->publish_op==NULL){
			linphone_proxy_config_send_publish(cfg,lc->presence_mode);
		}
smorlat's avatar
smorlat committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715
		cfg->commit=FALSE;
	}
}

void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
	if (cfg->type)
		ms_free(cfg->type);
	cfg->type=ms_strdup(type);
	if (linphone_proxy_config_get_addr(cfg)==NULL){
		/*put a placeholder so that the sip setup gets saved into the config */
		linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
	}
}

smorlat's avatar
smorlat committed
716 717 718 719
SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
	return cfg->ssctx;
}

smorlat's avatar
smorlat committed
720 721 722 723
/**
 * @}
**/

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
	LinphoneAccountCreator *obj;
	LinphoneProxyConfig *cfg;
	SipSetup *ss=sip_setup_lookup(type);
	SipSetupContext *ssctx;
	if (!ss){
		return NULL;
	}
	if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
		ms_error("%s cannot manage accounts.");
		return NULL;
	}
	obj=ms_new0(LinphoneAccountCreator,1);
	cfg=linphone_proxy_config_new();
	ssctx=sip_setup_context_new(ss,cfg);
	obj->lc=core;
	obj->ssctx=ssctx;
smorlat's avatar
smorlat committed
741
	set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
742 743 744 745
	cfg->lc=core;
	return obj;
}

smorlat's avatar
smorlat committed
746 747 748 749 750 751 752 753 754 755 756 757
void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
	set_string(&obj->username,username);
}

void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
	set_string(&obj->password,password);
}

void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
	set_string(&obj->domain,domain);
}

smorlat's avatar
smorlat committed
758 759 760 761 762 763 764 765
const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
	return obj->username;
}

const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
	return obj->domain;
}

766
int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
smorlat's avatar
smorlat committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	SipSetupContext *ssctx=obj->ssctx;
	char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
	int err=sip_setup_context_account_exists(ssctx,uri);
	ms_free(uri);
	return err;
}

LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
	SipSetupContext *ssctx=obj->ssctx;
	char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
	int err=sip_setup_context_create_account(ssctx,uri,obj->password);
	ms_free(uri);
	if (err==0) {
		obj->succeeded=TRUE;
		return sip_setup_context_get_proxy_config(ssctx);
	}
	return NULL;
}

void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
	if (obj->username)
		ms_free(obj->username);
	if (obj->password)
		ms_free(obj->password);
	if (obj->domain)
		ms_free(obj->domain);
	if (!obj->succeeded){
		linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
	}
}

jehan's avatar
jehan committed
798 799 800 801 802 803 804 805
void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
	cr->user_data=ud;
}

void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
	return cr->user_data;
}

806 807
void linphone_proxy_config_set_state(LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const char *message){
	LinphoneCore *lc=cfg->lc;
808
	cfg->state=state;
809 810 811 812
	if (lc && lc->vtable.registration_state_changed){
		lc->vtable.registration_state_changed(lc,cfg,state,message);
	}
}
smorlat's avatar
smorlat committed
813

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
LinphoneRegistrationState linphone_proxy_config_get_state(const LinphoneProxyConfig *cfg){
	return cfg->state;
}

 const char *linphone_registration_state_to_string(LinphoneRegistrationState cs){
	 switch(cs){
		case LinphoneRegistrationCleared:
			 return "LinphoneRegistrationCleared";
		break;
		case LinphoneRegistrationNone:
			 return "LinphoneRegistrationNone";
		break;
		case LinphoneRegistrationProgress:
			return "LinphoneRegistrationProgress";
		break;
		case LinphoneRegistrationOk:
			 return "LinphoneRegistrationOk";
		break;
		case LinphoneRegistrationFailed:
			 return "LinphoneRegistrationFailed";
		break;
	 }
	 return NULL;
 }
jehan's avatar
jehan committed
838 839 840 841 842 843
LinphoneError linphone_proxy_config_get_error(const LinphoneProxyConfig *cfg) {
	return cfg->error;
}
void linphone_proxy_config_set_error(LinphoneProxyConfig *cfg,LinphoneError error) {
	cfg->error = error;
}
smorlat's avatar
smorlat committed
844

845