Add a cache of recently seen addresses.
[tinc] / src / address_cache.h
1 #ifndef TINC_ADDRESS_CACHE_H
2 #define TINC_ADDRESS_CACHE_H
3
4 /*
5     address_cache.h -- header for address_cache.c
6     Copyright (C) 2018 Guus Sliepen <guus@tinc-vpn.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "net.h"
24
25 #define MAX_CACHED_ADDRESSES 8
26 #define ADDRESS_CACHE_VERSION 1
27
28 typedef struct address_cache_t {
29         struct node_t *node;
30         struct splay_tree_t *config_tree;
31         struct config_t *cfg;
32         struct addrinfo *ai;
33         struct addrinfo *aip;
34         unsigned int tried;
35
36         struct {
37                 unsigned int version;
38                 unsigned int used;
39                 sockaddr_t address[MAX_CACHED_ADDRESSES];
40         } data;
41 } address_cache_t;
42
43 void add_recent_address(address_cache_t *cache, const sockaddr_t *sa);
44 const sockaddr_t *get_recent_address(address_cache_t *cache);
45
46 address_cache_t *open_address_cache(struct node_t *node);
47 void reset_address_cache(address_cache_t *cache, const sockaddr_t *sa);
48 void close_address_cache(address_cache_t *cache);
49
50 #endif