doxygen
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.c
index 7556e66639b6ee292201fdf4e3f11bcd398a4e42..52469b93e96c6acba1ec4f47fc00af8573eaf37c 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 /**
- * @file src/transport/gnunet-wlan.c
+ * @file src/transport/gnunet-transport-wlan-helper.c
  * @brief wlan layer two server; must run as root (SUID will do)
  *        This code will work under GNU/Linux only.
  * @author David Brodski
  * This program serves as the mediator between the wlan interface and
  * gnunet
  */
-#include "gnunet-transport-wlan-helper.h"
+
+
+#include "platform.h"
+#include "gnunet_constants.h"
+#include "gnunet_os_lib.h"
+#include "gnunet_transport_plugin.h"
+#include "transport.h"
+#include "gnunet_util_lib.h"
 #include "plugin_transport_wlan.h"
+#include "gnunet_common.h"
+#include "gnunet-transport-wlan-helper.h"
 #include "ieee80211_radiotap.h"
-
 #include <pcap.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+
+
+
 
 //#include "radiotap.h"
 
@@ -378,11 +393,246 @@ int ieee80211_radiotap_iterator_next(
        return -ENOENT;
 }
 
+#define FIFO_FILE1       "/tmp/MYFIFOin"
+#define FIFO_FILE2       "/tmp/MYFIFOout"
+#define MAXLINE         20
+
+int first;
+int closeprog = 0;
+
+void sigfunc(int sig)
+{
+
+ if(sig != SIGINT || sig != SIGTERM || sig != SIGKILL)
+   return;
+ else
+    {
+      closeprog = 1;
+
+      unlink(FIFO_FILE1);
+      unlink(FIFO_FILE2);
+
+    }
+}
+
+
+
+int
+testmode(int argc, char *argv[])
+{
+  struct stat st;
+  int erg;
+
+  int fd[2];
+  FILE *fpin;
+  FILE *fpout;
+  pid_t pid;
+
+
+
+  //make the fifos if needed
+  if (stat(FIFO_FILE1, &st) != 0)
+    {
+      if (stat(FIFO_FILE2, &st) == 0)
+        {
+          perror("FIFO 2 exists, but FIFO 1 not");
+          exit(1);
+        }
+      first = 1;
+      umask(0);
+      erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
+      erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
+
+      if ((fpin = fopen(FIFO_FILE1, "r")) == NULL)
+        {
+          perror("fopen");
+          exit(1);
+        }
+      if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)
+        {
+          perror("fopen");
+          exit(1);
+        }
+    }
+  else
+    {
+      first = 0;
+      if (stat(FIFO_FILE2, &st) != 0)
+        {
+          perror("FIFO 1 exists, but FIFO 2 not");
+          exit(1);
+        }
+      if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)
+        {
+          perror("fopen");
+          exit(1);
+        }
+      if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)
+        {
+          perror("fopen");
+          exit(1);
+        }
+
+    }
+
+  // fork
+
+  fd[0] = STDIN_FILENO;
+  fd[1] = STDOUT_FILENO;
+
+  if ((pid = fork()) < 0)
+    {
+      perror("FORK ERROR");
+
+      //clean up
+      if (first == 1)
+              {
+                unlink(FIFO_FILE1);
+                unlink(FIFO_FILE2);
+              }
+      fclose(fpin);
+      fclose(fpout);
+      return -3;
+    }
+  else if (pid == 0) // CHILD PROCESS
+    {
+
+      signal(SIGINT, sigfunc);
+      signal(SIGTERM, sigfunc);
+      signal(SIGKILL, sigfunc);
+      int rv = 0;
+      int readc = 0;
+      int pos = 0;
+      char line[MAXLINE];
+      int ret = 0;
+
+      while (closeprog == 0)
+        {
+          readc = 0;
+
+          //while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){
+            if ((rv = read(fd[0], line, MAXLINE)) < 0)
+              {
+                perror("READ ERROR FROM STDIN");
+              }
+            readc += rv;
+          //}
+
+          if (closeprog == 1){
+            break;
+          }
+
+          pos = 0;
+
+
+          perror("writing blub");
+          //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);
+
+          //pos += sizeof(struct GNUNET_Mes#include "gnunet_util_lib.h"sageHeader);
+
+          //do not send radiotap header
+          //pos += sizeof( struct RadiotapHeader);
+
+          while (pos < readc)
+            {
+              ret = fwrite(&line[pos], 1, readc - pos, fpout);
+              if (ret < 0)
+                {
+                  closeprog = 1;
+                  perror("Write ERROR FROM fpout");
+                  break;
+                }
+              else
+                {
+                  pos += ret;
+                }
+            }
+        }
+
+
+      //clean up
+      fclose(fpout);
+    }
+  else // PARENT PROCESS
+    {
+      signal(SIGINT, sigfunc);
+      signal(SIGTERM, sigfunc);
+      signal(SIGKILL, sigfunc);
+      int rv = 0;
+      int ret = 0;
+      ssize_t pos = 0;
+      char line[MAXLINE];
+      struct Wlan_Helper_Control_Message macmsg;
+
+      //Send random mac address
+      macmsg.mac.mac[0] = 0x13;
+      macmsg.mac.mac[1] = 0x22;
+      macmsg.mac.mac[2] = 0x33;
+      macmsg.mac.mac[3] = 0x44;
+      macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
+      macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
+      macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
+      macmsg.hdr.type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL;
+
+      pos = 0;
+
+      while (pos < sizeof(struct Wlan_Helper_Control_Message))
+        {
+          pos += write(fd[1], &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos);
+        }
+
+      while (closeprog == 0)
+        {
+          if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)
+            {
+              perror("READ ERROR FROM fpin");
+            }
+          if (closeprog == 1){
+            break;
+          }
+          pos = 0;
+          while (pos < rv)
+            {
+
+              ret= write(fd[1], &line[pos], rv - pos);
+              if (ret < 0)
+                {
+                  closeprog = 1;
+                  perror("Write ERROR FROM STDout");
+                  break;
+                }
+              else
+                {
+                  pos += ret;
+                }
+            }
+        }
+
+
+      //clean up
+      fclose(fpin);
+
+      if (first == 1)
+        {
+          unlink(FIFO_FILE1);
+          unlink(FIFO_FILE2);
+        }
+    }
+
+  // Write the input to the output
+
+  return (0);
+}
 
 
 int
 main(int argc, char *argv[])
 {
+       if ((argc==2) && (strstr(argv[1],"1")))
+       {
+               return testmode(argc, argv);
+       }
+#if 0
        u8 u8aSendBuffer[500];
        char szErrbuf[PCAP_ERRBUF_SIZE];
        int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
@@ -480,7 +730,7 @@ main(int argc, char *argv[])
 
        //get header type
        nLinkEncap = pcap_datalink(ppcap);
-       nCaptureHeaderLength = 0;
+       nCaptureHeaderLength = 0;home/mwachs/gnb/bin/
 
        switch (nLinkEncap) {
 
@@ -640,6 +890,7 @@ main(int argc, char *argv[])
        }
 
 
-
+#endif
        return (0);
 }
+