summaryrefslogtreecommitdiffstats
path: root/libGeoIP/GeoIPCity.c
blob: 543f829fcb74ecd18a629b34477fb29fda60aa6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * GeoIPCity.c
 *
 * Copyright (C) 2016 MaxMind, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "GeoIPCity.h"
#include "GeoIP.h"
#include "GeoIP_internal.h"
#if !defined(_WIN32)
#include <netdb.h>
#include <netinet/in.h> /* For ntohl */
#include <unistd.h>
#else
#include <windows.h>
#include <winsock.h>

#if defined(_MSC_VER) &&                                                       \
    _MSC_VER >= 1400 // VS 2005+ deprecates fileno, lseek and read
#define fileno _fileno
#define read _read
#define lseek _lseek
#endif
#endif
#include <sys/types.h> /* For uint32_t */
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uint32_t */
#endif

#if defined(_WIN32) && !defined(__MINGW32__)
#include "pread.h"
#endif

#ifndef HAVE_PREAD
#define pread(fd, buf, count, offset)                                          \
    (lseek(fd, offset, SEEK_SET) == offset ? read(fd, buf, count) : -1)
#endif /* HAVE_PREAD */

static const int FULL_RECORD_LENGTH = 50;

static GeoIPRecord *
_extract_record(GeoIP *gi, unsigned int seek_record, int *next_record_ptr) {
    int record_pointer;
    unsigned char *record_buf = NULL;
    unsigned char *begin_record_buf = NULL;
    GeoIPRecord *record;
    int str_length = 0;
    int j;
    double latitude = 0, longitude = 0;
    int metroarea_combo = 0;
    int bytes_read = 0;
    if (seek_record == gi->databaseSegments[0]) {
        return NULL;
    }

    record = malloc(sizeof(GeoIPRecord));
    memset(record, 0, sizeof(GeoIPRecord));
    record->charset = gi->charset;

    record_pointer =
        seek_record + (2 * gi->record_length - 1) * gi->databaseSegments[0];

    if (gi->cache == NULL) {
        begin_record_buf = record_buf =
            malloc(sizeof(unsigned char) * FULL_RECORD_LENGTH);
        bytes_read = pread(fileno(gi->GeoIPDatabase),
                           record_buf,
                           FULL_RECORD_LENGTH,
                           record_pointer);
        if (bytes_read <= 0) {
            /* eof or other error */
            free(begin_record_buf);
            free(record);
            return NULL;
        }
    } else {
        if (gi->size <= record_pointer) {
            /* record does not exist in the cache */
            free(record);
            return NULL;
        }
        record_buf = gi->cache + (long)record_pointer;
    }

    /* get country */
    record->continent_code = (char *)GeoIP_country_continent[record_buf[0]];
    record->country_code = (char *)GeoIP_country_code[record_buf[0]];
    record->country_code3 = (char *)GeoIP_country_code3[record_buf[0]];
    record->country_name = (char *)GeoIP_country_name_by_id(gi, record_buf[0]);
    record_buf++;

    /* get region */
    while (record_buf[str_length] != '\0') {
        str_length++;
    }
    if (str_length > 0) {
        record->region = malloc(str_length + 1);
        strncpy(record->region, (char *)record_buf, str_length + 1);
    }
    record_buf += str_length + 1;
    str_length = 0;

    /* get city */
    while (record_buf[str_length] != '\0') {
        str_length++;
    }
    if (str_length > 0) {
        if (gi->charset == GEOIP_CHARSET_UTF8) {
            record->city = _GeoIP_iso_8859_1__utf8((const char *)record_buf);
        } else {
            record->city = malloc(str_length + 1);
            strncpy(record->city, (const char *)record_buf, str_length + 1);
        }
    }
    record_buf += (str_length + 1);
    str_length = 0;

    /* get postal code */
    while (record_buf[str_length] != '\0') {
        str_length++;
    }
    if (str_length > 0) {
        record->postal_code = malloc(str_length + 1);
        strncpy(record->postal_code, (char *)record_buf, str_length + 1);
    }
    record_buf += (str_length + 1);

    /* get latitude */
    for (j = 0; j < 3; ++j) {
        latitude += (record_buf[j] << (j * 8));
    }
    record->latitude = (float)(latitude / 10000 - 180);
    record_buf += 3;

    /* get longitude */
    for (j = 0; j < 3; ++j) {
        longitude += (record_buf[j] << (j * 8));
    }
    record->longitude = (float)(longitude / 10000 - 180);

    /*
     * get area code and metro code for post April 2002 databases and for US
     * locations
     */
    if (gi->databaseType == GEOIP_CITY_EDITION_REV1 ||
        gi->databaseType == GEOIP_CITY_EDITION_REV1_V6) {
        if (!strcmp(record->country_code, "US")) {
            record_buf += 3;
            for (j = 0; j < 3; ++j) {
                metroarea_combo += (record_buf[j] << (j * 8));
            }
            record->metro_code = metroarea_combo / 1000;
            record->area_code = metroarea_combo % 1000;
        }
    }

    if (begin_record_buf != NULL) {
        free(begin_record_buf);
    }

    /* Used for GeoIP_next_record */
    if (next_record_ptr != NULL) {
        *next_record_ptr = seek_record + record_buf - begin_record_buf + 3;
    }

    return record;
}

