Skip to content

Commit 51441c5

Browse files
收录 MaxMind DB 读取库 compat.libmaxminddb 1.14.1 (#976)
1 parent bcf6e27 commit 51441c5

9 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 收录 libmaxminddb 1.14.1
2+
3+
从 MaxMind 正式发行归档编译公开 C 接口所需的两个源文件。沿用上游对 128 位整数和 Windows Winsock 的处理。安装钩子只解包和生成版本头,C 源码由消费工程的工具链编译成静态库。打包保留 Apache 2.0 许可、NOTICE 和兼容代码的 BSD 声明。
4+
5+
发行包从 GitHub 下载,以 SHA-256 校验。自动更新跟踪相同的标签和发行资产格式。消费测试使用发行归档随附的 MaxMind 公开测试数据库,按其 MIT 许可保留许可文本。
6+
7+
Linux、macOS 和 Windows 的用例覆盖 IPv4 和 IPv6 查询、国家码、ASN 数值、库类型和语言列表。

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ members = [
240240
"tests/examples/libevdev",
241241
"tests/examples/libgit2",
242242
"tests/examples/libinput",
243+
"tests/examples/libmaxminddb",
243244
"tests/examples/libseat",
244245
"tests/examples/libudev",
245246
"tests/examples/bluez",

‎pkgs/c/compat.libmaxminddb.lua‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
local releases = {
2+
["1.14.1"] = {
3+
url = "https://github.com/maxmind/libmaxminddb/releases/download/1.14.1/libmaxminddb-1.14.1.tar.gz",
4+
sha256 = "ca5c87d41339f8bc4daabb53e8a9356b3c995f2d2419b85d7bff823b2ecc252d",
5+
},
6+
}
7+
8+
package = {
9+
spec = "1",
10+
namespace = "compat",
11+
name = "libmaxminddb",
12+
description = "C reader for MaxMind DB files",
13+
licenses = { "Apache-2.0", "BSD-2-Clause", "BSD-3-Clause" },
14+
repo = "https://github.com/maxmind/libmaxminddb",
15+
type = "package",
16+
extensions = {
17+
autoupdate = {
18+
tag = "${version}",
19+
url = "https://github.com/maxmind/libmaxminddb/releases/download/${tag}/libmaxminddb-${version}.tar.gz",
20+
},
21+
},
22+
xpm = {
23+
linux = { ["1.14.1"] = releases["1.14.1"] },
24+
macosx = { ["1.14.1"] = releases["1.14.1"] },
25+
windows = { ["1.14.1"] = releases["1.14.1"] },
26+
},
27+
mcpp = {
28+
language = "c++23",
29+
import_std = false,
30+
c_standard = "c11",
31+
include_dirs = { "*/include", "mcpp_generated" },
32+
sources = { "*/src/maxminddb.c", "*/src/data-pool.c" },
33+
cflags = { "-include", "mcpp_maxminddb_version.h" },
34+
license_files = { "*/LICENSE", "*/NOTICE", "*/src/maxminddb-compat-util.h" },
35+
generated_files = {
36+
["mcpp_generated/maxminddb_config.h"] = [==[
37+
#ifndef MAXMINDDB_CONFIG_H
38+
#define MAXMINDDB_CONFIG_H
39+
#define MMDB_UINT128_USING_MODE 0
40+
#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER)
41+
# define MMDB_UINT128_IS_BYTE_ARRAY 0
42+
#else
43+
# define MMDB_UINT128_IS_BYTE_ARRAY 1
44+
#endif
45+
#endif
46+
]==],
47+
},
48+
targets = { ["maxminddb"] = { kind = "lib" } },
49+
windows = { cflags = { "-D_CRT_SECURE_NO_WARNINGS" }, ldflags = { "-lws2_32" } },
50+
},
51+
}
52+
53+
import("xim.libxpkg.pkginfo")
54+
55+
function install()
56+
local prefix = pkginfo.install_dir()
57+
local version = pkginfo.version()
58+
local source = "libmaxminddb-" .. version
59+
os.mkdir(prefix)
60+
os.mv(source, path.join(prefix, source))
61+
local generated = path.join(prefix, "mcpp_generated")
62+
os.mkdir(generated)
63+
io.writefile(path.join(generated, "mcpp_maxminddb_version.h"),
64+
'#define PACKAGE_VERSION "' .. version .. '"\n')
65+
return true
66+
end
19 KB
Binary file not shown.
12.4 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Permission is hereby granted, free of charge, to any person obtaining a copy of
2+
this software and associated documentation files (the "Software"), to deal in
3+
the Software without restriction, including without limitation the rights to
4+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5+
of the Software, and to permit persons to whom the Software is furnished to do
6+
so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all
9+
copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
SOFTWARE.
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "libmaxminddb-tests"
3+
version = "0.1.0"
4+
5+
[dependencies.compat]
6+
libmaxminddb = "1.14.1"
7+
8+
[indices]
9+
compat = { path = "../../.." }
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <maxminddb.h>
2+
3+
#include <cstdio>
4+
#include <filesystem>
5+
#include <stdexcept>
6+
#include <string_view>
7+
8+
namespace {
9+
10+
namespace fs = std::filesystem;
11+
12+
class Database {
13+
public:
14+
explicit Database(const fs::path& path) {
15+
const int status = MMDB_open(path.string().c_str(), MMDB_MODE_MMAP, &database);
16+
if (status != MMDB_SUCCESS) throw std::runtime_error(MMDB_strerror(status));
17+
}
18+
19+
~Database() { MMDB_close(&database); }
20+
21+
Database(const Database&) = delete;
22+
Database& operator=(const Database&) = delete;
23+
24+
const MMDB_metadata_s& metadata() const { return database.metadata; }
25+
26+
MMDB_entry_s lookup(const char* address) const {
27+
int address_error{};
28+
int database_error{};
29+
auto result = MMDB_lookup_string(&database, address, &address_error, &database_error);
30+
if (address_error || database_error != MMDB_SUCCESS || !result.found_entry) {
31+
throw std::runtime_error(address);
32+
}
33+
return result.entry;
34+
}
35+
36+
private:
37+
MMDB_s database{};
38+
};
39+
40+
void expect_country(const Database& database, const char* address, std::string_view expected) {
41+
auto entry = database.lookup(address);
42+
MMDB_entry_data_s country{};
43+
if (MMDB_get_value(&entry, &country, "country", "iso_code", nullptr) != MMDB_SUCCESS ||
44+
!country.has_data || country.type != MMDB_DATA_TYPE_UTF8_STRING ||
45+
std::string_view(country.utf8_string, country.data_size) != expected) {
46+
throw std::runtime_error(address);
47+
}
48+
}
49+
50+
void expect_asn(const Database& database, const char* address, uint32_t expected) {
51+
auto entry = database.lookup(address);
52+
MMDB_entry_data_s asn{};
53+
if (MMDB_get_value(&entry, &asn, "autonomous_system_number", nullptr) != MMDB_SUCCESS ||
54+
!asn.has_data || asn.type != MMDB_DATA_TYPE_UINT32 || asn.uint32 != expected) {
55+
throw std::runtime_error(address);
56+
}
57+
}
58+
59+
}
60+
61+
int main() try {
62+
#ifdef _WIN32
63+
WSADATA winsock{};
64+
if (WSAStartup(MAKEWORD(2, 2), &winsock) != 0) return 1;
65+
#endif
66+
const auto fixtures = fs::path(__FILE__).parent_path().parent_path() / "fixtures";
67+
const Database country(fixtures / "GeoIP2-Country-Test.mmdb");
68+
const Database asn(fixtures / "GeoLite2-ASN-Test.mmdb");
69+
70+
expect_country(country, "2.125.160.216", "GB");
71+
expect_country(country, "2001:218::1", "JP");
72+
expect_asn(asn, "1.0.0.1", 15169);
73+
expect_asn(asn, "2001:1700::1", 6730);
74+
const Database metadata(fixtures / "MaxMind-DB-test-ipv4-24.mmdb");
75+
const auto& languages = metadata.metadata().languages;
76+
if (std::string_view(country.metadata().database_type) != "GeoIP2-Country" ||
77+
std::string_view(asn.metadata().database_type) != "GeoLite2-ASN" ||
78+
languages.count != 2 ||
79+
std::string_view(languages.names[0]) != "en" ||
80+
std::string_view(languages.names[1]) != "zh") {
81+
throw std::runtime_error("unexpected database metadata");
82+
}
83+
#ifdef _WIN32
84+
WSACleanup();
85+
#endif
86+
std::printf("libmaxminddb %s: IPv4, IPv6, country codes, ASNs and language metadata passed\n",
87+
MMDB_lib_version());
88+
return 0;
89+
} catch (const std::exception& error) {
90+
std::fprintf(stderr, "MMDB query failed: %s\n", error.what());
91+
return 1;
92+
}

0 commit comments

Comments
 (0)