]> sjero.net Git - linphone/blobdiff - coreapi/proxy.c
- do not register outside of tunnel when tunnel is activated but not yet connected.
[linphone] / coreapi / proxy.c
index ff13f839e4545f99245f2642583ec0ec83564757..05258c1d1f26bdf2212ca9d6083b48f2370e58b1 100644 (file)
@@ -24,22 +24,29 @@ Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
 #include "private.h"
 #include "mediastreamer2/mediastream.h"
 
-
 #include <ctype.h>
 
 
 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
        MSList *elem;
        int i;
+       if (!linphone_core_ready(lc)) return;
+       
        for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
                linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
        }
+       /*to ensure removed configs are erased:*/
+       linphone_proxy_config_write_to_config_file(lc->config,NULL,i);
+       lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
 }
 
-void linphone_proxy_config_init(LinphoneProxyConfig *obj){
+static void linphone_proxy_config_init(LinphoneCore* lc,LinphoneProxyConfig *obj){
        memset(obj,0,sizeof(LinphoneProxyConfig));
-       obj->expires=3600;
+       obj->magic=linphone_proxy_config_magic;
+       obj->expires=LP_CONFIG_DEFAULT_INT((lc?lc->config:NULL),"reg_expires",3600);
+       obj->dial_prefix=ms_strdup(LP_CONFIG_DEFAULT_STRING((lc?lc->config:NULL),"dial_prefix",'\0'));
+       obj->dial_escape_plus=LP_CONFIG_DEFAULT_INT((lc?lc->config:NULL),"dial_escape_plus",0);
 }
 
 /**
@@ -48,15 +55,21 @@ void linphone_proxy_config_init(LinphoneProxyConfig *obj){
 **/
 
 /**
- * Creates an empty proxy config.
+ * @deprecated, use #linphone_core_create_proxy_config instead
+ *Creates an empty proxy config.
 **/
-LinphoneProxyConfig *linphone_proxy_config_new(){
+LinphoneProxyConfig *linphone_proxy_config_new() {
+       return linphone_core_create_proxy_config(NULL);
+}
+LinphoneProxyConfig * linphone_core_create_proxy_config(LinphoneCore *lc) {
        LinphoneProxyConfig *obj=NULL;
        obj=ms_new(LinphoneProxyConfig,1);
-       linphone_proxy_config_init(obj);
+       linphone_proxy_config_init(lc,obj);
        return obj;
 }
 
+
+
 /**
  * Destroys a proxy config.
  * 
@@ -79,7 +92,7 @@ void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
  * Returns a boolean indicating that the user is sucessfully registered on the proxy.
 **/
 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
-       return obj->registered;
+       return obj->state == LinphoneRegistrationOk;
 }
 
 /**
@@ -106,7 +119,7 @@ int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *
                if (addr==NULL)
                        addr=linphone_address_new(server_addr);
                if (addr){
-                       obj->reg_proxy=linphone_address_as_string_uri_only(addr);
+                       obj->reg_proxy=linphone_address_as_string(addr);
                        linphone_address_destroy(addr);
                }else{
                        ms_warning("Could not parse %s",server_addr);
@@ -167,16 +180,20 @@ int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
                obj->reg_route=NULL;
        }
        if (route!=NULL){
-               LinphoneAddress *addr;
+               SalAddress *addr;
+               char *tmp;
                /*try to prepend 'sip:' */
                if (strstr(route,"sip:")==NULL){
-                       obj->reg_route=ms_strdup_printf("sip:%s",route);
-               }else obj->reg_route=ms_strdup(route);
-               addr=linphone_address_new(obj->reg_route);
-               if (addr==NULL){
-                       ms_free(obj->reg_route);
-                       obj->reg_route=NULL;
-               }else linphone_address_destroy(addr);
+                       tmp=ms_strdup_printf("sip:%s",route);
+               }else tmp=ms_strdup(route);
+               addr=sal_address_new(tmp);
+               if (addr!=NULL){
+                       sal_address_destroy(addr);
+               }else{
+                       ms_free(tmp);
+                       tmp=NULL;
+               }
+               obj->reg_route=tmp;
        }
        return 0;
 }
@@ -208,7 +225,7 @@ void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
  * Sets the registration expiration time in seconds.
 **/
 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
-       if (val<=0) val=600;
+       if (val<0) val=600;
        obj->expires=val;
 }
 
@@ -227,9 +244,8 @@ void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
        if (obj->reg_sendregister){
                /* unregister */
-               if (obj->registered) {
+               if (obj->state != LinphoneRegistrationNone && obj->state != LinphoneRegistrationCleared) {
                        sal_unregister(obj->op);
-                       obj->registered=FALSE;
                }
        }
 }
