1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2/* test-geoip-isp.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
36    if (argc == 2)
37        if (!strcmp(argv[1], "gen"))
38            generate = 1;
39
40    gi = GeoIP_open("../data/GeoIPISP.dat", GEOIP_STANDARD);
41
42    if (gi == NULL) {
43        fprintf(stderr, "Error opening database\n");
44        exit(1);
45    }
46
47    f = fopen("isp_test.txt", "r");
48
49    if (f == NULL) {
50        fprintf(stderr, "Error opening isp_test.txt\n");
51        exit(1);
52    }
53
54    while (fscanf(f, "%s", host) != EOF) {
55        org = GeoIP_org_by_name(gi, (const char *)host);
56
57        if (org != NULL) {
58            printf("%s\t%s\n", host, _mk_NA(org));
59            free(org);
60        }
61    }
62
63    fclose(f);
64    GeoIP_delete(gi);
65
66    return 0;
67}
68