2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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.
22 * @file transport/transport_api_blacklisting.c
23 * @brief test for the blacklisting API
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_hello_lib.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 "transport.h"
36 #include "transport-testing.h"
38 #define VERBOSE GNUNET_YES
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
41 #define START_ARM GNUNET_YES
44 struct PeerContext *p1;
46 struct PeerContext *p2;
48 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
50 struct GNUNET_TRANSPORT_TransmitHandle *th;
52 struct GNUNET_TRANSPORT_TESTING_handle *tth;
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
60 * How long until we give up on transmitting the message?
62 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
71 struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
73 struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
75 static GNUNET_SCHEDULER_TaskIdentifier die_task;
77 static GNUNET_SCHEDULER_TaskIdentifier send_task;
80 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
82 #define OKPP do { ok++; } while (0)
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
91 if (send_task != GNUNET_SCHEDULER_NO_TASK)
92 GNUNET_SCHEDULER_cancel (send_task);
94 if (die_task != GNUNET_SCHEDULER_NO_TASK)
95 GNUNET_SCHEDULER_cancel (die_task);
98 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
101 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
103 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
105 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
106 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
112 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 if (send_task != GNUNET_SCHEDULER_NO_TASK)
115 GNUNET_SCHEDULER_cancel (send_task);
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
120 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
125 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
128 if (blacklist_p1 != NULL)
129 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
131 if (blacklist_p2 != NULL)
132 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
135 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
137 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
144 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
145 const struct GNUNET_MessageHeader *message,
146 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
148 struct PeerContext *p = cls;
149 struct PeerContext *t = NULL;
151 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
153 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
155 GNUNET_assert (t != NULL);
157 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
161 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
162 GNUNET_i2s (&t->id));
165 if ((MTYPE == ntohs (message->type)) &&
166 (MSIZE == ntohs (message->size)))
181 notify_ready (void *cls, size_t size, void *buf)
183 struct PeerContext *p = cls;
184 struct GNUNET_MessageHeader *hdr;
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Timeout occurred while waiting for transmit_ready\n");
192 if (GNUNET_SCHEDULER_NO_TASK != die_task)
193 GNUNET_SCHEDULER_cancel (die_task);
194 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
199 GNUNET_assert (size >= MSIZE);
204 hdr->size = htons (MSIZE);
205 hdr->type = htons (MTYPE);
208 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
211 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
212 GNUNET_i2s (&p->id));
219 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 send_task = GNUNET_SCHEDULER_NO_TASK;
223 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
225 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
229 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
230 GNUNET_free (receiver_s);
232 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE, 0,
233 TIMEOUT_TRANSMIT, ¬ify_ready,
238 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
239 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
244 struct PeerContext *p = cls;
245 struct PeerContext *t = NULL;
247 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
249 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
251 GNUNET_assert (t != NULL);
253 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
257 t->no, GNUNET_i2s (peer));
263 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
265 struct PeerContext *p = cls;
266 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
275 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
280 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
283 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
286 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
289 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
293 int blacklist_cb (void *cls,
295 GNUNET_PeerIdentity * pid)
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist request\n", GNUNET_i2s (pid));
302 start_cb (struct PeerContext *p, void *cls)
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
309 GNUNET_i2s (&p->id));
314 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
318 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
319 GNUNET_free (sender_c);
321 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
327 run (void *cls, char *const *args, const char *cfgfile,
328 const struct GNUNET_CONFIGURATION_Handle *cfg)
330 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
332 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
333 ¬ify_receive, ¬ify_connect,
334 ¬ify_disconnect, &start_cb,
337 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
338 ¬ify_receive, ¬ify_connect,
339 ¬ify_disconnect, &start_cb,
342 blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
346 blacklist_p2 = GNUNET_TRANSPORT_blacklist (p1->cfg,
350 GNUNET_assert (blacklist_p1 != NULL);
351 GNUNET_assert (blacklist_p2 != NULL);
358 static char *const argv[] = { "test-transport-api-blacklisting",
360 "test_transport_api_data.conf",
366 static struct GNUNET_GETOPT_CommandLineOption options[] = {
367 GNUNET_GETOPT_OPTION_END
371 setTransportOptions ("test_transport_api_data.conf");
375 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
376 "nohelp", options, &run, &ok);
382 main (int argc, char *argv[])
386 GNUNET_log_setup ("test-transport-api-blacklisting",
394 tth = GNUNET_TRANSPORT_TESTING_init ();
398 GNUNET_TRANSPORT_TESTING_done (tth);
403 /* end of transport_api_blacklisting.c */