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