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, 10)
39 #define STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40 #define REPEATED_STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
41 #define STATS_VALUES 4
52 static int have_https;
55 static struct GNUNET_TRANSPORT_Handle *th;
56 static struct GNUNET_CORE_Handle *ch;
57 static struct GNUNET_PeerIdentity my_peer_id;
58 static const struct GNUNET_CONFIGURATION_Handle *mycfg;
59 static struct GNUNET_STATISTICS_Handle *stats;
62 static unsigned int transport_connections;
63 static unsigned int core_connections;
65 static GNUNET_SCHEDULER_TaskIdentifier check_task;
66 static GNUNET_SCHEDULER_TaskIdentifier statistics_task;
68 static uint64_t statistics_transport_connections;
69 static uint64_t statistics_transport_tcp_connections;
70 static uint64_t statistics_core_neighbour_entries;
71 static uint64_t statistics_core_entries_session_map;
73 int stat_check_running;
75 static struct GNUNET_CONTAINER_MultiHashMap *peers;
79 struct GNUNET_PeerIdentity id;
80 int transport_connected;
82 struct GNUNET_TRANSPORT_TransmitHandle *th_ping;
83 struct GNUNET_CORE_TransmitHandle *ch_ping;
85 struct GNUNET_TRANSPORT_TransmitHandle *th_pong;
86 struct GNUNET_CORE_TransmitHandle *ch_pong;
97 struct TransportPlugin
100 * This is a doubly-linked list.
102 struct TransportPlugin *next;
105 * This is a doubly-linked list.
107 struct TransportPlugin *prev;
110 * Short name for the plugin (i.e. "tcp").
119 struct TransportPlugin *phead;
120 struct TransportPlugin *ptail;
123 map_check_it (void *cls,
124 const struct GNUNET_HashCode * key,
128 struct PeerContainer *pc = value;
129 if (pc->core_connected != pc->transport_connected)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 "Inconsistent peer `%s': TRANSPORT %s <-> CORE %s\n",
133 GNUNET_i2s (&pc->id),
134 (GNUNET_YES == pc->transport_connected) ? "YES" : "NO",
135 (GNUNET_YES == pc->core_connected) ? "YES" : "NO");
144 map_cleanup_it (void *cls,
145 const struct GNUNET_HashCode * key,
148 struct PeerContainer *pc = value;
149 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove(peers, key, value));
150 if (NULL != pc->th_ping)
152 GNUNET_TRANSPORT_notify_transmit_ready_cancel(pc->th_ping);
155 if (NULL != pc->th_pong)
157 GNUNET_TRANSPORT_notify_transmit_ready_cancel(pc->th_pong);
160 if (NULL != pc->ch_ping)
162 GNUNET_CORE_notify_transmit_ready_cancel (pc->ch_ping);
165 if (NULL != pc->ch_pong)
167 GNUNET_CORE_notify_transmit_ready_cancel(pc->ch_pong);
175 map_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_cleanup_it, NULL);
178 GNUNET_CONTAINER_multihashmap_destroy(peers);
182 map_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 check_task = GNUNET_SCHEDULER_NO_TASK;
186 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_check_it, &fail);
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 "Inconsistent peers after connection consistency check: %u\n", fail);
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192 "Inconsistent peers after connection consistency check: %u\n", fail);
197 GNUNET_SCHEDULER_add_now (cls, NULL);
203 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
206 check_lowlevel_connections (int port, int protocol)
233 /* Use netstat to get a numeric list of all connections on port 'port' in state 'ESTABLISHED' */
234 GNUNET_asprintf(&cmdline, "netstat -n %s | grep %u | grep ESTABLISHED", proto, port);
236 if (system ("netstat -n > /dev/null 2> /dev/null"))
237 if (system ("netstat -n > /dev/null 2> /dev/null") == 0)
238 f = popen (cmdline, "r");
242 f = popen (cmdline, "r");
245 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "ss");
246 GNUNET_free (cmdline);
251 while (NULL != fgets (line, sizeof (line), f))
254 //printf ("%s", line);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i TCP connections established with port %u\n",
262 GNUNET_free (cmdline);
268 static struct TransportPlugin *
269 find_plugin (char * name)
271 struct TransportPlugin *cur = NULL;
273 for (cur = phead; cur != NULL; cur = cur->next)
275 if (0 == strcmp(name, cur->short_name))
282 stats_check_cb (void *cls, const char *subsystem,
283 const char *name, uint64_t value,
294 if ((STATS_VALUES == counter) || ((GNUNET_NO == have_tcp) && (STATS_VALUES - 1 == counter)))
296 int fail = GNUNET_NO;
300 int low_level_connections_udp = check_lowlevel_connections (2086, udp);
302 if (transport_connections != core_connections)
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "%u transport notifications <-> %u core notifications\n",
306 transport_connections, core_connections);
310 if (transport_connections != statistics_transport_connections)
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 "%u transport notifications <-> %u in statistics (peers connected)\n",
314 transport_connections, statistics_transport_connections);
318 if (core_connections != statistics_core_entries_session_map)
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "%u core notifications <-> %u in statistics (entries session map)\n",
322 core_connections, statistics_core_entries_session_map);
326 if (core_connections != statistics_core_neighbour_entries)
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "%u core notifications <-> %u in statistics (neighbour entries allocated)\n",
330 core_connections, statistics_core_neighbour_entries);
334 if (GNUNET_NO == fail)
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
336 "Check successful : (%u transport / %u core) connections established\n", transport_connections, core_connections);
338 /* TCP plugin specific checks */
339 if (GNUNET_YES == have_tcp)
341 struct TransportPlugin * p = find_plugin ("tcp");
342 int low_level_connections_tcp = check_lowlevel_connections (p->port, p->protocol);
344 if (low_level_connections_tcp != -1)
346 if (statistics_transport_tcp_connections > low_level_connections_tcp)
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 "%u transport tcp sessions <-> %i established tcp connections\n",
350 statistics_transport_tcp_connections, low_level_connections_tcp);
353 else if (low_level_connections_tcp != -1)
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 "%u TCP connections, %u UDP connections \n",
357 low_level_connections_tcp, low_level_connections_udp);
360 if (transport_connections > statistics_transport_tcp_connections)
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
363 "%u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
364 transport_connections, statistics_transport_tcp_connections);
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 " %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
371 transport_connections, statistics_transport_tcp_connections);
375 if (GNUNET_SCHEDULER_NO_TASK == statistics_task)
376 statistics_task = GNUNET_SCHEDULER_add_delayed(REPEATED_STATS_DELAY, &stats_check, NULL);
378 stat_check_running = GNUNET_NO;
385 GNUNET_NETWORK_STRUCT_BEGIN
389 struct GNUNET_MessageHeader header;
396 struct GNUNET_MessageHeader header;
400 GNUNET_NETWORK_STRUCT_END
404 send_transport_ping_cb (void *cls, size_t size, void *buf)
406 struct PeerContainer * pc = cls;
408 size_t mlen = sizeof (struct PING);
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Sending transport ping to `%s'\n", GNUNET_i2s (&pc->id));
418 ping.header.size = htons (mlen);
419 ping.header.type = htons (1234);
420 ping.src = htons (0);
424 memcpy (buf, &ping, mlen);
428 size_t send_core_ping_cb (void *cls, size_t size, void *buf)
430 struct PeerContainer * pc = cls;
432 size_t mlen = sizeof (struct PING);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Sending core ping to `%s'\n", GNUNET_i2s (&pc->id));
442 ping.header.size = htons (mlen);
443 ping.header.type = htons (1234);
444 ping.src = htons (1);
448 memcpy (buf, &ping, mlen);
453 int map_ping_it (void *cls,
454 const struct GNUNET_HashCode * key,
457 struct PeerContainer *pc = value;
459 if (ping == GNUNET_YES)
461 if ((GNUNET_YES == pc->transport_connected) && (NULL == pc->th_ping))
462 pc->th_ping = GNUNET_TRANSPORT_notify_transmit_ready(th, &pc->id,
463 sizeof (struct PING), UINT_MAX,
464 GNUNET_TIME_UNIT_FOREVER_REL, &send_transport_ping_cb, pc);
468 if ((GNUNET_YES == pc->core_connected) && (NULL == pc->ch_ping))
469 pc->ch_ping = GNUNET_CORE_notify_transmit_ready(ch,
471 GNUNET_TIME_UNIT_FOREVER_REL,
473 sizeof (struct PING),
474 send_core_ping_cb, pc);
483 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
485 statistics_task = GNUNET_SCHEDULER_NO_TASK;
487 if (GNUNET_YES == stat_check_running)
489 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
492 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_ping_it, NULL);
494 stat_check_running = GNUNET_YES;
496 statistics_transport_connections = 0 ;
497 statistics_core_entries_session_map = 0;
498 statistics_core_neighbour_entries = 0;
500 GNUNET_STATISTICS_get (stats, "transport", "# peers connected", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_connections);
501 GNUNET_STATISTICS_get (stats, "core", "# neighbour entries allocated", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_neighbour_entries);
502 GNUNET_STATISTICS_get (stats, "core", "# peers connected", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_entries_session_map);
504 /* TCP plugin specific checks */
505 if (GNUNET_YES == have_tcp)
506 GNUNET_STATISTICS_get (stats, "transport", "# TCP sessions active", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_tcp_connections);
511 size_t send_transport_pong_cb (void *cls, size_t size, void *buf)
513 struct PeerContainer * pc = cls;
515 size_t mlen = sizeof (struct PING);
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Sending transport pong to `%s'\n", GNUNET_i2s (&pc->id));
525 ping.header.size = htons (mlen);
526 ping.header.type = htons (4321);
527 ping.src = htons (0);
531 memcpy (buf, &ping, mlen);
536 send_core_pong_cb (void *cls, size_t size, void *buf)
538 struct PeerContainer * pc = cls;
540 size_t mlen = sizeof (struct PING);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Sending core pong to `%s'\n", GNUNET_i2s (&pc->id));
550 ping.header.size = htons (mlen);
551 ping.header.type = htons (4321);
552 ping.src = htons (1);
556 memcpy (buf, &ping, mlen);
562 map_connect (const struct GNUNET_PeerIdentity *peer, void * source)
564 struct PeerContainer * pc;
565 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
567 pc = GNUNET_malloc (sizeof (struct PeerContainer));
569 pc->core_connected = GNUNET_NO;
570 pc->transport_connected = GNUNET_NO;
571 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(peers, &peer->hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
574 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
577 if (GNUNET_NO == pc->transport_connected)
579 pc->transport_connected = GNUNET_YES;
580 if (GNUNET_YES == ping)
582 if (NULL == pc->th_ping)
583 pc->th_ping = GNUNET_TRANSPORT_notify_transmit_ready(th, peer, sizeof (struct PING), UINT_MAX, GNUNET_TIME_UNIT_FOREVER_REL, &send_transport_ping_cb, pc);
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 "%s notified multiple times about for peers `%s' (%s : %s)\n",
593 GNUNET_i2s (&pc->id),
594 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
600 if (GNUNET_NO == pc->core_connected)
602 pc->core_connected = GNUNET_YES;
603 if (GNUNET_YES == ping)
605 if (NULL == pc->ch_ping)
606 pc->ch_ping = GNUNET_CORE_notify_transmit_ready(ch,
608 GNUNET_TIME_UNIT_FOREVER_REL,
610 sizeof (struct PING),
611 send_core_ping_cb, pc);
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 "%s notified multiple times about for peers `%s' (%s : %s)\n",
621 GNUNET_i2s (&pc->id),
622 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
626 if (GNUNET_SCHEDULER_NO_TASK != check_task)
627 GNUNET_SCHEDULER_cancel(check_task);
628 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
630 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
631 GNUNET_SCHEDULER_cancel(statistics_task);
632 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
637 map_disconnect (const struct GNUNET_PeerIdentity * peer, void * source)
640 struct PeerContainer * pc;
641 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
646 "%s disconnect notification for unknown peer `%s'\n",
647 "TRANSPORT", GNUNET_i2s (peer));
653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654 "%s disconnect notification for unknown peer `%s'\n",
655 "CORE", GNUNET_i2s (peer));
660 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
663 if (NULL != pc->th_ping)
665 GNUNET_TRANSPORT_notify_transmit_ready_cancel(pc->th_ping);
668 if (NULL != pc->th_pong)
670 GNUNET_TRANSPORT_notify_transmit_ready_cancel(pc->th_pong);
674 if (GNUNET_YES == pc->transport_connected)
676 pc->transport_connected = GNUNET_NO;
680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
681 "%s notified for not connected peer `%s' (%s : %s)\n",
683 GNUNET_i2s (&pc->id),
684 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
690 if (NULL != pc->ch_ping)
692 GNUNET_CORE_notify_transmit_ready_cancel (pc->ch_ping);
695 if (NULL != pc->ch_pong)
697 GNUNET_CORE_notify_transmit_ready_cancel (pc->ch_pong);
701 if (GNUNET_YES == pc->core_connected)
703 pc->core_connected = GNUNET_NO;
707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
708 "%s notified for not connected peer `%s' (%s : %s)\n",
710 GNUNET_i2s (&pc->id),
711 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
716 if ((GNUNET_NO == pc->core_connected) && (GNUNET_NO == pc->transport_connected))
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing peer `%s'\n", GNUNET_i2s (&pc->id));
719 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey, pc));
725 if (GNUNET_SCHEDULER_NO_TASK != check_task)
726 GNUNET_SCHEDULER_cancel(check_task);
727 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
729 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
730 GNUNET_SCHEDULER_cancel(statistics_task);
731 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
736 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
738 struct TransportPlugin * cur = phead;
742 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from transport service\n");
743 GNUNET_TRANSPORT_disconnect (th);
750 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from core service\n");
751 GNUNET_CORE_disconnect (ch);
755 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
757 GNUNET_SCHEDULER_cancel(statistics_task);
758 statistics_task = GNUNET_SCHEDULER_NO_TASK;
761 if (GNUNET_SCHEDULER_NO_TASK != check_task)
763 GNUNET_SCHEDULER_cancel(check_task);
764 check_task = GNUNET_SCHEDULER_NO_TASK;
767 for (cur = phead; cur != NULL; cur = phead)
769 GNUNET_CONTAINER_DLL_remove(phead, ptail, cur);
770 GNUNET_free (cur->short_name);
774 check_task = GNUNET_SCHEDULER_add_now (&map_check, &map_cleanup);
778 transport_notify_connect_cb (void *cls,
779 const struct GNUNET_PeerIdentity
782 GNUNET_ATS_Information * ats,
785 transport_connections ++;
786 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT connect for peer `%s' (%u total)\n",
787 GNUNET_i2s (peer), transport_connections);
788 map_connect (peer, th);
792 * Function called to notify transport users that another
793 * peer disconnected from us.
796 * @param peer the peer that disconnected
799 transport_notify_disconnect_cb (void *cls,
801 GNUNET_PeerIdentity * peer)
803 GNUNET_assert (transport_connections > 0);
804 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT disconnect for peer `%s' (%u total)\n",
805 GNUNET_i2s (peer), transport_connections) ;
806 map_disconnect (peer, th);
807 transport_connections --;
812 transport_notify_receive_cb (void *cls,
814 GNUNET_PeerIdentity * peer,
816 GNUNET_MessageHeader *
819 GNUNET_ATS_Information * ats,
824 struct PeerContainer *pc = NULL;
826 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
834 if ((message->size == ntohs (sizeof (struct PING))) && (message->type == ntohs (1234)))
836 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s %s from peer `%s'\n",
840 if (GNUNET_YES == ping)
842 if (NULL == pc->th_pong)
843 pc->th_pong = GNUNET_TRANSPORT_notify_transmit_ready(th,
844 peer, sizeof (struct PONG),
845 UINT_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
846 &send_transport_pong_cb, pc);
852 if ((message->size == ntohs (sizeof (struct PONG))) && (message->type == ntohs (4321)))
854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received %s %s from peer `%s'\n",
862 core_notify_receive_cb (void *cls,
863 const struct GNUNET_PeerIdentity * peer,
864 const struct GNUNET_MessageHeader * message,
865 const struct GNUNET_ATS_Information* atsi,
866 unsigned int atsi_count)
868 struct PeerContainer *pc = NULL;
870 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
874 if (0 == memcmp (peer, &my_peer_id, sizeof (my_peer_id)))
877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received unexpected message type %u from unknown peer `%s'\n",
878 ntohs (message->type),
885 if ((message->size == ntohs (sizeof (struct PING))) && (message->type == ntohs (1234)))
887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received %s %s from peer `%s'\n",
891 if (GNUNET_YES == ping)
893 if (NULL == pc->ch_pong)
894 pc->ch_pong = GNUNET_CORE_notify_transmit_ready(ch,
896 GNUNET_TIME_UNIT_FOREVER_REL,
898 sizeof (struct PONG),
899 send_core_pong_cb, pc);
905 if ((message->size == ntohs (sizeof (struct PONG))) && (message->type == ntohs (4321)))
907 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received %s %s from peer `%s'\n",
918 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
919 const struct GNUNET_ATS_Information *atsi,
920 unsigned int atsi_count)
922 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
925 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for peer `%s' (%u total)\n",
926 GNUNET_i2s (peer), core_connections);
927 map_connect (peer, ch);
931 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for myself `%s' (%u total)\n",
932 GNUNET_i2s (peer), core_connections);
937 core_disconnect_cb (void *cls,
939 GNUNET_PeerIdentity * peer)
941 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
943 GNUNET_assert (core_connections > 0);
944 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for peer `%s' (%u total)\n",
945 GNUNET_i2s (peer), core_connections);
946 map_disconnect (peer, ch);
951 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for myself `%s' (%u total)\n",
952 GNUNET_i2s (peer), core_connections);
958 core_init_cb (void *cls, struct GNUNET_CORE_Handle *server,
959 const struct GNUNET_PeerIdentity *my_identity)
961 my_peer_id = *my_identity;
962 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to core service\n");
969 struct TransportPlugin * cur;
974 unsigned long long port;
976 have_tcp = GNUNET_NO;
977 have_udp = GNUNET_NO;
978 have_http = GNUNET_NO;
979 have_https = GNUNET_NO;
980 have_unix = GNUNET_NO;
982 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (mycfg, "TRANSPORT", "PLUGINS", &plugs))
985 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
989 GNUNET_asprintf(&secname, "transport-%s", pos);
991 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (mycfg, secname, "PORT", &port))
993 GNUNET_free (secname);
997 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin: `%s' port %llu\n"), pos, port);
998 cur = GNUNET_malloc(sizeof (struct TransportPlugin));
999 cur->short_name = GNUNET_strdup (pos);
1001 if (0 == strcmp("tcp", pos))
1003 have_tcp = GNUNET_YES;
1004 cur->protocol = tcp;
1006 if (0 == strcmp("udp", pos))
1008 have_udp = GNUNET_YES;
1009 cur->protocol = udp;
1011 if (0 == strcmp("http", pos))
1013 have_http = GNUNET_YES;
1014 cur->protocol = tcp;
1016 if (0 == strcmp("https", pos))
1018 have_https = GNUNET_YES;
1019 cur->protocol = tcp;
1021 if (0 == strcmp("unix", pos))
1023 have_unix = GNUNET_YES;
1024 cur->protocol = unixdomain;
1027 GNUNET_CONTAINER_DLL_insert(phead, ptail, cur);
1028 GNUNET_free (secname);
1030 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Found %u transport plugins: `%s'\n"),
1033 GNUNET_free (plugs);
1037 * Main function that will be run by the scheduler.
1039 * @param cls closure
1040 * @param args remaining command-line arguments
1041 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1042 * @param cfg configuration
1045 run (void *cls, char *const *args, const char *cfgfile,
1046 const struct GNUNET_CONFIGURATION_Handle *cfg)
1048 transport_connections = 0;
1049 core_connections = 0;
1054 stats = GNUNET_STATISTICS_create ("watchdog", cfg);
1055 peers = GNUNET_CONTAINER_multihashmap_create (20);
1057 th = GNUNET_TRANSPORT_connect(cfg, NULL, NULL,
1058 &transport_notify_receive_cb,
1059 &transport_notify_connect_cb,
1060 &transport_notify_disconnect_cb);
1061 GNUNET_assert (th != NULL);
1062 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to transport service\n");
1063 ch = GNUNET_CORE_connect (cfg, NULL,
1066 &core_disconnect_cb,
1067 &core_notify_receive_cb, GNUNET_NO,
1070 GNUNET_assert (ch != NULL);
1072 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, NULL);
1078 * The main function.
1080 * @param argc number of arguments from the command line
1081 * @param argv command line arguments
1082 * @return 0 ok, 1 on error
1085 main (int argc, char *const *argv)
1088 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1089 {'p', "ping", NULL, gettext_noop ("Send ping messages to test connectivity (default == NO)"),
1090 GNUNET_NO, &GNUNET_GETOPT_set_one, &ping},
1091 GNUNET_GETOPT_OPTION_END
1094 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1097 return (GNUNET_OK ==
1098 GNUNET_PROGRAM_run (argc, argv, "cn",
1099 gettext_noop ("help text"), options, &run,
1103 /* end of connection_watchdog.c */