b3468791dcebee8c875c301630d363c76523c0da
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010, 2011 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 /**
22  * @file src/transport/gnunet-transport-wlan-helper.c
23  * @brief wlan layer two server; must run as root (SUID will do)
24  *        This code will work under GNU/Linux only.
25  * @author David Brodski
26  *
27  * This program serves as the mediator between the wlan interface and
28  * gnunet
29  */
30
31 #define _GNU_SOURCE
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <sys/wait.h>
37 #include <sys/time.h>
38 #include <sys/stat.h>
39 #include <netpacket/packet.h>
40 #include <linux/if_ether.h>
41 #include <linux/if.h>
42 #include <linux/wireless.h>
43 #include <netinet/in.h>
44 #include <linux/if_tun.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <dirent.h>
52 //#include <sys/utsname.h>
53 #include <sys/param.h>
54
55 /*
56  //#include <resolv.h>
57  #include <string.h>
58  #include <utime.h>
59  #include <getopt.h>
60  */
61 //#include "platform.h"
62 #include "gnunet_constants.h"
63 #include "gnunet_os_lib.h"
64 #include "gnunet_transport_plugin.h"
65 #include "transport.h"
66 #include "gnunet_util_lib.h"
67 #include "plugin_transport_wlan.h"
68 #include "gnunet_common.h"
69 #include "gnunet-transport-wlan-helper.h"
70 #include "gnunet_crypto_lib.h"
71
72 #include "wlan/radiotap-parser.h"
73 /* radiotap-parser defines types like u8 that
74  * ieee80211_radiotap.h needs
75  *
76  * we use our local copy of ieee80211_radiotap.h
77  *
78  * - since we can't support extensions we don't understand
79  * - since linux does not include it in userspace headers
80  */
81 #include "wlan/ieee80211_radiotap.h"
82 #include "wlan/crctable_osdep.h"
83 #include "wlan/loopback_helper.h"
84 #include "wlan/ieee80211.h"
85
86 #define ARPHRD_IEEE80211        801
87 #define ARPHRD_IEEE80211_PRISM  802
88 #define ARPHRD_IEEE80211_FULL   803
89
90 #include "wlan/loopback_helper.h"
91
92 #define DEBUG 1
93
94 #define MAC_ADDR_SIZE 6
95
96 struct Hardware_Infos
97 {
98
99   struct sendbuf write_pout;
100   int fd_raw;
101   int arptype_in;
102
103   /**
104    * Name of the interface, not necessarily 0-terminated (!).
105    */
106   char iface[IFNAMSIZ];
107   unsigned char pl_mac[MAC_ADDR_SIZE];
108 };
109
110 // FIXME: inline?
111 int getChannelFromFrequency (int frequency);
112
113 // FIXME: make nice...
114 static unsigned long
115 calc_crc_osdep (unsigned char *buf, int len)
116 {
117   unsigned long crc = 0xFFFFFFFF;
118
119   for (; len > 0; len--, buf++)
120     crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
121
122   return (~crc);
123 }
124
125 /* CRC checksum verification routine */
126
127 // FIXME: make nice...
128 static int
129 check_crc_buf_osdep (unsigned char *buf, int len)
130 {
131   unsigned long crc;
132
133   if (0 > len)
134     return 0;
135
136   crc = calc_crc_osdep (buf, len);
137   buf += len;
138   return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
139           ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
140 }
141
142
143 // FIXME: make nice...
144 static int
145 linux_get_channel (struct Hardware_Infos *dev)
146 {
147   struct iwreq wrq;
148   int fd, frequency;
149   int chan = 0;
150
151   memset (&wrq, 0, sizeof (struct iwreq));
152
153   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
154
155   fd = dev->fd_raw;
156   if (0 > ioctl (fd, SIOCGIWFREQ, &wrq))
157     return (-1);
158
159   frequency = wrq.u.freq.m;
160   if (100000000 < frequency)
161     frequency /= 100000;
162   else if (1000000 < frequency)
163     frequency /= 1000;
164
165   if (1000 < frequency)
166     chan = getChannelFromFrequency (frequency);
167   else
168     chan = frequency;
169
170   return chan;
171 }
172
173
174 // FIXME: make nice...
175 static ssize_t
176 linux_read (struct Hardware_Infos *dev, unsigned char *buf,     /* FIXME: void*? */
177             size_t buf_size, struct Radiotap_rx *ri)
178 {
179   unsigned char tmpbuf[buf_size];
180   ssize_t caplen;
181   int n, got_signal, got_noise, got_channel, fcs_removed;
182
183   n = got_signal = got_noise = got_channel = fcs_removed = 0;
184
185   caplen = read (dev->fd_raw, tmpbuf, buf_size);
186   if (0 > caplen)
187   {
188     if (EAGAIN == errno)
189       return 0;
190     fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno));
191     return -1;
192   }
193
194   memset (buf, 0, buf_size);
195   memset (ri, 0, sizeof (*ri));
196
197   switch (dev->arptype_in)
198   {
199   case ARPHRD_IEEE80211_PRISM:
200   {
201     /* skip the prism header */
202     if (tmpbuf[7] == 0x40)
203     {
204       /* prism54 uses a different format */
205       ri->ri_power = tmpbuf[0x33];
206       ri->ri_noise = *(unsigned int *) (tmpbuf + 0x33 + 12);
207       ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x33 + 24)) * 500000;
208       got_signal = 1;
209       got_noise = 1;
210       n = 0x40;
211     }
212     else
213     {
214       ri->ri_mactime = *(u_int64_t *) (tmpbuf + 0x5C - 48);
215       ri->ri_channel = *(unsigned int *) (tmpbuf + 0x5C - 36);
216       ri->ri_power = *(unsigned int *) (tmpbuf + 0x5C);
217       ri->ri_noise = *(unsigned int *) (tmpbuf + 0x5C + 12);
218       ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x5C + 24)) * 500000;
219       got_channel = 1;
220       got_signal = 1;
221       got_noise = 1;
222       n = *(int *) (tmpbuf + 4);
223     }
224
225     if (n < 8 || n >= caplen)
226       return (0);
227   }
228     break;
229
230   case ARPHRD_IEEE80211_FULL:
231   {
232     struct ieee80211_radiotap_iterator iterator;
233     struct ieee80211_radiotap_header *rthdr;
234
235     rthdr = (struct ieee80211_radiotap_header *) tmpbuf;
236
237     if (ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen) < 0)
238       return (0);
239
240     /* go through the radiotap arguments we have been given
241      * by the driver
242      */
243
244     while (ieee80211_radiotap_iterator_next (&iterator) >= 0)
245     {
246
247       switch (iterator.this_arg_index)
248       {
249
250       case IEEE80211_RADIOTAP_TSFT:
251         ri->ri_mactime = le64_to_cpu (*((uint64_t *) iterator.this_arg));
252         break;
253
254       case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
255         if (!got_signal)
256         {
257           if (*iterator.this_arg < 127)
258             ri->ri_power = *iterator.this_arg;
259           else
260             ri->ri_power = *iterator.this_arg - 255;
261
262           got_signal = 1;
263         }
264         break;
265
266       case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
267         if (!got_signal)
268         {
269           if (*iterator.this_arg < 127)
270             ri->ri_power = *iterator.this_arg;
271           else
272             ri->ri_power = *iterator.this_arg - 255;
273
274           got_signal = 1;
275         }
276         break;
277
278       case IEEE80211_RADIOTAP_DBM_ANTNOISE:
279         if (!got_noise)
280         {
281           if (*iterator.this_arg < 127)
282             ri->ri_noise = *iterator.this_arg;
283           else
284             ri->ri_noise = *iterator.this_arg - 255;
285
286           got_noise = 1;
287         }
288         break;
289
290       case IEEE80211_RADIOTAP_DB_ANTNOISE:
291         if (!got_noise)
292         {
293           if (*iterator.this_arg < 127)
294             ri->ri_noise = *iterator.this_arg;
295           else
296             ri->ri_noise = *iterator.this_arg - 255;
297
298           got_noise = 1;
299         }
300         break;
301
302       case IEEE80211_RADIOTAP_ANTENNA:
303         ri->ri_antenna = *iterator.this_arg;
304         break;
305
306       case IEEE80211_RADIOTAP_CHANNEL:
307         ri->ri_channel = *iterator.this_arg;
308         got_channel = 1;
309         break;
310
311       case IEEE80211_RADIOTAP_RATE:
312         ri->ri_rate = (*iterator.this_arg) * 500000;
313         break;
314
315       case IEEE80211_RADIOTAP_FLAGS:
316         /* is the CRC visible at the end?
317          * remove
318          */
319         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
320         {
321           fcs_removed = 1;
322           caplen -= 4;
323         }
324
325         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_RX_BADFCS)
326           return (0);
327
328         break;
329       }
330     }
331     n = le16_to_cpu (rthdr->it_len);
332     if (n <= 0 || n >= caplen)
333       return 0;
334   }
335     break;
336   case ARPHRD_IEEE80211:
337     /* do nothing? */
338     break;
339   default:
340     errno = ENOTSUP;
341     return -1;
342   }
343
344   caplen -= n;
345
346   //detect fcs at the end, even if the flag wasn't set and remove it
347   if ((0 == fcs_removed) && (1 == check_crc_buf_osdep (tmpbuf + n, caplen - 4)))
348   {
349     caplen -= 4;
350   }
351   memcpy (buf, tmpbuf + n, caplen);
352   if (!got_channel)
353     ri->ri_channel = linux_get_channel (dev);
354
355   return caplen;
356 }
357
358
359 /**
360  * @return 0 on success
361  */
362 static int
363 openraw (struct Hardware_Infos *dev)
364 {
365   struct ifreq ifr;
366   struct iwreq wrq;
367   struct packet_mreq mr;
368   struct sockaddr_ll sll;
369
370   /* find the interface index */
371   memset (&ifr, 0, sizeof (ifr));
372   strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ);
373   if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr))
374   {
375     fprintf (stderr,
376              "Line: 381 ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n",
377              IFNAMSIZ, dev->iface, strerror (errno));
378     return 1;
379   }
380
381   /* lookup the hardware type */
382   memset (&sll, 0, sizeof (sll));
383   sll.sll_family = AF_PACKET;
384   sll.sll_ifindex = ifr.ifr_ifindex;
385   sll.sll_protocol = htons (ETH_P_ALL);
386   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
387   {
388     fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
389              IFNAMSIZ, dev->iface, strerror (errno));
390     return 1;
391   }
392
393   /* lookup iw mode */
394   memset (&wrq, 0, sizeof (struct iwreq));
395   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
396   if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq))
397   {
398     /* most probably not supported (ie for rtap ipw interface) *
399      * so just assume its correctly set...                     */
400     wrq.u.mode = IW_MODE_MONITOR;
401   }
402
403   if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
404        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
405        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) ||
406       (wrq.u.mode != IW_MODE_MONITOR))
407   {
408     fprintf (stderr, "Error: interface `%.*s' is not in monitor mode\n",
409              IFNAMSIZ, dev->iface);
410     return 1;
411   }
412
413   /* Is interface st to up, broadcast & running ? */
414   if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
415   {
416     /* Bring interface up */
417     ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
418
419     if (-1 == ioctl (dev->fd_raw, SIOCSIFFLAGS, &ifr))
420     {
421       fprintf (stderr,
422                "Line: 434 ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n",
423                IFNAMSIZ, dev->iface, strerror (errno));
424       return 1;
425     }
426   }
427
428   /* bind the raw socket to the interface */
429   if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll)))
430   {
431     fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
432              dev->iface, strerror (errno));
433     return 1;
434   }
435
436   /* lookup the hardware type */
437   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
438   {
439     fprintf (stderr,
440              "Line: 457 ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
441              IFNAMSIZ, dev->iface, strerror (errno));
442     return 1;
443   }
444
445   memcpy (dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE);
446   dev->arptype_in = ifr.ifr_hwaddr.sa_family;
447   if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
448       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
449       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
450   {
451     fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
452              ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
453     return 1;
454   }
455
456   /* enable promiscuous mode */
457   memset (&mr, 0, sizeof (mr));
458   mr.mr_ifindex = sll.sll_ifindex;
459   mr.mr_type = PACKET_MR_PROMISC;
460   if (0 !=
461       setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
462                   sizeof (mr)))
463   {
464     fprintf (stderr, "Failed to enable promiscuous mode on interface `%.*s'\n",
465              IFNAMSIZ, dev->iface);
466     return 1;
467   }
468
469   return 0;
470 }
471
472 /**
473  * @return 0 on success
474  */
475 static int
476 wlaninit (struct Hardware_Infos *dev, const char *iface)
477 {
478   char strbuf[512];
479   struct stat sbuf;
480   int ret;
481
482   dev->fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
483   if (0 > dev->fd_raw)
484   {
485     fprintf (stderr, "Failed to create raw socket: %s\n", strerror (errno));
486     return 1;
487   }
488   if (dev->fd_raw >= FD_SETSIZE)
489   {
490     fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
491              dev->fd_raw, FD_SETSIZE);
492     close (dev->fd_raw);
493     return 1;
494   }
495
496   /* mac80211 stack detection */
497   ret =
498       snprintf (strbuf, sizeof (strbuf), "/sys/class/net/%s/phy80211/subsystem",
499                 iface);
500   if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
501   {
502     fprintf (stderr, "Did not find 802.11 interface `%s'. Exiting.\n", iface);
503     close (dev->fd_raw);
504     return 1;
505   }
506   strncpy (dev->iface, iface, IFNAMSIZ);
507   if (0 != openraw (dev))
508   {
509     close (dev->fd_raw);
510     return 1;
511   }
512   return 0;
513 }
514
515
516 /**
517  * Function to test incoming packets mac for being our own.
518  *
519  * @param u8aIeeeHeader buffer of the packet
520  * @param dev the Hardware_Infos struct
521  * @return 0 if mac belongs to us, 1 if mac is for another target
522  */
523 static int
524 mac_test (const struct ieee80211_frame *u8aIeeeHeader,
525           const struct Hardware_Infos *dev)
526 {
527   if (0 != memcmp (u8aIeeeHeader->i_addr3, &mac_bssid, MAC_ADDR_SIZE))
528     return 1;
529   if (0 == memcmp (u8aIeeeHeader->i_addr1, dev->pl_mac, MAC_ADDR_SIZE))
530     return 0;
531   if (0 == memcmp (u8aIeeeHeader->i_addr1, &bc_all_mac, MAC_ADDR_SIZE))
532     return 0;
533   return 1;
534 }
535
536
537 /**
538  * function to set the wlan header to make attacks more difficult
539  * @param buf buffer of the packet
540  * @param dev pointer to the Hardware_Infos struct
541  */
542 static void
543 mac_set (struct ieee80211_frame *u8aIeeeHeader,
544          const struct Hardware_Infos *dev)
545 {
546   u8aIeeeHeader->i_fc[0] = 0x08;
547   u8aIeeeHeader->i_fc[1] = 0x00;
548   memcpy (u8aIeeeHeader->i_addr2, dev->pl_mac, MAC_ADDR_SIZE);
549   memcpy (u8aIeeeHeader->i_addr3, &mac_bssid, MAC_ADDR_SIZE);
550
551 }
552
553 struct RadioTapheader
554 {
555   struct ieee80211_radiotap_header header;
556   u8 rate;
557   u8 pad1;
558   u16 txflags;
559 };
560
561 static void
562 stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
563 {
564   struct Hardware_Infos *dev = cls;
565   struct sendbuf *write_pout = &dev->write_pout;
566   struct Radiotap_Send *header = (struct Radiotap_Send *) &hdr[1];
567   struct ieee80211_frame *wlanheader;
568   size_t sendsize;
569
570   // struct? // FIXME: make nice...
571   struct RadioTapheader rtheader;
572
573   rtheader.header.it_version = 0;
574   rtheader.header.it_len = htole16 (0x0c);
575   rtheader.header.it_present = htole32 (0x00008004);
576   rtheader.rate = 0x00;
577   rtheader.txflags =
578       htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ);
579
580   /*  { 0x00, 0x00, <-- radiotap version
581    * 0x0c, 0x00, <- radiotap header length
582    * 0x04, 0x80, 0x00, 0x00,  <-- bitmap
583    * 0x00,  <-- rate
584    * 0x00,  <-- padding for natural alignment
585    * 0x18, 0x00,  <-- TX flags
586    * }; */
587
588   sendsize = ntohs (hdr->size);
589   if (sendsize <
590       sizeof (struct Radiotap_Send) + sizeof (struct GNUNET_MessageHeader))
591   {
592     fprintf (stderr, "Function stdin_send_hw: mailformed packet (too small)\n");
593     exit (1);
594   }
595   sendsize -=
596       sizeof (struct Radiotap_Send) + sizeof (struct GNUNET_MessageHeader);
597
598   if (MAXLINE < sendsize)
599   {
600     fprintf (stderr, "Function stdin_send_hw: Packet too big for buffer\n");
601     exit (1);
602   }
603   if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
604   {
605     fprintf (stderr, "Function stdin_send: wrong packet type\n");
606     exit (1);
607   }
608
609   rtheader.header.it_len = htole16 (sizeof (rtheader));
610   rtheader.rate = header->rate;
611   memcpy (write_pout->buf, &rtheader, sizeof (rtheader));
612   memcpy (write_pout->buf + sizeof (rtheader), &header[1], sendsize);
613   /* payload contains MAC address, but we don't trust it, so we'll 
614    * overwrite it with OUR MAC address again to prevent mischief */
615   wlanheader = (struct ieee80211_frame *) (write_pout->buf + sizeof (rtheader));
616   mac_set (wlanheader, dev);
617   write_pout->size = sendsize + sizeof (rtheader);
618 }
619
620 #if 0
621 static int
622 maketest (unsigned char *buf, struct Hardware_Infos *dev)
623 {
624   uint16_t *tmp16;
625   static uint16_t seqenz = 0;
626   static int first = 0;
627
628   const int rate = 11000000;
629   static const char txt[] =
630       "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
631
632   unsigned char u8aRadiotap[] = { 0x00, 0x00,   // <-- radiotap version
633     0x00, 0x00,                 // <- radiotap header length
634     0x04, 0x80, 0x02, 0x00,     // <-- bitmap
635     0x00,                       // <-- rate
636     0x00,                       // <-- padding for natural alignment
637     0x10, 0x00,                 // <-- TX flags
638     0x04                        //retries
639   };
640
641   /*uint8_t u8aRadiotap[] =
642    * {
643    * 0x00, 0x00, // <-- radiotap version
644    * 0x19, 0x00, // <- radiotap header length
645    * 0x6f, 0x08, 0x00, 0x00, // <-- bitmap
646    * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // <-- timestamp
647    * 0x00, // <-- flags (Offset +0x10)
648    * 0x6c, // <-- rate (0ffset +0x11)
649    * 0x71, 0x09, 0xc0, 0x00, // <-- channel
650    * 0xde, // <-- antsignal
651    * 0x00, // <-- antnoise
652    * 0x01, // <-- antenna
653    * }; */
654
655   u8aRadiotap[8] = (rate / 500000);
656   u8aRadiotap[2] = htole16 (sizeof (u8aRadiotap));
657
658   static struct ieee80211_frame u8aIeeeHeader;
659
660   uint8_t u8aIeeeHeader_def[] = { 0x08, 0x00,   // Frame Control 0x08= 00001000 -> | b1,2 = 0 -> Version 0;
661     //      b3,4 = 10 -> Data; b5-8 = 0 -> Normal Data
662     //      0x01 = 00000001 -> | b1 = 1 to DS; b2 = 0 not from DS;
663     0x00, 0x00,                 // Duration/ID
664
665     //0x00, 0x1f, 0x3f, 0xd1, 0x8e, 0xe6, // mac1 - in this case receiver
666     0x00, 0x1d, 0xe0, 0xb0, 0x17, 0xdf, // mac1 - in this case receiver
667     0xC0, 0x3F, 0x0E, 0x44, 0x2D, 0x51, // mac2 - in this case sender
668     //0x02, 0x1d, 0xe0, 0x00, 0x01, 0xc4,
669     0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
670     0x10, 0x86,                 //Sequence Control
671   };
672   if (0 == first)
673   {
674     memcpy (&u8aIeeeHeader, u8aIeeeHeader_def, sizeof (struct ieee80211_frame));
675     memcpy (u8aIeeeHeader.i_addr2, dev->pl_mac, MAC_ADDR_SIZE);
676     first = 1;
677   }
678
679   tmp16 = (uint16_t *) u8aIeeeHeader.i_dur;
680   *tmp16 =
681       (uint16_t)
682       htole16 ((sizeof (txt) +
683                 sizeof (struct ieee80211_frame) * 1000000) / rate + 290);
684   tmp16 = (uint16_t *) u8aIeeeHeader.i_seq;
685   *tmp16 =
686       (*tmp16 & IEEE80211_SEQ_FRAG_MASK) | (htole16 (seqenz) <<
687                                             IEEE80211_SEQ_SEQ_SHIFT);
688   seqenz++;
689
690   memcpy (buf, u8aRadiotap, sizeof (u8aRadiotap));
691   memcpy (buf + sizeof (u8aRadiotap), &u8aIeeeHeader, sizeof (u8aIeeeHeader));
692   memcpy (buf + sizeof (u8aRadiotap) + sizeof (u8aIeeeHeader), txt,
693           sizeof (txt));
694   return sizeof (u8aRadiotap) + sizeof (u8aIeeeHeader) + sizeof (txt);
695
696 }
697 #endif
698
699
700 /**
701  * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin
702  * @param buffer pointer to buffer for the message
703  * @param mac pointer to the mac address
704  * @return number of bytes written
705  */
706 // FIXME: use 'struct MacAddress' for 'mac' (everywhere in this file)
707 static int
708 send_mac_to_plugin (char *buffer, uint8_t * mac)
709 {
710   struct Wlan_Helper_Control_Message macmsg;
711
712   macmsg.hdr.size = htons (sizeof (struct Wlan_Helper_Control_Message));
713   macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
714   memcpy (macmsg.mac.mac, mac, sizeof (struct MacAddress));
715   memcpy (buffer, &macmsg, sizeof (struct Wlan_Helper_Control_Message));
716   return sizeof (struct Wlan_Helper_Control_Message);
717 }
718
719
720 static int
721 hardwaremode (int argc, char *argv[])
722 {
723   uid_t uid;
724   struct Hardware_Infos dev;
725   char readbuf[MAXLINE];
726   struct sendbuf write_std;
727   ssize_t ret;
728   int maxfd;
729   fd_set rfds;
730   fd_set wfds;
731   int retval;
732   int stdin_open;
733   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
734
735   if (0 != wlaninit (&dev, argv[1]))
736     return 1;
737   uid = getuid ();
738   if (0 != setresuid (uid, uid, uid))
739   {
740     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
741     /* not critical, continue anyway */
742   }
743
744   dev.write_pout.size = 0;
745   dev.write_pout.pos = 0;
746   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send_hw, &dev);
747
748   /* send mac to STDOUT first */
749   write_std.pos = 0;
750   write_std.size = send_mac_to_plugin ((char *) &write_std.buf, dev.pl_mac);
751   stdin_open = 1;
752
753   while (1)
754   {
755     maxfd = -1;
756     FD_ZERO (&rfds);
757     if ((0 == dev.write_pout.size) && (1 == stdin_open))
758     {
759       FD_SET (STDIN_FILENO, &rfds);
760       maxfd = MAX (maxfd, STDIN_FILENO);
761     }
762     if (0 == write_std.size)
763     {
764       FD_SET (dev.fd_raw, &rfds);
765       maxfd = MAX (maxfd, dev.fd_raw);
766     }
767     FD_ZERO (&wfds);
768     if (0 < write_std.size)
769     {
770       FD_SET (STDOUT_FILENO, &wfds);
771       maxfd = MAX (maxfd, STDOUT_FILENO);
772     }
773     if (0 < dev.write_pout.size)
774     {
775       FD_SET (dev.fd_raw, &wfds);
776       maxfd = MAX (maxfd, dev.fd_raw);
777     }
778     retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
779     if ((-1 == retval) && (EINTR == errno))
780       continue;
781     if (0 > retval)
782     {
783       fprintf (stderr, "select failed: %s\n", strerror (errno));
784       break;
785     }
786
787     if (FD_ISSET (STDOUT_FILENO, &wfds))
788     {
789       ret =
790           write (STDOUT_FILENO, write_std.buf + write_std.pos,
791                  write_std.size - write_std.pos);
792       if (0 > ret)
793       {
794         fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
795         break;
796       }
797       write_std.pos += ret;
798       if (write_std.pos == write_std.size)
799       {
800         write_std.pos = 0;
801         write_std.size = 0;
802       }
803     }
804
805     if (FD_ISSET (dev.fd_raw, &wfds))
806     {
807       ret = write (dev.fd_raw, dev.write_pout.buf, dev.write_pout.size);
808       if (0 > ret)
809       {
810         fprintf (stderr,
811                  "Line %u: Failed to write to WLAN device: %s, Message-Size: %u\n",
812                  __LINE__, strerror (errno), dev.write_pout.size);
813         break;
814       }
815       dev.write_pout.pos += ret;
816       if ((dev.write_pout.pos != dev.write_pout.size) && (ret != 0))
817       {
818         fprintf (stderr, "Line %u: Write error, partial send: %u/%u\n",
819                  __LINE__, dev.write_pout.pos, dev.write_pout.size);
820         break;
821       }
822       if (dev.write_pout.pos == dev.write_pout.size)
823       {
824         dev.write_pout.pos = 0;
825         dev.write_pout.size = 0;
826       }
827     }
828
829     if (FD_ISSET (STDIN_FILENO, &rfds))
830     {
831       ret = read (STDIN_FILENO, readbuf, sizeof (readbuf));
832       if (0 > ret)
833       {
834         fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
835         break;
836       }
837       if (0 == ret)
838       {
839         /* stop reading... */
840         stdin_open = 0;
841       }
842       GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, ret, GNUNET_NO,
843                                  GNUNET_NO);
844     }
845
846     if (FD_ISSET (dev.fd_raw, &rfds))
847     {
848       struct GNUNET_MessageHeader *header;
849       struct Radiotap_rx *rxinfo;
850       struct ieee80211_frame *datastart;
851
852       header = (struct GNUNET_MessageHeader *) write_std.buf;
853       rxinfo = (struct Radiotap_rx *) &header[1];
854       datastart = (struct ieee80211_frame *) &rxinfo[1];
855       ret =
856           linux_read (&dev, (unsigned char *) datastart,
857                       sizeof (write_std.buf) - sizeof (struct Radiotap_rx) -
858                       sizeof (struct GNUNET_MessageHeader), rxinfo);
859       if (0 > ret)
860       {
861         fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
862         break;
863       }
864       if ((0 < ret) && (0 == mac_test (datastart, &dev)))
865       {
866         write_std.size =
867             ret + sizeof (struct GNUNET_MessageHeader) +
868             sizeof (struct Radiotap_rx);
869         header->size = htons (write_std.size);
870         header->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
871       }
872     }
873
874   }
875   /* Error handling, try to clean up a bit at least */
876   GNUNET_SERVER_mst_destroy (stdin_mst);
877   close (dev.fd_raw);
878   return 1;
879 }
880
881 int
882 main (int argc, char *argv[])
883 {
884   if (2 != argc)
885   {
886     fprintf (stderr,
887              "This program must be started with the interface as argument.\nThis program was compiled at ----- %s ----\n",
888              __TIMESTAMP__);
889     fprintf (stderr, "Usage: interface-name\n" "\n");
890     return 1;
891   }
892   return hardwaremode (argc, argv);
893 }