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"
25 #include "mediastreamer2/mediastream.h"
31 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
34 for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
35 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
36 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
40 void linphone_proxy_config_init(LinphoneProxyConfig *obj){
41 memset(obj,0,sizeof(LinphoneProxyConfig));
51 * Creates an empty proxy config.
53 LinphoneProxyConfig *linphone_proxy_config_new(){
54 LinphoneProxyConfig *obj=NULL;
55 obj=ms_new(LinphoneProxyConfig,1);
56 linphone_proxy_config_init(obj);
61 * Destroys a proxy config.
63 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
64 * linphone_core_remove_proxy_config() must not be freed.
66 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
67 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
68 if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
69 if (obj->reg_route!=NULL) ms_free(obj->reg_route);
70 if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
71 if (obj->realm!=NULL) ms_free(obj->realm);
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);
78 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
80 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
81 return obj->registered;
85 * Sets the proxy address
87 * Examples of valid sip proxy address are:
88 * - IP address: sip:87.98.157.38
89 * - IP address with port: sip:87.98.157.38:5062
90 * - hostnames : sip:sip.example.net
92 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
93 LinphoneAddress *addr=NULL;
96 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
99 if (server_addr!=NULL && strlen(server_addr)>0){
100 if (strstr(server_addr,"sip:")==NULL){
101 modified=ms_strdup_printf("sip:%s",server_addr);
102 addr=linphone_address_new(modified);
106 addr=linphone_address_new(server_addr);
108 obj->reg_proxy=linphone_address_as_string_uri_only(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);
169 /*try to prepend 'sip:' */
170 if (strstr(route,"sip:")==NULL){
171 obj->reg_route=ms_strdup_printf("sip:%s",route);
172 }else obj->reg_route=ms_strdup(route);
177 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
178 if (obj->reg_proxy==NULL){
179 if (lc->vtable.display_warning)
180 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
181 " followed by a hostname."));
184 if (obj->reg_identity==NULL){
185 if (lc->vtable.display_warning)
186 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
187 "sip:username@proxydomain, such as sip:alice@example.net"));
194 * Indicates whether a REGISTER request must be sent to the proxy.
196 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
197 obj->reg_sendregister=val;
201 * Sets the registration expiration time in seconds.
203 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
208 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
213 * Starts editing a proxy configuration.
215 * Because proxy configuration must be consistent, applications MUST
216 * call linphone_proxy_config_edit() before doing any attempts to modify
217 * proxy configuration (such as identity, proxy address and so on).
218 * Once the modifications are done, then the application must call
219 * linphone_proxy_config_done() to commit the changes.
221 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
222 if (obj->reg_sendregister){
224 if (obj->registered) {
225 sal_unregister(obj->op);
226 obj->registered=FALSE;
231 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
234 linphone_proxy_config_done(obj);
237 static char *guess_contact_for_register(LinphoneProxyConfig *obj){
238 LinphoneAddress *proxy=linphone_address_new(obj->reg_proxy);
241 if (proxy==NULL) return NULL;
242 host=linphone_address_get_domain (proxy);
244 LinphoneAddress *contact;
245 char localip[LINPHONE_IPADDR_SIZE];
247 linphone_core_get_local_ip(obj->lc,host,localip);
248 contact=linphone_address_new(obj->reg_identity);
249 linphone_address_set_domain (contact,localip);
250 linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
251 linphone_address_set_display_name(contact,NULL);
253 linphone_core_get_sip_transports(obj->lc,&tr);
254 if (tr.udp_port <= 0 && tr.tcp_port>0) {
255 sal_address_add_param(contact,"transport","tcp");
257 ret=linphone_address_as_string(contact);
258 linphone_address_destroy(contact);
260 linphone_address_destroy (proxy);
264 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
265 LinphoneGeneralStateContext gctx;
269 if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
270 else id_str=linphone_core_get_primary_contact(obj->lc);
271 if (obj->reg_sendregister){
274 sal_op_release(obj->op);
275 obj->op=sal_op_new(obj->lc->sal);
276 contact=guess_contact_for_register(obj);
277 sal_op_set_contact(obj->op,contact);
279 sal_op_set_user_pointer(obj->op,obj);
280 if (!sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)) {
281 gstate_new_state(obj->lc,GSTATE_REG_PENDING, gctx, NULL);
283 gstate_new_state(obj->lc,GSTATE_REG_FAILED, gctx, NULL);
290 * Sets a dialing prefix to be automatically prepended when inviting a number with
291 * #linphone_core_invite.
294 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
295 if (cfg->dial_prefix!=NULL){
296 ms_free(cfg->dial_prefix);
297 cfg->dial_prefix=NULL;
299 if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
303 * Returns dialing prefix.
307 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
308 return cfg->dial_prefix;
312 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
313 * #linphone_core_invite ).
316 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
317 cfg->dial_escape_plus=val;
321 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
322 * #linphone_core_invite ).
325 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
326 return cfg->dial_escape_plus;
330 static bool_t is_a_phone_number(const char *username){
332 for(p=username;*p!='\0';++p){
345 static char *flatten_number(const char *number){
346 char *result=ms_malloc0(strlen(number)+1);
349 for(r=number;*r!='\0';++r){
350 if (*r=='+' || isdigit(*r)){
358 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
361 if (escape_plus && src[0]=='+' && destlen>2){
368 for(;(i<destlen-1) && *src!='\0';++i){
376 static char *append_prefix(const char *number, const char *prefix){
377 char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
379 return strcat(res,number);
382 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
385 if (is_a_phone_number(username)){
386 flatten=flatten_number(username);
387 ms_message("Flattened number is '%s'",flatten);
388 numlen=strlen(flatten);
389 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
390 ms_message("No need to add a prefix");
391 /* prefix is already there */
392 copy_result(flatten,result,result_len,proxy->dial_escape_plus);
395 }else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){
398 ms_message("Need to prefix with %s",proxy->dial_prefix);
400 /*remove initial number before prepending prefix*/
403 prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
405 copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
408 }else strncpy(result,username,result_len);
413 * Commits modification made to the proxy configuration.
415 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
417 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
419 linphone_proxy_config_write_all_to_config_file(obj->lc);
423 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
425 if (cfg->realm!=NULL) {
429 if (realm!=NULL) cfg->realm=ms_strdup(realm);
432 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
433 LinphoneOnlineStatus presence_mode){
435 SalOp *op=sal_op_new(proxy->lc->sal);
436 err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
437 linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
443 * Returns the route set for this proxy configuration.
445 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
446 return obj->reg_route;
450 * Returns the SIP identity that belongs to this proxy configuration.
452 * The SIP identity is a SIP address (Display Name <sip:username@domain> )
454 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
455 return obj->reg_identity;
459 * Returns TRUE if PUBLISH request is enabled for this proxy.
461 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
466 * Returns the proxy's SIP address.
468 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
469 return obj->reg_proxy;
473 * Returns the duration of registration.
475 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
480 * Returns TRUE if registration to the proxy is enabled.
482 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
483 return obj->reg_sendregister;
486 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
491 * Add a proxy configuration.
492 * This will start registration on the proxy, if registration is enabled.
494 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
495 if (!linphone_proxy_config_check(lc,cfg)) return -1;
496 if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
497 ms_warning("ProxyConfig already entered, ignored.");
500 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
501 linphone_proxy_config_apply(cfg,lc);
506 * Removes a proxy configuration.
508 * LinphoneCore will then automatically unregister and place the proxy configuration
509 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
511 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
512 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
513 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
514 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
515 cfg->deletion_date=ms_time(NULL);
516 /* this will unREGISTER */
517 linphone_proxy_config_edit(cfg);
518 if (lc->default_proxy==cfg){
519 lc->default_proxy=NULL;
523 * Erase all proxies from config.
527 void linphone_core_clear_proxy_config(LinphoneCore *lc){
528 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
529 for(;list!=NULL;list=list->next){
530 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
535 * Sets the default proxy.
537 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
538 * Toggling it as default will make LinphoneCore use the identity associated with
539 * the proxy configuration in all incoming and outgoing calls.
541 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
542 /* check if this proxy is in our list */
544 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
545 ms_warning("Bad proxy address: it is not in the list !");
546 lc->default_proxy=NULL;
550 lc->default_proxy=config;
554 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
555 if (index<0) linphone_core_set_default_proxy(lc,NULL);
556 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
560 * Returns the default proxy configuration, that is the one used to determine the current identity.
562 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
564 if (config!=NULL) *config=lc->default_proxy;
565 if (lc->default_proxy!=NULL){
566 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
572 * Returns an unmodifiable list of entered proxy configurations.
574 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
575 return lc->sip_conf.proxies;
578 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
582 sprintf(key,"proxy_%i",index);
583 lp_config_clean_section(config,key);
587 if (obj->type!=NULL){
588 lp_config_set_string(config,key,"type",obj->type);
590 if (obj->reg_proxy!=NULL){
591 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
593 if (obj->reg_route!=NULL){
594 lp_config_set_string(config,key,"reg_route",obj->reg_route);
596 if (obj->reg_identity!=NULL){
597 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
599 lp_config_set_int(config,key,"reg_expires",obj->expires);
600 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
601 lp_config_set_int(config,key,"publish",obj->publish);
602 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
603 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
608 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
611 const char *identity;
613 LinphoneProxyConfig *cfg;
616 sprintf(key,"proxy_%i",index);
618 if (!lp_config_has_section(config,key)){
622 cfg=linphone_proxy_config_new();
624 identity=lp_config_get_string(config,key,"reg_identity",NULL);
625 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
627 linphone_proxy_config_set_identity(cfg,identity);
628 linphone_proxy_config_set_server_addr(cfg,proxy);
630 tmp=lp_config_get_string(config,key,"reg_route",NULL);
631 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
633 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
634 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
636 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
638 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
639 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
641 tmp=lp_config_get_string(config,key,"type",NULL);
642 if (tmp!=NULL && strlen(tmp)>0)
643 linphone_proxy_config_set_sip_setup(cfg,tmp);
648 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
649 SipSetupContext *ssc;
650 SipSetup *ss=sip_setup_lookup(cfg->type);
651 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
654 ssc=sip_setup_context_new(ss,cfg);
656 if (cfg->reg_identity==NULL){
657 ms_error("Invalid identity for this proxy configuration.");
660 caps=sip_setup_context_get_capabilities(ssc);
661 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
662 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
663 if (lc->vtable.display_warning){
664 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
665 lc->vtable.display_warning(lc,tmp);
671 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
673 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
674 linphone_proxy_config_set_server_addr(cfg,proxy);
676 ms_error("Could not retrieve proxy uri !");
682 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
683 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
684 if (cfg->type!=NULL){
685 return sip_setup_lookup(cfg->type);
690 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
691 LinphoneCore *lc=cfg->lc;
693 if (cfg->type && cfg->ssctx==NULL){
694 linphone_proxy_config_activate_sip_setup(cfg);
696 if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
697 linphone_proxy_config_register(cfg);
702 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
705 cfg->type=ms_strdup(type);
706 if (linphone_proxy_config_get_addr(cfg)==NULL){
707 /*put a placeholder so that the sip setup gets saved into the config */
708 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
712 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
720 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
721 LinphoneAccountCreator *obj;
722 LinphoneProxyConfig *cfg;
723 SipSetup *ss=sip_setup_lookup(type);
724 SipSetupContext *ssctx;
728 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
729 ms_error("%s cannot manage accounts.");
732 obj=ms_new0(LinphoneAccountCreator,1);
733 cfg=linphone_proxy_config_new();
734 ssctx=sip_setup_context_new(ss,cfg);
737 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
742 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
743 set_string(&obj->username,username);
746 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
747 set_string(&obj->password,password);
750 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
751 set_string(&obj->domain,domain);
754 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
755 return obj->username;
758 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
762 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
763 SipSetupContext *ssctx=obj->ssctx;
764 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
765 int err=sip_setup_context_account_exists(ssctx,uri);
770 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
771 SipSetupContext *ssctx=obj->ssctx;
772 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
773 int err=sip_setup_context_create_account(ssctx,uri,obj->password);
777 return sip_setup_context_get_proxy_config(ssctx);
782 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
784 ms_free(obj->username);
786 ms_free(obj->password);
788 ms_free(obj->domain);
789 if (!obj->succeeded){
790 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
794 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
798 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
799 return cr->user_data;