Use encrypted MESH by default
[oweals/gnunet.git] / src / mesh / test_mesh_local.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file mesh/test_mesh_local.c
23  * @brief test mesh local: test of mesh 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_mesh_service.h"
32
33 struct GNUNET_TESTING_Peer *me;
34
35 static struct GNUNET_MESH_Handle *mesh_peer_1;
36
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
38
39 static struct GNUNET_MESH_Channel *ch;
40
41 static int result = GNUNET_OK;
42
43 static int got_data = GNUNET_NO;
44
45 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
46
47 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
48
49 static struct GNUNET_MESH_TransmitHandle *mth;
50
51
52 /**
53  * Connect to other client and send data
54  *
55  * @param cls Closue (unused).
56  * @param tc TaskContext.
57  */
58 static void
59 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
60
61
62 /**
63  * Shutdown nicely
64  */
65 static void
66 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
69   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
70   {
71     GNUNET_SCHEDULER_cancel (abort_task);
72   }
73   if (NULL != ch)
74   {
75     GNUNET_MESH_channel_destroy (ch);
76   }
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
78   if (NULL != mesh_peer_1)
79   {
80     GNUNET_MESH_disconnect (mesh_peer_1);
81   }
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
83   if (NULL != mesh_peer_2)
84   {
85     GNUNET_MESH_disconnect (mesh_peer_2);
86   }
87 }
88
89
90 /**
91  * Something went wrong and timed out. Kill everything and set error flag
92  */
93 static void
94 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
97   result = GNUNET_SYSERR;
98   abort_task = GNUNET_SCHEDULER_NO_TASK;
99   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
100   {
101     GNUNET_SCHEDULER_cancel (shutdown_task);
102     shutdown_task = GNUNET_SCHEDULER_NO_TASK;
103   }
104   do_shutdown (cls, tc);
105 }
106
107
108 /**
109  * Function is called whenever a message is received.
110  *
111  * @param cls closure (set from GNUNET_MESH_connect)
112  * @param channel connection to the other end
113  * @param channel_ctx place to store local state associated with the channel
114  * @param message the actual message
115  *
116  * @return GNUNET_OK to keep the connection open,
117  *         GNUNET_SYSERR to close it (signal serious error)
118  */
119 static int
120 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
121                void **channel_ctx,
122                const struct GNUNET_MessageHeader *message)
123 {
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data callback! Shutting down.\n");
125   got_data = GNUNET_YES;
126   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
127     GNUNET_SCHEDULER_cancel (shutdown_task);
128   shutdown_task =
129     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown,
130                                   NULL);
131   GNUNET_MESH_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  * @return initial channel context for the channel
145  *         (can be NULL -- that's not an error)
146  */
147 static void *
148 inbound_channel (void *cls, struct GNUNET_MESH_Channel *channel,
149                 const struct GNUNET_PeerIdentity *initiator,
150                 uint32_t port)
151 {
152   long id = (long) cls;
153
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "received incoming channel on peer %d, port %u\n",
156               id, port);
157   if (id != 2L)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
160                 "wrong peer\n");
161     result = GNUNET_SYSERR;
162   }
163   return NULL;
164 }
165
166
167 /**
168  * Function called whenever an channel is destroyed.  Should clean up
169  * any associated state.
170  *
171  * @param cls closure (set from GNUNET_MESH_connect)
172  * @param channel connection to the other end (henceforth invalid)
173  * @param channel_ctx place where local state associated
174  *                    with the channel is stored
175  */
176 static void
177 channel_end (void *cls, const struct GNUNET_MESH_Channel *channel,
178              void *channel_ctx)
179 {
180   long id = (long) cls;
181
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183               "incoming channel closed at peer %ld\n",
184               id);
185   if (NULL != mth)
186   {
187     GNUNET_MESH_notify_transmit_ready_cancel (mth);
188     mth = NULL;
189   }
190   if (GNUNET_NO == got_data)
191   {
192     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
193                                   GNUNET_TIME_UNIT_SECONDS,
194                                   2),
195                                   &do_connect, NULL);
196   }
197 }
198
199
200 /**
201  * Handler array for traffic received on peer1
202  */
203 static struct GNUNET_MESH_MessageHandler handlers1[] = {
204   {&data_callback, 1, 0},
205   {NULL, 0, 0}
206 };
207
208
209 /**
210  * Handler array for traffic received on peer2 (none expected)
211  */
212 static struct GNUNET_MESH_MessageHandler handlers2[] = {
213   {&data_callback, 1, 0},
214   {NULL, 0, 0}
215 };
216
217
218 /**
219  * Data send callback: fillbuffer with test packet.
220  *
221  * @param cls Closure (unused).
222  * @param size Buffer size.
223  * @param buf Buffer to fill.
224  *
225  * @return size of test packet.
226  */
227 static size_t
228 do_send (void *cls, size_t size, void *buf)
229 {
230   struct GNUNET_MessageHeader *m = buf;
231
232   mth = NULL;
233   if (NULL == buf)
234   {
235     GNUNET_break (0);
236     result = GNUNET_SYSERR;
237     return 0;
238   }
239   m->size = htons (sizeof (struct GNUNET_MessageHeader));
240   m->type = htons (1);
241   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
242   return sizeof (struct GNUNET_MessageHeader);
243 }
244
245 /**
246  * Connect to other client and send data
247  *
248  * @param cls Closue (unused).
249  * @param tc TaskContext.
250  */
251 static void
252 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
253 {
254   struct GNUNET_PeerIdentity id;
255
256   GNUNET_TESTING_peer_get_identity (me, &id);
257   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
258   ch = GNUNET_MESH_channel_create (mesh_peer_1, NULL, &id, 1,
259                                   GNUNET_YES, GNUNET_NO);
260   mth = GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
261                                            GNUNET_TIME_UNIT_FOREVER_REL,
262                                            sizeof (struct GNUNET_MessageHeader),
263                                            &do_send, NULL);
264 }
265
266
267 /**
268  * Initialize framework and start test
269  *
270  * @param cls Closure (unused).
271  * @param cfg Configuration handle.
272  * @param peer Testing peer handle.
273  */
274 static void
275 run (void *cls,
276      const struct GNUNET_CONFIGURATION_Handle *cfg,
277      struct GNUNET_TESTING_Peer *peer)
278 {
279   static uint32_t ports[] = {1, 0};
280
281   me = peer;
282   abort_task =
283       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
284                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
285                                     NULL);
286   mesh_peer_1 = GNUNET_MESH_connect (cfg,       /* configuration */
287                                      (void *) 1L,     /* cls */
288                                      &inbound_channel,   /* inbound new hndlr */
289                                      &channel_end,      /* channel end hndlr */
290                                      handlers1, /* traffic handlers */
291                                      NULL);     /* ports offered */
292
293   mesh_peer_2 = GNUNET_MESH_connect (cfg,       /* configuration */
294                                      (void *) 2L,     /* cls */
295                                      &inbound_channel,   /* inbound new hndlr */
296                                      &channel_end,      /* channel end hndlr */
297                                      handlers2, /* traffic handlers */
298                                      ports);     /* ports offered */
299   if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
300   {
301     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
302     result = GNUNET_SYSERR;
303     return;
304   }
305   else
306   {
307     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
308   }
309   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
310                                   GNUNET_TIME_UNIT_SECONDS,
311                                   2),
312                                 &do_connect, NULL);
313 }
314
315
316 /**
317  * Main
318  */
319 int
320 main (int argc, char *argv[])
321 {
322   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local",
323                                     "test_mesh.conf",
324                                 &run, NULL))
325   {
326     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
327     return 2;
328   }
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
330   return (result == GNUNET_OK) ? 0 : 1;
331 }
332
333 /* end of test_mesh_local_1.c */