towards having sending in transport-testing API (not yet flexible enough)
[oweals/gnunet.git] / src / transport / gnunet-helper-transport-wlan-dummy.c
index dddd51cb7ecfd90bffc1f0eb5567b698b07c07e5..684546314044514355c8c708af0396f48068c4bd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- (C) 2010, 2012 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010, 2012 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 /**
  * @file transport/gnunet-helper-transport-wlan-dummy.c
@@ -103,10 +103,14 @@ send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
 
   struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
 
-  memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
+  GNUNET_memcpy (&macmsg.mac,
+          (char *) mac,
+          sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
   macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
   macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
-  memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
+  GNUNET_memcpy (buffer,
+          &macmsg,
+          sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
   return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
 }
 
@@ -119,7 +123,7 @@ send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
  * @param client unused
  * @param hdr inbound message from the FIFO
  */
-static void
+static int
 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
 {
   struct SendBuffer *write_pout = cls;
@@ -133,7 +137,7 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
   if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
        (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) )
   {
-    fprintf (stderr, "Received malformed message\n");
+    FPRINTF (stderr, "%s", "Received malformed message\n");
     exit (1);
   }
   payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
@@ -146,14 +150,15 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
   newheader.header.size = htons (payload_size + sizeof (newheader));
   newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
   newheader.frame = in->frame;
-  memcpy (write_pout->buf + write_pout->size,
+  GNUNET_memcpy (write_pout->buf + write_pout->size,
          &newheader,
          sizeof (newheader));
   write_pout->size += sizeof (newheader);
-  memcpy (write_pout->buf + write_pout->size,
+  GNUNET_memcpy (write_pout->buf + write_pout->size,
          &in[1],
          payload_size);
   write_pout->size += payload_size;
+  return GNUNET_OK;
 }
 
 
@@ -164,7 +169,7 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
  * @param client unused
  * @param hdr the message we received to copy to the buffer
  */
-static void
+static int
 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
 {
   struct SendBuffer *write_std = cls;
@@ -176,8 +181,9 @@ file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
     FPRINTF (stderr, "%s", "Packet too big for buffer\n");
     exit (1);
   }
-  memcpy (write_std->buf + write_std->size, hdr, sendsize);
+  GNUNET_memcpy (write_std->buf + write_std->size, hdr, sendsize);
   write_std->size += sendsize;
+  return GNUNET_OK;
 }
 
 
@@ -222,65 +228,83 @@ main (int argc, char *argv[])
   }
 
   /* make the fifos if needed */
-  if (0 != stat (FIFO_FILE1, &st))
+  umask (0);
+  if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
+       (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
   {
-    if (0 == stat (FIFO_FILE2, &st))
-    {
-      FPRINTF (stderr, "%s", "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
-      exit (1);
-    }
-    umask (0);
-    erg = mkfifo (FIFO_FILE1, 0666);
-    if (0 != erg)
-    {
-      FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
-               strerror (errno));
-    }
-    erg = mkfifo (FIFO_FILE2, 0666);
-    if (0 != erg)
+    FPRINTF (stderr,
+             "Failed to create directory for file `%s'\n",
+             FIFO_FILE1);
+    return 1;
+  }
+  if (0 == strcmp (argv[1], "1") )
+  {
+    if (0 != stat (FIFO_FILE1, &st))
     {
-      FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
-               strerror (errno));
+      erg = mkfifo (FIFO_FILE1, 0666);
+      if ( (0 != erg) && (EEXIST != errno) )
+       FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
+                strerror (errno));
     }
   }
   else
   {
     if (0 != stat (FIFO_FILE2, &st))
     {
-      FPRINTF (stderr, "%s", "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
-      exit (1);
+       GNUNET_break (0 == (erg = mkfifo (FIFO_FILE2, 0666)));
+      if ( (0 != erg) && (EEXIST != errno) )
+       FPRINTF (stderr,
+                 "Error in mkfifo(%s): %s\n",
+                 FIFO_FILE2,
+                strerror (errno));
     }
   }
 
-  if (0 != strcmp (argv[1], "1"))
+  if (0 == strcmp (argv[1], "1"))
   {
     first = 1;
     fpin = fopen (FIFO_FILE1, "r");
     if (NULL == fpin)
     {
-      FPRINTF (stderr, "fopen of read FIFO_FILE1 failed: %s\n", STRERROR (errno));
+      FPRINTF (stderr,
+               "fopen of read FIFO_FILE1 failed: %s\n",
+               STRERROR (errno));
       goto end;
     }
-    fpout = fopen (FIFO_FILE2, "w");
+    if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
+    {
+      GNUNET_break (0 == mkfifo (FIFO_FILE2, 0666));
+      fpout = fopen (FIFO_FILE2, "w");
+    }
     if (NULL == fpout)
     {
-      FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
+      FPRINTF (stderr,
+               "fopen of write FIFO_FILE2 failed: %s\n",
+               STRERROR (errno));
       goto end;
     }
   }
   else
   {
     first = 0;
-    fpout = fopen (FIFO_FILE1, "w");
+    if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
+    {
+       GNUNET_break (0 == mkfifo (FIFO_FILE1, 0666));
+      fpout = fopen (FIFO_FILE1, "w");
+    }
     if (NULL == fpout)
     {
-      FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
+      FPRINTF (stderr,
+               "fopen of write FIFO_FILE1 failed: %s\n",
+               STRERROR (errno));
       goto end;
     }
     fpin = fopen (FIFO_FILE2, "r");
     if (NULL == fpin)
     {
-      FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
+      FPRINTF (stderr,
+               "fopen of read FIFO_FILE2 failed: %s\n",
+               STRERROR (errno));
       goto end;
     }
   }
@@ -289,7 +313,9 @@ main (int argc, char *argv[])
   GNUNET_assert (fpin >= 0);
   if (fdpin >= FD_SETSIZE)
   {
-    FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
+    FPRINTF (stderr,
+             "File fdpin number too large (%d > %u)\n",
+             fdpin,
              (unsigned int) FD_SETSIZE);
     goto end;
   }
@@ -299,13 +325,16 @@ main (int argc, char *argv[])
 
   if (fdpout >= FD_SETSIZE)
   {
-    FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
+    FPRINTF (stderr,
+             "File fdpout number too large (%d > %u)\n",
+             fdpout,
              (unsigned int) FD_SETSIZE);
     goto end;
   }
 
   signal (SIGINT, &sigfunc);
   signal (SIGTERM, &sigfunc);
+  signal (GNUNET_TERM_SIG, &sigfunc);
 
   write_std.size = 0;
   write_std.pos = 0;