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