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