geoip2_c 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ubuntu.yml +33 -0
  3. data/.github/workflows/windows.yml +52 -0
  4. data/README.md +22 -10
  5. data/docker-compose.yml +22 -1
  6. data/dockerfiles/{Dockerfile-ruby2.3 → Dockerfile-ruby2.5} +1 -1
  7. data/dockerfiles/{Dockerfile-ruby2.1 → Dockerfile-ruby2.6} +1 -1
  8. data/dockerfiles/{Dockerfile-ruby2.2 → Dockerfile-ruby2.7} +1 -1
  9. data/ext/geoip2/extconf.rb +11 -4
  10. data/ext/geoip2/geoip2.c +63 -18
  11. data/ext/geoip2/libmaxminddb/.gitignore +3 -0
  12. data/ext/geoip2/libmaxminddb/.travis.yml +24 -2
  13. data/ext/geoip2/libmaxminddb/Changes.md +39 -0
  14. data/ext/geoip2/libmaxminddb/README.dev.md +41 -30
  15. data/ext/geoip2/libmaxminddb/README.md +3 -3
  16. data/ext/geoip2/libmaxminddb/bin/Makefile.am +5 -0
  17. data/ext/geoip2/libmaxminddb/bin/mmdblookup.c +333 -15
  18. data/ext/geoip2/libmaxminddb/configure.ac +5 -5
  19. data/ext/geoip2/libmaxminddb/dev-bin/ppa-release.sh +8 -5
  20. data/ext/geoip2/libmaxminddb/dev-bin/release.sh +7 -0
  21. data/ext/geoip2/libmaxminddb/dev-bin/valgrind-all.pl +10 -3
  22. data/ext/geoip2/libmaxminddb/include/maxminddb.h +11 -2
  23. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj +3 -1
  24. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj.filters +7 -1
  25. data/ext/geoip2/libmaxminddb/src/Makefile.am +18 -2
  26. data/ext/geoip2/libmaxminddb/src/data-pool.c +180 -0
  27. data/ext/geoip2/libmaxminddb/src/data-pool.h +52 -0
  28. data/ext/geoip2/libmaxminddb/src/maxminddb.c +58 -48
  29. data/ext/geoip2/libmaxminddb/t/Makefile.am +6 -2
  30. data/ext/geoip2/libmaxminddb/t/bad_databases_t.c +1 -0
  31. data/ext/geoip2/libmaxminddb/t/basic_lookup_t.c +35 -0
  32. data/ext/geoip2/libmaxminddb/t/data-pool-t.c +374 -0
  33. data/ext/geoip2/libmaxminddb/t/external_symbols_t.pl +106 -0
  34. data/ext/geoip2/libmaxminddb/t/libtap/.gitignore +13 -0
  35. data/ext/geoip2/libmaxminddb/t/libtap/.travis.yml +13 -0
  36. data/ext/geoip2/libmaxminddb/t/libtap/COPYING +165 -0
  37. data/ext/geoip2/libmaxminddb/t/libtap/INSTALL +41 -0
  38. data/ext/geoip2/libmaxminddb/t/libtap/Makefile +72 -0
  39. data/ext/geoip2/libmaxminddb/t/libtap/Makefile.win +37 -0
  40. data/ext/geoip2/libmaxminddb/t/libtap/README.md +268 -0
  41. data/ext/geoip2/libmaxminddb/t/libtap/t/cmp_mem.c +20 -0
  42. data/ext/geoip2/libmaxminddb/t/libtap/t/cmp_mem.expected +28 -0
  43. data/ext/geoip2/libmaxminddb/t/libtap/t/cmpok.c +16 -0
  44. data/ext/geoip2/libmaxminddb/t/libtap/t/cmpok.expected +37 -0
  45. data/ext/geoip2/libmaxminddb/t/libtap/t/diag.c +10 -0
  46. data/ext/geoip2/libmaxminddb/t/libtap/t/diag.expected +2 -0
  47. data/ext/geoip2/libmaxminddb/t/libtap/t/diesok.c +14 -0
  48. data/ext/geoip2/libmaxminddb/t/libtap/t/diesok.expected +6 -0
  49. data/ext/geoip2/libmaxminddb/t/libtap/t/is.c +24 -0
  50. data/ext/geoip2/libmaxminddb/t/libtap/t/is.expected +58 -0
  51. data/ext/geoip2/libmaxminddb/t/libtap/t/like.c +10 -0
  52. data/ext/geoip2/libmaxminddb/t/libtap/t/like.expected +4 -0
  53. data/ext/geoip2/libmaxminddb/t/libtap/t/simple.c +31 -0
  54. data/ext/geoip2/libmaxminddb/t/libtap/t/simple.expected +32 -0
  55. data/ext/geoip2/libmaxminddb/t/libtap/t/skip.c +23 -0
  56. data/ext/geoip2/libmaxminddb/t/libtap/t/skip.expected +9 -0
  57. data/ext/geoip2/libmaxminddb/t/libtap/t/synopsis.c +13 -0
  58. data/ext/geoip2/libmaxminddb/t/libtap/t/synopsis.expected +9 -0
  59. data/ext/geoip2/libmaxminddb/t/libtap/t/test.c +28 -0
  60. data/ext/geoip2/libmaxminddb/t/libtap/t/todo.c +17 -0
  61. data/ext/geoip2/libmaxminddb/t/libtap/t/todo.expected +11 -0
  62. data/ext/geoip2/libmaxminddb/t/libtap/tap.c +354 -0
  63. data/ext/geoip2/libmaxminddb/t/libtap/tap.h +115 -0
  64. data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitattributes +1 -0
  65. data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitconfig +2 -0
  66. data/ext/geoip2/libmaxminddb/t/maxmind-db/.gitignore +2 -0
  67. data/ext/geoip2/libmaxminddb/t/maxmind-db/.perltidyallrc +11 -0
  68. data/ext/geoip2/libmaxminddb/t/maxmind-db/.tidyallrc +7 -0
  69. data/ext/geoip2/libmaxminddb/t/maxmind-db/LICENSE +4 -0
  70. data/ext/geoip2/libmaxminddb/t/maxmind-db/MaxMind-DB-spec.md +558 -0
  71. data/ext/geoip2/libmaxminddb/t/maxmind-db/README.md +4 -0
  72. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/README.md +7 -0
  73. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  74. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  75. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
  76. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  77. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  78. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
  79. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
  80. data/ext/geoip2/libmaxminddb/t/maxmind-db/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  81. data/ext/geoip2/libmaxminddb/t/maxmind-db/perltidyrc +12 -0
  82. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Anonymous-IP-Test.json +32 -0
  83. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-City-Test.json +12616 -0
  84. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Connection-Type-Test.json +102 -0
  85. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Country-Test.json +10975 -0
  86. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-DensityIncome-Test.json +14 -0
  87. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Domain-Test.json +452 -0
  88. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Enterprise-Test.json +666 -0
  89. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-ISP-Test.json +12585 -0
  90. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoIP2-Precision-Enterprise-Test.json +1035 -0
  91. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/GeoLite2-ASN-Test.json +37 -0
  92. data/ext/geoip2/libmaxminddb/t/maxmind-db/source-data/README +13 -0
  93. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  94. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  95. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  96. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-City-Test.mmdb +0 -0
  97. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  98. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Country-Test.mmdb +0 -0
  99. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  100. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  101. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  102. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  103. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  104. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  105. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  106. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  107. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  108. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  109. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  110. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  111. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  112. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  113. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  114. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  115. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  116. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  117. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  118. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  119. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  120. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  121. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/README.md +26 -0
  122. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/maps-with-pointers.raw +0 -0
  123. data/ext/geoip2/libmaxminddb/t/maxmind-db/test-data/write-test-data.pl +614 -0
  124. data/ext/geoip2/libmaxminddb/t/maxmind-db/tidyall.ini +5 -0
  125. data/geoip2_c.gemspec +2 -3
  126. data/lib/geoip2/database.rb +4 -0
  127. data/lib/geoip2/version.rb +1 -1
  128. metadata +114 -22
  129. data/.travis.yml +0 -31
  130. data/Appraisals +0 -7
  131. data/gemfiles/ruby_2.1.gemfile +0 -7
  132. data/gemfiles/ruby_2.2.gemfile +0 -7
