1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2/* test-geoip-org.c
3 *
4 * Copyright (C) 2006 MaxMind LLC
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "GeoIP.h"
22
23static const char *_mk_NA(const char *p)
24{
25    return p ? p : "N/A";
26}
27
28int main(int argc, char *argv[])
29{
30    FILE *f;
31    GeoIP *gi;
32    char *org;
33    int generate = 0;
34    char host[50];
35    char **ret;
36    if (argc == 2)
37        if (!strcmp(argv[1], "gen"))
38            generate = 1;
39
40    gi = GeoIP_open("../data/GeoIPOrg.dat", GEOIP_INDEX_CACHE);
41
42    if (gi == NULL) {
43        fprintf(stderr, "Error opening database\n");
44        exit(1);
45    }
46
47    f = fopen("org_test.txt", "r");
48
49    if (f == NULL) {
50        fprintf(stderr, "Error opening org_test.txt\n");
51        exit(1);
52    }
53
54    printf("IP\torganization\tnetmask\tbeginIp\tendIp\n");
55    while (fscanf(f, "%s", host) != EOF) {
56        org = GeoIP_name_by_name(gi, (const char *)host);
57
58        if (org != NULL) {
59            ret = GeoIP_range_by_ip(gi, (const char *)host);
60
61            printf("%s\t%s\t%d\t%s\t%s\n", host, _mk_NA(org),
62                   GeoIP_last_netmask(gi), ret[0], ret[1]);
63            GeoIP_range_by_ip_delete(ret);
64            free(org);
65        }
66    }
67
68    fclose(f);
69    GeoIP_delete(gi);
70    return 0;
71}
72