1 /* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> 2 3 This program is free software: you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation, either version 3 of the License, or 6 (at your option) any later version. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program. If not, see <https://www.gnu.org/licenses/>. 15 */ 16 17 #pragma once 18 19 #include <libknot/libknot.h> 20 #if HAVE_MAXMINDDB 21 #include <maxminddb.h> 22 #endif 23 24 #if HAVE_MAXMINDDB 25 #define geodb_t MMDB_s 26 #define geodb_data_t MMDB_entry_data_s 27 #else 28 #define geodb_t void 29 #define geodb_data_t char 30 #endif 31 32 // MaxMind DB related constants. 33 #define GEODB_MAX_PATH_LEN 8 34 #define GEODB_MAX_DEPTH 8 35 36 typedef enum { 37 GEODB_KEY_ID, 38 GEODB_KEY_TXT 39 } geodb_key_type_t; 40 41 static const knot_lookup_t geodb_key_types[] = { 42 { GEODB_KEY_ID, "id" }, 43 { GEODB_KEY_TXT, "" } 44 }; 45 46 typedef struct { 47 geodb_key_type_t type; 48 char *path[GEODB_MAX_PATH_LEN + 1]; // MMDB_aget_value() requires last member to be NULL. 49 } geodb_path_t; 50 51 int parse_geodb_path(geodb_path_t *path, const char *input); 52 53 int parse_geodb_data(const char *input, void **geodata, uint32_t *geodata_len, 54 uint8_t *geodepth, geodb_path_t *path, uint16_t path_cnt); 55 56 bool geodb_available(void); 57 58 geodb_t *geodb_open(const char *filename); 59 60 void geodb_close(geodb_t *geodb); 61 62 int geodb_query(geodb_t *geodb, geodb_data_t *entries, struct sockaddr *remote, 63 geodb_path_t *paths, uint16_t path_cnt, uint16_t *netmask); 64 65 void geodb_fill_geodata(geodb_data_t *entries, uint16_t path_cnt, 66 void **geodata, uint32_t *geodata_len, uint8_t *geodepth); 67