Skip to content

Commit 00f407c

Browse files
author
Yorhel
committed
Actually add geoip.c + improved error handling
1 parent 6a60a75 commit 00f407c

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

src/geoip.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,9 @@ int main(int argc, char **argv) {
451451
listen_global_init();
452452
cc_global_init();
453453
dl_init_global();
454-
geoip_reinit();
455454
ui_cmdhist_init("history");
456455
ui_init(bracketed_paste);
456+
geoip_reinit();
457457

458458
// setup SIGWINCH
459459
struct sigaction act;

src/uit_userlist.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ static void draw_row(ui_listing_t *list, GSequenceIter *iter, int row, void *dat
167167
mvaddch(row, 4, 't');
168168

169169
int j = 6;
170-
DRAW_COL(row, j, t->cw_country,
171-
!ip4_isany(user->ip4) ? geoip_country4(ip4_unpack(user->ip4)) :
172-
!ip6_isany(user->ip6) ? geoip_country6(ip6_unpack(user->ip6)) : "");
170+
const char *cc =
171+
!ip4_isany(user->ip4) ? geoip_country4(ip4_unpack(user->ip4)) :
172+
!ip6_isany(user->ip6) ? geoip_country6(ip6_unpack(user->ip6)) : NULL;
173+
DRAW_COL(row, j, t->cw_country, cc?cc:"");
173174
if(t->cw_user > 1)
174175
ui_listing_draw_match(list, iter, row, j, str_offset_from_columns(user->name, t->cw_user-1));
175176
j += t->cw_user;

0 commit comments

Comments
 (0)