3 Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "linphonecore.h"
30 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
33 for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
34 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
35 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
39 void linphone_proxy_config_init(LinphoneProxyConfig *obj){
40 memset(obj,0,sizeof(LinphoneProxyConfig));
50 * Creates an empty proxy config.
52 LinphoneProxyConfig *linphone_proxy_config_new(){
53 LinphoneProxyConfig *obj=NULL;
54 obj=ms_new(LinphoneProxyConfig,1);
55 linphone_proxy_config_init(obj);
60 * Destroys a proxy config.
62 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
63 * linphone_core_remove_proxy_config() must not be freed.
65 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
66 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
67 if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
68 if (obj->reg_route!=NULL) ms_free(obj->reg_route);
69 if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
70 if (obj->realm!=NULL) ms_free(obj->realm);
71 if (obj->type!=NULL) ms_free(obj->type);
72 if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
73 if (obj->op) sal_op_release(obj->op);
77 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
79 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
80 return obj->registered;
84 * Sets the proxy address
86 * Examples of valid sip proxy address are:
87 * - IP address: sip:87.98.157.38
88 * - IP address with port: sip:87.98.157.38:5062
89 * - hostnames : sip:sip.example.net
91 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
92 LinphoneAddress *addr;
93 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
95 if (server_addr!=NULL && strlen(server_addr)>0){
96 addr=linphone_address_new(server_addr);
98 obj->reg_proxy=ms_strdup(server_addr);
99 linphone_address_destroy(addr);
101 ms_warning("Could not parse %s",server_addr);
109 * Sets the user identity as a SIP address.
111 * This identity is normally formed with display name, username and domain, such
113 * Alice <sip:alice@example.net>
114 * The REGISTER messages will have from and to set to this identity.
117 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
118 LinphoneAddress *addr;
119 if (identity!=NULL && strlen(identity)>0){
120 addr=linphone_address_new(identity);
121 if (!addr || linphone_address_get_username(addr)==NULL){
122 ms_warning("Invalid sip identity: %s",identity);
124 linphone_address_destroy(addr);
127 if (obj->reg_identity!=NULL) {
128 ms_free(obj->reg_identity);
129 obj->reg_identity=NULL;
131 obj->reg_identity=ms_strdup(identity);
135 obj->realm=ms_strdup(linphone_address_get_domain(addr));
136 linphone_address_destroy(addr);
143 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
149 * When a route is set, all outgoing calls will go to the route's destination if this proxy
150 * is the default one (see linphone_core_set_default_proxy() ).
152 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
154 if (obj->reg_route!=NULL){
155 ms_free(obj->reg_route);
158 obj->reg_route=ms_strdup(route);
162 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
163 if (obj->reg_proxy==NULL){
164 if (lc->vtable.display_warning)
165 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
166 " followed by a hostname."));
169 if (obj->reg_identity==NULL){
170 if (lc->vtable.display_warning)
171 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
172 "sip:username@proxydomain, such as sip:alice@example.net"));
179 * Indicates whether a REGISTER request must be sent to the proxy.
181 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
182 obj->reg_sendregister=val;
186 * Sets the registration expiration time in seconds.
188 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
193 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
198 * Starts editing a proxy configuration.
200 * Because proxy configuration must be consistent, applications MUST
201 * call linphone_proxy_config_edit() before doing any attempts to modify
202 * proxy configuration (such as identity, proxy address and so on).
203 * Once the modifications are done, then the application must call
204 * linphone_proxy_config_done() to commit the changes.
206 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
207 if (obj->reg_sendregister){
209 if (obj->registered) {
210 sal_unregister(obj->op);
211 obj->registered=FALSE;
216 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
219 linphone_proxy_config_done(obj);
222 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
224 if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
225 else id_str=linphone_core_get_primary_contact(obj->lc);
226 if (obj->reg_sendregister){
228 sal_op_release(obj->op);
229 obj->op=sal_op_new(obj->lc->sal);
230 sal_op_set_user_pointer(obj->op,obj);
231 sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires);
237 * Sets a dialing prefix to be automatically prepended when inviting a number with
238 * #linphone_core_invite.
241 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
242 if (cfg->dial_prefix!=NULL){
243 ms_free(cfg->dial_prefix);
244 cfg->dial_prefix=NULL;
246 if (prefix) cfg->dial_prefix=ms_strdup(prefix);
250 * Returns dialing prefix.
254 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
255 return cfg->dial_prefix;
259 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
260 * #linphone_core_invite ).
263 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
264 cfg->dial_escape_plus=val;
268 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
269 * #linphone_core_invite ).
272 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
273 return cfg->dial_escape_plus;
277 static bool_t is_a_phone_number(const char *username){
279 for(p=username;*p!='\0';++p){
292 static char *flatten_number(const char *number){
293 char *result=ms_malloc0(strlen(number)+1);
296 for(r=number;*r!='\0';++r){
297 if (*r=='+' || isdigit(*r)){
305 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
308 if (escape_plus && src[0]=='+' && destlen>2){
315 for(;i<destlen-1;++i){
323 static char *append_prefix(const char *number, const char *prefix){
324 char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
326 return strcat(res,number);
329 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
332 if (is_a_phone_number(username)){
333 flatten=flatten_number(username);
334 ms_message("Flattened number is '%s'",flatten);
335 numlen=strlen(flatten);
336 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL){
337 ms_message("No need to add a prefix");
338 /* prefix is already there */
339 copy_result(flatten,result,result_len,proxy->dial_escape_plus);
342 }else if (proxy->dial_prefix){
345 ms_message("Need to prefix with %s",proxy->dial_prefix);
347 /*remove initial number before prepending prefix*/
350 prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
352 copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
355 }else strncpy(result,username,result_len);
360 * Commits modification made to the proxy configuration.
362 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
364 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
366 linphone_proxy_config_write_all_to_config_file(obj->lc);
370 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
372 if (cfg->realm!=NULL) {
376 if (realm!=NULL) cfg->realm=ms_strdup(realm);
379 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
380 LinphoneOnlineStatus presence_mode){
382 SalOp *op=sal_op_new(proxy->lc->sal);
383 err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
384 linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
390 * Returns the route set for this proxy configuration.
392 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
393 return obj->reg_route;
397 * Returns the SIP identity that belongs to this proxy configuration.
399 * The SIP identity is a SIP address (Display Name <sip:username@domain> )
401 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
402 return obj->reg_identity;
406 * Returns TRUE if PUBLISH request is enabled for this proxy.
408 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
413 * Returns the proxy's SIP address.
415 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
416 return obj->reg_proxy;
420 * Returns the duration of registration.
422 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
427 * Returns TRUE if registration to the proxy is enabled.
429 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
430 return obj->reg_sendregister;
433 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
438 * Add a proxy configuration.
439 * This will start registration on the proxy, if registration is enabled.
441 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
442 if (!linphone_proxy_config_check(lc,cfg)) return -1;
443 if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
444 ms_warning("ProxyConfig already entered, ignored.");
447 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
448 linphone_proxy_config_apply(cfg,lc);
453 * Removes a proxy configuration.
455 * LinphoneCore will then automatically unregister and place the proxy configuration
456 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
458 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
459 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
460 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
461 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
462 /* this will unREGISTER */
463 linphone_proxy_config_edit(cfg);
464 if (lc->default_proxy==cfg){
465 lc->default_proxy=NULL;
469 * Erase all proxies from config.
473 void linphone_core_clear_proxy_config(LinphoneCore *lc){
474 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
475 for(;list!=NULL;list=list->next){
476 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
481 * Sets the default proxy.
483 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
484 * Toggling it as default will make LinphoneCore use the identity associated with
485 * the proxy configuration in all incoming and outgoing calls.
487 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
488 /* check if this proxy is in our list */
490 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
491 ms_warning("Bad proxy address: it is not in the list !");
492 lc->default_proxy=NULL;
496 lc->default_proxy=config;
500 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
501 if (index<0) linphone_core_set_default_proxy(lc,NULL);
502 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
506 * Returns the default proxy configuration, that is the one used to determine the current identity.
508 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
510 if (config!=NULL) *config=lc->default_proxy;
511 if (lc->default_proxy!=NULL){
512 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
518 * Returns an unmodifiable list of entered proxy configurations.
520 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
521 return lc->sip_conf.proxies;
524 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
528 sprintf(key,"proxy_%i",index);
529 lp_config_clean_section(config,key);
533 if (obj->type!=NULL){
534 lp_config_set_string(config,key,"type",obj->type);
536 if (obj->reg_proxy!=NULL){
537 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
539 if (obj->reg_route!=NULL){
540 lp_config_set_string(config,key,"reg_route",obj->reg_route);
542 if (obj->reg_identity!=NULL){
543 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
545 lp_config_set_int(config,key,"reg_expires",obj->expires);
546 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
547 lp_config_set_int(config,key,"publish",obj->publish);
548 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
549 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
554 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
557 const char *identity;
559 LinphoneProxyConfig *cfg;
562 sprintf(key,"proxy_%i",index);
564 if (!lp_config_has_section(config,key)){
568 cfg=linphone_proxy_config_new();
570 identity=lp_config_get_string(config,key,"reg_identity",NULL);
571 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
573 linphone_proxy_config_set_identity(cfg,identity);
574 linphone_proxy_config_set_server_addr(cfg,proxy);
576 tmp=lp_config_get_string(config,key,"reg_route",NULL);
577 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
579 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
580 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
582 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
584 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
585 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
587 tmp=lp_config_get_string(config,key,"type",NULL);
588 if (tmp!=NULL && strlen(tmp)>0)
589 linphone_proxy_config_set_sip_setup(cfg,tmp);
594 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
595 SipSetupContext *ssc;
596 SipSetup *ss=sip_setup_lookup(cfg->type);
597 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
600 ssc=sip_setup_context_new(ss,cfg);
602 if (cfg->reg_identity==NULL){
603 ms_error("Invalid identity for this proxy configuration.");
606 caps=sip_setup_context_get_capabilities(ssc);
607 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
608 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
609 if (lc->vtable.display_warning){
610 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
611 lc->vtable.display_warning(lc,tmp);
617 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
619 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
620 linphone_proxy_config_set_server_addr(cfg,proxy);
622 ms_error("Could not retrieve proxy uri !");
628 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
629 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
630 if (cfg->type!=NULL){
631 return sip_setup_lookup(cfg->type);
636 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
637 LinphoneCore *lc=cfg->lc;
639 if (cfg->type && cfg->ssctx==NULL){
640 linphone_proxy_config_activate_sip_setup(cfg);
642 if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
643 linphone_proxy_config_register(cfg);
648 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
651 cfg->type=ms_strdup(type);
652 if (linphone_proxy_config_get_addr(cfg)==NULL){
653 /*put a placeholder so that the sip setup gets saved into the config */
654 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
658 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
666 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
667 LinphoneAccountCreator *obj;
668 LinphoneProxyConfig *cfg;
669 SipSetup *ss=sip_setup_lookup(type);
670 SipSetupContext *ssctx;
674 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
675 ms_error("%s cannot manage accounts.");
678 obj=ms_new0(LinphoneAccountCreator,1);
679 cfg=linphone_proxy_config_new();
680 ssctx=sip_setup_context_new(ss,cfg);
683 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
688 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
689 set_string(&obj->username,username);
692 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
693 set_string(&obj->password,password);
696 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
697 set_string(&obj->domain,domain);
700 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
701 return obj->username;
704 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
708 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
709 SipSetupContext *ssctx=obj->ssctx;
710 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
711 int err=sip_setup_context_account_exists(ssctx,uri);
716 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
717 SipSetupContext *ssctx=obj->ssctx;
718 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
719 int err=sip_setup_context_create_account(ssctx,uri,obj->password);
723 return sip_setup_context_get_proxy_config(ssctx);
728 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
730 ms_free(obj->username);
732 ms_free(obj->password);
734 ms_free(obj->domain);
735 if (!obj->succeeded){
736 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
740 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
744 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
745 return cr->user_data;