corrected HELLO_ACK message size
[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 HOSTKEYFILESIZE 914
31
32 static const char *
33 get_host_key (struct GNUNET_TRANSPORT_TESTING_handle *tth)
34 {
35   if (tth->hostkey_data == NULL)
36   {
37     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
38                      "No precomputed hostkeys available\n");
39     return NULL;
40   }
41   if (tth->hostkeys_total > tth->hostkeys_last)
42   {
43     tth->hostkeys_last++;
44     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
45                      "Used hostkey %u of %u available hostkeys\n",
46                      tth->hostkeys_last, tth->hostkeys_total);
47     return &tth->hostkey_data[(tth->hostkeys_last - 1) * HOSTKEYFILESIZE];
48   }
49   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
50                    "No hostkey available (%u of %u already used)\n",
51                    tth->hostkeys_last, tth->hostkeys_total);
52   return NULL;
53 }
54
55 static struct PeerContext *
56 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
57                    const struct GNUNET_PeerIdentity *peer)
58 {
59   GNUNET_assert (tth != NULL);
60   struct PeerContext *t = tth->p_head;
61
62   while (t != NULL)
63   {
64     if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
65       break;
66     t = t->next;
67   }
68
69   return t;
70 }
71
72 struct ConnectingContext *
73 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
74                          struct PeerContext *p1, struct PeerContext *p2)
75 {
76   GNUNET_assert (tth != NULL);
77   struct ConnectingContext *cc = tth->cc_head;
78
79   while (cc != NULL)
80   {
81     if ((cc->p1 == p1) && (cc->p2 == p2))
82       break;
83     if ((cc->p1 == p2) && (cc->p2 == p1))
84       break;
85     cc = cc->next;
86   }
87
88   return cc;
89 }
90
91 static void
92 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
93                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
94 {
95   struct PeerContext *p = cls;
96
97   /* Find PeerContext */
98   GNUNET_assert (p != 0);
99   GNUNET_assert (p->tth != NULL);
100   struct PeerContext *p2 = find_peer_context (p->tth, peer);
101
102   if (p == NULL)
103     return;
104   if (p->nc != NULL)
105     p->nc (p->cb_cls, peer, ats, ats_count);
106
107   char *p2_s;
108
109   if (p2 != NULL)
110     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
111   else
112     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
113   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
114                    "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
115                    GNUNET_i2s (&p->id));
116   GNUNET_free (p2_s);
117
118   /* Find ConnectingContext */
119   struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
120
121   if (cc == NULL)
122     return;
123
124   if (p == cc->p1)
125     cc->p1_c = GNUNET_YES;
126
127   if (p == cc->p2)
128     cc->p2_c = GNUNET_YES;
129
130   if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
131   {
132     cc->cb (cc->p1, cc->p2, cc->cb_cls);
133     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
134   }
135 }
136
137 static void
138 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
139 {
140   struct PeerContext *p = cls;
141
142   /* Find PeerContext */
143   int no = 0;
144   struct PeerContext *p2 = NULL;
145
146   if (p != NULL)
147   {
148     GNUNET_assert (p->tth != NULL);
149     p2 = find_peer_context (p->tth, peer);
150     no = p->no;
151   }
152
153   char *p2_s;
154
155   if (p2 != NULL)
156     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
157   else
158     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
159   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
160                    "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
161                    GNUNET_i2s (&p->id));
162   GNUNET_free (p2_s);
163
164   if (p == NULL)
165     return;
166   if (p->nd != NULL)
167     p->nd (p->cb_cls, peer);
168 }
169
170 static void
171 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
172                 const struct GNUNET_MessageHeader *message,
173                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
174 {
175   struct PeerContext *p = cls;
176
177   if (p == NULL)
178     return;
179   if (p->rec != NULL)
180     p->rec (p->cb_cls, peer, message, ats, ats_count);
181 }
182
183 static void
184 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
185 {
186   struct PeerContext *p = cb_cls;
187
188   GNUNET_assert (message != NULL);
189   GNUNET_assert (GNUNET_OK ==
190                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
191                                       message, &p->id));
192   GNUNET_free_non_null (p->hello);
193   p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
194
195   if (p->start_cb != NULL)
196   {
197     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
198                      "Peer %u (`%s') successfully started\n", p->no,
199                      GNUNET_i2s (&p->id));
200
201     p->start_cb (p, p->cb_cls);
202     p->start_cb = NULL;
203   }
204 }
205
206
207 static void
208 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 {
210   struct ConnectingContext *cc = cls;
211   struct PeerContext *p1 = cc->p1;
212   struct PeerContext *p2 = cc->p2;
213
214   cc->tct = GNUNET_SCHEDULER_NO_TASK;
215   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
216     return;
217
218   GNUNET_assert (cc != NULL);
219   GNUNET_assert (cc->p1 != NULL);
220   GNUNET_assert (cc->p2 != NULL);
221
222   char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
223
224   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
225                    "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
226                    p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
227                    GNUNET_HELLO_size (cc->p2->hello));
228   GNUNET_free (p2_s);
229
230   GNUNET_TRANSPORT_offer_hello (cc->th_p1,
231                                 (const struct GNUNET_MessageHeader *) cc->
232                                 p2->hello, NULL, NULL);
233   GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
234
235   cc->tct =
236       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
237 }
238
239
240 /**
241  * Start a peer with the given configuration
242  * @param tth the testing handle
243  * @param cfgname configuration file
244  * @param peer_id the peer_id
245  * @param rec receive callback
246  * @param nc connect callback
247  * @param nd disconnect callback
248  * @param start_cb start callback
249  * @param cb_cls closure for callback
250  * @return the peer context
251  */
252 struct PeerContext *
253 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
254                                      *tth, const char *cfgname, int peer_id,
255                                      GNUNET_TRANSPORT_ReceiveCallback rec,
256                                      GNUNET_TRANSPORT_NotifyConnect nc,
257                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
258                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
259                                      void *cb_cls)
260 {
261   const char *hostkey = NULL;
262   struct GNUNET_DISK_FileHandle *fn;
263
264   GNUNET_assert (tth != NULL);
265   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
266   {
267     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
268                      "File not found: `%s' \n", cfgname);
269     return NULL;
270   }
271
272   struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
273
274   p->cfg = GNUNET_CONFIGURATION_create ();
275   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
276
277   if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
278     GNUNET_assert (GNUNET_OK ==
279                    GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
280                                                           "SERVICEHOME",
281                                                           &p->servicehome));
282
283   if (NULL != p->servicehome)
284     GNUNET_DISK_directory_remove (p->servicehome);
285
286   hostkey = get_host_key (tth);
287   if (hostkey != NULL)
288   {
289
290     GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
291     GNUNET_assert (GNUNET_OK ==
292                    GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
293     fn = GNUNET_DISK_file_open (p->hostkeyfile,
294                                 GNUNET_DISK_OPEN_READWRITE |
295                                 GNUNET_DISK_OPEN_CREATE,
296                                 GNUNET_DISK_PERM_USER_READ |
297                                 GNUNET_DISK_PERM_USER_WRITE);
298     GNUNET_assert (fn != NULL);
299     GNUNET_assert (HOSTKEYFILESIZE ==
300                    GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
301     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
302     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
303                      "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
304   }
305
306   p->arm_proc =
307       GNUNET_OS_start_process (GNUNET_YES,
308                                NULL, NULL, "gnunet-service-arm",
309                                "gnunet-service-arm", "-c", cfgname,
310 #if VERBOSE_PEERS
311                                "-L", "DEBUG",
312 #else
313                                "-L", "ERROR",
314 #endif
315                                NULL);
316
317   p->no = peer_id;
318   p->tth = tth;
319   p->nc = nc;
320   p->nd = nd;
321   p->rec = rec;
322   p->start_cb = start_cb;
323   if (cb_cls != NULL)
324     p->cb_cls = cb_cls;
325   else
326     p->cb_cls = p;
327
328   p->th =
329       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
330                                 &notify_connect, &notify_disconnect);
331   GNUNET_assert (p->th != NULL);
332
333   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
334   GNUNET_assert (p->ghh != NULL);
335
336   GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
337   return p;
338 }
339
340 /**
341 * Restart the given peer
342 * @param tth testing handle
343 * @param p the peer
344 * @param cfgname the cfg file used to restart
345 * @param restart_cb callback to call when restarted
346 * @param cb_cls callback closure
347 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
348 */
349 int
350 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
351                                        *tth, struct PeerContext *p,
352                                        const char *cfgname,
353                                        GNUNET_TRANSPORT_TESTING_start_cb
354                                        restart_cb, void *cb_cls)
355 {
356   struct GNUNET_DISK_FileHandle *fn;
357
358   GNUNET_assert (tth != NULL);
359   GNUNET_assert (p != NULL);
360   GNUNET_assert (p->hostkeyfile != NULL);
361   GNUNET_assert (p->servicehome != NULL);
362
363   /* shutdown */
364 #if VERBOSE
365   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
366                    "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
367 #endif
368   if (p->ghh != NULL)
369     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
370   p->ghh = NULL;
371
372   if (p->th != NULL)
373     GNUNET_TRANSPORT_disconnect (p->th);
374
375   if (NULL != p->arm_proc)
376   {
377     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
378       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
379     GNUNET_OS_process_wait (p->arm_proc);
380     GNUNET_OS_process_close (p->arm_proc);
381     p->arm_proc = NULL;
382   }
383   if (p->hello != NULL)
384     GNUNET_free (p->hello);
385   p->hello = NULL;
386
387   if (p->cfg != NULL)
388     GNUNET_CONFIGURATION_destroy (p->cfg);
389   p->cfg = NULL;
390
391
392   /* start */
393 #if VERBOSE
394   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
395                    "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
396 #endif
397
398   sleep (5);                    // YUCK!
399
400   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
401   {
402     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
403                      "File not found: `%s' \n", cfgname);
404     goto fail;
405   }
406
407   p->cfg = GNUNET_CONFIGURATION_create ();
408   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
409
410   if (!GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
411     goto fail;
412
413   fn = GNUNET_DISK_file_open (p->hostkeyfile,
414                               GNUNET_DISK_OPEN_READWRITE |
415                               GNUNET_DISK_OPEN_CREATE,
416                               GNUNET_DISK_PERM_USER_READ |
417                               GNUNET_DISK_PERM_USER_WRITE);
418   if (fn == NULL)
419     goto fail;
420   if (GNUNET_OK != GNUNET_DISK_file_close (fn))
421     goto fail;
422
423   p->arm_proc =
424       GNUNET_OS_start_process (GNUNET_YES, 
425                                NULL, NULL, "gnunet-service-arm",
426                                "gnunet-service-arm", "-c", cfgname,
427 #if VERBOSE_PEERS
428                                "-L", "DEBUG",
429 #else
430                                "-L", "ERROR",
431 #endif
432                                NULL);
433
434   p->th =
435       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
436                                 &notify_connect, &notify_disconnect);
437   GNUNET_assert (p->th != NULL);
438
439   p->start_cb = restart_cb;
440   p->cb_cls = cb_cls;
441
442   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
443   GNUNET_assert (p->ghh != NULL);
444   return GNUNET_OK;
445
446 fail:
447   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
448                    "Restarting peer %u (`%s') failed, removing peer\n", p->no,
449                    GNUNET_i2s (&p->id));
450   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
451   return GNUNET_SYSERR;
452 }
453
454 /**
455  * shutdown the given peer
456  * @param tth testing handle
457  * @param p the peer
458  */
459 void
460 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
461                                     struct PeerContext *p)
462 {
463   GNUNET_assert (p != NULL);
464
465   if (p->ghh != NULL)
466     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
467   p->ghh = NULL;
468
469   if (p->th != NULL)
470     GNUNET_TRANSPORT_disconnect (p->th);
471
472   if (NULL != p->arm_proc)
473   {
474     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
475       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
476     GNUNET_OS_process_wait (p->arm_proc);
477     GNUNET_OS_process_close (p->arm_proc);
478     p->arm_proc = NULL;
479   }
480
481   if (p->hostkeyfile != NULL)
482   {
483     GNUNET_DISK_directory_remove (p->hostkeyfile);
484     GNUNET_free (p->hostkeyfile);
485   }
486
487   if (p->servicehome != NULL)
488   {
489     GNUNET_DISK_directory_remove (p->servicehome);
490     GNUNET_free (p->servicehome);
491   }
492
493   if (p->hello != NULL)
494     GNUNET_free (p->hello);
495   p->hello = NULL;
496
497   if (p->cfg != NULL)
498     GNUNET_CONFIGURATION_destroy (p->cfg);
499   p->cfg = NULL;
500
501   GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
502
503   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
504                    "Peer %u (`%s') stopped \n", p->no,
505                    GNUNET_i2s (&p->id));
506
507   GNUNET_free (p);
508   p = NULL;
509 }
510
511 /**
512  * Connect the given peers and call the callback when both peers report the
513  * inbound connection. Remarks: start_peer's notify_connect callback can be called
514  * before.
515  *
516  * @param tth transport testing handle
517  * @param p1 peer 1
518  * @param p2 peer 2
519  * @param cb the callback to call when both peers notified that they are connected
520  * @param cls callback cls
521  * @return a connect request handle
522  */
523 GNUNET_TRANSPORT_TESTING_ConnectRequest
524 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
525                                         struct PeerContext *p1,
526                                         struct PeerContext *p2,
527                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
528                                         void *cls)
529 {
530   GNUNET_assert (tth != NULL);
531
532   struct ConnectingContext *cc =
533       GNUNET_malloc (sizeof (struct ConnectingContext));
534
535   GNUNET_assert (p1 != NULL);
536   GNUNET_assert (p2 != NULL);
537
538   cc->p1 = p1;
539   cc->p2 = p2;
540
541   cc->cb = cb;
542   if (cls != NULL)
543     cc->cb_cls = cls;
544   else
545     cc->cb_cls = cc;
546
547   cc->th_p1 = p1->th;
548   cc->th_p2 = p2->th;
549
550   GNUNET_assert (cc->th_p1 != NULL);
551   GNUNET_assert (cc->th_p2 != NULL);
552
553   GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
554
555   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
556   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
557                    "New connect request %X\n", cc);
558
559   return cc;
560 }
561
562 /**
563  * Cancel the request to connect two peers
564  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
565  *
566  * @param tth transport testing handle
567  * @param ccr a connect request handle
568  */
569 void
570 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
571                                                GNUNET_TRANSPORT_TESTING_handle
572                                                *tth,
573                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
574                                                ccr)
575 {
576   struct ConnectingContext *cc = ccr;
577
578   GNUNET_assert (tth != NULL);
579
580   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
581                    "Canceling connect request %X!\n", cc);
582
583   if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
584     GNUNET_SCHEDULER_cancel (cc->tct);
585   cc->tct = GNUNET_SCHEDULER_NO_TASK;
586
587   GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
588   GNUNET_free (cc);
589 }
590
591
592 /**
593  * Clean up the transport testing
594  * @param tth transport testing handle
595  */
596 void
597 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
598 {
599   struct ConnectingContext *cc = tth->cc_head;
600   struct ConnectingContext *ct = NULL;
601   struct PeerContext *p = tth->p_head;
602   struct PeerContext *t = NULL;
603
604   GNUNET_assert (tth != NULL);
605
606   while (cc != tth->cc_tail)
607   {
608     ct = cc->next;
609     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
610                      "Developer forgot to cancel connect request %X!\n", cc);
611     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
612     cc = ct;
613   }
614
615   while (p != NULL)
616   {
617     t = p->next;
618     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
619                      "Developer forgot to stop peer!\n");
620     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
621     p = t;
622   }
623
624   GNUNET_free_non_null (tth->hostkey_data);
625
626   GNUNET_free (tth);
627   tth = NULL;
628 }
629
630 /**
631  * Initialize the transport testing
632  * @return transport testing handle
633  */
634 struct GNUNET_TRANSPORT_TESTING_handle *
635 GNUNET_TRANSPORT_TESTING_init ()
636 {
637   struct GNUNET_TRANSPORT_TESTING_handle *tth;
638   struct GNUNET_DISK_FileHandle *fd;
639   uint64_t fs;
640   uint64_t total_hostkeys;
641
642
643   /* prepare hostkeys */
644   tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
645   tth->hostkey_data = NULL;
646   const char *hostkeys_file = "../../contrib/testing_hostkeys.dat";
647
648   if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
649   {
650     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file!\n"));
651   }
652   else
653   {
654     /* Check hostkey file size, read entire thing into memory */
655     fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
656                                 GNUNET_DISK_PERM_NONE);
657     if (NULL == fd)
658     {
659       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
660       GNUNET_free (tth);
661       return NULL;
662     }
663
664     if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
665       fs = 0;
666
667     if (0 != (fs % HOSTKEYFILESIZE))
668     {
669       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
670                        "File size %llu seems incorrect for hostkeys...\n", fs);
671     }
672     else
673     {
674       total_hostkeys = fs / HOSTKEYFILESIZE;
675       tth->hostkey_data = GNUNET_malloc_large (fs);
676       GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
677       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
678                        "Read %llu hostkeys from file\n", total_hostkeys);
679       tth->hostkeys_total = total_hostkeys;
680     }
681     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
682   }
683
684   return tth;
685 }
686
687
688 /*
689  * Some utility functions
690  */
691
692 /**
693  * Removes all directory separators from absolute filename
694  * @param file the absolute file name, e.g. as found in argv[0]
695  * @return extracted file name, has to be freed by caller
696  */
697 static char *
698 extract_filename (const char *file)
699 {
700   char *pch = GNUNET_strdup (file);
701   char *backup = pch;
702   char *filename = NULL;
703   char *res;
704
705   if (NULL != strstr (pch, "/"))
706   {
707     pch = strtok (pch, "/");
708     while (pch != NULL)
709     {
710       pch = strtok (NULL, "/");
711       if (pch != NULL)
712       {
713         filename = pch;
714       }
715     }
716   }
717   else
718     filename = pch;
719
720   res = GNUNET_strdup (filename);
721   GNUNET_free (backup);
722   return res;
723 }
724
725 /**
726  * Extracts the test filename from an absolute file name and removes the extension
727  * @param file absolute file name
728  * @param dest where to store result
729  */
730 void
731 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
732 {
733   char *filename = extract_filename (file);
734   char *backup = filename;
735   char *dotexe;
736
737   if (filename == NULL)
738     goto fail;
739
740   /* remove "lt-" */
741   filename = strstr (filename, "tes");
742   if (filename == NULL)
743     goto fail;
744
745   /* remove ".exe" */
746   if (NULL != (dotexe = strstr (filename, ".exe")))
747     dotexe[0] = '\0';
748
749   goto suc;
750
751 fail:
752   (*dest) = NULL;
753   return;
754
755 suc:
756   /* create filename */
757   GNUNET_asprintf (dest, "%s", filename);
758   GNUNET_free (backup);
759 }
760
761
762 /**
763  * Extracts the filename from an absolute file name and removes the extension
764  * @param file absolute file name
765  * @param dest where to store result
766  */
767 void
768 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
769 {
770   char *src = extract_filename (file);
771   char *split;
772
773   split = strstr (src, ".");
774   if (split != NULL)
775   {
776     split[0] = '\0';
777   }
778   GNUNET_asprintf (dest, "%s", src);
779   GNUNET_free (src);
780 }
781
782
783 /**
784  * Extracts the plugin anme from an absolute file name and the test name
785  * @param file absolute file name
786  * @param test test name
787  * @param dest where to store result
788  */
789 void
790 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
791                                                const char *test, char **dest)
792 {
793   char *e = extract_filename (file);
794   char *t = extract_filename (test);
795
796   char *filename = NULL;
797   char *dotexe;
798
799   if (e == NULL)
800     goto fail;
801
802   /* remove "lt-" */
803   filename = strstr (e, "tes");
804   if (filename == NULL)
805     goto fail;
806
807   /* remove ".exe" */
808   if (NULL != (dotexe = strstr (filename, ".exe")))
809     dotexe[0] = '\0';
810
811   /* find last _ */
812   filename = strstr (filename, t);
813   if (filename == NULL)
814     goto fail;
815
816   /* copy plugin */
817   filename += strlen (t);
818   filename++;
819   GNUNET_asprintf (dest, "%s", filename);
820   goto suc;
821
822 fail:
823   (*dest) = NULL;
824 suc:
825   GNUNET_free (t);
826   GNUNET_free (e);
827
828 }
829
830 /**
831  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
832  * if existing ".exe"-prefix and adds the peer-number
833  *
834  * @param file filename of the test, e.g. argv[0]
835  * @param dest where to write the filename
836  * @param count peer number
837  */
838 void
839 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
840                                           int count)
841 {
842   char *filename = extract_filename (file);
843   char *backup = filename;
844   char *dotexe;
845
846   if (filename == NULL)
847     goto fail;
848
849   /* remove "lt-" */
850   filename = strstr (filename, "tes");
851   if (filename == NULL)
852     goto fail;
853
854   /* remove ".exe" */
855   if (NULL != (dotexe = strstr (filename, ".exe")))
856     dotexe[0] = '\0';
857
858   goto suc;
859
860 fail:
861   (*dest) = NULL;
862   return;
863
864 suc:
865   /* create cfg filename */
866   GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
867   GNUNET_free (backup);
868 }
869
870
871
872 /* end of transport_testing.h */