1 /* Read IPv4 and IPv6 addresses on stdin and print their MMDB entries on stdout.
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * This progam uses the MaxMind DB library (libmaxminddb) and MUST be
8 * compatible with its license (Apache 2.0).
10 * SPDX-License-Identifier: MIT
19 #include <maxminddb.h>
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
26 static const char *co_iso_key[] = {"country", "iso_code", NULL};
27 static const char *co_name_key[] = {"country", "names", "en", NULL};
28 static const char *ci_name_key[] = {"city", "names", "en", NULL};
29 static const char *asn_o_key[] = {"autonomous_system_organization", NULL};
30 static const char *asn_key[] = {"autonomous_system_number", NULL};
31 static const char *l_lat_key[] = {"location", "latitude", NULL};
32 static const char *l_lon_key[] = {"location", "longitude", NULL};
33 static const char *empty_key[] = {NULL};
35 static const char **lookup_keys[] = {
46 static void exit_err(void) {
47 fprintf(stderr, "Usage: mmdbresolve -f db_file [-f db_file ...]\n");
52 main(int argc, char *argv[])
54 char addr_str[MAX_ADDR_LEN+1];
55 size_t mmdb_count = 0;
56 MMDB_s *mmdbs = NULL, *new_mmdbs;
59 char *out_buf = (char *) malloc(OUT_BUF_SIZE);
60 setvbuf(stdout, out_buf, _IOFBF, OUT_BUF_SIZE);
62 fprintf(stdout, "[init]\n");
64 // If we need to handle anything beyond "-f" we'll probably want to
65 // link with GLib and use GOption.
67 while (arg_idx < argc - 1) {
68 if (strcmp(argv[arg_idx], "-f") == 0) {
70 const char *db_arg = argv[arg_idx];
72 mmdb_err = MMDB_open(db_arg, 0, &try_mmdb);
73 fprintf(stdout, "db.%zd.path: %s\n", mmdb_count, db_arg);
74 fprintf(stdout, "db.%zd.status: ", mmdb_count);
75 if (mmdb_err == MMDB_SUCCESS) {
77 new_mmdbs = (MMDB_s *) realloc(mmdbs, mmdb_count * sizeof(MMDB_s));
78 if (new_mmdbs == NULL) {
80 fprintf(stdout, "ERROR out of memory\n");
84 mmdbs[mmdb_count - 1] = try_mmdb;
85 fprintf(stdout, "OK\n");
86 fprintf(stdout, "db.%zd.type: %s\n", mmdb_count, mmdbs[mmdb_count - 1].metadata.database_type);
88 fprintf(stdout, "ERROR %s\n", MMDB_strerror(mmdb_err));
94 fprintf(stdout, "mmdbresolve.status: %s\n", mmdb_count > 0 ? "true": "false");
95 fprintf(stdout, "# End init\n");
98 if (arg_idx != argc || mmdb_count < 1) {
102 while (!feof(stdin)) {
105 if (fscanf(stdin, "%" MMDBR_STRINGIFY(MAX_ADDR_LEN) "s", addr_str) < 1) {
108 fprintf(stdout, "[%s]\n", addr_str);
110 for (size_t mmdb_idx = 0; mmdb_idx < mmdb_count; mmdb_idx++) {
111 fprintf(stdout, "# %s\n", mmdbs[mmdb_idx].metadata.database_type);
112 MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[mmdb_idx], addr_str, &gai_err, &mmdb_err);
114 if (result.found_entry && gai_err == 0 && mmdb_err == MMDB_SUCCESS) {
115 for (size_t key_idx = 0; lookup_keys[key_idx][0]; key_idx++) {
116 MMDB_entry_data_s entry_data;
117 int status = MMDB_aget_value(&result.entry, &entry_data, lookup_keys[key_idx]);
118 if (status == MMDB_SUCCESS && entry_data.has_data) {
120 for (int idx = 0; lookup_keys[key_idx][idx] != 0; idx++) {
121 fprintf(stdout, "%s%s", sep, lookup_keys[key_idx][idx]);
124 switch (entry_data.type) {
125 case MMDB_DATA_TYPE_UTF8_STRING:
127 char len_fmt[12]; // : %.xxxxxs\n\0
128 snprintf(len_fmt, 11, ": %%.%us\n", entry_data.data_size);
129 fprintf(stdout, len_fmt, entry_data.utf8_string);
132 case MMDB_DATA_TYPE_UINT16:
133 fprintf(stdout, ": %u\n", entry_data.uint16);
135 case MMDB_DATA_TYPE_UINT32:
136 fprintf(stdout, ": %u\n", entry_data.uint32);
138 case MMDB_DATA_TYPE_INT32:
139 fprintf(stdout, ": %d\n", entry_data.int32);
141 case MMDB_DATA_TYPE_BOOLEAN:
142 fprintf(stdout, ": %s\n", entry_data.boolean ? "True" : "False");
144 case MMDB_DATA_TYPE_DOUBLE:
145 fprintf(stdout, ": %f\n", entry_data.double_value);
147 case MMDB_DATA_TYPE_FLOAT:
148 fprintf(stdout, ": %f\n", entry_data.float_value);
151 fprintf(stdout, ": UNKNOWN (%u)\n", entry_data.type);
159 fprintf(stdout, "# End %s\n", addr_str);
169 * Editor modelines - http://www.wireshark.org/tools/modelines.html
174 * indent-tabs-mode: nil
177 * vi: set shiftwidth=4 tabstop=8 expandtab:
178 * :indentSize=4:tabSize=8:noTabs=true: