2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file integration-tests/connection_watchdog.c
22 * @brief tool to monitor core and transport connections for consistency
23 * @author Matthias Wachs
26 #include "gnunet_common.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_statistics_service.h"
38 #define CHECK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 #define STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 #define REPEATED_STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
41 #define STATS_VALUES 4
51 static int have_https;
54 static struct GNUNET_TRANSPORT_Handle *th;
55 static struct GNUNET_CORE_Handle *ch;
56 static struct GNUNET_PeerIdentity my_peer_id;
57 static const struct GNUNET_CONFIGURATION_Handle *mycfg;
58 static struct GNUNET_STATISTICS_Handle *stats;
61 static unsigned int transport_connections;
62 static unsigned int core_connections;
64 static GNUNET_SCHEDULER_TaskIdentifier check_task;
65 static GNUNET_SCHEDULER_TaskIdentifier statistics_task;
67 static uint64_t statistics_transport_connections;
68 static uint64_t statistics_transport_tcp_connections;
69 static uint64_t statistics_core_neighbour_entries;
70 static uint64_t statistics_core_entries_session_map;
72 int stat_check_running;
74 static struct GNUNET_CONTAINER_MultiHashMap *peers;
78 struct GNUNET_PeerIdentity id;
79 int transport_connected;
91 struct TransportPlugin
94 * This is a doubly-linked list.
96 struct TransportPlugin *next;
99 * This is a doubly-linked list.
101 struct TransportPlugin *prev;
104 * Short name for the plugin (i.e. "tcp").
113 struct TransportPlugin *phead;
114 struct TransportPlugin *ptail;
116 int map_check_it (void *cls,
117 const GNUNET_HashCode * key,
121 struct PeerContainer *pc = value;
122 if (pc->core_connected != pc->transport_connected)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125 "Inconsistend peer `%s': TRANSPORT %s <-> CORE %s\n",
126 GNUNET_i2s (&pc->id),
127 (GNUNET_YES == pc->transport_connected) ? "YES" : "NO",
128 (GNUNET_YES == pc->core_connected) ? "YES" : "NO");
136 int map_cleanup_it (void *cls,
137 const GNUNET_HashCode * key,
140 struct PeerContainer *pc = value;
141 GNUNET_CONTAINER_multihashmap_remove(peers, key, value);
147 map_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_cleanup_it, NULL);
150 GNUNET_CONTAINER_multihashmap_destroy(peers);
154 map_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 check_task = GNUNET_SCHEDULER_NO_TASK;
158 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_check_it, &fail);
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 "Inconsistent peers after connection consistency check: %u\n", fail);
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164 "Inconsistent peers after connection consistency check: %u\n", fail);
169 GNUNET_SCHEDULER_add_now (cls, NULL);
175 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
178 check_lowlevel_connections (int port, int protocol)
206 GNUNET_asprintf(&cmdline, "netstat -n %s | grep %u", proto, port);
208 if (system ("netstat -n > /dev/null 2> /dev/null"))
209 if (system ("netstat -n > /dev/null 2> /dev/null") == 0)
210 f = popen (cmdline, "r");
214 f = popen (cmdline, "r");
217 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "ss");
218 GNUNET_free (cmdline);
223 while (NULL != fgets (line, sizeof (line), f))
226 //printf ("%s", line);
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i TCP connections established with port %u\n",
234 GNUNET_free (cmdline);
240 static struct TransportPlugin *
241 find_plugin (char * name)
243 struct TransportPlugin *cur = NULL;
245 for (cur = phead; cur != NULL; cur = phead)
247 if (0 == strcmp(name, cur->short_name))
253 int stats_check_cb (void *cls, const char *subsystem,
254 const char *name, uint64_t value,
265 if ((STATS_VALUES == counter) || ((GNUNET_NO == have_tcp) && (STATS_VALUES - 1 == counter)))
267 int fail = GNUNET_NO;
271 int low_level_connections_udp = check_lowlevel_connections (2086, udp);
273 if (transport_connections != core_connections)
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 "%u transport notifications <-> %u core notifications\n",
277 transport_connections, core_connections);
281 if (transport_connections != statistics_transport_connections)
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "%u transport notifications <-> %u in statistics (peers connected)\n",
285 transport_connections, statistics_transport_connections);
289 if (core_connections != statistics_core_entries_session_map)
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "%u core notifications <-> %u in statistics (entries session map)\n",
293 core_connections, statistics_core_entries_session_map);
297 if (core_connections != statistics_core_neighbour_entries)
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 "%u core notifications <-> %u in statistics (neighbour entries allocated)\n",
301 core_connections, statistics_core_neighbour_entries);
305 if (GNUNET_NO == fail)
306 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
307 "Check successful : (%u transport / %u core) connections established\n", transport_connections, core_connections);
309 /* TCP plugin specific checks */
310 if (GNUNET_YES == have_tcp)
312 struct TransportPlugin * p = find_plugin ("tcp");
313 int low_level_connections_tcp = check_lowlevel_connections (p->port, p->protocol);
315 if (low_level_connections_tcp != -1)
317 if (statistics_transport_tcp_connections > low_level_connections_tcp)
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "%u transport tcp sessions <-> %i established tcp connections\n",
321 statistics_transport_tcp_connections, low_level_connections_tcp);
324 else if (low_level_connections_tcp != -1)
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
327 "%u TCP connections, %u UDP connections \n",
328 low_level_connections_tcp, low_level_connections_udp);
331 if (transport_connections > statistics_transport_tcp_connections)
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
334 "%u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
335 transport_connections, statistics_transport_tcp_connections);
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 " %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
342 transport_connections, statistics_transport_tcp_connections);
346 if (GNUNET_SCHEDULER_NO_TASK == statistics_task)
347 statistics_task = GNUNET_SCHEDULER_add_delayed(REPEATED_STATS_DELAY, &stats_check, NULL);
349 stat_check_running = GNUNET_NO;
358 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
360 statistics_task = GNUNET_SCHEDULER_NO_TASK;
362 if (GNUNET_YES == stat_check_running)
364 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
367 stat_check_running = GNUNET_YES;
369 statistics_transport_connections = 0 ;
370 statistics_core_entries_session_map = 0;
371 statistics_core_neighbour_entries = 0;
373 GNUNET_STATISTICS_get (stats, "transport", "# peers connected", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_connections);
374 GNUNET_STATISTICS_get (stats, "core", "# neighbour entries allocated", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_neighbour_entries);
375 GNUNET_STATISTICS_get (stats, "core", "# entries in session map", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_entries_session_map);
377 /* TCP plugin specific checks */
378 if (GNUNET_YES == have_tcp)
379 GNUNET_STATISTICS_get (stats, "transport", "# TCP sessions active", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_tcp_connections);
386 map_connect (const struct GNUNET_PeerIdentity *peer, void * source)
388 struct PeerContainer * pc;
389 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
391 pc = GNUNET_malloc (sizeof (struct PeerContainer));
393 pc->core_connected = GNUNET_NO;
394 pc->transport_connected = GNUNET_NO;
395 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(peers, &peer->hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
398 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
401 if (GNUNET_NO == pc->transport_connected)
403 pc->transport_connected = GNUNET_YES;
407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
408 "%s notified multiple times about for peers `%s' (%s : %s)\n",
410 GNUNET_i2s (&pc->id),
411 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
417 if (GNUNET_NO == pc->core_connected)
419 pc->core_connected = GNUNET_YES;
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
424 "%s notified multiple times about for peers `%s' (%s : %s)\n",
426 GNUNET_i2s (&pc->id),
427 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
431 if (GNUNET_SCHEDULER_NO_TASK != check_task)
432 GNUNET_SCHEDULER_cancel(check_task);
433 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
435 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
436 GNUNET_SCHEDULER_cancel(statistics_task);
437 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
442 map_disconnect (const struct GNUNET_PeerIdentity * peer, void * source)
445 struct PeerContainer * pc;
446 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451 "%s disconnect notification for unknown peer `%s'\n",
452 "TRANSPORT", GNUNET_i2s (peer));
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 "%s disconnect notification for unknown peer `%s'\n",
460 "CORE", GNUNET_i2s (peer));
465 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
468 if (GNUNET_YES == pc->transport_connected)
470 pc->transport_connected = GNUNET_NO;
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "%s notified for not connected peer `%s' (%s : %s)\n",
477 GNUNET_i2s (&pc->id),
478 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
484 if (GNUNET_YES == pc->core_connected)
486 pc->core_connected = GNUNET_NO;
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "%s notified for not connected peer `%s' (%s : %s)\n",
493 GNUNET_i2s (&pc->id),
494 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
499 if ((GNUNET_NO == pc->core_connected) && (GNUNET_NO == pc->transport_connected))
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing peer `%s'\n", GNUNET_i2s (&pc->id));
502 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey, pc));
506 if (GNUNET_SCHEDULER_NO_TASK != check_task)
507 GNUNET_SCHEDULER_cancel(check_task);
508 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
510 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
511 GNUNET_SCHEDULER_cancel(statistics_task);
512 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
517 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 struct TransportPlugin * cur = phead;
523 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from transport service\n");
524 GNUNET_TRANSPORT_disconnect (th);
529 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from core service\n");
530 GNUNET_CORE_disconnect (ch);
534 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
536 GNUNET_SCHEDULER_cancel(statistics_task);
537 statistics_task = GNUNET_SCHEDULER_NO_TASK;
540 if (GNUNET_SCHEDULER_NO_TASK != check_task)
542 GNUNET_SCHEDULER_cancel(check_task);
543 check_task = GNUNET_SCHEDULER_NO_TASK;
546 for (cur = phead; cur != NULL; cur = phead)
548 GNUNET_CONTAINER_DLL_remove(phead, ptail, cur);
549 GNUNET_free (cur->short_name);
553 check_task = GNUNET_SCHEDULER_add_now (&map_check, &map_cleanup);
557 transport_notify_connect_cb (void *cls,
558 const struct GNUNET_PeerIdentity
561 GNUNET_ATS_Information * ats,
564 transport_connections ++;
565 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT connect for peer `%s' (%u total)\n",
566 GNUNET_i2s (peer), transport_connections);
567 map_connect (peer, th);
571 * Function called to notify transport users that another
572 * peer disconnected from us.
575 * @param peer the peer that disconnected
578 transport_notify_disconnect_cb (void *cls,
580 GNUNET_PeerIdentity * peer)
582 GNUNET_assert (transport_connections > 0);
583 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT disconnect for peer `%s' (%u total)\n",
584 GNUNET_i2s (peer), transport_connections) ;
585 map_disconnect (peer, th);
586 transport_connections --;
592 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
593 const struct GNUNET_ATS_Information *atsi,
594 unsigned int atsi_count)
596 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
599 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for peer `%s' (%u total)\n",
600 GNUNET_i2s (peer), core_connections);
601 map_connect (peer, ch);
605 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for myself `%s' (%u total)\n",
606 GNUNET_i2s (peer), core_connections);
611 core_disconnect_cb (void *cls,
613 GNUNET_PeerIdentity * peer)
615 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
617 GNUNET_assert (core_connections >= 0);
618 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for peer `%s' (%u total)\n",
619 GNUNET_i2s (peer), core_connections);
620 map_disconnect (peer, ch);
625 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for myself `%s' (%u total)\n",
626 GNUNET_i2s (peer), core_connections);
632 core_init_cb (void *cls, struct GNUNET_CORE_Handle *server,
633 const struct GNUNET_PeerIdentity *my_identity)
635 my_peer_id = *my_identity;
636 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to core service\n");
643 struct TransportPlugin * cur;
648 long long unsigned int port;
650 have_tcp = GNUNET_NO;
651 have_udp = GNUNET_NO;
652 have_http = GNUNET_NO;
653 have_https = GNUNET_NO;
654 have_unix = GNUNET_NO;
656 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (mycfg, "TRANSPORT", "PLUGINS", &plugs))
659 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
663 GNUNET_asprintf(&secname, "transport-%s", pos);
665 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (mycfg, secname, "PORT", &port))
667 GNUNET_free (secname);
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin: `%s' port %llu\n"), pos, port);
672 cur = GNUNET_malloc(sizeof (struct TransportPlugin));
673 cur->short_name = strdup (pos);
675 if (0 == strcmp("tcp", pos))
677 have_tcp = GNUNET_YES;
680 if (0 == strcmp("udp", pos))
682 have_udp = GNUNET_YES;
685 if (0 == strcmp("http", pos))
687 have_http = GNUNET_YES;
690 if (0 == strcmp("https", pos))
692 have_https = GNUNET_YES;
695 if (0 == strcmp("unix", pos))
697 have_unix = GNUNET_YES;
698 cur->protocol = unixdomain;
701 GNUNET_CONTAINER_DLL_insert(phead, ptail, cur);
702 GNUNET_free (secname);
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Found %u transport plugins: `%s'\n"),
711 * Main function that will be run by the scheduler.
714 * @param args remaining command-line arguments
715 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
716 * @param cfg configuration
719 run (void *cls, char *const *args, const char *cfgfile,
720 const struct GNUNET_CONFIGURATION_Handle *cfg)
722 transport_connections = 0;
723 core_connections = 0;
728 stats = GNUNET_STATISTICS_create ("watchdog", cfg);
729 peers = GNUNET_CONTAINER_multihashmap_create (20);
731 th = GNUNET_TRANSPORT_connect(cfg, NULL, NULL, NULL,
732 &transport_notify_connect_cb,
733 &transport_notify_disconnect_cb);
734 GNUNET_assert (th != NULL);
735 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to transport service\n");
736 ch = GNUNET_CORE_connect (cfg, 1, NULL,
743 GNUNET_assert (ch != NULL);
745 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, NULL);
753 * @param argc number of arguments from the command line
754 * @param argv command line arguments
755 * @return 0 ok, 1 on error
758 main (int argc, char *const *argv)
760 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
761 /* FIMXE: add options here */
762 GNUNET_GETOPT_OPTION_END
765 GNUNET_PROGRAM_run (argc, argv, "cn",
766 gettext_noop ("help text"), options, &run,
770 /* end of connection_watchdog.c */