b23879032f6a3cc8f7afe0de6e7133a643320cc6
[oweals/gnunet.git] / src / transport / transport-testing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 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 2, 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 transport-testing.c
23  * @brief testing lib for transport service
24  *
25  * @author Matthias Wachs
26  */
27
28 #include "transport-testing.h"
29
30 #define VERBOSE GNUNET_YES
31
32
33 static struct PeerContext *
34 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
35                    const struct GNUNET_PeerIdentity *peer)
36 {
37   GNUNET_assert (tth != NULL);
38   struct PeerContext *t = tth->p_head;
39
40   while (t != NULL)
41   {
42     if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
43       break;
44     t = t->next;
45   }
46
47   return t;
48 }
49
50 struct ConnectingContext *
51 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
52                          struct PeerContext *p1, struct PeerContext *p2)
53 {
54   GNUNET_assert (tth != NULL);
55   struct ConnectingContext *cc = tth->cc_head;
56
57   while (cc != NULL)
58   {
59     if ((cc->p1 == p1) && (cc->p2 == p2))
60       break;
61     if ((cc->p1 == p2) && (cc->p2 == p1))
62       break;
63     cc = cc->next;
64   }
65
66   return cc;
67 }
68
69 static void
70 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
71                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
72 {
73   struct PeerContext *p = cls;
74
75   /* Find PeerContext */
76   GNUNET_assert (p != 0);
77   GNUNET_assert (p->tth != NULL);
78   struct PeerContext *p2 = find_peer_context (p->tth, peer);
79
80   if (p == NULL)
81     return;
82   if (p->nc != NULL)
83     p->nc (p->cb_cls, peer, ats, ats_count);
84
85 #if VERBOSE
86   char *p2_s;
87
88   if (p2 != NULL)
89     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
90   else
91     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
92   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
93                    "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
94                    GNUNET_i2s (&p->id));
95   GNUNET_free (p2_s);
96 #endif
97
98
99   /* Find ConnectingContext */
100   struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
101
102   if (cc == NULL)
103     return;
104
105   if (p == cc->p1)
106     cc->p1_c = GNUNET_YES;
107
108   if (p == cc->p2)
109     cc->p2_c = GNUNET_YES;
110
111   if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
112   {
113     cc->cb (cc->p1, cc->p2, cc->cb_cls);
114     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
115   }
116 }
117
118 static void
119 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
120 {
121   struct PeerContext *p = cls;
122
123   /* Find PeerContext */
124   int no = 0;
125   struct PeerContext *p2 = NULL;
126
127   if (p != NULL)
128   {
129     GNUNET_assert (p->tth != NULL);
130     p2 = find_peer_context (p->tth, peer);
131     no = p->no;
132   }
133
134   char *p2_s;
135
136   if (p2 != NULL)
137     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
138   else
139     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
140   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
141                    "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
142                    GNUNET_i2s (&p->id));
143   GNUNET_free (p2_s);
144
145   if (p == NULL)
146     return;
147   if (p->nd != NULL)
148     p->nd (p->cb_cls, peer);
149 }
150
151 static void
152 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
153                 const struct GNUNET_MessageHeader *message,
154                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
155 {
156   struct PeerContext *p = cls;
157
158   if (p == NULL)
159     return;
160   if (p->rec != NULL)
161     p->rec (p->cb_cls, peer, message, ats, ats_count);
162 }
163
164 static void
165 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
166 {
167   struct PeerContext *p = cb_cls;
168
169   GNUNET_assert (message != NULL);
170   GNUNET_assert (GNUNET_OK ==
171                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
172                                       message, &p->id));
173   size_t size =
174       GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
175   GNUNET_free_non_null (p->hello);
176   p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
177
178 #if VERBOSE
179   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
180                    "New HELLO for peer %u (`%s') with size %u\n", p->no,
181                    GNUNET_i2s (&p->id), size);
182 #endif
183
184   if (p->start_cb != NULL)
185   {
186 #if VERBOSE
187     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
188                      "Peer %u (`%s') successfully started\n", p->no,
189                      GNUNET_i2s (&p->id));
190 #endif
191     p->start_cb (p, p->cb_cls);
192     p->start_cb = NULL;
193   }
194 }
195
196
197 static void
198 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 {
200   struct ConnectingContext *cc = cls;
201   struct PeerContext *p1 = cc->p1;
202   struct PeerContext *p2 = cc->p2;
203
204   cc->tct = GNUNET_SCHEDULER_NO_TASK;
205   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
206     return;
207
208   char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
209
210   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
211                    "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
212                    p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
213                    GNUNET_HELLO_size (cc->p2->hello));
214   GNUNET_free (p2_s);
215
216   GNUNET_TRANSPORT_offer_hello (cc->th_p1,
217                                 (const struct GNUNET_MessageHeader *) cc->
218                                 p2->hello, NULL, NULL);
219   GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
220
221   cc->tct =
222       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
223 }
224
225
226 /**
227  * Start a peer with the given configuration
228  * @param rec receive callback
229  * @param nc connect callback
230  * @param nd disconnect callback
231  * @param cb_cls closure for callback
232  * @return the peer context
233  */
234 struct PeerContext *
235 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
236                                      *tth, const char *cfgname, int peer_id,
237                                      GNUNET_TRANSPORT_ReceiveCallback rec,
238                                      GNUNET_TRANSPORT_NotifyConnect nc,
239                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
240                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
241                                      void *cb_cls)
242 {
243   GNUNET_assert (tth != NULL);
244   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
245   {
246     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
247                      "File not found: `%s' \n", cfgname);
248     return NULL;
249   }
250
251   struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
252
253   p->cfg = GNUNET_CONFIGURATION_create ();
254
255   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
256   if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
257     GNUNET_assert (GNUNET_OK ==
258                    GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
259                                                           "SERVICEHOME",
260                                                           &p->servicehome));
261   if (NULL != p->servicehome)
262     GNUNET_DISK_directory_remove (p->servicehome);
263   p->arm_proc =
264       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
265                                "gnunet-service-arm", "-c", cfgname,
266 #if VERBOSE_PEERS
267                                "-L", "DEBUG",
268 #else
269                                "-L", "ERROR",
270 #endif
271                                NULL);
272
273   p->no = peer_id;
274   p->tth = tth;
275   p->nc = nc;
276   p->nd = nd;
277   p->rec = rec;
278   p->start_cb = start_cb;
279   if (cb_cls != NULL)
280     p->cb_cls = cb_cls;
281   else
282     p->cb_cls = p;
283
284   p->th =
285       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
286                                 &notify_connect, &notify_disconnect);
287   GNUNET_assert (p->th != NULL);
288
289   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
290   GNUNET_assert (p->ghh != NULL);
291
292   GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
293
294   return p;
295 }
296
297 /**
298  * shutdown the given peer
299  * @param p the peer
300  */
301 void
302 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
303                                     struct PeerContext *p)
304 {
305   GNUNET_assert (p != NULL);
306
307   if (p->ghh != NULL)
308     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
309   p->ghh = NULL;
310
311   if (p->th != NULL)
312     GNUNET_TRANSPORT_disconnect (p->th);
313
314   if (NULL != p->arm_proc)
315   {
316     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
317       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
318     GNUNET_OS_process_wait (p->arm_proc);
319     GNUNET_OS_process_close (p->arm_proc);
320     p->arm_proc = NULL;
321   }
322
323   if (p->servicehome != NULL)
324   {
325     GNUNET_DISK_directory_remove (p->servicehome);
326     GNUNET_free (p->servicehome);
327   }
328
329   if (p->hello != NULL)
330     GNUNET_free (p->hello);
331
332   if (p->cfg != NULL)
333     GNUNET_CONFIGURATION_destroy (p->cfg);
334
335   GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
336
337   GNUNET_free (p);
338   p = NULL;
339 }
340
341 /**
342  * Initiate peer p1 to connect to peer p2
343  * Get peer p2's HELLO and offer it to p1
344  * p1 then tries to connect to p2
345  * @param p1 peer 1
346  * @param p2 peer 2
347  * @param cb the callback to call when both peers notified that they are connected
348  * @param cb_cls callback cls
349  * @return connect context
350  */
351 GNUNET_TRANSPORT_TESTING_ConnectRequest
352 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
353                                         *tth, struct PeerContext *p1,
354                                         struct PeerContext *p2,
355                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
356                                         void *cb_cls)
357 {
358   GNUNET_assert (tth != NULL);
359
360   struct ConnectingContext *cc =
361       GNUNET_malloc (sizeof (struct ConnectingContext));
362
363   GNUNET_assert (p1 != NULL);
364   GNUNET_assert (p2 != NULL);
365
366   cc->p1 = p1;
367   cc->p2 = p2;
368
369   cc->cb = cb;
370   cc->cb_cls = cb_cls;
371
372   cc->th_p1 = p1->th;
373   cc->th_p2 = p2->th;
374
375   GNUNET_assert (cc->th_p1 != NULL);
376   GNUNET_assert (cc->th_p2 != NULL);
377
378   GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
379
380   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
381   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
382                    "New connect request %X\n", cc);
383
384   return cc;
385 }
386
387 /**
388  * Cancel the request to connect two peers
389  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
390  * @param cc a connect request handle
391  */
392 void
393 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
394                                                GNUNET_TRANSPORT_TESTING_handle
395                                                *tth,
396                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
397                                                ccr)
398 {
399   struct ConnectingContext *cc = ccr;
400
401   GNUNET_assert (tth != NULL);
402
403   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
404                    "Canceling connect request %X!\n", cc);
405   if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
406     GNUNET_SCHEDULER_cancel (cc->tct);
407
408   cc->tct = GNUNET_SCHEDULER_NO_TASK;
409
410   GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
411   GNUNET_free (cc);
412 }
413
414
415 /**
416  * Clean up the transport testing
417  * @param tth transport testing handle
418  */
419 void
420 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
421 {
422   struct ConnectingContext *cc = tth->cc_head;
423   struct ConnectingContext *ct = NULL;
424   struct PeerContext *p = tth->p_head;
425   struct PeerContext *t = NULL;
426
427   GNUNET_assert (tth != NULL);
428
429   while (cc != tth->cc_tail)
430   {
431     ct = cc->next;
432     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
433                      "Developer forgot to cancel connect request %X!\n", cc);
434     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
435     cc = ct;
436   }
437
438   while (p != NULL)
439   {
440     t = p->next;
441     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
442                      "Developer forgot to stop peer!\n");
443     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
444     p = t;
445   }
446
447   GNUNET_free (tth);
448   tth = NULL;
449 }
450
451 /**
452  * Initialize the transport testing
453  * @return transport testing handle
454  */
455 struct GNUNET_TRANSPORT_TESTING_handle *
456 GNUNET_TRANSPORT_TESTING_init ()
457 {
458   struct GNUNET_TRANSPORT_TESTING_handle *tth =
459       GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
460
461   return tth;
462 }
463
464
465 /*
466  * Some utility functions
467  */
468
469 /**
470  * Removes all directory separators from absolute filename
471  * @param file the absolute file name, e.g. as found in argv[0]
472  * @return extracted file name, has to be freed by caller
473  */
474 static char *
475 extract_filename (const char *file)
476 {
477   char *pch = GNUNET_strdup (file);
478   char *backup = pch;
479   char *filename = NULL;
480   char *res;
481
482   if (NULL != strstr (pch, "/"))
483   {
484     pch = strtok (pch, "/");
485     while (pch != NULL)
486     {
487       pch = strtok (NULL, "/");
488       if (pch != NULL)
489       {
490         filename = pch;
491       }
492     }
493   }
494   else
495     filename = pch;
496
497   res = GNUNET_strdup (filename);
498   GNUNET_free (backup);
499   return res;
500 }
501
502 /**
503  * Extracts the test filename from an absolute file name and removes the extension
504  * @param file absolute file name
505  * @param dest where to store result
506  */
507 void
508 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
509 {
510   char *filename = extract_filename (file);
511   char *backup = filename;
512   char *dotexe;
513
514   if (filename == NULL)
515     goto fail;
516
517   /* remove "lt-" */
518   filename = strstr (filename, "tes");
519   if (filename == NULL)
520     goto fail;
521
522   /* remove ".exe" */
523   if (NULL != (dotexe = strstr (filename, ".exe")))
524     dotexe[0] = '\0';
525
526   goto suc;
527
528 fail:
529   (*dest) = NULL;
530   return;
531
532 suc:
533   /* create filename */
534   GNUNET_asprintf (dest, "%s", filename);
535   GNUNET_free (backup);
536 }
537
538
539 /**
540  * Extracts the filename from an absolute file name and removes the extension
541  * @param file absolute file name
542  * @param dest where to store result
543  */
544 void
545 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
546 {
547   char *src = extract_filename (file);
548   char *split;
549
550   split = strstr (src, ".");
551   if (split != NULL)
552   {
553     split[0] = '\0';
554   }
555   GNUNET_asprintf (dest, "%s", src);
556   GNUNET_free (src);
557 }
558
559
560 /**
561  * Extracts the plugin anme from an absolute file name and the test name
562  * @param file absolute file name
563  * @param test test name
564  * @param dest where to store result
565  */
566 void
567 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
568                                                const char *test, char **dest)
569 {
570   char *e = extract_filename (file);
571   char *t = extract_filename (test);
572
573   char *filename = NULL;
574   char *dotexe;
575
576   if (e == NULL)
577     goto fail;
578
579   /* remove "lt-" */
580   filename = strstr (e, "tes");
581   if (filename == NULL)
582     goto fail;
583
584   /* remove ".exe" */
585   if (NULL != (dotexe = strstr (filename, ".exe")))
586     dotexe[0] = '\0';
587
588   /* find last _ */
589   filename = strstr (filename, t);
590   if (filename == NULL)
591     goto fail;
592
593   /* copy plugin */
594   filename += strlen (t);
595   filename++;
596   GNUNET_asprintf (dest, "%s", filename);
597   goto suc;
598
599 fail:
600   (*dest) = NULL;
601 suc:
602   GNUNET_free (t);
603   GNUNET_free (e);
604
605 }
606
607 /**
608  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
609  * if existing ".exe"-prefix and adds the peer-number
610  * @param file filename of the test, e.g. argv[0]
611  * @param cfgname where to write the result
612  * @param count peer number
613  */
614 void
615 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
616                                           int count)
617 {
618   char *filename = extract_filename (file);
619   char *backup = filename;
620   char *dotexe;
621
622   if (filename == NULL)
623     goto fail;
624
625   /* remove "lt-" */
626   filename = strstr (filename, "tes");
627   if (filename == NULL)
628     goto fail;
629
630   /* remove ".exe" */
631   if (NULL != (dotexe = strstr (filename, ".exe")))
632     dotexe[0] = '\0';
633
634   goto suc;
635
636 fail:
637   (*dest) = NULL;
638   return;
639
640 suc:
641   /* create cfg filename */
642   GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
643   GNUNET_free (backup);
644 }
645
646
647
648 /* end of transport_testing.h */