07426ca739f2dd08863267c2507c072ca941aedc
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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  * @file transport/test_transport_api_reliability.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves as a base for tcp and http
25  * transport test cases to check that the transports
26  * achieve reliable message delivery.
27  */
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "gauger.h"
38 #include "transport.h"
39 #include "transport-testing.h"
40
41 #define VERBOSE GNUNET_NO
42
43 #define VERBOSE_ARM GNUNET_NO
44
45 #define START_ARM GNUNET_YES
46
47 /**
48  * Note that this value must not significantly exceed
49  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
50  * messages may be dropped even for a reliable transport.
51  */
52 #define TOTAL_MSGS (10000 * 2)
53
54 /**
55  * How long until we give up on transmitting the message?
56  */
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
58
59 #define MTYPE 12345
60
61 static struct PeerContext p1;
62
63 static struct PeerContext p2;
64
65 static int ok;
66
67 static int is_tcp;
68
69 static int is_tcp_nat;
70
71 static int is_http;
72
73 static int is_https;
74
75 static int is_udp;
76
77 static int is_unix;
78
79 static int is_wlan;
80
81 static int connected;
82
83 static unsigned long long total_bytes;
84
85 static struct GNUNET_TIME_Absolute start_time;
86
87 static GNUNET_SCHEDULER_TaskIdentifier die_task;
88
89 static GNUNET_SCHEDULER_TaskIdentifier tct;
90
91 struct GNUNET_TRANSPORT_TransmitHandle * th_p2;
92
93 static char * key_file_p1;
94 static char * cert_file_p1;
95
96 static char * key_file_p2;
97 static char * cert_file_p2;
98 static char *test_name;
99 static int msg_scheduled;
100 static int msg_sent;
101 static int msg_recv_expected;
102 static int msg_recv;
103
104 static int p1_hello_canceled;
105 static int p2_hello_canceled;
106
107 #if VERBOSE
108 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
109 #else
110 #define OKPP do { ok++; } while (0)
111 #endif
112
113
114 static void
115 end ()
116 {
117   unsigned long long delta;
118   char *value_name;
119
120   if (die_task != GNUNET_SCHEDULER_NO_TASK)
121     GNUNET_SCHEDULER_cancel (die_task);
122   die_task = GNUNET_SCHEDULER_NO_TASK;
123 #if VERBOSE
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
125 #endif
126
127   if (th_p2 != NULL)
128     GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
129   th_p2 = NULL;
130
131   GNUNET_TRANSPORT_disconnect (p1.th);
132   GNUNET_TRANSPORT_disconnect (p2.th);
133 #if VERBOSE
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135               "Transports disconnected, returning success!\n");
136 #endif
137   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
138   fprintf (stderr,
139            "\nThroughput was %llu kb/s\n",
140            total_bytes * 1000 / 1024 / delta);
141   GNUNET_asprintf(&value_name, "reliable_%s", test_name);
142   GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
143   GNUNET_free(value_name);
144   ok = 0;
145
146 }
147
148
149
150 static void
151 stop_arm (struct PeerContext *p)
152 {
153 #if START_ARM
154   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
155     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
156   GNUNET_OS_process_wait (p->arm_proc);
157   GNUNET_OS_process_close (p->arm_proc);
158   p->arm_proc = NULL;
159 #endif
160   GNUNET_CONFIGURATION_destroy (p->cfg);
161 }
162
163
164 static void
165 end_badly (void *cls,
166            const struct GNUNET_SCHEDULER_TaskContext *tc)
167 {
168   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169               "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
170               msg_sent, 
171               msg_scheduled, 
172               msg_recv, 
173               msg_recv_expected);
174   if (th_p2 != NULL)
175     GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
176   th_p2 = NULL;
177
178   GNUNET_break (0);
179   GNUNET_TRANSPORT_disconnect (p1.th);
180   GNUNET_TRANSPORT_disconnect (p2.th);
181   if (GNUNET_SCHEDULER_NO_TASK != tct)
182     {
183       GNUNET_SCHEDULER_cancel (tct);
184       tct = GNUNET_SCHEDULER_NO_TASK;
185     }
186   ok = 1;
187 }
188
189
190 struct TestMessage
191 {
192   struct GNUNET_MessageHeader header;
193   uint32_t num;
194 };
195
196
197 static unsigned int
198 get_size (unsigned int iter)
199 {
200   unsigned int ret;
201
202   if (iter < 60000)
203     return iter + sizeof (struct TestMessage);
204   ret = (iter * iter * iter);
205   return sizeof (struct TestMessage) + (ret % 60000);
206 }
207
208
209 static void
210 notify_receive (void *cls,
211                 const struct GNUNET_PeerIdentity *peer,
212                 const struct GNUNET_MessageHeader *message,
213                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
214                 uint32_t ats_count)
215 {
216   static int n;
217   unsigned int s;
218   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
219   const struct TestMessage *hdr;
220
221   hdr = (const struct TestMessage*) message;
222   s = get_size (n);
223   if (MTYPE != ntohs (message->type))
224     return;
225   msg_recv_expected = n;
226   msg_recv = ntohl(hdr->num);
227   if (ntohs (message->size) != s)
228     {
229       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                   "Expected message %u of size %u, got %u bytes of message %u\n",
231                   n, s,
232                   ntohs (message->size),
233                   ntohl (hdr->num));
234       if (die_task != GNUNET_SCHEDULER_NO_TASK)
235         GNUNET_SCHEDULER_cancel (die_task);
236       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
237       return;
238     }
239   if (ntohl (hdr->num) != n)
240     {
241       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242                   "Expected message %u of size %u, got %u bytes of message %u\n",
243                   n, s,
244                   ntohs (message->size),
245                   ntohl (hdr->num));
246       if (die_task != GNUNET_SCHEDULER_NO_TASK)
247         GNUNET_SCHEDULER_cancel (die_task);
248       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
249       return;
250     }
251   memset (cbuf, n, s - sizeof (struct TestMessage));
252   if (0 != memcmp (cbuf,
253                    &hdr[1],
254                    s - sizeof (struct TestMessage)))
255     {
256       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257                   "Expected message %u with bits %u, but body did not match\n",
258                   n, (unsigned char) n);
259       if (die_task != GNUNET_SCHEDULER_NO_TASK)
260         GNUNET_SCHEDULER_cancel (die_task);
261       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
262       return;
263     }
264 #if VERBOSE
265   if (ntohl(hdr->num) % 5000 == 0)
266     {
267       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268                   "Got message %u of size %u\n",
269                   ntohl (hdr->num),
270                   ntohs (message->size));
271     }
272 #endif
273   n++;
274   if (0 == (n % (TOTAL_MSGS/100)))
275     {
276       fprintf (stderr, ".");
277       if (die_task != GNUNET_SCHEDULER_NO_TASK)
278         GNUNET_SCHEDULER_cancel (die_task);
279       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
280                                                &end_badly,
281                                                NULL);
282     }
283   if (n == TOTAL_MSGS)
284     end ();
285 }
286
287
288 static size_t
289 notify_ready (void *cls, size_t size, void *buf)
290 {
291   static int n;
292   char *cbuf = buf;
293   struct TestMessage hdr;
294   unsigned int s;
295   unsigned int ret;
296
297   if (buf == NULL)
298     {
299       GNUNET_break (0);
300       ok = 42;
301       return 0;
302     }
303   th_p2 = NULL;
304   ret = 0;
305   s = get_size (n);
306   GNUNET_assert (size >= s);
307   GNUNET_assert (buf != NULL);
308   cbuf = buf;
309   do
310     {
311       hdr.header.size = htons (s);
312       hdr.header.type = htons (MTYPE);
313       hdr.num = htonl (n);
314       msg_sent = n;
315       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
316       ret += sizeof (struct TestMessage);
317       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
318       ret += s - sizeof (struct TestMessage);
319 #if VERBOSE
320       if (n % 5000 == 0)
321         {
322           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323                       "Sending message %u of size %u\n",
324                       n,
325                       s);
326         }
327 #endif
328       n++;
329       s = get_size (n);
330       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
331         break; /* sometimes pack buffer full, sometimes not */
332     }
333   while (size - ret >= s);
334   if (n < TOTAL_MSGS)
335   {
336     if (th_p2 == NULL)
337       th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
338                                             &p1.id,
339                                             s, 0, TIMEOUT,
340                                             &notify_ready,
341                                             NULL);
342     msg_scheduled = n;
343   }
344   if (n % 5000 == 0)
345     {
346       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347                   "Returning total message block of size %u\n",
348                   ret);
349     }
350   total_bytes += ret;
351   return ret;
352 }
353
354
355 static void
356 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
357 {
358   connected--;
359 #if VERBOSE
360   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361               "Peer `%4s' disconnected (%p)!\n",
362               GNUNET_i2s (peer), cls);
363 #endif
364   if (th_p2 != NULL)
365     {
366       GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
367       th-p2 = NULL;
368     }
369 }
370
371
372 static void
373 exchange_hello_last (void *cls,
374                      const struct GNUNET_MessageHeader *message)
375 {
376   struct PeerContext *me = cls;
377
378   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379               "Exchanging HELLO of size %d with peer (%s)!\n", 
380               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
381               GNUNET_i2s (&me->id));
382   GNUNET_assert (message != NULL);
383   GNUNET_assert (GNUNET_OK ==
384                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
385                                       message, &me->id));
386   GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
387 }
388
389
390
391 static void
392 exchange_hello (void *cls,
393                 const struct GNUNET_MessageHeader *message)
394 {
395   struct PeerContext *me = cls;
396
397   GNUNET_assert (message != NULL);
398   GNUNET_assert (GNUNET_OK ==
399                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
400                                       message, &me->id));
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402               "Exchanging HELLO of size %d from peer %s!\n", 
403               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
404               GNUNET_i2s (&me->id));
405   GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
406 }
407
408
409 static void
410 notify_connect (void *cls,
411                 const struct GNUNET_PeerIdentity *peer,
412                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
413                 uint32_t ats_count)
414 {
415   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416               "Peer `%4s' connected to us (%p)!\n", 
417               GNUNET_i2s (peer), 
418               cls);
419   connected++;
420   if (cls == &p1)
421     {
422       GNUNET_TRANSPORT_set_quota (p1.th,
423                                   &p2.id,
424                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
425                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
426       start_time = GNUNET_TIME_absolute_get ();
427     }
428   else
429     {
430       GNUNET_TRANSPORT_set_quota (p2.th,
431                                   &p1.id,
432                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
433                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
434     }
435   if (2 == connected)
436     {
437       if (die_task != GNUNET_SCHEDULER_NO_TASK)
438         GNUNET_SCHEDULER_cancel (die_task);
439       if (tct != GNUNET_SCHEDULER_NO_TASK)
440         GNUNET_SCHEDULER_cancel (tct);
441       tct = GNUNET_SCHEDULER_NO_TASK;
442       if (p2_hello_canceled == GNUNET_NO)
443       {
444         GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
445         p2_hello_canceled = GNUNET_YES;
446       }
447       if (p1_hello_canceled == GNUNET_NO)
448       {
449         GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
450         p1_hello_canceled = GNUNET_YES;
451       }
452       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
453                                                &end_badly, NULL);
454       th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
455                                               &p1.id,
456                                               get_size (0), 0, TIMEOUT,
457                                               &notify_ready,
458                                               NULL);
459       
460     }
461 }
462
463
464 static void
465 setup_peer (struct PeerContext *p, const char *cfgname)
466 {
467   p->cfg = GNUNET_CONFIGURATION_create ();
468   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
469   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
470       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
471   GNUNET_DISK_directory_remove (p->servicehome);
472
473 #if START_ARM
474   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
475                                         "gnunet-service-arm",
476                                         "gnunet-service-arm",
477 #if VERBOSE_ARM
478                                         "-L", "DEBUG",
479 #endif
480                                         "-c", cfgname, NULL);
481 #endif
482
483   if (is_https)
484     {
485       struct stat sbuf;
486       if (p==&p1)
487         {
488           if (GNUNET_CONFIGURATION_have_value (p->cfg,
489                                                "transport-https", "KEY_FILE"))
490             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
491           if (key_file_p1 == NULL)
492             GNUNET_asprintf(&key_file_p1,"https_p1.key");
493           if (0 == stat (key_file_p1, &sbuf ))
494             {
495               if (0 == remove(key_file_p1))
496                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497                             "Successfully removed existing private key file `%s'\n",
498                             key_file_p1);
499               else
500                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501                             "Failed to remove private key file `%s'\n",
502                             key_file_p1);
503             }
504           if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
505             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
506           if (cert_file_p1 == NULL)
507             GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
508           if (0 == stat (cert_file_p1, &sbuf ))
509             {
510               if (0 == remove(cert_file_p1))
511                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512                             "Successfully removed existing certificate file `%s'\n",
513                             cert_file_p1);
514               else
515                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516                             "Failed to remove existing certificate file `%s'\n",
517                             cert_file_p1);
518             }
519         }
520       else if (p==&p2)
521         {
522           if (GNUNET_CONFIGURATION_have_value (p->cfg,
523                                                "transport-https", "KEY_FILE"))
524             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
525           if (key_file_p2 == NULL)
526             GNUNET_asprintf(&key_file_p2,"https_p2.key");
527           if (0 == stat (key_file_p2, &sbuf ))
528             {
529               if (0 == remove(key_file_p2))
530                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531                             "Successfully removed existing private key file `%s'\n",
532                             key_file_p2);
533               else
534                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535                             "Failed to remove private key file `%s'\n",
536                             key_file_p2);
537             }
538           if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
539             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
540           if (cert_file_p2 == NULL)
541             GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
542           if (0 == stat (cert_file_p2, &sbuf ))
543             {
544               if (0 == remove(cert_file_p2))
545                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546                             "Successfully removed existing certificate file `%s'\n",
547                             cert_file_p2);
548               else
549                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550                             "Failed to remove existing certificate file `%s'\n",
551                             cert_file_p2);
552             }
553         }
554     }
555   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
556                                     p,
557                                     &notify_receive,
558                                     &notify_connect,
559                                     &notify_disconnect);
560   GNUNET_assert (p->th != NULL);
561 }
562
563
564 /**
565  * Return the actual path to a file found in the current
566  * PATH environment variable.
567  *
568  * @param binary the name of the file to find
569  */
570 static char *
571 get_path_from_PATH (char *binary)
572 {
573   char *path;
574   char *pos;
575   char *end;
576   char *buf;
577   const char *p;
578
579   p = getenv ("PATH");
580   if (p == NULL)
581     {
582       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583                   _("PATH environment variable is unset.\n"));
584       return NULL;
585     }
586   path = GNUNET_strdup (p);     /* because we write on it */
587   buf = GNUNET_malloc (strlen (path) + 20);
588   pos = path;
589
590   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
591     {
592       *end = '\0';
593       sprintf (buf, "%s/%s", pos, binary);
594       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
595         {
596           GNUNET_free (path);
597           return buf;
598         }
599       pos = end + 1;
600     }
601   sprintf (buf, "%s/%s", pos, binary);
602   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
603     {
604       GNUNET_free (path);
605       return buf;
606     }
607   GNUNET_free (buf);
608   GNUNET_free (path);
609   return NULL;
610 }
611
612 /**
613  * Check whether the suid bit is set on a file.
614  * Attempts to find the file using the current
615  * PATH environment variable as a search path.
616  *
617  * @param binary the name of the file to check
618  *
619  * @return GNUNET_YES if the binary is found and
620  *         can be run properly, GNUNET_NO otherwise
621  */
622 static int
623 check_gnunet_nat_binary(char *binary)
624 {
625   struct stat statbuf;
626   char *p;
627 #ifdef MINGW
628   SOCKET rawsock;
629 #endif
630
631 #ifdef MINGW
632   char *binaryexe;
633   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
634   p = get_path_from_PATH (binaryexe);
635   free (binaryexe);
636 #else
637   p = get_path_from_PATH (binary);
638 #endif
639   if (p == NULL)
640     {
641       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
642                   _("Could not find binary `%s' in PATH!\n"),
643                   binary);
644       return GNUNET_NO;
645     }
646   if (0 != STAT (p, &statbuf))
647     {
648       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
649                   _("stat (%s) failed: %s\n"),
650                   p,
651                   STRERROR (errno));
652       GNUNET_free (p);
653       return GNUNET_SYSERR;
654     }
655   GNUNET_free (p);
656 #ifndef MINGW
657   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
658        (statbuf.st_uid == 0) )
659     return GNUNET_YES;
660   return GNUNET_NO;
661 #else
662   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
663   if (INVALID_SOCKET == rawsock)
664     {
665       DWORD err = GetLastError ();
666       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
667                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
668       return GNUNET_NO; /* not running as administrator */
669     }
670   closesocket (rawsock);
671   return GNUNET_YES;
672 #endif
673 }
674
675
676 static void
677 try_connect (void *cls,
678              const struct GNUNET_SCHEDULER_TaskContext *tc)
679 {
680   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681               "Asking peers to connect...\n");
682   GNUNET_TRANSPORT_try_connect (p2.th,
683                                 &p1.id);
684   GNUNET_TRANSPORT_try_connect (p1.th,
685                                 &p2.id);
686   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
687                                       &try_connect,
688                                       NULL);
689 }
690
691
692 static void
693 run (void *cls,
694      char *const *args,
695      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
696 {
697   GNUNET_assert (ok == 1);
698   OKPP;
699   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
700                                            &end_badly,
701                                            NULL);
702   if (is_tcp)
703     {
704       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
705       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
706     }
707   else if (is_http)
708     {
709       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
710       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
711     }
712   else if (is_https)
713     {
714       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
715       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
716     }
717   else if (is_udp)
718     {
719       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
720       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
721     }
722   else if (is_unix)
723     {
724       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
725       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
726     }
727   else if (is_tcp_nat)
728     {
729       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
730       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
731     }
732   else if (is_wlan)
733     {
734       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
735       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
736     }
737   else
738     GNUNET_assert (0);
739   GNUNET_assert(p1.th != NULL);
740   GNUNET_assert(p2.th != NULL);
741   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
742   p1_hello_canceled = GNUNET_NO;
743   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
744   p2_hello_canceled = GNUNET_NO;
745   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
746 }
747
748
749 static int
750 check ()
751 {
752   char *const argv[] = { "test-transport-api-reliability",
753     "-c",
754     "test_transport_api_data.conf",
755 #if VERBOSE
756     "-L", "DEBUG",
757 #endif
758     NULL
759   };
760   struct GNUNET_GETOPT_CommandLineOption options[] = {
761     GNUNET_GETOPT_OPTION_END
762   };
763
764 #if WRITECONFIG
765   setTransportOptions("test_transport_api_data.conf");
766 #endif
767   ok = 1;
768
769   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
770     {
771       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
772                  "Not running NAT test case, binaries not properly installed.\n");
773       return 0;
774     }
775
776   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
777                       argv, "test-transport-api-reliability", "nohelp",
778                       options, &run, &ok);
779   stop_arm (&p1);
780   stop_arm (&p2);
781
782   if (is_https)
783   {
784     struct stat sbuf;
785     if (0 == stat (cert_file_p1, &sbuf ))
786     {
787       if (0 == remove(cert_file_p1))
788         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
789                     "Successfully removed existing certificate file `%s'\n",
790                     cert_file_p1);
791       else
792         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
793                     "Failed to remove certfile `%s'\n",
794                     cert_file_p1);
795     }
796
797     if (0 == stat (key_file_p1, &sbuf ))
798     {
799       if (0 == remove(key_file_p1))
800         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
801                     "Successfully removed private key file `%s'\n",
802                     key_file_p1);
803       else
804         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
805                     "Failed to private key file `%s'\n",key_file_p1);
806     }
807
808     if (0 == stat (cert_file_p2, &sbuf ))
809     {
810       if (0 == remove(cert_file_p2))
811         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
812                     "Successfully removed existing certificate file `%s'\n",
813                     cert_file_p2);
814       else
815         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
816                     "Failed to remove certfile `%s'\n",cert_file_p2);
817     }
818
819     if (0 == stat (key_file_p2, &sbuf ))
820     {
821       if (0 == remove(key_file_p2))
822         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
823                     "Successfully removed private key file `%s'\n",
824                     key_file_p2);
825       else
826         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
827                     "Failed to private key file `%s'\n",
828                     key_file_p2);
829     }
830     GNUNET_free(key_file_p1);
831     GNUNET_free(key_file_p2);
832     GNUNET_free(cert_file_p1);
833     GNUNET_free(cert_file_p2);
834     GNUNET_free(p1.servicehome);
835     GNUNET_free(p2.servicehome);
836   }
837
838   return ok;
839 }
840
841
842 int
843 main (int argc, char *argv[])
844 {
845   int ret;
846
847   if (strstr(argv[0], "tcp_nat") != NULL)
848     {
849       is_tcp_nat = GNUNET_YES;
850       GNUNET_asprintf(&test_name, "tcp_nat");
851     }
852   else if (strstr(argv[0], "tcp") != NULL)
853     {
854       is_tcp = GNUNET_YES;
855       GNUNET_asprintf(&test_name, "tcp");
856     }
857   else if (strstr(argv[0], "https") != NULL)
858     {
859       is_https = GNUNET_YES;
860       GNUNET_asprintf(&test_name, "https");
861     }
862   else if (strstr(argv[0], "http") != NULL)
863     {
864       is_http = GNUNET_YES;
865       GNUNET_asprintf(&test_name, "http");
866     }
867   else if (strstr(argv[0], "udp") != NULL)
868     {
869       is_udp = GNUNET_YES;
870       GNUNET_asprintf(&test_name, "udp");
871     }
872   else if (strstr(argv[0], "unix") != NULL)
873     {
874       is_unix = GNUNET_YES;
875       GNUNET_asprintf(&test_name, "unix");
876     }
877   else if (strstr(argv[0], "wlan") != NULL)
878     {
879        is_wlan = GNUNET_YES;
880     }
881   GNUNET_log_setup ("test-transport-api-reliability",
882 #if VERBOSE
883                     "DEBUG",
884 #else
885                     "WARNING",
886 #endif
887                     NULL);
888   ret = check ();
889   GNUNET_DISK_directory_remove (p1.servicehome);
890   GNUNET_DISK_directory_remove (p2.servicehome);
891   return ret;
892 }
893
894 /* end of test_transport_api_reliability.c */