geoip2_c 0.3.4 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +23 -0
- data/.github/workflows/ubuntu.yml +17 -8
- data/.github/workflows/windows.yml +18 -8
- data/.gitmodules +0 -3
- data/ext/geoip2/extconf.rb +9 -25
- data/ext/geoip2/geoip2.c +82 -32
- data/geoip2_c.gemspec +1 -0
- data/lib/geoip2/version.rb +1 -1
- metadata +18 -182
- data/ext/geoip2/libmaxminddb/.gitignore +0 -38
- data/ext/geoip2/libmaxminddb/.gitmodules +0 -9
- data/ext/geoip2/libmaxminddb/.perltidyrc +0 -11
- data/ext/geoip2/libmaxminddb/.travis.yml +0 -70
- data/ext/geoip2/libmaxminddb/.uncrustify.cfg +0 -78
- data/ext/geoip2/libmaxminddb/AUTHORS +0 -0
- data/ext/geoip2/libmaxminddb/Changes.md +0 -277
- data/ext/geoip2/libmaxminddb/LICENSE +0 -202
- data/ext/geoip2/libmaxminddb/Makefile.am +0 -41
- data/ext/geoip2/libmaxminddb/NOTICE +0 -13
- data/ext/geoip2/libmaxminddb/README.dev.md +0 -69
- data/ext/geoip2/libmaxminddb/README.md +0 -122
- data/ext/geoip2/libmaxminddb/appveyor.yml +0 -33
- data/ext/geoip2/libmaxminddb/bin/Makefile.am +0 -10
- data/ext/geoip2/libmaxminddb/bin/mmdblookup.c +0 -715
- data/ext/geoip2/libmaxminddb/bootstrap +0 -21
- data/ext/geoip2/libmaxminddb/common.mk +0 -7
- data/ext/geoip2/libmaxminddb/configure.ac +0 -132
- data/ext/geoip2/libmaxminddb/dev-bin/make-man-pages.pl +0 -76
- data/ext/geoip2/libmaxminddb/dev-bin/ppa-release.sh +0 -53
- data/ext/geoip2/libmaxminddb/dev-bin/regen-prototypes.pl +0 -136
- data/ext/geoip2/libmaxminddb/dev-bin/regen-win32-test-projs.pl +0 -54
- data/ext/geoip2/libmaxminddb/dev-bin/release.sh +0 -113
- data/ext/geoip2/libmaxminddb/dev-bin/uncrustify-all.sh +0 -21
- data/ext/geoip2/libmaxminddb/dev-bin/valgrind-all.pl +0 -53
- data/ext/geoip2/libmaxminddb/doc/libmaxminddb.md +0 -889
- data/ext/geoip2/libmaxminddb/doc/mmdblookup.md +0 -103
- data/ext/geoip2/libmaxminddb/include/maxminddb.h +0 -241
- data/ext/geoip2/libmaxminddb/include/maxminddb_config.h.in +0 -14
- data/ext/geoip2/libmaxminddb/projects/VS12/README.md +0 -59
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb-release.props +0 -32
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb-x64.props +0 -14
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.props +0 -32
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.sln +0 -150
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj +0 -143
- data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj.filters +0 -32
- data/ext/geoip2/libmaxminddb/projects/VS12/maxminddb_config.h +0 -14
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/bad_pointers.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/basic_lookup.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/data_entry_list.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/data_types.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/dump.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/get_value.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/get_value_pointer_bug.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/ipv4_start_cache.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/ipv6_lookup_in_ipv4.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/libtap.vcxproj +0 -85
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/maxminddb_test_helper.vcxproj +0 -107
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/metadata.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/metadata_pointers.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/no_map_get_value.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/read_node.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/shared.vcxproj +0 -104
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/threads.vcxproj +0 -103
- data/ext/geoip2/libmaxminddb/projects/VS12-tests/version.vcxproj +0 -105
- data/ext/geoip2/libmaxminddb/projects/test.vcxproj.template +0 -105
- data/ext/geoip2/libmaxminddb/src/Makefile.am +0 -25
- data/ext/geoip2/libmaxminddb/src/data-pool.c +0 -180
- data/ext/geoip2/libmaxminddb/src/data-pool.h +0 -52
- data/ext/geoip2/libmaxminddb/src/libmaxminddb.pc.in +0 -11
- data/ext/geoip2/libmaxminddb/src/maxminddb-compat-util.h +0 -167
- data/ext/geoip2/libmaxminddb/src/maxminddb.c +0 -2181
- data/ext/geoip2/libmaxminddb/t/Makefile.am +0 -27
- data/ext/geoip2/libmaxminddb/t/bad_databases_t.c +0 -67
- data/ext/geoip2/libmaxminddb/t/bad_pointers_t.c +0 -53
- data/ext/geoip2/libmaxminddb/t/basic_lookup_t.c +0 -207
- data/ext/geoip2/libmaxminddb/t/compile_c++_t.pl +0 -107
- data/ext/geoip2/libmaxminddb/t/data-pool-t.c +0 -374
- data/ext/geoip2/libmaxminddb/t/data_entry_list_t.c +0 -353
- data/ext/geoip2/libmaxminddb/t/data_types_t.c +0 -439
- data/ext/geoip2/libmaxminddb/t/dump_t.c +0 -103
- data/ext/geoip2/libmaxminddb/t/external_symbols_t.pl +0 -106
- data/ext/geoip2/libmaxminddb/t/get_value_pointer_bug_t.c +0 -66
- data/ext/geoip2/libmaxminddb/t/get_value_t.c +0 -249
- data/ext/geoip2/libmaxminddb/t/ipv4_start_cache_t.c +0 -36
- data/ext/geoip2/libmaxminddb/t/ipv6_lookup_in_ipv4_t.c +0 -48
- data/ext/geoip2/libmaxminddb/t/libtap/.gitignore +0 -13
- data/ext/geoip2/libmaxminddb/t/libtap/.travis.yml +0 -13
- data/ext/geoip2/libmaxminddb/t/libtap/COPYING +0 -165
- data/ext/geoip2/libmaxminddb/t/libtap/INSTALL +0 -41
- data/ext/geoip2/libmaxminddb/t/libtap/Makefile +0 -72
- data/ext/geoip2/libmaxminddb/t/libtap/Makefile.win +0 -37
- data/ext/geoip2/libmaxminddb/t/libtap/README.md +0 -268
- data/ext/geoip2/libmaxminddb/t/libtap/t/cmp_mem.c +0 -20
- data/ext/geoip2/libmaxminddb/t/libtap/t/cmp_mem.expected +0 -28
- data/ext/geoip2/libmaxminddb/t/libtap/t/cmpok.c +0 -16
- data/ext/geoip2/libmaxminddb/t/libtap/t/cmpok.expected +0 -37
- data/ext/geoip2/libmaxminddb/t/libtap/t/diag.c +0 -10
- data/ext/geoip2/libmaxminddb/t/libtap/t/diag.expected +0 -2
- data/ext/geoip2/libmaxminddb/t/libtap/t/diesok.c +0 -14
- data/ext/geoip2/libmaxminddb/t/libtap/t/diesok.expected +0 -6
- data/ext/geoip2/libmaxminddb/t/libtap/t/is.c +0 -24
- data/ext/geoip2/libmaxminddb/t/libtap/t/is.expected +0 -58
- data/ext/geoip2/libmaxminddb/t/libtap/t/like.c +0 -10
- data/ext/geoip2/libmaxminddb/t/libtap/t/like.expected +0 -4
- data/ext/geoip2/libmaxminddb/t/libtap/t/simple.c +0 -31
- data/ext/geoip2/libmaxminddb/t/libtap/t/simple.expected +0 -32
- data/ext/geoip2/libmaxminddb/t/libtap/t/skip.c +0 -23
- data/ext/geoip2/libmaxminddb/t/libtap/t/skip.expected +0 -9
- data/ext/geoip2/libmaxminddb/t/libtap/t/synopsis.c +0 -13
- data/ext/geoip2/libmaxminddb/t/libtap/t/synopsis.expected +0 -9
- data/ext/geoip2/libmaxminddb/t/libtap/t/test.c +0 -28
- data/ext/geoip2/libmaxminddb/t/libtap/t/todo.c +0 -17
- data/ext/geoip2/libmaxminddb/t/libtap/t/todo.expected +0 -11
- data/ext/geoip2/libmaxminddb/t/libtap/tap.c +0 -354
- data/ext/geoip2/libmaxminddb/t/libtap/tap.h +0 -115
- data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitattributes +0 -1
- data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitconfig +0 -2
- data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitignore +0 -2
- data/ext/geoip2/libmaxminddb/t/maxmind-db/.perltidyallrc +0 -11
- data/ext/geoip2/libmaxminddb/t/maxmind-db/.tidyallrc +0 -7
- data/ext/geoip2/libmaxminddb/t/maxmind-db/LICENSE +0 -4
- data/ext/geoip2/libmaxminddb/t/maxmind-db/MaxMind-DB-spec.md +0 -558
- data/ext/geoip2/libmaxminddb/t/maxmind-db/README.md +0 -4
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/README.md +0 -7
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +0 -1
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-string-length.mmdb +0 -1
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +0 -1
- data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/perltidyrc +0 -12
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Anonymous-IP-Test.json +0 -32
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-City-Test.json +0 -12616
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Connection-Type-Test.json +0 -102
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Country-Test.json +0 -10975
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-DensityIncome-Test.json +0 -14
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Domain-Test.json +0 -452
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Enterprise-Test.json +0 -666
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-ISP-Test.json +0 -12585
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Precision-Enterprise-Test.json +0 -1035
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoLite2-ASN-Test.json +0 -37
- data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/README +0 -13
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Country-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Domain-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-ISP-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoLite2-ASN-Test.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-nested.mmdb +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/README.md +0 -26
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/maps-with-pointers.raw +0 -0
- data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/write-test-data.pl +0 -614
- data/ext/geoip2/libmaxminddb/t/maxmind-db/tidyall.ini +0 -5
- data/ext/geoip2/libmaxminddb/t/maxminddb_test_helper.c +0 -255
- data/ext/geoip2/libmaxminddb/t/maxminddb_test_helper.h +0 -69
- data/ext/geoip2/libmaxminddb/t/metadata_pointers_t.c +0 -32
- data/ext/geoip2/libmaxminddb/t/metadata_t.c +0 -226
- data/ext/geoip2/libmaxminddb/t/mmdblookup_t.pl +0 -158
- data/ext/geoip2/libmaxminddb/t/no_map_get_value_t.c +0 -32
- data/ext/geoip2/libmaxminddb/t/read_node_t.c +0 -157
- data/ext/geoip2/libmaxminddb/t/threads_t.c +0 -196
- data/ext/geoip2/libmaxminddb/t/version_t.c +0 -10
|
@@ -1,715 +0,0 @@
|
|
|
1
|
-
#ifdef HAVE_CONFIG_H
|
|
2
|
-
#include <config.h>
|
|
3
|
-
#endif
|
|
4
|
-
#include "maxminddb.h"
|
|
5
|
-
#include <errno.h>
|
|
6
|
-
#include <getopt.h>
|
|
7
|
-
#ifndef _WIN32
|
|
8
|
-
#include <pthread.h>
|
|
9
|
-
#endif
|
|
10
|
-
#include <stdbool.h>
|
|
11
|
-
#include <stdio.h>
|
|
12
|
-
#include <stdlib.h>
|
|
13
|
-
#include <string.h>
|
|
14
|
-
#include <time.h>
|
|
15
|
-
|
|
16
|
-
#ifdef _WIN32
|
|
17
|
-
#include <malloc.h>
|
|
18
|
-
#define snprintf _snprintf
|
|
19
|
-
#undef UNICODE /* Use the non-UTF16 version of the gai_strerror */
|
|
20
|
-
#else
|
|
21
|
-
#include <libgen.h>
|
|
22
|
-
#include <unistd.h>
|
|
23
|
-
#endif
|
|
24
|
-
|
|
25
|
-
#define LOCAL static
|
|
26
|
-
#define NO_PROTO
|
|
27
|
-
|
|
28
|
-
/* *INDENT-OFF* */
|
|
29
|
-
/* --prototypes automatically generated by dev-bin/regen-prototypes.pl - don't remove this comment */
|
|
30
|
-
LOCAL void usage(char *program, int exit_code, const char *error);
|
|
31
|
-
LOCAL const char **get_options(
|
|
32
|
-
int argc,
|
|
33
|
-
char **argv,
|
|
34
|
-
char **mmdb_file,
|
|
35
|
-
char **ip_address,
|
|
36
|
-
int *verbose,
|
|
37
|
-
int *iterations,
|
|
38
|
-
int *lookup_path_length,
|
|
39
|
-
int *const thread_count,
|
|
40
|
-
char **const ip_file);
|
|
41
|
-
LOCAL MMDB_s open_or_die(const char *fname);
|
|
42
|
-
LOCAL void dump_meta(MMDB_s *mmdb);
|
|
43
|
-
LOCAL bool lookup_from_file(MMDB_s *const mmdb,
|
|
44
|
-
char const *const ip_file,
|
|
45
|
-
bool const dump);
|
|
46
|
-
LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
|
|
47
|
-
const char **lookup_path,
|
|
48
|
-
int lookup_path_length);
|
|
49
|
-
LOCAL int benchmark(MMDB_s *mmdb, int iterations);
|
|
50
|
-
LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr);
|
|
51
|
-
LOCAL void random_ipv4(char *ip);
|
|
52
|
-
/* --prototypes end - don't remove this comment-- */
|
|
53
|
-
/* *INDENT-ON* */
|
|
54
|
-
|
|
55
|
-
#ifndef _WIN32
|
|
56
|
-
// These aren't with the automatically generated prototypes as we'd lose the
|
|
57
|
-
// enclosing macros.
|
|
58
|
-
static bool start_threaded_benchmark(
|
|
59
|
-
MMDB_s *const mmdb,
|
|
60
|
-
int const thread_count,
|
|
61
|
-
int const iterations);
|
|
62
|
-
static long double get_time(void);
|
|
63
|
-
static void *thread(void *arg);
|
|
64
|
-
#endif
|
|
65
|
-
|
|
66
|
-
int main(int argc, char **argv)
|
|
67
|
-
{
|
|
68
|
-
char *mmdb_file = NULL;
|
|
69
|
-
char *ip_address = NULL;
|
|
70
|
-
int verbose = 0;
|
|
71
|
-
int iterations = 0;
|
|
72
|
-
int lookup_path_length = 0;
|
|
73
|
-
int thread_count = 0;
|
|
74
|
-
char *ip_file = NULL;
|
|
75
|
-
|
|
76
|
-
const char **lookup_path =
|
|
77
|
-
get_options(argc, argv, &mmdb_file, &ip_address, &verbose, &iterations,
|
|
78
|
-
&lookup_path_length, &thread_count, &ip_file);
|
|
79
|
-
|
|
80
|
-
MMDB_s mmdb = open_or_die(mmdb_file);
|
|
81
|
-
|
|
82
|
-
if (verbose) {
|
|
83
|
-
dump_meta(&mmdb);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// The benchmarking and lookup from file modes are hidden features mainly
|
|
87
|
-
// intended for development right now. This means there are several flags
|
|
88
|
-
// that exist but are intentionally not mentioned in the usage or man page.
|
|
89
|
-
|
|
90
|
-
// The lookup from file mode may be useful to expose publicly in the usage,
|
|
91
|
-
// but we should have it respect the lookup_path functionality if we do so.
|
|
92
|
-
if (ip_file) {
|
|
93
|
-
free(lookup_path);
|
|
94
|
-
if (!lookup_from_file(&mmdb, ip_file, verbose == 1)) {
|
|
95
|
-
MMDB_close(&mmdb);
|
|
96
|
-
return 1;
|
|
97
|
-
}
|
|
98
|
-
MMDB_close(&mmdb);
|
|
99
|
-
return 0;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (0 == iterations) {
|
|
103
|
-
exit(lookup_and_print(&mmdb, ip_address, lookup_path,
|
|
104
|
-
lookup_path_length));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
free(lookup_path);
|
|
108
|
-
|
|
109
|
-
srand( time(NULL) );
|
|
110
|
-
|
|
111
|
-
#ifndef _WIN32
|
|
112
|
-
if (thread_count > 0) {
|
|
113
|
-
if (!start_threaded_benchmark(&mmdb, thread_count, iterations)) {
|
|
114
|
-
MMDB_close(&mmdb);
|
|
115
|
-
exit(1);
|
|
116
|
-
}
|
|
117
|
-
MMDB_close(&mmdb);
|
|
118
|
-
exit(0);
|
|
119
|
-
}
|
|
120
|
-
#endif
|
|
121
|
-
|
|
122
|
-
exit(benchmark(&mmdb, iterations));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
LOCAL void usage(char *program, int exit_code, const char *error)
|
|
126
|
-
{
|
|
127
|
-
if (NULL != error) {
|
|
128
|
-
fprintf(stderr, "\n *ERROR: %s\n", error);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
char *usage = "\n"
|
|
132
|
-
" %s --file /path/to/file.mmdb --ip 1.2.3.4 [path to lookup]\n"
|
|
133
|
-
"\n"
|
|
134
|
-
" This application accepts the following options:\n"
|
|
135
|
-
"\n"
|
|
136
|
-
" --file (-f) The path to the MMDB file. Required.\n"
|
|
137
|
-
"\n"
|
|
138
|
-
" --ip (-i) The IP address to look up. Required.\n"
|
|
139
|
-
"\n"
|
|
140
|
-
" --verbose (-v) Turns on verbose output. Specifically, this causes this\n"
|
|
141
|
-
" application to output the database metadata.\n"
|
|
142
|
-
"\n"
|
|
143
|
-
" --version Print the program's version number and exit.\n"
|
|
144
|
-
"\n"
|
|
145
|
-
" --help (-h -?) Show usage information.\n"
|
|
146
|
-
"\n"
|
|
147
|
-
" If an IP's data entry resolves to a map or array, you can provide\n"
|
|
148
|
-
" a lookup path to only show part of that data.\n"
|
|
149
|
-
"\n"
|
|
150
|
-
" For example, given a JSON structure like this:\n"
|
|
151
|
-
"\n"
|
|
152
|
-
" {\n"
|
|
153
|
-
" \"names\": {\n"
|
|
154
|
-
" \"en\": \"Germany\",\n"
|
|
155
|
-
" \"de\": \"Deutschland\"\n"
|
|
156
|
-
" },\n"
|
|
157
|
-
" \"cities\": [ \"Berlin\", \"Frankfurt\" ]\n"
|
|
158
|
-
" }\n"
|
|
159
|
-
"\n"
|
|
160
|
-
" You could look up just the English name by calling mmdblookup with a lookup path of:\n"
|
|
161
|
-
"\n"
|
|
162
|
-
" mmdblookup --file ... --ip ... names en\n"
|
|
163
|
-
"\n"
|
|
164
|
-
" Or you could look up the second city in the list with:\n"
|
|
165
|
-
"\n"
|
|
166
|
-
" mmdblookup --file ... --ip ... cities 1\n"
|
|
167
|
-
"\n"
|
|
168
|
-
" Array numbering begins with zero (0).\n"
|
|
169
|
-
"\n"
|
|
170
|
-
" If you do not provide a path to lookup, all of the information for a given IP\n"
|
|
171
|
-
" will be shown.\n"
|
|
172
|
-
"\n";
|
|
173
|
-
|
|
174
|
-
fprintf(stdout, usage, program);
|
|
175
|
-
exit(exit_code);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
LOCAL const char **get_options(
|
|
179
|
-
int argc,
|
|
180
|
-
char **argv,
|
|
181
|
-
char **mmdb_file,
|
|
182
|
-
char **ip_address,
|
|
183
|
-
int *verbose,
|
|
184
|
-
int *iterations,
|
|
185
|
-
int *lookup_path_length,
|
|
186
|
-
int *const thread_count,
|
|
187
|
-
char **const ip_file)
|
|
188
|
-
{
|
|
189
|
-
static int help = 0;
|
|
190
|
-
static int version = 0;
|
|
191
|
-
|
|
192
|
-
while (1) {
|
|
193
|
-
static struct option options[] = {
|
|
194
|
-
{ "file", required_argument, 0, 'f' },
|
|
195
|
-
{ "ip", required_argument, 0, 'i' },
|
|
196
|
-
{ "verbose", no_argument, 0, 'v' },
|
|
197
|
-
{ "version", no_argument, 0, 'n' },
|
|
198
|
-
{ "benchmark", required_argument, 0, 'b' },
|
|
199
|
-
#ifndef _WIN32
|
|
200
|
-
{ "threads", required_argument, 0, 't' },
|
|
201
|
-
#endif
|
|
202
|
-
{ "ip-file", required_argument, 0, 'I' },
|
|
203
|
-
{ "help", no_argument, 0, 'h' },
|
|
204
|
-
{ "?", no_argument, 0, 1 },
|
|
205
|
-
{ 0, 0, 0, 0 }
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
int opt_index;
|
|
209
|
-
#ifdef _WIN32
|
|
210
|
-
char const * const optstring = "f:i:b:I:vnh?";
|
|
211
|
-
#else
|
|
212
|
-
char const * const optstring = "f:i:b:t:I:vnh?";
|
|
213
|
-
#endif
|
|
214
|
-
int opt_char = getopt_long(argc, argv, optstring, options,
|
|
215
|
-
&opt_index);
|
|
216
|
-
|
|
217
|
-
if (-1 == opt_char) {
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if ('f' == opt_char) {
|
|
222
|
-
*mmdb_file = optarg;
|
|
223
|
-
} else if ('i' == opt_char) {
|
|
224
|
-
*ip_address = optarg;
|
|
225
|
-
} else if ('v' == opt_char) {
|
|
226
|
-
*verbose = 1;
|
|
227
|
-
} else if ('n' == opt_char) {
|
|
228
|
-
version = 1;
|
|
229
|
-
} else if ('b' == opt_char) {
|
|
230
|
-
*iterations = strtol(optarg, NULL, 10);
|
|
231
|
-
} else if ('h' == opt_char || '?' == opt_char) {
|
|
232
|
-
help = 1;
|
|
233
|
-
} else if (opt_char == 't') {
|
|
234
|
-
*thread_count = strtol(optarg, NULL, 10);
|
|
235
|
-
} else if (opt_char == 'I') {
|
|
236
|
-
*ip_file = optarg;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
#ifdef _WIN32
|
|
241
|
-
char *program = alloca(strlen(argv[0]));
|
|
242
|
-
_splitpath(argv[0], NULL, NULL, program, NULL);
|
|
243
|
-
_splitpath(argv[0], NULL, NULL, NULL, program + strlen(program));
|
|
244
|
-
#else
|
|
245
|
-
char *program = basename(argv[0]);
|
|
246
|
-
#endif
|
|
247
|
-
|
|
248
|
-
if (help) {
|
|
249
|
-
usage(program, 0, NULL);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (version) {
|
|
253
|
-
fprintf(stdout, "\n %s version %s\n\n", program, VERSION);
|
|
254
|
-
exit(0);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (NULL == *mmdb_file) {
|
|
258
|
-
usage(program, 1, "You must provide a filename with --file");
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (*ip_address == NULL && *iterations == 0 && !*ip_file) {
|
|
262
|
-
usage(program, 1, "You must provide an IP address with --ip");
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const char **lookup_path =
|
|
266
|
-
malloc(sizeof(const char *) * ((argc - optind) + 1));
|
|
267
|
-
int i;
|
|
268
|
-
for (i = 0; i < argc - optind; i++) {
|
|
269
|
-
lookup_path[i] = argv[i + optind];
|
|
270
|
-
(*lookup_path_length)++;
|
|
271
|
-
}
|
|
272
|
-
lookup_path[i] = NULL;
|
|
273
|
-
|
|
274
|
-
return lookup_path;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
LOCAL MMDB_s open_or_die(const char *fname)
|
|
278
|
-
{
|
|
279
|
-
MMDB_s mmdb;
|
|
280
|
-
int status = MMDB_open(fname, MMDB_MODE_MMAP, &mmdb);
|
|
281
|
-
|
|
282
|
-
if (MMDB_SUCCESS != status) {
|
|
283
|
-
fprintf(stderr, "\n Can't open %s - %s\n", fname,
|
|
284
|
-
MMDB_strerror(status));
|
|
285
|
-
|
|
286
|
-
if (MMDB_IO_ERROR == status) {
|
|
287
|
-
fprintf(stderr, " IO error: %s\n", strerror(errno));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
fprintf(stderr, "\n");
|
|
291
|
-
|
|
292
|
-
exit(2);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return mmdb;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
LOCAL void dump_meta(MMDB_s *mmdb)
|
|
299
|
-
{
|
|
300
|
-
const char *meta_dump = "\n"
|
|
301
|
-
" Database metadata\n"
|
|
302
|
-
" Node count: %i\n"
|
|
303
|
-
" Record size: %i bits\n"
|
|
304
|
-
" IP version: IPv%i\n"
|
|
305
|
-
" Binary format: %i.%i\n"
|
|
306
|
-
" Build epoch: %llu (%s)\n"
|
|
307
|
-
" Type: %s\n"
|
|
308
|
-
" Languages: ";
|
|
309
|
-
|
|
310
|
-
char date[40];
|
|
311
|
-
const time_t epoch = (const time_t)mmdb->metadata.build_epoch;
|
|
312
|
-
strftime(date, 40, "%F %T UTC", gmtime(&epoch));
|
|
313
|
-
|
|
314
|
-
fprintf(stdout, meta_dump,
|
|
315
|
-
mmdb->metadata.node_count,
|
|
316
|
-
mmdb->metadata.record_size,
|
|
317
|
-
mmdb->metadata.ip_version,
|
|
318
|
-
mmdb->metadata.binary_format_major_version,
|
|
319
|
-
mmdb->metadata.binary_format_minor_version,
|
|
320
|
-
mmdb->metadata.build_epoch,
|
|
321
|
-
date,
|
|
322
|
-
mmdb->metadata.database_type);
|
|
323
|
-
|
|
324
|
-
for (size_t i = 0; i < mmdb->metadata.languages.count; i++) {
|
|
325
|
-
fprintf(stdout, "%s", mmdb->metadata.languages.names[i]);
|
|
326
|
-
if (i < mmdb->metadata.languages.count - 1) {
|
|
327
|
-
fprintf(stdout, " ");
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
fprintf(stdout, "\n");
|
|
331
|
-
|
|
332
|
-
fprintf(stdout, " Description:\n");
|
|
333
|
-
for (size_t i = 0; i < mmdb->metadata.description.count; i++) {
|
|
334
|
-
fprintf(stdout, " %s: %s\n",
|
|
335
|
-
mmdb->metadata.description.descriptions[i]->language,
|
|
336
|
-
mmdb->metadata.description.descriptions[i]->description);
|
|
337
|
-
}
|
|
338
|
-
fprintf(stdout, "\n");
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// The input file should have one IP per line.
|
|
342
|
-
//
|
|
343
|
-
// We look up each IP.
|
|
344
|
-
//
|
|
345
|
-
// If dump is true, we dump the data for each IP to stderr. This is useful for
|
|
346
|
-
// comparison in that you can dump out the data for the IPs before and after
|
|
347
|
-
// making changes. It goes to stderr rather than stdout so that the report does
|
|
348
|
-
// not get included in what you will compare (since it will almost always be
|
|
349
|
-
// different).
|
|
350
|
-
//
|
|
351
|
-
// In addition to being useful for comparisons, this function provides a way to
|
|
352
|
-
// have a more deterministic set of lookups for benchmarking.
|
|
353
|
-
LOCAL bool lookup_from_file(MMDB_s *const mmdb,
|
|
354
|
-
char const *const ip_file,
|
|
355
|
-
bool const dump)
|
|
356
|
-
{
|
|
357
|
-
FILE *const fh = fopen(ip_file, "r");
|
|
358
|
-
if (!fh) {
|
|
359
|
-
fprintf(stderr, "fopen(): %s: %s\n", ip_file, strerror(errno));
|
|
360
|
-
return false;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
clock_t const clock_start = clock();
|
|
364
|
-
char buf[1024] = { 0 };
|
|
365
|
-
// I'd normally use uint64_t, but support for it is optional in C99.
|
|
366
|
-
unsigned long long i = 0;
|
|
367
|
-
while (1) {
|
|
368
|
-
if (fgets(buf, sizeof(buf), fh) == NULL) {
|
|
369
|
-
if (!feof(fh)) {
|
|
370
|
-
fprintf(stderr, "fgets(): %s\n", strerror(errno));
|
|
371
|
-
fclose(fh);
|
|
372
|
-
return false;
|
|
373
|
-
}
|
|
374
|
-
if (fclose(fh) != 0) {
|
|
375
|
-
fprintf(stderr, "fclose(): %s\n", strerror(errno));
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
char *ptr = buf;
|
|
382
|
-
while (*ptr != '\0') {
|
|
383
|
-
if (*ptr == '\n') {
|
|
384
|
-
*ptr = '\0';
|
|
385
|
-
break;
|
|
386
|
-
}
|
|
387
|
-
ptr++;
|
|
388
|
-
}
|
|
389
|
-
if (strlen(buf) == 0) {
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
i++;
|
|
394
|
-
|
|
395
|
-
MMDB_lookup_result_s result = lookup_or_die(mmdb, buf);
|
|
396
|
-
if (!result.found_entry) {
|
|
397
|
-
continue;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
MMDB_entry_data_list_s *entry_data_list = NULL;
|
|
401
|
-
int const status = MMDB_get_entry_data_list(&result.entry,
|
|
402
|
-
&entry_data_list);
|
|
403
|
-
if (status != MMDB_SUCCESS) {
|
|
404
|
-
fprintf(stderr, "MMDB_get_entry_data_list(): %s\n",
|
|
405
|
-
MMDB_strerror(status));
|
|
406
|
-
fclose(fh);
|
|
407
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
408
|
-
return false;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (!entry_data_list) {
|
|
412
|
-
fprintf(stderr, "entry_data_list is NULL\n");
|
|
413
|
-
fclose(fh);
|
|
414
|
-
return false;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (dump) {
|
|
418
|
-
fprintf(stdout, "%s:\n", buf);
|
|
419
|
-
int const status = MMDB_dump_entry_data_list(stderr,
|
|
420
|
-
entry_data_list, 0);
|
|
421
|
-
if (status != MMDB_SUCCESS) {
|
|
422
|
-
fprintf(stderr, "MMDB_dump_entry_data_list(): %s\n",
|
|
423
|
-
MMDB_strerror(status));
|
|
424
|
-
fclose(fh);
|
|
425
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
426
|
-
return false;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
clock_t const clock_diff = clock() - clock_start;
|
|
434
|
-
double const seconds = (double)clock_diff / CLOCKS_PER_SEC;
|
|
435
|
-
|
|
436
|
-
fprintf(
|
|
437
|
-
stdout,
|
|
438
|
-
"Looked up %llu addresses in %.2f seconds. %.2f lookups per second.\n",
|
|
439
|
-
i, seconds, i / seconds);
|
|
440
|
-
|
|
441
|
-
return true;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
|
|
445
|
-
const char **lookup_path,
|
|
446
|
-
int lookup_path_length)
|
|
447
|
-
{
|
|
448
|
-
|
|
449
|
-
MMDB_lookup_result_s result = lookup_or_die(mmdb, ip_address);
|
|
450
|
-
MMDB_entry_data_list_s *entry_data_list = NULL;
|
|
451
|
-
|
|
452
|
-
int exit_code = 0;
|
|
453
|
-
|
|
454
|
-
if (result.found_entry) {
|
|
455
|
-
int status;
|
|
456
|
-
if (lookup_path_length) {
|
|
457
|
-
MMDB_entry_data_s entry_data;
|
|
458
|
-
status = MMDB_aget_value(&result.entry, &entry_data,
|
|
459
|
-
lookup_path);
|
|
460
|
-
if (MMDB_SUCCESS == status) {
|
|
461
|
-
if (entry_data.offset) {
|
|
462
|
-
MMDB_entry_s entry =
|
|
463
|
-
{ .mmdb = mmdb, .offset = entry_data.offset };
|
|
464
|
-
status = MMDB_get_entry_data_list(&entry,
|
|
465
|
-
&entry_data_list);
|
|
466
|
-
} else {
|
|
467
|
-
fprintf(
|
|
468
|
-
stdout,
|
|
469
|
-
"\n No data was found at the lookup path you provided\n\n");
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
} else {
|
|
473
|
-
status = MMDB_get_entry_data_list(&result.entry,
|
|
474
|
-
&entry_data_list);
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
if (MMDB_SUCCESS != status) {
|
|
478
|
-
fprintf(stderr, "Got an error looking up the entry data - %s\n",
|
|
479
|
-
MMDB_strerror(status));
|
|
480
|
-
exit_code = 5;
|
|
481
|
-
goto end;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (NULL != entry_data_list) {
|
|
485
|
-
fprintf(stdout, "\n");
|
|
486
|
-
MMDB_dump_entry_data_list(stdout, entry_data_list, 2);
|
|
487
|
-
fprintf(stdout, "\n");
|
|
488
|
-
}
|
|
489
|
-
} else {
|
|
490
|
-
fprintf(stderr,
|
|
491
|
-
"\n Could not find an entry for this IP address (%s)\n\n",
|
|
492
|
-
ip_address);
|
|
493
|
-
exit_code = 6;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
end:
|
|
497
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
498
|
-
MMDB_close(mmdb);
|
|
499
|
-
free(lookup_path);
|
|
500
|
-
|
|
501
|
-
return exit_code;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
LOCAL int benchmark(MMDB_s *mmdb, int iterations)
|
|
505
|
-
{
|
|
506
|
-
char ip_address[16];
|
|
507
|
-
int exit_code = 0;
|
|
508
|
-
|
|
509
|
-
clock_t time = clock();
|
|
510
|
-
|
|
511
|
-
for (int i = 0; i < iterations; i++) {
|
|
512
|
-
random_ipv4(ip_address);
|
|
513
|
-
|
|
514
|
-
MMDB_lookup_result_s result = lookup_or_die(mmdb, ip_address);
|
|
515
|
-
MMDB_entry_data_list_s *entry_data_list = NULL;
|
|
516
|
-
|
|
517
|
-
if (result.found_entry) {
|
|
518
|
-
|
|
519
|
-
int status = MMDB_get_entry_data_list(&result.entry,
|
|
520
|
-
&entry_data_list);
|
|
521
|
-
|
|
522
|
-
if (MMDB_SUCCESS != status) {
|
|
523
|
-
fprintf(stderr, "Got an error looking up the entry data - %s\n",
|
|
524
|
-
MMDB_strerror(status));
|
|
525
|
-
exit_code = 5;
|
|
526
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
527
|
-
goto end;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
time = clock() - time;
|
|
535
|
-
double seconds = ((double)time / CLOCKS_PER_SEC);
|
|
536
|
-
fprintf(
|
|
537
|
-
stdout,
|
|
538
|
-
"\n Looked up %i addresses in %.2f seconds. %.2f lookups per second.\n\n",
|
|
539
|
-
iterations, seconds, iterations / seconds);
|
|
540
|
-
|
|
541
|
-
end:
|
|
542
|
-
MMDB_close(mmdb);
|
|
543
|
-
|
|
544
|
-
return exit_code;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr)
|
|
548
|
-
{
|
|
549
|
-
int gai_error, mmdb_error;
|
|
550
|
-
MMDB_lookup_result_s result =
|
|
551
|
-
MMDB_lookup_string(mmdb, ipstr, &gai_error, &mmdb_error);
|
|
552
|
-
|
|
553
|
-
if (0 != gai_error) {
|
|
554
|
-
fprintf(stderr,
|
|
555
|
-
"\n Error from call to getaddrinfo for %s - %s\n\n",
|
|
556
|
-
ipstr, gai_strerror(gai_error));
|
|
557
|
-
exit(3);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
if (MMDB_SUCCESS != mmdb_error) {
|
|
561
|
-
fprintf(stderr, "\n Got an error from the maxminddb library: %s\n\n",
|
|
562
|
-
MMDB_strerror(mmdb_error));
|
|
563
|
-
exit(4);
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
return result;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
LOCAL void random_ipv4(char *ip)
|
|
570
|
-
{
|
|
571
|
-
// rand() is perfectly fine for this use case
|
|
572
|
-
// coverity[dont_call]
|
|
573
|
-
int ip_int = rand();
|
|
574
|
-
uint8_t *bytes = (uint8_t *)&ip_int;
|
|
575
|
-
|
|
576
|
-
snprintf(ip, 16, "%u.%u.%u.%u",
|
|
577
|
-
*bytes, *(bytes + 1), *(bytes + 2), *(bytes + 3));
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
#ifndef _WIN32
|
|
581
|
-
struct thread_info {
|
|
582
|
-
pthread_t id;
|
|
583
|
-
int num;
|
|
584
|
-
MMDB_s *mmdb;
|
|
585
|
-
int iterations;
|
|
586
|
-
};
|
|
587
|
-
|
|
588
|
-
NO_PROTO static bool start_threaded_benchmark(
|
|
589
|
-
MMDB_s *const mmdb,
|
|
590
|
-
int const thread_count,
|
|
591
|
-
int const iterations)
|
|
592
|
-
{
|
|
593
|
-
struct thread_info *const tinfo = calloc(thread_count,
|
|
594
|
-
sizeof(struct thread_info));
|
|
595
|
-
if (!tinfo) {
|
|
596
|
-
fprintf(stderr, "calloc(): %s\n", strerror(errno));
|
|
597
|
-
return false;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
// Using clock() isn't appropriate for multiple threads. It's CPU time, not
|
|
601
|
-
// wall time.
|
|
602
|
-
long double const start_time = get_time();
|
|
603
|
-
if (start_time == -1) {
|
|
604
|
-
return false;
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
for (int i = 0; i < thread_count; i++) {
|
|
608
|
-
tinfo[i].num = i;
|
|
609
|
-
tinfo[i].mmdb = mmdb;
|
|
610
|
-
tinfo[i].iterations = iterations;
|
|
611
|
-
|
|
612
|
-
if (pthread_create(&tinfo[i].id, NULL, &thread, &tinfo[i]) != 0) {
|
|
613
|
-
fprintf(stderr, "pthread_create() failed\n");
|
|
614
|
-
free(tinfo);
|
|
615
|
-
return false;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
for (int i = 0; i < thread_count; i++) {
|
|
620
|
-
if (pthread_join(tinfo[i].id, NULL) != 0) {
|
|
621
|
-
fprintf(stderr, "pthread_join() failed\n");
|
|
622
|
-
free(tinfo);
|
|
623
|
-
return false;
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
free(tinfo);
|
|
628
|
-
|
|
629
|
-
long double const end_time = get_time();
|
|
630
|
-
if (end_time == -1) {
|
|
631
|
-
return false;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
long double const elapsed = end_time - start_time;
|
|
635
|
-
unsigned long long const total_ips = iterations * thread_count;
|
|
636
|
-
long double rate = total_ips;
|
|
637
|
-
if (elapsed != 0) {
|
|
638
|
-
rate = total_ips / elapsed;
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
fprintf(
|
|
642
|
-
stdout,
|
|
643
|
-
"Looked up %llu addresses using %d threads in %.2Lf seconds. %.2Lf lookups per second.\n",
|
|
644
|
-
total_ips, thread_count, elapsed, rate);
|
|
645
|
-
|
|
646
|
-
return true;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
NO_PROTO static long double get_time(void)
|
|
650
|
-
{
|
|
651
|
-
// clock_gettime() is not present on OSX until 10.12.
|
|
652
|
-
#ifdef HAVE_CLOCK_GETTIME
|
|
653
|
-
struct timespec tp = {
|
|
654
|
-
.tv_sec = 0,
|
|
655
|
-
.tv_nsec = 0,
|
|
656
|
-
};
|
|
657
|
-
clockid_t clk_id = CLOCK_REALTIME;
|
|
658
|
-
#ifdef _POSIX_MONOTONIC_CLOCK
|
|
659
|
-
clk_id = CLOCK_MONOTONIC;
|
|
660
|
-
#endif
|
|
661
|
-
if (clock_gettime(clk_id, &tp) != 0) {
|
|
662
|
-
fprintf(stderr, "clock_gettime(): %s\n", strerror(errno));
|
|
663
|
-
return -1;
|
|
664
|
-
}
|
|
665
|
-
return tp.tv_sec + ((float)tp.tv_nsec / 1e9);
|
|
666
|
-
#else
|
|
667
|
-
time_t t = time(NULL);
|
|
668
|
-
if (t == (time_t)-1) {
|
|
669
|
-
fprintf(stderr, "time(): %s\n", strerror(errno));
|
|
670
|
-
return -1;
|
|
671
|
-
}
|
|
672
|
-
return (long double)t;
|
|
673
|
-
#endif
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
NO_PROTO static void *thread(void *arg)
|
|
677
|
-
{
|
|
678
|
-
const struct thread_info *const tinfo = arg;
|
|
679
|
-
if (!tinfo) {
|
|
680
|
-
fprintf(stderr, "thread(): %s\n", strerror(EINVAL));
|
|
681
|
-
return NULL;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
char ip_address[16] = { 0 };
|
|
685
|
-
|
|
686
|
-
for (int i = 0; i < tinfo->iterations; i++) {
|
|
687
|
-
memset(ip_address, 0, 16);
|
|
688
|
-
random_ipv4(ip_address);
|
|
689
|
-
|
|
690
|
-
MMDB_lookup_result_s result = lookup_or_die(tinfo->mmdb, ip_address);
|
|
691
|
-
if (!result.found_entry) {
|
|
692
|
-
continue;
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
MMDB_entry_data_list_s *entry_data_list = NULL;
|
|
696
|
-
int const status = MMDB_get_entry_data_list(&result.entry,
|
|
697
|
-
&entry_data_list);
|
|
698
|
-
if (status != MMDB_SUCCESS) {
|
|
699
|
-
fprintf(stderr, "MMDB_get_entry_data_list(): %s\n",
|
|
700
|
-
MMDB_strerror(status));
|
|
701
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
702
|
-
return NULL;
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
if (!entry_data_list) {
|
|
706
|
-
fprintf(stderr, "entry_data_list is NULL\n");
|
|
707
|
-
return NULL;
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
MMDB_free_entry_data_list(entry_data_list);
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
return NULL;
|
|
714
|
-
}
|
|
715
|
-
#endif
|