3
* oFono - Open Source Telephony
5
* Copyright (C) 2013 Canonical Ltd.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License version 2 as
9
* published by the Free Software Foundation.
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 General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
#define OFONO_API_SUBJECT_TO_CHANGE
33
#include <ofono/types.h>
34
#include <ofono/log.h>
35
#include <ofono/plugin.h>
36
#include <ofono/sim-mnclength.h>
38
struct mcc_mnclength {
44
* Database of MCC to MNC length correspondences based on "Mobile Network Codes
45
* (MNC) for the international identification plan for public networks and
46
* subscriptions (According to Recommendation ITU-T E.212 (05/2008))". Based on
47
* position on 1st January 2013 (http://www.itu.int/pub/T-SP-E.212B-2013).
48
* Latest version of that document can be found in
49
* http://www.itu.int/pub/T-SP-E.212B. Countries wiht no operators have been
50
* given a default length depending on their geographical area.
52
static struct mcc_mnclength mnclen_db[] = {
53
{202, 2}, /* Greece */
54
{204, 2}, /* Netherlands (Kingdom of the) */
55
{206, 2}, /* Belgium */
56
{208, 2}, /* France */
57
{212, 2}, /* Monaco (Principality of) */
58
{213, 2}, /* Andorra (Principality of) */
60
{216, 2}, /* Hungary */
61
{218, 2}, /* Bosnia and Herzegovina */
62
{219, 2}, /* Croatia (Republic of) */
63
{220, 2}, /* Serbia (Republic of) */
65
{225, 2}, /* Vatican City State */
66
{226, 2}, /* Romania */
67
{228, 2}, /* Switzerland (Confederation of) */
68
{230, 2}, /* Czech Republic */
69
{231, 2}, /* Slovak Republic */
70
{232, 2}, /* Austria */
71
{234, 2}, /* United Kingdom of G. Britain and Northern Ireland */
72
{235, 2}, /* United Kingdom of G. Britain and Northern Ireland */
73
{238, 2}, /* Denmark */
74
{240, 2}, /* Sweden */
75
{242, 2}, /* Norway */
76
{244, 2}, /* Finland */
77
{246, 2}, /* Lithuania (Republic of) */
78
{247, 2}, /* Latvia (Republic of) */
79
{248, 2}, /* Estonia (Republic of) */
80
{250, 2}, /* Russian Federation */
81
{255, 2}, /* Ukraine */
82
{257, 2}, /* Belarus (Republic of) */
83
{259, 2}, /* Moldova (Republic of) */
84
{260, 2}, /* Poland (Republic of) */
85
{262, 2}, /* Germany (Federal Republic of) */
86
{266, 2}, /* Gibraltar */
87
{268, 2}, /* Portugal */
88
{270, 2}, /* Luxembourg */
89
{272, 2}, /* Ireland */
90
{274, 2}, /* Iceland */
91
{276, 2}, /* Albania (Republic of) */
93
{280, 2}, /* Cyprus (Republic of) */
94
{282, 2}, /* Georgia */
95
{283, 2}, /* Armenia (Republic of) */
96
{284, 2}, /* Bulgaria (Republic of) */
97
{286, 2}, /* Turkey */
98
{288, 2}, /* Faroe Islands */
99
{290, 2}, /* Greenland (Denmark) */
100
{292, 2}, /* San Marino (Republic of) */
101
{293, 2}, /* Slovenia (Republic of) */
102
{294, 2}, /* The Former Yugoslav Republic of Macedonia */
103
{295, 2}, /* Liechtenstein (Principality of) */
104
{297, 2}, /* Montenegro (Republic of) */
105
{302, 3}, /* Canada */
106
{308, 2}, /* Saint Pierre and Miquelon (french Republic) */
107
{310, 3}, /* United States of America */
108
{311, 3}, /* United States of America */
109
{312, 3}, /* United States of America */
110
{313, 3}, /* United States of America */
111
{314, 3}, /* United States of America */
112
{315, 3}, /* United States of America */
113
{316, 3}, /* United States of America */
114
{330, 3}, /* Puerto Rico */
115
{332, 3}, /* United States Virgin Islands */
116
{334, 3}, /* Mexico */
117
{338, 3}, /* Jamaica */
118
{340, 2}, /* Guadeloupe and Martinique (French Departments) */
119
{342, 3}, /* Barbados */
120
{344, 3}, /* Antigua and Barbuda */
121
{346, 3}, /* Cayman Islands */
122
{348, 3}, /* British Virgin Islands */
123
{350, 3}, /* Bermuda */
124
{352, 3}, /* Grenada */
125
{354, 3}, /* Montserrat */
126
{356, 3}, /* Saint Kitts and Nevis */
127
{358, 3}, /* Saint Lucia */
128
{360, 3}, /* Saint Vincent and the Grenadines */
129
{362, 2}, /* Curazao, St Maarten, Bonaire, St Eustatius, Saba */
130
{363, 2}, /* Aruba */
131
{364, 3}, /* Bahamas (Commonwealth of the) */
132
{365, 3}, /* Anguilla */
133
{366, 3}, /* Dominica (Commonwealth of) */
135
{370, 2}, /* Dominican Republic */
136
{372, 2}, /* Haiti (Republic of) */
137
{374, 2}, /* Trinidad and Tobago */
138
{376, 3}, /* Turks and Caicos Islands */
139
{400, 2}, /* Azerbaijani Republic */
140
{401, 2}, /* Kazakhstan (Republic of) */
141
{402, 2}, /* Bhutan (Kingdom of) */
142
{404, 2}, /* India (Republic of) */
143
{405, 2}, /* India (Republic of) */
144
{406, 2}, /* India (Republic of) */
145
{410, 2}, /* Pakistan (Islamic Republic of) */
146
{412, 2}, /* Afghanistan */
147
{413, 2}, /* Sri Lanka (Democratic Socialist Republic of) */
148
{414, 2}, /* Myanmar (the Republic of the Union of) */
149
{415, 2}, /* Lebanon */
150
{416, 2}, /* Jordan (Hashemite Kingdom of) */
151
{417, 2}, /* Syrian Arab Republic */
152
{418, 2}, /* Iraq (Republic of) */
153
{419, 2}, /* Kuwait (State of) */
154
{420, 2}, /* Saudi Arabia (Kingdom of) */
155
{421, 2}, /* Yemen (Republic of) */
156
{422, 2}, /* Oman (Sultanate of) */
157
{424, 2}, /* United Arab Emirates */
158
{425, 2}, /* Israel (State of) */
159
{426, 2}, /* Bahrain (Kingdom of) */
160
{427, 2}, /* Qatar (State of) */
161
{428, 2}, /* Mongolia */
162
{429, 2}, /* Nepal (Federal Democratic Republic of) */
163
{430, 2}, /* United Arab Emirates */
164
{431, 2}, /* United Arab Emirates */
165
{432, 2}, /* Iran (Islamic Republic of) */
166
{434, 2}, /* Uzbekistan (Republic of) */
167
{436, 2}, /* Tajikistan (Republic of) */
168
{437, 2}, /* Kyrgyz Republic */
169
{438, 2}, /* Turkmenistan */
170
{440, 2}, /* Japan */
171
{441, 2}, /* Japan */
172
{450, 2}, /* Korea (Republic of) */
173
{452, 2}, /* Viet Nam (Socialist Republic of) */
174
{454, 2}, /* Hong Kong, China */
175
{455, 2}, /* Macao, China */
176
{456, 2}, /* Cambodia (Kingdom of) */
177
{457, 2}, /* Lao People's Democratic Republic */
178
{460, 2}, /* China (People's Republic of) */
179
{461, 2}, /* China (People's Republic of) */
180
{466, 2}, /* Taiwan, China */
181
{467, 2}, /* Democratic People's Republic of Korea */
182
{470, 2}, /* Bangladesh (People's Republic of) */
183
{472, 2}, /* Maldives (Republic of) */
184
{502, 2}, /* Malaysia */
185
{505, 2}, /* Australia */
186
{510, 2}, /* Indonesia (Republic of) */
187
{514, 2}, /* Democratic Republic of Timor-Leste */
188
{515, 2}, /* Philippines (Republic of the) */
189
{520, 2}, /* Thailand */
190
{525, 2}, /* Singapore (Republic of) */
191
{528, 2}, /* Brunei Darussalam */
192
{530, 2}, /* New Zealand */
193
{536, 2}, /* Nauru (Republic of) */
194
{537, 2}, /* Papua New Guinea */
195
{539, 2}, /* Tonga (Kingdom of) */
196
{540, 2}, /* Solomon Islands */
197
{541, 2}, /* Vanuatu (Republic of) */
198
{542, 2}, /* Fiji (Republic of) */
199
{543, 2}, /* Wallis and Futuna (french territory) */
200
{544, 2}, /* American Samoa */
201
{545, 2}, /* Kiribati (Republic of) */
202
{546, 2}, /* New Caledonia (french territory) */
203
{547, 2}, /* French Polynesia (french territory) */
204
{548, 2}, /* Cook Islands */
205
{549, 2}, /* Samoa (Independent State of) */
206
{550, 2}, /* Micronesia (Federated States of) */
207
{551, 2}, /* Marshall Islands (Republic of the) */
208
{552, 2}, /* Palau (Republic of) */
209
{553, 2}, /* Tuvalu */
211
{602, 2}, /* Egypt (Arab Republic of) */
212
{603, 2}, /* Algeria (People's Democratic Republic of) */
213
{604, 2}, /* Morocco (Kingdom of) */
214
{605, 2}, /* Tunisia */
215
{606, 2}, /* Libya */
216
{607, 2}, /* Gambia (Republic of the) */
217
{608, 2}, /* Senegal (Republic of) */
218
{609, 2}, /* Mauritania (Islamic Republic of) */
219
{610, 2}, /* Mali (Republic of) */
220
{611, 2}, /* Guinea (Republic of) */
221
{612, 2}, /* Ivory Coast (Republic of) */
222
{613, 2}, /* Burkina Faso */
223
{614, 2}, /* Niger (Republic of the) */
224
{615, 2}, /* Togolese Republic */
225
{616, 2}, /* Benin (Republic of) */
226
{617, 2}, /* Mauritius (Republic of) */
227
{618, 2}, /* Liberia (Republic of) */
228
{619, 2}, /* Sierra Leone */
229
{620, 2}, /* Ghana */
230
{621, 2}, /* Nigeria (Federal Republic of) */
231
{622, 2}, /* Chad (Republic of) */
232
{623, 2}, /* Central African Republic */
233
{624, 2}, /* Cameroon (Republic of) */
234
{625, 2}, /* Cape Verde (Republic of) */
235
{626, 2}, /* Sao Tome and Principe (Democratic Republic of) */
236
{627, 2}, /* Equatorial Guinea (Republic of) */
237
{628, 2}, /* Gabonese Republic */
238
{629, 2}, /* Congo (Republic of the) */
239
{630, 2}, /* Democratic Republic of the Congo */
240
{631, 2}, /* Angola (Republic of) */
241
{632, 2}, /* Guinea-Bissau (Republic of) */
242
{633, 2}, /* Seychelles (Republic of) */
243
{634, 2}, /* Sudan (Republic of the) */
244
{635, 2}, /* Rwanda (Republic of) */
245
{636, 2}, /* Ethiopia (Federal Democratic Republic of) */
246
{637, 2}, /* Somali Democratic Republic */
247
{638, 2}, /* Djibouti (Republic of) */
248
{639, 2}, /* Kenya (Republic of) */
249
{640, 2}, /* Tanzania (United Republic of) */
250
{641, 2}, /* Uganda (Republic of) */
251
{642, 2}, /* Burundi (Republic of) */
252
{643, 2}, /* Mozambique (Republic of) */
253
{645, 2}, /* Zambia (Republic of) */
254
{646, 2}, /* Madagascar (Republic of) */
255
{647, 2}, /* French Departments in the Indian Ocean */
256
{648, 2}, /* Zimbabwe (Republic of) */
257
{649, 2}, /* Namibia (Republic of) */
258
{650, 2}, /* Malawi */
259
{651, 2}, /* Lesotho (Kingdom of) */
260
{652, 2}, /* Botswana (Republic of) */
261
{653, 2}, /* Swaziland (Kingdom of) */
262
{654, 2}, /* Comoros (Union of the) */
263
{655, 2}, /* South Africa (Republic of) */
264
{657, 2}, /* Eritrea */
265
{658, 2}, /* Saint Helena, Ascension and Tristan da Cunha */
266
{659, 2}, /* South Sudan (Republic of) */
267
{702, 2}, /* Belize */
268
{704, 2}, /* Guatemala (Republic of) */
269
{706, 2}, /* El Salvador (Republic of) */
270
{708, 3}, /* Honduras (Republic of) */
271
{710, 2}, /* Nicaragua */
272
{712, 2}, /* Costa Rica */
273
{714, 2}, /* Panama (Republic of) */
275
{722, 3}, /* Argentine Republic */
276
{724, 2}, /* Brazil (Federative Republic of) */
277
{730, 2}, /* Chile */
278
{732, 3}, /* Colombia (Republic of) */
279
{734, 2}, /* Venezuela (Bolivarian Republic of) */
280
{736, 2}, /* Bolivia (Plurinational State of) */
281
{738, 2}, /* Guyana */
282
{740, 2}, /* Ecuador */
283
{742, 2}, /* French Guiana (French Department of) */
284
{744, 2}, /* Paraguay (Republic of) */
285
{746, 2}, /* Suriname (Republic of) */
286
{748, 2}, /* Uruguay (Eastern Republic of) */
287
{750, 3}, /* Falkland Islands (Malvinas) */
288
{901, 2}, /* International Mobile, shared code */
292
* These MCC+MNC combinations have 3 digit MNC even though the default for
293
* the corresponing MCC in mnclen_db is length 2.
295
static int codes_mnclen3_db[] = {
296
374130, 374140, 405000, 405005, 405006, 405007, 405009, 405010,
297
405011, 405012, 405013, 405014, 405018, 405020, 405021, 405022,
298
405025, 405027, 405029, 405030, 405031, 405032, 405033, 405034,
299
405035, 405036, 405037, 405038, 405039, 405040, 405041, 405042,
300
405043, 405044, 405045, 405046, 405047, 405750, 405751, 405752,
301
405753, 405754, 405755, 405799, 405800, 405801, 405802, 405803,
302
405804, 405805, 405806, 405807, 405808, 405809, 714020
305
static int comp_int(const void *key, const void *value)
307
int mccmnckey = *(int *) key;
308
int mccmnccurr = *(int *) value;
310
return mccmnckey - mccmnccurr;
313
static int comp_mcc(const void *key, const void *value)
315
int mcc = *(int *) key;
316
struct mcc_mnclength *mccmnc = (struct mcc_mnclength *) value;
318
return mcc - mccmnc->mcc;
321
static int mnclength_get_mnclength(const char *imsi)
323
char mccmnc[OFONO_MAX_MCC_LENGTH + OFONO_MAX_MNC_LENGTH + 1];
327
struct mcc_mnclength *mccmnc_res;
330
if (imsi == NULL || *imsi == '\0')
333
/* Special case for some operators */
334
strncpy(mccmnc, imsi, sizeof(mccmnc) - 1);
335
mccmnc[sizeof(mccmnc) - 1] = '\0';
337
mccmnc_num = (int) strtoul(mccmnc, &endp, 10);
341
mccmnc3_res = bsearch(&mccmnc_num, codes_mnclen3_db,
342
G_N_ELEMENTS(codes_mnclen3_db),
343
sizeof(codes_mnclen3_db[0]), comp_int);
348
mccmnc[OFONO_MAX_MCC_LENGTH] = '\0';
349
mcc_num = (int) strtoul(mccmnc, &endp, 10);
354
bsearch(&mcc_num, mnclen_db, G_N_ELEMENTS(mnclen_db),
355
sizeof(mnclen_db[0]), comp_mcc);
357
return mccmnc_res->mnclength;
362
static struct ofono_sim_mnclength_driver mnclength_driver = {
363
.name = "MNC length",
364
.get_mnclength = mnclength_get_mnclength
367
static int mnclength_init(void)
369
return ofono_sim_mnclength_driver_register(&mnclength_driver);
372
static void mnclength_exit(void)
374
ofono_sim_mnclength_driver_unregister(&mnclength_driver);
377
OFONO_PLUGIN_DEFINE(mnclength, "MNC length Plugin", VERSION,
378
OFONO_PLUGIN_PRIORITY_DEFAULT,
379
mnclength_init, mnclength_exit)