@@ -247,22 +263,46 @@ static char *guess_contact_for_register(LinphoneProxyConfig *obj){
        if (proxy==NULL) return NULL;
        host=linphone_address_get_domain (proxy);
        if (host!=NULL){
-               LinphoneAddress *contact;
-               char localip[LINPHONE_IPADDR_SIZE];
+               int localport = -1;
+               char localip_tmp[LINPHONE_IPADDR_SIZE] = {'\0'};
+               const char *localip = NULL;
+               char *tmp;
                LCSipTransports tr;
+               LinphoneAddress *contact;
                
-               linphone_core_get_local_ip(obj->lc,host,localip);
                contact=linphone_address_new(obj->reg_identity);
-               linphone_address_set_domain (contact,localip);
-               linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
+#ifdef BUILD_UPNP
+               if (obj->lc->upnp != NULL && linphone_core_get_firewall_policy(obj->lc)==LinphonePolicyUseUpnp &&
+                       linphone_upnp_context_get_state(obj->lc->upnp) == LinphoneUpnpStateOk) {
+                       localip = linphone_upnp_context_get_external_ipaddress(obj->lc->upnp);
+                       localport = linphone_upnp_context_get_external_port(obj->lc->upnp);
+               }
+#endif //BUILD_UPNP            
+               if(localip == NULL) {
+                       localip = localip_tmp;
+                       linphone_core_get_local_ip(obj->lc,host,localip_tmp);
+               }
+               if(localport == -1) {
+                       localport = linphone_core_get_sip_port(obj->lc);
+               }
+               linphone_address_set_port_int(contact,localport);
+               linphone_address_set_domain(contact,localip);
                linphone_address_set_display_name(contact,NULL);
                
                linphone_core_get_sip_transports(obj->lc,&tr);
-               if (tr.udp_port <= 0 && tr.tcp_port>0) {
-                       sal_address_set_param(contact,"transport","TCP");
+               if (tr.udp_port <= 0) {
+                       if (tr.tcp_port>0) {
+                               sal_address_set_param(contact,"transport","tcp");
+                       } else if (tr.tls_port>0) {
+                               sal_address_set_param(contact,"transport","tls");
+                       }
                }
-               ret=linphone_address_as_string(contact);
+               tmp=linphone_address_as_string_uri_only(contact);
+               if (obj->contact_params)
+                       ret=ms_strdup_printf("<%s;%s>",tmp,obj->contact_params);
+               else ret=ms_strdup_printf("<%s>",tmp);
                linphone_address_destroy(contact);
+               ms_free(tmp);
        }
        linphone_address_destroy (proxy);
        return ret;
@@ -292,15 +332,17 @@ static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
 **/
 void linphone_proxy_config_refresh_register(LinphoneProxyConfig *obj){
        if (obj->reg_sendregister && obj->op){
-               obj->registered=FALSE;
-               sal_register_refresh(obj->op,obj->expires);
+               if (sal_register_refresh(obj->op,obj->expires) == 0) {
+                       linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress, "Refresh registration");
+               }
        }
 }
 
 
 /**
  * Sets a dialing prefix to be automatically prepended when inviting a number with 
- * #linphone_core_invite.
+ * linphone_core_invite();
+ * This dialing prefix shall usually be the country code of the country where the user is living.
  *
 **/
 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
@@ -321,7 +363,7 @@ const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg
 }
 
 /**
- * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
+ * Sets whether liblinphone should replace "+" by international calling prefix in dialed numbers (passed to
  * #linphone_core_invite ).
  *
 **/
@@ -337,13 +379,304 @@ void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t
 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
        return cfg->dial_escape_plus;
 }
