curly wars / auto-indentation
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.c
index 1cfcf2c4e167a509b7a628043769930c5e3774f9..25e55e510a583ca3287aa457608a10addb84f999 100644 (file)
  * gnunet
  */
 
+/**
+ * parts taken from aircrack-ng, parts changend.
+ */
+
 #define _GNU_SOURCE
 #include <sys/socket.h>
 #include <sys/ioctl.h>
  */
 #include "wlan/ieee80211_radiotap.h"
 #include "wlan/crctable_osdep.h"
-#include "wlan/loopback_helper.h"
-#include "wlan/ieee80211.h"
+//#include "wlan/loopback_helper.h"
+//#include "wlan/ieee80211.h"
+#include "wlan/helper_common.h"
 
 #define ARPHRD_IEEE80211        801
 #define ARPHRD_IEEE80211_PRISM  802
 #define ARPHRD_IEEE80211_FULL   803
 
-#include "wlan/loopback_helper.h"
-
 #define DEBUG 1
 
 #define MAC_ADDR_SIZE 6
 
+
+#define IEEE80211_ADDR_LEN      6       /* size of 802.11 address */
+
+/*
+ * generic definitions for IEEE 802.11 frames
+ */
+struct ieee80211_frame
+{
+  u_int8_t i_fc[2];
+  u_int8_t i_dur[2];
+  u_int8_t i_addr1[IEEE80211_ADDR_LEN];
+  u_int8_t i_addr2[IEEE80211_ADDR_LEN];
+  u_int8_t i_addr3[IEEE80211_ADDR_LEN];
+  u_int8_t i_seq[2];
+  /* possibly followed by addr4[IEEE80211_ADDR_LEN]; */
+  /* see below */
+} GNUNET_PACKED;
+
+/**
+ * struct for storing the information of the hardware
+ */
 struct Hardware_Infos
 {
 
+  /**
+  * send buffer
+  */
   struct sendbuf write_pout;
+  /**
+   * file descriptor for the raw socket
+   */
   int fd_raw;
+
   int arptype_in;
 
   /**
@@ -107,10 +138,25 @@ struct Hardware_Infos
   unsigned char pl_mac[MAC_ADDR_SIZE];
 };
 
+struct RadioTapheader
+{
+  struct ieee80211_radiotap_header header;
+  u8 rate;
+  u8 pad1;
+  u16 txflags;
+};
+
 // FIXME: inline?
-int getChannelFromFrequency (int frequency);
+int
+getChannelFromFrequency (int frequency);
 
 // FIXME: make nice...
+/**
+ * function to calculate the crc, the start of the calculation
+ * @param buf buffer to calc the crc
+ * @param len len of the buffer
+ * @return crc sum
+ */
 static unsigned long
 calc_crc_osdep (unsigned char *buf, int len)
 {
@@ -125,6 +171,12 @@ calc_crc_osdep (unsigned char *buf, int len)
 /* CRC checksum verification routine */
 
 // FIXME: make nice...
+/**
+ * Function to check crc of the wlan packet
+ * @param buf buffer of the packet
+ * @param len len of the data
+ * @return crc sum of the data
+ */
 static int
 check_crc_buf_osdep (unsigned char *buf, int len)
 {
@@ -135,14 +187,17 @@ check_crc_buf_osdep (unsigned char *buf, int len)
 
   crc = calc_crc_osdep (buf, len);
   buf += len;
-  return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && ((crc
-                                                                         >> 16)
-                                                                        & 0xFF)
-          == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
+  return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
+          ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
 }
 
 
 // FIXME: make nice...
+/**
+ * function to get the channel of a specific wlan card
+ * @param dev pointer to the dev struct of the card
+ * @return channel number
+ */
 static int
 linux_get_channel (struct Hardware_Infos *dev)
 {
@@ -174,6 +229,14 @@ linux_get_channel (struct Hardware_Infos *dev)
 
 
 // FIXME: make nice...
+/**
+ * function to read from a wlan card
+ * @param dev pointer to the struct of the wlan card
+ * @param buf buffer to read to
+ * @param buf_size size of the buffer
+ * @param ri radiotap_rx info
+ * @return size read from the buffer
+ */
 static ssize_t
 linux_read (struct Hardware_Infos *dev, unsigned char *buf,     /* FIXME: void*? */
             size_t buf_size, struct Radiotap_rx *ri)
@@ -357,8 +420,9 @@ linux_read (struct Hardware_Infos *dev, unsigned char *buf,     /* FIXME: void*?
   return caplen;
 }
 
-
 /**
+ * function to open the device for read/write
+ * @param dev pointer to the device struct
  * @return 0 on success
  */
 static int
@@ -387,8 +451,7 @@ openraw (struct Hardware_Infos *dev)
   sll.sll_protocol = htons (ETH_P_ALL);
   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
   {
-    fprintf (stderr,
-             "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
+    fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
              IFNAMSIZ, dev->iface, strerror (errno));
     return 1;
   }
@@ -408,8 +471,7 @@ openraw (struct Hardware_Infos *dev)
        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) ||
       (wrq.u.mode != IW_MODE_MONITOR))
   {
-    fprintf (stderr,
-             "Error: interface `%.*s' is not in monitor mode\n",
+    fprintf (stderr, "Error: interface `%.*s' is not in monitor mode\n",
              IFNAMSIZ, dev->iface);
     return 1;
   }
@@ -432,9 +494,8 @@ openraw (struct Hardware_Infos *dev)
   /* bind the raw socket to the interface */
   if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll)))
   {
-    fprintf (stderr,
-             "Failed to bind interface `%.*s': %s\n",
-             IFNAMSIZ, dev->iface, strerror (errno));
+    fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
+             dev->iface, strerror (errno));
     return 1;
   }
 
