]> sjero.net Git - linphone/blob - coreapi/proxy.c
Merge remote-tracking branch 'origin/master' into dev_gtk_new_ui
[linphone] / coreapi / proxy.c
1 /*
2 linphone
3 Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4 */
5 /*
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.
10  *
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.
15  *
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.
19  */
20  
21 #include "linphonecore.h"
22 #include "sipsetup.h"
23 #include "lpconfig.h"
24 #include "private.h"
25 #include "mediastreamer2/mediastream.h"
26
27 #include <ctype.h>
28
29
30 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
31         MSList *elem;
32         int i;
33         if (!linphone_core_ready(lc)) return;
34         
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);
38         }
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));
42 }
43
44 static void linphone_proxy_config_init(LinphoneCore* lc,LinphoneProxyConfig *obj){
45         memset(obj,0,sizeof(LinphoneProxyConfig));
46         obj->magic=linphone_proxy_config_magic;
47         obj->expires=LP_CONFIG_DEFAULT_INT((lc?lc->config:NULL),"reg_expires",3600);
48         obj->dial_prefix=ms_strdup(LP_CONFIG_DEFAULT_STRING((lc?lc->config:NULL),"dial_prefix",'\0'));
49         obj->dial_escape_plus=LP_CONFIG_DEFAULT_INT((lc?lc->config:NULL),"dial_escape_plus",0);
50 }
51
52 /**
53  * @addtogroup proxies
54  * @{
55 **/
56
57 /**
58  * @deprecated, use #linphone_core_create_proxy_config instead
59  *Creates an empty proxy config.
60 **/
61 LinphoneProxyConfig *linphone_proxy_config_new() {
62         return linphone_core_create_proxy_config(NULL);
63 }
64 LinphoneProxyConfig * linphone_core_create_proxy_config(LinphoneCore *lc) {
65         LinphoneProxyConfig *obj=NULL;
66         obj=ms_new(LinphoneProxyConfig,1);
67         linphone_proxy_config_init(lc,obj);
68         return obj;
69 }
70
71
72
73 /**
74  * Destroys a proxy config.
75  * 
76  * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
77  * linphone_core_remove_proxy_config() must not be freed.
78 **/
79 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
80         if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
81         if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
82         if (obj->reg_route!=NULL) ms_free(obj->reg_route);
83         if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
84         if (obj->realm!=NULL) ms_free(obj->realm);
85         if (obj->type!=NULL) ms_free(obj->type);
86         if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
87         if (obj->op) sal_op_release(obj->op);
88         if (obj->publish_op) sal_op_release(obj->publish_op);
89 }
90
91 /**
92  * Returns a boolean indicating that the user is sucessfully registered on the proxy.
93 **/
94 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
95         return obj->state == LinphoneRegistrationOk;
96 }
97
98 /**
99  * Sets the proxy address
100  *
101  * Examples of valid sip proxy address are:
102  * - IP address: sip:87.98.157.38
103  * - IP address with port: sip:87.98.157.38:5062
104  * - hostnames : sip:sip.example.net
105 **/
106 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
107         LinphoneAddress *addr=NULL;
108         char *modified=NULL;
109         
110         if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
111         obj->reg_proxy=NULL;
112         
113         if (server_addr!=NULL && strlen(server_addr)>0){
114                 if (strstr(server_addr,"sip:")==NULL){
115                         modified=ms_strdup_printf("sip:%s",server_addr);
116                         addr=linphone_address_new(modified);
117                         ms_free(modified);
118                 }
119                 if (addr==NULL)
120                         addr=linphone_address_new(server_addr);
121                 if (addr){
122                         obj->reg_proxy=linphone_address_as_string_uri_only(addr);
123                         linphone_address_destroy(addr);
124                 }else{
125                         ms_warning("Could not parse %s",server_addr);
126                         return -1;
127                 }
128         }
129         return 0;
130 }
131
132 /**
133  * Sets the user identity as a SIP address.
134  *
135  * This identity is normally formed with display name, username and domain, such 
136  * as:
137  * Alice <sip:alice@example.net>
138  * The REGISTER messages will have from and to set to this identity.
139  *
140 **/
141 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
142         LinphoneAddress *addr;
143         if (identity!=NULL && strlen(identity)>0){
144                 addr=linphone_address_new(identity);
145                 if (!addr || linphone_address_get_username(addr)==NULL){
146                         ms_warning("Invalid sip identity: %s",identity);
147                         if (addr)
148                                 linphone_address_destroy(addr);
149                         return -1;
150                 }else{
151                         if (obj->reg_identity!=NULL) {
152                                 ms_free(obj->reg_identity);
153                                 obj->reg_identity=NULL;
154                         }
155                         obj->reg_identity=ms_strdup(identity);
156                         if (obj->realm){
157                                 ms_free(obj->realm);
158                         }
159                         obj->realm=ms_strdup(linphone_address_get_domain(addr));
160                         linphone_address_destroy(addr);
161                         return 0;
162                 }
163         }
164         return -1;
165 }
166
167 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
168         return cfg->realm;
169 }
170
171 /**
172  * Sets a SIP route.
173  * When a route is set, all outgoing calls will go to the route's destination if this proxy
174  * is the default one (see linphone_core_set_default_proxy() ).
175 **/
176 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
177 {
178         if (obj->reg_route!=NULL){
179                 ms_free(obj->reg_route);
180                 obj->reg_route=NULL;
181         }
182         if (route!=NULL){
183                 SalAddress *addr;
184                 char *tmp;
185                 /*try to prepend 'sip:' */
186                 if (strstr(route,"sip:")==NULL){
187                         tmp=ms_strdup_printf("sip:%s",route);
188                 }else tmp=ms_strdup(route);
189                 addr=sal_address_new(tmp);
190                 if (addr!=NULL){
191                         sal_address_destroy(addr);
192                 }else{
193                         ms_free(tmp);
194                         tmp=NULL;
195                 }
196                 obj->reg_route=tmp;
197         }
198         return 0;
199 }
200
201 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
202         if (obj->reg_proxy==NULL){
203                 if (lc->vtable.display_warning)
204                         lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
205                                                 " followed by a hostname."));
206                 return FALSE;
207         }
208         if (obj->reg_identity==NULL){
209                 if (lc->vtable.display_warning)
210                         lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
211                                         "sip:username@proxydomain, such as sip:alice@example.net"));
212                 return FALSE;
213         }
214         return TRUE;
215 }
216
217 /**
218  * Indicates whether a REGISTER request must be sent to the proxy.
219 **/
220 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
221         obj->reg_sendregister=val;
222 }
223
224 /**
225  * Sets the registration expiration time in seconds.
226 **/
227 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
228         if (val<0) val=600;
229         obj->expires=val;
230 }
231
232 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
233         obj->publish=val;
234 }
235 /**
236  * Starts editing a proxy configuration.
237  *
238  * Because proxy configuration must be consistent, applications MUST
239  * call linphone_proxy_config_edit() before doing any attempts to modify
240  * proxy configuration (such as identity, proxy address and so on).
241  * Once the modifications are done, then the application must call
242  * linphone_proxy_config_done() to commit the changes.
243 **/
244 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
245         if (obj->reg_sendregister){
246                 /* unregister */
247                 if (obj->state != LinphoneRegistrationNone && obj->state != LinphoneRegistrationCleared) {
248                         sal_unregister(obj->op);
249                 }
250         }
251 }
252
253 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
254 {
255         obj->lc=lc;
256         linphone_proxy_config_done(obj);
257 }
258
259 static char *guess_contact_for_register(LinphoneProxyConfig *obj){
260         LinphoneAddress *proxy=linphone_address_new(obj->reg_proxy);
261         char *ret=NULL;
262         const char *host;
263         if (proxy==NULL) return NULL;
264         host=linphone_address_get_domain (proxy);
265         if (host!=NULL){
266                 char localip[LINPHONE_IPADDR_SIZE];
267                 char *tmp;
268                 LCSipTransports tr;
269                 LinphoneAddress *contact;
270                 
271                 linphone_core_get_local_ip(obj->lc,host,localip);
272                 contact=linphone_address_new(obj->reg_identity);
273                 linphone_address_set_domain (contact,localip);
274                 linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
275                 linphone_address_set_display_name(contact,NULL);
276                 
277                 linphone_core_get_sip_transports(obj->lc,&tr);
278                 if (tr.udp_port <= 0) {
279                         if (tr.tcp_port>0) {
280                                 sal_address_set_param(contact,"transport","tcp");
281                         } else if (tr.tls_port>0) {
282                                 sal_address_set_param(contact,"transport","tls");
283                         }
284                 }
285                 tmp=linphone_address_as_string_uri_only(contact);
286                 if (obj->contact_params)
287                         ret=ms_strdup_printf("<%s;%s>",tmp,obj->contact_params);
288                 else ret=ms_strdup_printf("<%s>",tmp);
289                 linphone_address_destroy(contact);
290                 ms_free(tmp);
291         }
292         linphone_address_destroy (proxy);
293         return ret;
294 }
295
296 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
297         if (obj->reg_sendregister){
298                 char *contact;
299                 if (obj->op)
300                         sal_op_release(obj->op);
301                 obj->op=sal_op_new(obj->lc->sal);
302                 contact=guess_contact_for_register(obj);
303                 sal_op_set_contact(obj->op,contact);
304                 ms_free(contact);
305                 sal_op_set_user_pointer(obj->op,obj);
306                 if (sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)==0) {
307                         linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress,"Registration in progress");
308                 } else {
309                         linphone_proxy_config_set_state(obj,LinphoneRegistrationFailed,"Registration failed");
310                 }
311         }
312 }
313
314 /**
315  * Refresh a proxy registration.
316  * This is useful if for example you resuming from suspend, thus IP address may have changed.
317 **/
318 void linphone_proxy_config_refresh_register(LinphoneProxyConfig *obj){
319         if (obj->reg_sendregister && obj->op){
320                 if (sal_register_refresh(obj->op,obj->expires) == 0) {
321                         linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress, "Refresh registration");
322                 }
323         }
324 }
325
326
327 /**
328  * Sets a dialing prefix to be automatically prepended when inviting a number with 
329  * linphone_core_invite();
330  * This dialing prefix shall usually be the country code of the country where the user is living.
331  *
332 **/
333 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
334         if (cfg->dial_prefix!=NULL){
335                 ms_free(cfg->dial_prefix);
336                 cfg->dial_prefix=NULL;
337         }
338         if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
339 }
340
341 /**
342  * Returns dialing prefix.
343  *
344  * 
345 **/
346 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
347         return cfg->dial_prefix;
348 }
349
350 /**
351  * Sets whether liblinphone should replace "+" by international calling prefix in dialed numbers (passed to
352  * #linphone_core_invite ).
353  *
354 **/
355 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
356         cfg->dial_escape_plus=val;
357 }
358
359 /**
360  * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
361  * #linphone_core_invite ).
362  *
363 **/
364 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
365         return cfg->dial_escape_plus;
366 }
367 /*
368  * http://en.wikipedia.org/wiki/Telephone_numbering_plan
369  * http://en.wikipedia.org/wiki/Telephone_numbers_in_Europe
370  */
371 typedef struct dial_plan{
372         const char *country;
373         const char* iso_country_code; /* ISO 3166-1 alpha-2 code, ex: FR for France*/
374         char  ccc[8]; /*country calling code*/
375         int nnl; /*maximum national number length*/
376         const char * icp; /*international call prefix, ex: 00 in europe*/
377         
378 }dial_plan_t;
379
380 /* TODO: fill with information for all countries over the world*/
381 static dial_plan_t const dial_plans[]={
382         {"France"                       ,"FR"           , "33"  , 9             , "00"  },
383         {"United States"        ,"US"           , "1"   , 10    , "011" },
384         {"Turkey"                       ,"TR"           , "90"  , 10    , "00"  },
385         {"Switzerland"          ,"XK"           , "41"  , 9             , "00"  },
386         {NULL           ,NULL,""        , 0     , NULL  }
387 };
388
389 static dial_plan_t most_common_dialplan={ "generic" ,"", "", 10, "00"};
390
391 int linphone_dial_plan_lookup_ccc_from_iso(const char* iso) {
392         dial_plan_t* dial_plan;
393         for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
394                 if (strcmp(iso, dial_plan->iso_country_code)==0) {
395                         return atoi(dial_plan->ccc);
396                 }
397         }
398         return -1;
399 }
400
401 static void lookup_dial_plan(const char *ccc, dial_plan_t *plan){
402         int i;
403         for(i=0;dial_plans[i].country!=NULL;++i){
404                 if (strcmp(ccc,dial_plans[i].ccc)==0){
405                         *plan=dial_plans[i];
406                         return;
407                 }
408         }
409         /*else return a generic "most common" dial plan*/
410         *plan=most_common_dialplan;
411         strcpy(plan->ccc,ccc);
412 }
413
414 static bool_t is_a_phone_number(const char *username){
415         const char *p;
416         for(p=username;*p!='\0';++p){
417                 if (isdigit(*p) || 
418                     *p==' ' ||
419                     *p=='.' ||
420                     *p=='-' ||
421                     *p==')' ||
422                         *p=='(' ||
423                         *p=='/' ||
424                         *p=='+') continue;
425                 else return FALSE;
426         }
427         return TRUE;
428 }
429
430 static char *flatten_number(const char *number){
431         char *result=ms_malloc0(strlen(number)+1);
432         char *w=result;
433         const char *r;
434         for(r=number;*r!='\0';++r){
435                 if (*r=='+' || isdigit(*r)){
436                         *w++=*r;
437                 }
438         }
439         *w++='\0';
440         return result;
441 }
442
443 static void replace_plus(const char *src, char *dest, size_t destlen, const char *icp){
444         int i=0;
445         
446         if (icp && src[0]=='+' && (destlen>(i=strlen(icp))) ){
447                 src++;
448                 strcpy(dest,icp);
449         }
450         
451         for(;(i<destlen-1) && *src!='\0';++i){
452                 dest[i]=*src;
453                 src++;
454         }
455         dest[i]='\0';
456 }
457
458
459 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
460         int numlen;
461         if (is_a_phone_number(username)){
462                 char *flatten;
463                 flatten=flatten_number(username);
464                 ms_message("Flattened number is '%s'",flatten);
465                 
466                 if (proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
467                         /*no prefix configured, nothing else to do*/
468                         strncpy(result,flatten,result_len);
469                         ms_free(flatten);
470                         return 0;
471                 }else{
472                         dial_plan_t dialplan;
473                         lookup_dial_plan(proxy->dial_prefix,&dialplan);
474                         ms_message("Using dialplan '%s'",dialplan.country);
475                         if (flatten[0]=='+' || strstr(flatten,dialplan.icp)==flatten){
476                                 /* the number has international prefix or +, so nothing to do*/
477                                 ms_message("Prefix already present.");
478                                 /*eventually replace the plus*/
479                                 replace_plus(flatten,result,result_len,proxy->dial_escape_plus ? dialplan.icp : NULL);
480                                 ms_free(flatten);
481                                 return 0;
482                         }else{
483                                 int i=0;
484                                 int skip;
485                                 numlen=strlen(flatten);
486                                 /*keep at most national number significant digits */
487                                 skip=numlen-dialplan.nnl;
488                                 if (skip<0) skip=0;
489                                 /*first prepend internation calling prefix or +*/
490                                 if (proxy->dial_escape_plus){
491                                         strncpy(result,dialplan.icp,result_len);
492                                         i+=strlen(dialplan.icp);
493                                 }else{
494                                         strncpy(result,"+",result_len);
495                                         i+=1;
496                                 }
497                                 /*add prefix*/
498                                 if (result_len-i>strlen(dialplan.ccc)){
499                                         strcpy(result+i,dialplan.ccc);
500                                         i+=strlen(dialplan.ccc);
501                                 }
502                                 /*add user digits */
503                                 strncpy(result+i,flatten+skip,result_len-i-1);
504                                 ms_free(flatten);
505                         }
506                 }
507         }else strncpy(result,username,result_len);
508         return 0;
509 }
510
511 /**
512  * Commits modification made to the proxy configuration.
513 **/
514 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
515 {
516         if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
517         obj->commit=TRUE;
518         linphone_proxy_config_write_all_to_config_file(obj->lc);
519         return 0;
520 }
521
522 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
523 {
524         if (cfg->realm!=NULL) {
525                 ms_free(cfg->realm);
526                 cfg->realm=NULL;
527         }
528         if (realm!=NULL) cfg->realm=ms_strdup(realm);
529 }
530
531 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
532                                LinphoneOnlineStatus presence_mode){
533         int err;
534         SalOp *op=sal_op_new(proxy->lc->sal);
535         err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
536             linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
537         if (proxy->publish_op!=NULL)
538                 sal_op_release(proxy->publish_op);
539         proxy->publish_op=op;
540         return err;
541 }
542
543 /**
544  * Returns the route set for this proxy configuration.
545 **/
546 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
547         return obj->reg_route;
548 }
549
550 /**
551  * Returns the SIP identity that belongs to this proxy configuration.
552  *
553  * The SIP identity is a SIP address (Display Name <sip:username@@domain> )
554 **/
555 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
556         return obj->reg_identity;
557 }
558
559 /**
560  * Returns TRUE if PUBLISH request is enabled for this proxy.
561 **/
562 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
563         return obj->publish;
564 }
565
566 /**
567  * Returns the proxy's SIP address.
568 **/
569 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
570         return obj->reg_proxy;
571 }
572
573 /**
574  * Returns the duration of registration.
575 **/
576 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
577         return obj->expires;
578 }
579
580 /**
581  * Returns TRUE if registration to the proxy is enabled.
582 **/
583 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
584         return obj->reg_sendregister;
585 }
586
587 /**
588  * Set optional contact parameters that will be added to the contact information sent in the registration.
589  * @param obj the proxy config object
590  * @param contact_params a string contaning the additional parameters in text form, like "myparam=something;myparam2=something_else"
591  *
592  * 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.
593  * 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>.
594 **/
595 void linphone_proxy_config_set_contact_parameters(LinphoneProxyConfig *obj, const char *contact_params){
596         if (obj->contact_params) {
597                 ms_free(obj->contact_params);
598                 obj->contact_params=NULL;
599         }
600         if (contact_params){
601                 obj->contact_params=ms_strdup(contact_params);
602         }
603 }
604
605 /**
606  * Returns previously set contact parameters.
607 **/
608 const char *linphone_proxy_config_get_contact_parameters(const LinphoneProxyConfig *obj){
609         return obj->contact_params;
610 }
611
612 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
613         return obj->lc;
614 }
615
616 /**
617  * Add a proxy configuration.
618  * This will start registration on the proxy, if registration is enabled.
619 **/
620 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
621         if (!linphone_proxy_config_check(lc,cfg)) {
622                 return -1;
623         }
624         if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
625                 ms_warning("ProxyConfig already entered, ignored.");
626                 return 0;
627         }
628         lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
629         linphone_proxy_config_apply(cfg,lc);
630         return 0;
631 }
632
633 /**
634  * Removes a proxy configuration.
635  *
636  * LinphoneCore will then automatically unregister and place the proxy configuration
637  * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
638 **/
639 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
640         /* check this proxy config is in the list before doing more*/
641         if (ms_list_find(lc->sip_conf.proxies,cfg)==NULL){
642                 ms_error("linphone_core_remove_proxy_config: LinphoneProxyConfig %p is not known by LinphoneCore (programming error?)",cfg);
643                 return;
644         }
645         lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
646         /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
647         lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
648         cfg->deletion_date=ms_time(NULL);
649         if (cfg->state==LinphoneRegistrationOk){
650                 /* this will unREGISTER */
651                 linphone_proxy_config_edit(cfg);
652         }
653         if (lc->default_proxy==cfg){
654                 lc->default_proxy=NULL;
655         }
656         linphone_proxy_config_write_all_to_config_file(lc);
657 }
658 /**
659  * Erase all proxies from config.
660  *
661  * @ingroup proxy
662 **/
663 void linphone_core_clear_proxy_config(LinphoneCore *lc){
664         MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
665         MSList* copy=list;
666         for(;list!=NULL;list=list->next){
667                 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
668         }
669         ms_list_free(copy);
670         linphone_proxy_config_write_all_to_config_file(lc);
671 }
672 /**
673  * Sets the default proxy.
674  *
675  * This default proxy must be part of the list of already entered LinphoneProxyConfig.
676  * Toggling it as default will make LinphoneCore use the identity associated with
677  * the proxy configuration in all incoming and outgoing calls.
678 **/
679 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
680         /* check if this proxy is in our list */
681         if (config!=NULL){
682                 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
683                         ms_warning("Bad proxy address: it is not in the list !");
684                         lc->default_proxy=NULL;
685                         return ;
686                 }
687         }
688         lc->default_proxy=config;
689         if (linphone_core_ready(lc))
690                 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
691 }       
692
693 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
694         if (index<0) linphone_core_set_default_proxy(lc,NULL);
695         else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
696 }
697
698 /**
699  * Returns the default proxy configuration, that is the one used to determine the current identity.
700 **/
701 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
702         int pos=-1;
703         if (config!=NULL) *config=lc->default_proxy;
704         if (lc->default_proxy!=NULL){
705                 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
706         }
707         return pos;
708 }
709
710 /**
711  * Returns an unmodifiable list of entered proxy configurations.
712 **/
713 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
714         return lc->sip_conf.proxies;
715 }
716
717 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
718 {
719         char key[50];
720
721         sprintf(key,"proxy_%i",index);
722         lp_config_clean_section(config,key);
723         if (obj==NULL){
724                 return;
725         }
726         if (obj->type!=NULL){
727                 lp_config_set_string(config,key,"type",obj->type);
728         }
729         if (obj->reg_proxy!=NULL){
730                 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
731         }
732         if (obj->reg_route!=NULL){
733                 lp_config_set_string(config,key,"reg_route",obj->reg_route);
734         }
735         if (obj->reg_identity!=NULL){
736                 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
737         }
738         if (obj->contact_params!=NULL){
739                 lp_config_set_string(config,key,"contact_params",obj->contact_params);
740         }
741         lp_config_set_int(config,key,"reg_expires",obj->expires);
742         lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
743         lp_config_set_int(config,key,"publish",obj->publish);
744         lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
745         lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
746 }
747
748
749
750 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
751 {
752         const char *tmp;
753         const char *identity;
754         const char *proxy;
755         LinphoneProxyConfig *cfg;
756         char key[50];
757         
758         sprintf(key,"proxy_%i",index);
759
760         if (!lp_config_has_section(config,key)){
761                 return NULL;
762         }
763
764         cfg=linphone_proxy_config_new();
765
766         identity=lp_config_get_string(config,key,"reg_identity",NULL);  
767         proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
768         
769         linphone_proxy_config_set_identity(cfg,identity);
770         linphone_proxy_config_set_server_addr(cfg,proxy);
771         
772         tmp=lp_config_get_string(config,key,"reg_route",NULL);
773         if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
774
775         linphone_proxy_config_set_contact_parameters(cfg,lp_config_get_string(config,key,"contact_parameters",NULL));
776         
777         linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",LP_CONFIG_DEFAULT_INT(config,"reg_expires",600)));
778         linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
779         
780         linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
781
782         linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",LP_CONFIG_DEFAULT_INT(config,"dial_escape_plus",0)));
783         linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",LP_CONFIG_DEFAULT_STRING(config,"dial_prefix",NULL)));
784         
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);
788
789         return cfg;
790 }
791
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);
796         unsigned int caps;
797         if (!ss) return ;
798         ssc=sip_setup_context_new(ss,cfg);
799         cfg->ssctx=ssc;
800         if (cfg->reg_identity==NULL){
801                 ms_error("Invalid identity for this proxy configuration.");
802                 return;
803         }
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);
810                                 ms_free(tmp);
811                         }
812                         return;
813                 }
814         }
815         if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
816                 char proxy[256];
817                 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
818                         linphone_proxy_config_set_server_addr(cfg,proxy);
819                 }else{
820                         ms_error("Could not retrieve proxy uri !");
821                 }
822         }
823         
824 }
825
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);
830         }
831         return NULL;
832 }
833
834 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
835         LinphoneCore *lc=cfg->lc;
836         if (cfg->commit){
837                 if (cfg->type && cfg->ssctx==NULL){
838                         linphone_proxy_config_activate_sip_setup(cfg);
839                 }
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);
844                 }
845                 cfg->commit=FALSE;
846         }
847 }
848
849 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
850         if (cfg->type)
851                 ms_free(cfg->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");
856         }
857 }
858
859 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
860         return cfg->ssctx;
861 }
862
863 /**
864  * @}
865 **/
866
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;
872         if (!ss){
873                 return NULL;
874         }
875         if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
876                 ms_error("%s cannot manage accounts.",type);
877                 return NULL;
878         }
879         obj=ms_new0(LinphoneAccountCreator,1);
880         cfg=linphone_proxy_config_new();
881         ssctx=sip_setup_context_new(ss,cfg);
882         obj->lc=core;
883         obj->ssctx=ssctx;
884         set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
885         cfg->lc=core;
886         return obj;
887 }
888
889 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
890         set_string(&obj->username,username);
891 }
892
893 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
894         set_string(&obj->password,password);
895 }
896
897 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
898         set_string(&obj->domain,domain);
899 }
900
901 void linphone_account_creator_set_route(LinphoneAccountCreator *obj, const char *route) {
902         set_string(&obj->route,route);
903 }
904
905 void linphone_account_creator_set_email(LinphoneAccountCreator *obj, const char *email) {
906         set_string(&obj->email,email);
907 }
908
909 void linphone_account_creator_set_suscribe(LinphoneAccountCreator *obj, int suscribe) {
910         obj->suscribe = suscribe;
911 }
912
913 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
914         return obj->username;
915 }
916
917 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
918         return obj->domain;
919 }
920
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);
925         ms_free(uri);
926         return err;
927 }
928
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);
932         return err;
933 }
934
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);
939         ms_free(uri);
940         if (err==0) {
941                 obj->succeeded=TRUE;
942                 return sip_setup_context_get_proxy_config(ssctx);
943         }
944         return NULL;
945 }
946
947 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
948         if (obj->username)
949                 ms_free(obj->username);
950         if (obj->password)
951                 ms_free(obj->password);
952         if (obj->domain)
953                 ms_free(obj->domain);
954         if (!obj->succeeded){
955                 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
956         }
957 }
958
959 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
960         cr->user_data=ud;
961 }
962
963 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
964         return cr->user_data;
965 }
966
967 void linphone_proxy_config_set_state(LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const char *message){
968         LinphoneCore *lc=cfg->lc;
969         cfg->state=state;
970         if (lc && lc->vtable.registration_state_changed){
971                 lc->vtable.registration_state_changed(lc,cfg,state,message);
972         }
973 }
974
975 LinphoneRegistrationState linphone_proxy_config_get_state(const LinphoneProxyConfig *cfg){
976         return cfg->state;
977 }
978
979  const char *linphone_registration_state_to_string(LinphoneRegistrationState cs){
980          switch(cs){
981                 case LinphoneRegistrationCleared:
982                          return "LinphoneRegistrationCleared";
983                 break;
984                 case LinphoneRegistrationNone:
985                          return "LinphoneRegistrationNone";
986                 break;
987                 case LinphoneRegistrationProgress:
988                         return "LinphoneRegistrationProgress";
989                 break;
990                 case LinphoneRegistrationOk:
991                          return "LinphoneRegistrationOk";
992                 break;
993                 case LinphoneRegistrationFailed:
994                          return "LinphoneRegistrationFailed";
995                 break;
996          }
997          return NULL;
998  }
999
1000 LinphoneReason linphone_proxy_config_get_error(const LinphoneProxyConfig *cfg) {
1001         return cfg->error;
1002 }
1003
1004 void linphone_proxy_config_set_error(LinphoneProxyConfig *cfg,LinphoneReason error) {
1005         cfg->error = error;
1006 }
1007
1008