+/*
+ * http://en.wikipedia.org/wiki/Telephone_numbering_plan
+ * http://en.wikipedia.org/wiki/Telephone_numbers_in_Europe
+ */
+typedef struct dial_plan{
+       const char *country;
+       const char* iso_country_code; /* ISO 3166-1 alpha-2 code, ex: FR for France*/
+       char  ccc[8]; /*country calling code*/
+       int nnl; /*maximum national number length*/
+       const char * icp; /*international call prefix, ex: 00 in europe*/
+       
+}dial_plan_t;
+
+/* TODO: fill with information for all countries over the world*/
+
+static dial_plan_t const dial_plans[]={
+       {"Afghanistan"                  ,"AF"           , "93"      , 9         , "00"  },
+       {"Albania"                      ,"AL"           , "355"     , 9         , "00"  },
+       {"Algeria"                      ,"DZ"           , "213"     , 9         , "00"  },
+       {"American Samoa"               ,"AS"           , "1"       , 10        , "011" },
+       {"Andorra"                      ,"AD"           , "376"     , 6         , "00"  },
+       {"Angola"                       ,"AO"           , "244"     , 9         , "00"  },
+       {"Anguilla"                     ,"AI"           , "1"       , 10        , "011" },
+       {"Antigua and Barbuda"          ,"AG"           , "1"       , 10        , "011" },
+       {"Argentina"                    ,"AR"           , "54"      , 10        , "00"  },
+       {"Armenia"                      ,"AM"           , "374"     , 8         , "00"  },
+       {"Aruba"                        ,"AW"           , "297"     , 7         , "011" },
+       {"Australia"                    ,"AU"           , "61"      , 9     , "0011"},
+       {"Austria"                      ,"AT"           , "43"      , 10        , "00"  },
+       {"Azerbaijan"                   ,"AZ"       , "994"     , 9             , "00"  },
+       {"Bahamas"                      ,"BS"           , "1"       , 10    , "011"     },
+       {"Bahrain"                      ,"BH"           , "973"     , 8     , "00"  },
+       {"Bangladesh"                   ,"BD"           , "880"     , 10    , "00"  },
+       {"Barbados"                     ,"BB"           , "1"       , 10    , "011"     },
+       {"Belarus"                      ,"BY"           , "375"     , 9     , "00"  },
+       {"Belgium"                      ,"BE"           , "32"      , 9     , "00"  },
+       {"Belize"                       ,"BZ"           , "501"     , 7     , "00"  },
+       {"Benin"                        ,"BJ"           , "229"     , 8     , "00"      },
+       {"Bermuda"                      ,"BM"           , "1"       , 10    , "011" },
+       {"Bhutan"                       ,"BT"           , "975"     , 8     , "00"  },
+       {"Bolivia"                      ,"BO"           , "591"     , 8     , "00"      },
+       {"Bosnia and Herzegovina"       ,"BA"           , "387"     , 8     , "00"  },
+       {"Botswana"                     ,"BW"           , "267"     , 8     , "00"  },
+       {"Brazil"                       ,"BR"           , "55"      , 10        , "00"  },
+       {"Brunei Darussalam"            ,"BN"           , "673"     , 7         , "00"  },
+       {"Bulgaria"                     ,"BG"           , "359"     , 9         , "00"  },
+       {"Burkina Faso"                 ,"BF"           , "226"     , 8         , "00"  },
+       {"Burundi"                      ,"BI"           , "257"     , 8     , "011" },
+       {"Cambodia"                     ,"KH"           , "855"     , 9         , "00"  },
+       {"Cameroon"                     ,"CM"           , "237"     , 8         , "00"  },
+       {"Canada"                       ,"CA"           , "1"       , 10        , "011" },
+       {"Cape Verde"                   ,"CV"           , "238"     , 7         , "00"  },
+       {"Cayman Islands"               ,"KY"           , "1"       , 10        , "011" },
+       {"Central African Republic"     ,"CF"           , "236"     , 8     , "00"  },
+       {"Chad"                         ,"TD"           , "235"     , 8         , "00"  },
+       {"Chile"                        ,"CL"           , "56"      , 9     , "00"  },
+       {"China"                        ,"CN"           , "86"      , 11        , "00"  },
+       {"Colombia"                     ,"CO"       , "57"      , 10    , "00"  },
+       {"Comoros"                      ,"KM"           , "269"     , 7     , "00"      },
+       {"Congo"                        ,"CG"           , "242"     , 9         , "00"  },
+       {"Congo Democratic Republic"    ,"CD"           , "243"     , 9         , "00"  },
+       {"Cook Islands"                 ,"CK"           , "682"     , 5         , "00"  },
+       {"Costa Rica"                   ,"CR"           , "506"     , 8     , "00"      },
+       {"C�te d'Ivoire"                  ,"AD"               , "225"     , 8     , "00"  },
+       {"Croatia"                      ,"HR"           , "385"     , 9         , "00"  },
+       {"Cuba"                         ,"CU"           , "53"      , 8     , "119" },
+       {"Cyprus"                       ,"CY"           , "357"     , 8     , "00"      },
+       {"Czech Republic"               ,"CZ"           , "420"     , 9     , "00"  },
+       {"Denmark"                      ,"DK"           , "45"      , 8         , "00"  },
+       {"Djibouti"                     ,"DJ"           , "253"     , 8         , "00"  },
+       {"Dominica"                     ,"DM"           , "1"       , 10        , "011" },
+       {"Dominican Republic"           ,"DO"           , "1"       , 10        , "011" },
+       {"Ecuador"                      ,"EC"       , "593"     , 9             , "00"  },
+       {"Egypt"                        ,"EG"           , "20"      , 10        , "00"  },
+       {"El Salvador"                  ,"SV"           , "503"     , 8         , "00"  },
+       {"Equatorial Guinea"            ,"GQ"           , "240"     , 9         , "00"  },
+       {"Eritrea"                      ,"ER"           , "291"     , 7         , "00"  },
+       {"Estonia"                      ,"EE"           , "372"     , 8     , "00"      },
+       {"Ethiopia"                     ,"ET"           , "251"     , 9     , "00"  },
+       {"Falkland Islands"                 ,"FK"               , "500"     , 5         , "00"  },
+       {"Faroe Islands"                    ,"FO"               , "298"     , 6     , "00"  },
+       {"Fiji"                         ,"FJ"           , "679"     , 7     , "00"      },
+       {"Finland"                      ,"FI"           , "358"     , 9     , "00"  },
+       {"France"                       ,"FR"           , "33"      , 9         , "00"  },
+       {"French Guiana"                                ,"GF"           , "594"     , 9         , "00"  },
+       {"French Polynesia"             ,"PF"           , "689"     , 6     , "00"  },
+       {"Gabon"                        ,"GA"           , "241"     , 8     , "00"  },
+       {"Gambia"                       ,"GM"       , "220"     , 7             , "00"  },
+       {"Georgia"                      ,"GE"           , "995"     , 9     , "00"      },
+       {"Germany"                      ,"DE"           , "49"      , 11        , "00"  },
+       {"Ghana"                        ,"GH"           , "233"     , 9         , "00"  },
+       {"Gibraltar"                    ,"GI"           , "350"     , 8         , "00"  },
+       {"Greece"                       ,"GR"           , "30"      ,10     , "00"      },
+       {"Greenland"                    ,"GL"           , "299"     , 6         , "00"  },
+       {"Grenada"                      ,"GD"           , "1"       , 10        , "011" },
+       {"Guadeloupe"                   ,"GP"           , "590"     , 9     , "00"  },
+       {"Guam"                         ,"GU"           , "1"       , 10        , "011" },
+       {"Guatemala"                    ,"GT"           , "502"     , 8     , "00"  },
+       {"Guinea"                       ,"GN"           , "224"     , 8         , "00"  },
+       {"Guinea-Bissau"                                ,"GW"           , "245"     , 7         , "00"  },
+       {"Guyana"                       ,"GY"           , "592"     , 7     , "001" },
+       {"Haiti"                        ,"HT"           , "509"     , 8     , "00"  },
+       {"Honduras"                     ,"HN"       , "504"     , 8             , "00"  },
+       {"Hong Kong"                    ,"HK"           , "852"     , 8     , "001"     },
+       {"Hungary"                      ,"HU"           , "36"      , 9     , "00"  },
+       {"Iceland"                      ,"IS"           , "354"     , 9     , "00"  },
+       {"India"                        ,"IN"           , "91"      , 10    , "00"  },
+       {"Indonesia"                    ,"ID"           , "62"      , 10        , "001" },
+       {"Iran"                         ,"IR"           , "98"      , 10        , "00"  },
+       {"Iraq"                         ,"IQ"           , "964"     , 10        , "00"  },
+       {"Ireland"                      ,"IE"           , "353"     , 9         , "00"  },
+       {"Israel"                       ,"IL"           , "972"     , 9     , "00"      },
+       {"Italy"                        ,"IT"           , "39"      , 10        , "00"  },
+       {"Jamaica"                      ,"JM"           , "1"       , 10        , "011" },
+       {"Japan"                        ,"JP"           , "81"      , 10        , "010" },
+       {"Jordan"                       ,"JO"           , "962"     , 9     , "00"      },
+       {"Kazakhstan"                   ,"KZ"           , "7"       , 10    , "00"  },
+       {"Kenya"                        ,"KE"           , "254"     , 9         , "000" },
+       {"Kiribati"                     ,"KI"           , "686"     , 5         , "00"  },
+       {"Korea, North"                 ,"KP"           , "850"     , 12        , "99"  },
+       {"Korea, South"                 ,"KR"       , "82"      , 12    , "001" },
+       {"Kuwait"                       ,"KW"           , "965"     , 8     , "00"      },
+       {"Kyrgyzstan"                   ,"KG"           , "996"     , 9     , "00"  },
+       {"Laos"                         ,"LA"           , "856"     , 10    , "00"  },
+       {"Latvia"                       ,"LV"           , "371"     , 8     , "00"      },
+       {"Lebanon"                      ,"LB"           , "961"     , 7     , "00"      },
+       {"Lesotho"                      ,"LS"           , "266"     , 8         , "00"  },
+       {"Liberia"                      ,"LR"           , "231"     , 8         , "00"  },
+       {"Libya"                        ,"LY"           , "218"     , 8         , "00"  },
+       {"Liechtenstein"                ,"LI"           , "423"     , 7     , "00"      },
+       {"Lithuania"                    ,"LT"           , "370"     , 8         , "00"  },
+       {"Luxembourg"                   ,"LU"           , "352"     , 9         , "00"  },
+       {"Macau"                        ,"MO"           , "853"     , 8     , "00"  },
+       {"Macedonia"                    ,"MK"           , "389"     , 8     , "00"      },
+       {"Madagascar"                   ,"MG"           , "261"     , 9     , "00"  },
+       {"Malawi"                       ,"MW"           , "265"     , 9         , "00"  },
+       {"Malaysia"                     ,"MY"           , "60"      , 9         , "00"  },
+       {"Maldives"                     ,"MV"           , "960"     , 7     , "00"  },
+       {"Mali"                         ,"ML"           , "223"     , 8     , "00"  },
+       {"Malta"                        ,"MT"       , "356"     , 8             , "00"  },
+       {"Marshall Islands"                             ,"MH"           , "692"     , 7     , "011"     },
+       {"Martinique"                   ,"MQ"           , "596"     , 9     , "00"  },
+       {"Mauritania"                   ,"MR"           , "222"     , 8     , "00"  },
+       {"Mauritius"                    ,"MU"           , "230"     , 7     , "00"      },
+       {"Mayotte Island"               ,"YT"           , "262"     , 9     , "00"      },
+       {"Mexico"                       ,"MX"           , "52"      , 10        , "00"  },
+       {"Micronesia"                   ,"FM"           , "691"     , 7         , "011" },
+       {"Moldova"                      ,"MD"           , "373"     , 8         , "00"  },
+       {"Monaco"                       ,"MC"           , "377"     , 8     , "00"      },
+       {"Mongolia"                     ,"MN"           , "976"     , 8     , "001" },
+       {"Montenegro"                   ,"ME"           , "382"     , 8         , "00"  },
+       {"Montserrat"                   ,"MS"           , "664"     , 10        , "011" },
+       {"Morocco"                      ,"MA"           , "212"     , 9     , "00"      },
+       {"Mozambique"                   ,"MZ"           , "258"     , 9     , "00"  },
+       {"Myanmar"                      ,"MM"           , "95"      , 8         , "00"  },
+       {"Namibia"                      ,"NA"           , "264"     , 9         , "00"  },
+       {"Nauru"                        ,"NR"           , "674"     , 7     , "00"  },
+       {"Nepal"                        ,"NP"           , "43"      , 10        , "00"  },
+       {"Netherlands"                  ,"NL"       , "31"      , 9             , "00"  },
+       {"New Caledonia"                                ,"NC"           , "687"     , 6     , "00"      },
+       {"New Zealand"                  ,"NZ"           , "64"      , 10        , "00"  },
+       {"Nicaragua"                    ,"NI"           , "505"     , 8     , "00"  },
+       {"Niger"                        ,"NE"           , "227"     , 8     , "00"      },
+       {"Nigeria"                      ,"NG"           , "234"     , 10        , "009" },
+       {"Niue"                         ,"NU"           , "683"     , 4         , "00"  },
+       {"Norfolk Island"                   ,"NF"               , "672"     , 5         , "00"  },
+       {"Northern Mariana Islands"         ,"MP"               , "1"       , 10        , "011" },
+       {"Norway"                       ,"NO"           , "47"      , 8     , "00"      },
+       {"Oman"                         ,"OM"           , "968"     , 8         , "00"  },
+       {"Pakistan"                     ,"PK"           , "92"      , 10        , "00"  },
+       {"Palau"                        ,"PW"           , "680"     , 7     , "011" },
+       {"Palestine"                    ,"PS"           , "970"     , 9     , "00"      },
+       {"Panama"                       ,"PA"           , "507"     , 8     , "00"  },
+       {"Papua New Guinea"                 ,"PG"               , "675"     , 8         , "00"  },
+       {"Paraguay"                     ,"PY"           , "595"     , 9         , "00"  },
+       {"Peru"                         ,"PE"           , "51"      , 9     , "00"  },
+       {"Philippines"                  ,"PH"           , "63"      , 10        , "00"  },
+       {"Poland"                       ,"PL"       , "48"      , 9             , "00"  },
+       {"Portugal"                     ,"PT"           , "351"     , 9     , "00"      },
+       {"Puerto Rico"                  ,"PR"           , "1"       , 10        , "011" },
+       {"Qatar"                        ,"QA"           , "974"     , 8     , "00"  },
+       {"R�union Island"                             ,"RE"           , "262"     , 9     , "011"     },
+       {"Romania"                      ,"RO"           , "40"      , 9     , "00"      },
+       {"Russian Federation"           ,"RU"           , "7"       , 10        , "8"   },
+       {"Rwanda"                       ,"RW"           , "250"     , 9         , "00"  },
+       {"Saint Helena"                 ,"SH"           , "290"     , 4         , "00"  },
+       {"Saint Kitts and Nevis"                ,"KN"           , "1"       , 10        , "011" },
+       {"Saint Lucia"                  ,"LC"           , "1"       , 10        , "011" },
+       {"Saint Pierre and Miquelon"    ,"PM"           , "508"     , 6         , "00"  },
+       {"Saint Vincent and the Grenadines","VC"        , "1"       , 10        , "011" },
+       {"Samoa"                        ,"WS"           , "685"     , 7     , "0"       },
+       {"San Marino"                   ,"SM"           , "378"     , 10        , "00"  },
+       {"S�o Tom� and Pr�ncipe"        ,"ST"             , "239"     , 7         , "00"  },
+       {"Saudi Arabia"                 ,"SA"           , "966"     , 9         , "00"  },
+       {"Senegal"                      ,"SN"           , "221"     , 9     , "00"  },
+       {"Serbia"                       ,"RS"           , "381"     , 9     , "00"  },
+       {"Seychelles"                   ,"SC"       , "248"     , 7             , "00"  },
+       {"Sierra Leone"                 ,"SL"           , "232"     , 8     , "00"      },
+       {"Singapore"                    ,"SG"           , "65"      , 8     , "001" },
+       {"Slovakia"                     ,"SK"           , "421"     , 9     , "00"  },
+       {"Slovenia"                     ,"SI"           , "386"     , 8     , "00"      },
+       {"Solomon Islands"              ,"SB"           , "677"     , 7     , "00"      },
+       {"Somalia"                      ,"SO"           , "252"     , 8         , "00"  },
+       {"South Africa"                 ,"ZA"           , "27"      , 9         , "00"  },
+       {"Spain"                        ,"ES"           , "34"      , 9         , "00"  },
+       {"Sri Lanka"                    ,"LK"           , "94"      , 9     , "00"      },
+       {"Sudan"                        ,"SD"           , "249"     , 9         , "00"  },
+       {"Suriname"                     ,"SR"           , "597"     , 7         , "00"  },
+       {"Swaziland"                    ,"SZ"           , "268"     , 8     , "00"  },
+       {"Sweden"                       ,"SE"           , "1"       , 9     , "00"      },
+       {"Switzerland"                  ,"XK"           , "41"      , 9         , "00"  },
+       {"Syria"                        ,"SY"           , "963"     , 9         , "00"  },
+       {"Taiwan"                       ,"TW"           , "886"     , 9         , "810" },
+       {"Tajikistan"                   ,"TJ"           , "992"     , 9     , "002" },
+       {"Tanzania"                     ,"TZ"           , "255"     , 9     , "000" },
+       {"Thailand"                     ,"TH"       , "66"      , 9             , "001" },
+       {"Togo"                         ,"TG"           , "228"     , 8     , "00"      },
+       {"Tokelau"                      ,"TK"           , "690"     , 4     , "00"  },
+       {"Tonga"                        ,"TO"           , "676"     , 5     , "00"  },
+       {"Trinidad and Tobago"                  ,"TT"           , "1"       , 10    , "011"     },
+       {"Tunisia"                      ,"TN"           , "216"     , 8     , "00"      },
+       {"Turkey"                       ,"TR"           , "90"      , 10        , "00"  },
+       {"Turkmenistan"                 ,"TM"           , "993"     , 8         , "00"  },
+       {"Turks and Caicos Islands"         ,"TC"               , "1"       , 7         , "0"   },
+       {"Tuvalu"                       ,"TV"           , "688"     , 5     , "00"      },
+       {"Uganda"                       ,"UG"           , "256"     , 9     , "000" },
+       {"Ukraine"                      ,"UA"           , "380"     , 9         , "00"  },
+       {"United Arab Emirates"         ,"AE"           , "971"     , 9     , "00"  },
+       {"United Kingdom"               ,"UK"           , "44"      , 10        , "00"  },
+       {"United States"                ,"US"           , "1"       , 10        , "011" },
+       {"Uruguay"                      ,"UY"           , "598"     , 8         , "00"  },
+       {"Uzbekistan"                   ,"UZ"           , "998"     , 9         , "8"   },
+       {"Vanuatu"                      ,"VU"           , "678"     , 7     , "00"  },
+       {"Venezuela"                    ,"VE"           , "58"      , 10        , "00"  },
+       {"Vietnam"                      ,"VN"           , "84"      , 9     , "00"  },
+       {"Wallis and Futuna"            ,"WF"           , "681"     , 5         , "00"  },
+       {"Yemen"                        ,"YE"           , "967"     , 9     , "00"  },
+       {"Zambia"                       ,"ZM"           , "260"     , 9     , "00"      },
+       {"Zimbabwe"                     ,"ZW"           , "263"     , 9     , "00"  },
+       {NULL                           ,NULL       ,  ""       , 0     , NULL  }
+};
+static dial_plan_t most_common_dialplan={ "generic" ,"", "", 10, "00"};
+
+int linphone_dial_plan_lookup_ccc_from_e164(const char* e164) {
+       dial_plan_t* dial_plan;
+       dial_plan_t* elected_dial_plan=NULL;
+       unsigned int found;
+       unsigned int i=0;
+       if (e164[1]=='1') {
+               /*USA case*/
+               return 1;
+       }
+       do {
+               found=0;
+               i++;
+               for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
+                       if (strncmp(dial_plan->ccc,&e164[1],i) == 0) {
+                               elected_dial_plan=dial_plan;
+                               found++;
+                       }
+               }
+       } while ((found>1 || found==0) && i < sizeof(dial_plan->ccc));
+       if (found==1) {
+               return atoi(elected_dial_plan->ccc);
+       } else {
+               return -1; /*not found */
+       }
+
+}
+int linphone_dial_plan_lookup_ccc_from_iso(const char* iso) {
+       dial_plan_t* dial_plan;
+       for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
+               if (strcmp(iso, dial_plan->iso_country_code)==0) {
+                       return atoi(dial_plan->ccc);
+               }
+       }
+       return -1;
+}
 
