sq krb_pa_supported_enctypes
[metze/wireshark/wip.git] / mmdbresolve.c
1 /* Read IPv4 and IPv6 addresses on stdin and print their MMDB entries on stdout.
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This progam uses the MaxMind DB library (libmaxminddb) and MUST be
8  * compatible with its license (Apache 2.0).
9  *
10  * SPDX-License-Identifier: MIT
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18
19 #include <maxminddb.h>
20
21 #define MAX_ADDR_LEN 46
22 #define MMDBR_STRINGIFY(x) MMDBR_STRINGIFY_S(x)
23 #define MMDBR_STRINGIFY_S(s) #s
24 #define OUT_BUF_SIZE 65536
25
26 // Uncomment to enable slow lookups. Only useful on Windows for now.
27 // #define MMDB_DEBUG_SLOW 1
28
29 #ifdef MMDB_DEBUG_SLOW
30 #ifdef _WIN32
31 #include <Windows.h>
32 #endif
33 #endif
34
35 static const char *co_iso_key[]     = {"country", "iso_code", NULL};
36 static const char *co_name_key[]    = {"country", "names", "en", NULL};
37 static const char *ci_name_key[]    = {"city", "names", "en", NULL};
38 static const char *asn_o_key[]      = {"autonomous_system_organization", NULL};
39 static const char *asn_key[]        = {"autonomous_system_number", NULL};
40 static const char *l_lat_key[]      = {"location", "latitude", NULL};
41 static const char *l_lon_key[]      = {"location", "longitude", NULL};
42 static const char *empty_key[]      = {NULL};
43
44 static const char **lookup_keys[] = {
45     co_iso_key,
46     co_name_key,
47     ci_name_key,
48     asn_o_key,
49     asn_key,
50     l_lat_key,
51     l_lon_key,
52     empty_key
53 };
54
55 static void exit_err(void) {
56     fprintf(stderr, "Usage: mmdbresolve -f db_file [-f db_file ...]\n");
57     exit(1);
58 }
59
60 int
61 main(int argc, char *argv[])
62 {
63     char addr_str[MAX_ADDR_LEN+1];
64     size_t mmdb_count = 0;
65     MMDB_s *mmdbs = NULL, *new_mmdbs;
66     int mmdb_err;
67
68     char *out_buf = (char *) malloc(OUT_BUF_SIZE);
69     setvbuf(stdout, out_buf, _IOFBF, OUT_BUF_SIZE);
70
71     fprintf(stdout, "[init]\n");
72
73     // If we need to handle anything beyond "-f" we'll probably want to
74     // link with GLib and use GOption.
75     int arg_idx = 0;
76     while (arg_idx < argc - 1) {
77         if (strcmp(argv[arg_idx], "-f") == 0) {
78             arg_idx++;
79             const char *db_arg = argv[arg_idx];
80             MMDB_s try_mmdb;
81             mmdb_err = MMDB_open(db_arg, 0, &try_mmdb);
82             fprintf(stdout, "db.%zd.path: %s\n", mmdb_count, db_arg);
83             fprintf(stdout, "db.%zd.status: ", mmdb_count);
84             if (mmdb_err == MMDB_SUCCESS) {
85                 mmdb_count++;
86                 new_mmdbs = (MMDB_s *) realloc(mmdbs, mmdb_count * sizeof(MMDB_s));
87                 if (new_mmdbs == NULL) {
88                     free(mmdbs);
89                     fprintf(stdout, "ERROR out of memory\n");
90                     return 1;
91                 }
92                 mmdbs = new_mmdbs;
93                 mmdbs[mmdb_count - 1] = try_mmdb;
94                 fprintf(stdout, "OK\n");
95                 fprintf(stdout, "db.%zd.type: %s\n", mmdb_count, mmdbs[mmdb_count - 1].metadata.database_type);
96             } else {
97                 fprintf(stdout, "ERROR %s\n", MMDB_strerror(mmdb_err));
98             }
99         }
100         arg_idx++;
101     }
102
103     fprintf(stdout, "mmdbresolve.status: %s\n", mmdb_count > 0 ? "true": "false");
104     fprintf(stdout, "# End init\n");
105     fflush(stdout);
106
107     if (arg_idx != argc || mmdb_count < 1) {
108         exit_err();
109     }
110
111     while (!feof(stdin)) {
112         int gai_err;
113
114         if (fscanf(stdin, "%" MMDBR_STRINGIFY(MAX_ADDR_LEN) "s", addr_str) < 1) {
115             continue;
116         }
117         fprintf(stdout, "[%s]\n", addr_str);
118
119 #ifdef MMDB_DEBUG_SLOW
120 #ifdef _WIN32
121         Sleep(1000);
122 #endif
123 #endif
124
125         for (size_t mmdb_idx = 0; mmdb_idx < mmdb_count; mmdb_idx++) {
126             fprintf(stdout, "# %s\n", mmdbs[mmdb_idx].metadata.database_type);
127             MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[mmdb_idx], addr_str, &gai_err, &mmdb_err);
128
129             if (result.found_entry && gai_err == 0 && mmdb_err == MMDB_SUCCESS) {
130                 for (size_t key_idx = 0; lookup_keys[key_idx][0]; key_idx++) {
131                     MMDB_entry_data_s entry_data;
132                     int status = MMDB_aget_value(&result.entry, &entry_data, lookup_keys[key_idx]);
133                     if (status == MMDB_SUCCESS && entry_data.has_data) {
134                         char *sep = "";
135                         for (int idx = 0; lookup_keys[key_idx][idx] != 0; idx++) {
136                             fprintf(stdout, "%s%s", sep, lookup_keys[key_idx][idx]);
137                             sep = ".";
138                         }
139                         switch (entry_data.type) {
140                             case MMDB_DATA_TYPE_UTF8_STRING:
141                             {
142                                 char len_fmt[12]; // : %.xxxxxs\n\0
143                                 snprintf(len_fmt, 11, ": %%.%us\n", entry_data.data_size);
144                                 fprintf(stdout, len_fmt, entry_data.utf8_string);
145                             }
146                             break;
147                             case MMDB_DATA_TYPE_UINT16:
148                                 fprintf(stdout, ": %u\n", entry_data.uint16);
149                                 break;
150                             case MMDB_DATA_TYPE_UINT32:
151                                 fprintf(stdout, ": %u\n", entry_data.uint32);
152                                 break;
153                             case MMDB_DATA_TYPE_INT32:
154                                 fprintf(stdout, ": %d\n", entry_data.int32);
155                                 break;
156                             case MMDB_DATA_TYPE_BOOLEAN:
157                                 fprintf(stdout, ": %s\n", entry_data.boolean ? "True" : "False");
158                                 break;
159                             case MMDB_DATA_TYPE_DOUBLE:
160                                 fprintf(stdout, ": %f\n", entry_data.double_value);
161                                 break;
162                             case MMDB_DATA_TYPE_FLOAT:
163                                 fprintf(stdout, ": %f\n", entry_data.float_value);
164                                 break;
165                             default:
166                                 fprintf(stdout, ": UNKNOWN (%u)\n", entry_data.type);
167                         }
168                     }
169                 }
170             } else {
171                 // dump error info.
172             }
173         }
174         fprintf(stdout, "# End %s\n", addr_str);
175         fflush(stdout);
176     }
177
178     free(mmdbs);
179
180     return 0;
181 }
182
183 /*
184  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
185  *
186  * Local variables:
187  * c-basic-offset: 4
188  * tab-width: 8
189  * indent-tabs-mode: nil
190  * End:
191  *
192  * vi: set shiftwidth=4 tabstop=8 expandtab:
193  * :indentSize=4:tabSize=8:noTabs=true:
194  */