1
2 """
3 Constants needed for parsing binary GeoIP databases. It is part of the pygeoip
4 package.
5
6 @author: Jennifer Ennis <zaylea at gmail dot com>
7
8 @license:
9 Copyright(C) 2004 MaxMind LLC
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
23 """
24
25 from platform import python_version_tuple
26
27 PY2 = python_version_tuple()[0] == '2'
28 PY3 = python_version_tuple()[0] == '3'
29
30 GEOIP_STANDARD = 0
31 GEOIP_MEMORY_CACHE = 1
32
33 DMA_MAP = {
34 500: 'Portland-Auburn, ME',
35 501: 'New York, NY',
36 502: 'Binghamton, NY',
37 503: 'Macon, GA',
38 504: 'Philadelphia, PA',
39 505: 'Detroit, MI',
40 506: 'Boston, MA',
41 507: 'Savannah, GA',
42 508: 'Pittsburgh, PA',
43 509: 'Ft Wayne, IN',
44 510: 'Cleveland, OH',
45 511: 'Washington, DC',
46 512: 'Baltimore, MD',
47 513: 'Flint, MI',
48 514: 'Buffalo, NY',
49 515: 'Cincinnati, OH',
50 516: 'Erie, PA',
51 517: 'Charlotte, NC',
52 518: 'Greensboro, NC',
53 519: 'Charleston, SC',
54 520: 'Augusta, GA',
55 521: 'Providence, RI',
56 522: 'Columbus, GA',
57 523: 'Burlington, VT',
58 524: 'Atlanta, GA',
59 525: 'Albany, GA',
60 526: 'Utica-Rome, NY',
61 527: 'Indianapolis, IN',
62 528: 'Miami, FL',
63 529: 'Louisville, KY',
64 530: 'Tallahassee, FL',
65 531: 'Tri-Cities, TN',
66 532: 'Albany-Schenectady-Troy, NY',
67 533: 'Hartford, CT',
68 534: 'Orlando, FL',
69 535: 'Columbus, OH',
70 536: 'Youngstown-Warren, OH',
71 537: 'Bangor, ME',
72 538: 'Rochester, NY',
73 539: 'Tampa, FL',
74 540: 'Traverse City-Cadillac, MI',
75 541: 'Lexington, KY',
76 542: 'Dayton, OH',
77 543: 'Springfield-Holyoke, MA',
78 544: 'Norfolk-Portsmouth, VA',
79 545: 'Greenville-New Bern-Washington, NC',
80 546: 'Columbia, SC',
81 547: 'Toledo, OH',
82 548: 'West Palm Beach, FL',
83 549: 'Watertown, NY',
84 550: 'Wilmington, NC',
85 551: 'Lansing, MI',
86 552: 'Presque Isle, ME',
87 553: 'Marquette, MI',
88 554: 'Wheeling, WV',
89 555: 'Syracuse, NY',
90 556: 'Richmond-Petersburg, VA',
91 557: 'Knoxville, TN',
92 558: 'Lima, OH',
93 559: 'Bluefield-Beckley-Oak Hill, WV',
94 560: 'Raleigh-Durham, NC',
95 561: 'Jacksonville, FL',
96 563: 'Grand Rapids, MI',
97 564: 'Charleston-Huntington, WV',
98 565: 'Elmira, NY',
99 566: 'Harrisburg-Lancaster-Lebanon-York, PA',
100 567: 'Greenville-Spartenburg, SC',
101 569: 'Harrisonburg, VA',
102 570: 'Florence-Myrtle Beach, SC',
103 571: 'Ft Myers, FL',
104 573: 'Roanoke-Lynchburg, VA',
105 574: 'Johnstown-Altoona, PA',
106 575: 'Chattanooga, TN',
107 576: 'Salisbury, MD',
108 577: 'Wilkes Barre-Scranton, PA',
109 581: 'Terre Haute, IN',
110 582: 'Lafayette, IN',
111 583: 'Alpena, MI',
112 584: 'Charlottesville, VA',
113 588: 'South Bend, IN',
114 592: 'Gainesville, FL',
115 596: 'Zanesville, OH',
116 597: 'Parkersburg, WV',
117 598: 'Clarksburg-Weston, WV',
118 600: 'Corpus Christi, TX',
119 602: 'Chicago, IL',
120 603: 'Joplin-Pittsburg, MO',
121 604: 'Columbia-Jefferson City, MO',
122 605: 'Topeka, KS',
123 606: 'Dothan, AL',
124 609: 'St Louis, MO',
125 610: 'Rockford, IL',
126 611: 'Rochester-Mason City-Austin, MN',
127 612: 'Shreveport, LA',
128 613: 'Minneapolis-St Paul, MN',
129 616: 'Kansas City, MO',
130 617: 'Milwaukee, WI',
131 618: 'Houston, TX',
132 619: 'Springfield, MO',
133 620: 'Tuscaloosa, AL',
134 622: 'New Orleans, LA',
135 623: 'Dallas-Fort Worth, TX',
136 624: 'Sioux City, IA',
137 625: 'Waco-Temple-Bryan, TX',
138 626: 'Victoria, TX',
139 627: 'Wichita Falls, TX',
140 628: 'Monroe, LA',
141 630: 'Birmingham, AL',
142 631: 'Ottumwa-Kirksville, IA',
143 632: 'Paducah, KY',
144 633: 'Odessa-Midland, TX',
145 634: 'Amarillo, TX',
146 635: 'Austin, TX',
147 636: 'Harlingen, TX',
148 637: 'Cedar Rapids-Waterloo, IA',
149 638: 'St Joseph, MO',
150 639: 'Jackson, TN',
151 640: 'Memphis, TN',
152 641: 'San Antonio, TX',
153 642: 'Lafayette, LA',
154 643: 'Lake Charles, LA',
155 644: 'Alexandria, LA',
156 646: 'Anniston, AL',
157 647: 'Greenwood-Greenville, MS',
158 648: 'Champaign-Springfield-Decatur, IL',
159 649: 'Evansville, IN',
160 650: 'Oklahoma City, OK',
161 651: 'Lubbock, TX',
162 652: 'Omaha, NE',
163 656: 'Panama City, FL',
164 657: 'Sherman, TX',
165 658: 'Green Bay-Appleton, WI',
166 659: 'Nashville, TN',
167 661: 'San Angelo, TX',
168 662: 'Abilene-Sweetwater, TX',
169 669: 'Madison, WI',
170 670: 'Ft Smith-Fay-Springfield, AR',
171 671: 'Tulsa, OK',
172 673: 'Columbus-Tupelo-West Point, MS',
173 675: 'Peoria-Bloomington, IL',
174 676: 'Duluth, MN',
175 678: 'Wichita, KS',
176 679: 'Des Moines, IA',
177 682: 'Davenport-Rock Island-Moline, IL',
178 686: 'Mobile, AL',
179 687: 'Minot-Bismarck-Dickinson, ND',
180 691: 'Huntsville, AL',
181 692: 'Beaumont-Port Author, TX',
182 693: 'Little Rock-Pine Bluff, AR',
183 698: 'Montgomery, AL',
184 702: 'La Crosse-Eau Claire, WI',
185 705: 'Wausau-Rhinelander, WI',
186 709: 'Tyler-Longview, TX',
187 710: 'Hattiesburg-Laurel, MS',
188 711: 'Meridian, MS',
189 716: 'Baton Rouge, LA',
190 717: 'Quincy, IL',
191 718: 'Jackson, MS',
192 722: 'Lincoln-Hastings, NE',
193 724: 'Fargo-Valley City, ND',
194 725: 'Sioux Falls, SD',
195 734: 'Jonesboro, AR',
196 736: 'Bowling Green, KY',
197 737: 'Mankato, MN',
198 740: 'North Platte, NE',
199 743: 'Anchorage, AK',
200 744: 'Honolulu, HI',
201 745: 'Fairbanks, AK',
202 746: 'Biloxi-Gulfport, MS',
203 747: 'Juneau, AK',
204 749: 'Laredo, TX',
205 751: 'Denver, CO',
206 752: 'Colorado Springs, CO',
207 753: 'Phoenix, AZ',
208 754: 'Butte-Bozeman, MT',
209 755: 'Great Falls, MT',
210 756: 'Billings, MT',
211 757: 'Boise, ID',
212 758: 'Idaho Falls-Pocatello, ID',
213 759: 'Cheyenne, WY',
214 760: 'Twin Falls, ID',
215 762: 'Missoula, MT',
216 764: 'Rapid City, SD',
217 765: 'El Paso, TX',
218 766: 'Helena, MT',
219 767: 'Casper-Riverton, WY',
220 770: 'Salt Lake City, UT',
221 771: 'Yuma, AZ',
222 773: 'Grand Junction, CO',
223 789: 'Tucson, AZ',
224 790: 'Albuquerque, NM',
225 798: 'Glendive, MT',
226 800: 'Bakersfield, CA',
227 801: 'Eugene, OR',
228 802: 'Eureka, CA',
229 803: 'Los Angeles, CA',
230 804: 'Palm Springs, CA',
231 807: 'San Francisco, CA',
232 810: 'Yakima-Pasco, WA',
233 811: 'Reno, NV',
234 813: 'Medford-Klamath Falls, OR',
235 819: 'Seattle-Tacoma, WA',
236 820: 'Portland, OR',
237 821: 'Bend, OR',
238 825: 'San Diego, CA',
239 828: 'Monterey-Salinas, CA',
240 839: 'Las Vegas, NV',
241 855: 'Santa Barbara, CA',
242 862: 'Sacramento, CA',
243 866: 'Fresno, CA',
244 868: 'Chico-Redding, CA',
245 881: 'Spokane, WA'
246 }
247
248 COUNTRY_CODES = (
249 '',
250 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ',
251 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG',
252 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY',
253 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN',
254 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO',
255 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM',
256 'FO', 'FR', 'FX', 'GA', 'GB', 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM',
257 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN',
258 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT',
259 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW',
260 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV',
261 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP',
262 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC',
263 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA',
264 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW',
265 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG',
266 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY',
267 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TM', 'TN', 'TO', 'TL',
268 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA',
269 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'RS', 'ZA',
270 'ZM', 'ME', 'ZW', 'A1', 'A2', 'O1', 'AX', 'GG', 'IM', 'JE', 'BL', 'MF'
271 )
272
273 COUNTRY_CODES3 = (
274 '', 'AP', 'EU', 'AND', 'ARE', 'AFG', 'ATG', 'AIA', 'ALB', 'ARM', 'ANT',
275 'AGO', 'AQ', 'ARG', 'ASM', 'AUT', 'AUS', 'ABW', 'AZE', 'BIH', 'BRB', 'BGD',
276 'BEL', 'BFA', 'BGR', 'BHR', 'BDI', 'BEN', 'BMU', 'BRN', 'BOL', 'BRA',
277 'BHS', 'BTN', 'BV', 'BWA', 'BLR', 'BLZ', 'CAN', 'CC', 'COD', 'CAF', 'COG',
278 'CHE', 'CIV', 'COK', 'CHL', 'CMR', 'CHN', 'COL', 'CRI', 'CUB', 'CPV', 'CX',
279 'CYP', 'CZE', 'DEU', 'DJI', 'DNK', 'DMA', 'DOM', 'DZA', 'ECU', 'EST',
280 'EGY', 'ESH', 'ERI', 'ESP', 'ETH', 'FIN', 'FJI', 'FLK', 'FSM', 'FRO',
281 'FRA', 'FX', 'GAB', 'GBR', 'GRD', 'GEO', 'GUF', 'GHA', 'GIB', 'GRL', 'GMB',
282 'GIN', 'GLP', 'GNQ', 'GRC', 'GS', 'GTM', 'GUM', 'GNB', 'GUY', 'HKG', 'HM',
283 'HND', 'HRV', 'HTI', 'HUN', 'IDN', 'IRL', 'ISR', 'IND', 'IO', 'IRQ', 'IRN',
284 'ISL', 'ITA', 'JAM', 'JOR', 'JPN', 'KEN', 'KGZ', 'KHM', 'KIR', 'COM',
285 'KNA', 'PRK', 'KOR', 'KWT', 'CYM', 'KAZ', 'LAO', 'LBN', 'LCA', 'LIE',
286 'LKA', 'LBR', 'LSO', 'LTU', 'LUX', 'LVA', 'LBY', 'MAR', 'MCO', 'MDA',
287 'MDG', 'MHL', 'MKD', 'MLI', 'MMR', 'MNG', 'MAC', 'MNP', 'MTQ', 'MRT',
288 'MSR', 'MLT', 'MUS', 'MDV', 'MWI', 'MEX', 'MYS', 'MOZ', 'NAM', 'NCL',
289 'NER', 'NFK', 'NGA', 'NIC', 'NLD', 'NOR', 'NPL', 'NRU', 'NIU', 'NZL',
290 'OMN', 'PAN', 'PER', 'PYF', 'PNG', 'PHL', 'PAK', 'POL', 'SPM', 'PCN',
291 'PRI', 'PSE', 'PRT', 'PLW', 'PRY', 'QAT', 'REU', 'ROU', 'RUS', 'RWA',
292 'SAU', 'SLB', 'SYC', 'SDN', 'SWE', 'SGP', 'SHN', 'SVN', 'SJM', 'SVK',
293 'SLE', 'SMR', 'SEN', 'SOM', 'SUR', 'STP', 'SLV', 'SYR', 'SWZ', 'TCA',
294 'TCD', 'TF', 'TGO', 'THA', 'TJK', 'TKL', 'TLS', 'TKM', 'TUN', 'TON', 'TUR',
295 'TTO', 'TUV', 'TWN', 'TZA', 'UKR', 'UGA', 'UM', 'USA', 'URY', 'UZB', 'VAT',
296 'VCT', 'VEN', 'VGB', 'VIR', 'VNM', 'VUT', 'WLF', 'WSM', 'YEM', 'YT', 'SRB',
297 'ZAF', 'ZMB', 'MNE', 'ZWE', 'A1', 'A2', 'O1', 'ALA', 'GGY', 'IMN', 'JEY',
298 'BLM', 'MAF'
299 )
300
301 COUNTRY_NAMES = (
302 '', 'Asia/Pacific Region', 'Europe', 'Andorra', 'United Arab Emirates',
303 'Afghanistan', 'Antigua and Barbuda', 'Anguilla', 'Albania', 'Armenia',
304 'Netherlands Antilles', 'Angola', 'Antarctica', 'Argentina',
305 'American Samoa', 'Austria', 'Australia', 'Aruba', 'Azerbaijan',
306 'Bosnia and Herzegovina', 'Barbados', 'Bangladesh', 'Belgium',
307 'Burkina Faso', 'Bulgaria', 'Bahrain', 'Burundi', 'Benin', 'Bermuda',
308 'Brunei Darussalam', 'Bolivia', 'Brazil', 'Bahamas', 'Bhutan',
309 'Bouvet Island', 'Botswana', 'Belarus', 'Belize', 'Canada',
310 'Cocos (Keeling) Islands', 'Congo, The Democratic Republic of the',
311 'Central African Republic', 'Congo', 'Switzerland', 'Cote D\'Ivoire',
312 'Cook Islands', 'Chile', 'Cameroon', 'China', 'Colombia', 'Costa Rica',
313 'Cuba', 'Cape Verde', 'Christmas Island', 'Cyprus', 'Czech Republic',
314 'Germany', 'Djibouti', 'Denmark', 'Dominica', 'Dominican Republic',
315 'Algeria', 'Ecuador', 'Estonia', 'Egypt', 'Western Sahara', 'Eritrea',
316 'Spain', 'Ethiopia', 'Finland', 'Fiji', 'Falkland Islands (Malvinas)',
317 'Micronesia, Federated States of', 'Faroe Islands', 'France',
318 'France, Metropolitan', 'Gabon', 'United Kingdom', 'Grenada', 'Georgia',
319 'French Guiana', 'Ghana', 'Gibraltar', 'Greenland', 'Gambia', 'Guinea',
320 'Guadeloupe', 'Equatorial Guinea', 'Greece',
321 'South Georgia and the South Sandwich Islands', 'Guatemala', 'Guam',
322 'Guinea-Bissau', 'Guyana', 'Hong Kong',
323 'Heard Island and McDonald Islands', 'Honduras', 'Croatia', 'Haiti',
324 'Hungary', 'Indonesia', 'Ireland', 'Israel', 'India',
325 'British Indian Ocean Territory', 'Iraq', 'Iran, Islamic Republic of',
326 'Iceland', 'Italy', 'Jamaica', 'Jordan', 'Japan', 'Kenya', 'Kyrgyzstan',
327 'Cambodia', 'Kiribati', 'Comoros', 'Saint Kitts and Nevis',
328 'Korea, Democratic People\'s Republic of', 'Korea, Republic of', 'Kuwait',
329 'Cayman Islands', 'Kazakhstan', 'Lao People\'s Democratic Republic',
330 'Lebanon', 'Saint Lucia', 'Liechtenstein', 'Sri Lanka', 'Liberia',
331 'Lesotho', 'Lithuania', 'Luxembourg', 'Latvia', 'Libya', 'Morocco',
332 'Monaco', 'Moldova, Republic of', 'Madagascar', 'Marshall Islands',
333 'Macedonia', 'Mali', 'Myanmar', 'Mongolia', 'Macau',
334 'Northern Mariana Islands', 'Martinique', 'Mauritania', 'Montserrat',
335 'Malta', 'Mauritius', 'Maldives', 'Malawi', 'Mexico', 'Malaysia',
336 'Mozambique', 'Namibia', 'New Caledonia', 'Niger', 'Norfolk Island',
337 'Nigeria', 'Nicaragua', 'Netherlands', 'Norway', 'Nepal', 'Nauru', 'Niue',
338 'New Zealand', 'Oman', 'Panama', 'Peru', 'French Polynesia',
339 'Papua New Guinea', 'Philippines', 'Pakistan', 'Poland',
340 'Saint Pierre and Miquelon', 'Pitcairn Islands', 'Puerto Rico',
341 'Palestinian Territory', 'Portugal', 'Palau', 'Paraguay', 'Qatar',
342 'Reunion', 'Romania', 'Russian Federation', 'Rwanda', 'Saudi Arabia',
343 'Solomon Islands', 'Seychelles', 'Sudan', 'Sweden', 'Singapore',
344 'Saint Helena', 'Slovenia', 'Svalbard and Jan Mayen', 'Slovakia',
345 'Sierra Leone', 'San Marino', 'Senegal', 'Somalia', 'Suriname',
346 'Sao Tome and Principe', 'El Salvador', 'Syrian Arab Republic',
347 'Swaziland', 'Turks and Caicos Islands', 'Chad',
348 'French Southern Territories', 'Togo', 'Thailand', 'Tajikistan', 'Tokelau',
349 'Turkmenistan', 'Tunisia', 'Tonga', 'Timor-Leste', 'Turkey',
350 'Trinidad and Tobago', 'Tuvalu', 'Taiwan', 'Tanzania, United Republic of',
351 'Ukraine', 'Uganda', 'United States Minor Outlying Islands',
352 'United States', 'Uruguay', 'Uzbekistan', 'Holy See (Vatican City State)',
353 'Saint Vincent and the Grenadines', 'Venezuela', 'Virgin Islands, British',
354 'Virgin Islands, U.S.', 'Vietnam', 'Vanuatu', 'Wallis and Futuna', 'Samoa',
355 'Yemen', 'Mayotte', 'Serbia', 'South Africa', 'Zambia', 'Montenegro',
356 'Zimbabwe', 'Anonymous Proxy', 'Satellite Provider', 'Other',
357 'Aland Islands', 'Guernsey', 'Isle of Man', 'Jersey', 'Saint Barthelemy',
358 'Saint Martin'
359 )
360
361
362 STANDARD = 0
363 MEMORY_CACHE = 1
364 MMAP_CACHE = 8
365
366
367 COUNTRY_BEGIN = 16776960
368 STATE_BEGIN_REV0 = 16700000
369 STATE_BEGIN_REV1 = 16000000
370
371 STRUCTURE_INFO_MAX_SIZE = 20
372 DATABASE_INFO_MAX_SIZE = 100
373
374
375 COUNTRY_EDITION = 1
376 COUNTRY_EDITION_V6 = 12
377 REGION_EDITION_REV0 = 7
378 REGION_EDITION_REV1 = 3
379 CITY_EDITION_REV0 = 6
380 CITY_EDITION_REV1 = 2
381 ORG_EDITION = 5
382 ISP_EDITION = 4
383 ASNUM_EDITION = 9
384
385 PROXY_EDITION = 8
386 NETSPEED_EDITION = 11
387
388
389 IPV6_EDITIONS = (COUNTRY_EDITION_V6,)
390 CITY_EDITIONS = (CITY_EDITION_REV0, CITY_EDITION_REV1)
391 REGION_EDITIONS = (REGION_EDITION_REV0, REGION_EDITION_REV1)
392 REGION_CITY_EDITIONS = REGION_EDITIONS + CITY_EDITIONS
393
394 SEGMENT_RECORD_LENGTH = 3
395 STANDARD_RECORD_LENGTH = 3
396 ORG_RECORD_LENGTH = 4
397 MAX_RECORD_LENGTH = 4
398 MAX_ORG_RECORD_LENGTH = 300
399 FULL_RECORD_LENGTH = 50
400
401 US_OFFSET = 1
402 CANADA_OFFSET = 677
403 WORLD_OFFSET = 1353
404 FIPS_RANGE = 360
405