@@ -4,6 +4,10 @@
4
4
  #include "maxminddb.h"
5
5
  #include <errno.h>
6
6
  #include <getopt.h>
7
+ #ifndef _WIN32
8
+ #include <pthread.h>
9
+ #endif
10
+ #include <stdbool.h>
7
11
  #include <stdio.h>
8
12
  #include <stdlib.h>
9
13
  #include <string.h>
@@ -18,16 +22,27 @@
18
22
  #include <unistd.h>
19
23
  #endif
20
24
 
21
- #define LOCAL
25
+ #define LOCAL static
26
+ #define NO_PROTO
22
27
 
23
28
  /* *INDENT-OFF* */
24
29
  /* --prototypes automatically generated by dev-bin/regen-prototypes.pl - don't remove this comment */
25
30
  LOCAL void usage(char *program, int exit_code, const char *error);
26
- LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
27
- char **ip_address, int *verbose, int *iterations,
28
- int *lookup_path_length);
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);
29
41
  LOCAL MMDB_s open_or_die(const char *fname);
30
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);
31
46
  LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
32
47
  const char **lookup_path,
33
48
  int lookup_path_length);
@@ -37,6 +52,17 @@ LOCAL void random_ipv4(char *ip);
37
52
  /* --prototypes end - don't remove this comment-- */