@@ -453,8 +514,7 @@ openraw (struct Hardware_Infos *dev)
       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
   {
-    fprintf (stderr,
-             "Unsupported hardware link type %d on interface `%.*s'\n",
+    fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
              ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
     return 1;
   }
@@ -467,8 +527,7 @@ openraw (struct Hardware_Infos *dev)
       setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
                   sizeof (mr)))
   {
-    fprintf (stderr,
-             "Failed to enable promiscuous mode on interface `%.*s'\n",
+    fprintf (stderr, "Failed to enable promiscuous mode on interface `%.*s'\n",
              IFNAMSIZ, dev->iface);
     return 1;
   }
@@ -477,6 +536,9 @@ openraw (struct Hardware_Infos *dev)
 }
 
 /**
+ * function to prepare the helper, e.g. sockets, device...
+ * @param dev struct for the device
+ * @param iface name of the interface
  * @return 0 on success
  */
 static int
@@ -494,17 +556,16 @@ wlaninit (struct Hardware_Infos *dev, const char *iface)
   }
   if (dev->fd_raw >= FD_SETSIZE)
   {
-    fprintf (stderr,
-             "File descriptor too large for select (%d > %d)\n",
+    fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
              dev->fd_raw, FD_SETSIZE);
     close (dev->fd_raw);
     return 1;
   }
 
   /* mac80211 stack detection */
