Check all Address statements when making outgoing connections.
[tinc] / src / address_cache.c
1 /*
2     address_cache.c -- Manage cache of recently seen addresses
3     Copyright (C) 2018 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "address_cache.h"
23 #include "conf.h"
24 #include "names.h"
25 #include "netutl.h"
26 #include "xalloc.h"
27
28 static const unsigned int NOT_CACHED = -1;
29
30 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
31 static struct addrinfo *get_known_addresses(node_t *n) {
32         struct addrinfo *ai = NULL;
33         struct addrinfo *oai = NULL;
34
35         for splay_each(edge_t, e, n->edge_tree) {
36                 if(!e->reverse) {
37                         continue;
38                 }
39
40                 bool found = false;
41
42                 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
43                         if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
44                                 found = true;
45                                 break;
46                         }
47                 }
48
49                 if(found) {
50                         continue;
51                 }
52
53                 oai = ai;
54                 ai = xzalloc(sizeof(*ai));
55                 ai->ai_family = e->reverse->address.sa.sa_family;
56                 ai->ai_socktype = SOCK_STREAM;
57                 ai->ai_protocol = IPPROTO_TCP;
58                 ai->ai_addrlen = SALEN(e->reverse->address.sa);
59                 ai->ai_addr = xmalloc(ai->ai_addrlen);
60                 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
61                 ai->ai_next = oai;
62         }
63
64         return ai;
65 }
66
67 static void free_known_addresses(struct addrinfo *ai) {
68         for(struct addrinfo *aip = ai, *next; aip; aip = next) {
69                 next = aip->ai_next;
70                 free(aip);
71         }
72 }
73
74 static unsigned int find_cached(address_cache_t *cache, const sockaddr_t *sa) {
75         for(unsigned int i = 0; i < cache->data.used; i++)
76                 if(!sockaddrcmp(&cache->data.address[i], sa)) {
77                         return i;
78                 }
79
80         return NOT_CACHED;
81 }
82
83 void add_recent_address(address_cache_t *cache, const sockaddr_t *sa) {
84         // Check if it's already cached
85         unsigned int pos = find_cached(cache, sa);
86
87         // It's in the first spot, so nothing to do
88         if(pos == 0) {
89                 return;
90         }
91
92         // Shift everything, move/add the address to the first slot
93         if(pos == NOT_CACHED) {
94                 if(cache->data.used < MAX_CACHED_ADDRESSES) {
95                         cache->data.used++;
96                 }
97
98                 pos = cache->data.used - 1;
99         }
100
101         memmove(&cache->data.address[1], &cache->data.address[0], pos * sizeof(cache->data.address[0]));
102
103         cache->data.address[0] = *sa;
104
105         // Write the cache
106         char fname[PATH_MAX];
107         snprintf(fname, sizeof(fname), "%s" SLASH "cache" SLASH "%s", confbase, cache->node->name);
108         FILE *fp = fopen(fname, "wb");
109
110         if(fp) {
111                 fwrite(&cache->data, sizeof(cache->data), 1, fp);
112                 fclose(fp);
113         }
114 }
115
116 const sockaddr_t *get_recent_address(address_cache_t *cache) {
117         // Check if there is an address in our cache of recently seen addresses
118         if(cache->tried < cache->data.used) {
119                 return &cache->data.address[cache->tried++];
120         }
121
122         // Next, check any recently seen addresses not in our cache
123         while(cache->tried == cache->data.used) {
124                 if(!cache->ai) {
125                         cache->aip = cache->ai = get_known_addresses(cache->node);
126                 }
127
128                 if(cache->ai) {
129                         if(cache->aip) {
130                                 sockaddr_t *sa = (sockaddr_t *)cache->aip->ai_addr;
131                                 cache->aip = cache->aip->ai_next;
132
133                                 if(find_cached(cache, sa) != NOT_CACHED) {
134                                         continue;
135                                 }
136
137                                 return sa;
138                         } else {
139                                 free_known_addresses(cache->ai);
140                                 cache->ai = NULL;
141                         }
142                 }
143
144                 cache->tried++;
145         }
146
147         // Otherwise, check if there are any known Address statements
148         if(!cache->config_tree) {
149                 init_configuration(&cache->config_tree);
150                 read_host_config(cache->config_tree, cache->node->name, false);
151                 cache->cfg = lookup_config(cache->config_tree, "Address");
152         }
153
154         while(cache->cfg && !cache->aip) {
155                 char *address, *port;
156
157                 get_config_string(cache->cfg, &address);
158
159                 char *space = strchr(address, ' ');
160
161                 if(space) {
162                         port = xstrdup(space + 1);
163                         *space = 0;
164                 } else {
165                         if(!get_config_string(lookup_config(cache->config_tree, "Port"), &port)) {
166                                 port = xstrdup("655");
167                         }
168                 }
169
170                 if(cache->ai) {
171                         free_known_addresses(cache->ai);
172                 }
173
174                 cache->aip = cache->ai = str2addrinfo(address, port, SOCK_STREAM);
175
176                 if(cache->ai) {
177                         struct addrinfo *ai = NULL;
178
179                         for(; cache->aip; cache->aip = cache->aip->ai_next) {
180                                 struct addrinfo *oai = ai;
181
182                                 ai = xzalloc(sizeof(*ai));
183                                 ai->ai_family = cache->aip->ai_family;
184                                 ai->ai_socktype = cache->aip->ai_socktype;
185                                 ai->ai_protocol = cache->aip->ai_protocol;
186                                 ai->ai_addrlen = cache->aip->ai_addrlen;
187                                 ai->ai_addr = xmalloc(ai->ai_addrlen);
188                                 memcpy(ai->ai_addr, cache->aip->ai_addr, ai->ai_addrlen);
189                                 ai->ai_next = oai;
190                         }
191
192                         freeaddrinfo(cache->ai);
193                         cache->aip = cache->ai = ai;
194                 }
195
196                 free(address);
197                 free(port);
198
199                 cache->cfg = lookup_config_next(cache->config_tree, cache->cfg);
200         }
201
202         if(cache->ai) {
203                 if(cache->aip) {
204                         sockaddr_t *sa = (sockaddr_t *)cache->aip->ai_addr;
205
206                         cache->aip = cache->aip->ai_next;
207                         return sa;
208                 } else {
209                         free_known_addresses(cache->ai);
210                         cache->ai = NULL;
211                 }
212         }
213
214         // We're all out of addresses.
215         exit_configuration(&cache->config_tree);
216         return false;
217 }
218
219 address_cache_t *open_address_cache(node_t *node) {
220         address_cache_t *cache = xmalloc(sizeof(*cache));
221         cache->node = node;
222
223         // Try to open an existing address cache
224         char fname[PATH_MAX];
225         snprintf(fname, sizeof(fname), "%s" SLASH "cache" SLASH "%s", confbase, node->name);
226         FILE *fp = fopen(fname, "rb");
227
228         if(!fp || fread(&cache->data, sizeof(cache->data), 1, fp) != 1 || cache->data.version != ADDRESS_CACHE_VERSION) {
229                 memset(&cache->data, 0, sizeof(cache->data));
230         }
231
232         if(fp) {
233                 fclose(fp);
234         }
235
236         // Ensure we have a valid state
237         cache->config_tree = NULL;
238         cache->cfg = NULL;
239         cache->ai = NULL;
240         cache->aip = NULL;
241         cache->tried = 0;
242         cache->data.version = ADDRESS_CACHE_VERSION;
243
244         if(cache->data.used > MAX_CACHED_ADDRESSES) {
245                 cache->data.used = 0;
246         }
247
248         return cache;
249 }
250
251 void reset_address_cache(address_cache_t *cache, const sockaddr_t *sa) {
252         if(sa) {
253                 add_recent_address(cache, sa);
254         }
255
256         if(cache->config_tree) {
257                 exit_configuration(&cache->config_tree);
258         }
259
260         if(cache->ai) {
261                 free_known_addresses(cache->ai);
262         }
263
264         cache->config_tree = NULL;
265         cache->cfg = NULL;
266         cache->ai = NULL;
267         cache->aip = NULL;
268         cache->tried = 0;
269 }
270
271 void close_address_cache(address_cache_t *cache) {
272         if(cache->config_tree) {
273                 exit_configuration(&cache->config_tree);
274         }
275
276         if(cache->ai) {
277                 free_known_addresses(cache->ai);
278         }
279
280         free(cache);
281 }