|
19 | 19 | * *
|
20 | 20 | \********************************************************************/
|
21 | 21 |
|
22 |
| -/* |
23 |
| - * $Id$ |
24 |
| - */ |
25 | 22 | /**
|
26 | 23 | @file util.c
|
27 | 24 | @brief Misc utility functions
|
|
38 | 35 | #include <pthread.h>
|
39 | 36 | #include <sys/wait.h>
|
40 | 37 | #include <sys/types.h>
|
| 38 | +#include <sys/time.h> |
41 | 39 | #include <sys/unistd.h>
|
42 | 40 | #include <netinet/in.h>
|
43 | 41 | #include <sys/ioctl.h>
|
44 | 42 | #include <arpa/inet.h>
|
45 | 43 |
|
46 |
| -#include <netinet/in.h> |
47 | 44 | #include <net/if.h>
|
48 | 45 |
|
49 | 46 | #include <fcntl.h>
|
|
53 | 50 | #include <netpacket/packet.h>
|
54 | 51 |
|
55 | 52 | #include <string.h>
|
56 |
| -#include <pthread.h> |
57 | 53 | #include <netdb.h>
|
58 | 54 |
|
59 | 55 | #include "common.h"
|
60 |
| -#include "client_list.h" |
61 | 56 | #include "safe.h"
|
62 | 57 | #include "util.h"
|
63 |
| -#include "conf.h" |
64 | 58 | #include "debug.h"
|
65 | 59 | #include "pstring.h"
|
66 |
| -#include "gateway.h" |
67 |
| -#include "commandline.h" |
68 | 60 |
|
69 | 61 | #include "../config.h"
|
70 | 62 |
|
| 63 | +#define LOCK_GHBN() do { \ |
| 64 | + debug(LOG_DEBUG, "Locking wd_gethostbyname()"); \ |
| 65 | + pthread_mutex_lock(&ghbn_mutex); \ |
| 66 | + debug(LOG_DEBUG, "wd_gethostbyname() locked"); \ |
| 67 | +} while (0) |
| 68 | + |
| 69 | +#define UNLOCK_GHBN() do { \ |
| 70 | + debug(LOG_DEBUG, "Unlocking wd_gethostbyname()"); \ |
| 71 | + pthread_mutex_unlock(&ghbn_mutex); \ |
| 72 | + debug(LOG_DEBUG, "wd_gethostbyname() unlocked"); \ |
| 73 | +} while (0) |
| 74 | + |
71 | 75 | /** @brief FD for icmp raw socket */
|
72 | 76 | static int icmp_fd;
|
73 | 77 |
|
74 | 78 | /** @brief Mutex to protect gethostbyname since not reentrant */
|
75 | 79 | static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
|
76 | 80 |
|
77 |
| -/* XXX Do these need to be locked ? */ |
78 |
| -static time_t last_online_time = 0; |
79 |
| -static time_t last_offline_time = 0; |
80 |
| -static time_t last_auth_online_time = 0; |
81 |
| -static time_t last_auth_offline_time = 0; |
82 |
| - |
83 |
| -long served_this_session = 0; |
84 |
| - |
85 | 81 | static unsigned short rand16(void);
|
86 | 82 |
|
87 | 83 | /** Fork a child and execute a shell command, the parent
|
@@ -153,8 +149,6 @@ wd_gethostbyname(const char *name)
|
153 | 149 | return NULL;
|
154 | 150 | }
|
155 | 151 |
|
156 |
| - mark_online(); |
157 |
| - |
158 | 152 | in_addr_temp = (struct in_addr *)he->h_addr_list[0];
|
159 | 153 | addr->s_addr = in_addr_temp->s_addr;
|
160 | 154 |
|
@@ -285,190 +279,6 @@ get_ext_iface(void)
|
285 | 279 | return NULL;
|
286 | 280 | }
|
287 | 281 |
|
288 |
| -void |
289 |
| -mark_online() |
290 |
| -{ |
291 |
| - int before; |
292 |
| - int after; |
293 |
| - |
294 |
| - before = is_online(); |
295 |
| - time(&last_online_time); |
296 |
| - after = is_online(); /* XXX is_online() looks at last_online_time... */ |
297 |
| - |
298 |
| - if (before != after) { |
299 |
| - debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF")); |
300 |
| - } |
301 |
| - |
302 |
| -} |
303 |
| - |
304 |
| -void |
305 |
| -mark_offline() |
306 |
| -{ |
307 |
| - int before; |
308 |
| - int after; |
309 |
| - |
310 |
| - before = is_online(); |
311 |
| - time(&last_offline_time); |
312 |
| - after = is_online(); |
313 |
| - |
314 |
| - if (before != after) { |
315 |
| - debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF")); |
316 |
| - } |
317 |
| - |
318 |
| - /* If we're offline it definately means the auth server is offline */ |
319 |
| - mark_auth_offline(); |
320 |
| - |
321 |
| -} |
322 |
| - |
323 |
| -int |
324 |
| -is_online() |
325 |
| -{ |
326 |
| - if (last_online_time == 0 || (last_offline_time - last_online_time) >= (config_get_config()->checkinterval * 2)) { |
327 |
| - /* We're probably offline */ |
328 |
| - return (0); |
329 |
| - } else { |
330 |
| - /* We're probably online */ |
331 |
| - return (1); |
332 |
| - } |
333 |
| -} |
334 |
| - |
335 |
| -void |
336 |
| -mark_auth_online() |
337 |
| -{ |
338 |
| - int before; |
339 |
| - int after; |
340 |
| - |
341 |
| - before = is_auth_online(); |
342 |
| - time(&last_auth_online_time); |
343 |
| - after = is_auth_online(); |
344 |
| - |
345 |
| - if (before != after) { |
346 |
| - debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF")); |
347 |
| - } |
348 |
| - |
349 |
| - /* If auth server is online it means we're definately online */ |
350 |
| - mark_online(); |
351 |
| - |
352 |
| -} |
353 |
| - |
354 |
| -void |
355 |
| -mark_auth_offline() |
356 |
| -{ |
357 |
| - int before; |
358 |
| - int after; |
359 |
| - |
360 |
| - before = is_auth_online(); |
361 |
| - time(&last_auth_offline_time); |
362 |
| - after = is_auth_online(); |
363 |
| - |
364 |
| - if (before != after) { |
365 |
| - debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF")); |
366 |
| - } |
367 |
| - |
368 |
| -} |
369 |
| - |
370 |
| -int |
371 |
| -is_auth_online() |
372 |
| -{ |
373 |
| - if (!is_online()) { |
374 |
| - /* If we're not online auth is definately not online :) */ |
375 |
| - return (0); |
376 |
| - } else if (last_auth_online_time == 0 |
377 |
| - || (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2)) { |
378 |
| - /* Auth is probably offline */ |
379 |
| - return (0); |
380 |
| - } else { |
381 |
| - /* Auth is probably online */ |
382 |
| - return (1); |
383 |
| - } |
384 |
| -} |
385 |
| - |
386 |
| - /* |
387 |
| - * @return A string containing human-readable status text. MUST BE free()d by caller |
388 |
| - */ |
389 |
| -char * |
390 |
| -get_status_text() |
391 |
| -{ |
392 |
| - pstr_t *pstr = pstr_new(); |
393 |
| - s_config *config; |
394 |
| - t_auth_serv *auth_server; |
395 |
| - t_client *sublist, *current; |
396 |
| - int count; |
397 |
| - time_t uptime = 0; |
398 |
| - unsigned int days = 0, hours = 0, minutes = 0, seconds = 0; |
399 |
| - t_trusted_mac *p; |
400 |
| - |
401 |
| - pstr_cat(pstr, "WiFiDog status\n\n"); |
402 |
| - |
403 |
| - uptime = time(NULL) - started_time; |
404 |
| - days = (unsigned int)uptime / (24 * 60 * 60); |
405 |
| - uptime -= days * (24 * 60 * 60); |
406 |
| - hours = (unsigned int)uptime / (60 * 60); |
407 |
| - uptime -= hours * (60 * 60); |
408 |
| - minutes = (unsigned int)uptime / 60; |
409 |
| - uptime -= minutes * 60; |
410 |
| - seconds = (unsigned int)uptime; |
411 |
| - |
412 |
| - pstr_cat(pstr, "Version: " VERSION "\n"); |
413 |
| - pstr_append_sprintf(pstr, "Uptime: %ud %uh %um %us\n", days, hours, minutes, seconds); |
414 |
| - pstr_cat(pstr, "Has been restarted: "); |
415 |
| - |
416 |
| - if (restart_orig_pid) { |
417 |
| - pstr_append_sprintf(pstr, "yes (from PID %d)\n", restart_orig_pid); |
418 |
| - } else { |
419 |
| - pstr_cat(pstr, "no\n"); |
420 |
| - } |
421 |
| - |
422 |
| - pstr_append_sprintf(pstr, "Internet Connectivity: %s\n", (is_online()? "yes" : "no")); |
423 |
| - pstr_append_sprintf(pstr, "Auth server reachable: %s\n", (is_auth_online()? "yes" : "no")); |
424 |
| - pstr_append_sprintf(pstr, "Clients served this session: %lu\n\n", served_this_session); |
425 |
| - |
426 |
| - LOCK_CLIENT_LIST(); |
427 |
| - |
428 |
| - count = client_list_dup(&sublist); |
429 |
| - |
430 |
| - UNLOCK_CLIENT_LIST(); |
431 |
| - |
432 |
| - current = sublist; |
433 |
| - |
434 |
| - pstr_append_sprintf(pstr, "%d clients " "connected.\n", count); |
435 |
| - |
436 |
| - count = 1; |
437 |
| - while (current != NULL) { |
438 |
| - pstr_append_sprintf(pstr, "\nClient %d\n", count); |
439 |
| - pstr_append_sprintf(pstr, " IP: %s MAC: %s\n", current->ip, current->mac); |
440 |
| - pstr_append_sprintf(pstr, " Token: %s\n", current->token); |
441 |
| - pstr_append_sprintf(pstr, " Downloaded: %llu\n Uploaded: %llu\n", current->counters.incoming, |
442 |
| - current->counters.outgoing); |
443 |
| - count++; |
444 |
| - current = current->next; |
445 |
| - } |
446 |
| - |
447 |
| - client_list_destroy(sublist); |
448 |
| - |
449 |
| - config = config_get_config(); |
450 |
| - |
451 |
| - if (config->trustedmaclist != NULL) { |
452 |
| - pstr_cat(pstr, "\nTrusted MAC addresses:\n"); |
453 |
| - |
454 |
| - for (p = config->trustedmaclist; p != NULL; p = p->next) { |
455 |
| - pstr_append_sprintf(pstr, " %s\n", p->mac); |
456 |
| - } |
457 |
| - } |
458 |
| - |
459 |
| - pstr_cat(pstr, "\nAuthentication servers:\n"); |
460 |
| - |
461 |
| - LOCK_CONFIG(); |
462 |
| - |
463 |
| - for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) { |
464 |
| - pstr_append_sprintf(pstr, " Host: %s (%s)\n", auth_server->authserv_hostname, auth_server->last_ip); |
465 |
| - } |
466 |
| - |
467 |
| - UNLOCK_CONFIG(); |
468 |
| - |
469 |
| - return pstr_to_string(pstr); |
470 |
| -} |
471 |
| - |
472 | 282 | /** Initialize the ICMP socket
|
473 | 283 | * @return A boolean of the success
|
474 | 284 | */
|
|
0 commit comments