From 9f3bdc0471ae8e44762c99a05ed47108f2f90077 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Wed, 27 Jul 2016 14:34:18 +0200 Subject: [PATCH] dialplan: add some public APIs --- coreapi/CMakeLists.txt | 1 + coreapi/Makefile.am | 1 + coreapi/account_creator.c | 39 ++++- coreapi/account_creator.h | 19 +++ coreapi/dial_plan.c | 312 ++++++++++++++++++++++++++++++++++ coreapi/linphonecore_utils.h | 22 ++- coreapi/private.h | 1 + coreapi/proxy.c | 317 ++--------------------------------- 8 files changed, 404 insertions(+), 308 deletions(-) create mode 100644 coreapi/dial_plan.c diff --git a/coreapi/CMakeLists.txt b/coreapi/CMakeLists.txt index 69170ab7f..6d80f966b 100644 --- a/coreapi/CMakeLists.txt +++ b/coreapi/CMakeLists.txt @@ -83,6 +83,7 @@ set(LINPHONE_SOURCE_FILES_C chat_file_transfer.c contactprovider.c content.c + dial_plan.c dict.c ec-calibrator.c enum.c diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am index 97ba75460..b62a2aa6a 100644 --- a/coreapi/Makefile.am +++ b/coreapi/Makefile.am @@ -65,6 +65,7 @@ liblinphone_la_SOURCES=\ conference.cc conference_private.h \ contactprovider.c contactprovider.h contact_providers_priv.h \ content.c \ + dial_plan.c \ dict.c \ ec-calibrator.c \ enum.c enum.h \ diff --git a/coreapi/account_creator.c b/coreapi/account_creator.c index 4469bc7d1..d23620ad3 100644 --- a/coreapi/account_creator.c +++ b/coreapi/account_creator.c @@ -209,7 +209,7 @@ LinphoneAccountCreatorStatus linphone_account_creator_set_username(LinphoneAccou int max_length = lp_config_get_int(creator->core->config, "assistant", "username_max_length", -1); bool_t use_phone_number = lp_config_get_int(creator->core->config, "assistant", "use_phone_number", 0); const char* regex = lp_config_get_string(creator->core->config, "assistant", "username_regex", 0); - LinphoneAccountCreatorStatus status; + LinphoneAccountCreatorStatus status = LinphoneAccountCreatorOK; if (min_length > 0 && strlen(username) < (size_t)min_length) { return LinphoneAccountCreatorUsernameTooShort; } else if (max_length > 0 && strlen(username) > (size_t)max_length) { @@ -233,6 +233,43 @@ const char * linphone_account_creator_get_username(const LinphoneAccountCreator return creator->username; } + +LinphoneAccountCreatorStatus linphone_account_creator_set_phone_number(LinphoneAccountCreator *creator, const char *phone_number, const char *country_code) { + char *user; + if (!phone_number || !country_code) { + return LinphoneAccountCreatorPhoneNumberInvalid; + } else { + LinphoneProxyConfig *numCfg = linphone_proxy_config_new(); + linphone_proxy_config_set_dial_prefix(numCfg, country_code); + user = linphone_proxy_config_normalize_phone_number(numCfg, phone_number); + linphone_proxy_config_destroy(numCfg); + if (!user) { + return LinphoneAccountCreatorPhoneNumberInvalid; + } + + // if phone is valid, we lastly want to check that length is OK + { + int ccc = linphone_dial_plan_lookup_ccc_from_e164(phone_number); + char *ccc_str = ms_strdup_printf("%d", ccc); + const LinphoneDialPlan* plan = linphone_dial_plan_by_ccc(ccc_str); + ms_free(ccc_str); + if (strlen(phone_number) < plan->nnl) { + return LinphoneAccountCreatorPhoneNumberTooShort; + } else if (strlen(phone_number) > plan->nnl) { + return LinphoneAccountCreatorPhoneNumberTooLong; + } + } + } + set_string(&creator->phone_number, user, TRUE); + ms_free(user); + + return LinphoneAccountCreatorOK; +} + +const char * linphone_account_creator_get_phone_number(const LinphoneAccountCreator *creator) { + return creator->phone_number; +} + LinphoneAccountCreatorStatus linphone_account_creator_set_password(LinphoneAccountCreator *creator, const char *password){ int min_length = lp_config_get_int(creator->core->config, "assistant", "password_min_length", -1); int max_length = lp_config_get_int(creator->core->config, "assistant", "password_max_length", -1); diff --git a/coreapi/account_creator.h b/coreapi/account_creator.h index 94c16670a..953e36e4e 100644 --- a/coreapi/account_creator.h +++ b/coreapi/account_creator.h @@ -52,6 +52,9 @@ typedef enum _LinphoneAccountCreatorStatus { LinphoneAccountCreatorUsernameTooShort, LinphoneAccountCreatorUsernameTooLong, LinphoneAccountCreatorUsernameInvalidSize, + LinphoneAccountCreatorPhoneNumberInvalid, + LinphoneAccountCreatorPhoneNumberTooShort, + LinphoneAccountCreatorPhoneNumberTooLong, LinphoneAccountCreatorPasswordTooShort, LinphoneAccountCreatorPasswordTooLong, LinphoneAccountCreatorDomainInvalid, @@ -141,6 +144,22 @@ LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_set_userna **/ LINPHONE_PUBLIC const char * linphone_account_creator_get_username(const LinphoneAccountCreator *creator); +/** + * Set the phone number normalized. + * @param[in] creator LinphoneAccountCreator object + * @param[in] phone number The phone number to set + * @param[in] country code Country code to associate phone number with + * @return LinphoneAccountCreatorOk if everything is OK, or a specific error otherwise. +**/ +LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_set_phone_number(LinphoneAccountCreator *creator, const char *phone_number, const char *country_code); + +/** + * Get the RFC 3966 normalized phone number. + * @param[in] creator LinphoneAccountCreator object + * @return The phone number of the LinphoneAccountCreator +**/ +LINPHONE_PUBLIC const char * linphone_account_creator_get_phone_number(const LinphoneAccountCreator *creator); + /** * Set the password. * @param[in] creator LinphoneAccountCreator object diff --git a/coreapi/dial_plan.c b/coreapi/dial_plan.c new file mode 100644 index 000000000..4f23b05e6 --- /dev/null +++ b/coreapi/dial_plan.c @@ -0,0 +1,312 @@ +/* +linphone +Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org) +*/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "linphonecore_utils.h" + +/* + * http://en.wikipedia.org/wiki/Telephone_numbering_plan + * http://en.wikipedia.org/wiki/Telephone_numbers_in_Europe + * imported from https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country + */ +static LinphoneDialPlan const dial_plans[]={ + //Country , iso country code, e164 country calling code, number length, international usual prefix + {"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" }, + {"Cote 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" }, +/* {"Jersey" ,"JE" , "44" , 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" }, + {"Sao Tome and Principe" ,"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" ,"GB" , "44" , 10 , "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 LinphoneDialPlan most_common_dialplan={ "generic" ,"", "", 10, "00"}; + +int linphone_dial_plan_lookup_ccc_from_e164(const char* e164) { + LinphoneDialPlan* dial_plan; + LinphoneDialPlan* 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=(LinphoneDialPlan*)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) { + LinphoneDialPlan* dial_plan; + for (dial_plan=(LinphoneDialPlan*)dial_plans; dial_plan->country!=NULL; dial_plan++) { + if (strcmp(iso, dial_plan->iso_country_code)==0) { + return atoi(dial_plan->ccc); + } + } + return -1; +} + +const LinphoneDialPlan* linphone_dial_plan_by_ccc(const char *ccc) { + int i; + if (!ccc) { + return &most_common_dialplan; + } + + for(i=0;dial_plans[i].country!=NULL;++i){ + if (strcmp(ccc,dial_plans[i].ccc)==0){ + return &dial_plans[i]; + } + } + /*else return a generic "most common" dial plan*/ + return &most_common_dialplan; +} + +const LinphoneDialPlan* linphone_dial_plan_get_all() { + return dial_plans; +} diff --git a/coreapi/linphonecore_utils.h b/coreapi/linphonecore_utils.h index 9350a179d..6b199162c 100644 --- a/coreapi/linphonecore_utils.h +++ b/coreapi/linphonecore_utils.h @@ -90,13 +90,22 @@ typedef bool_t (*LinphoneCoreIterateHook)(void *data); void linphone_core_add_iterate_hook(LinphoneCore *lc, LinphoneCoreIterateHook hook, void *hook_data); void linphone_core_remove_iterate_hook(LinphoneCore *lc, LinphoneCoreIterateHook hook, void *hook_data); + +typedef struct _LinphoneDialPlan { + 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*/ +} LinphoneDialPlan; + /** * @ingroup misc *Function to get call country code from ISO 3166-1 alpha-2 code, ex: FR returns 33 *@param iso country code alpha2 *@return call country code or -1 if not found */ -LINPHONE_PUBLIC int linphone_dial_plan_lookup_ccc_from_iso(const char* iso); +LINPHONE_PUBLIC int linphone_dial_plan_lookup_ccc_from_iso(const char* iso); /** * @ingroup misc *Function to get call country code from an e164 number, ex: +33952650121 will return 33 @@ -105,6 +114,17 @@ LINPHONE_PUBLIC int linphone_dial_plan_lookup_ccc_from_iso(const char* iso); */ LINPHONE_PUBLIC int linphone_dial_plan_lookup_ccc_from_e164(const char* e164); +/** + * Return NULL-terminated array of all known dial plans +**/ +LINPHONE_PUBLIC const LinphoneDialPlan* linphone_dial_plan_get_all(); + +/** + * Find best match for given CCC + * @return Return matching dial plan, or a generic one if none found +**/ +LINPHONE_PUBLIC const LinphoneDialPlan* linphone_dial_plan_by_ccc(const char *ccc); + #ifdef __cplusplus } #endif diff --git a/coreapi/private.h b/coreapi/private.h index b4ed35089..d62d71436 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -1281,6 +1281,7 @@ struct _LinphoneAccountCreator { LinphoneCore *core; char *xmlrpc_url; char *username; + char *phone_number; char *password; char *domain; char *route; diff --git a/coreapi/proxy.c b/coreapi/proxy.c index 4c39c24a7..8ae879f63 100644 --- a/coreapi/proxy.c +++ b/coreapi/proxy.c @@ -19,12 +19,12 @@ Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org) */ #include "linphonecore.h" +#include "linphonecore_utils.h" #include "sipsetup.h" #include "lpconfig.h" #include "private.h" #include "mediastreamer2/mediastream.h" #include "enum.h" - #include /*store current config related to server location*/ @@ -566,300 +566,6 @@ const char *linphone_proxy_config_get_quality_reporting_collector(const Linphone } -/* - * 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; - -static dial_plan_t const dial_plans[]={ - //Country , iso country code, e164 country calling code, number length, international usual prefix - {"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" }, - {"Cote 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" }, -/* {"Jersey" ,"JE" , "44" , 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" }, - {"Sao Tome and Principe" ,"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" ,"GB" , "44" , 10 , "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 bool_t lookup_dial_plan_by_ccc(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 TRUE; - } - } - /*else return a generic "most common" dial plan*/ - *plan=most_common_dialplan; - strcpy(plan->ccc,ccc); - return FALSE; -} - bool_t linphone_proxy_config_is_phone_number(LinphoneProxyConfig *proxy, const char *username){ const char *p; if (!username) return FALSE; @@ -916,39 +622,38 @@ char* linphone_proxy_config_normalize_phone_number(LinphoneProxyConfig *proxy, c LinphoneProxyConfig *tmpproxy = proxy ? proxy : linphone_proxy_config_new(); char* result = NULL; if (linphone_proxy_config_is_phone_number(tmpproxy, username)){ - dial_plan_t dialplan = {0}; char * flatten=flatten_number(username); + const LinphoneDialPlan *dialplan = linphone_dial_plan_by_ccc(tmpproxy->dial_prefix); ms_debug("Flattened number is '%s' for '%s'",flatten, username); /*if proxy has a dial prefix, modify phonenumber accordingly*/ if (tmpproxy->dial_prefix!=NULL && tmpproxy->dial_prefix[0]!='\0'){ - lookup_dial_plan_by_ccc(tmpproxy->dial_prefix,&dialplan); - ms_debug("Using dial plan '%s'",dialplan.country); + ms_debug("Using dial plan '%s'",dialplan->country); /* the number already starts with + or international prefix*/ - if (flatten[0]=='+'||strstr(flatten,dialplan.icp)==flatten){ + if (flatten[0]=='+'||strstr(flatten,dialplan->icp)==flatten){ ms_debug("Prefix already present."); if (tmpproxy->dial_escape_plus) { - result = replace_plus_with_icp(flatten,dialplan.icp); + result = replace_plus_with_icp(flatten,dialplan->icp); } else { - result = replace_icp_with_plus(flatten,dialplan.icp); + result = replace_icp_with_plus(flatten,dialplan->icp); } }else{ /*0. keep at most national number significant digits */ - char* flatten_start = flatten + MAX(0, (int)strlen(flatten) - (int)dialplan.nnl); - ms_debug("Prefix not present. Keeping at most %d digits: %s", dialplan.nnl, flatten_start); + char* flatten_start = flatten + MAX(0, (int)strlen(flatten) - (int)dialplan->nnl); + ms_debug("Prefix not present. Keeping at most %d digits: %s", dialplan->nnl, flatten_start); /*1. First prepend international calling prefix or +*/ /*2. Second add prefix*/ /*3. Finally add user digits */ result = ms_strdup_printf("%s%s%s" - , tmpproxy->dial_escape_plus ? dialplan.icp : "+" - , dialplan.ccc + , tmpproxy->dial_escape_plus ? dialplan->icp : "+" + , dialplan->ccc , flatten_start); ms_debug("Prepended prefix resulted in %s", result); } }else if (tmpproxy->dial_escape_plus){ /* user did not provide dial prefix, so we'll take the most generic one */ - result = replace_plus_with_icp(flatten,most_common_dialplan.icp); + result = replace_plus_with_icp(flatten,dialplan->icp); } if (result==NULL) { result = flatten;