+static void lookup_dial_plan(const char *ccc, dial_plan_t *plan){
+       int i;
+       for(i=0;dial_plans[i].country!=NULL;++i){
+               if (strcmp(ccc,dial_plans[i].ccc)==0){
+                       *plan=dial_plans[i];
+                       return;
+               }
+       }
+       /*else return a generic "most common" dial plan*/
+       *plan=most_common_dialplan;
+       strcpy(plan->ccc,ccc);
+}
 
 static bool_t is_a_phone_number(const char *username){
        const char *p;
        for(p=username;*p!='\0';++p){
                if (isdigit(*p) || 
                    *p==' ' ||
+                   *p=='.' ||
                    *p=='-' ||
                    *p==')' ||
                        *p=='(' ||
@@ -367,14 +700,12 @@ static char *flatten_number(const char *number){
        return result;
 }
 
-static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
+static void replace_plus(const char *src, char *dest, size_t destlen, const char *icp){
        int i=0;
        
-       if (escape_plus && src[0]=='+' && destlen>2){
-               dest[0]='0';
-               dest[1]='0';
+       if (icp && src[0]=='+' && (destlen>(i=strlen(icp))) ){
                src++;
-               i=2;
+               strcpy(dest,icp);
        }
        
        for(;(i<destlen-1) && *src!='\0';++i){
@@ -385,37 +716,53 @@ static void copy_result(const char *src, char *dest, size_t destlen, bool_t esca
 }
 
 
-static char *append_prefix(const char *number, const char *prefix){
-       char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
-       strcpy(res,prefix);
-       return strcat(res,number);
-}
-
 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
-       char *flatten;
        int numlen;
        if (is_a_phone_number(username)){
+               char *flatten;
                flatten=flatten_number(username);
                ms_message("Flattened number is '%s'",flatten);
-               numlen=strlen(flatten);
-               if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
-                       ms_message("No need to add a prefix");
-                       /* prefix is already there */
-                       copy_result(flatten,result,result_len,proxy->dial_escape_plus);
+               
+               if (proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
+                       /*no prefix configured, nothing else to do*/
+                       strncpy(result,flatten,result_len);
                        ms_free(flatten);
                        return 0;
-               }else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){
-                       char *prefixed;
-                       int skipped=0;
-                       ms_message("Need to prefix with %s",proxy->dial_prefix);
-                       if (numlen==10){
-                               /*remove initial number before prepending prefix*/
-                               skipped=1;
+               }else{
+                       dial_plan_t dialplan;
+                       lookup_dial_plan(proxy->dial_prefix,&dialplan);
+                       ms_message("Using dialplan '%s'",dialplan.country);
+                       if (flatten[0]=='+' || strstr(flatten,dialplan.icp)==flatten){
+                               /* the number has international prefix or +, so nothing to do*/
+                               ms_message("Prefix already present.");
+                               /*eventually replace the plus*/
+                               replace_plus(flatten,result,result_len,proxy->dial_escape_plus ? dialplan.icp : NULL);
+                               ms_free(flatten);
+                               return 0;
+                       }else{
+                               int i=0;
+                               int skip;
+                               numlen=strlen(flatten);
+                               /*keep at most national number significant digits */
+                               skip=numlen-dialplan.nnl;
+                               if (skip<0) skip=0;
+                               /*first prepend internation calling prefix or +*/
+                               if (proxy->dial_escape_plus){
+                                       strncpy(result,dialplan.icp,result_len);
+                                       i+=strlen(dialplan.icp);
+                               }else{
+                                       strncpy(result,"+",result_len);
+                                       i+=1;
+                               }
+                               /*add prefix*/
+                               if (result_len-i>strlen(dialplan.ccc)){
+                                       strcpy(result+i,dialplan.ccc);
+                                       i+=strlen(dialplan.ccc);
+                               }
+                               /*add user digits */
+                               strncpy(result+i,flatten+skip,result_len-i-1);
+                               ms_free(flatten);
                        }
-                       prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
-                       ms_free(flatten);
-                       copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
-                       ms_free(prefixed);
                }
        }else strncpy(result,username,result_len);
        return 0;
@@ -445,6 +792,7 @@ int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
                               LinphoneOnlineStatus presence_mode){
        int err;
        SalOp *op=sal_op_new(proxy->lc->sal);
+       sal_op_set_route(op,proxy->reg_proxy);
        err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
            linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
        if (proxy->publish_op!=NULL)
@@ -463,7 +811,7 @@ const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
 /**
  * Returns the SIP identity that belongs to this proxy configuration.
  *
- * The SIP identity is a SIP address (Display Name <sip:username@domain> )
+ * The SIP identity is a SIP address (Display Name <sip:username@@domain> )
 **/
 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
        return obj->reg_identity;
@@ -497,6 +845,31 @@ bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
        return obj->reg_sendregister;
 }
 
+/**
+ * Set optional contact parameters that will be added to the contact information sent in the registration.
+ * @param obj the proxy config object
+ * @param contact_params a string contaning the additional parameters in text form, like "myparam=something;myparam2=something_else"
+ *
+ * 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.
+ * 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>.
+**/
+void linphone_proxy_config_set_contact_parameters(LinphoneProxyConfig *obj, const char *contact_params){
+       if (obj->contact_params) {
+               ms_free(obj->contact_params);
+               obj->contact_params=NULL;
+       }
+       if (contact_params){
+               obj->contact_params=ms_strdup(contact_params);
+       }
+}
+
+/**
+ * Returns previously set contact parameters.
+**/
+const char *linphone_proxy_config_get_contact_parameters(const LinphoneProxyConfig *obj){
+       return obj->contact_params;
+}
+
 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
        return obj->lc;
 }
@@ -506,7 +879,9 @@ struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig
  * This will start registration on the proxy, if registration is enabled.
 **/
 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
-       if (!linphone_proxy_config_check(lc,cfg)) return -1;
+       if (!linphone_proxy_config_check(lc,cfg)) {
+               return -1;
+       }
        if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
                ms_warning("ProxyConfig already entered, ignored.");
                return 0;
@@ -523,15 +898,23 @@ int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
  * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
 **/
 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
+       /* check this proxy config is in the list before doing more*/
+       if (ms_list_find(lc->sip_conf.proxies,cfg)==NULL){
+               ms_error("linphone_core_remove_proxy_config: LinphoneProxyConfig %p is not known by LinphoneCore (programming error?)",cfg);
+               return;
+       }
        lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
        /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
        lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
        cfg->deletion_date=ms_time(NULL);
-       /* this will unREGISTER */
-       linphone_proxy_config_edit(cfg);
+       if (cfg->state==LinphoneRegistrationOk){
+               /* this will unREGISTER */
+               linphone_proxy_config_edit(cfg);
+       }
        if (lc->default_proxy==cfg){
                lc->default_proxy=NULL;
        }
+       linphone_proxy_config_write_all_to_config_file(lc);
 }
 /**
  * Erase all proxies from config.
@@ -540,10 +923,12 @@ void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cf
 **/
 void linphone_core_clear_proxy_config(LinphoneCore *lc){
        MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
+       MSList* copy=list;
        for(;list!=NULL;list=list->next){
                linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
        }
-       ms_list_free(list);
+       ms_list_free(copy);
+       linphone_proxy_config_write_all_to_config_file(lc);
 }
 /**
  * Sets the default proxy.
@@ -562,7 +947,8 @@ void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *conf
                }
        }
        lc->default_proxy=config;
-       
+       if (linphone_core_ready(lc))
+               lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
 }      
 
 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
@@ -610,6 +996,9 @@ void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyC
        if (obj->reg_identity!=NULL){
                lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
        }
+       if (obj->contact_params!=NULL){
+               lp_config_set_string(config,key,"contact_parameters",obj->contact_params);
+       }
        lp_config_set_int(config,key,"reg_expires",obj->expires);
        lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
        lp_config_set_int(config,key,"publish",obj->publish);
@@ -644,13 +1033,15 @@ LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config
        tmp=lp_config_get_string(config,key,"reg_route",NULL);
        if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
 
-       linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
+       linphone_proxy_config_set_contact_parameters(cfg,lp_config_get_string(config,key,"contact_parameters",NULL));
+       
+       linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",LP_CONFIG_DEFAULT_INT(config,"reg_expires",600)));
        linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
        
        linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
 
-       linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
-       linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
+       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)));
+       linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",LP_CONFIG_DEFAULT_STRING(config,"dial_prefix",NULL)));
        
        tmp=lp_config_get_string(config,key,"type",NULL);
        if (tmp!=NULL && strlen(tmp)>0) 
@@ -701,18 +1092,40 @@ SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
        return NULL;
 }
 
+static bool_t can_register(LinphoneProxyConfig *cfg){
+       LinphoneCore *lc=cfg->lc;
+#ifdef BUILD_UPNP
+       if (linphone_core_get_firewall_policy(lc)==LinphonePolicyUseUpnp){
+               if(lc->sip_conf.register_only_when_upnp_is_ok && 
+                       (lc->upnp == NULL || !linphone_upnp_context_is_ready_for_register(lc->upnp))) {
+                       return FALSE;
+               }
+       }
+#endif //BUILD_UPNP
+       if (lc->sip_conf.register_only_when_network_is_up){
+               LinphoneTunnel *tunnel=linphone_core_get_tunnel(lc);
+               if (tunnel && linphone_tunnel_enabled(tunnel)){
+                       return linphone_tunnel_connected(tunnel);
+               }else{
+                       return lc->network_reachable;
+               }
+       }
+       return TRUE;
+}
+
 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
        LinphoneCore *lc=cfg->lc;
        if (cfg->commit){
                if (cfg->type && cfg->ssctx==NULL){
                        linphone_proxy_config_activate_sip_setup(cfg);
                }
-               if (!lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
+               if (can_register(cfg)){
                        linphone_proxy_config_register(cfg);
-               if (cfg->publish && cfg->publish_op==NULL){
-                       linphone_proxy_config_send_publish(cfg,lc->presence_mode);
+                       if (cfg->publish && cfg->publish_op==NULL){
+                               linphone_proxy_config_send_publish(cfg,lc->presence_mode);
+                       }
+                       cfg->commit=FALSE;
                }
-               cfg->commit=FALSE;
        }
 }
 
@@ -743,7 +1156,7 @@ LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core,
                return NULL;
        }
        if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
-               ms_error("%s cannot manage accounts.");
+               ms_error("%s cannot manage accounts.",type);
                return NULL;
        }
        obj=ms_new0(LinphoneAccountCreator,1);
@@ -768,6 +1181,18 @@ void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char
        set_string(&obj->domain,domain);
 }
 
+void linphone_account_creator_set_route(LinphoneAccountCreator *obj, const char *route) {
+       set_string(&obj->route,route);
+}
+
+void linphone_account_creator_set_email(LinphoneAccountCreator *obj, const char *email) {
+       set_string(&obj->email,email);
+}
+
+void linphone_account_creator_set_suscribe(LinphoneAccountCreator *obj, int suscribe) {
+       obj->suscribe = suscribe;
+}
+
 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
        return obj->username;
 }
@@ -784,10 +1209,16 @@ int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
        return err;
 }
 
+int linphone_account_creator_test_validation(LinphoneAccountCreator *obj) {
+       SipSetupContext *ssctx=obj->ssctx;
+       int err=sip_setup_context_account_validated(ssctx,obj->username);
+       return err;
+}
+
 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
        SipSetupContext *ssctx=obj->ssctx;
        char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
-       int err=sip_setup_context_create_account(ssctx,uri,obj->password);
+       int err=sip_setup_context_create_account(ssctx, uri, obj->password, obj->email, obj->suscribe);
        ms_free(uri);
        if (err==0) {
                obj->succeeded=TRUE;