test_multicast_2peers.c: fix code style
[oweals/gnunet.git] / src / multicast / test_multicast_2peers.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 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 multicast/test_multicast_2peers.c
23  * @brief Tests for the Multicast API with two peers doing the ping 
24  *        pong test.
25  * @author xrs
26  */
27
28 #include <inttypes.h>
29
30 #include "platform.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_common.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_multicast_service.h"
36
37 #define NUM_PEERS 2
38
39 static struct GNUNET_TESTBED_Operation *op0;
40 static struct GNUNET_TESTBED_Operation *op1;
41 static struct GNUNET_TESTBED_Operation *pi_op0;
42 static struct GNUNET_TESTBED_Operation *pi_op1;
43
44 static struct GNUNET_TESTBED_Peer **peers;
45 const struct GNUNET_PeerIdentity *peer_id[2];
46
47 static struct GNUNET_SCHEDULER_Task *timeout_tid;
48
49 static struct GNUNET_MULTICAST_Origin *origin;
50 static struct GNUNET_MULTICAST_Member *member;
51
52 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
53 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
54
55 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
56 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
57
58 /**
59  * Global result for testcase.
60  */
61 static int result;
62
63
64 /**
65  * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
66  * Cleans up.
67  */
68 static void
69 shutdown_task (void *cls)
70 {
71   if (NULL != op0)
72   {
73     GNUNET_TESTBED_operation_done (op0);
74     op0 = NULL;
75   }
76   if (NULL != op1)
77   {
78     GNUNET_TESTBED_operation_done (op1);
79     op1 = NULL;
80   }
81   if (NULL != pi_op0)
82   {
83     GNUNET_TESTBED_operation_done (pi_op0);
84     pi_op0 = NULL;
85   }
86   if (NULL != pi_op1)
87   {
88     GNUNET_TESTBED_operation_done (pi_op1);
89     pi_op1 = NULL;
90   }
91   if (NULL != timeout_tid)
92     {
93       GNUNET_SCHEDULER_cancel (timeout_tid);
94       timeout_tid = NULL;
95     }
96 }
97
98
99 static void
100 timeout_task (void *cls)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103               "Timeout!\n");
104   result = GNUNET_SYSERR;
105   GNUNET_SCHEDULER_shutdown ();
106 }
107
108
109 static void
110 member_join_request (void *cls,
111                      const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
112                      const struct GNUNET_MessageHeader *join_msg,
113                      struct GNUNET_MULTICAST_JoinHandle *jh)
114 {
115   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
116               "Member sent a join request.\n");
117
118 }
119
120
121 int notify (void *cls,
122             size_t *data_size,
123             void *data)
124 {
125
126   char text[] = "ping";
127   *data_size = strlen(text)+1;
128   GNUNET_memcpy(data, text, *data_size);
129
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
131               "Member sents message to origin: %s\n", text);
132
133   return GNUNET_YES;
134 }
135
136
137 static void
138 member_join_decision (void *cls,
139                       int is_admitted,
140                       const struct GNUNET_PeerIdentity *peer,
141                       uint16_t relay_count,
142                       const struct GNUNET_PeerIdentity *relays,
143                       const struct GNUNET_MessageHeader *join_msg)
144 {
145   struct GNUNET_MULTICAST_MemberTransmitHandle *req;
146   
147   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
148               "Member received a decision from origin: %s\n", (GNUNET_YES == is_admitted)?"accepted":"rejected");
149   
150   if (GNUNET_YES == is_admitted)
151   {
152     req = GNUNET_MULTICAST_member_to_origin (member,
153                                              0,
154                                              notify,
155                                              NULL);
156   }
157 }
158
159
160 static void
161 member_message (void *cls, 
162                 const struct GNUNET_MULTICAST_MessageHeader *msg)
163 {
164   if (0 != strncmp ("pong", (char *)&msg[1], 4)) 
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "member did not receive pong\n");
167     result = GNUNET_SYSERR;
168     GNUNET_SCHEDULER_shutdown ();
169   }
170
171   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172               "member receives: %s\n", (char *)&msg[1]);
173
174   // Testcase ends here.
175   result = GNUNET_YES;
176   GNUNET_SCHEDULER_shutdown ();
177 }
178
179
180 static void
181 origin_join_request (void *cls,
182                  const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
183                  const struct GNUNET_MessageHeader *join_msg,
184                  struct GNUNET_MULTICAST_JoinHandle *jh)
185 {
186   struct GNUNET_MessageHeader *join_resp;
187
188   uint8_t data_size = ntohs (join_msg->size);
189
190   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
191               "origin got a join request...\n");
192   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
193               "origin receives: '%s'\n", (char *)&join_msg[1]);
194
195   char data[] = "Come in!";
196   data_size = strlen (data) + 1;
197   join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
198   join_resp->size = htons (sizeof (join_resp) + data_size);
199   join_resp->type = htons (123);
200   GNUNET_memcpy (&join_resp[1], data, data_size);
201
202   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
203               "origin sends: '%s'\n", data);
204
205   GNUNET_MULTICAST_join_decision (jh,
206                                   GNUNET_YES,
207                                   0,
208                                   NULL,
209                                   join_resp);
210
211   result = GNUNET_OK;
212 }
213
214
215 int
216 origin_notify (void *cls, 
217                size_t *data_size, 
218                void *data)
219 {
220   char text[] = "pong";
221   *data_size = strlen(text)+1;
222   memcpy(data, text, *data_size); 
223
224   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin sends (to all): %s\n", text);
225
226   return GNUNET_YES; 
227 }
228
229
230 static void
231 origin_request (void *cls,
232                 const struct GNUNET_MULTICAST_RequestHeader *req)
233 {
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin receives: %s\n", (char *)&req[1]);
235   
236   if (0 != strncmp ("ping", (char *)&req[1], 4)) 
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "origin didn't reveice a correct request");
238
239   GNUNET_MULTICAST_origin_to_all (origin,
240                                   0,
241                                   0,
242                                   origin_notify,
243                                   NULL);
244 }
245
246
247 static void
248 origin_message (void *cls,
249                 const struct GNUNET_MULTICAST_MessageHeader *msg) 
250 {
251   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin message msg\n");
252 }
253
254
255 static void
256 service_connect1 (void *cls,
257                   struct GNUNET_TESTBED_Operation *op,
258                   void *ca_result,
259                   const char *emsg)
260 {
261   member = ca_result;
262
263   if (NULL == member)
264   {
265     result = GNUNET_SYSERR;
266     GNUNET_SCHEDULER_shutdown ();
267   }
268   else
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
271   }
272 }
273
274
275 static void
276 multicast_da1 (void *cls,
277                void * op_result)
278 {
279   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280               "Member parting from multicast group\n");
281
282   GNUNET_MULTICAST_member_part (member, NULL, NULL);
283 }
284
285
286 static void *
287 multicast_ca1 (void *cls,
288                const struct GNUNET_CONFIGURATION_Handle *cfg)
289 {
290   struct GNUNET_MessageHeader *join_msg;
291
292   // Get members keys
293   member_key = GNUNET_CRYPTO_ecdsa_key_create ();
294   GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
295   
296   char data[] = "Hi, can I enter?";
297   uint8_t data_size = strlen (data) + 1;
298   join_msg = GNUNET_malloc (sizeof (join_msg) + data_size);
299   join_msg->size = htons (sizeof (join_msg) + data_size);
300   join_msg->type = htons (123);
301   GNUNET_memcpy (&join_msg[1], data, data_size);
302
303   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304               "Members tries to join multicast group\n");
305
306   return GNUNET_MULTICAST_member_join (cfg,
307                                        &group_pub_key,
308                                        member_key,
309                                        peer_id[0],
310                                        0,
311                                        NULL,
312                                        join_msg, /* join message */
313                                        member_join_request,
314                                        member_join_decision,
315                                        NULL, /* no test for member_replay_frag */
316                                        NULL, /* no test for member_replay_msg */
317                                        member_message,
318                                        NULL);
319 }
320
321
322 static void
323 peer_information_cb (void *cls,
324                      struct GNUNET_TESTBED_Operation *op,
325                      const struct GNUNET_TESTBED_PeerInformation *pinfo,
326                      const char *emsg)
327 {
328   int i = (int) (long) cls;
329
330   if (NULL == pinfo) 
331   {
332     result = GNUNET_SYSERR;
333     GNUNET_SCHEDULER_shutdown ();
334   }
335
336   peer_id[i] = pinfo->result.id;
337
338   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
339               "Got peer information of %s (%s)\n", (0==i)?"origin":"member" ,GNUNET_i2s(pinfo->result.id));
340
341   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
342               "Create member peer\n");
343
344   if (0 == i) 
345   {
346     /* connect to multicast service of member */
347     op1 = GNUNET_TESTBED_service_connect (NULL,                    /* Closure for operation */
348                                           peers[1],                /* The peer whose service to connect to */
349                                           "multicast",             /* The name of the service */
350                                           service_connect1,   /* callback to call after a handle to service
351                                                                  is opened */
352                                           NULL,                    /* closure for the above callback */
353                                           multicast_ca1,      /* callback to call with peer's configuration;
354                                                                  this should open the needed service connection */
355                                           multicast_da1,     /* callback to be called when closing the
356                                                                 opened service connection */
357                                           NULL);                   /* closure for the above two callbacks */
358   }
359 }
360
361
362 /**
363  * Test logic of peer "0" being origin starts here.
364  *
365  * @param cls closure, for the example: NULL
366  * @param op should be equal to "dht_op"
367  * @param ca_result result of the connect operation, the
368  *        connection to the DHT service
369  * @param emsg error message, if testbed somehow failed to
370  *        connect to the DHT.
371  */
372 static void
373 service_connect0 (void *cls,
374                   struct GNUNET_TESTBED_Operation *op,
375                   void *ca_result,
376                   const char *emsg)
377 {
378   origin = ca_result;
379
380   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
381               "Connected to multicast service of origin\n");
382
383   // Get GNUnet identity of origin
384   pi_op0 = GNUNET_TESTBED_peer_get_information (peers[0],
385                                                GNUNET_TESTBED_PIT_IDENTITY,
386                                                peer_information_cb,
387                                                (void *) 0);
388   // Get GNUnet identity of member
389   pi_op1 = GNUNET_TESTBED_peer_get_information (peers[1],
390                                                GNUNET_TESTBED_PIT_IDENTITY,
391                                                peer_information_cb,
392                                                (void *) 1);
393
394   /* Connection to service successful. Here we'd usually do something with
395    * the service. */
396   result = GNUNET_OK;
397   //GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
398 }
399
400
401
402 /**
403  * Function run when service multicast has started and is providing us
404  * with a configuration file.
405  */
406 static void *
407 multicast_ca0 (void *cls,
408                const struct GNUNET_CONFIGURATION_Handle *cfg)
409 {
410   group_key = GNUNET_CRYPTO_eddsa_key_create ();
411   GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
412
413   return GNUNET_MULTICAST_origin_start (cfg,
414                                         group_key,
415                                         0,
416                                         origin_join_request,
417                                         NULL, /* no test for origin_replay_frag */
418                                         NULL, /* no test for origin_replay_msg */
419                                         origin_request,
420                                         origin_message,
421                                         NULL);
422 }
423
424 static void
425 multicast_da0 (void *cls,
426                void *op_result)
427 {
428   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
429               "Origin closes multicast group\n");
430
431   GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
432 }
433
434
435 /**
436  * Main function inovked from TESTBED once all of the
437  * peers are up and running.  This one then connects
438  * just to the multicast service of peer 0 and 1.
439  * Peer 0 is going to be origin.
440  * Peer 1 is going to be one member.
441  * Origin will start a multicast group and the member will try to join it.
442  * After that we execute some multicast test.
443  *
444  * @param cls closure
445  * @param h the run handle
446  * @param peers started peers for the test
447  * @param num_peers size of the 'peers' array
448  * @param links_succeeded number of links between peers that were created
449  * @param links_failed number of links testbed was unable to establish
450  */
451 static void
452 testbed_master (void *cls,
453      struct GNUNET_TESTBED_RunHandle *h,
454      unsigned int num_peers,
455      struct GNUNET_TESTBED_Peer **p,
456      unsigned int links_succeeded,
457      unsigned int links_failed)
458 {
459   /* Testbed is ready with peers running and connected in a pre-defined overlay
460      topology (FIXME)  */
461   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
462               "Connected to testbed_master()\n");
463
464   peers = p;
465
466   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
467               "Create origin peer\n");
468   op0 = GNUNET_TESTBED_service_connect (NULL,                    /* Closure for operation */
469                                         peers[0],                /* The peer whose service to connect to */
470                                         "multicast",             /* The name of the service */
471                                         service_connect0,   /* callback to call after a handle to service
472                                                                is opened */
473                                         NULL,                    /* closure for the above callback */
474                                         multicast_ca0,      /* callback to call with peer's configuration;
475                                                                this should open the needed service connection */
476                                         multicast_da0,     /* callback to be called when closing the
477                                                               opened service connection */
478                                         NULL);                   /* closure for the above two callbacks */
479
480   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); /* Schedule a new task on shutdown */
481
482   /* Schedule the shutdown task with a delay of a few Seconds */
483   timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 50),
484                                               &timeout_task, NULL);
485 }
486
487
488 int
489 main (int argc, char *argv[])
490 {
491   int ret;
492
493   result = GNUNET_SYSERR;
494   ret = GNUNET_TESTBED_test_run
495       ("test-multicast-2peers",  /* test case name */
496        "test_multicast.conf", /* template configuration */
497        NUM_PEERS,       /* number of peers to start */
498        0LL, /* Event mask - set to 0 for no event notifications */
499        NULL, /* Controller event callback */
500        NULL, /* Closure for controller event callback */
501        testbed_master, /* continuation callback to be called when testbed setup is complete */
502        NULL); /* Closure for the test_master callback */
503   if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
504     return 1;
505   return 0;
506 }
507
508
509 /* end of test_multicast_2peers.c */