-  ret = snprintf (strbuf,
-                  sizeof (strbuf),
-                  "/sys/class/net/%s/phy80211/subsystem", iface);
+  ret =
+      snprintf (strbuf, sizeof (strbuf), "/sys/class/net/%s/phy80211/subsystem",
+                iface);
   if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
   {
     fprintf (stderr, "Did not find 802.11 interface `%s'. Exiting.\n", iface);
@@ -544,7 +605,7 @@ mac_test (const struct ieee80211_frame *u8aIeeeHeader,
 
 /**
  * function to set the wlan header to make attacks more difficult
- * @param buf buffer of the packet
+ * @param u8aIeeeHeader pointer to the header of the packet
  * @param dev pointer to the Hardware_Infos struct
  */
 static void
@@ -558,14 +619,12 @@ mac_set (struct ieee80211_frame *u8aIeeeHeader,
 
 }
 
-struct RadioTapheader
-{
-  struct ieee80211_radiotap_header header;
-  u8 rate;
-  u8 pad1;
-  u16 txflags;
-};
-
+/**
+ * function to process the data from the stdin
+ * @param cls pointer to the device struct
+ * @param client not used
+ * @param hdr pointer to the start of the packet
+ */
 static void
 stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
 {
@@ -582,6 +641,7 @@ stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
   rtheader.header.it_len = htole16 (0x0c);
   rtheader.header.it_present = htole32 (0x00008004);
   rtheader.rate = 0x00;
+  rtheader.pad1 = 0x00;
   rtheader.txflags =
       htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ);
 
@@ -597,7 +657,7 @@ stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
   if (sendsize <
       sizeof (struct Radiotap_Send) + sizeof (struct GNUNET_MessageHeader))
   {
-    fprintf (stderr, "Function stdin_send_hw: mailformed packet (too small)\n");
+    fprintf (stderr, "Function stdin_send_hw: malformed packet (too small)\n");
     exit (1);
   }
   sendsize -=
@@ -618,7 +678,7 @@ stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
   rtheader.rate = header->rate;
   memcpy (write_pout->buf, &rtheader, sizeof (rtheader));
   memcpy (write_pout->buf + sizeof (rtheader), &header[1], sendsize);
-  /* payload contains MAC address, but we don't trust it, so we'll 
+  /* payload contains MAC address, but we don't trust it, so we'll
    * overwrite it with OUR MAC address again to prevent mischief */
   wlanheader = (struct ieee80211_frame *) (write_pout->buf + sizeof (rtheader));
   mac_set (wlanheader, dev);
@@ -626,6 +686,12 @@ stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
 }
 
 #if 0
+/**
+ * Function to make test packets with special options
+ * @param buf buffer to write the data to
+ * @param dev device to send the data from
+ * @return size of packet (what should be send)
+ */
 static int
 maketest (unsigned char *buf, struct Hardware_Infos *dev)
 {
@@ -634,8 +700,7 @@ maketest (unsigned char *buf, struct Hardware_Infos *dev)
   static int first = 0;
 
   const int rate = 11000000;
-  static const char
-      txt[] =
+  static const char txt[] =
       "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
 
   unsigned char u8aRadiotap[] = { 0x00, 0x00,   // <-- radiotap version
@@ -686,14 +751,14 @@ maketest (unsigned char *buf, struct Hardware_Infos *dev)
   }
 
   tmp16 = (uint16_t *) u8aIeeeHeader.i_dur;
-  *tmp16
-      =
+  *tmp16 =
       (uint16_t)
       htole16 ((sizeof (txt) +
                 sizeof (struct ieee80211_frame) * 1000000) / rate + 290);
   tmp16 = (uint16_t *) u8aIeeeHeader.i_seq;
-  *tmp16 = (*tmp16 & IEEE80211_SEQ_FRAG_MASK) | (htole16 (seqenz)
-                                                 << IEEE80211_SEQ_SEQ_SHIFT);
+  *tmp16 =
+      (*tmp16 & IEEE80211_SEQ_FRAG_MASK) | (htole16 (seqenz) <<
+                                            IEEE80211_SEQ_SEQ_SHIFT);
   seqenz++;
 
   memcpy (buf, u8aRadiotap, sizeof (u8aRadiotap));
@@ -707,25 +772,11 @@ maketest (unsigned char *buf, struct Hardware_Infos *dev)
 
 
 /**
- * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin
- * @param buffer pointer to buffer for the message
- * @param mac pointer to the mac address
- * @return number of bytes written
+ * Function to start the hardware for the wlan helper
+ * @param argc number of arguments
+ * @param argv arguments
+ * @return returns one on error
  */
-// FIXME: use 'struct MacAddress' for 'mac' (everywhere in this file)
-static int
-send_mac_to_plugin (char *buffer, uint8_t * mac)
-{
-  struct Wlan_Helper_Control_Message macmsg;
-
-  macmsg.hdr.size = htons (sizeof (struct Wlan_Helper_Control_Message));
-  macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
-  memcpy (macmsg.mac.mac, mac, sizeof (struct MacAddress));
-  memcpy (buffer, &macmsg, sizeof (struct Wlan_Helper_Control_Message));
-  return sizeof (struct Wlan_Helper_Control_Message);
-}
-
-
 static int
 hardwaremode (int argc, char *argv[])
 {
@@ -795,9 +846,9 @@ hardwaremode (int argc, char *argv[])
 
     if (FD_ISSET (STDOUT_FILENO, &wfds))
     {
-      ret = write (STDOUT_FILENO,
-                   write_std.buf + write_std.pos,
-                   write_std.size - write_std.pos);
+      ret =
+          write (STDOUT_FILENO, write_std.buf + write_std.pos,
+                 write_std.size - write_std.pos);
       if (0 > ret)
       {
         fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
@@ -824,9 +875,8 @@ hardwaremode (int argc, char *argv[])
       dev.write_pout.pos += ret;
       if ((dev.write_pout.pos != dev.write_pout.size) && (ret != 0))
       {
-        fprintf (stderr,
-                 "Line %u: Write error, partial send: %u/%u\n", __LINE__,
-                 dev.write_pout.pos, dev.write_pout.size);
+        fprintf (stderr, "Line %u: Write error, partial send: %u/%u\n",
+                 __LINE__, dev.write_pout.pos, dev.write_pout.size);
         break;
       }
       if (dev.write_pout.pos == dev.write_pout.size)
@@ -849,8 +899,8 @@ hardwaremode (int argc, char *argv[])
         /* stop reading... */
         stdin_open = 0;
       }
-      GNUNET_SERVER_mst_receive (stdin_mst, NULL,
-                                 readbuf, ret, GNUNET_NO, GNUNET_NO);
+      GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, ret, GNUNET_NO,
+                                 GNUNET_NO);
     }
 
     if (FD_ISSET (dev.fd_raw, &rfds))
@@ -862,10 +912,10 @@ hardwaremode (int argc, char *argv[])
       header = (struct GNUNET_MessageHeader *) write_std.buf;
       rxinfo = (struct Radiotap_rx *) &header[1];
       datastart = (struct ieee80211_frame *) &rxinfo[1];
-      ret = linux_read (&dev,
-                        (unsigned char *) datastart,
-                        sizeof (write_std.buf) - sizeof (struct Radiotap_rx) -
-                        sizeof (struct GNUNET_MessageHeader), rxinfo);
+      ret =
+          linux_read (&dev, (unsigned char *) datastart,
+                      sizeof (write_std.buf) - sizeof (struct Radiotap_rx) -
+                      sizeof (struct GNUNET_MessageHeader), rxinfo);
       if (0 > ret)
       {
         fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
@@ -888,6 +938,12 @@ hardwaremode (int argc, char *argv[])
   return 1;
 }
 
+/**
+ * main function of the helper
+ * @param argc number of arguments
+ * @param argv arguments
+ * @return 0 on success, 1 on error
+ */
 int
 main (int argc, char *argv[])
 {
@@ -901,3 +957,84 @@ main (int argc, char *argv[])
   }
   return hardwaremode (argc, argv);
 }
+
+/*
+   *  Copyright (c) 2008, Thomas d'Otreppe
+   *
+   *  Common OSdep stuff
+   *
+   *  This program is free software; you can redistribute it and/or modify
+   *  it under the terms of the GNU General Public License as published by
+   *  the Free Software Foundation; either version 2 of the License, or
+   *  (at your option) any later version.
+   *
+   *  This program is distributed in the hope that it will be useful,
+   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   *  GNU General Public License for more details.
+   *
+   *  You should have received a copy of the GNU General Public License
+   *  along with this program; if not, write to the Free Software
+   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   */
+
+/**
+ * Return the frequency in Mhz from a channel number
+ * @param channel number of the channel
+ * @return frequency of the channel
+ */
+int
+getFrequencyFromChannel (int channel)
+{
+  static int frequencies[] = {
+    -1,                         // No channel 0
+    2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467,
+    2472, 2484,
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Nothing from channel 15 to 34 (exclusive)
+    5170, 5175, 5180, 5185, 5190, 5195, 5200, 5205, 5210, 5215, 5220, 5225,
+    5230, 5235, 5240, 5245,
+    5250, 5255, 5260, 5265, 5270, 5275, 5280, 5285, 5290, 5295, 5300, 5305,
+    5310, 5315, 5320, 5325,
+    5330, 5335, 5340, 5345, 5350, 5355, 5360, 5365, 5370, 5375, 5380, 5385,
+    5390, 5395, 5400, 5405,
+    5410, 5415, 5420, 5425, 5430, 5435, 5440, 5445, 5450, 5455, 5460, 5465,
+    5470, 5475, 5480, 5485,
+    5490, 5495, 5500, 5505, 5510, 5515, 5520, 5525, 5530, 5535, 5540, 5545,
+    5550, 5555, 5560, 5565,
+    5570, 5575, 5580, 5585, 5590, 5595, 5600, 5605, 5610, 5615, 5620, 5625,
+    5630, 5635, 5640, 5645,
+    5650, 5655, 5660, 5665, 5670, 5675, 5680, 5685, 5690, 5695, 5700, 5705,
+    5710, 5715, 5720, 5725,
+    5730, 5735, 5740, 5745, 5750, 5755, 5760, 5765, 5770, 5775, 5780, 5785,
+    5790, 5795, 5800, 5805,
+    5810, 5815, 5820, 5825, 5830, 5835, 5840, 5845, 5850, 5855, 5860, 5865,
+    5870, 5875, 5880, 5885,
+    5890, 5895, 5900, 5905, 5910, 5915, 5920, 5925, 5930, 5935, 5940, 5945,
+    5950, 5955, 5960, 5965,
+    5970, 5975, 5980, 5985, 5990, 5995, 6000, 6005, 6010, 6015, 6020, 6025,
+    6030, 6035, 6040, 6045,
+    6050, 6055, 6060, 6065, 6070, 6075, 6080, 6085, 6090, 6095, 6100
+  };
+
+  return ((channel > 0) &&
+          (channel <
+           sizeof (frequencies) / sizeof (int))) ? frequencies[channel] : -1;
+}
+
+/**
+ * Return the channel from the frequency (in Mhz)
+ * @param frequency of the channel
+ * @return number of the channel
+ */
+int
+getChannelFromFrequency (int frequency)
+{
+  if (frequency >= 2412 && frequency <= 2472)
+    return (frequency - 2407) / 5;
+  else if (frequency == 2484)
+    return 14;
+  else if (frequency >= 5000 && frequency <= 6100)
+    return (frequency - 5000) / 5;
+  else
+    return -1;
+}