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;
95 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
98 if (server_addr!=NULL && strlen(server_addr)>0){
99 addr=linphone_address_new(server_addr);
101 /*try to prepend 'sip:' */
102 if (strstr(server_addr,"sip:")==NULL){
103 try=ms_strdup_printf("sip:%s",server_addr);
104 addr=linphone_address_new(try);
109 obj->reg_proxy=linphone_address_as_string_uri_only(addr);
110 linphone_address_destroy(addr);
112 ms_warning("Could not parse %s",server_addr);
120 * Sets the user identity as a SIP address.
122 * This identity is normally formed with display name, username and domain, such
124 * Alice <sip:alice@example.net>
125 * The REGISTER messages will have from and to set to this identity.
128 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
129 LinphoneAddress *addr;
130 if (identity!=NULL && strlen(identity)>0){
131 addr=linphone_address_new(identity);
132 if (!addr || linphone_address_get_username(addr)==NULL){
133 ms_warning("Invalid sip identity: %s",identity);
135 linphone_address_destroy(addr);
138 if (obj->reg_identity!=NULL) {
139 ms_free(obj->reg_identity);
140 obj->reg_identity=NULL;
142 obj->reg_identity=ms_strdup(identity);
146 obj->realm=ms_strdup(linphone_address_get_domain(addr));
147 linphone_address_destroy(addr);
154 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
160 * When a route is set, all outgoing calls will go to the route's destination if this proxy
161 * is the default one (see linphone_core_set_default_proxy() ).
163 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
165 if (obj->reg_route!=NULL){
166 ms_free(obj->reg_route);
169 obj->reg_route=ms_strdup(route);
173 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
174 if (obj->reg_proxy==NULL){
175 if (lc->vtable.display_warning)
176 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
177 " followed by a hostname."));
180 if (obj->reg_identity==NULL){
181 if (lc->vtable.display_warning)
182 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
183 "sip:username@proxydomain, such as sip:alice@example.net"));
190 * Indicates whether a REGISTER request must be sent to the proxy.
192 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
193 obj->reg_sendregister=val;
197 * Sets the registration expiration time in seconds.
199 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
204 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
209 * Starts editing a proxy configuration.
211 * Because proxy configuration must be consistent, applications MUST
212 * call linphone_proxy_config_edit() before doing any attempts to modify
213 * proxy configuration (such as identity, proxy address and so on).
214 * Once the modifications are done, then the application must call
215 * linphone_proxy_config_done() to commit the changes.
217 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
218 if (obj->reg_sendregister){
220 if (obj->registered) {
221 sal_unregister(obj->op);
222 obj->registered=FALSE;
227 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
230 linphone_proxy_config_done(obj);
233 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
235 if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
236 else id_str=linphone_core_get_primary_contact(obj->lc);
237 if (obj->reg_sendregister){
239 sal_op_release(obj->op);
240 obj->op=sal_op_new(obj->lc->sal);
241 sal_op_set_user_pointer(obj->op,obj);
242 if (!sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)) {
243 gstate_new_state(obj->lc,GSTATE_REG_PENDING,NULL);
245 gstate_new_state(obj->lc,GSTATE_REG_FAILED,NULL);
252 * Sets a dialing prefix to be automatically prepended when inviting a number with
253 * #linphone_core_invite.
256 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
257 if (cfg->dial_prefix!=NULL){
258 ms_free(cfg->dial_prefix);
259 cfg->dial_prefix=NULL;
261 if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
265 * Returns dialing prefix.
269 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
270 return cfg->dial_prefix;
274 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
275 * #linphone_core_invite ).
278 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
279 cfg->dial_escape_plus=val;
283 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
284 * #linphone_core_invite ).
287 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
288 return cfg->dial_escape_plus;
292 static bool_t is_a_phone_number(const char *username){
294 for(p=username;*p!='\0';++p){
307 static char *flatten_number(const char *number){
308 char *result=ms_malloc0(strlen(number)+1);
311 for(r=number;*r!='\0';++r){
312 if (*r=='+' || isdigit(*r)){
320 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
323 if (escape_plus && src[0]=='+' && destlen>2){
330 for(;(i<destlen-1) && *src!='\0';++i){
338 static char *append_prefix(const char *number, const char *prefix){
339 char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
341 return strcat(res,number);
344 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
347 if (is_a_phone_number(username)){
348 flatten=flatten_number(username);
349 ms_message("Flattened number is '%s'",flatten);
350 numlen=strlen(flatten);
351 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
352 ms_message("No need to add a prefix");
353 /* prefix is already there */
354 copy_result(flatten,result,result_len,proxy->dial_escape_plus);
357 }else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){
360 ms_message("Need to prefix with %s",proxy->dial_prefix);
362 /*remove initial number before prepending prefix*/
365 prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
367 copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
370 }else strncpy(result,username,result_len);
375 * Commits modification made to the proxy configuration.
377 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
379 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
381 linphone_proxy_config_write_all_to_config_file(obj->lc);
385 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
387 if (cfg->realm!=NULL) {
391 if (realm!=NULL) cfg->realm=ms_strdup(realm);
394 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
395 LinphoneOnlineStatus presence_mode){
397 SalOp *op=sal_op_new(proxy->lc->sal);
398 err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
399 linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
405 * Returns the route set for this proxy configuration.
407 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
408 return obj->reg_route;
412 * Returns the SIP identity that belongs to this proxy configuration.
414 * The SIP identity is a SIP address (Display Name <sip:username@domain> )
416 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
417 return obj->reg_identity;
421 * Returns TRUE if PUBLISH request is enabled for this proxy.
423 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
428 * Returns the proxy's SIP address.
430 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
431 return obj->reg_proxy;
435 * Returns the duration of registration.
437 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
442 * Returns TRUE if registration to the proxy is enabled.
444 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
445 return obj->reg_sendregister;
448 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
453 * Add a proxy configuration.
454 * This will start registration on the proxy, if registration is enabled.
456 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
457 if (!linphone_proxy_config_check(lc,cfg)) return -1;
458 if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
459 ms_warning("ProxyConfig already entered, ignored.");
462 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
463 linphone_proxy_config_apply(cfg,lc);
468 * Removes a proxy configuration.
470 * LinphoneCore will then automatically unregister and place the proxy configuration
471 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
473 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
474 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
475 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
476 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
477 /* this will unREGISTER */
478 linphone_proxy_config_edit(cfg);
479 if (lc->default_proxy==cfg){
480 lc->default_proxy=NULL;
484 * Erase all proxies from config.
488 void linphone_core_clear_proxy_config(LinphoneCore *lc){
489 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
490 for(;list!=NULL;list=list->next){
491 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
496 * Sets the default proxy.
498 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
499 * Toggling it as default will make LinphoneCore use the identity associated with
500 * the proxy configuration in all incoming and outgoing calls.
502 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
503 /* check if this proxy is in our list */
505 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
506 ms_warning("Bad proxy address: it is not in the list !");
507 lc->default_proxy=NULL;
511 lc->default_proxy=config;
515 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
516 if (index<0) linphone_core_set_default_proxy(lc,NULL);
517 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
521 * Returns the default proxy configuration, that is the one used to determine the current identity.
523 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
525 if (config!=NULL) *config=lc->default_proxy;
526 if (lc->default_proxy!=NULL){
527 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
533 * Returns an unmodifiable list of entered proxy configurations.
535 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
536 return lc->sip_conf.proxies;
539 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
543 sprintf(key,"proxy_%i",index);
544 lp_config_clean_section(config,key);
548 if (obj->type!=NULL){
549 lp_config_set_string(config,key,"type",obj->type);
551 if (obj->reg_proxy!=NULL){
552 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
554 if (obj->reg_route!=NULL){
555 lp_config_set_string(config,key,"reg_route",obj->reg_route);
557 if (obj->reg_identity!=NULL){
558 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
560 lp_config_set_int(config,key,"reg_expires",obj->expires);
561 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
562 lp_config_set_int(config,key,"publish",obj->publish);
563 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
564 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
569 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
572 const char *identity;
574 LinphoneProxyConfig *cfg;
577 sprintf(key,"proxy_%i",index);
579 if (!lp_config_has_section(config,key)){
583 cfg=linphone_proxy_config_new();
585 identity=lp_config_get_string(config,key,"reg_identity",NULL);
586 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
588 linphone_proxy_config_set_identity(cfg,identity);
589 linphone_proxy_config_set_server_addr(cfg,proxy);
591 tmp=lp_config_get_string(config,key,"reg_route",NULL);
592 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
594 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
595 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
597 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
599 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
600 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
602 tmp=lp_config_get_string(config,key,"type",NULL);
603 if (tmp!=NULL && strlen(tmp)>0)
604 linphone_proxy_config_set_sip_setup(cfg,tmp);
609 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
610 SipSetupContext *ssc;
611 SipSetup *ss=sip_setup_lookup(cfg->type);
612 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
615 ssc=sip_setup_context_new(ss,cfg);
617 if (cfg->reg_identity==NULL){
618 ms_error("Invalid identity for this proxy configuration.");
621 caps=sip_setup_context_get_capabilities(ssc);
622 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
623 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
624 if (lc->vtable.display_warning){
625 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
626 lc->vtable.display_warning(lc,tmp);
632 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
634 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
635 linphone_proxy_config_set_server_addr(cfg,proxy);
637 ms_error("Could not retrieve proxy uri !");
643 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
644 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
645 if (cfg->type!=NULL){
646 return sip_setup_lookup(cfg->type);
651 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
652 LinphoneCore *lc=cfg->lc;
654 if (cfg->type && cfg->ssctx==NULL){
655 linphone_proxy_config_activate_sip_setup(cfg);
657 if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
658 linphone_proxy_config_register(cfg);
663 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
666 cfg->type=ms_strdup(type);
667 if (linphone_proxy_config_get_addr(cfg)==NULL){
668 /*put a placeholder so that the sip setup gets saved into the config */
669 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
673 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
681 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
682 LinphoneAccountCreator *obj;
683 LinphoneProxyConfig *cfg;
684 SipSetup *ss=sip_setup_lookup(type);
685 SipSetupContext *ssctx;
689 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
690 ms_error("%s cannot manage accounts.");
693 obj=ms_new0(LinphoneAccountCreator,1);
694 cfg=linphone_proxy_config_new();
695 ssctx=sip_setup_context_new(ss,cfg);
698 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
703 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
704 set_string(&obj->username,username);
707 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
708 set_string(&obj->password,password);
711 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
712 set_string(&obj->domain,domain);
715 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
716 return obj->username;
719 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
723 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
724 SipSetupContext *ssctx=obj->ssctx;
725 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
726 int err=sip_setup_context_account_exists(ssctx,uri);
731 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
732 SipSetupContext *ssctx=obj->ssctx;
733 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
734 int err=sip_setup_context_create_account(ssctx,uri,obj->password);
738 return sip_setup_context_get_proxy_config(ssctx);
743 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
745 ms_free(obj->username);
747 ms_free(obj->password);
749 ms_free(obj->domain);
750 if (!obj->succeeded){
751 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
755 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
759 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
760 return cr->user_data;