This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkmmdb-client.cpp
88 lines (83 loc) · 2.7 KB
/
mkmmdb-client.cpp
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
#include <string.h>
#include <iostream>
#include <ios>
#include "mkmmdb.hpp"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion"
#endif // __clang__
#include "argh.h"
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
// LCOV_EXCL_START
static void usage() {
std::clog << "Usage: mkmmdb-client [-k] [--asn-db <path>] "
<< "[--country-db <path>] <ip>" << std::endl;
}
// LCOV_EXCL_STOP
int main(int, char **argv) {
std::string asn_db = ".mkbuild/download/asn.mmdb";
std::string country_db = ".mkbuild/download/country.mmdb";
bool keep_going = false;
std::string ip;
{
argh::parser cmdline;
cmdline.add_param("asn-db");
cmdline.add_param("country-db");
cmdline.parse(argv);
for (auto &flag : cmdline.flags()) {
if (flag == "k") {
keep_going = true;
} else {
// LCOV_EXCL_START
std::clog << "fatal: unrecognized flag: " << flag << std::endl;
usage();
exit(EXIT_FAILURE);
// LCOV_EXCL_STOP
}
}
for (auto ¶m : cmdline.params()) {
if (param.first == "asn-db") {
asn_db = param.second;
} else if (param.first == "country-db") {
country_db = param.second;
} else {
// LCOV_EXCL_START
std::clog << "fatal: unrecognized param: " << param.first << std::endl;
usage();
exit(EXIT_FAILURE);
// LCOV_EXCL_STOP
}
}
auto sz = cmdline.pos_args().size();
if (sz != 2) {
// LCOV_EXCL_START
usage();
exit(EXIT_FAILURE);
// LCOV_EXCL_STOP
}
ip = cmdline.pos_args()[1];
}
std::clog << std::boolalpha;
#define LOOKUP(Database, Feature) \
do { \
std::vector<std::string> logs; \
mk::mmdb::Handle db; \
std::string value; \
bool ok = db.open(Database, logs); \
std::clog << "open: " << ok << std::endl; \
ok = ok || keep_going; \
if (ok) { \
ok = db.lookup_##Feature(ip, value, logs); \
std::clog << "lookup: " << ok << std::endl; \
ok = ok || keep_going; \
std::clog << #Feature << ": " << value << std::endl; \
} \
for (const std::string &s : logs) std::clog << s; \
if (!ok) exit(EXIT_FAILURE); \
} while (0)
LOOKUP(country_db, cc);
LOOKUP(asn_db, asn2);
LOOKUP(asn_db, org);
}