fix crash on early abort due to dangling connect task
[oweals/gnunet.git] / src / cadet / test_cadet_local.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file cadet/test_cadet_local.c
23  * @brief test cadet local: test of cadet channels with just one peer
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
32
33 struct GNUNET_TESTING_Peer *me;
34
35 static struct GNUNET_CADET_Handle *cadet_peer_1;
36
37 static struct GNUNET_CADET_Handle *cadet_peer_2;
38
39 static struct GNUNET_CADET_Channel *ch;
40
41 static int result = GNUNET_OK;
42
43 static int got_data = GNUNET_NO;
44
45 static struct GNUNET_SCHEDULER_Task *abort_task;
46
47 static struct GNUNET_SCHEDULER_Task *connect_task;
48
49 static struct GNUNET_CADET_TransmitHandle *mth;
50
51
52 /**
53  * Connect to other client and send data
54  *
55  * @param cls Closue (unused).
56  */
57 static void
58 do_connect (void *cls);
59
60
61 /**
62  * Shutdown nicely
63  */
64 static void
65 do_shutdown (void *cls)
66 {
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
68   if (NULL != abort_task)
69   {
70     GNUNET_SCHEDULER_cancel (abort_task);
71     abort_task = NULL;
72   }
73   if (NULL != connect_task)
74   {
75     GNUNET_SCHEDULER_cancel (connect_task);
76     connect_task = NULL;
77   }
78   if (NULL != ch)
79   {
80     GNUNET_CADET_channel_destroy (ch);
81     ch = NULL;
82   }
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
84   if (NULL != cadet_peer_1)
85   {
86     GNUNET_CADET_disconnect (cadet_peer_1);
87     cadet_peer_1 = NULL;
88   }
89   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
90   if (NULL != cadet_peer_2)
91   {
92     GNUNET_CADET_disconnect (cadet_peer_2);
93     cadet_peer_2 = NULL;
94   }
95 }
96
97
98 /**
99  * Something went wrong and timed out. Kill everything and set error flag
100  */
101 static void
102 do_abort (void *cls)
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
105   result = GNUNET_SYSERR;
106   abort_task = NULL;
107   GNUNET_SCHEDULER_shutdown ();
108 }
109
110
111 /**
112  * Function is called whenever a message is received.
113  *
114  * @param cls closure (set from GNUNET_CADET_connect)
115  * @param channel connection to the other end
116  * @param channel_ctx place to store local state associated with the channel
117  * @param message the actual message
118  *
119  * @return GNUNET_OK to keep the connection open,
120  *         GNUNET_SYSERR to close it (signal serious error)
121  */
122 static int
123 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
124                void **channel_ctx,
125                const struct GNUNET_MessageHeader *message)
126 {
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Data callback! Shutting down.\n");
129   got_data = GNUNET_YES;
130   GNUNET_SCHEDULER_shutdown ();
131   GNUNET_CADET_receive_done (channel);
132   return GNUNET_OK;
133 }
134
135
136 /**
137  * Method called whenever another peer has added us to a channel
138  * the other peer initiated.
139  *
140  * @param cls closure
141  * @param channel new handle to the channel
142  * @param initiator peer that started the channel
143  * @param port port number
144  * @param options channel options
145  * @return initial channel context for the channel
146  *         (can be NULL -- that's not an error)
147  */
148 static void *
149 inbound_channel (void *cls,
150                  struct GNUNET_CADET_Channel *channel,
151                  const struct GNUNET_PeerIdentity *initiator,
152                  const struct GNUNET_HashCode *port,
153                  enum GNUNET_CADET_ChannelOption options)
154 {
155   long id = (long) cls;
156
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "received incoming channel on peer %d, port %s\n",
159               (int) id,
160               GNUNET_h2s (port));
161   if (id != 2L)
162   {
163     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
164                 "wrong peer\n");
165     result = GNUNET_SYSERR;
166   }
167   return NULL;
168 }
169
170
171 /**
172  * Function called whenever an channel is destroyed.  Should clean up
173  * any associated state.
174  *
175  * @param cls closure (set from GNUNET_CADET_connect)
176  * @param channel connection to the other end (henceforth invalid)
177  * @param channel_ctx place where local state associated
178  *                    with the channel is stored
179  */
180 static void
181 channel_end (void *cls,
182              const struct GNUNET_CADET_Channel *channel,
183              void *channel_ctx)
184 {
185   long id = (long) cls;
186
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188               "incoming channel closed at peer %ld\n",
189               id);
190   if (NULL != mth)
191   {
192     GNUNET_CADET_notify_transmit_ready_cancel (mth);
193     mth = NULL;
194   }
195   if (channel == ch)
196     ch = NULL;
197   if (GNUNET_NO == got_data)
198   {
199     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
200                                   GNUNET_TIME_UNIT_SECONDS,
201                                   2),
202                                   &do_connect, NULL);
203   }
204 }
205
206
207 /**
208  * Handler array for traffic received on peer1
209  */
210 static struct GNUNET_CADET_MessageHandler handlers1[] = {
211   {&data_callback, 1, 0},
212   {NULL, 0, 0}
213 };
214
215
216 /**
217  * Handler array for traffic received on peer2 (none expected)
218  */
219 static struct GNUNET_CADET_MessageHandler handlers2[] = {
220   {&data_callback, 1, 0},
221   {NULL, 0, 0}
222 };
223
224
225 /**
226  * Data send callback: fillbuffer with test packet.
227  *
228  * @param cls Closure (unused).
229  * @param size Buffer size.
230  * @param buf Buffer to fill.
231  *
232  * @return size of test packet.
233  */
234 static size_t
235 do_send (void *cls, size_t size, void *buf)
236 {
237   struct GNUNET_MessageHeader *m = buf;
238
239   mth = NULL;
240   if (NULL == buf)
241   {
242     GNUNET_break (0);
243     result = GNUNET_SYSERR;
244     return 0;
245   }
246   m->size = htons (sizeof (struct GNUNET_MessageHeader));
247   m->type = htons (1);
248   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
249   return sizeof (struct GNUNET_MessageHeader);
250 }
251
252
253 /**
254  * Connect to other client and send data
255  *
256  * @param cls Closue (unused).
257  */
258 static void
259 do_connect (void *cls)
260 {
261   struct GNUNET_PeerIdentity id;
262
263   connect_task = NULL;
264   GNUNET_TESTING_peer_get_identity (me, &id);
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "CONNECT BY PORT\n");
267   ch = GNUNET_CADET_channel_create (cadet_peer_1,
268                                     NULL,
269                                     &id, GC_u2h (1),
270                                     GNUNET_CADET_OPTION_DEFAULT);
271   mth = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
272                                             GNUNET_TIME_UNIT_FOREVER_REL,
273                                             sizeof (struct GNUNET_MessageHeader),
274                                             &do_send, NULL);
275 }
276
277
278 /**
279  * Initialize framework and start test
280  *
281  * @param cls Closure (unused).
282  * @param cfg Configuration handle.
283  * @param peer Testing peer handle.
284  */
285 static void
286 run (void *cls,
287      const struct GNUNET_CONFIGURATION_Handle *cfg,
288      struct GNUNET_TESTING_Peer *peer)
289 {
290   me = peer;
291   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
292   abort_task =
293       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
294                                     (GNUNET_TIME_UNIT_SECONDS, 15),
295                                     &do_abort,
296                                     NULL);
297   cadet_peer_1 = GNUNET_CADET_connect (cfg,       /* configuration */
298                                      (void *) 1L,       /* cls */
299                                      &channel_end,      /* channel end hndlr */
300                                      handlers1); /* traffic handlers */
301
302   cadet_peer_2 = GNUNET_CADET_connect (cfg,       /* configuration */
303                                      (void *) 2L,     /* cls */
304                                      &channel_end,      /* channel end hndlr */
305                                      handlers2); /* traffic handlers */
306
307   if (NULL == cadet_peer_1 || NULL == cadet_peer_2)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 "Couldn't connect to cadet :(\n");
311     result = GNUNET_SYSERR;
312     GNUNET_SCHEDULER_shutdown ();
313     return;
314   }
315   GNUNET_CADET_open_port (cadet_peer_2,
316                           GC_u2h (1),
317                           &inbound_channel,
318                           (void *) 2L);
319   connect_task
320     = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
321                                                                    2),
322                                     &do_connect,
323                                     NULL);
324 }
325
326
327 /**
328  * Main
329  */
330 int
331 main (int argc, char *argv[])
332 {
333   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
334                                     "test_cadet.conf",
335                                 &run, NULL))
336   {
337     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
338     return 2;
339   }
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
341   return (result == GNUNET_OK) ? 0 : 1;
342 }
343
344 /* end of test_cadet_local_1.c */