3ef6e4b21dfec7e21c11046c94f758a04703c79c
[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 (NULL, NULL, "gnunet-service-arm",
308                                "gnunet-service-arm", "-c", cfgname,
309 #if VERBOSE_PEERS
310                                "-L", "DEBUG",
311 #else
312                                "-L", "ERROR",
313 #endif
314                                NULL);
315
316   p->no = peer_id;
317   p->tth = tth;
318   p->nc = nc;
319   p->nd = nd;
320   p->rec = rec;
321   p->start_cb = start_cb;
322   if (cb_cls != NULL)
323     p->cb_cls = cb_cls;
324   else
325     p->cb_cls = p;
326
327   p->th =
328       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
329                                 &notify_connect, &notify_disconnect);
330   GNUNET_assert (p->th != NULL);
331
332   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
333   GNUNET_assert (p->ghh != NULL);
334
335   GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
336   return p;
337 }
338
339 /**
340 * Restart the given peer
341 * @param tth testing handle
342 * @param p the peer
343 * @param cfgname the cfg file used to restart
344 * @param restart_cb callback to call when restarted
345 * @param cb_cls callback closure
346 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
347 */
348 int
349 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
350                                        *tth, struct PeerContext *p,
351                                        const char *cfgname,
352                                        GNUNET_TRANSPORT_TESTING_start_cb
353                                        restart_cb, void *cb_cls)
354 {
355   struct GNUNET_DISK_FileHandle *fn;
356
357   GNUNET_assert (tth != NULL);
358   GNUNET_assert (p != NULL);
359   GNUNET_assert (p->hostkeyfile != NULL);
360   GNUNET_assert (p->servicehome != NULL);
361
362   /* shutdown */
363 #if VERBOSE
364   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
365                    "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
366 #endif
367   if (p->ghh != NULL)
368     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
369   p->ghh = NULL;
370
371   if (p->th != NULL)
372     GNUNET_TRANSPORT_disconnect (p->th);
373
374   if (NULL != p->arm_proc)
375   {
376     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
377       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
378     GNUNET_OS_process_wait (p->arm_proc);
379     GNUNET_OS_process_close (p->arm_proc);
380     p->arm_proc = NULL;
381   }
382   if (p->hello != NULL)
383     GNUNET_free (p->hello);
384   p->hello = NULL;
385
386   if (p->cfg != NULL)
387     GNUNET_CONFIGURATION_destroy (p->cfg);
388   p->cfg = NULL;
389
390
391   /* start */
392 #if VERBOSE
393   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
394                    "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
395 #endif
396
397   sleep (5);                    // YUCK!
398
399   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
400   {
401     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
402                      "File not found: `%s' \n", cfgname);
403     goto fail;
404   }
405
406   p->cfg = GNUNET_CONFIGURATION_create ();
407   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
408
409   if (!GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
410     goto fail;
411
412   fn = GNUNET_DISK_file_open (p->hostkeyfile,
413                               GNUNET_DISK_OPEN_READWRITE |
414                               GNUNET_DISK_OPEN_CREATE,
415                               GNUNET_DISK_PERM_USER_READ |
416                               GNUNET_DISK_PERM_USER_WRITE);
417   if (fn == NULL)
418     goto fail;
419   if (GNUNET_OK != GNUNET_DISK_file_close (fn))
420     goto fail;
421
422   p->arm_proc =
423       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
424                                "gnunet-service-arm", "-c", cfgname,
425 #if VERBOSE_PEERS
426                                "-L", "DEBUG",
427 #else
428                                "-L", "ERROR",
429 #endif
430                                NULL);
431
432   p->th =
433       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
434                                 &notify_connect, &notify_disconnect);
435   GNUNET_assert (p->th != NULL);
436
437   p->start_cb = restart_cb;
438   p->cb_cls = cb_cls;
439
440   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
441   GNUNET_assert (p->ghh != NULL);
442   return GNUNET_OK;
443
444 fail:
445   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
446                    "Restarting peer %u (`%s') failed, removing peer\n", p->no,
447                    GNUNET_i2s (&p->id));
448   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
449   return GNUNET_SYSERR;
450 }
451
452 /**
453  * shutdown the given peer
454  * @param tth testing handle
455  * @param p the peer
456  */
457 void
458 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
459                                     struct PeerContext *p)
460 {
461   GNUNET_assert (p != NULL);
462
463   if (p->ghh != NULL)
464     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
465   p->ghh = NULL;
466
467   if (p->th != NULL)
468     GNUNET_TRANSPORT_disconnect (p->th);
469
470   if (NULL != p->arm_proc)
471   {
472     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
473       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
474     GNUNET_OS_process_wait (p->arm_proc);
475     GNUNET_OS_process_close (p->arm_proc);
476     p->arm_proc = NULL;
477   }
478
479   if (p->hostkeyfile != NULL)
480   {
481     GNUNET_DISK_directory_remove (p->hostkeyfile);
482     GNUNET_free (p->hostkeyfile);
483   }
484
485   if (p->servicehome != NULL)
486   {
487     GNUNET_DISK_directory_remove (p->servicehome);
488     GNUNET_free (p->servicehome);
489   }
490
491   if (p->hello != NULL)
492     GNUNET_free (p->hello);
493   p->hello = NULL;
494
495   if (p->cfg != NULL)
496     GNUNET_CONFIGURATION_destroy (p->cfg);
497   p->cfg = NULL;
498
499   GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
500
501   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
502                    "Peer %u (`%s') stopped \n", p->no,
503                    GNUNET_i2s (&p->id));
504
505   GNUNET_free (p);
506   p = NULL;
507 }
508
509 /**
510  * Connect the given peers and call the callback when both peers report the
511  * inbound connection. Remarks: start_peer's notify_connect callback can be called
512  * before.
513  *
514  * @param tth transport testing handle
515  * @param p1 peer 1
516  * @param p2 peer 2
517  * @param cb the callback to call when both peers notified that they are connected
518  * @param cls callback cls
519  * @return a connect request handle
520  */
521 GNUNET_TRANSPORT_TESTING_ConnectRequest
522 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
523                                         struct PeerContext *p1,
524                                         struct PeerContext *p2,
525                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
526                                         void *cls)
527 {
528   GNUNET_assert (tth != NULL);
529
530   struct ConnectingContext *cc =
531       GNUNET_malloc (sizeof (struct ConnectingContext));
532
533   GNUNET_assert (p1 != NULL);
534   GNUNET_assert (p2 != NULL);
535
536   cc->p1 = p1;
537   cc->p2 = p2;
538
539   cc->cb = cb;
540   if (cls != NULL)
541     cc->cb_cls = cls;
542   else
543     cc->cb_cls = cc;
544
545   cc->th_p1 = p1->th;
546   cc->th_p2 = p2->th;
547
548   GNUNET_assert (cc->th_p1 != NULL);
549   GNUNET_assert (cc->th_p2 != NULL);
550
551   GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
552
553   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
554   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
555                    "New connect request %X\n", cc);
556
557   return cc;
558 }
559
560 /**
561  * Cancel the request to connect two peers
562  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
563  *
564  * @param tth transport testing handle
565  * @param ccr a connect request handle
566  */
567 void
568 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
569                                                GNUNET_TRANSPORT_TESTING_handle
570                                                *tth,
571                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
572                                                ccr)
573 {
574   struct ConnectingContext *cc = ccr;
575
576   GNUNET_assert (tth != NULL);
577
578   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
579                    "Canceling connect request %X!\n", cc);
580
581   if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
582     GNUNET_SCHEDULER_cancel (cc->tct);
583   cc->tct = GNUNET_SCHEDULER_NO_TASK;
584
585   GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
586   GNUNET_free (cc);
587 }
588
589
590 /**
591  * Clean up the transport testing
592  * @param tth transport testing handle
593  */
594 void
595 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
596 {
597   struct ConnectingContext *cc = tth->cc_head;
598   struct ConnectingContext *ct = NULL;
599   struct PeerContext *p = tth->p_head;
600   struct PeerContext *t = NULL;
601
602   GNUNET_assert (tth != NULL);
603
604   while (cc != tth->cc_tail)
605   {
606     ct = cc->next;
607     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
608                      "Developer forgot to cancel connect request %X!\n", cc);
609     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
610     cc = ct;
611   }
612
613   while (p != NULL)
614   {
615     t = p->next;
616     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
617                      "Developer forgot to stop peer!\n");
618     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
619     p = t;
620   }
621
622   GNUNET_free_non_null (tth->hostkey_data);
623
624   GNUNET_free (tth);
625   tth = NULL;
626 }
627
628 /**
629  * Initialize the transport testing
630  * @return transport testing handle
631  */
632 struct GNUNET_TRANSPORT_TESTING_handle *
633 GNUNET_TRANSPORT_TESTING_init ()
634 {
635   struct GNUNET_TRANSPORT_TESTING_handle *tth;
636   struct GNUNET_DISK_FileHandle *fd;
637   uint64_t fs;
638   uint64_t total_hostkeys;
639
640
641   /* prepare hostkeys */
642   tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
643   tth->hostkey_data = NULL;
644   const char *hostkeys_file = "../../contrib/testing_hostkeys.dat";
645
646   if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file!\n"));
649   }
650   else
651   {
652     /* Check hostkey file size, read entire thing into memory */
653     fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
654                                 GNUNET_DISK_PERM_NONE);
655     if (NULL == fd)
656     {
657       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
658       GNUNET_free (tth);
659       return NULL;
660     }
661
662     if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
663       fs = 0;
664
665     if (0 != (fs % HOSTKEYFILESIZE))
666     {
667       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
668                        "File size %llu seems incorrect for hostkeys...\n", fs);
669     }
670     else
671     {
672       total_hostkeys = fs / HOSTKEYFILESIZE;
673       tth->hostkey_data = GNUNET_malloc_large (fs);
674       GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
675       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
676                        "Read %llu hostkeys from file\n", total_hostkeys);
677       tth->hostkeys_total = total_hostkeys;
678     }
679     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
680   }
681
682   return tth;
683 }
684
685
686 /*
687  * Some utility functions
688  */
689
690 /**
691  * Removes all directory separators from absolute filename
692  * @param file the absolute file name, e.g. as found in argv[0]
693  * @return extracted file name, has to be freed by caller
694  */
695 static char *
696 extract_filename (const char *file)
697 {
698   char *pch = GNUNET_strdup (file);
699   char *backup = pch;
700   char *filename = NULL;
701   char *res;
702
703   if (NULL != strstr (pch, "/"))
704   {
705     pch = strtok (pch, "/");
706     while (pch != NULL)
707     {
708       pch = strtok (NULL, "/");
709       if (pch != NULL)
710       {
711         filename = pch;
712       }
713     }
714   }
715   else
716     filename = pch;
717
718   res = GNUNET_strdup (filename);
719   GNUNET_free (backup);
720   return res;
721 }
722
723 /**
724  * Extracts the test filename from an absolute file name and removes the extension
725  * @param file absolute file name
726  * @param dest where to store result
727  */
728 void
729 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
730 {
731   char *filename = extract_filename (file);
732   char *backup = filename;
733   char *dotexe;
734
735   if (filename == NULL)
736     goto fail;
737
738   /* remove "lt-" */
739   filename = strstr (filename, "tes");
740   if (filename == NULL)
741     goto fail;
742
743   /* remove ".exe" */
744   if (NULL != (dotexe = strstr (filename, ".exe")))
745     dotexe[0] = '\0';
746
747   goto suc;
748
749 fail:
750   (*dest) = NULL;
751   return;
752
753 suc:
754   /* create filename */
755   GNUNET_asprintf (dest, "%s", filename);
756   GNUNET_free (backup);
757 }
758
759
760 /**
761  * Extracts the filename from an absolute file name and removes the extension
762  * @param file absolute file name
763  * @param dest where to store result
764  */
765 void
766 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
767 {
768   char *src = extract_filename (file);
769   char *split;
770
771   split = strstr (src, ".");
772   if (split != NULL)
773   {
774     split[0] = '\0';
775   }
776   GNUNET_asprintf (dest, "%s", src);
777   GNUNET_free (src);
778 }
779
780
781 /**
782  * Extracts the plugin anme from an absolute file name and the test name
783  * @param file absolute file name
784  * @param test test name
785  * @param dest where to store result
786  */
787 void
788 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
789                                                const char *test, char **dest)
790 {
791   char *e = extract_filename (file);
792   char *t = extract_filename (test);
793
794   char *filename = NULL;
795   char *dotexe;
796
797   if (e == NULL)
798     goto fail;
799
800   /* remove "lt-" */
801   filename = strstr (e, "tes");
802   if (filename == NULL)
803     goto fail;
804
805   /* remove ".exe" */
806   if (NULL != (dotexe = strstr (filename, ".exe")))
807     dotexe[0] = '\0';
808
809   /* find last _ */
810   filename = strstr (filename, t);
811   if (filename == NULL)
812     goto fail;
813
814   /* copy plugin */
815   filename += strlen (t);
816   filename++;
817   GNUNET_asprintf (dest, "%s", filename);
818   goto suc;
819
820 fail:
821   (*dest) = NULL;
822 suc:
823   GNUNET_free (t);
824   GNUNET_free (e);
825
826 }
827
828 /**
829  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
830  * if existing ".exe"-prefix and adds the peer-number
831  *
832  * @param file filename of the test, e.g. argv[0]
833  * @param dest where to write the filename
834  * @param count peer number
835  */
836 void
837 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
838                                           int count)
839 {
840   char *filename = extract_filename (file);
841   char *backup = filename;
842   char *dotexe;
843
844   if (filename == NULL)
845     goto fail;
846
847   /* remove "lt-" */
848   filename = strstr (filename, "tes");
849   if (filename == NULL)
850     goto fail;
851
852   /* remove ".exe" */
853   if (NULL != (dotexe = strstr (filename, ".exe")))
854     dotexe[0] = '\0';
855
856   goto suc;
857
858 fail:
859   (*dest) = NULL;
860   return;
861
862 suc:
863   /* create cfg filename */
864   GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
865   GNUNET_free (backup);
866 }
867
868
869
870 /* end of transport_testing.h */