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