e459532392bf1ebde4623fc22ed2bd584a6847b7
[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 #if VERBOSE
108   char *p2_s;
109
110   if (p2 != NULL)
111     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
112   else
113     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
114   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
115                    "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
116                    GNUNET_i2s (&p->id));
117   GNUNET_free (p2_s);
118 #endif
119
120
121   /* Find ConnectingContext */
122   struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
123
124   if (cc == NULL)
125     return;
126
127   if (p == cc->p1)
128     cc->p1_c = GNUNET_YES;
129
130   if (p == cc->p2)
131     cc->p2_c = GNUNET_YES;
132
133   if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
134   {
135     cc->cb (cc->p1, cc->p2, cc->cb_cls);
136     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
137   }
138 }
139
140 static void
141 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
142 {
143   struct PeerContext *p = cls;
144
145   /* Find PeerContext */
146   int no = 0;
147   struct PeerContext *p2 = NULL;
148
149   if (p != NULL)
150   {
151     GNUNET_assert (p->tth != NULL);
152     p2 = find_peer_context (p->tth, peer);
153     no = p->no;
154   }
155
156   char *p2_s;
157
158   if (p2 != NULL)
159     GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
160   else
161     GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
162   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
163                    "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
164                    GNUNET_i2s (&p->id));
165   GNUNET_free (p2_s);
166
167   if (p == NULL)
168     return;
169   if (p->nd != NULL)
170     p->nd (p->cb_cls, peer);
171 }
172
173 static void
174 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
175                 const struct GNUNET_MessageHeader *message,
176                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
177 {
178   struct PeerContext *p = cls;
179
180   if (p == NULL)
181     return;
182   if (p->rec != NULL)
183     p->rec (p->cb_cls, peer, message, ats, ats_count);
184 }
185
186 static void
187 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
188 {
189   struct PeerContext *p = cb_cls;
190
191   GNUNET_assert (message != NULL);
192   GNUNET_assert (GNUNET_OK ==
193                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
194                                       message, &p->id));
195 #if VERBOSE
196   size_t size =
197       GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
198 #endif
199   GNUNET_free_non_null (p->hello);
200   p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
201
202 #if VERBOSE
203   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
204                    "New HELLO for peer %u (`%s') with size %u\n", p->no,
205                    GNUNET_i2s (&p->id), size);
206 #endif
207
208   if (p->start_cb != NULL)
209   {
210 #if VERBOSE
211     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
212                      "Peer %u (`%s') successfully started\n", p->no,
213                      GNUNET_i2s (&p->id));
214 #endif
215     p->start_cb (p, p->cb_cls);
216     p->start_cb = NULL;
217   }
218 }
219
220
221 static void
222 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 {
224   struct ConnectingContext *cc = cls;
225   struct PeerContext *p1 = cc->p1;
226   struct PeerContext *p2 = cc->p2;
227
228   cc->tct = GNUNET_SCHEDULER_NO_TASK;
229   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
230     return;
231
232   GNUNET_assert (cc != NULL);
233   GNUNET_assert (cc->p1 != NULL);
234   GNUNET_assert (cc->p2 != NULL);
235
236   char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
237
238   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
239                    "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
240                    p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
241                    GNUNET_HELLO_size (cc->p2->hello));
242   GNUNET_free (p2_s);
243
244   GNUNET_TRANSPORT_offer_hello (cc->th_p1,
245                                 (const struct GNUNET_MessageHeader *) cc->
246                                 p2->hello, NULL, NULL);
247   GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
248
249   cc->tct =
250       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
251 }
252
253
254 /**
255  * Start a peer with the given configuration
256  * @param rec receive callback
257  * @param nc connect callback
258  * @param nd disconnect callback
259  * @param cb_cls closure for callback
260  * @return the peer context
261  */
262 struct PeerContext *
263 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
264                                      *tth, const char *cfgname, int peer_id,
265                                      GNUNET_TRANSPORT_ReceiveCallback rec,
266                                      GNUNET_TRANSPORT_NotifyConnect nc,
267                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
268                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
269                                      void *cb_cls)
270 {
271   const char *hostkey = NULL;
272   struct GNUNET_DISK_FileHandle *fn;
273
274   GNUNET_assert (tth != NULL);
275   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
276   {
277     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
278                      "File not found: `%s' \n", cfgname);
279     return NULL;
280   }
281
282   struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
283
284   p->cfg = GNUNET_CONFIGURATION_create ();
285   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
286
287   if (GNUNET_CONFIGURATION_have_value (p->
288       cfg, "PATHS", "SERVICEHOME"))
289     GNUNET_assert (GNUNET_OK ==
290                    GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
291                                                           "SERVICEHOME",
292                                                           &p->servicehome));
293
294     if (NULL != p->servicehome)
295       GNUNET_DISK_directory_remove (p->servicehome);
296
297   hostkey = get_host_key(tth);
298   if (hostkey != NULL)
299   {
300
301     GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
302     GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
303     fn = GNUNET_DISK_file_open (p->hostkeyfile,
304                                 GNUNET_DISK_OPEN_READWRITE |
305                                 GNUNET_DISK_OPEN_CREATE,
306                                 GNUNET_DISK_PERM_USER_READ |
307                                 GNUNET_DISK_PERM_USER_WRITE);
308     GNUNET_assert (fn != NULL);
309     GNUNET_assert (HOSTKEYFILESIZE ==
310                    GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
311     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
312     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
313                      "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
314   }
315
316   p->arm_proc =
317       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
318                                "gnunet-service-arm", "-c", cfgname,
319 #if VERBOSE_PEERS
320                                "-L", "DEBUG",
321 #else
322                                "-L", "ERROR",
323 #endif
324                                NULL);
325
326   p->no = peer_id;
327   p->tth = tth;
328   p->nc = nc;
329   p->nd = nd;
330   p->rec = rec;
331   p->start_cb = start_cb;
332   if (cb_cls != NULL)
333     p->cb_cls = cb_cls;
334   else
335     p->cb_cls = p;
336
337   p->th =
338       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
339                                 &notify_connect, &notify_disconnect);
340   GNUNET_assert (p->th != NULL);
341
342   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
343   GNUNET_assert (p->ghh != NULL);
344
345   GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
346
347   return p;
348 }
349
350 /**
351 * Restart the given peer
352 * @param tth testing handle
353 * @param p the peer
354 * @param cfgname the cfg file used to restart
355 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
356 */
357 int
358 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
359                                        struct PeerContext *p,
360                                        const char *cfgname,
361                                        GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
362                                        void *cb_cls)
363 {
364   struct GNUNET_DISK_FileHandle *fn;
365
366   GNUNET_assert (tth != NULL);
367   GNUNET_assert (p != NULL);
368   GNUNET_assert (p->hostkeyfile != NULL);
369   GNUNET_assert (p->servicehome != NULL);
370
371   /* shutdown */
372 #if VERBOSE
373     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
374                      "Stopping peer %u (`%s')\n", p->no,
375                      GNUNET_i2s (&p->id));
376 #endif
377   if (p->ghh != NULL)
378     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
379   p->ghh = NULL;
380
381   if (p->th != NULL)
382     GNUNET_TRANSPORT_disconnect (p->th);
383
384   if (NULL != p->arm_proc)
385   {
386     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
387       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
388     GNUNET_OS_process_wait (p->arm_proc);
389     GNUNET_OS_process_close (p->arm_proc);
390     p->arm_proc = NULL;
391   }
392   if (p->hello != NULL)
393     GNUNET_free (p->hello);
394   p->hello = NULL;
395
396   if (p->cfg != NULL)
397     GNUNET_CONFIGURATION_destroy (p->cfg);
398   p->cfg = NULL;
399
400
401   /* start */
402 #if VERBOSE
403     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
404                      "Restarting peer %u (`%s')\n", p->no,
405                      GNUNET_i2s (&p->id));
406 #endif
407
408   sleep (5); // YUCK!
409
410   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
411   {
412     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
413                      "File not found: `%s' \n", cfgname);
414     goto fail;
415   }
416
417   p->cfg = GNUNET_CONFIGURATION_create ();
418   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
419
420   if (! GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))  
421     goto fail;
422
423   fn = GNUNET_DISK_file_open (p->hostkeyfile,
424                               GNUNET_DISK_OPEN_READWRITE |
425                               GNUNET_DISK_OPEN_CREATE,
426                               GNUNET_DISK_PERM_USER_READ |
427                               GNUNET_DISK_PERM_USER_WRITE);
428   if (fn == NULL)
429    goto fail;
430   if (GNUNET_OK != GNUNET_DISK_file_close (fn))
431    goto fail;
432
433   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
434                               "gnunet-service-arm", "-c", cfgname,
435   #if VERBOSE_PEERS
436                               "-L", "DEBUG",
437   #else
438                               "-L", "ERROR",
439   #endif
440                               NULL);
441
442   p->th =
443      GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
444                                &notify_connect, &notify_disconnect);
445   GNUNET_assert (p->th != NULL);
446
447   p->start_cb = restart_cb;
448   p->cb_cls = cb_cls;
449
450   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
451   GNUNET_assert (p->ghh != NULL);
452   return GNUNET_OK;
453
454  fail:
455   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
456                    "Restarting peer %u (`%s') failed, removing peer\n", p->no,
457                    GNUNET_i2s (&p->id));
458   GNUNET_TRANSPORT_TESTING_stop_peer (tth,p);  
459   return GNUNET_SYSERR;
460 }
461
462 /**
463  * shutdown the given peer
464  * @param p the peer
465  */
466 void
467 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
468                                     struct PeerContext *p)
469 {
470   GNUNET_assert (p != NULL);
471
472   if (p->ghh != NULL)
473     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
474   p->ghh = NULL;
475
476   if (p->th != NULL)
477     GNUNET_TRANSPORT_disconnect (p->th);
478
479   if (NULL != p->arm_proc)
480   {
481     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
482       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
483     GNUNET_OS_process_wait (p->arm_proc);
484     GNUNET_OS_process_close (p->arm_proc);
485     p->arm_proc = NULL;
486   }
487
488   if (p->hostkeyfile != NULL)
489   {
490     GNUNET_DISK_directory_remove (p->hostkeyfile);
491     GNUNET_free (p->hostkeyfile);
492   }
493
494   if (p->servicehome != NULL)
495   {
496     GNUNET_DISK_directory_remove (p->servicehome);
497     GNUNET_free (p->servicehome);
498   }
499
500   if (p->hello != NULL)
501     GNUNET_free (p->hello);
502   p->hello = NULL;
503
504   if (p->cfg != NULL)
505     GNUNET_CONFIGURATION_destroy (p->cfg);
506   p->cfg = NULL;
507
508   GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
509
510   GNUNET_free (p);
511   p = NULL;
512 }
513
514 /**
515  * Initiate peer p1 to connect to peer p2
516  * Get peer p2's HELLO and offer it to p1
517  * p1 then tries to connect to p2
518  * @param p1 peer 1
519  * @param p2 peer 2
520  * @param cb the callback to call when both peers notified that they are connected
521  * @param cb_cls callback cls (or a pointer to the
522  *        GNUNET_TRANSPORT_TESTING_ConnectRequest itself if null)
523  * @return connect context
524  */
525 GNUNET_TRANSPORT_TESTING_ConnectRequest
526 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
527                                         *tth, struct PeerContext *p1,
528                                         struct PeerContext *p2,
529                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
530                                         void *cb_cls)
531 {
532   GNUNET_assert (tth != NULL);
533
534   struct ConnectingContext *cc =
535       GNUNET_malloc (sizeof (struct ConnectingContext));
536
537   GNUNET_assert (p1 != NULL);
538   GNUNET_assert (p2 != NULL);
539
540   cc->p1 = p1;
541   cc->p2 = p2;
542
543   cc->cb = cb;
544   if (cb_cls != NULL)
545     cc->cb_cls = cb_cls;
546   else
547     cc->cb_cls = cc;
548
549   cc->th_p1 = p1->th;
550   cc->th_p2 = p2->th;
551
552   GNUNET_assert (cc->th_p1 != NULL);
553   GNUNET_assert (cc->th_p2 != NULL);
554
555   GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
556
557   cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
558   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
559                    "New connect request %X\n", cc);
560
561   return cc;
562 }
563
564 /**
565  * Cancel the request to connect two peers
566  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
567  * @param cc 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   if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
648   {
649     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
650                 _("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",
660                                 hostkeys_file);
661       GNUNET_free (tth);
662       return NULL;
663     }
664
665     if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
666       fs = 0;
667
668     if (0 != (fs % HOSTKEYFILESIZE))
669     {
670       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
671                   "File size %llu seems incorrect for hostkeys...\n", fs);
672     }
673     else
674     {
675       total_hostkeys = fs / HOSTKEYFILESIZE;
676       tth->hostkey_data = GNUNET_malloc_large (fs);
677       GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
678       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
679                   "Read %llu hostkeys from file\n", total_hostkeys);
680       tth->hostkeys_total = total_hostkeys;
681     }
682     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
683   }
684
685   return tth;
686 }
687
688
689 /*
690  * Some utility functions
691  */
692
693 /**
694  * Removes all directory separators from absolute filename
695  * @param file the absolute file name, e.g. as found in argv[0]
696  * @return extracted file name, has to be freed by caller
697  */
698 static char *
699 extract_filename (const char *file)
700 {
701   char *pch = GNUNET_strdup (file);
702   char *backup = pch;
703   char *filename = NULL;
704   char *res;
705
706   if (NULL != strstr (pch, "/"))
707   {
708     pch = strtok (pch, "/");
709     while (pch != NULL)
710     {
711       pch = strtok (NULL, "/");
712       if (pch != NULL)
713       {
714         filename = pch;
715       }
716     }
717   }
718   else
719     filename = pch;
720
721   res = GNUNET_strdup (filename);
722   GNUNET_free (backup);
723   return res;
724 }
725
726 /**
727  * Extracts the test filename from an absolute file name and removes the extension
728  * @param file absolute file name
729  * @param dest where to store result
730  */
731 void
732 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
733 {
734   char *filename = extract_filename (file);
735   char *backup = filename;
736   char *dotexe;
737
738   if (filename == NULL)
739     goto fail;
740
741   /* remove "lt-" */
742   filename = strstr (filename, "tes");
743   if (filename == NULL)
744     goto fail;
745
746   /* remove ".exe" */
747   if (NULL != (dotexe = strstr (filename, ".exe")))
748     dotexe[0] = '\0';
749
750   goto suc;
751
752 fail:
753   (*dest) = NULL;
754   return;
755
756 suc:
757   /* create filename */
758   GNUNET_asprintf (dest, "%s", filename);
759   GNUNET_free (backup);
760 }
761
762
763 /**
764  * Extracts the filename from an absolute file name and removes the extension
765  * @param file absolute file name
766  * @param dest where to store result
767  */
768 void
769 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
770 {
771   char *src = extract_filename (file);
772   char *split;
773
774   split = strstr (src, ".");
775   if (split != NULL)
776   {
777     split[0] = '\0';
778   }
779   GNUNET_asprintf (dest, "%s", src);
780   GNUNET_free (src);
781 }
782
783
784 /**
785  * Extracts the plugin anme from an absolute file name and the test name
786  * @param file absolute file name
787  * @param test test name
788  * @param dest where to store result
789  */
790 void
791 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
792                                                const char *test, char **dest)
793 {
794   char *e = extract_filename (file);
795   char *t = extract_filename (test);
796
797   char *filename = NULL;
798   char *dotexe;
799
800   if (e == NULL)
801     goto fail;
802
803   /* remove "lt-" */
804   filename = strstr (e, "tes");
805   if (filename == NULL)
806     goto fail;
807
808   /* remove ".exe" */
809   if (NULL != (dotexe = strstr (filename, ".exe")))
810     dotexe[0] = '\0';
811
812   /* find last _ */
813   filename = strstr (filename, t);
814   if (filename == NULL)
815     goto fail;
816
817   /* copy plugin */
818   filename += strlen (t);
819   filename++;
820   GNUNET_asprintf (dest, "%s", filename);
821   goto suc;
822
823 fail:
824   (*dest) = NULL;
825 suc:
826   GNUNET_free (t);
827   GNUNET_free (e);
828
829 }
830
831 /**
832  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
833  * if existing ".exe"-prefix and adds the peer-number
834  * @param file filename of the test, e.g. argv[0]
835  * @param cfgname where to write the result
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 */