38
53
  /* *INDENT-ON* */
39
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
+
40
66
  int main(int argc, char **argv)
41
67
  {
42
68
  char *mmdb_file = NULL;
@@ -44,10 +70,12 @@ int main(int argc, char **argv)
44
70
  int verbose = 0;
45
71
  int iterations = 0;
46
72
  int lookup_path_length = 0;
73
+ int thread_count = 0;
74
+ char *ip_file = NULL;
47
75
 
48
76
  const char **lookup_path =
49
77
  get_options(argc, argv, &mmdb_file, &ip_address, &verbose, &iterations,
50
- &lookup_path_length);
78
+ &lookup_path_length, &thread_count, &ip_file);
51
79
 
52
80
  MMDB_s mmdb = open_or_die(mmdb_file);
53
81
 
@@ -55,12 +83,43 @@ int main(int argc, char **argv)
55
83
  dump_meta(&mmdb);
56
84
  }
57
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
+
58
102
  if (0 == iterations) {
59
103
  exit(lookup_and_print(&mmdb, ip_address, lookup_path,
60
104
  lookup_path_length));
61
- } else {
62
- exit(benchmark(&mmdb, iterations));
63
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));
64
123
  }
65
124
 
66
125
  LOCAL void usage(char *program, int exit_code, const char *error)
@@ -116,9 +175,16 @@ LOCAL void usage(char *program, int exit_code, const char *error)
116
175
  exit(exit_code);
117
176
  }
118
177
 
119
- LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
120
- char **ip_address, int *verbose, int *iterations,
121
- int *lookup_path_length)
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)
122
188
  {
123
189
  static int help = 0;
124
190
  static int version = 0;
@@ -130,13 +196,22 @@ LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
130
196
  { "verbose", no_argument, 0, 'v' },
131
197
  { "version", no_argument, 0, 'n' },
132
198
  { "benchmark", required_argument, 0, 'b' },
199
+ #ifndef _WIN32
200
+ { "threads", required_argument, 0, 't' },
201
+ #endif
202
+ { "ip-file", required_argument, 0, 'I' },
133
203
  { "help", no_argument, 0, 'h' },
134
204
  { "?", no_argument, 0, 1 },
135
205
  { 0, 0, 0, 0 }
136
206
  };
137
207
 
138
208
  int opt_index;
139
- int opt_char = getopt_long(argc, argv, "f:i:b:vnh?", options,
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,
140
215
  &opt_index);
141
216
 
142
217
  if (-1 == opt_char) {
@@ -155,6 +230,10 @@ LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
155
230
  *iterations = strtol(optarg, NULL, 10);
156
231
  } else if ('h' == opt_char || '?' == opt_char) {
157
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;
158
237
  }
159
238
  }
160
239
 
@@ -179,7 +258,7 @@ LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
179
258
  usage(program, 1, "You must provide a filename with --file");
180
259
  }
181
260
 
