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