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"
30 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
33 if (!linphone_core_ready(lc)) return;
35 for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
36 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
37 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
39 /*to ensure removed configs are erased:*/
40 linphone_proxy_config_write_to_config_file(lc->config,NULL,i);
41 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
44 #define DEFAULT_INT(config,name,default) \
45 config?lp_config_get_int(config,"default_values",#name,default):default
46 #define DEFAULT_STRING(config,name,default) \
47 config?lp_config_get_string(config,"default_values",#name,default):default
49 static void linphone_proxy_config_init(LinphoneCore* lc,LinphoneProxyConfig *obj){
50 memset(obj,0,sizeof(LinphoneProxyConfig));
51 obj->magic=linphone_proxy_config_magic;
52 obj->expires=DEFAULT_INT((lc?lc->config:NULL),reg_expires,3600);
53 obj->dial_prefix=ms_strdup(DEFAULT_STRING((lc?lc->config:NULL),dial_prefix,'\0'));
54 obj->dial_escape_plus=DEFAULT_INT((lc?lc->config:NULL),dial_escape_plus,0);
63 * @deprecated, use #linphone_core_create_proxy_config instead
64 *Creates an empty proxy config.
66 LinphoneProxyConfig *linphone_proxy_config_new() {
67 return linphone_core_create_proxy_config(NULL);
69 LinphoneProxyConfig * linphone_core_create_proxy_config(LinphoneCore *lc) {
70 LinphoneProxyConfig *obj=NULL;
71 obj=ms_new(LinphoneProxyConfig,1);
72 linphone_proxy_config_init(lc,obj);
79 * Destroys a proxy config.
81 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
82 * linphone_core_remove_proxy_config() must not be freed.
84 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
85 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
86 if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
87 if (obj->reg_route!=NULL) ms_free(obj->reg_route);
88 if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
89 if (obj->realm!=NULL) ms_free(obj->realm);
90 if (obj->type!=NULL) ms_free(obj->type);
91 if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
92 if (obj->op) sal_op_release(obj->op);
93 if (obj->publish_op) sal_op_release(obj->publish_op);
97 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
99 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
100 return obj->state == LinphoneRegistrationOk;
104 * Sets the proxy address
106 * Examples of valid sip proxy address are:
107 * - IP address: sip:87.98.157.38
108 * - IP address with port: sip:87.98.157.38:5062
109 * - hostnames : sip:sip.example.net
111 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
112 LinphoneAddress *addr=NULL;
115 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
118 if (server_addr!=NULL && strlen(server_addr)>0){
119 if (strstr(server_addr,"sip:")==NULL){
120 modified=ms_strdup_printf("sip:%s",server_addr);
121 addr=linphone_address_new(modified);
125 addr=linphone_address_new(server_addr);
127 obj->reg_proxy=linphone_address_as_string_uri_only(addr);
128 linphone_address_destroy(addr);
130 ms_warning("Could not parse %s",server_addr);
138 * Sets the user identity as a SIP address.
140 * This identity is normally formed with display name, username and domain, such
142 * Alice <sip:alice@example.net>
143 * The REGISTER messages will have from and to set to this identity.
146 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
147 LinphoneAddress *addr;
148 if (identity!=NULL && strlen(identity)>0){
149 addr=linphone_address_new(identity);
150 if (!addr || linphone_address_get_username(addr)==NULL){
151 ms_warning("Invalid sip identity: %s",identity);
153 linphone_address_destroy(addr);
156 if (obj->reg_identity!=NULL) {
157 ms_free(obj->reg_identity);
158 obj->reg_identity=NULL;
160 obj->reg_identity=ms_strdup(identity);
164 obj->realm=ms_strdup(linphone_address_get_domain(addr));
165 linphone_address_destroy(addr);
172 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
178 * When a route is set, all outgoing calls will go to the route's destination if this proxy
179 * is the default one (see linphone_core_set_default_proxy() ).
181 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
183 if (obj->reg_route!=NULL){
184 ms_free(obj->reg_route);
190 /*try to prepend 'sip:' */
191 if (strstr(route,"sip:")==NULL){
192 tmp=ms_strdup_printf("sip:%s",route);
193 }else tmp=ms_strdup(route);
194 addr=sal_address_new(tmp);
196 sal_address_destroy(addr);
206 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
207 if (obj->reg_proxy==NULL){
208 if (lc->vtable.display_warning)
209 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
210 " followed by a hostname."));
213 if (obj->reg_identity==NULL){
214 if (lc->vtable.display_warning)
215 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
216 "sip:username@proxydomain, such as sip:alice@example.net"));
223 * Indicates whether a REGISTER request must be sent to the proxy.
225 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
226 obj->reg_sendregister=val;
230 * Sets the registration expiration time in seconds.
232 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
237 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
241 * Starts editing a proxy configuration.
243 * Because proxy configuration must be consistent, applications MUST
244 * call linphone_proxy_config_edit() before doing any attempts to modify
245 * proxy configuration (such as identity, proxy address and so on).
246 * Once the modifications are done, then the application must call
247 * linphone_proxy_config_done() to commit the changes.
249 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
250 if (obj->reg_sendregister){
252 if (obj->state != LinphoneRegistrationNone && obj->state != LinphoneRegistrationCleared) {
253 sal_unregister(obj->op);
258 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
261 linphone_proxy_config_done(obj);
264 static char *guess_contact_for_register(LinphoneProxyConfig *obj){
265 LinphoneAddress *proxy=linphone_address_new(obj->reg_proxy);
268 if (proxy==NULL) return NULL;
269 host=linphone_address_get_domain (proxy);
271 char localip[LINPHONE_IPADDR_SIZE];
274 LinphoneAddress *contact;
276 linphone_core_get_local_ip(obj->lc,host,localip);
277 contact=linphone_address_new(obj->reg_identity);
278 linphone_address_set_domain (contact,localip);
279 linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
280 linphone_address_set_display_name(contact,NULL);
282 linphone_core_get_sip_transports(obj->lc,&tr);
283 if (tr.udp_port <= 0) {
285 sal_address_set_param(contact,"transport","tcp");
286 } else if (tr.tls_port>0) {
287 sal_address_set_param(contact,"transport","tls");
290 tmp=linphone_address_as_string_uri_only(contact);
291 if (obj->contact_params)
292 ret=ms_strdup_printf("<%s;%s>",tmp,obj->contact_params);
293 else ret=ms_strdup_printf("<%s>",tmp);
294 linphone_address_destroy(contact);
297 linphone_address_destroy (proxy);
301 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
302 if (obj->reg_sendregister){
305 sal_op_release(obj->op);
306 obj->op=sal_op_new(obj->lc->sal);
307 contact=guess_contact_for_register(obj);
308 sal_op_set_contact(obj->op,contact);
310 sal_op_set_user_pointer(obj->op,obj);
311 if (sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)==0) {
312 linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress,"Registration in progress");
314 linphone_proxy_config_set_state(obj,LinphoneRegistrationFailed,"Registration failed");
320 * Refresh a proxy registration.
321 * This is useful if for example you resuming from suspend, thus IP address may have changed.
323 void linphone_proxy_config_refresh_register(LinphoneProxyConfig *obj){
324 if (obj->reg_sendregister && obj->op){
325 if (sal_register_refresh(obj->op,obj->expires) == 0) {
326 linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress, "Refresh registration");
333 * Sets a dialing prefix to be automatically prepended when inviting a number with
334 * linphone_core_invite();
335 * This dialing prefix shall usually be the country code of the country where the user is living.
338 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
339 if (cfg->dial_prefix!=NULL){
340 ms_free(cfg->dial_prefix);
341 cfg->dial_prefix=NULL;
343 if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
347 * Returns dialing prefix.
351 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
352 return cfg->dial_prefix;
356 * Sets whether liblinphone should replace "+" by international calling prefix in dialed numbers (passed to
357 * #linphone_core_invite ).
360 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
361 cfg->dial_escape_plus=val;
365 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
366 * #linphone_core_invite ).
369 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
370 return cfg->dial_escape_plus;
373 * http://en.wikipedia.org/wiki/Telephone_numbering_plan
374 * http://en.wikipedia.org/wiki/Telephone_numbers_in_Europe
376 typedef struct dial_plan{
378 const char* iso_country_code; /* ISO 3166-1 alpha-2 code, ex: FR for France*/
379 char ccc[8]; /*country calling code*/
380 int nnl; /*maximum national number length*/
381 const char * icp; /*international call prefix, ex: 00 in europe*/
385 /* TODO: fill with information for all countries over the world*/
386 static dial_plan_t const dial_plans[]={
387 {"France" ,"FR" , "33" , 9 , "00" },
388 {"United States" ,"US" , "1" , 10 , "011" },
389 {"Turkey" ,"TR" , "90" , 10 , "00" },
390 {"Switzerland" ,"XK" , "41" , 9 , "00" },
391 {NULL ,NULL,"" , 0 , NULL }
394 static dial_plan_t most_common_dialplan={ "generic" ,"", "", 10, "00"};
396 int linphone_dial_plan_lookup_ccc_from_iso(const char* iso) {
397 dial_plan_t* dial_plan;
398 for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
399 if (strcmp(iso, dial_plan->iso_country_code)==0) {
400 return atoi(dial_plan->ccc);
406 static void lookup_dial_plan(const char *ccc, dial_plan_t *plan){
408 for(i=0;dial_plans[i].country!=NULL;++i){
409 if (strcmp(ccc,dial_plans[i].ccc)==0){
414 /*else return a generic "most common" dial plan*/
415 *plan=most_common_dialplan;
416 strcpy(plan->ccc,ccc);
419 static bool_t is_a_phone_number(const char *username){
421 for(p=username;*p!='\0';++p){
435 static char *flatten_number(const char *number){
436 char *result=ms_malloc0(strlen(number)+1);
439 for(r=number;*r!='\0';++r){
440 if (*r=='+' || isdigit(*r)){
448 static void replace_plus(const char *src, char *dest, size_t destlen, const char *icp){
451 if (icp && src[0]=='+' && (destlen>(i=strlen(icp))) ){
456 for(;(i<destlen-1) && *src!='\0';++i){
464 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
466 if (is_a_phone_number(username)){
468 flatten=flatten_number(username);
469 ms_message("Flattened number is '%s'",flatten);
471 if (proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
472 /*no prefix configured, nothing else to do*/
473 strncpy(result,flatten,result_len);
477 dial_plan_t dialplan;
478 lookup_dial_plan(proxy->dial_prefix,&dialplan);
479 ms_message("Using dialplan '%s'",dialplan.country);
480 if (flatten[0]=='+' || strstr(flatten,dialplan.icp)==flatten){
481 /* the number has international prefix or +, so nothing to do*/
482 ms_message("Prefix already present.");
483 /*eventually replace the plus*/
484 replace_plus(flatten,result,result_len,proxy->dial_escape_plus ? dialplan.icp : NULL);
490 numlen=strlen(flatten);
491 /*keep at most national number significant digits */
492 skip=numlen-dialplan.nnl;
494 /*first prepend internation calling prefix or +*/
495 if (proxy->dial_escape_plus){
496 strncpy(result,dialplan.icp,result_len);
497 i+=strlen(dialplan.icp);
499 strncpy(result,"+",result_len);
503 if (result_len-i>strlen(dialplan.ccc)){
504 strcpy(result+i,dialplan.ccc);
505 i+=strlen(dialplan.ccc);
508 strncpy(result+i,flatten+skip,result_len-i-1);
512 }else strncpy(result,username,result_len);
517 * Commits modification made to the proxy configuration.
519 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
521 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
523 linphone_proxy_config_write_all_to_config_file(obj->lc);
527 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
529 if (cfg->realm!=NULL) {
533 if (realm!=NULL) cfg->realm=ms_strdup(realm);
536 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
537 LinphoneOnlineStatus presence_mode){
539 SalOp *op=sal_op_new(proxy->lc->sal);
540 err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
541 linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
542 if (proxy->publish_op!=NULL)
543 sal_op_release(proxy->publish_op);
544 proxy->publish_op=op;
549 * Returns the route set for this proxy configuration.
551 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
552 return obj->reg_route;
556 * Returns the SIP identity that belongs to this proxy configuration.
558 * The SIP identity is a SIP address (Display Name <sip:username@@domain> )
560 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
561 return obj->reg_identity;
565 * Returns TRUE if PUBLISH request is enabled for this proxy.
567 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
572 * Returns the proxy's SIP address.
574 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
575 return obj->reg_proxy;
579 * Returns the duration of registration.
581 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
586 * Returns TRUE if registration to the proxy is enabled.
588 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
589 return obj->reg_sendregister;
593 * Set optional contact parameters that will be added to the contact information sent in the registration.
594 * @param obj the proxy config object
595 * @param contact_params a string contaning the additional parameters in text form, like "myparam=something;myparam2=something_else"
597 * The main use case for this function is provide the proxy additional information regarding the user agent, like for example unique identifier or apple push id.
598 * As an example, the contact address in the SIP register sent will look like <sip:joe@15.128.128.93:50421;apple-push-id=43143-DFE23F-2323-FA2232>.
600 void linphone_proxy_config_set_contact_parameters(LinphoneProxyConfig *obj, const char *contact_params){
601 if (obj->contact_params) {
602 ms_free(obj->contact_params);
603 obj->contact_params=NULL;
606 obj->contact_params=ms_strdup(contact_params);
611 * Returns previously set contact parameters.
613 const char *linphone_proxy_config_get_contact_parameters(const LinphoneProxyConfig *obj){
614 return obj->contact_params;
617 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
622 * Add a proxy configuration.
623 * This will start registration on the proxy, if registration is enabled.
625 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
626 if (!linphone_proxy_config_check(lc,cfg)) {
629 if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
630 ms_warning("ProxyConfig already entered, ignored.");
633 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
634 linphone_proxy_config_apply(cfg,lc);
639 * Removes a proxy configuration.
641 * LinphoneCore will then automatically unregister and place the proxy configuration
642 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
644 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
645 /* check this proxy config is in the list before doing more*/
646 if (ms_list_find(lc->sip_conf.proxies,cfg)==NULL){
647 ms_error("linphone_core_remove_proxy_config: LinphoneProxyConfig %p is not known by LinphoneCore (programming error?)",cfg);
650 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
651 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
652 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
653 cfg->deletion_date=ms_time(NULL);
654 if (cfg->state==LinphoneRegistrationOk){
655 /* this will unREGISTER */
656 linphone_proxy_config_edit(cfg);
658 if (lc->default_proxy==cfg){
659 lc->default_proxy=NULL;
661 linphone_proxy_config_write_all_to_config_file(lc);
664 * Erase all proxies from config.
668 void linphone_core_clear_proxy_config(LinphoneCore *lc){
669 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
671 for(;list!=NULL;list=list->next){
672 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
675 linphone_proxy_config_write_all_to_config_file(lc);
678 * Sets the default proxy.
680 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
681 * Toggling it as default will make LinphoneCore use the identity associated with
682 * the proxy configuration in all incoming and outgoing calls.
684 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
685 /* check if this proxy is in our list */
687 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
688 ms_warning("Bad proxy address: it is not in the list !");
689 lc->default_proxy=NULL;
693 lc->default_proxy=config;
694 if (linphone_core_ready(lc))
695 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
698 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
699 if (index<0) linphone_core_set_default_proxy(lc,NULL);
700 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
704 * Returns the default proxy configuration, that is the one used to determine the current identity.
706 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
708 if (config!=NULL) *config=lc->default_proxy;
709 if (lc->default_proxy!=NULL){
710 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
716 * Returns an unmodifiable list of entered proxy configurations.
718 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
719 return lc->sip_conf.proxies;
722 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
726 sprintf(key,"proxy_%i",index);
727 lp_config_clean_section(config,key);
731 if (obj->type!=NULL){
732 lp_config_set_string(config,key,"type",obj->type);
734 if (obj->reg_proxy!=NULL){
735 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
737 if (obj->reg_route!=NULL){
738 lp_config_set_string(config,key,"reg_route",obj->reg_route);
740 if (obj->reg_identity!=NULL){
741 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
743 lp_config_set_int(config,key,"reg_expires",obj->expires);
744 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
745 lp_config_set_int(config,key,"publish",obj->publish);
746 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
747 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
752 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
755 const char *identity;
757 LinphoneProxyConfig *cfg;
760 sprintf(key,"proxy_%i",index);
762 if (!lp_config_has_section(config,key)){
766 cfg=linphone_proxy_config_new();
768 identity=lp_config_get_string(config,key,"reg_identity",NULL);
769 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
771 linphone_proxy_config_set_identity(cfg,identity);
772 linphone_proxy_config_set_server_addr(cfg,proxy);
774 tmp=lp_config_get_string(config,key,"reg_route",NULL);
775 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
777 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",DEFAULT_INT(config,reg_expires,600)));
778 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
780 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
782 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",DEFAULT_INT(config,dial_escape_plus,0)));
783 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",DEFAULT_STRING(config,dial_prefix,NULL)));
785 tmp=lp_config_get_string(config,key,"type",NULL);
786 if (tmp!=NULL && strlen(tmp)>0)
787 linphone_proxy_config_set_sip_setup(cfg,tmp);
792 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
793 SipSetupContext *ssc;
794 SipSetup *ss=sip_setup_lookup(cfg->type);
795 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
798 ssc=sip_setup_context_new(ss,cfg);
800 if (cfg->reg_identity==NULL){
801 ms_error("Invalid identity for this proxy configuration.");
804 caps=sip_setup_context_get_capabilities(ssc);
805 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
806 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
807 if (lc->vtable.display_warning){
808 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
809 lc->vtable.display_warning(lc,tmp);
815 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
817 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
818 linphone_proxy_config_set_server_addr(cfg,proxy);
820 ms_error("Could not retrieve proxy uri !");
826 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
827 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
828 if (cfg->type!=NULL){
829 return sip_setup_lookup(cfg->type);
834 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
835 LinphoneCore *lc=cfg->lc;
837 if (cfg->type && cfg->ssctx==NULL){
838 linphone_proxy_config_activate_sip_setup(cfg);
840 if (!lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
841 linphone_proxy_config_register(cfg);
842 if (cfg->publish && cfg->publish_op==NULL){
843 linphone_proxy_config_send_publish(cfg,lc->presence_mode);
849 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
852 cfg->type=ms_strdup(type);
853 if (linphone_proxy_config_get_addr(cfg)==NULL){
854 /*put a placeholder so that the sip setup gets saved into the config */
855 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
859 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
867 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
868 LinphoneAccountCreator *obj;
869 LinphoneProxyConfig *cfg;
870 SipSetup *ss=sip_setup_lookup(type);
871 SipSetupContext *ssctx;
875 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
876 ms_error("%s cannot manage accounts.",type);
879 obj=ms_new0(LinphoneAccountCreator,1);
880 cfg=linphone_proxy_config_new();
881 ssctx=sip_setup_context_new(ss,cfg);
884 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
889 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
890 set_string(&obj->username,username);
893 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
894 set_string(&obj->password,password);
897 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
898 set_string(&obj->domain,domain);
901 void linphone_account_creator_set_route(LinphoneAccountCreator *obj, const char *route) {
902 set_string(&obj->route,route);
905 void linphone_account_creator_set_email(LinphoneAccountCreator *obj, const char *email) {
906 set_string(&obj->email,email);
909 void linphone_account_creator_set_suscribe(LinphoneAccountCreator *obj, int suscribe) {
910 obj->suscribe = suscribe;
913 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
914 return obj->username;
917 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
921 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
922 SipSetupContext *ssctx=obj->ssctx;
923 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
924 int err=sip_setup_context_account_exists(ssctx,uri);
929 int linphone_account_creator_test_validation(LinphoneAccountCreator *obj) {
930 SipSetupContext *ssctx=obj->ssctx;
931 int err=sip_setup_context_account_validated(ssctx,obj->username);
935 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
936 SipSetupContext *ssctx=obj->ssctx;
937 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
938 int err=sip_setup_context_create_account(ssctx, uri, obj->password, obj->email, obj->suscribe);
942 return sip_setup_context_get_proxy_config(ssctx);
947 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
949 ms_free(obj->username);
951 ms_free(obj->password);
953 ms_free(obj->domain);
954 if (!obj->succeeded){
955 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
959 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
963 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
964 return cr->user_data;
967 void linphone_proxy_config_set_state(LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const char *message){
968 LinphoneCore *lc=cfg->lc;
970 if (lc && lc->vtable.registration_state_changed){
971 lc->vtable.registration_state_changed(lc,cfg,state,message);
975 LinphoneRegistrationState linphone_proxy_config_get_state(const LinphoneProxyConfig *cfg){
979 const char *linphone_registration_state_to_string(LinphoneRegistrationState cs){
981 case LinphoneRegistrationCleared:
982 return "LinphoneRegistrationCleared";
984 case LinphoneRegistrationNone:
985 return "LinphoneRegistrationNone";
987 case LinphoneRegistrationProgress:
988 return "LinphoneRegistrationProgress";
990 case LinphoneRegistrationOk:
991 return "LinphoneRegistrationOk";
993 case LinphoneRegistrationFailed:
994 return "LinphoneRegistrationFailed";
1000 LinphoneReason linphone_proxy_config_get_error(const LinphoneProxyConfig *cfg) {
1004 void linphone_proxy_config_set_error(LinphoneProxyConfig *cfg,LinphoneReason error) {