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