182
- if (NULL == *ip_address && *iterations == 0) {
261
+ if (*ip_address == NULL && *iterations == 0 && !*ip_file) {
183
262
  usage(program, 1, "You must provide an IP address with --ip");
184
263
  }
185
264
 
@@ -229,8 +308,8 @@ LOCAL void dump_meta(MMDB_s *mmdb)
229
308
  " Languages: ";
230
309
 
231
310
  char date[40];
232
- strftime(date, 40, "%F %T UTC",
233
- gmtime((const time_t *)&mmdb->metadata.build_epoch));
311
+ const time_t epoch = (const time_t)mmdb->metadata.build_epoch;
312
+ strftime(date, 40, "%F %T UTC", gmtime(&epoch));
234
313
 
235
314
  fprintf(stdout, meta_dump,
236
315
  mmdb->metadata.node_count,
@@ -259,6 +338,109 @@ LOCAL void dump_meta(MMDB_s *mmdb)
259
338
  fprintf(stdout, "\n");
260
339
  }
261
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
+
262
444
  LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address,
263
445
  const char **lookup_path,
264
446
  int lookup_path_length)
@@ -324,7 +506,6 @@ LOCAL int benchmark(MMDB_s *mmdb, int iterations)
324
506
  char ip_address[16];
325
507
  int exit_code = 0;
326
508
 
327
- srand( time(NULL) );
328
509
  clock_t time = clock();
329
510
 