static GeoIPRecord *
_get_record_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl) {
    unsigned int seek_record;
    GeoIPRecord *r;
    if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
        gi->databaseType != GEOIP_CITY_EDITION_REV1) {
        printf("Invalid database type %s, expected %s\n",
               GeoIPDBDescription[(int)gi->databaseType],
               GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
        return NULL;
    }

    seek_record = _GeoIP_seek_record_gl(gi, ipnum, gl);
    r = _extract_record(gi, seek_record, NULL);
    if (r) {
        r->netmask = gl->netmask;
    }
    return r;
}

static GeoIPRecord *_get_record(GeoIP *gi, unsigned long ipnum) {
    GeoIPLookup gl;
    return _get_record_gl(gi, ipnum, &gl);
}

static GeoIPRecord *
_get_record_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl) {
    GeoIPRecord *r;
    unsigned int seek_record;
    if (gi->databaseType != GEOIP_CITY_EDITION_REV0_V6 &&
        gi->databaseType != GEOIP_CITY_EDITION_REV1_V6) {
        printf("Invalid database type %s, expected %s\n",
               GeoIPDBDescription[(int)gi->databaseType],
               GeoIPDBDescription[GEOIP_CITY_EDITION_REV1_V6]);
        return NULL;
    }

    seek_record = _GeoIP_seek_record_v6_gl(gi, ipnum, gl);
    r = _extract_record(gi, seek_record, NULL);
    if (r) {
        r->netmask = gl->netmask;
    }
    return r;
}

static GeoIPRecord *_get_record_v6(GeoIP *gi, geoipv6_t ipnum) {
    GeoIPLookup gl;
    return _get_record_v6_gl(gi, ipnum, &gl);
}

GeoIPRecord *GeoIP_record_by_ipnum(GeoIP *gi, unsigned long ipnum) {
    return _get_record(gi, ipnum);
}

GeoIPRecord *GeoIP_record_by_ipnum_v6(GeoIP *gi, geoipv6_t ipnum) {
    return _get_record_v6(gi, ipnum);
}

GeoIPRecord *GeoIP_record_by_addr(GeoIP *gi, const char *addr) {
    unsigned long ipnum;
    GeoIPLookup gl;
    if (addr == NULL) {
        return 0;
    }
    ipnum = GeoIP_addr_to_num(addr);
    return _get_record_gl(gi, ipnum, &gl);
}

GeoIPRecord *GeoIP_record_by_addr_v6(GeoIP *gi, const char *addr) {
    geoipv6_t ipnum;
    if (addr == NULL) {
        return 0;
    }
    ipnum = _GeoIP_addr_to_num_v6(addr);
    return _get_record_v6(gi, ipnum);
}

GeoIPRecord *GeoIP_record_by_name(GeoIP *gi, const char *name) {
    unsigned long ipnum;
    if (name == NULL) {
        return 0;
    }
    ipnum = _GeoIP_lookupaddress(name);
    return _get_record(gi, ipnum);
}

GeoIPRecord *GeoIP_record_by_name_v6(GeoIP *gi, const char *name) {
    geoipv6_t ipnum;
    if (name == NULL) {
        return 0;
    }
    ipnum = _GeoIP_lookupaddress_v6(name);
    return _get_record_v6(gi, ipnum);
}

int GeoIP_record_id_by_addr(GeoIP *gi, const char *addr) {
    unsigned long ipnum;
    if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
        gi->databaseType != GEOIP_CITY_EDITION_REV1) {
        printf("Invalid database type %s, expected %s\n",
               GeoIPDBDescription[(int)gi->databaseType],
               GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
        return 0;
    }
    if (addr == NULL) {
        return 0;
    }
    ipnum = GeoIP_addr_to_num(addr);
    return _GeoIP_seek_record(gi, ipnum);
}

int GeoIP_record_id_by_addr_v6(GeoIP *gi, const char *addr) {
    geoipv6_t ipnum;
    if (gi->databaseType != GEOIP_CITY_EDITION_REV0_V6 &&
        gi->databaseType != GEOIP_CITY_EDITION_REV1_V6) {
        printf("Invalid database type %s, expected %s\n",
               GeoIPDBDescription[(int)gi->databaseType],
               GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
        return 0;
    }
    if (addr == NULL) {
        return 0;
    }
    ipnum = _GeoIP_addr_to_num_v6(addr);
    return _GeoIP_seek_record_v6(gi, ipnum);
}

int GeoIP_init_record_iter(GeoIP *gi) { return gi->databaseSegments[0] + 1; }

int GeoIP_next_record(GeoIP *gi, GeoIPRecord **gir, int *record_iter) {
    if (gi->cache != NULL) {
        printf("GeoIP_next_record not supported in memory cache mode\n");
        return 1;
    }
    *gir = _extract_record(gi, *record_iter, record_iter);
    return 0;
}

void GeoIPRecord_delete(GeoIPRecord *gir) {
    if (gir == NULL) {
        return;
    }
    free(gir->region);
    free(gir->city);
    free(gir->postal_code);
    free(gir);
}