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