7 typedef struct Country Country;
17 {"af", "afghanistan"},
22 {"kp", "north korea"},
30 // the original, us and canada
31 {"us", "united states of america"},
37 { "eu", "european union" },
42 { "lu", "luxembourg" },
43 { "nl", "netherlands" },
46 { "gb", "great britain" },
47 { "uk", "united kingdom" },
51 { "au", "australia" },
58 {"cz", "czech republic"},
62 {"ch", "switzerland"},
63 {"nz", "new zealand"},
79 { "ae", "united arab emirates" },
80 { "af", "afghanistan" },
81 { "ag", "antigua and barbuda" },
85 { "an", "netherlands antilles" },
87 { "aq", "antarctica" },
88 { "ar", "argentina" },
89 { "as", "american samoa" },
91 { "au", "australia" },
93 { "az", "azerbaijan" },
94 { "ba", "bosnia and herzegovina" },
96 { "bd", "bangladesh" },
98 { "bf", "burkina faso" },
104 { "bn", "brunei darussalam" },
110 { "bv", "bouvet island" },
111 { "bw", "botswana" },
115 { "cc", "cocos (keeling) islands" },
116 { "cf", "central african republic" },
118 { "ch", "switzerland" },
119 { "ci", "cote d'ivoire (ivory coast)" },
120 { "ck", "cook islands" },
122 { "cm", "cameroon" },
124 { "co", "colombia" },
125 { "cr", "costa rica" },
126 { "cs", "czechoslovakia (former)" },
127 { "ct", "canton and enderbury island" },
129 { "cv", "cape verde" },
130 { "cx", "christmas island" },
132 { "cz", "czech republic" },
133 { "dd", "german democratic republic" },
135 { "dj", "djibouti" },
137 { "dm", "dominica" },
138 { "do", "dominican republic" },
143 { "eh", "western sahara" },
146 { "et", "ethiopia" },
147 { "eu", "european union" },
150 { "fk", "falkland islands (malvinas)" },
151 { "fm", "micronesia" },
152 { "fo", "faroe islands" },
154 { "fx", "france, metropolitan" },
156 { "gb", "great britain (uk)" },
159 { "gf", "french guiana" },
161 { "gi", "gibraltar" },
162 { "gl", "greenland" },
165 { "gp", "guadeloupe" },
166 { "gq", "equatorial guinea" },
168 { "gs", "s. georgia and s. sandwich isls." },
169 { "gt", "guatemala" },
171 { "gw", "guinea-bissau" },
173 { "hk", "hong kong" },
174 { "hm", "heard and mcdonald islands" },
175 { "hn", "honduras" },
176 { "hr", "croatia (hrvatska)" },
179 { "id", "indonesia" },
183 { "io", "british indian ocean territory" },
191 { "jt", "johnston island" },
193 { "kg", "kyrgyzstan" },
194 { "kh", "cambodia (democratic kampuchea)" },
195 { "ki", "kiribati" },
197 { "kn", "saint kitts and nevis" },
198 { "kp", "korea (north)" },
199 { "kr", "korea (south)" },
201 { "ky", "cayman islands" },
202 { "kz", "kazakhstan" },
205 { "lc", "saint lucia" },
206 { "li", "liechtenstein" },
207 { "lk", "sri lanka" },
210 { "lt", "lithuania" },
211 { "lu", "luxembourg" },
217 { "mg", "madagascar" },
218 { "mh", "marshall islands" },
219 { "mi", "midway islands" },
220 { "mk", "macedonia" },
223 { "mn", "mongolia" },
225 { "mp", "northern mariana islands" },
226 { "mq", "martinique" },
227 { "mr", "mauritania" },
228 { "ms", "montserrat" },
230 { "mu", "mauritius" },
231 { "mv", "maldives" },
234 { "my", "malaysia" },
235 { "mz", "mozambique" },
237 { "nc", "new caledonia" },
239 { "nf", "norfolk island" },
241 { "ni", "nicaragua" },
242 { "nl", "netherlands" },
245 { "nq", "dronning maud land" },
247 { "nt", "neutral zone" },
249 { "nz", "new zealand (aotearoa)" },
252 { "pc", "pacific islands" },
254 { "pf", "french polynesia" },
255 { "pg", "papua new guinea" },
256 { "ph", "philippines" },
257 { "pk", "pakistan" },
259 { "pm", "st. pierre and miquelon" },
260 { "pn", "pitcairn" },
261 { "pr", "puerto rico" },
262 { "pu", "united states misc. pacific islands" },
263 { "pt", "portugal" },
265 { "py", "paraguay" },
269 { "ru", "russian federation" },
271 { "sa", "saudi arabia" },
272 { "sb", "solomon islands" },
273 { "sc", "seychelles" },
276 { "sg", "singapore" },
277 { "sh", "st. helena" },
278 { "si", "slovenia" },
279 { "sj", "svalbard and jan mayen islands" },
280 { "sk", "slovak republic" },
281 { "sl", "sierra leone" },
282 { "sm", "san marino" },
285 { "sr", "suriname" },
286 { "st", "sao tome and principe" },
287 { "su", "ussr (former)" },
288 { "sv", "el salvador" },
290 { "sz", "swaziland" },
291 { "tc", "turks and caicos islands" },
293 { "tf", "french southern territories" },
295 { "th", "thailand" },
296 { "tj", "tajikistan" },
298 { "tm", "turkmenistan" },
301 { "tp", "east timor" },
303 { "tt", "trinidad and tobago" },
306 { "tz", "tanzania" },
309 { "uk", "united kingdom" },
310 { "um", "us minor outlying islands" },
311 { "us", "united states" },
313 { "uz", "uzbekistan" },
314 { "va", "vatican city state (holy see)" },
315 { "vc", "saint vincent and the grenadines" },
316 { "ve", "venezuela" },
317 { "vg", "virgin islands (british)" },
318 { "vi", "virgin islands (u.s.)" },
319 { "vn", "viet nam" },
321 { "wf", "wallis and futuna islands" },
322 { "wk", "wake island" },
324 { "yd", "democratic yemen" },
327 { "yu", "yugoslavia" },
328 { "za", "south africa" },
331 { "zw", "zimbabwe" },
342 incountries(char *s, Country *cp)
344 for(; cp->code != 0; cp++)
345 if(cistrcmp(s, cp->code) == 0
346 || cistrcmp(s, cp->name) == 0)
352 indomains(char *s, char **dp)
354 for(; *dp != nil; dp++)
355 if(cistrcmp(s, *dp) == 0)
362 classify(char *ip, Ndbtuple *t)
364 int isgov, iscountry, isbadc, isgoodc;
370 isgov = iscountry = isbadc = 0;
373 for(nt = t; nt != nil; nt = nt->entry){
374 if(strcmp(nt->attr, "country") == 0){
376 if(incountries(nt->val, badc)){
377 if(classdebug)fprint(2, "isbadc\n");
380 } else if(!incountries(nt->val, goodc)){
381 if(classdebug)fprint(2, "!isgoodc\n");
386 /* domain names can always hurt, even without forward verification */
387 if(strcmp(nt->attr, "dom") == 0){
388 strncpy(dom, nt->val, sizeof dom);
389 dom[sizeof(dom)-1] = 0;
390 n = getfields(dom, df, nelem(df), 0, ".");
392 /* a bad country in a domain name is always believed */
393 if(incountries(df[n-1], badc)){
394 if(classdebug)fprint(2, "isbadc dom\n");
399 /* a goverment in a domain name is always believed */
400 if(n > 1 && indomains(df[n-2], gov))
405 /* did the forward lookup work? */
406 for(nt = t; nt != nil; nt = nt->entry){
407 if(strcmp(nt->attr, "ip") == 0 && strcmp(nt->val, ip) == 0)
411 /* see if the domain name ends in a country code */
412 if(nt != nil && (x = ndbfindattr(t, nt, "dom")) != nil){
413 strncpy(dom, x->val, sizeof dom);
414 dom[sizeof(dom)-1] = 0;
415 n = getfields(dom, df, nelem(df), 0, ".");
416 if(incountries(df[n-1], allc))
424 if(!isgoodc && isgov)