make more use of testing lib
[oweals/gnunet.git] / src / transport / gnunet-helper-transport-wlan.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2010, 2011, 2012 GNUnet e.V.
4    Copyright (c) 2007, 2008, Andy Green <andy@warmcat.com>
5    Copyright Copyright (C) 2009 Thomas d'Otreppe
6
7    GNUnet is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3, or (at your
10    option) any later version.
11
12    GNUnet is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GNUnet; see the file COPYING.  If not, write to the
19    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 */
22 /**
23  * @file src/transport/gnunet-helper-transport-wlan.c
24  * @brief mediator between the wlan interface and gnunet; must run as root (SUID will do)
25  *        This code will work under GNU/Linux only.
26  * @author David Brodski
27  * @author Christian Grothoff
28  *
29  * This program will allow receiving and sending traffic from the WLAN
30  * interface.  It will force traffic to be in 'ad-hoc' mode, use the
31  * proper MAC address of the WLAN interface and use a GNUnet-specific
32  * SSID (and a GNUnet-specific SNAP header).  It only takes a single
33  * argument, which is the name of the WLAN interface to use.  The
34  * program detects if the interface is not a WLAN interface and exits
35  * with an error in that case.
36  *
37  * Once initialized, the program will first send a 'struct
38  * GNUNET_TRANSPORT_WLAN_HelperControlMessage' to 'stdout'.  That
39  * message contains the MAC address of the WLAN interface.  It will
40  * then read messages from the WLAN interface and send them together
41  * with performance information as 'struct
42  * GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage' messages to 'stdout'.
43  * Furthermore, it will read a stream of messages from 'stdin' that
44  * have the format from 'struct
45  * GNUNET_TRANSPORT_WLAN_RadiotapSendMessage'.  Those messages will
46  * then be sent via the WLAN interface; however, the sender MAC
47  * address will be forced to be the correct address from our WLAN
48  * card.  If 'stdin' closes, receiving from the WLAN interface will
49  * continue.  If 'stdout' causes a SIGPIPE, the process dies from the
50  * signal.  Errors cause an error message to be reported to 'stderr',
51  * in most cases the process also exits (with status code '1').  The
52  * program never terminates normally; it is safe to kill the
53  * process with SIGTERM or SIGKILL at any time.
54  *
55  * Since it uses RAW sockets, the binary must be installed SUID or run
56  * as 'root'.  In order to keep the security risk of the resulting
57  * SUID binary minimal, the program ONLY opens the RAW socket with
58  * root privileges, then drops them and only then starts to process
59  * command line arguments.  The code also does not link against any
60  * shared libraries (except libc) and is strictly minimal (except for
61  * checking for errors).  The following list of people have reviewed
62  * this code and considered it safe since the last modification (if
63  * you reviewed it, please have your name added to the list):
64  *
65  * - Christian Grothoff (Apr 3rd 2012)
66  */
67
68 /*-
69  * we use our local copy of ieee80211_radiotap.h
70  *
71  * - since we can't support extensions we don't understand
72  * - since linux does not include it in userspace headers
73  *
74  * Portions of this code were taken from the ieee80211_radiotap.h header,
75  * which is
76  *
77  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
78  *
79  * Redistribution and use in source and binary forms, with or without
80  * modification, are permitted provided that the following conditions
81  * are met:
82  * 1. Redistributions of source code must retain the above copyright
83  *    notice, this list of conditions and the following disclaimer.
84  * 2. Redistributions in binary form must reproduce the above copyright
85  *    notice, this list of conditions and the following disclaimer in the
86  *    documentation and/or other materials provided with the distribution.
87  * 3. The name of David Young may not be used to endorse or promote
88  *    products derived from this software without specific prior
89  *    written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
92  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
93  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
94  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
95  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
96  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
97  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
98  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
99  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
100  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
102  * OF SUCH DAMAGE.
103  */
104
105 /*
106  * Modifications to fit into the linux IEEE 802.11 stack,
107  * Mike Kershaw (dragorn@kismetwireless.net)
108  */
109 /*
110  * parts taken from aircrack-ng, parts changend.
111  */
112 #include "gnunet_config.h"
113 #define SOCKTYPE int
114 #define FDTYPE int
115 #include <sys/socket.h>
116 #include <sys/ioctl.h>
117 #include <sys/types.h>
118 #include <unistd.h>
119 #include <sys/wait.h>
120 #include <sys/time.h>
121 #include <sys/stat.h>
122 #include <netpacket/packet.h>
123 #include <linux/if_ether.h>
124 #include <linux/if.h>
125 #include <linux/wireless.h>
126 #include <netinet/in.h>
127 #include <linux/if_tun.h>
128 #include <stdio.h>
129 #include <stdlib.h>
130 #include <string.h>
131 #include <stdarg.h>
132 #include <fcntl.h>
133 #include <errno.h>
134 #include <dirent.h>
135 #include <sys/param.h>
136 #include <unistd.h>
137 #include <stdint.h>
138
139 #include "gnunet_protocols.h"
140 #include "plugin_transport_wlan.h"
141
142 /**
143  * Packet format type for the messages we receive from
144  * the kernel.  This is for Ethernet 10Mbps format (no
145  * performance information included).
146  */
147 #define ARPHRD_ETHER        1
148
149
150 /**
151  * Packet format type for the messages we receive from
152  * the kernel.  This is for plain messages (with no
153  * performance information included).
154  */
155 #define ARPHRD_IEEE80211        801
156
157
158 /**
159  * Packet format type for the messages we receive from
160  * the kernel.  This is for the PRISM format.
161  */
162 #define ARPHRD_IEEE80211_PRISM  802
163
164 /**
165  * Packet format type for the messages we receive from
166  * the kernel.  This is for messages with a
167  * 'struct Ieee80211RadiotapHeader' (see below).
168  */
169 #define ARPHRD_IEEE80211_FULL   803
170
171
172 /**
173  * Maximum size of a message allowed in either direction
174  * (used for our receive and sent buffers).
175  */
176 #define MAXLINE 4096
177
178
179
180 /* ********* structure of messages of type ARPHRD_IEEE80211_PRISM *********** */
181
182 /**
183  * Device name length in PRISM frames.
184  * (In the kernel, this is "WLAN_DEVNAMELEN_MAX")
185  */
186 #define PRISM_DEVICE_NAME_LENGTH 16
187
188 /**
189  * Monitor Frame (indicator that we have a 'struct PrismHeader').
190  */
191 #define PRISM_MSGCODE_MONITOR 0x0041
192
193 /**
194  * Mac time element.  In micro-seconds.
195  * Drivers appear to use a 64bit counter to hold mactime internal
196  * the then fill the prism header with the lower 32 bits
197  */
198 #define PRISM_DID_MACTIME 0x2041
199
200 /**
201  * Channel element
202  */
203 #define PRISM_DID_CHANNEL 0x3041
204
205 /**
206  * Signal element.  Should be the signal strength in dbm, some people
207  * suggest that instead "100 - (strength in dbm)" is used (to make this
208  * a positive integer).
209  */
210 #define PRISM_DID_SIGNAL 0x6041
211
212 /**
213  * Noise element
214  */
215 #define PRISM_DID_NOISE 0x7041
216
217 /**
218  * Rate element, in units/multiples of 500Khz
219  */
220 #define PRISM_DID_RATE 0x8041
221
222
223 /**
224  * Value is set (supplied)
225  */
226 #define PRISM_STATUS_OK 0
227
228 /**
229  * Value not supplied.
230  */
231 #define PRISM_STATUS_NO_VALUE 1
232
233
234 /**
235  * Values in the 'struct PrismHeader'.  All in host byte order (!).
236  */
237 struct PrismValue
238 {
239   /**
240    * This has a different ID for each parameter, see
241    * PRISM_DID_* constants.
242    */
243   uint32_t did;
244
245   /**
246    * See PRISM_STATUS_*-constants.  Note that they are unusual: 0 = set;  1 = not set
247    */
248   uint16_t status;
249
250   /**
251    * length of data (which is always a uint32_t, but presumably this can be used
252    * to specify that fewer bytes are used (with values in 'len' from 0-4).  We
253    * ignore this field.
254    */
255   uint16_t len;
256
257   /**
258    * The data value
259    */
260   uint32_t data;
261
262 } __attribute__ ((packed));
263
264
265 /**
266  * Prism header format ('struct p80211msg' in Linux).  All in host byte order (!).
267  */
268 struct PrismHeader
269 {
270   /**
271    * We expect this to be a PRISM_MSGCODE_*.
272    */
273   uint32_t msgcode;
274
275   /**
276    * The length of the entire header.
277    */
278   uint32_t msglen;
279
280   /**
281    * Name of the device that captured the packet.
282    */
283   char devname[PRISM_DEVICE_NAME_LENGTH];
284
285   /* followed by 'struct PrismValue's.  Documentation suggests that these
286      are typically the hosttime, mactime, channel, rssi, sq, signal, noise,
287      rate, istx and frmlen values, but documentation is sparse.  So we
288      will use the 'did' fields to find out what we actually got. */
289
290 } __attribute__ ((packed));
291
292
293 /* ******  end of structure of messages of type ARPHRD_IEEE80211_PRISM ******* */
294
295 /* ********** structure of messages of type ARPHRD_IEEE80211_FULL *********** */
296
297 /**
298  * Bits in the 'it_present' bitmask from the 'struct
299  * Ieee80211RadiotapHeader'.  For each value, we give the name, data
300  * type, unit and then a description below.  Note that the actual size
301  * of the extension can be bigger as arguments must be padded so that
302  * args of a given length must begin at a boundary of that length.
303  * However, note that compound args are allowed (eg, 2 x uint16_t for
304  * IEEE80211_RADIOTAP_CHANNEL) so total argument length is not a
305  * reliable indicator of alignment requirement.  See also
306  * 'man 9 ieee80211_radiotap'.
307  */
308 enum RadiotapType
309 {
310
311   /**
312    * IEEE80211_RADIOTAP_TSFT              __le64       microseconds
313    *
314    *      Value in microseconds of the MAC's 64-bit 802.11 Time
315    *      Synchronization Function timer when the first bit of the
316    *      MPDU arrived at the MAC. For received frames, only.
317    */
318   IEEE80211_RADIOTAP_TSFT = 0,
319
320   /**
321    * IEEE80211_RADIOTAP_FLAGS             uint8_t           bitmap
322    *
323    *      Properties of transmitted and received frames. See flags
324    *      defined below.
325    */
326   IEEE80211_RADIOTAP_FLAGS = 1,
327
328   /**
329    * IEEE80211_RADIOTAP_RATE              uint8_t           500kb/s
330    *
331    *      Tx/Rx data rate
332    */
333   IEEE80211_RADIOTAP_RATE = 2,
334
335   /**
336    * IEEE80211_RADIOTAP_CHANNEL           2 x __le16   MHz, bitmap
337    *
338    *      Tx/Rx frequency in MHz, followed by flags (see below).
339    */
340   IEEE80211_RADIOTAP_CHANNEL = 3,
341   /**
342    * IEEE80211_RADIOTAP_FHSS              __le16       see below
343    *
344    *      For frequency-hopping radios, the hop set (first byte)
345    *      and pattern (second byte).
346    */
347   IEEE80211_RADIOTAP_FHSS = 4,
348
349   /**
350    * IEEE80211_RADIOTAP_DBM_ANTSIGNAL     s8           decibels from
351    *                                                   one milliwatt (dBm)
352    *
353    *      RF signal power at the antenna, decibel difference from
354    *      one milliwatt.
355    */
356   IEEE80211_RADIOTAP_DBM_ANTSIGNAL = 5,
357
358   /**
359    * IEEE80211_RADIOTAP_DBM_ANTNOISE      s8           decibels from
360    *                                                   one milliwatt (dBm)
361    *
362    *      RF noise power at the antenna, decibel difference from one
363    *      milliwatt.
364    */
365   IEEE80211_RADIOTAP_DBM_ANTNOISE = 6,
366
367   /**
368    * IEEE80211_RADIOTAP_LOCK_QUALITY      __le16       unitless
369    *
370    *      Quality of Barker code lock. Unitless. Monotonically
371    *      nondecreasing with "better" lock strength. Called "Signal
372    *      Quality" in datasheets.  (Is there a standard way to measure
373    *      this?)
374    */
375   IEEE80211_RADIOTAP_LOCK_QUALITY = 7,
376
377   /**
378    * IEEE80211_RADIOTAP_TX_ATTENUATION    __le16       unitless
379    *
380    *      Transmit power expressed as unitless distance from max
381    *      power set at factory calibration.  0 is max power.
382    *      Monotonically nondecreasing with lower power levels.
383    */
384   IEEE80211_RADIOTAP_TX_ATTENUATION = 8,
385
386   /**
387    * IEEE80211_RADIOTAP_DB_TX_ATTENUATION __le16       decibels (dB)
388    *
389    *      Transmit power expressed as decibel distance from max power
390    *      set at factory calibration.  0 is max power.  Monotonically
391    *      nondecreasing with lower power levels.
392    */
393   IEEE80211_RADIOTAP_DB_TX_ATTENUATION = 9,
394
395   /**
396    * IEEE80211_RADIOTAP_DBM_TX_POWER      s8           decibels from
397    *                                                   one milliwatt (dBm)
398    *
399    *      Transmit power expressed as dBm (decibels from a 1 milliwatt
400    *      reference). This is the absolute power level measured at
401    *      the antenna port.
402    */
403   IEEE80211_RADIOTAP_DBM_TX_POWER = 10,
404
405   /**
406    * IEEE80211_RADIOTAP_ANTENNA           uint8_t           antenna index
407    *
408    *      Unitless indication of the Rx/Tx antenna for this packet.
409    *      The first antenna is antenna 0.
410    */
411   IEEE80211_RADIOTAP_ANTENNA = 11,
412
413   /**
414    * IEEE80211_RADIOTAP_DB_ANTSIGNAL      uint8_t           decibel (dB)
415    *
416    *      RF signal power at the antenna, decibel difference from an
417    *      arbitrary, fixed reference.
418    */
419   IEEE80211_RADIOTAP_DB_ANTSIGNAL = 12,
420
421   /**
422    * IEEE80211_RADIOTAP_DB_ANTNOISE       uint8_t           decibel (dB)
423    *
424    *      RF noise power at the antenna, decibel difference from an
425    *      arbitrary, fixed reference point.
426    */
427   IEEE80211_RADIOTAP_DB_ANTNOISE = 13,
428
429   /**
430    * IEEE80211_RADIOTAP_RX_FLAGS          __le16       bitmap
431    *
432    *     Properties of received frames. See flags defined below.
433    */
434   IEEE80211_RADIOTAP_RX_FLAGS = 14,
435
436   /**
437    * IEEE80211_RADIOTAP_TX_FLAGS          __le16       bitmap
438    *
439    *     Properties of transmitted frames. See flags defined below.
440    */
441   IEEE80211_RADIOTAP_TX_FLAGS = 15,
442
443   /**
444    * IEEE80211_RADIOTAP_RTS_RETRIES       uint8_t           data
445    *
446    *     Number of rts retries a transmitted frame used.
447    */
448   IEEE80211_RADIOTAP_RTS_RETRIES = 16,
449
450   /**
451    * IEEE80211_RADIOTAP_DATA_RETRIES      uint8_t           data
452    *
453    *     Number of unicast retries a transmitted frame used.
454    */
455   IEEE80211_RADIOTAP_DATA_RETRIES = 17,
456
457   /**
458    * Extension bit, used to indicate that more bits are needed for
459    * the bitmask.
460    */
461   IEEE80211_RADIOTAP_EXT = 31
462 };
463
464 /**
465  * Bitmask indicating an extension of the bitmask is used.
466  * (Mask corresponding to IEEE80211_RADIOTAP_EXT).
467  */
468 #define IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK (1 << IEEE80211_RADIOTAP_EXT)
469
470
471
472 /**
473  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
474  * as part of a 'struct Ieee80211RadiotapHeader' extension
475  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
476  * 'it_present').  The radiotap flags are an 8-bit field.
477  *
478  * Frame was sent/received during CFP (Contention Free Period)
479  */
480 #define IEEE80211_RADIOTAP_F_CFP        0x01
481
482 /**
483  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
484  * as part of a 'struct Ieee80211RadiotapHeader' extension
485  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
486  * 'it_present').  The radiotap flags are an 8-bit field.
487  *
488  * Frame was sent/received with short preamble
489  */
490 #define IEEE80211_RADIOTAP_F_SHORTPRE   0x02
491
492 /**
493  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
494  * as part of a 'struct Ieee80211RadiotapHeader' extension
495  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
496  * 'it_present').  The radiotap flags are an 8-bit field.
497  *
498  * Frame was sent/received with WEP encryption
499  */
500 #define IEEE80211_RADIOTAP_F_WEP        0x04
501
502 /**
503  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
504  * as part of a 'struct Ieee80211RadiotapHeader' extension
505  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
506  * 'it_present').  The radiotap flags are an 8-bit field.
507  *
508  * Frame was sent/received with fragmentation
509  */
510 #define IEEE80211_RADIOTAP_F_FRAG       0x08
511
512 /**
513  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
514  * as part of a 'struct Ieee80211RadiotapHeader' extension
515  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
516  * 'it_present').  The radiotap flags are an 8-bit field.
517  *
518  * Frame includes FCS (CRC at the end that needs to be removeD).
519  */
520 #define IEEE80211_RADIOTAP_F_FCS        0x10
521
522 /**
523  * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
524  * as part of a 'struct Ieee80211RadiotapHeader' extension
525  * if the IEEE80211_RADIOTAP_FLAGS bit is set in
526  * 'it_present').  The radiotap flags are an 8-bit field.
527  *
528  * Frame has padding between 802.11 header and payload
529  * (to 32-bit boundary)
530  */
531 #define IEEE80211_RADIOTAP_F_DATAPAD    0x20
532
533
534 /**
535  * For IEEE80211_RADIOTAP_RX_FLAGS:
536  * frame failed crc check
537  */
538 #define IEEE80211_RADIOTAP_F_RX_BADFCS  0x0001
539
540 /**
541  * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
542  * failed due to excessive retries
543  */
544 #define IEEE80211_RADIOTAP_F_TX_FAIL    0x0001
545
546 /**
547  * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
548  * used cts 'protection'
549  */
550 #define IEEE80211_RADIOTAP_F_TX_CTS     0x0002
551
552 /**
553  * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
554  * used rts/cts handshake
555  */
556 #define IEEE80211_RADIOTAP_F_TX_RTS     0x0004
557
558 /**
559  * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
560  * frame should not be ACKed
561  */
562 #define IEEE80211_RADIOTAP_F_TX_NOACK   0x0008
563
564 /**
565  * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
566  * sequence number handled by userspace
567  */
568 #define IEEE80211_RADIOTAP_F_TX_NOSEQ   0x0010
569
570
571 /**
572  * Generic header for radiotap messages (receiving and sending).  A
573  * bit mask (it_present) determines which specific records follow.
574  *
575  * I am trying to describe precisely what the application programmer
576  * should expect in the following, and for that reason I tell the
577  * units and origin of each measurement (where it applies), or else I
578  * use sufficiently weaselly language ("is a monotonically nondecreasing
579  * function of...") that I cannot set false expectations for lawyerly
580  * readers.
581  *
582  * The radio capture header precedes the 802.11 header.
583  * All data in the header is little endian on all platforms.
584  */
585 struct Ieee80211RadiotapHeader
586 {
587   /**
588    * Version 0. Only increases for drastic changes, introduction of
589    * compatible new fields does not count.
590    */
591   uint8_t it_version;
592
593   /**
594    * Padding.  Set to 0.
595    */
596   uint8_t it_pad;
597
598   /**
599    * length of the whole header in bytes, including it_version,
600    * it_pad, it_len, and data fields.
601    */
602   uint16_t it_len;
603
604   /**
605    * A bitmap telling which fields are present. Set bit 31
606    * (0x80000000) to extend the bitmap by another 32 bits.  Additional
607    * extensions are made by setting bit 31.
608    */
609   uint32_t it_present;
610 };
611
612
613 /**
614  * Format of the header we need to prepend to messages to be sent to the
615  * Kernel.
616  */
617 struct RadiotapTransmissionHeader
618 {
619
620   /**
621    * First we begin with the 'generic' header we also get when receiving
622    * messages.
623    */
624   struct Ieee80211RadiotapHeader header;
625
626   /**
627    * Transmission rate (we use 0, kernel makes up its mind anyway).
628    */
629   uint8_t rate;
630
631   /**
632    * Padding (we use 0).  There is a requirement to pad args, so that
633    * args of a given length must begin at a boundary of that length.
634    * As our next argument is the 'it_len' with 2 bytes, we need 1 byte
635    * of padding.
636    */
637   uint8_t pad1;
638
639   /**
640    * Transmission flags from on the IEEE80211_RADIOTAP_F_TX_* constant family.
641    */
642   uint16_t txflags;
643
644 };
645
646 /**
647  * The above 'struct RadiotapTransmissionHeader' should have the
648  * following value for 'header.it_present' based on the presence of
649  * the 'rate' and 'txflags' in the overall struct.
650  */
651 #define IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK ((1 << IEEE80211_RADIOTAP_RATE) | (1 << IEEE80211_RADIOTAP_TX_FLAGS))
652
653
654
655 /**
656  * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments
657  * in the radiotap header.  Used when we parse radiotap packets received from the kernel.
658  */
659 struct Ieee80211RadiotapHeaderIterator
660 {
661   /**
662    * pointer to the radiotap header we are walking through
663    */
664   const struct Ieee80211RadiotapHeader *rtheader;
665
666   /**
667    * pointer to current radiotap arg
668    */
669   const uint8_t *this_arg;
670
671   /**
672    * internal next argument pointer
673    */
674   const uint8_t *arg;
675
676   /**
677    * internal pointer to next present uint32_t (if IEEE80211_RADIOTAP_EXT is used).
678    */
679   const uint32_t *next_bitmap;
680
681   /**
682    * length of radiotap header in host byte ordering
683    */
684   size_t max_length;
685
686   /**
687    * internal shifter for current uint32_t bitmap, (it_present in host byte order),
688    * If bit 0 is set, the 'arg_index' argument is present.
689    */
690   uint32_t bitmap_shifter;
691
692   /**
693    * IEEE80211_RADIOTAP_... index of current arg
694    */
695   unsigned int this_arg_index;
696
697   /**
698    * internal next argument index
699    */
700   unsigned int arg_index;
701
702 };
703
704
705 /* ************** end of structure of ARPHRD_IEEE80211_FULL ************** */
706
707 /* ************************** our globals ******************************* */
708
709 /**
710  * struct for storing the information of the hardware.  There is only
711  * one of these.
712  */
713 struct HardwareInfos
714 {
715
716   /**
717    * file descriptor for the raw socket
718    */
719   int fd_raw;
720
721   /**
722    * Which format has the header that we're getting when receiving packets?
723    * Some  ARPHRD_IEEE80211_XXX-value.
724    */
725   int arptype_in;
726
727   /**
728    * Name of the interface, not necessarily 0-terminated (!).
729    */
730   char iface[IFNAMSIZ];
731
732   /**
733    * MAC address of our own WLAN interface.
734    */
735   struct GNUNET_TRANSPORT_WLAN_MacAddress pl_mac;
736 };
737
738
739 /**
740  * IO buffer used for buffering data in transit (to wireless or to stdout).
741  */
742 struct SendBuffer
743 {
744   /**
745    * How many bytes of data are stored in 'buf' for transmission right now?
746    * Data always starts at offset 0 and extends to 'size'.
747    */
748   size_t size;
749
750   /**
751    * How many bytes that were stored in 'buf' did we already write to the
752    * destination?  Always smaller than 'size'.
753    */
754   size_t pos;
755
756   /**
757    * Buffered data; twice the maximum allowed message size as we add some
758    * headers.
759    */
760   char buf[MAXLINE * 2];
761 };
762
763
764 /**
765  * Buffer for data read from stdin to be transmitted to the wirless card.
766  */
767 static struct SendBuffer write_pout;
768
769 /**
770  * Buffer for data read from the wireless card to be transmitted to stdout.
771  */
772 static struct SendBuffer write_std;
773
774
775
776 /* *********** specialized version of server_mst.c begins here ********** */
777
778 /**
779  * To what multiple do we align messages?  8 byte should suffice for everyone
780  * for now.
781  */
782 #define ALIGN_FACTOR 8
783
784 /**
785  * Smallest supported message.
786  */
787 #define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
788
789
790 /**
791  * Functions with this signature are called whenever a
792  * complete message is received by the tokenizer.
793  *
794  * @param cls closure
795  * @param message the actual message
796  */
797 typedef void (*MessageTokenizerCallback) (void *cls,
798                                           const struct
799                                           GNUNET_MessageHeader *
800                                           message);
801
802 /**
803  * Handle to a message stream tokenizer.
804  */
805 struct MessageStreamTokenizer
806 {
807
808   /**
809    * Function to call on completed messages.
810    */
811   MessageTokenizerCallback cb;
812
813   /**
814    * Closure for cb.
815    */
816   void *cb_cls;
817
818   /**
819    * Size of the buffer (starting at 'hdr').
820    */
821   size_t curr_buf;
822
823   /**
824    * How many bytes in buffer have we already processed?
825    */
826   size_t off;
827
828   /**
829    * How many bytes in buffer are valid right now?
830    */
831   size_t pos;
832
833   /**
834    * Beginning of the buffer.  Typed like this to force alignment.
835    */
836   struct GNUNET_MessageHeader *hdr;
837
838 };
839
840
841 /**
842  * Create a message stream tokenizer.
843  *
844  * @param cb function to call on completed messages
845  * @param cb_cls closure for cb
846  * @return handle to tokenizer
847  */
848 static struct MessageStreamTokenizer *
849 mst_create (MessageTokenizerCallback cb,
850             void *cb_cls)
851 {
852   struct MessageStreamTokenizer *ret;
853
854   ret = malloc (sizeof (struct MessageStreamTokenizer));
855   if (NULL == ret)
856   {
857     fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
858     exit (1);
859   }
860   ret->hdr = malloc (MIN_BUFFER_SIZE);
861   if (NULL == ret->hdr)
862   {
863     fprintf (stderr, "Failed to allocate buffer for alignment\n");
864     exit (1);
865   }
866   ret->curr_buf = MIN_BUFFER_SIZE;
867   ret->cb = cb;
868   ret->cb_cls = cb_cls;
869   return ret;
870 }
871
872
873 /**
874  * Add incoming data to the receive buffer and call the
875  * callback for all complete messages.
876  *
877  * @param mst tokenizer to use
878  * @param buf input data to add
879  * @param size number of bytes in buf
880  * @return GNUNET_OK if we are done processing (need more data)
881  *         GNUNET_SYSERR if the data stream is corrupt
882  */
883 static int
884 mst_receive (struct MessageStreamTokenizer *mst,
885              const char *buf, size_t size)
886 {
887   const struct GNUNET_MessageHeader *hdr;
888   size_t delta;
889   uint16_t want;
890   char *ibuf;
891   int need_align;
892   unsigned long offset;
893   int ret;
894
895   ret = GNUNET_OK;
896   ibuf = (char *) mst->hdr;
897   while (mst->pos > 0)
898   {
899 do_align:
900     if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
901         (0 != (mst->off % ALIGN_FACTOR)))
902     {
903       /* need to align or need more space */
904       mst->pos -= mst->off;
905       memmove (ibuf, &ibuf[mst->off], mst->pos);
906       mst->off = 0;
907     }
908     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
909     {
910       delta =
911           GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
912                       (mst->pos - mst->off), size);
913       GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
914       mst->pos += delta;
915       buf += delta;
916       size -= delta;
917     }
918     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
919     {
920       return GNUNET_OK;
921     }
922     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
923     want = ntohs (hdr->size);
924     if (want < sizeof (struct GNUNET_MessageHeader))
925     {
926       fprintf (stderr,
927                "Received invalid message from stdin\n");
928       exit (1);
929     }
930     if (mst->curr_buf - mst->off < want)
931     {
932       /* need more space */
933       mst->pos -= mst->off;
934       memmove (ibuf, &ibuf[mst->off], mst->pos);
935       mst->off = 0;
936     }
937     if (want > mst->curr_buf)
938     {
939       mst->hdr = realloc (mst->hdr, want);
940       if (NULL == mst->hdr)
941       {
942         fprintf (stderr, "Failed to allocate buffer for alignment\n");
943         exit (1);
944       }
945       ibuf = (char *) mst->hdr;
946       mst->curr_buf = want;
947     }
948     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
949     if (mst->pos - mst->off < want)
950     {
951       delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
952       GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
953       mst->pos += delta;
954       buf += delta;
955       size -= delta;
956     }
957     if (mst->pos - mst->off < want)
958     {
959       return GNUNET_OK;
960     }
961     mst->cb (mst->cb_cls, hdr);
962     mst->off += want;
963     if (mst->off == mst->pos)
964     {
965       /* reset to beginning of buffer, it's free right now! */
966       mst->off = 0;
967       mst->pos = 0;
968     }
969   }
970   while (size > 0)
971   {
972     if (size < sizeof (struct GNUNET_MessageHeader))
973       break;
974     offset = (unsigned long) buf;
975     need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
976     if (GNUNET_NO == need_align)
977     {
978       /* can try to do zero-copy and process directly from original buffer */
979       hdr = (const struct GNUNET_MessageHeader *) buf;
980       want = ntohs (hdr->size);
981       if (want < sizeof (struct GNUNET_MessageHeader))
982       {
983         fprintf (stderr,
984                  "Received invalid message from stdin\n");
985         exit (1);
986       }
987       if (size < want)
988         break;                  /* or not, buffer incomplete, so copy to private buffer... */
989       mst->cb (mst->cb_cls, hdr);
990       buf += want;
991       size -= want;
992     }
993     else
994     {
995       /* need to copy to private buffer to align;
996        * yes, we go a bit more spagetti than usual here */
997       goto do_align;
998     }
999   }
1000   if (size > 0)
1001   {
1002     if (size + mst->pos > mst->curr_buf)
1003     {
1004       mst->hdr = realloc (mst->hdr, size + mst->pos);
1005       if (NULL == mst->hdr)
1006       {
1007         fprintf (stderr, "Failed to allocate buffer for alignment\n");
1008         exit (1);
1009       }
1010       ibuf = (char *) mst->hdr;
1011       mst->curr_buf = size + mst->pos;
1012     }
1013     if (mst->pos + size > mst->curr_buf)
1014     {
1015       fprintf (stderr,
1016                "Assertion failed\n");
1017       exit (1);
1018     }
1019     GNUNET_memcpy (&ibuf[mst->pos], buf, size);
1020     mst->pos += size;
1021   }
1022   return ret;
1023 }
1024
1025
1026 /**
1027  * Destroys a tokenizer.
1028  *
1029  * @param mst tokenizer to destroy
1030  */
1031 static void
1032 mst_destroy (struct MessageStreamTokenizer *mst)
1033 {
1034   free (mst->hdr);
1035   free (mst);
1036 }
1037
1038 /* *****************  end of server_mst.c clone ***************** **/
1039
1040
1041 /* ************** code for handling of ARPHRD_IEEE80211_FULL ************** */
1042
1043 /**
1044  * Radiotap header iteration
1045  *
1046  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
1047  * struct Ieee80211RadiotapHeaderIterator (no need to init the struct beforehand)
1048  * then loop calling __ieee80211_radiotap_iterator_next()... it returns -1
1049  * if there are no more args in the header, or the next argument type index
1050  * that is present.  The iterator's this_arg member points to the start of the
1051  * argument associated with the current argument index that is present,
1052  * which can be found in the iterator's this_arg_index member.  This arg
1053  * index corresponds to the IEEE80211_RADIOTAP_... defines.
1054  *
1055  * @param iterator iterator to initialize
1056  * @param radiotap_header message to parse
1057  * @param max_length number of valid bytes in radiotap_header
1058  * @return 0 on success, -1 on error
1059  */
1060 static int
1061 ieee80211_radiotap_iterator_init (struct Ieee80211RadiotapHeaderIterator *iterator,
1062                                   const struct Ieee80211RadiotapHeader *radiotap_header,
1063                                   size_t max_length)
1064 {
1065   if ( (iterator == NULL) ||
1066        (radiotap_header == NULL) )
1067     return -1;
1068
1069   /* Linux only supports version 0 radiotap format */
1070   if (0 != radiotap_header->it_version)
1071     return -1;
1072
1073   /* sanity check for allowed length and radiotap length field */
1074   if ( (max_length < sizeof (struct Ieee80211RadiotapHeader)) ||
1075        (max_length < (GNUNET_le16toh (radiotap_header->it_len))) )
1076     return -1;
1077
1078   memset (iterator, 0, sizeof (struct Ieee80211RadiotapHeaderIterator));
1079   iterator->rtheader = radiotap_header;
1080   iterator->max_length = GNUNET_le16toh (radiotap_header->it_len);
1081   iterator->bitmap_shifter = GNUNET_le32toh (radiotap_header->it_present);
1082   iterator->arg = ((uint8_t *) radiotap_header) + sizeof (struct Ieee80211RadiotapHeader);
1083
1084   /* find payload start allowing for extended bitmap(s) */
1085   if (0 != (iterator->bitmap_shifter & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK))
1086   {
1087     while (GNUNET_le32toh (*((uint32_t *) iterator->arg)) & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK)
1088     {
1089       iterator->arg += sizeof (uint32_t);
1090       /*
1091        * check for insanity where the present bitmaps
1092        * keep claiming to extend up to or even beyond the
1093        * stated radiotap header length
1094        */
1095       if (iterator->arg - ((uint8_t*) iterator->rtheader) > iterator->max_length)
1096         return -1;
1097     }
1098     iterator->arg += sizeof (uint32_t);
1099     /*
1100      * no need to check again for blowing past stated radiotap
1101      * header length, becuase ieee80211_radiotap_iterator_next
1102      * checks it before it is dereferenced
1103      */
1104   }
1105   /* we are all initialized happily */
1106   return 0;
1107 }
1108
1109
1110 /**
1111  * Returns the next radiotap parser iterator arg.
1112  *
1113  * This function returns the next radiotap arg index (IEEE80211_RADIOTAP_...)
1114  * and sets iterator->this_arg to point to the payload for the arg.  It takes
1115  * care of alignment handling and extended present fields.  interator->this_arg
1116  * can be changed by the caller.  The args pointed to are in little-endian
1117  * format.
1118  *
1119  * @param iterator: radiotap_iterator to move to next arg (if any)
1120  * @return next present arg index on success or -1 if no more or error
1121  */
1122 static int
1123 ieee80211_radiotap_iterator_next (struct Ieee80211RadiotapHeaderIterator *iterator)
1124 {
1125   /*
1126    * small length lookup table for all radiotap types we heard of
1127    * starting from b0 in the bitmap, so we can walk the payload
1128    * area of the radiotap header
1129    *
1130    * There is a requirement to pad args, so that args
1131    * of a given length must begin at a boundary of that length
1132    * -- but note that compound args are allowed (eg, 2 x uint16_t
1133    * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
1134    * a reliable indicator of alignment requirement.
1135    *
1136    * upper nybble: content alignment for arg
1137    * lower nybble: content length for arg
1138    */
1139
1140   static const uint8_t rt_sizes[] = {
1141     [IEEE80211_RADIOTAP_TSFT] = 0x88,
1142     [IEEE80211_RADIOTAP_FLAGS] = 0x11,
1143     [IEEE80211_RADIOTAP_RATE] = 0x11,
1144     [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
1145     [IEEE80211_RADIOTAP_FHSS] = 0x22,
1146     [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
1147     [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
1148     [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
1149     [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
1150     [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
1151     [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
1152     [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
1153     [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
1154     [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11,
1155     [IEEE80211_RADIOTAP_TX_FLAGS] = 0x22,
1156     [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22,
1157     [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11,
1158     [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11
1159         /*
1160          * add more here as they are defined in
1161          * include/net/ieee80211_radiotap.h
1162          */
1163   };
1164
1165   /*
1166    * for every radiotap entry we can at
1167    * least skip (by knowing the length)...
1168    */
1169   while (iterator->arg_index < sizeof (rt_sizes))
1170   {
1171     int hit = (0 != (iterator->bitmap_shifter & 1));
1172
1173     if (hit)
1174     {
1175       unsigned int wanted_alignment;
1176       unsigned int unalignment;
1177       /*
1178        * arg is present, account for alignment padding
1179        *  8-bit args can be at any alignment
1180        * 16-bit args must start on 16-bit boundary
1181        * 32-bit args must start on 32-bit boundary
1182        * 64-bit args must start on 64-bit boundary
1183        *
1184        * note that total arg size can differ from alignment of
1185        * elements inside arg, so we use upper nybble of length table
1186        * to base alignment on.  First, 'wanted_alignment' is set to be
1187        * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit
1188        * arguments.  Then, we calculate the 'unalignment' (how many
1189        * bytes we are over by taking the difference of 'arg' and the
1190        * overall starting point modulo the desired alignment.  As
1191        * desired alignments are powers of two, we can do modulo with
1192        * binary "&" (and also avoid the possibility of a division by
1193        * zero if the 'rt_sizes' table contains bogus entries).
1194        *
1195        * also note: these alignments are relative to the start of the
1196        * radiotap header.  There is no guarantee that the radiotap
1197        * header itself is aligned on any kind of boundary, thus we
1198        * need to really look at the delta here.
1199        */
1200       wanted_alignment = rt_sizes[iterator->arg_index] >> 4;
1201       unalignment = (((void *) iterator->arg) - ((void *) iterator->rtheader)) & (wanted_alignment - 1);
1202       if (0 != unalignment)
1203       {
1204         /* need padding (by 'wanted_alignment - unalignment') */
1205         iterator->arg_index += wanted_alignment - unalignment;
1206       }
1207
1208       /*
1209        * this is what we will return to user, but we need to
1210        * move on first so next call has something fresh to test
1211        */
1212       iterator->this_arg_index = iterator->arg_index;
1213       iterator->this_arg = iterator->arg;
1214
1215       /* internally move on the size of this arg (using lower nybble from
1216          the table) */
1217       iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
1218
1219       /*
1220        * check for insanity where we are given a bitmap that
1221        * claims to have more arg content than the length of the
1222        * radiotap section.  We will normally end up equalling this
1223        * max_length on the last arg, never exceeding it.
1224        */
1225       if ((((void *) iterator->arg) - ((void *) iterator->rtheader)) > iterator->max_length)
1226         return -1;
1227     }
1228
1229     /* Now, move on to next bit / next entry */
1230     iterator->arg_index++;
1231
1232     if (0 == (iterator->arg_index % 32))
1233     {
1234       /* completed current uint32_t bitmap */
1235       if (0 != (iterator->bitmap_shifter & 1))
1236       {
1237         /* bit 31 was set, there is more; move to next uint32_t bitmap */
1238         iterator->bitmap_shifter = GNUNET_le32toh (*iterator->next_bitmap);
1239         iterator->next_bitmap++;
1240       }
1241       else
1242       {
1243         /* no more bitmaps: end (by setting arg_index to high, unsupported value) */
1244         iterator->arg_index = sizeof (rt_sizes);
1245       }
1246     }
1247     else
1248     {
1249       /* just try the next bit (while loop will move on) */
1250       iterator->bitmap_shifter >>= 1;
1251     }
1252
1253     /* if we found a valid arg earlier, return it now */
1254     if (hit)
1255       return iterator->this_arg_index;
1256   }
1257
1258   /* we don't know how to handle any more args (or there are no more),
1259      so we're done (this is not an error) */
1260   return -1;
1261 }
1262
1263
1264 /**
1265  * Calculate crc32, the start of the calculation
1266  *
1267  * @param buf buffer to calc the crc
1268  * @param len len of the buffer
1269  * @return crc sum
1270  */
1271 static unsigned long
1272 calc_crc_osdep (const unsigned char *buf, size_t len)
1273 {
1274   static const unsigned long int crc_tbl_osdep[256] = {
1275     0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
1276     0xE963A535, 0x9E6495A3,
1277     0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
1278     0xE7B82D07, 0x90BF1D91,
1279     0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB,
1280     0xF4D4B551, 0x83D385C7,
1281     0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
1282     0xFA0F3D63, 0x8D080DF5,
1283     0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447,
1284     0xD20D85FD, 0xA50AB56B,
1285     0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75,
1286     0xDCD60DCF, 0xABD13D59,
1287     0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
1288     0xCFBA9599, 0xB8BDA50F,
1289     0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11,
1290     0xC1611DAB, 0xB6662D3D,
1291     0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
1292     0x9FBFE4A5, 0xE8B8D433,
1293     0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
1294     0x91646C97, 0xE6635C01,
1295     0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B,
1296     0x8208F4C1, 0xF50FC457,
1297     0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49,
1298     0x8CD37CF3, 0xFBD44C65,
1299     0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
1300     0xA4D1C46D, 0xD3D6F4FB,
1301     0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
1302     0xAA0A4C5F, 0xDD0D7CC9,
1303     0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3,
1304     0xB966D409, 0xCE61E49F,
1305     0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
1306     0xB7BD5C3B, 0xC0BA6CAD,
1307     0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF,
1308     0x04DB2615, 0x73DC1683,
1309     0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D,
1310     0x0A00AE27, 0x7D079EB1,
1311     0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
1312     0x196C3671, 0x6E6B06E7,
1313     0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9,
1314     0x17B7BE43, 0x60B08ED5,
1315     0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767,
1316     0x3FB506DD, 0x48B2364B,
1317     0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
1318     0x316E8EEF, 0x4669BE79,
1319     0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
1320     0x220216B9, 0x5505262F,
1321     0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
1322     0x2CD99E8B, 0x5BDEAE1D,
1323     0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
1324     0x72076785, 0x05005713,
1325     0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D,
1326     0x7CDCEFB7, 0x0BDBDF21,
1327     0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B,
1328     0x6FB077E1, 0x18B74777,
1329     0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
1330     0x616BFFD3, 0x166CCF45,
1331     0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
1332     0x4969474D, 0x3E6E77DB,
1333     0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
1334     0x47B2CF7F, 0x30B5FFE9,
1335     0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
1336     0x54DE5729, 0x23D967BF,
1337     0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1,
1338     0x5A05DF1B, 0x2D02EF8D
1339   };
1340
1341   unsigned long crc = 0xFFFFFFFF;
1342
1343   for (; len > 0; len--, buf++)
1344     crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
1345   return (~crc);
1346 }
1347
1348
1349 /**
1350  * Calculate and check crc of the wlan packet
1351  *
1352  * @param buf buffer of the packet, with len + 4 bytes of data,
1353  *            the last 4 bytes being the checksum
1354  * @param len length of the payload in data
1355  * @return 0 on success (checksum matches), 1 on error
1356  */
1357 static int
1358 check_crc_buf_osdep (const unsigned char *buf, size_t len)
1359 {
1360   unsigned long crc;
1361
1362   crc = calc_crc_osdep (buf, len);
1363   buf += len;
1364   if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
1365       ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3])
1366     return 0;
1367   return 1;
1368 }
1369
1370
1371 /* ************end of code for handling of ARPHRD_IEEE80211_FULL ************** */
1372
1373
1374 /* ************beginning of code for reading packets from kernel ************** */
1375
1376 /**
1377  * Return the channel from the frequency (in Mhz)
1378  *
1379  * @param frequency of the channel
1380  * @return number of the channel
1381  */
1382 static int
1383 get_channel_from_frequency (int32_t frequency)
1384 {
1385   if (frequency >= 2412 && frequency <= 2472)
1386     return (frequency - 2407) / 5;
1387   if (frequency == 2484)
1388     return 14;
1389   if (frequency >= 5000 && frequency <= 6100)
1390     return (frequency - 5000) / 5;
1391   return -1;
1392 }
1393
1394
1395 /**
1396  * Get the channel used by our WLAN interface.
1397  *
1398  * @param dev pointer to the dev struct of the card
1399  * @return channel number, -1 on error
1400  */
1401 static int
1402 linux_get_channel (const struct HardwareInfos *dev)
1403 {
1404   struct iwreq wrq;
1405   int32_t frequency;
1406
1407   memset (&wrq, 0, sizeof (struct iwreq));
1408   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
1409   if (0 > ioctl (dev->fd_raw, SIOCGIWFREQ, &wrq))
1410     return -1;
1411   frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */
1412   if (100000000 < frequency)
1413     frequency /= 100000;
1414   else if (1000000 < frequency)
1415     frequency /= 1000;
1416   if (1000 < frequency)
1417     return get_channel_from_frequency (frequency);
1418   return frequency;
1419 }
1420
1421
1422 /**
1423  * Read from the raw socket (the wlan card), parse the packet and
1424  * put the result into the buffer for transmission to 'stdout'.
1425  *
1426  * @param dev pointer to the struct of the wlan card
1427  * @param buf buffer to read to; first bytes will be the 'struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame',
1428  *            followed by the actual payload
1429  * @param buf_size size of the buffer
1430  * @param ri where to write radiotap_rx info
1431  * @return number of bytes written to 'buf'
1432  */
1433 static ssize_t
1434 linux_read (struct HardwareInfos *dev,
1435             unsigned char *buf, size_t buf_size,
1436             struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri)
1437 {
1438   unsigned char tmpbuf[buf_size];
1439   ssize_t caplen;
1440   size_t n;
1441   int got_signal = 0;
1442   int got_noise = 0;
1443   int got_channel = 0;
1444   int fcs_removed = 0;
1445
1446   caplen = read (dev->fd_raw, tmpbuf, buf_size);
1447   if (0 > caplen)
1448   {
1449     if (EAGAIN == errno)
1450       return 0;
1451     fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno));
1452     return -1;
1453   }
1454
1455   memset (ri, 0, sizeof (*ri));
1456   switch (dev->arptype_in)
1457   {
1458   case ARPHRD_IEEE80211_PRISM:
1459     {
1460       const struct PrismHeader *ph;
1461
1462       ph = (const struct PrismHeader*) tmpbuf;
1463       n = ph->msglen;
1464       if ( (n < 8) || (n >= caplen) )
1465         return 0; /* invalid format */
1466       if ( (PRISM_MSGCODE_MONITOR == ph->msgcode) &&
1467            (n >= sizeof (struct PrismHeader)) )
1468       {
1469         const char *pos;
1470         size_t left;
1471         struct PrismValue pv;
1472         
1473         left = n - sizeof (struct PrismHeader);
1474         pos = (const char *) &ph[1];
1475         while (left > sizeof (struct PrismValue))
1476         {
1477           left -= sizeof (struct PrismValue);
1478           GNUNET_memcpy (&pv, pos, sizeof (struct PrismValue));
1479           pos += sizeof (struct PrismValue);
1480
1481           switch (pv.did)
1482           {
1483           case PRISM_DID_NOISE:
1484             if (PRISM_STATUS_OK == pv.status)
1485             {
1486               ri->ri_noise = pv.data;
1487               /* got_noise = 1; */
1488             }
1489             break;
1490           case PRISM_DID_RATE:
1491             if (PRISM_STATUS_OK == pv.status)
1492               ri->ri_rate = pv.data * 500000;
1493             break;
1494           case PRISM_DID_CHANNEL:
1495             if (PRISM_STATUS_OK == pv.status)
1496             {
1497               ri->ri_channel = pv.data;
1498               got_channel = 1;
1499             }
1500             break;
1501           case PRISM_DID_MACTIME:
1502             if (PRISM_STATUS_OK == pv.status)
1503               ri->ri_mactime = pv.data;
1504             break;
1505           case PRISM_DID_SIGNAL:
1506             if (PRISM_STATUS_OK == pv.status)
1507             {
1508               ri->ri_power = pv.data;
1509               /* got_signal = 1; */
1510             }
1511             break;
1512           }
1513         }
1514       }
1515       if ( (n < 8) || (n >= caplen) )
1516         return 0; /* invalid format */
1517     }
1518     break;
1519   case ARPHRD_IEEE80211_FULL:
1520     {
1521       struct Ieee80211RadiotapHeaderIterator iterator;
1522       struct Ieee80211RadiotapHeader *rthdr;
1523
1524       memset (&iterator, 0, sizeof (iterator));
1525       rthdr = (struct Ieee80211RadiotapHeader *) tmpbuf;
1526       n = GNUNET_le16toh (rthdr->it_len);
1527       if ( (n < sizeof (struct Ieee80211RadiotapHeader)) || (n >= caplen))
1528         return 0; /* invalid 'it_len' */
1529       if (0 != ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen))
1530         return 0;
1531       /* go through the radiotap arguments we have been given by the driver */
1532       while (0 <= ieee80211_radiotap_iterator_next (&iterator))
1533       {
1534         switch (iterator.this_arg_index)
1535         {
1536         case IEEE80211_RADIOTAP_TSFT:
1537           ri->ri_mactime = GNUNET_le64toh (*((uint64_t *) iterator.this_arg));
1538           break;
1539         case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1540           if (!got_signal)
1541           {
1542             ri->ri_power = * ((int8_t*) iterator.this_arg);
1543             got_signal = 1;     
1544           }
1545           break;
1546         case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1547           if (!got_signal)
1548           {
1549             ri->ri_power = * ((int8_t*) iterator.this_arg);
1550             got_signal = 1;
1551           }
1552           break;
1553         case IEEE80211_RADIOTAP_DBM_ANTNOISE:
1554           if (!got_noise)
1555           {
1556             ri->ri_noise = * ((int8_t*) iterator.this_arg);
1557             got_noise = 1;
1558           }
1559           break;
1560         case IEEE80211_RADIOTAP_DB_ANTNOISE:
1561           if (!got_noise)
1562           {
1563             ri->ri_noise = * ((int8_t*) iterator.this_arg);
1564             got_noise = 1;
1565           }
1566           break;
1567         case IEEE80211_RADIOTAP_ANTENNA:
1568           ri->ri_antenna = *iterator.this_arg;
1569           break;
1570         case IEEE80211_RADIOTAP_CHANNEL:
1571           ri->ri_channel = *iterator.this_arg;
1572           got_channel = 1;
1573           break;
1574         case IEEE80211_RADIOTAP_RATE:
1575           ri->ri_rate = (*iterator.this_arg) * 500000;
1576           break;
1577         case IEEE80211_RADIOTAP_FLAGS:
1578           {
1579             uint8_t flags = *iterator.this_arg;
1580             /* is the CRC visible at the end? if so, remove */
1581             if (0 != (flags & IEEE80211_RADIOTAP_F_FCS))
1582             {
1583               fcs_removed = 1;
1584               caplen -= sizeof (uint32_t);
1585             }
1586             break;
1587           }
1588         case IEEE80211_RADIOTAP_RX_FLAGS:
1589           {
1590             uint16_t flags = ntohs (* ((uint16_t *) iterator.this_arg));
1591             if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS))
1592               return 0;
1593           }
1594           break;
1595         } /* end of 'switch' */
1596       } /* end of the 'while' loop */
1597     }
1598     break;
1599   case ARPHRD_IEEE80211:
1600     n = 0; /* no header */
1601     break;
1602   case ARPHRD_ETHER:
1603     {
1604       if (sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen)
1605         return 0; /* invalid */
1606       GNUNET_memcpy (&buf[sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)],
1607               tmpbuf + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame),
1608               caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4 /* 4 byte FCS */);
1609       return caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4;
1610     }   
1611   default:
1612     errno = ENOTSUP; /* unsupported format */
1613     return -1;
1614   }
1615   caplen -= n;
1616   if (! got_channel)
1617     ri->ri_channel = linux_get_channel (dev);
1618
1619   /* detect CRC32 at the end, even if the flag wasn't set and remove it */
1620   if ( (0 == fcs_removed) &&
1621        (0 == check_crc_buf_osdep (tmpbuf + n, caplen - sizeof (uint32_t))) )
1622   {
1623     /* NOTE: this heuristic can of course fail if there happens to
1624        be a matching checksum at the end. Would be good to have
1625        some data to see how often this heuristic actually works. */
1626     caplen -= sizeof (uint32_t);
1627   }
1628   /* copy payload to target buffer */
1629   GNUNET_memcpy (buf, tmpbuf + n, caplen);
1630   return caplen;
1631 }
1632
1633
1634 /* ************end of code for reading packets from kernel ************** */
1635
1636 /* ************other helper functions for main start here ************** */
1637
1638
1639 /**
1640  * Open the wireless network interface for reading/writing.
1641  *
1642  * @param dev pointer to the device struct
1643  * @return 0 on success
1644  */
1645 static int
1646 open_device_raw (struct HardwareInfos *dev)
1647 {
1648   struct ifreq ifr;
1649   struct iwreq wrq;
1650   struct packet_mreq mr;
1651   struct sockaddr_ll sll;
1652
1653   /* find the interface index */
1654   memset (&ifr, 0, sizeof (ifr));
1655   strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ);
1656   if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr))
1657   {
1658     fprintf (stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n",
1659              IFNAMSIZ, dev->iface, strerror (errno));
1660     return 1;
1661   }
1662
1663   /* lookup the hardware type */
1664   memset (&sll, 0, sizeof (sll));
1665   sll.sll_family = AF_PACKET;
1666   sll.sll_ifindex = ifr.ifr_ifindex;
1667   sll.sll_protocol = htons (ETH_P_ALL);
1668   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
1669   {
1670     fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
1671              IFNAMSIZ, dev->iface, strerror (errno));
1672     return 1;
1673   }
1674   if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
1675        (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
1676        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
1677        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) )
1678   {
1679     fprintf (stderr, "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n",
1680              IFNAMSIZ, dev->iface,
1681              ifr.ifr_hwaddr.sa_family);
1682     return 1;
1683   }
1684
1685   /* lookup iw mode */
1686   memset (&wrq, 0, sizeof (struct iwreq));
1687   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
1688   if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq))
1689   {
1690     /* most probably not supported (ie for rtap ipw interface) *
1691      * so just assume its correctly set...                     */
1692     wrq.u.mode = IW_MODE_MONITOR;
1693   }
1694
1695   if ( (wrq.u.mode != IW_MODE_MONITOR) &&
1696        (wrq.u.mode != IW_MODE_ADHOC) )  
1697   {
1698     fprintf (stderr, "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n",
1699              IFNAMSIZ, dev->iface,
1700              wrq.u.mode);
1701     return 1;
1702   }
1703
1704   /* Is interface st to up, broadcast & running ? */
1705   if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
1706   {
1707     /* Bring interface up */
1708     ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
1709
1710     if (-1 == ioctl (dev->fd_raw, SIOCSIFFLAGS, &ifr))
1711     {
1712       fprintf (stderr, "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n",
1713                IFNAMSIZ, dev->iface, strerror (errno));
1714       return 1;
1715     }
1716   }
1717
1718   /* bind the raw socket to the interface */
1719   if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll)))
1720   {
1721     fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
1722              dev->iface, strerror (errno));
1723     return 1;
1724   }
1725
1726   /* lookup the hardware type */
1727   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
1728   {
1729     fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
1730              IFNAMSIZ, dev->iface, strerror (errno));
1731     return 1;
1732   }
1733
1734   GNUNET_memcpy (&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE);
1735   dev->arptype_in = ifr.ifr_hwaddr.sa_family;
1736   if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
1737       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
1738       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
1739       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
1740   {
1741     fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
1742              ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
1743     return 1;
1744   }
1745
1746   /* enable promiscuous mode */
1747   memset (&mr, 0, sizeof (mr));
1748   mr.mr_ifindex = sll.sll_ifindex;
1749   mr.mr_type = PACKET_MR_PROMISC;
1750   if (0 !=
1751       setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
1752                   sizeof (mr)))
1753   {
1754     fprintf (stderr,
1755              "Failed to enable promiscuous mode on interface `%.*s'\n",
1756              IFNAMSIZ,
1757              dev->iface);
1758     return 1;
1759   }
1760   return 0;
1761 }
1762
1763
1764 /**
1765  * Test if the given interface name really corresponds to a wireless
1766  * device.
1767  *
1768  * @param iface name of the interface
1769  * @return 0 on success, 1 on error
1770  */
1771 static int
1772 test_wlan_interface (const char *iface)
1773 {
1774   char strbuf[512];
1775   struct stat sbuf;
1776   int ret;
1777
1778   ret = snprintf (strbuf, sizeof (strbuf),
1779                   "/sys/class/net/%s/phy80211/subsystem",
1780                   iface);
1781   if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
1782   {
1783     fprintf (stderr,
1784              "Did not find 802.11 interface `%s'. Exiting.\n",
1785              iface);
1786     exit (1);
1787   }
1788   return 0;
1789 }
1790
1791
1792 /**
1793  * Test incoming packets mac for being our own.
1794  *
1795  * @param taIeeeHeader buffer of the packet
1796  * @param dev the Hardware_Infos struct
1797  * @return 0 if mac belongs to us, 1 if mac is for another target
1798  */
1799 static int
1800 mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1801           const struct HardwareInfos *dev)
1802 {
1803   static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros;
1804
1805   if ( (0 == memcmp (&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) ||
1806        (0 == memcmp (&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE)) )
1807     return 0; /* some drivers set no Macs, then assume it is all for us! */
1808
1809   if (0 != memcmp (&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE))
1810     return 1; /* not a GNUnet ad-hoc package */
1811   if ( (0 == memcmp (&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) ||
1812        (0 == memcmp (&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE)) )
1813     return 0; /* for us, or broadcast */
1814   return 1; /* not for us */
1815 }
1816
1817
1818 /**
1819  * Set the wlan header to sane values to make attacks more difficult
1820  *
1821  * @param taIeeeHeader pointer to the header of the packet
1822  * @param dev pointer to the Hardware_Infos struct
1823  */
1824 static void
1825 mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1826          const struct HardwareInfos *dev)
1827 {
1828   taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1829   taIeeeHeader->addr2 = dev->pl_mac;
1830   taIeeeHeader->addr3 = mac_bssid_gnunet;
1831 }
1832
1833
1834 /**
1835  * Process data from the stdin.  Takes the message, prepends the
1836  * radiotap transmission header, forces the sender MAC to be correct
1837  * and puts it into our buffer for transmission to the kernel.
1838  *
1839  * @param cls pointer to the device struct ('struct HardwareInfos*')
1840  * @param hdr pointer to the start of the packet
1841  */
1842 static void
1843 stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr)
1844 {
1845   struct HardwareInfos *dev = cls;
1846   const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header;
1847   struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *wlanheader;
1848   size_t sendsize;
1849   struct RadiotapTransmissionHeader rtheader;
1850   struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader;
1851
1852   sendsize = ntohs (hdr->size);
1853   if ( (sendsize <
1854         sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) ||
1855        (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) )
1856   {
1857     fprintf (stderr, "Received malformed message\n");
1858     exit (1);
1859   }
1860   sendsize -= (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
1861   if (MAXLINE < sendsize)
1862   {
1863     fprintf (stderr, "Packet too big for buffer\n");
1864     exit (1);
1865   }
1866   header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
1867   switch (dev->arptype_in)
1868   {
1869   case ARPHRD_IEEE80211_PRISM:
1870   case ARPHRD_IEEE80211_FULL:
1871   case ARPHRD_IEEE80211:
1872     rtheader.header.it_version = 0;
1873     rtheader.header.it_pad = 0;
1874     rtheader.header.it_len = GNUNET_htole16 (sizeof (rtheader));
1875     rtheader.header.it_present = GNUNET_htole16 (IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK);
1876     rtheader.rate = header->rate;
1877     rtheader.pad1 = 0;
1878     rtheader.txflags = GNUNET_htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ);
1879     GNUNET_memcpy (write_pout.buf, &rtheader, sizeof (rtheader));
1880     GNUNET_memcpy (&write_pout.buf[sizeof (rtheader)], &header->frame, sendsize);
1881     wlanheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) &write_pout.buf[sizeof (rtheader)];
1882
1883     /* payload contains MAC address, but we don't trust it, so we'll
1884      * overwrite it with OUR MAC address to prevent mischief */
1885     mac_set (wlanheader, dev);
1886     write_pout.size = sendsize + sizeof (rtheader);
1887     break;
1888   case ARPHRD_ETHER:
1889     etheader.dst = header->frame.addr1;
1890     /* etheader.src = header->frame.addr2; --- untrusted input */
1891     etheader.src = dev->pl_mac;
1892     etheader.type = htons (ETH_P_IP);
1893     GNUNET_memcpy (write_pout.buf, &etheader, sizeof (etheader));
1894     GNUNET_memcpy (&write_pout.buf[sizeof (etheader)], &header[1], sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
1895     write_pout.size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) + sizeof (etheader);
1896     break;
1897   default:
1898     fprintf (stderr,
1899              "Unsupported ARPTYPE!\n");
1900     break;
1901   }
1902 }
1903
1904
1905 /**
1906  * Main function of the helper.  This code accesses a WLAN interface
1907  * in monitoring mode (layer 2) and then forwards traffic in both
1908  * directions between the WLAN interface and stdin/stdout of this
1909  * process.  Error messages are written to stdout.
1910  *
1911  * @param argc number of arguments, must be 2
1912  * @param argv arguments only argument is the name of the interface (i.e. 'mon0')
1913  * @return 0 on success (never happens, as we don't return unless aborted), 1 on error
1914  */
1915 int
1916 main (int argc, char *argv[])
1917 {
1918   struct HardwareInfos dev;
1919   char readbuf[MAXLINE];
1920   int maxfd;
1921   fd_set rfds;
1922   fd_set wfds;
1923   int stdin_open;
1924   struct MessageStreamTokenizer *stdin_mst;
1925   int raw_eno;
1926
1927   /* assert privs so we can modify the firewall rules! */
1928   {
1929 #ifdef HAVE_SETRESUID
1930     uid_t uid = getuid ();
1931
1932     if (0 != setresuid (uid, 0, 0))
1933     {
1934       fprintf (stderr,
1935                "Failed to setresuid to root: %s\n", 
1936                strerror (errno));
1937       return 254;
1938     }
1939 #else
1940     if (0 != seteuid (0))
1941     {
1942       fprintf (stderr, 
1943                "Failed to seteuid back to root: %s\n", strerror (errno));
1944       return 254;
1945     }
1946 #endif
1947   }
1948
1949   /* make use of SGID capabilities on POSIX */
1950   memset (&dev, 0, sizeof (dev));
1951   dev.fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
1952   raw_eno = errno; /* remember for later */
1953
1954   /* now that we've dropped root rights, we can do error checking */
1955   if (2 != argc)
1956   {
1957     fprintf (stderr,
1958              "You must specify the name of the interface as the first and only argument to this program.\n");
1959     if (-1 != dev.fd_raw)
1960       (void) close (dev.fd_raw);
1961     return 1;
1962   }
1963
1964   if (-1 == dev.fd_raw)
1965   {
1966     fprintf (stderr, "Failed to create raw socket: %s\n", strerror (raw_eno));
1967     return 1;
1968   }
1969   if (dev.fd_raw >= FD_SETSIZE)
1970   {
1971     fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
1972              dev.fd_raw, FD_SETSIZE);
1973     (void) close (dev.fd_raw);
1974     return 1;
1975   }
1976   if (0 != test_wlan_interface (argv[1]))
1977   {
1978     (void) close (dev.fd_raw);
1979     return 1;
1980   }
1981   strncpy (dev.iface, argv[1], IFNAMSIZ);
1982   if (0 != open_device_raw (&dev))
1983   {
1984     (void) close (dev.fd_raw);
1985     return 1;
1986   }
1987
1988   /* drop privs */
1989   {
1990     uid_t uid = getuid ();
1991 #ifdef HAVE_SETRESUID
1992     if (0 != setresuid (uid, uid, uid))
1993     {
1994       fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
1995       if (-1 != dev.fd_raw)
1996         (void) close (dev.fd_raw);
1997       return 1;
1998     }
1999 #else
2000     if (0 != (setuid (uid) | seteuid (uid)))
2001     {
2002       fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
2003       if (-1 != dev.fd_raw)
2004         (void) close (dev.fd_raw);
2005       return 1;
2006     }
2007 #endif
2008   }
2009
2010
2011   /* send MAC address of the WLAN interface to STDOUT first */
2012   {
2013     struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
2014
2015     macmsg.hdr.size = htons (sizeof (macmsg));
2016     macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
2017     GNUNET_memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
2018     GNUNET_memcpy (write_std.buf, &macmsg, sizeof (macmsg));
2019     write_std.size = sizeof (macmsg);
2020   }
2021
2022   stdin_mst = mst_create (&stdin_send_hw, &dev);
2023   stdin_open = 1;
2024   while (1)
2025   {
2026     maxfd = -1;
2027     FD_ZERO (&rfds);
2028     if ((0 == write_pout.size) && (1 == stdin_open))
2029     {
2030       FD_SET (STDIN_FILENO, &rfds);
2031       maxfd = MAX (maxfd, STDIN_FILENO);
2032     }
2033     if (0 == write_std.size)
2034     {
2035       FD_SET (dev.fd_raw, &rfds);
2036       maxfd = MAX (maxfd, dev.fd_raw);
2037     }
2038     FD_ZERO (&wfds);
2039     if (0 < write_std.size)
2040     {
2041       FD_SET (STDOUT_FILENO, &wfds);
2042       maxfd = MAX (maxfd, STDOUT_FILENO);
2043     }
2044     if (0 < write_pout.size)
2045     {
2046       FD_SET (dev.fd_raw, &wfds);
2047       maxfd = MAX (maxfd, dev.fd_raw);
2048     }
2049     {
2050       int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
2051       if ((-1 == retval) && (EINTR == errno))
2052         continue;
2053       if (0 > retval)
2054       {
2055         fprintf (stderr, "select failed: %s\n", strerror (errno));
2056         break;
2057       }
2058     }
2059     if (FD_ISSET (STDOUT_FILENO, &wfds))
2060     {
2061       ssize_t ret =
2062           write (STDOUT_FILENO, write_std.buf + write_std.pos,
2063                  write_std.size - write_std.pos);
2064       if (0 > ret)
2065       {
2066         fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
2067         break;
2068       }
2069       write_std.pos += ret;
2070       if (write_std.pos == write_std.size)
2071       {
2072         write_std.pos = 0;
2073         write_std.size = 0;
2074       }
2075     }
2076     if (FD_ISSET (dev.fd_raw, &wfds))
2077     {
2078       ssize_t ret =
2079         write (dev.fd_raw, write_pout.buf + write_std.pos,
2080                write_pout.size - write_pout.pos);
2081       if (0 > ret)
2082       {
2083         fprintf (stderr, "Failed to write to WLAN device: %s\n",
2084                  strerror (errno));
2085         break;
2086       }
2087       write_pout.pos += ret;
2088       if ((write_pout.pos != write_pout.size) && (0 != ret))
2089       {
2090         /* we should not get partial sends with packet-oriented devices... */
2091         fprintf (stderr, "Write error, partial send: %u/%u\n",
2092                  (unsigned int) write_pout.pos,
2093                  (unsigned int) write_pout.size);
2094         break;
2095       }
2096       if (write_pout.pos == write_pout.size)
2097       {
2098         write_pout.pos = 0;
2099         write_pout.size = 0;
2100       }
2101     }
2102
2103     if (FD_ISSET (STDIN_FILENO, &rfds))
2104     {
2105       ssize_t ret =
2106         read (STDIN_FILENO, readbuf, sizeof (readbuf));
2107       if (0 > ret)
2108       {
2109         fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
2110         break;
2111       }
2112       if (0 == ret)
2113       {
2114         /* stop reading... */
2115         stdin_open = 0;
2116       }
2117       mst_receive (stdin_mst, readbuf, ret);
2118     }
2119
2120     if (FD_ISSET (dev.fd_raw, &rfds))
2121     {
2122       struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
2123       ssize_t ret;
2124
2125       rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
2126       ret =
2127           linux_read (&dev, (unsigned char *) &rrm->frame,
2128                       sizeof (write_std.buf)
2129                       - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
2130                       + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
2131                       rrm);
2132       if (0 > ret)
2133       {
2134         fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
2135         break;
2136       }
2137       if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
2138       {
2139         write_std.size = ret
2140           + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
2141           - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
2142         rrm->header.size = htons (write_std.size);
2143         rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
2144       }
2145     }
2146   }
2147   /* Error handling, try to clean up a bit at least */
2148   mst_destroy (stdin_mst);
2149   (void) close (dev.fd_raw);
2150   return 1;                     /* we never exit 'normally' */
2151 }
2152
2153 /* end of gnunet-helper-transport-wlan.c */