330
511
  for (int i = 0; i < iterations; i++) {
@@ -395,3 +576,140 @@ LOCAL void random_ipv4(char *ip)
395
576
  snprintf(ip, 16, "%u.%u.%u.%u",
396
577
  *bytes, *(bytes + 1), *(bytes + 2), *(bytes + 3));
397
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
@@ -2,7 +2,7 @@
2
2
  # Process this file with autoconf to produce a configure script.
3
3
 
4
4
  AC_PREREQ([2.63])
5
- AC_INIT([libmaxminddb], [1.2.1], [support@maxmind.com])
5
+ AC_INIT([libmaxminddb], [1.3.2], [support@maxmind.com])
6
6
  AC_CONFIG_SRCDIR([include/maxminddb.h])
7
7
  AC_CONFIG_HEADERS([config.h include/maxminddb_config.h])
8
8
 
@@ -63,9 +63,11 @@ AC_CHECK_HEADERS([arpa/inet.h assert.h fcntl.h inttypes.h libgen.h math.h netdb.
63
63
  # so we only run them on non MinGW-Systems. For MinGW we also need to link
64
64
  # against ws2_32.
65
65
  AC_CANONICAL_HOST
66
+ is_windows=false
66
67
  case $host_os in
67
68
  mingw*)
68
69
  LDFLAGS="-lws2_32"
70
+ is_windows=true
69
71
  ;;
70
72
  *)
71
73
  AC_TYPE_OFF_T
@@ -77,6 +79,7 @@ case $host_os in
77
79
  ;;
78
80
  esac
79
81
 
82
+ AM_CONDITIONAL([WINDOWS], [test x$is_windows = xtrue])
80
83
 
81
84
  # This check is backwards in order to make life easier for people writing
82
85
  # extensions in other languages that link to this library. If they want to
@@ -94,15 +97,12 @@ AC_CHECK_TYPE(
94
97
 
95
98
  AC_CHECK_TYPES([boolean])
96
99
 
97
- AC_CHECK_FUNC(
98
- [open_memstream],
99
- [AC_DEFINE([HAVE_OPEN_MEMSTREAM], [1], [Has an open_memstream() function])])
100
+ AC_CHECK_FUNCS([clock_gettime open_memstream])
100
101
 
101
102
  AC_C_BIGENDIAN(
102
103
  [AC_DEFINE([MMDB_LITTLE_ENDIAN], [0], [System is big-endian])],
103
104
  [AC_DEFINE([MMDB_LITTLE_ENDIAN], [1], [System is little-endian])])
104
105
 
105
- AC_FUNC_MALLOC
106
106
  AC_FUNC_MMAP
107
107
 
108
108
  AC_SEARCH_LIBS([fabs], [m])
@@ -4,7 +4,7 @@ set -e
4
4
  set -x
5
5
  set -u
6
6
 
7
- DISTS=( wily trusty precise )
7
+ DISTS=( artful zesty xenial trusty precise )
8
8
 
9
9
  VERSION=$(perl -MFile::Slurp::Tiny=read_file -MDateTime <<EOF
10
10
  use v5.16;
@@ -27,11 +27,11 @@ pushd "$SRCDIR"
27
27
  git merge "$VERSION"
28
28
 
29
29
  for dist in "${DISTS[@]}"; do
30
- git clean -xfd
31
- git reset HEAD --hard
32
-
33
30
  dch -v "$VERSION-0+maxmind1~$dist" -D "$dist" -u low "New upstream release."
34
31
  gbp buildpackage -S --git-ignore-new
32
+
33
+ git clean -xfd
34
+ git reset HEAD --hard
35
35
  done
36
36
 
37
37
  read -e -p "Release to PPA? (y/n)" SHOULD_RELEASE
@@ -41,9 +41,12 @@ if [ "$SHOULD_RELEASE" != "y" ]; then
41
41
  exit 1
42
42
  fi
43
43
 
44
+ # Upload to launchpad
44
45
  dput ppa:maxmind/ppa ../*source.changes
45
46
 
46
- popd
47
+ # Make the changelog up to date in git
48
+
49
+ dch -v "$VERSION-0+maxmind1" -D "${DISTS[0]}" -u low "New upstream release."
47
50
 
48
51
  git add debian/changelog
49
52
  git commit -m "Update debian/changelog for $VERSION"
@@ -47,6 +47,13 @@ if [ -n "$(git status --porcelain)" ]; then
47
47
  git commit -m "Bumped version to $version"
48
48
  fi
49
49
 
50
+ ./bootstrap
51
+ ./configure
52
+ make
53
+ make check
54
+ make clean
55
+ make safedist
56
+
50
57
  if [ ! -d .gh-pages ]; then
51
58
  echo "Checking out gh-pages in .gh-pages"
52
59
  git clone -b gh-pages git@github.com:maxmind/libmaxminddb.git .gh-pages
@@ -1,5 +1,10 @@
1
1
  #!/usr/bin/env perl
2
2
 
3
+ # Note to run this you will probably want to build with ./configure
4
+ # --disable-shared. You don't want to valgrind the libtool script.
5
+ #
6
+ # Also make sure you compile the tests first (`make check').
7
+
3
8
  use strict;
4
9
  use warnings;
5
10
 
@@ -11,11 +16,13 @@ my $top_dir = "$Bin/..";
11
16
 
12
17
  my $output;
13
18
 
14
- my @tests = glob "$top_dir/t/.libs/lt-*_t";
19
+ my @tests;
20
+ push @tests, glob "$top_dir/t/*_t";
21
+ push @tests, glob "$top_dir/t/*-t";
15
22
 
16
23
  my @mmdblookup = (
17
24
  "$top_dir/bin/mmdblookup",
18
- '--file', "$top_dir/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb",
25
+ '--file', "$top_dir/t/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb",
19
26
  '--ip',
20
27
  );
21
28
 
@@ -31,7 +38,7 @@ my @cmds = (
31
38
  for my $cmd (@cmds) {
32
39
  my $output;
33
40
  run3(
34
- [ qw( valgrind --leak-check=full -- ), @{$cmd} ],
41
+ [ qw( valgrind -v --leak-check=full --show-leak-kinds=all -- ), @{$cmd} ],
35
42
  \undef,
36
43
  \$output,
37
44
  \$output,
@@ -5,8 +5,16 @@ extern "C" {
5
5
  #ifndef MAXMINDDB_H
6
6
  #define MAXMINDDB_H
7
7
 
8
+ /* Request POSIX.1-2008. However, we want to remain compatible with
9
+ * POSIX.1-2001 (since we have been historically and see no reason to drop
10
+ * compatibility). By requesting POSIX.1-2008, we can conditionally use
11
+ * features provided by that standard if the implementation provides it. We can
12
+ * check for what the implementation provides by checking the _POSIX_VERSION
13
+ * macro after including unistd.h. If a feature is in POSIX.1-2008 but not
14
+ * POSIX.1-2001, check that macro before using the feature (or check for the
15
+ * feature directly if possible). */
8
16
  #ifndef _POSIX_C_SOURCE
9
- #define _POSIX_C_SOURCE 200112L
17
+ #define _POSIX_C_SOURCE 200809L
10
18
  #endif
11
19
 
12
20
  #include "maxminddb_config.h"
@@ -20,7 +28,7 @@ extern "C" {
20
28
  #include <winsock2.h>
21
29
  #include <ws2tcpip.h>
22
30
  /* libmaxminddb package version from configure */
23
- #define PACKAGE_VERSION "1.2.1"
31
+ #define PACKAGE_VERSION "1.3.2"
24
32
 
25
33
  typedef ADDRESS_FAMILY sa_family_t;
26
34
 
@@ -135,6 +143,7 @@ typedef struct MMDB_entry_data_s {
135
143
  typedef struct MMDB_entry_data_list_s {
136
144
  MMDB_entry_data_s entry_data;
137
145
  struct MMDB_entry_data_list_s *next;
146
+ void *pool;
138
147
  } MMDB_entry_data_list_s;
139
148
 
140
149
  typedef struct MMDB_description_s {
@@ -20,9 +20,11 @@
20
20
  </ItemGroup>
21
21
  <ItemGroup>
22
22
  <ClCompile Include="..\..\src\maxminddb.c" />
23
+ <ClCompile Include="..\..\src\data-pool.c" />
23
24
  </ItemGroup>
24
25
  <ItemGroup>
25
26
  <ClInclude Include="..\..\include\maxminddb.h" />
27
+ <ClInclude Include="..\..\src\data-pool.h" />
26
28
  </ItemGroup>
27
29
  <ItemGroup>
28
30
  <None Include="README" />
@@ -138,4 +140,4 @@
138
140
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
139
141
  <ImportGroup Label="ExtensionTargets">
140
142
  </ImportGroup>
141
- </Project>
143
+ </Project>
@@ -14,13 +14,19 @@
14
14
  <ClCompile Include="..\..\src\maxminddb.c">
15
15
  <Filter>Source Files</Filter>
16
16
  </ClCompile>
17
+ <ClCompile Include="..\..\src\data-pool.c">
18
+ <Filter>Source Files</Filter>
19
+ </ClCompile>
17
20
  </ItemGroup>
18
21
  <ItemGroup>
19
22
  <ClInclude Include="..\..\include\maxminddb.h">
20
23
  <Filter>Header Files</Filter>
21
24
  </ClInclude>
25
+ <ClInclude Include="..\..\src\data-pool.h">
26
+ <Filter>Header Files</Filter>
27
+ </ClInclude>
22
28
  </ItemGroup>
23
29
  <ItemGroup>
24
30
  <None Include="README" />
25
31
  </ItemGroup>
26
- </Project>
32
+ </Project>
@@ -2,8 +2,24 @@ include $(top_srcdir)/common.mk
2
2
 
3
3
  lib_LTLIBRARIES = libmaxminddb.la
4
4
 
5
- libmaxminddb_la_SOURCES = maxminddb.c maxminddb-compat-util.h
6
- libmaxminddb_la_LDFLAGS = -version-info 0:7:0
5
+ libmaxminddb_la_SOURCES = maxminddb.c maxminddb-compat-util.h \
6
+ data-pool.c data-pool.h
7
+ libmaxminddb_la_LDFLAGS = -version-info 0:7:0 -export-symbols-regex '^MMDB_.*'
7
8
  include_HEADERS = $(top_srcdir)/include/maxminddb.h
8
9
 
9
10
  pkgconfig_DATA = libmaxminddb.pc
11
+
12
+ TESTS = test-data-pool
13
+
14
+ check_PROGRAMS = test-data-pool
15
+
16
+ test_data_pool_SOURCES = data-pool.c data-pool.h
17
+ test_data_pool_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/t -DTEST_DATA_POOL
18
+ test_data_pool_LDADD = $(top_srcdir)/t/libmmdbtest.la \
19
+ $(top_srcdir)/t/libtap/libtap.a
20
+
21
+ $(top_srcdir)/t/libmmdbtest.la:
22
+ $(MAKE) -C $(top_srcdir)/t libmmdbtest.la
23
+
24
+ $(top_srcdir)/t/libtap/libtap.a:
25
+ $(MAKE) -C $(top_srcdir)/t/libtap libtap.a