diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-02-03 17:40:44 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-02-03 17:40:44 +0000 |
commit | 799030df01f2306a804c5389dd936106c0a6dd42 (patch) | |
tree | 8b4f1d1bd8224e4a1daf285002c95f7e0db05f8a /src | |
parent | d9b68f30f930830b455eea95d0bc619553180136 (diff) | |
download | gnunet-799030df01f2306a804c5389dd936106c0a6dd42.tar.gz gnunet-799030df01f2306a804c5389dd936106c0a6dd42.zip |
-forgot to add file
Diffstat (limited to 'src')
-rw-r--r-- | src/ats/ats_api_scanner.c | 451 | ||||
-rw-r--r-- | src/ats/ats_api_scheduling.c | 4 |
2 files changed, 455 insertions, 0 deletions
diff --git a/src/ats/ats_api_scanner.c b/src/ats/ats_api_scanner.c new file mode 100644 index 000000000..43e79b3cf --- /dev/null +++ b/src/ats/ats_api_scanner.c | |||
@@ -0,0 +1,451 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010-2015 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file ats/ats_api_scanner.c | ||
22 | * @brief LAN interface scanning to determine IPs in LAN | ||
23 | * @author Christian Grothoff | ||
24 | * @author Matthias Wachs | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_ats_service.h" | ||
28 | |||
29 | /** | ||
30 | * How frequently do we scan the interfaces for changes to the addresses? | ||
31 | */ | ||
32 | #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2) | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Convert a `enum GNUNET_ATS_Network_Type` to a string | ||
37 | * | ||
38 | * @param net the network type | ||
39 | * @return a string or NULL if invalid | ||
40 | */ | ||
41 | const char * | ||
42 | GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net) | ||
43 | { | ||
44 | switch (net) | ||
45 | { | ||
46 | case GNUNET_ATS_NET_UNSPECIFIED: | ||
47 | return "UNSPECIFIED"; | ||
48 | case GNUNET_ATS_NET_LOOPBACK: | ||
49 | return "LOOPBACK"; | ||
50 | case GNUNET_ATS_NET_LAN: | ||
51 | return "LAN"; | ||
52 | case GNUNET_ATS_NET_WAN: | ||
53 | return "WAN"; | ||
54 | case GNUNET_ATS_NET_WLAN: | ||
55 | return "WLAN"; | ||
56 | case GNUNET_ATS_NET_BT: | ||
57 | return "BLUETOOTH"; | ||
58 | default: | ||
59 | return NULL; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | |||
64 | /** | ||
65 | * Convert a ATS property to a string | ||
66 | * | ||
67 | * @param type the property type | ||
68 | * @return a string or NULL if invalid | ||
69 | */ | ||
70 | const char * | ||
71 | GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type) | ||
72 | { | ||
73 | switch (type) | ||
74 | { | ||
75 | case GNUNET_ATS_ARRAY_TERMINATOR: | ||
76 | return "TERMINATOR"; | ||
77 | case GNUNET_ATS_UTILIZATION_OUT: | ||
78 | return "UTILIZATION_UP"; | ||
79 | case GNUNET_ATS_UTILIZATION_IN: | ||
80 | return "UTILIZATION_DOWN"; | ||
81 | case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT: | ||
82 | return "UTILIZATION_PAYLOAD_UP"; | ||
83 | case GNUNET_ATS_UTILIZATION_PAYLOAD_IN: | ||
84 | return "UTILIZATION_PAYLOAD_DOWN"; | ||
85 | case GNUNET_ATS_NETWORK_TYPE: | ||
86 | return "NETWORK_TYPE"; | ||
87 | case GNUNET_ATS_QUALITY_NET_DELAY: | ||
88 | return "DELAY"; | ||
89 | case GNUNET_ATS_QUALITY_NET_DISTANCE: | ||
90 | return "DISTANCE"; | ||
91 | case GNUNET_ATS_COST_WAN: | ||
92 | return "COST_WAN"; | ||
93 | case GNUNET_ATS_COST_LAN: | ||
94 | return "COST_LAN"; | ||
95 | case GNUNET_ATS_COST_WLAN: | ||
96 | return "COST_WLAN"; | ||
97 | default: | ||
98 | return NULL; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | |||
103 | /** | ||
104 | * We keep a list of our local networks so we can answer | ||
105 | * LAN vs. WAN questions. Note: WLAN is not detected yet. | ||
106 | * (maybe we can do that heuristically based on interface | ||
107 | * name in the future?). | ||
108 | */ | ||
109 | struct ATS_Network | ||
110 | { | ||
111 | /** | ||
112 | * Kept in a DLL. | ||
113 | */ | ||
114 | struct ATS_Network *next; | ||
115 | |||
116 | /** | ||
117 | * Kept in a DLL. | ||
118 | */ | ||
119 | struct ATS_Network *prev; | ||
120 | |||
121 | /** | ||
122 | * Network address. | ||
123 | */ | ||
124 | struct sockaddr *network; | ||
125 | |||
126 | /** | ||
127 | * Netmask to determine what is in the LAN. | ||
128 | */ | ||
129 | struct sockaddr *netmask; | ||
130 | |||
131 | /** | ||
132 | * How long are @e network and @e netmask? | ||
133 | */ | ||
134 | socklen_t length; | ||
135 | }; | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Handle to the interface scanner. | ||
140 | */ | ||
141 | struct GNUNET_ATS_InterfaceScanner | ||
142 | { | ||
143 | |||
144 | /** | ||
145 | * Head of LAN networks list. | ||
146 | */ | ||
147 | struct ATS_Network *net_head; | ||
148 | |||
149 | /** | ||
150 | * Tail of LAN networks list. | ||
151 | */ | ||
152 | struct ATS_Network *net_tail; | ||
153 | |||
154 | /** | ||
155 | * Task for periodically refreshing our LAN network list. | ||
156 | */ | ||
157 | struct GNUNET_SCHEDULER_Task *interface_task; | ||
158 | |||
159 | }; | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Delete all entries from the current network list. | ||
164 | * | ||
165 | * @param is scanner to clean up | ||
166 | */ | ||
167 | static void | ||
168 | delete_networks (struct GNUNET_ATS_InterfaceScanner *is) | ||
169 | { | ||
170 | struct ATS_Network *cur; | ||
171 | |||
172 | while (NULL != (cur = is->net_head)) | ||
173 | { | ||
174 | GNUNET_CONTAINER_DLL_remove (is->net_head, | ||
175 | is->net_tail, | ||
176 | cur); | ||
177 | GNUNET_free (cur); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Function invoked for each interface found. Adds the interface's | ||
184 | * network addresses to the respective DLL, so we can distinguish | ||
185 | * between LAN and WAN. | ||
186 | * | ||
187 | * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner` | ||
188 | * @param name name of the interface (can be NULL for unknown) | ||
189 | * @param isDefault is this presumably the default interface | ||
190 | * @param addr address of this interface (can be NULL for unknown or unassigned) | ||
191 | * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned) | ||
192 | * @param netmask the network mask (can be NULL for unknown or unassigned) | ||
193 | * @param addrlen length of the address | ||
194 | * @return #GNUNET_OK to continue iteration | ||
195 | */ | ||
196 | static int | ||
197 | interface_proc (void *cls, | ||
198 | const char *name, | ||
199 | int isDefault, | ||
200 | const struct sockaddr *addr, | ||
201 | const struct sockaddr *broadcast_addr, | ||
202 | const struct sockaddr *netmask, | ||
203 | socklen_t addrlen) | ||
204 | { | ||
205 | struct GNUNET_ATS_InterfaceScanner *is = cls; | ||
206 | /* Calculate network */ | ||
207 | struct ATS_Network *net = NULL; | ||
208 | |||
209 | /* Skipping IPv4 loopback addresses since we have special check */ | ||
210 | if (addr->sa_family == AF_INET) | ||
211 | { | ||
212 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
213 | |||
214 | if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000)) | ||
215 | return GNUNET_OK; | ||
216 | } | ||
217 | /* Skipping IPv6 loopback addresses since we have special check */ | ||
218 | if (addr->sa_family == AF_INET6) | ||
219 | { | ||
220 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
221 | if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) | ||
222 | return GNUNET_OK; | ||
223 | } | ||
224 | |||
225 | if (addr->sa_family == AF_INET) | ||
226 | { | ||
227 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr; | ||
228 | const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask; | ||
229 | struct sockaddr_in *tmp; | ||
230 | struct sockaddr_in network4; | ||
231 | |||
232 | net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in)); | ||
233 | tmp = (struct sockaddr_in *) &net[1]; | ||
234 | net->network = (struct sockaddr *) &tmp[0]; | ||
235 | net->netmask = (struct sockaddr *) &tmp[1]; | ||
236 | net->length = addrlen; | ||
237 | |||
238 | memset (&network4, 0, sizeof (network4)); | ||
239 | network4.sin_family = AF_INET; | ||
240 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
241 | network4.sin_len = sizeof (network4); | ||
242 | #endif | ||
243 | network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr); | ||
244 | |||
245 | memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in)); | ||
246 | memcpy (net->network, &network4, sizeof (struct sockaddr_in)); | ||
247 | } | ||
248 | |||
249 | if (addr->sa_family == AF_INET6) | ||
250 | { | ||
251 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr; | ||
252 | const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask; | ||
253 | struct sockaddr_in6 * tmp; | ||
254 | struct sockaddr_in6 network6; | ||
255 | |||
256 | net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6)); | ||
257 | tmp = (struct sockaddr_in6 *) &net[1]; | ||
258 | net->network = (struct sockaddr *) &tmp[0]; | ||
259 | net->netmask = (struct sockaddr *) &tmp[1]; | ||
260 | net->length = addrlen; | ||
261 | |||
262 | memset (&network6, 0, sizeof (network6)); | ||
263 | network6.sin6_family = AF_INET6; | ||
264 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
265 | network6.sin6_len = sizeof (network6); | ||
266 | #endif | ||
267 | unsigned int c = 0; | ||
268 | uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr; | ||
269 | uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr; | ||
270 | uint32_t *net_elem = (uint32_t *) &network6.sin6_addr; | ||
271 | for (c = 0; c < 4; c++) | ||
272 | net_elem[c] = addr_elem[c] & mask_elem[c]; | ||
273 | |||
274 | memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6)); | ||
275 | memcpy (net->network, &network6, sizeof (struct sockaddr_in6)); | ||
276 | } | ||
277 | if (NULL == net) | ||
278 | return GNUNET_OK; /* odd / unsupported address family */ | ||
279 | |||
280 | /* Store in list */ | ||
281 | #if VERBOSE_ATS | ||
282 | char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen)); | ||
283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Adding network `%s', netmask `%s'\n", | ||
285 | GNUNET_a2s ((struct sockaddr *) net->network, | ||
286 | addrlen), | ||
287 | netmask); | ||
288 | GNUNET_free (netmask); | ||
289 | #endif | ||
290 | GNUNET_CONTAINER_DLL_insert (is->net_head, | ||
291 | is->net_tail, | ||
292 | net); | ||
293 | |||
294 | return GNUNET_OK; | ||
295 | } | ||
296 | |||
297 | |||
298 | /** | ||
299 | * Periodically get list of network addresses from our interfaces. | ||
300 | * | ||
301 | * @param cls closure | ||
302 | * @param tc Task context | ||
303 | */ | ||
304 | static void | ||
305 | get_addresses (void *cls, | ||
306 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
307 | { | ||
308 | struct GNUNET_ATS_InterfaceScanner *is = cls; | ||
309 | |||
310 | is->interface_task = NULL; | ||
311 | delete_networks (is); | ||
312 | GNUNET_OS_network_interfaces_list (&interface_proc, | ||
313 | is); | ||
314 | is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL, | ||
315 | &get_addresses, | ||
316 | is); | ||
317 | } | ||
318 | |||
319 | |||
320 | /** | ||
321 | * Returns where the address is located: LAN or WAN or ... | ||
322 | * | ||
323 | * @param is the interface scanner handle | ||
324 | * @param addr address | ||
325 | * @param addrlen address length | ||
326 | * @return type of the network the address belongs to | ||
327 | */ | ||
328 | enum GNUNET_ATS_Network_Type | ||
329 | GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is, | ||
330 | const struct sockaddr *addr, | ||
331 | socklen_t addrlen) | ||
332 | { | ||
333 | struct ATS_Network *cur = is->net_head; | ||
334 | enum GNUNET_ATS_NetworkType type = GNUNET_ATS_NET_UNSPECIFIED; | ||
335 | |||
336 | switch (addr->sa_family) | ||
337 | { | ||
338 | case AF_UNIX: | ||
339 | type = GNUNET_ATS_NET_LOOPBACK; | ||
340 | break; | ||
341 | case AF_INET: | ||
342 | { | ||
343 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
344 | |||
345 | if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000)) | ||
346 | type = GNUNET_ATS_NET_LOOPBACK; | ||
347 | break; | ||
348 | } | ||
349 | case AF_INET6: | ||
350 | { | ||
351 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
352 | |||
353 | if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) | ||
354 | type = GNUNET_ATS_NET_LOOPBACK; | ||
355 | break; | ||
356 | } | ||
357 | default: | ||
358 | GNUNET_break (0); | ||
359 | break; | ||
360 | } | ||
361 | |||
362 | /* Check local networks */ | ||
363 | while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type)) | ||
364 | { | ||
365 | if (addrlen != cur->length) | ||
366 | { | ||
367 | cur = cur->next; | ||
368 | continue; | ||
369 | } | ||
370 | if (addr->sa_family == AF_INET) | ||
371 | { | ||
372 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
373 | const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network; | ||
374 | const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask; | ||
375 | |||
376 | if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr) | ||
377 | type = GNUNET_ATS_NET_LAN; | ||
378 | } | ||
379 | if (addr->sa_family == AF_INET6) | ||
380 | { | ||
381 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
382 | const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network; | ||
383 | const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask; | ||
384 | |||
385 | int res = GNUNET_YES; | ||
386 | int c = 0; | ||
387 | uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr; | ||
388 | uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr; | ||
389 | uint32_t *net_elem = (uint32_t *) &net6->sin6_addr; | ||
390 | for (c = 0; c < 4; c++) | ||
391 | if ((addr_elem[c] & mask_elem[c]) != net_elem[c]) | ||
392 | res = GNUNET_NO; | ||
393 | |||
394 | if (res == GNUNET_YES) | ||
395 | type = GNUNET_ATS_NET_LAN; | ||
396 | } | ||
397 | cur = cur->next; | ||
398 | } | ||
399 | |||
400 | /* no local network found for this address, default: WAN */ | ||
401 | if (type == GNUNET_ATS_NET_UNSPECIFIED) | ||
402 | type = GNUNET_ATS_NET_WAN; | ||
403 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
404 | "ats-scheduling-api", | ||
405 | "`%s' is in network `%s'\n", | ||
406 | GNUNET_a2s (addr, | ||
407 | addrlen), | ||
408 | GNUNET_ATS_print_network_type (type)); | ||
409 | return type; | ||
410 | } | ||
411 | |||
412 | |||
413 | /** | ||
414 | * Initialize the interface scanner. | ||
415 | * | ||
416 | * @return interface scanner | ||
417 | */ | ||
418 | struct GNUNET_ATS_InterfaceScanner * | ||
419 | GNUNET_ATS_scanner_init () | ||
420 | { | ||
421 | struct GNUNET_ATS_InterfaceScanner *is; | ||
422 | |||
423 | is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner); | ||
424 | GNUNET_OS_network_interfaces_list (&interface_proc, | ||
425 | is); | ||
426 | is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL, | ||
427 | &get_addresses, | ||
428 | is); | ||
429 | return is; | ||
430 | } | ||
431 | |||
432 | |||
433 | /** | ||
434 | * Client is done with the interface scanner, release resources. | ||
435 | * | ||
436 | * @param is handle to release | ||
437 | */ | ||
438 | void | ||
439 | GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is) | ||
440 | { | ||
441 | if (NULL != is->interface_task) | ||
442 | { | ||
443 | GNUNET_SCHEDULER_cancel (is->interface_task); | ||
444 | is->interface_task = NULL; | ||
445 | } | ||
446 | delete_networks (is); | ||
447 | GNUNET_free (is); | ||
448 | } | ||
449 | |||
450 | |||
451 | /* end of ats_api_scanner.c */ | ||
diff --git a/src/ats/ats_api_scheduling.c b/src/ats/ats_api_scheduling.c index 954396b66..f81dfe7eb 100644 --- a/src/ats/ats_api_scheduling.c +++ b/src/ats/ats_api_scheduling.c | |||
@@ -203,6 +203,10 @@ force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh) | |||
203 | GNUNET_CLIENT_disconnect (sh->client); | 203 | GNUNET_CLIENT_disconnect (sh->client); |
204 | sh->client = NULL; | 204 | sh->client = NULL; |
205 | } | 205 | } |
206 | sh->suggest_cb (sh->suggest_cb_cls, | ||
207 | NULL, NULL, NULL, | ||
208 | GNUNET_BANDWIDTH_ZERO, | ||
209 | GNUNET_BANDWIDTH_ZERO); | ||
206 | sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | 210 | sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, |
207 | &reconnect_task, | 211 | &reconnect_task, |
208 | sh); | 212 | sh); |