diff options
author | Timo Teräs <timo.teras@iki.fi> | 2013-11-14 15:33:16 +0200 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2013-11-14 15:33:16 +0200 |
commit | 6fd658a9994c9d1d1aa3447bddc8c1a15744e766 (patch) | |
tree | 150cfd517c52015f1081c23f2fb2092cbd4dbc4c /main/libmaxminddb | |
parent | 3df7efd33ac16ad57c9d2c2e8cb7b29576edd6fa (diff) | |
download | aports-6fd658a9994c9d1d1aa3447bddc8c1a15744e766.tar.bz2 aports-6fd658a9994c9d1d1aa3447bddc8c1a15744e766.tar.xz |
main/libmaxminddb: new aport
Maxmind GeoIP2 database library
https://github.com/maxmind/$pkgname
Diffstat (limited to 'main/libmaxminddb')
-rw-r--r-- | main/libmaxminddb/0001-avoid-unneeded-memory-allocations.patch | 112 | ||||
-rw-r--r-- | main/libmaxminddb/0002-make-MMDB_aget_value-s-path-argument-const-correct.patch | 174 | ||||
-rw-r--r-- | main/libmaxminddb/APKBUILD | 72 | ||||
-rwxr-xr-x | main/libmaxminddb/libmaxminddb.confd | 8 | ||||
-rwxr-xr-x | main/libmaxminddb/libmaxminddb.cron | 22 |
5 files changed, 388 insertions, 0 deletions
diff --git a/main/libmaxminddb/0001-avoid-unneeded-memory-allocations.patch b/main/libmaxminddb/0001-avoid-unneeded-memory-allocations.patch new file mode 100644 index 0000000000..3126a56036 --- /dev/null +++ b/main/libmaxminddb/0001-avoid-unneeded-memory-allocations.patch @@ -0,0 +1,112 @@ +From f4e82c231bd5089db5ee8b8082489304264bc805 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi> +Date: Thu, 14 Nov 2013 14:35:18 +0200 +Subject: [PATCH 1/2] avoid unneeded memory allocations + +--- + bin/mmdblookup.c | 18 ++++++------------ + src/maxminddb.c | 14 +++++--------- + 2 files changed, 11 insertions(+), 21 deletions(-) + +diff --git a/bin/mmdblookup.c b/bin/mmdblookup.c +index ac72a93..4b7733d 100644 +--- a/bin/mmdblookup.c ++++ b/bin/mmdblookup.c +@@ -24,7 +24,7 @@ LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address, + int lookup_path_length); + LOCAL int benchmark(MMDB_s *mmdb, int iterations); + LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr); +-LOCAL char *random_ipv4(); ++LOCAL void random_ipv4(char *ip); + /* --prototypes end - don't remove this comment-- */ + /* *INDENT-ON* */ + +@@ -304,13 +304,14 @@ LOCAL int lookup_and_print(MMDB_s *mmdb, const char *ip_address, + + LOCAL int benchmark(MMDB_s *mmdb, int iterations) + { ++ char ip_address[16]; + int exit_code = 0; +- srand( time(NULL) ); + ++ srand( time(NULL) ); + clock_t time = clock(); + + for (int i = 0; i < iterations; i++) { +- char *ip_address = random_ipv4(); ++ random_ipv4(ip_address); + + MMDB_lookup_result_s result = lookup_or_die(mmdb, ip_address); + MMDB_entry_data_list_s *entry_data_list = NULL; +@@ -324,13 +325,11 @@ LOCAL int benchmark(MMDB_s *mmdb, int iterations) + fprintf(stderr, "Got an error looking up the entry data - %s\n", + MMDB_strerror(status)); + exit_code = 5; +- free(ip_address); + MMDB_free_entry_data_list(entry_data_list); + goto end; + } + } + +- free(ip_address); + MMDB_free_entry_data_list(entry_data_list); + } + +@@ -369,13 +368,8 @@ LOCAL MMDB_lookup_result_s lookup_or_die(MMDB_s *mmdb, const char *ipstr) + return result; + } + +-LOCAL char *random_ipv4() ++LOCAL void random_ipv4(char *ip) + { +- int ip_int = rand(); +- uint8_t *bytes = (uint8_t *)&ip_int; +- +- char *ip = malloc(16); + snprintf(ip, 16, "%u.%u.%u.%u", +- *bytes, *(bytes + 1), *(bytes + 2), *(bytes + 3)); +- return ip; ++ rand()&0xff, rand()&0xff, rand()&0xff, rand()&0xff); + } +diff --git a/src/maxminddb.c b/src/maxminddb.c +index f3a7dfd..3bf7154 100644 +--- a/src/maxminddb.c ++++ b/src/maxminddb.c +@@ -625,20 +625,18 @@ MMDB_lookup_result_s MMDB_lookup_sockaddr(MMDB_s *mmdb, + } + }; + +- uint8_t *address; ++ uint8_t mapped_address[16], *address; + if (mmdb->metadata.ip_version == 4) { + if (sockaddr->sa_family == AF_INET6) { + return result; + } +- address = malloc(4); +- memcpy(address, &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr, 4); ++ address = (uint8_t*) &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; + } else { +- // We need calloc() here for the IPv4 case - the first 12 bytes must be 0 +- address = calloc(1, 16); + if (sockaddr->sa_family == AF_INET6) { +- memcpy(address, +- ((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr, 16); ++ address = (uint8_t*) &((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr; + } else { ++ address = mapped_address; ++ memset(address, 0, 12); + memcpy(address + 12, + &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr, 4); + } +@@ -648,8 +646,6 @@ MMDB_lookup_result_s MMDB_lookup_sockaddr(MMDB_s *mmdb, + find_address_in_search_tree(mmdb, address, sockaddr->sa_family, + &result); + +- free(address); +- + return result; + } + +-- +1.8.4.1 + diff --git a/main/libmaxminddb/0002-make-MMDB_aget_value-s-path-argument-const-correct.patch b/main/libmaxminddb/0002-make-MMDB_aget_value-s-path-argument-const-correct.patch new file mode 100644 index 0000000000..958067185f --- /dev/null +++ b/main/libmaxminddb/0002-make-MMDB_aget_value-s-path-argument-const-correct.patch @@ -0,0 +1,174 @@ +From ea773070121c9e0ef55680af4d9b45a8be6bfe1b Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi> +Date: Thu, 14 Nov 2013 15:28:04 +0200 +Subject: [PATCH 2/2] make MMDB_aget_value()'s path argument const correct + +--- + include/maxminddb.h | 2 +- + src/maxminddb.c | 49 ++++++++++++++++++------------------------------- + 2 files changed, 19 insertions(+), 32 deletions(-) + +diff --git a/include/maxminddb.h b/include/maxminddb.h +index 599d4d5..3c6e645 100644 +--- a/include/maxminddb.h ++++ b/include/maxminddb.h +@@ -178,7 +178,7 @@ typedef struct MMDB_search_node_s { + extern int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, + va_list va_path); + extern int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, +- char **path); ++ const char *const * path); + extern int MMDB_get_metadata_as_entry_data_list( + MMDB_s *mmdb, MMDB_entry_data_list_s **entry_data_list); + extern int MMDB_get_entry_data_list(MMDB_entry_s *start, +diff --git a/src/maxminddb.c b/src/maxminddb.c +index 3bf7154..fc82c9a 100644 +--- a/src/maxminddb.c ++++ b/src/maxminddb.c +@@ -132,9 +132,9 @@ LOCAL int populate_result(MMDB_s *mmdb, uint32_t node_count, uint32_t value, + uint16_t netmask, MMDB_lookup_result_s *result); + LOCAL uint32_t get_left_28_bit_record(const uint8_t *record); + LOCAL uint32_t get_right_28_bit_record(const uint8_t *record); +-LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, ++LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb, + MMDB_entry_data_s *entry_data); +-LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb, ++LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb, + MMDB_entry_data_s *entry_data); + LOCAL int skip_map_or_array(MMDB_s *mmdb, MMDB_entry_data_s *entry_data); + LOCAL int decode_one_follow(MMDB_s *mmdb, uint32_t offset, +@@ -402,7 +402,7 @@ LOCAL MMDB_s make_fake_metadata_db(MMDB_s *mmdb) + LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key) + { + MMDB_entry_data_s entry_data; +- char *path[] = { key, NULL }; ++ const char *path[] = { key, NULL }; + MMDB_aget_value(start, &entry_data, path); + return entry_data.uint16; + } +@@ -410,7 +410,7 @@ LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key) + LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key) + { + MMDB_entry_data_s entry_data; +- char *path[] = { key, NULL }; ++ const char *path[] = { key, NULL }; + MMDB_aget_value(start, &entry_data, path); + return entry_data.uint32; + } +@@ -418,7 +418,7 @@ LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key) + LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key) + { + MMDB_entry_data_s entry_data; +- char *path[] = { key, NULL }; ++ const char *path[] = { key, NULL }; + MMDB_aget_value(start, &entry_data, path); + return entry_data.uint64; + } +@@ -426,7 +426,7 @@ LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key) + LOCAL char *value_for_key_as_string(MMDB_entry_s *start, char *key) + { + MMDB_entry_data_s entry_data; +- char *path[] = { key, NULL }; ++ const char *path[] = { key, NULL }; + MMDB_aget_value(start, &entry_data, path); + return strndup((char *)entry_data.utf8_string, entry_data.data_size); + } +@@ -436,7 +436,7 @@ LOCAL int populate_languages_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, + { + MMDB_entry_data_s entry_data; + +- char *path[] = { "languages", NULL }; ++ const char *path[] = { "languages", NULL }; + MMDB_aget_value(metadata_start, &entry_data, path); + + if (MMDB_DATA_TYPE_ARRAY != entry_data.type) { +@@ -487,7 +487,7 @@ LOCAL int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, + { + MMDB_entry_data_s entry_data; + +- char *path[] = { "description", NULL }; ++ const char *path[] = { "description", NULL }; + MMDB_aget_value(metadata_start, &entry_data, path); + + if (MMDB_DATA_TYPE_MAP != entry_data.type) { +@@ -831,43 +831,30 @@ int MMDB_get_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, ...) + int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, + va_list va_path) + { +- char **path = NULL; +- ++ const char **path = NULL; ++ const char *path_elem; + int i = 0; +- char *path_elem; +- while (NULL != (path_elem = va_arg(va_path, char *))) { +- path = realloc(path, sizeof(char *) * (i + 1)); ++ ++ while (NULL != (path_elem = va_arg(va_path, const char *))) { ++ path = realloc(path, sizeof(const char *) * (i + 2)); + if (NULL == path) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + +- path[i] = strdup(path_elem); +- if (NULL == path[i]) { +- return MMDB_OUT_OF_MEMORY_ERROR; +- } ++ path[i] = path_elem; + i++; + } +- +- path = realloc(path, sizeof(char *) * (i + 1)); +- if (NULL == path) { +- return MMDB_OUT_OF_MEMORY_ERROR; +- } + path[i] = NULL; + + int status = MMDB_aget_value(start, entry_data, path); + +- i = 0; +- char *elem; +- while (NULL != (elem = path[i++])) { +- free(elem); +- } + free(path); + + return status; + } + + int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, +- char **path) ++ const char *const * path) + { + MMDB_s *mmdb = start->mmdb; + uint32_t offset = start->offset; +@@ -888,7 +875,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, + return MMDB_INVALID_LOOKUP_PATH_ERROR; + } + +- char *path_elem; ++ const char *path_elem; + while (NULL != (path_elem = *(path++))) { + DEBUG_NL; + DEBUG_MSGF("path elem = %s", path_elem); +@@ -921,7 +908,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, + return MMDB_SUCCESS; + } + +-LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, ++LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb, + MMDB_entry_data_s *entry_data) + { + uint32_t size = entry_data->data_size; +@@ -952,7 +939,7 @@ LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, + return MMDB_SUCCESS; + } + +-LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb, ++LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb, + MMDB_entry_data_s *entry_data) + { + uint32_t size = entry_data->data_size; +-- +1.8.4.1 + diff --git a/main/libmaxminddb/APKBUILD b/main/libmaxminddb/APKBUILD new file mode 100644 index 0000000000..1126cec575 --- /dev/null +++ b/main/libmaxminddb/APKBUILD @@ -0,0 +1,72 @@ +# Maintainer: Timo Teräs <timo.teras@iki.fi> +pkgname=libmaxminddb +pkgver=0.0.20131114 +_commitid=159b18652fb265025e1617266692f8e5a39cccf6 +pkgrel=0 +pkgdesc="Maxmind GeoIP2 database library" +url="https://github.com/maxmind/$pkgname" +arch="all" +license="LGPL" +depends="curl" +makedepends="automake autoconf libtool" +install="" +options="" +subpackages="$pkgname-dev" +source="libmaxminddb-$pkgver.tar.gz::$url/archive/$_commitid.tar.gz + 0001-avoid-unneeded-memory-allocations.patch + 0002-make-MMDB_aget_value-s-path-argument-const-correct.patch + + libmaxminddb.cron + libmaxminddb.confd + " + +_builddir="$srcdir"/$pkgname-$_commitid + +prepare() { + local i + cd "$_builddir" + for i in $source; do + case $i in + *.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;; + esac + done + ./bootstrap +} + +build() { + cd "$_builddir" + ./configure \ + --build=$CBUILD \ + --host=$CHOST \ + --prefix=/usr \ + --with-pic \ + || return 1 + make || return 1 +} + +package() { + cd "$_builddir" + make install DESTDIR="$pkgdir" || return 1 + rm -rf "$pkgdir"/usr/lib/*.la || return 0 + + # install alpine specifics + mkdir -p "$pkgdir"/var/lib/libmaxminddb + install -m755 -D "$srcdir"/libmaxminddb.cron "$pkgdir"/etc/periodic/weekly/libmaxminddb + install -m755 -D "$srcdir"/libmaxminddb.confd "$pkgdir"/etc/conf.d/libmaxminddb +} + +md5sums="0d0005c520e5fa98b7bbf4a46acf41a3 libmaxminddb-0.0.20131114.tar.gz +4f1231828896d02da82bb14e0ca21db2 0001-avoid-unneeded-memory-allocations.patch +19cab6295f75fb72d50328be44f5ef5a 0002-make-MMDB_aget_value-s-path-argument-const-correct.patch +3866d16335eeaa8573cf625981979a56 libmaxminddb.cron +f86fba9d801d5f9c76166c11c224f474 libmaxminddb.confd" +sha256sums="8db56de9bf067461239bcb11974c3e148e2fc1217c82e57f7228a8b230f7cf77 libmaxminddb-0.0.20131114.tar.gz +9a6a95dd339189288f8af3e5e8f5ec9a18411df77f6d8bde8b6702b95f73dce2 0001-avoid-unneeded-memory-allocations.patch +278da1a942df301c1acb19b6851039e495b3f968b2603ab317a853f220c428d4 0002-make-MMDB_aget_value-s-path-argument-const-correct.patch +34f32f544f0537e37783de61f09e4b5b7b080aaa2e9514afe1eaea8425f547e9 libmaxminddb.cron +f8af67264b8711ab40f99d364e4987c14f4928932624993c72188f59203526be libmaxminddb.confd" +sha512sums="f606fb22c3887953e7fc7a798c4065b658c6895e58838e14b1bde66fd8d7907e27f6ee8545bd510d9745432f7176047e4d36be2ce20b035d20589d1106f6784a libmaxminddb-0.0.20131114.tar.gz +99eb21e9e0b43346ccf8eb72f40403dec36866cbff992c1aea771abcdbe9c820fdb24c64e45f4d8f6fb811e3330591e31851ec626f8e3d84cd8f34754d030519 0001-avoid-unneeded-memory-allocations.patch +655b79e806ca0801d5609aa9e582533fcb2719f3f0eef88e3924135888a1791cb551843261834a01ec8f3d6569d843a3e97970d8be239cd6983ba13cdda847b3 0002-make-MMDB_aget_value-s-path-argument-const-correct.patch +1feb1f2dd57991d729b6f9d29834f43d7405038cdbdfb0113a0e8f8f951a74c5e40651f9d241460f110acdd300196cf580b370e6cec56985cca797ba5610e622 libmaxminddb.cron +5f8dc6dad84cb1d188504a22470acf89542755c0bb3a78e4d3ae4e5bfa49fe64a7d2ee17441084db2710115463d39361df060a74b3a48fc4d8fc5e802afd2099 libmaxminddb.confd" diff --git a/main/libmaxminddb/libmaxminddb.confd b/main/libmaxminddb/libmaxminddb.confd new file mode 100755 index 0000000000..6955e542f1 --- /dev/null +++ b/main/libmaxminddb/libmaxminddb.confd @@ -0,0 +1,8 @@ +# Database files to download +# MAXMINDDB_FILES="GeoLite2-City.mmdb" + +# Database download directory +# MAXMINDDB_URL="http://geolite.maxmind.com/download/geoip/database" + +# Local target directory +# MAXMINDDB_LIBDIR="/var/lib/libmaxminddb" diff --git a/main/libmaxminddb/libmaxminddb.cron b/main/libmaxminddb/libmaxminddb.cron new file mode 100755 index 0000000000..9b6e5fab8e --- /dev/null +++ b/main/libmaxminddb/libmaxminddb.cron @@ -0,0 +1,22 @@ +#!/bin/sh + +. /etc/conf.d/libmaxminddb + +set -e + +[ -z "$MAXMINDDB_FILES" ] && MAXMINDDB_FILES="GeoLite2-City.mmdb" +[ -z "$MAXMINDDB_URL" ] && MAXMINDDB_URL="http://geolite.maxmind.com/download/geoip/database" +[ -z "$MAXMINDDB_LIBDIR" ] && MAXMINDDB_LIBDIR="/var/lib/libmaxminddb" + +clean_up() { + [ -n "$TMPDIR" ] && rm -rf "$TMPDIR" +} +trap clean_up EXIT SIGTERM SIGINT SIGQUIT + +TMPDIR="$(mktemp -d)" +for filename in $MAXMINDDB_FILES; do + curl --silent "$MAXMINDDB_URL/$filename.gz" -o "$TMPDIR/$filename.gz" + gunzip "$TMPDIR/$filename.gz" + mv "$TMPDIR/$filename" "$MAXMINDDB_LIBDIR" +done +exit 0 |