03a2458b2c7a518e09bfdb1fa36151b21dda9e56
[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       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
479   GNUNET_DISK_directory_remove (p->servicehome);
480
481 #if START_ARM
482   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
483                                         "gnunet-service-arm",
484                                         "gnunet-service-arm",
485 #if VERBOSE_ARM
486                                         "-L", "DEBUG",
487 #endif
488                                         "-c", cfgname, NULL);
489 #endif
490
491   if (is_https)
492     {
493       struct stat sbuf;
494       if (p==&p1)
495         {
496           if (GNUNET_CONFIGURATION_have_value (p->cfg,
497                                                "transport-https", "KEY_FILE"))
498             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
499           if (key_file_p1 == NULL)
500             GNUNET_asprintf(&key_file_p1,"https_p1.key");
501           if (0 == stat (key_file_p1, &sbuf ))
502             {
503               if (0 == remove(key_file_p1))
504                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505                             "Successfully removed existing private key file `%s'\n",
506                             key_file_p1);
507               else
508                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509                             "Failed to remove private key file `%s'\n",
510                             key_file_p1);
511             }
512           if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
513             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
514           if (cert_file_p1 == NULL)
515             GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
516           if (0 == stat (cert_file_p1, &sbuf ))
517             {
518               if (0 == remove(cert_file_p1))
519                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520                             "Successfully removed existing certificate file `%s'\n",
521                             cert_file_p1);
522               else
523                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524                             "Failed to remove existing certificate file `%s'\n",
525                             cert_file_p1);
526             }
527         }
528       else if (p==&p2)
529         {
530           if (GNUNET_CONFIGURATION_have_value (p->cfg,
531                                                "transport-https", "KEY_FILE"))
532             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
533           if (key_file_p2 == NULL)
534             GNUNET_asprintf(&key_file_p2,"https_p2.key");
535           if (0 == stat (key_file_p2, &sbuf ))
536             {
537               if (0 == remove(key_file_p2))
538                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539                             "Successfully removed existing private key file `%s'\n",
540                             key_file_p2);
541               else
542                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543                             "Failed to remove private key file `%s'\n",
544                             key_file_p2);
545             }
546           if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
547             GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
548           if (cert_file_p2 == NULL)
549             GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
550           if (0 == stat (cert_file_p2, &sbuf ))
551             {
552               if (0 == remove(cert_file_p2))
553                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554                             "Successfully removed existing certificate file `%s'\n",
555                             cert_file_p2);
556               else
557                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558                             "Failed to remove existing certificate file `%s'\n",
559                             cert_file_p2);
560             }
561         }
562     }
563   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
564                                     p,
565                                     &notify_receive,
566                                     &notify_connect,
567                                     &notify_disconnect);
568   GNUNET_assert (p->th != NULL);
569 }
570
571
572 /**
573  * Return the actual path to a file found in the current
574  * PATH environment variable.
575  *
576  * @param binary the name of the file to find
577  */
578 static char *
579 get_path_from_PATH (char *binary)
580 {
581   char *path;
582   char *pos;
583   char *end;
584   char *buf;
585   const char *p;
586
587   p = getenv ("PATH");
588   if (p == NULL)
589     {
590       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591                   _("PATH environment variable is unset.\n"));
592       return NULL;
593     }
594   path = GNUNET_strdup (p);     /* because we write on it */
595   buf = GNUNET_malloc (strlen (path) + 20);
596   pos = path;
597
598   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
599     {
600       *end = '\0';
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       pos = end + 1;
608     }
609   sprintf (buf, "%s/%s", pos, binary);
610   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
611     {
612       GNUNET_free (path);
613       return buf;
614     }
615   GNUNET_free (buf);
616   GNUNET_free (path);
617   return NULL;
618 }
619
620 /**
621  * Check whether the suid bit is set on a file.
622  * Attempts to find the file using the current
623  * PATH environment variable as a search path.
624  *
625  * @param binary the name of the file to check
626  *
627  * @return GNUNET_YES if the binary is found and
628  *         can be run properly, GNUNET_NO otherwise
629  */
630 static int
631 check_gnunet_nat_binary(char *binary)
632 {
633   struct stat statbuf;
634   char *p;
635 #ifdef MINGW
636   SOCKET rawsock;
637 #endif
638
639 #ifdef MINGW
640   char *binaryexe;
641   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
642   p = get_path_from_PATH (binaryexe);
643   free (binaryexe);
644 #else
645   p = get_path_from_PATH (binary);
646 #endif
647   if (p == NULL)
648     {
649       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
650                   _("Could not find binary `%s' in PATH!\n"),
651                   binary);
652       return GNUNET_NO;
653     }
654   if (0 != STAT (p, &statbuf))
655     {
656       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
657                   _("stat (%s) failed: %s\n"),
658                   p,
659                   STRERROR (errno));
660       GNUNET_free (p);
661       return GNUNET_SYSERR;
662     }
663   GNUNET_free (p);
664 #ifndef MINGW
665   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
666        (statbuf.st_uid == 0) )
667     return GNUNET_YES;
668   return GNUNET_NO;
669 #else
670   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
671   if (INVALID_SOCKET == rawsock)
672     {
673       DWORD err = GetLastError ();
674       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
675                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
676       return GNUNET_NO; /* not running as administrator */
677     }
678   closesocket (rawsock);
679   return GNUNET_YES;
680 #endif
681 }
682
683
684 static void
685 try_connect (void *cls,
686              const struct GNUNET_SCHEDULER_TaskContext *tc)
687 {
688   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689               "Asking peers to connect...\n");
690   GNUNET_TRANSPORT_try_connect (p2.th,
691                                 &p1.id);
692   GNUNET_TRANSPORT_try_connect (p1.th,
693                                 &p2.id);
694   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
695                                       &try_connect,
696                                       NULL);
697 }
698
699
700 static void
701 run (void *cls,
702      char *const *args,
703      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
704 {
705   GNUNET_assert (ok == 1);
706   OKPP;
707   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
708                                            &end_badly,
709                                            NULL);
710   if (is_tcp)
711     {
712       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
713       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
714     }
715   else if (is_http)
716     {
717       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
718       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
719     }
720   else if (is_https)
721     {
722       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
723       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
724     }
725   else if (is_udp)
726     {
727       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
728       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
729     }
730   else if (is_unix)
731     {
732       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
733       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
734     }
735   else if (is_tcp_nat)
736     {
737       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
738       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
739     }
740   else if (is_wlan)
741     {
742       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
743       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
744     }
745   else
746     GNUNET_assert (0);
747   GNUNET_assert(p1.th != NULL);
748   GNUNET_assert(p2.th != NULL);
749   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
750   p1_hello_canceled = GNUNET_NO;
751   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
752   p2_hello_canceled = GNUNET_NO;
753   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
754 }
755
756
757 static int
758 check ()
759 {
760   char *const argv[] = { "test-transport-api-reliability",
761     "-c",
762     "test_transport_api_data.conf",
763 #if VERBOSE
764     "-L", "DEBUG",
765 #endif
766     NULL
767   };
768   struct GNUNET_GETOPT_CommandLineOption options[] = {
769     GNUNET_GETOPT_OPTION_END
770   };
771
772 #if WRITECONFIG
773   setTransportOptions("test_transport_api_data.conf");
774 #endif
775   ok = 1;
776
777   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
778     {
779       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
780                  "Not running NAT test case, binaries not properly installed.\n");
781       return 0;
782     }
783
784   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
785                       argv, "test-transport-api-reliability", "nohelp",
786                       options, &run, &ok);
787   stop_arm (&p1);
788   stop_arm (&p2);
789
790   if (is_https)
791   {
792     struct stat sbuf;
793     if (0 == stat (cert_file_p1, &sbuf ))
794     {
795       if (0 == remove(cert_file_p1))
796         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
797                     "Successfully removed existing certificate file `%s'\n",
798                     cert_file_p1);
799       else
800         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
801                     "Failed to remove certfile `%s'\n",
802                     cert_file_p1);
803     }
804
805     if (0 == stat (key_file_p1, &sbuf ))
806     {
807       if (0 == remove(key_file_p1))
808         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
809                     "Successfully removed private key file `%s'\n",
810                     key_file_p1);
811       else
812         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
813                     "Failed to private key file `%s'\n",key_file_p1);
814     }
815
816     if (0 == stat (cert_file_p2, &sbuf ))
817     {
818       if (0 == remove(cert_file_p2))
819         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
820                     "Successfully removed existing certificate file `%s'\n",
821                     cert_file_p2);
822       else
823         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
824                     "Failed to remove certfile `%s'\n",cert_file_p2);
825     }
826
827     if (0 == stat (key_file_p2, &sbuf ))
828     {
829       if (0 == remove(key_file_p2))
830         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
831                     "Successfully removed private key file `%s'\n",
832                     key_file_p2);
833       else
834         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
835                     "Failed to private key file `%s'\n",
836                     key_file_p2);
837     }
838     GNUNET_free(key_file_p1);
839     GNUNET_free(key_file_p2);
840     GNUNET_free(cert_file_p1);
841     GNUNET_free(cert_file_p2);
842     GNUNET_free(p1.servicehome);
843     GNUNET_free(p2.servicehome);
844   }
845
846   return ok;
847 }
848
849
850 int
851 main (int argc, char *argv[])
852 {
853   int ret;
854
855   test_failed = GNUNET_NO;
856
857   if (strstr(argv[0], "tcp_nat") != NULL)
858     {
859       is_tcp_nat = GNUNET_YES;
860       GNUNET_asprintf(&test_name, "tcp_nat");
861     }
862   else if (strstr(argv[0], "tcp") != NULL)
863     {
864       is_tcp = GNUNET_YES;
865       GNUNET_asprintf(&test_name, "tcp");
866     }
867   else if (strstr(argv[0], "https") != NULL)
868     {
869       is_https = GNUNET_YES;
870       GNUNET_asprintf(&test_name, "https");
871     }
872   else if (strstr(argv[0], "http") != NULL)
873     {
874       is_http = GNUNET_YES;
875       GNUNET_asprintf(&test_name, "http");
876     }
877   else if (strstr(argv[0], "udp") != NULL)
878     {
879       is_udp = GNUNET_YES;
880       GNUNET_asprintf(&test_name, "udp");
881     }
882   else if (strstr(argv[0], "unix") != NULL)
883     {
884       is_unix = GNUNET_YES;
885       GNUNET_asprintf(&test_name, "unix");
886     }
887   else if (strstr(argv[0], "wlan") != NULL)
888     {
889        is_wlan = GNUNET_YES;
890     }
891   GNUNET_log_setup ("test-transport-api-reliability",
892 #if VERBOSE
893                     "DEBUG",
894 #else
895                     "WARNING",
896 #endif
897                     NULL);
898   ret = check ();
899   GNUNET_DISK_directory_remove (p1.servicehome);
900   GNUNET_DISK_directory_remove (p2.servicehome);
901   return ret;
902 }
903
904 /* end of test_transport_api_reliability.c */