|
| 1 | +/* ncdc - NCurses Direct Connect client |
| 2 | +
|
| 3 | + Copyright (c) 2011-2014 Yoran Heling |
| 4 | +
|
| 5 | + Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + a copy of this software and associated documentation files (the |
| 7 | + "Software"), to deal in the Software without restriction, including |
| 8 | + without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + permit persons to whom the Software is furnished to do so, subject to |
| 11 | + the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included |
| 14 | + in all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +
|
| 24 | +*/ |
| 25 | + |
| 26 | + |
| 27 | +#include "ncdc.h" |
| 28 | +#include "geoip.h" |
| 29 | + |
| 30 | +gboolean geoip_available = FALSE; |
| 31 | + |
| 32 | +#ifdef USE_GEOIP |
| 33 | +static GeoIP *geoip4; |
| 34 | +static GeoIP *geoip6; |
| 35 | +#endif |
| 36 | + |
| 37 | + |
| 38 | +void geoip_reinit() { |
| 39 | +#ifdef USE_GEOIP |
| 40 | + if(geoip4) |
| 41 | + GeoIP_delete(geoip4); |
| 42 | + if(geoip6) |
| 43 | + GeoIP_delete(geoip6); |
| 44 | + /* Get the file paths directly, so that we can offer more useful diagnostic |
| 45 | + * messages in case we fail to open it. Calling GeoIP_db_avail() ensures that |
| 46 | + * the GeoIPDBFileName variable has been initialized. */ |
| 47 | + if(!GeoIPDBFileName) |
| 48 | + GeoIP_db_avail(GEOIP_COUNTRY_EDITION); |
| 49 | + const char *f4 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION]; |
| 50 | + const char *f6 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION_V6]; |
| 51 | + |
| 52 | + /* The '16' flag is GEOIP_SILENCE, but it's a fairly new option and not |
| 53 | + * defined in older versions. Just pass it along directly, ABI compatibility |
| 54 | + * should ensure this works with both old and new versions. |
| 55 | + * Also perform a g_file_test() first to ensure we're not opening |
| 56 | + * non-existing files. GeoIP versions that do not support GEOIP_SILENCE will |
| 57 | + * throw error messages on stdout, which screws up our ncurses UI, so |
| 58 | + * catching the most common error here is worth it. */ |
| 59 | + geoip4 = g_file_test(f4, G_FILE_TEST_EXISTS) ? GeoIP_open(f4, 16 | GEOIP_MEMORY_CACHE) : NULL; |
| 60 | + geoip6 = g_file_test(f6, G_FILE_TEST_EXISTS) ? GeoIP_open(f6, 16 | GEOIP_MEMORY_CACHE) : NULL; |
| 61 | + if(!geoip4) |
| 62 | + ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv4 addresses.", f4); |
| 63 | + if(!geoip6) |
| 64 | + ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv6 addresses.", f6); |
| 65 | + geoip_available = geoip4 || geoip6; |
| 66 | +#endif |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +const char *geoip_country4(const char *ip) { |
| 71 | +#ifdef USE_GEOIP |
| 72 | + if(geoip4) |
| 73 | + return GeoIP_country_code_by_addr(geoip4, ip); |
| 74 | +#endif |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +const char *geoip_country6(const char *ip) { |
| 80 | +#ifdef USE_GEOIP |
| 81 | + if(geoip6) |
| 82 | + return GeoIP_country_code_by_addr_v6(geoip6, ip); |
| 83 | +#endif |
| 84 | + return NULL; |
| 85 | +} |
0 commit comments