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