summaryrefslogtreecommitdiffstats
path: root/src/knot/modules/geoip/geodb.h
blob: 2ec870111918255fe89200c0402a29d7a25cc1bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include <libknot/libknot.h>
#if HAVE_MAXMINDDB
#include <maxminddb.h>
#endif

#if HAVE_MAXMINDDB
#define geodb_t		MMDB_s
#define geodb_data_t	MMDB_entry_data_s
#else
#define geodb_t		void
#define geodb_data_t	char
#endif

// MaxMind DB related constants.
#define GEODB_MAX_PATH_LEN 8
#define GEODB_MAX_DEPTH 8

typedef enum {
	GEODB_KEY_ID,
	GEODB_KEY_TXT
} geodb_key_type_t;

static const knot_lookup_t geodb_key_types[] = {
	{ GEODB_KEY_ID, "id" },
	{ GEODB_KEY_TXT, "" },
	{ 0, NULL }
};

typedef struct {
	geodb_key_type_t type;
	char *path[GEODB_MAX_PATH_LEN + 1]; // MMDB_aget_value() requires last member to be NULL.
} geodb_path_t;

int parse_geodb_path(geodb_path_t *path, const char *input);

int parse_geodb_data(const char *input, void **geodata, uint32_t *geodata_len,
                     uint8_t *geodepth, geodb_path_t *path, uint16_t path_cnt);

bool geodb_available(void);

geodb_t *geodb_open(const char *filename);

void geodb_close(geodb_t *geodb);

int geodb_query(geodb_t *geodb, geodb_data_t *entries, struct sockaddr *remote,
                geodb_path_t *paths, uint16_t path_cnt, uint16_t *netmask);

void geodb_fill_geodata(geodb_data_t *entries, uint16_t path_cnt,
                        void **geodata, uint32_t *geodata_len, uint8_t *geodepth);