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;
94 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
97 if (server_addr!=NULL && strlen(server_addr)>0){
98 addr=linphone_address_new(server_addr);
100 /*try to prepend 'sip:' */
101 if (strstr(server_addr,"sip:")==NULL){
102 char *try=ms_strdup_printf("sip:%s",server_addr);
103 addr=linphone_address_new(try);
108 obj->reg_proxy=ms_strdup(server_addr);
109 linphone_address_destroy(addr);
111 ms_warning("Could not parse %s",server_addr);
119 * Sets the user identity as a SIP address.
121 * This identity is normally formed with display name, username and domain, such
123 * Alice <sip:alice@example.net>
124 * The REGISTER messages will have from and to set to this identity.
127 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
128 LinphoneAddress *addr;
129 if (identity!=NULL && strlen(identity)>0){
130 addr=linphone_address_new(identity);
131 if (!addr || linphone_address_get_username(addr)==NULL){
132 ms_warning("Invalid sip identity: %s",identity);
134 linphone_address_destroy(addr);
137 if (obj->reg_identity!=NULL) {
138 ms_free(obj->reg_identity);
139 obj->reg_identity=NULL;
141 obj->reg_identity=ms_strdup(identity);
145 obj->realm=ms_strdup(linphone_address_get_domain(addr));
146 linphone_address_destroy(addr);
153 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
159 * When a route is set, all outgoing calls will go to the route's destination if this proxy
160 * is the default one (see linphone_core_set_default_proxy() ).
162 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
164 if (obj->reg_route!=NULL){
165 ms_free(obj->reg_route);
168 obj->reg_route=ms_strdup(route);
172 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
173 if (obj->reg_proxy==NULL){
174 if (lc->vtable.display_warning)
175 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
176 " followed by a hostname."));
179 if (obj->reg_identity==NULL){
180 if (lc->vtable.display_warning)
181 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
182 "sip:username@proxydomain, such as sip:alice@example.net"));
189 * Indicates whether a REGISTER request must be sent to the proxy.
191 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
192 obj->reg_sendregister=val;
196 * Sets the registration expiration time in seconds.
198 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
203 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
208 * Starts editing a proxy configuration.
210 * Because proxy configuration must be consistent, applications MUST
211 * call linphone_proxy_config_edit() before doing any attempts to modify
212 * proxy configuration (such as identity, proxy address and so on).
213 * Once the modifications are done, then the application must call
214 * linphone_proxy_config_done() to commit the changes.
216 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
217 if (obj->reg_sendregister){
219 if (obj->registered) {
220 sal_unregister(obj->op);
221 obj->registered=FALSE;
226 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
229 linphone_proxy_config_done(obj);
232 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
234 if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
235 else id_str=linphone_core_get_primary_contact(obj->lc);
236 if (obj->reg_sendregister){
238 sal_op_release(obj->op);
239 obj->op=sal_op_new(obj->lc->sal);
240 sal_op_set_user_pointer(obj->op,obj);
241 sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires);
247 * Sets a dialing prefix to be automatically prepended when inviting a number with
248 * #linphone_core_invite.
251 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
252 if (cfg->dial_prefix!=NULL){
253 ms_free(cfg->dial_prefix);
254 cfg->dial_prefix=NULL;
256 if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
260 * Returns dialing prefix.
264 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
265 return cfg->dial_prefix;
269 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
270 * #linphone_core_invite ).
273 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
274 cfg->dial_escape_plus=val;
278 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
279 * #linphone_core_invite ).
282 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
283 return cfg->dial_escape_plus;
287 static bool_t is_a_phone_number(const char *username){
289 for(p=username;*p!='\0';++p){
302 static char *flatten_number(const char *number){
303 char *result=ms_malloc0(strlen(number)+1);
306 for(r=number;*r!='\0';++r){
307 if (*r=='+' || isdigit(*r)){
315 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
318 if (escape_plus && src[0]=='+' && destlen>2){
325 for(;i<destlen-1;++i){
333 static char *append_prefix(const char *number, const char *prefix){
334 char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
336 return strcat(res,number);
339 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
342 if (is_a_phone_number(username)){
343 flatten=flatten_number(username);
344 ms_message("Flattened number is '%s'",flatten);
345 numlen=strlen(flatten);
346 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL){
347 ms_message("No need to add a prefix");
348 /* prefix is already there */
349 copy_result(flatten,result,result_len,proxy->dial_escape_plus);
352 }else if (proxy->dial_prefix){
355 ms_message("Need to prefix with %s",proxy->dial_prefix);
357 /*remove initial number before prepending prefix*/
360 prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
362 copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
365 }else strncpy(result,username,result_len);
370 * Commits modification made to the proxy configuration.
372 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
374 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
376 linphone_proxy_config_write_all_to_config_file(obj->lc);
380 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
382 if (cfg->realm!=NULL) {
386 if (realm!=NULL) cfg->realm=ms_strdup(realm);
389 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
390 LinphoneOnlineStatus presence_mode){
392 SalOp *op=sal_op_new(proxy->lc->sal);
393 err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
394 linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
400 * Returns the route set for this proxy configuration.
402 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
403 return obj->reg_route;
407 * Returns the SIP identity that belongs to this proxy configuration.
409 * The SIP identity is a SIP address (Display Name <sip:username@domain> )
411 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
412 return obj->reg_identity;
416 * Returns TRUE if PUBLISH request is enabled for this proxy.
418 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
423 * Returns the proxy's SIP address.
425 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
426 return obj->reg_proxy;
430 * Returns the duration of registration.
432 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
437 * Returns TRUE if registration to the proxy is enabled.
439 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
440 return obj->reg_sendregister;
443 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
448 * Add a proxy configuration.
449 * This will start registration on the proxy, if registration is enabled.
451 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
452 if (!linphone_proxy_config_check(lc,cfg)) return -1;
453 if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
454 ms_warning("ProxyConfig already entered, ignored.");
457 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
458 linphone_proxy_config_apply(cfg,lc);
463 * Removes a proxy configuration.
465 * LinphoneCore will then automatically unregister and place the proxy configuration
466 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
468 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
469 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
470 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
471 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
472 /* this will unREGISTER */
473 linphone_proxy_config_edit(cfg);
474 if (lc->default_proxy==cfg){
475 lc->default_proxy=NULL;
479 * Erase all proxies from config.
483 void linphone_core_clear_proxy_config(LinphoneCore *lc){
484 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
485 for(;list!=NULL;list=list->next){
486 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
491 * Sets the default proxy.
493 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
494 * Toggling it as default will make LinphoneCore use the identity associated with
495 * the proxy configuration in all incoming and outgoing calls.
497 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
498 /* check if this proxy is in our list */
500 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
501 ms_warning("Bad proxy address: it is not in the list !");
502 lc->default_proxy=NULL;
506 lc->default_proxy=config;
510 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
511 if (index<0) linphone_core_set_default_proxy(lc,NULL);
512 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
516 * Returns the default proxy configuration, that is the one used to determine the current identity.
518 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
520 if (config!=NULL) *config=lc->default_proxy;
521 if (lc->default_proxy!=NULL){
522 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
528 * Returns an unmodifiable list of entered proxy configurations.
530 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
531 return lc->sip_conf.proxies;
534 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
538 sprintf(key,"proxy_%i",index);
539 lp_config_clean_section(config,key);
543 if (obj->type!=NULL){
544 lp_config_set_string(config,key,"type",obj->type);
546 if (obj->reg_proxy!=NULL){
547 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
549 if (obj->reg_route!=NULL){
550 lp_config_set_string(config,key,"reg_route",obj->reg_route);
552 if (obj->reg_identity!=NULL){
553 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
555 lp_config_set_int(config,key,"reg_expires",obj->expires);
556 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
557 lp_config_set_int(config,key,"publish",obj->publish);
558 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
559 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
564 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
567 const char *identity;
569 LinphoneProxyConfig *cfg;
572 sprintf(key,"proxy_%i",index);
574 if (!lp_config_has_section(config,key)){
578 cfg=linphone_proxy_config_new();
580 identity=lp_config_get_string(config,key,"reg_identity",NULL);
581 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
583 linphone_proxy_config_set_identity(cfg,identity);
584 linphone_proxy_config_set_server_addr(cfg,proxy);
586 tmp=lp_config_get_string(config,key,"reg_route",NULL);
587 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
589 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
590 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
592 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
594 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
595 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
597 tmp=lp_config_get_string(config,key,"type",NULL);
598 if (tmp!=NULL && strlen(tmp)>0)
599 linphone_proxy_config_set_sip_setup(cfg,tmp);
604 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
605 SipSetupContext *ssc;
606 SipSetup *ss=sip_setup_lookup(cfg->type);
607 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
610 ssc=sip_setup_context_new(ss,cfg);
612 if (cfg->reg_identity==NULL){
613 ms_error("Invalid identity for this proxy configuration.");
616 caps=sip_setup_context_get_capabilities(ssc);
617 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
618 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
619 if (lc->vtable.display_warning){
620 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
621 lc->vtable.display_warning(lc,tmp);
627 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
629 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
630 linphone_proxy_config_set_server_addr(cfg,proxy);
632 ms_error("Could not retrieve proxy uri !");
638 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
639 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
640 if (cfg->type!=NULL){
641 return sip_setup_lookup(cfg->type);
646 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
647 LinphoneCore *lc=cfg->lc;
649 if (cfg->type && cfg->ssctx==NULL){
650 linphone_proxy_config_activate_sip_setup(cfg);
652 if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
653 linphone_proxy_config_register(cfg);
658 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
661 cfg->type=ms_strdup(type);
662 if (linphone_proxy_config_get_addr(cfg)==NULL){
663 /*put a placeholder so that the sip setup gets saved into the config */
664 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
668 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
676 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
677 LinphoneAccountCreator *obj;
678 LinphoneProxyConfig *cfg;
679 SipSetup *ss=sip_setup_lookup(type);
680 SipSetupContext *ssctx;
684 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
685 ms_error("%s cannot manage accounts.");
688 obj=ms_new0(LinphoneAccountCreator,1);
689 cfg=linphone_proxy_config_new();
690 ssctx=sip_setup_context_new(ss,cfg);
693 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
698 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
699 set_string(&obj->username,username);
702 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
703 set_string(&obj->password,password);
706 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
707 set_string(&obj->domain,domain);
710 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
711 return obj->username;
714 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
718 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
719 SipSetupContext *ssctx=obj->ssctx;
720 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
721 int err=sip_setup_context_account_exists(ssctx,uri);
726 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
727 SipSetupContext *ssctx=obj->ssctx;
728 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
729 int err=sip_setup_context_create_account(ssctx,uri,obj->password);
733 return sip_setup_context_get_proxy_config(ssctx);
738 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
740 ms_free(obj->username);
742 ms_free(obj->password);
744 ms_free(obj->domain);
745 if (!obj->succeeded){
746 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
750 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
754 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
755 return cr->user_data;