tests
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 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-wlan.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
32 #include "platform.h"
33 #include "gnunet_constants.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
37 #include "gnunet_util_lib.h"
38 #include "plugin_transport_wlan.h"
39 #include "gnunet_common.h"
40 #include "gnunet-transport-wlan-helper.h"
41 #include "ieee80211_radiotap.h"
42 #include <pcap.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/stat.h>
46
47
48
49
50
51 //#include "radiotap.h"
52
53 // mac of this node
54 char mac[] =
55   { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
56
57 /* wifi bitrate to use in 500kHz units */
58
59 static const u8 u8aRatesToUse[] = {
60
61         54*2,
62         48*2,
63         36*2,
64         24*2,
65         18*2,
66         12*2,
67         9*2,
68         11*2,
69         11, // 5.5
70         2*2,
71         1*2
72 };
73
74 #define OFFSET_FLAGS 0x10
75 #define OFFSET_RATE 0x11
76
77 // this is where we store a summary of the
78 // information from the radiotap header
79
80 typedef struct  {
81         int m_nChannel;
82         int m_nChannelFlags;
83         int m_nRate;
84         int m_nAntenna;
85         int m_nRadiotapFlags;
86 } __attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
87
88 void
89 Dump(u8 * pu8, int nLength)
90 {
91         char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
92         unsigned char baaLast[2][16];
93         uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
94
95         buf = szBuf;
96         szChar[0] = '\0';
97
98         for (n = 0; n < nLength; n++) {
99                 baaLast[(nLine&1)^1][n&0xf] = pu8[n];
100                 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
101                         szChar[n&0xf] = '.';
102                 else
103                         szChar[n&0xf] = pu8[n];
104                 szChar[(n&0xf)+1] = '\0';
105                 nPos += sprintf(&sz[nPos], "%02X ",
106                         baaLast[(nLine&1)^1][n&0xf]);
107                 if ((n&15) != 15)
108                         continue;
109                 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst)) {
110                         nSameCount++;
111                 } else {
112                         if (nSameCount)
113                                 buf += sprintf(buf, "(repeated %d times)\n",
114                                         nSameCount);
115                         buf += sprintf(buf, "%04x: %s %s\n",
116                                 nStart, sz, szChar);
117                         nSameCount = 0;
118                         printf("%s", szBuf);
119                         buf = szBuf;
120                 }
121                 nPos = 0; nStart = n+1; nLine++;
122                 fFirst = 0; sz[0] = '\0'; szChar[0] = '\0';
123         }
124         if (nSameCount)
125                 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
126
127         buf += sprintf(buf, "%04x: %s", nStart, sz);
128         if (n & 0xf) {
129                 *buf++ = ' ';
130                 while (n & 0xf) {
131                         buf += sprintf(buf, "   ");
132                         n++;
133                 }
134         }
135         buf += sprintf(buf, "%s\n", szChar);
136         printf("%s", szBuf);
137 }
138
139
140 void
141 usage(void)
142 {
143         printf(
144             "Usage: wlan-hwd [options] <interface>\n\nOptions\n"
145             "-f/--fcs           Mark as having FCS (CRC) already\n"
146             "                   (pkt ends with 4 x sacrificial - chars)\n"
147             "Example:\n"
148             "  echo -n mon0 > /sys/class/ieee80211/phy0/add_iface\n"
149             "  iwconfig mon0 mode monitor\n"
150             "  ifconfig mon0 up\n"
151             "  wlan-hwd mon0        Spam down mon0 with\n"
152             "                       radiotap header first\n"
153             "\n");
154         exit(1);
155 }
156
157 int flagHelp = 0, flagMarkWithFCS = 0;
158 int flagVerbose = 0;
159
160
161 /*
162  * Radiotap parser
163  *
164  * Copyright 2007               Andy Green <andy@warmcat.com>
165  */
166
167 /**
168  * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
169  * @param iterator: radiotap_iterator to initialize
170  * @param radiotap_header: radiotap header to parse
171  * @param max_length: total length we can parse into (eg, whole packet length)
172  *
173  * @return 0 or a negative error code if there is a problem.
174  *
175  * This function initializes an opaque iterator struct which can then
176  * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
177  * argument which is present in the header.  It knows about extended
178  * present headers and handles them.
179  *
180  * How to use:
181  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
182  * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
183  * checking for a good 0 return code.  Then loop calling
184  * __ieee80211_radiotap_iterator_next()... it returns either 0,
185  * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
186  * The iterator's this_arg member points to the start of the argument
187  * associated with the current argument index that is present, which can be
188  * found in the iterator's this_arg_index member.  This arg index corresponds
189  * to the IEEE80211_RADIOTAP_... defines.
190  *
191  * Radiotap header length:
192  * You can find the CPU-endian total radiotap header length in
193  * iterator->max_length after executing ieee80211_radiotap_iterator_init()
194  * successfully.
195  *
196  * Example code:
197  * See Documentation/networking/radiotap-headers.txt
198  */
199
200 int ieee80211_radiotap_iterator_init(
201     struct ieee80211_radiotap_iterator *iterator,
202     struct ieee80211_radiotap_header *radiotap_header,
203     int max_length)
204 {
205         /* Linux only supports version 0 radiotap format */
206         if (radiotap_header->it_version)
207                 return -EINVAL;
208
209         /* sanity check for allowed length and radiotap length field */
210         if (max_length < le16_to_cpu(radiotap_header->it_len))
211                 return -EINVAL;
212
213         iterator->rtheader = radiotap_header;
214         iterator->max_length = le16_to_cpu(radiotap_header->it_len);
215         iterator->arg_index = 0;
216         iterator->bitmap_shifter = le32_to_cpu(radiotap_header->it_present);
217         iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
218         iterator->this_arg = 0;
219
220         /* find payload start allowing for extended bitmap(s) */
221
222         if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
223                 while (le32_to_cpu(*((u32 *)iterator->arg)) &
224                                    (1<<IEEE80211_RADIOTAP_EXT)) {
225                         iterator->arg += sizeof(u32);
226
227                         /*
228                          * check for insanity where the present bitmaps
229                          * keep claiming to extend up to or even beyond the
230                          * stated radiotap header length
231                          */
232
233                         if (((ulong)iterator->arg -
234                              (ulong)iterator->rtheader) > iterator->max_length)
235                                 return -EINVAL;
236                 }
237
238                 iterator->arg += sizeof(u32);
239
240                 /*
241                  * no need to check again for blowing past stated radiotap
242                  * header length, because ieee80211_radiotap_iterator_next
243                  * checks it before it is dereferenced
244                  */
245         }
246
247         /* we are all initialized happily */
248
249         return 0;
250 }
251
252
253 /**
254  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
255  * @param iterator: radiotap_iterator to move to next arg (if any)
256  *
257  * @return 0 if there is an argument to handle,
258  * -ENOENT if there are no more args or -EINVAL
259  * if there is something else wrong.
260  *
261  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
262  * in this_arg_index and sets this_arg to point to the
263  * payload for the field.  It takes care of alignment handling and extended
264  * present fields.  this_arg can be changed by the caller (eg,
265  * incremented to move inside a compound argument like
266  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
267  * little-endian format whatever the endianess of your CPU.
268  */
269
270 int ieee80211_radiotap_iterator_next(
271     struct ieee80211_radiotap_iterator *iterator)
272 {
273
274         /*
275          * small length lookup table for all radiotap types we heard of
276          * starting from b0 in the bitmap, so we can walk the payload
277          * area of the radiotap header
278          *
279          * There is a requirement to pad args, so that args
280          * of a given length must begin at a boundary of that length
281          * -- but note that compound args are allowed (eg, 2 x u16
282          * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
283          * a reliable indicator of alignment requirement.
284          *
285          * upper nybble: content alignment for arg
286          * lower nybble: content length for arg
287          */
288
289         static const u8 rt_sizes[] = {
290                 [IEEE80211_RADIOTAP_TSFT] = 0x88,
291                 [IEEE80211_RADIOTAP_FLAGS] = 0x11,
292                 [IEEE80211_RADIOTAP_RATE] = 0x11,
293                 [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
294                 [IEEE80211_RADIOTAP_FHSS] = 0x22,
295                 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
296                 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
297                 [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
298                 [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
299                 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
300                 [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
301                 [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
302                 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
303                 [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11
304                 /*
305                  * add more here as they are defined in
306                  * include/net/ieee80211_radiotap.h
307                  */
308         };
309
310         /*
311          * for every radiotap entry we can at
312          * least skip (by knowing the length)...
313          */
314
315         while (iterator->arg_index < sizeof(rt_sizes)) {
316                 int hit = 0;
317                 int pad;
318
319                 if (!(iterator->bitmap_shifter & 1))
320                         goto next_entry; /* arg not present */
321
322                 /*
323                  * arg is present, account for alignment padding
324                  *  8-bit args can be at any alignment
325                  * 16-bit args must start on 16-bit boundary
326                  * 32-bit args must start on 32-bit boundary
327                  * 64-bit args must start on 64-bit boundary
328                  *
329                  * note that total arg size can differ from alignment of
330                  * elements inside arg, so we use upper nybble of length
331                  * table to base alignment on
332                  *
333                  * also note: these alignments are ** relative to the
334                  * start of the radiotap header **.  There is no guarantee
335                  * that the radiotap header itself is aligned on any
336                  * kind of boundary.
337                  */
338
339                 pad = (((ulong)iterator->arg) -
340                         ((ulong)iterator->rtheader)) &
341                         ((rt_sizes[iterator->arg_index] >> 4) - 1);
342
343                 if (pad)
344                         iterator->arg_index +=
345                                 (rt_sizes[iterator->arg_index] >> 4) - pad;
346
347                 /*
348                  * this is what we will return to user, but we need to
349                  * move on first so next call has something fresh to test
350                  */
351                 iterator->this_arg_index = iterator->arg_index;
352                 iterator->this_arg = iterator->arg;
353                 hit = 1;
354
355                 /* internally move on the size of this arg */
356                 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
357
358                 /*
359                  * check for insanity where we are given a bitmap that
360                  * claims to have more arg content than the length of the
361                  * radiotap section.  We will normally end up equalling this
362                  * max_length on the last arg, never exceeding it.
363                  */
364
365                 if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
366                     iterator->max_length)
367                         return -EINVAL;
368
369         next_entry:
370                 iterator->arg_index++;
371                 if (unlikely((iterator->arg_index & 31) == 0)) {
372                         /* completed current u32 bitmap */
373                         if (iterator->bitmap_shifter & 1) {
374                                 /* b31 was set, there is more */
375                                 /* move to next u32 bitmap */
376                                 iterator->bitmap_shifter =
377                                     le32_to_cpu(*iterator->next_bitmap);
378                                 iterator->next_bitmap++;
379                         } else {
380                                 /* no more bitmaps: end */
381                                 iterator->arg_index = sizeof(rt_sizes);
382                         }
383                 } else { /* just try the next bit */
384                         iterator->bitmap_shifter >>= 1;
385                 }
386
387                 /* if we found a valid arg earlier, return it now */
388                 if (hit)
389                         return 0;
390         }
391
392         /* we don't know how to handle any more args, we're done */
393         return -ENOENT;
394 }
395
396 #define FIFO_FILE1       "/tmp/MYFIFOin"
397 #define FIFO_FILE2       "/tmp/MYFIFOout"
398 #define MAXLINE         20
399
400 int first;
401 int closeprog = 0;
402
403 void sigfunc(int sig)
404 {
405
406  if(sig != SIGINT || sig != SIGTERM || sig != SIGKILL)
407    return;
408  else
409     {
410       closeprog = 1;
411
412       unlink(FIFO_FILE1);
413       unlink(FIFO_FILE2);
414
415     }
416 }
417
418
419
420 int
421 testmode(int argc, char *argv[])
422 {
423   struct stat st;
424   int erg;
425
426   int fd[2];
427   FILE *fpin;
428   FILE *fpout;
429   pid_t pid;
430
431
432
433   //make the fifos if needed
434   if (stat(FIFO_FILE1, &st) != 0)
435     {
436       if (stat(FIFO_FILE2, &st) == 0)
437         {
438           perror("FIFO 2 exists, but FIFO 1 not");
439           exit(1);
440         }
441       first = 1;
442       umask(0);
443       erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
444       erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
445
446       if ((fpin = fopen(FIFO_FILE1, "r")) == NULL)
447         {
448           perror("fopen");
449           exit(1);
450         }
451       if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)
452         {
453           perror("fopen");
454           exit(1);
455         }
456     }
457   else
458     {
459       first = 0;
460       if (stat(FIFO_FILE2, &st) != 0)
461         {
462           perror("FIFO 1 exists, but FIFO 2 not");
463           exit(1);
464         }
465       if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)
466         {
467           perror("fopen");
468           exit(1);
469         }
470       if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)
471         {
472           perror("fopen");
473           exit(1);
474         }
475
476     }
477
478   // fork
479
480   fd[0] = STDIN_FILENO;
481   fd[1] = STDOUT_FILENO;
482
483   if ((pid = fork()) < 0)
484     {
485       perror("FORK ERROR");
486
487       //clean up
488       if (first == 1)
489               {
490                 unlink(FIFO_FILE1);
491                 unlink(FIFO_FILE2);
492               }
493       fclose(fpin);
494       fclose(fpout);
495       return -3;
496     }
497   else if (pid == 0) // CHILD PROCESS
498     {
499
500       signal(SIGINT, sigfunc);
501       signal(SIGTERM, sigfunc);
502       signal(SIGKILL, sigfunc);
503       int rv = 0;
504       int readc = 0;
505       int pos = 0;
506       char line[MAXLINE];
507       int ret = 0;
508
509       while (closeprog == 0)
510         {
511           readc = 0;
512
513           //while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){
514             if ((rv = read(fd[0], line, MAXLINE)) < 0)
515               {
516                 perror("READ ERROR FROM STDIN");
517               }
518             readc += rv;
519           //}
520
521           if (closeprog == 1){
522             break;
523           }
524
525           pos = 0;
526
527
528           perror("writing blub");
529           //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);
530
531           //pos += sizeof(struct GNUNET_Mes#include "gnunet_util_lib.h"sageHeader);
532
533           //do not send radiotap header
534           //pos += sizeof( struct RadiotapHeader);
535
536           while (pos < readc)
537             {
538               ret = fwrite(&line[pos], 1, readc - pos, fpout);
539               if (ret < 0)
540                 {
541                   closeprog = 1;
542                   perror("Write ERROR FROM fpout");
543                   break;
544                 }
545               else
546                 {
547                   pos += ret;
548                 }
549             }
550         }
551
552
553       //clean up
554       fclose(fpout);
555     }
556   else // PARENT PROCESS
557     {
558       signal(SIGINT, sigfunc);
559       signal(SIGTERM, sigfunc);
560       signal(SIGKILL, sigfunc);
561       int rv = 0;
562       int ret = 0;
563       ssize_t pos = 0;
564       char line[MAXLINE];
565       struct Wlan_Helper_Control_Message macmsg;
566
567       //Send random mac address
568       macmsg.mac.mac[0] = 0x13;
569       macmsg.mac.mac[1] = 0x22;
570       macmsg.mac.mac[2] = 0x33;
571       macmsg.mac.mac[3] = 0x44;
572       macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
573       macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
574       macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
575       macmsg.hdr.type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL;
576
577       pos = 0;
578
579       while (pos < sizeof(struct Wlan_Helper_Control_Message))
580         {
581           pos += write(fd[1], &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos);
582         }
583
584       while (closeprog == 0)
585         {
586           if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)
587             {
588               perror("READ ERROR FROM fpin");
589             }
590           if (closeprog == 1){
591             break;
592           }
593           pos = 0;
594           while (pos < rv)
595             {
596
597               ret= write(fd[1], &line[pos], rv - pos);
598               if (ret < 0)
599                 {
600                   closeprog = 1;
601                   perror("Write ERROR FROM STDout");
602                   break;
603                 }
604               else
605                 {
606                   pos += ret;
607                 }
608             }
609         }
610
611
612       //clean up
613       fclose(fpin);
614
615       if (first == 1)
616         {
617           unlink(FIFO_FILE1);
618           unlink(FIFO_FILE2);
619         }
620     }
621
622   // Write the input to the output
623
624   return (0);
625 }
626
627
628 int
629 main(int argc, char *argv[])
630 {
631         if ((argc==2) && (strstr(argv[1],"1")))
632         {
633                 return testmode(argc, argv);
634         }
635 #if 0
636         u8 u8aSendBuffer[500];
637         char szErrbuf[PCAP_ERRBUF_SIZE];
638         int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
639         int nOrdinal = 0, r, nDelay = 100000;
640         int nRateIndex = 0, retval, bytes;
641         pcap_t *ppcap = NULL;
642         struct bpf_program bpfprogram;
643         char * szProgram = "", fBrokenSocket = 0;
644         u16 u16HeaderLen;
645         char szHostname[PATH_MAX];
646
647         if (gethostname(szHostname, sizeof (szHostname) - 1)) {
648                 perror("unable to get hostname");
649         }
650         szHostname[sizeof (szHostname) - 1] = '\0';
651
652
653         printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com>  GPL2\n");
654
655         while (1) {
656                 int nOptionIndex;
657                 static const struct option optiona[] = {
658                         { "delay", required_argument, NULL, 'd' },
659                         { "fcs", no_argument, &flagMarkWithFCS, 1 },
660                         { "help", no_argument, &flagHelp, 1 },
661                         { "verbose", no_argument, &flagVerbose, 1},
662                         { 0, 0, 0, 0 }
663                 };
664                 int c = getopt_long(argc, argv, "d:hf",
665                         optiona, &nOptionIndex);
666
667                 if (c == -1)
668                         break;
669                 switch (c) {
670                 case 0: // long option
671                         break;
672
673                 case 'h': // help
674                         usage();
675
676                 case 'd': // delay
677                         nDelay = atoi(optarg);
678                         break;
679
680                 case 'f': // mark as FCS attached
681                         flagMarkWithFCS = 1;
682                         break;
683
684                 case 'v': //Verbose / readable output to cout
685                         flagVerbose = 1;
686                         break;
687
688                 default:
689                         printf("unknown switch %c\n", c);
690                         usage();
691                         break;
692                 }
693         }
694
695         if (optind >= argc)
696                 usage();
697
698
699                 // open the interface in pcap
700
701         szErrbuf[0] = '\0';
702         ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
703         if (ppcap == NULL) {
704                 printf("Unable to open interface %s in pcap: %s\n",
705                     argv[optind], szErrbuf);
706                 return (1);
707         }
708
709         //get mac from interface
710
711         /*int sock, j, k;
712         char mac[32];
713
714         sock=socket(PF_INET, SOCK_STREAM, 0);
715         if (-1==sock) {
716                 perror("can not open socket\n");
717                 return 1;
718         }
719
720         if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
721                 perror("ioctl(SIOCGIFHWADDR) ");
722                 return 1;
723         }
724         for (j=0, k=0; j<6; j++) {
725                 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
726                         (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
727         }
728         mac[sizeof(mac)-1]='\0';
729         */
730
731         //get header type
732         nLinkEncap = pcap_datalink(ppcap);
733         nCaptureHeaderLength = 0;home/mwachs/gnb/bin/
734
735         switch (nLinkEncap) {
736
737                 case DLT_PRISM_HEADER:
738                         printf("DLT_PRISM_HEADER Encap\n");
739                         nCaptureHeaderLength = 0x40;
740                         n80211HeaderLength = 0x20; // ieee80211 comes after this
741                         szProgram = "radio[0x4a:4]==0x13223344";
742                         break;
743
744                 case DLT_IEEE802_11_RADIO:
745                         printf("DLT_IEEE802_11_RADIO Encap\n");
746                         nCaptureHeaderLength = 0x40;
747                         n80211HeaderLength = 0x18; // ieee80211 comes after this
748                         szProgram = "ether[0x0a:4]==0x13223344";
749                         break;
750
751                 default:
752                         printf("!!! unknown encapsulation on %s !\n", argv[1]);
753                         return (1);
754
755         }
756
757         if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1) {
758                 puts(szProgram);
759                 puts(pcap_geterr(ppcap));
760                 return (1);
761         } else {
762                 if (pcap_setfilter(ppcap, &bpfprogram) == -1) {
763                         puts(szProgram);
764                         puts(pcap_geterr(ppcap));
765                 } else {
766                         printf("RX Filter applied\n");
767                 }
768                 pcap_freecode(&bpfprogram);
769         }
770
771         pcap_setnonblock(ppcap, 1, szErrbuf);
772
773         printf("   (delay between packets %dus)\n", nDelay);
774
775         memset(u8aSendBuffer, 0, sizeof (u8aSendBuffer));
776
777         while (!fBrokenSocket) {
778                 u8 * pu8 = u8aSendBuffer;
779                 struct pcap_pkthdr * ppcapPacketHeader = NULL;
780                 struct ieee80211_radiotap_iterator rti;
781                 PENUMBRA_RADIOTAP_DATA prd;
782                 //init of the values
783                 prd.m_nRate = 255;
784                 prd.m_nChannel = 255;
785                 prd.m_nAntenna = 255;
786                 prd.m_nRadiotapFlags = 255;
787                 u8 * pu8Payload = u8aSendBuffer;
788                 int n, nRate;
789
790                 // receive
791
792                 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
793                     (const u_char**)&pu8Payload);
794
795                 if (retval < 0) {
796                         fBrokenSocket = 1;
797                         continue;
798                 }
799
800                 if (retval != 1)
801                         goto do_tx;
802
803                 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
804
805                 printf("rtap: ");
806                 Dump(pu8Payload, u16HeaderLen);
807
808                 if (ppcapPacketHeader->len <
809                     (u16HeaderLen + n80211HeaderLength))
810                         continue;
811
812                 bytes = ppcapPacketHeader->len -
813                         (u16HeaderLen + n80211HeaderLength);
814                 if (bytes < 0)
815                         continue;
816
817                 if (ieee80211_radiotap_iterator_init(&rti,
818                     (struct ieee80211_radiotap_header *)pu8Payload,
819                     bytes) < 0)
820                         continue;
821
822                 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0) {
823
824                         switch (rti.this_arg_index) {
825                         case IEEE80211_RADIOTAP_RATE:
826                                 prd.m_nRate = (*rti.this_arg);
827                                 break;
828
829                         case IEEE80211_RADIOTAP_CHANNEL:
830                                 prd.m_nChannel =
831                                     le16_to_cpu(*((u16 *)rti.this_arg));
832                                 prd.m_nChannelFlags =
833                                     le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
834                                 break;
835
836                         case IEEE80211_RADIOTAP_ANTENNA:
837                                 prd.m_nAntenna = (*rti.this_arg) + 1;
838                                 break;
839
840                         case IEEE80211_RADIOTAP_FLAGS:
841                                 prd.m_nRadiotapFlags = *rti.this_arg;
842                                 break;
843
844                         }
845                 }
846
847                 pu8Payload += u16HeaderLen + n80211HeaderLength;
848
849                 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
850                         bytes -= 4;
851
852                 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
853                     "Ant: %d, Flags: 0x%X\n",
854                     prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
855                     prd.m_nChannel / 1000,
856                     prd.m_nChannel - ((prd.m_nChannel / 1000) * 1000),
857                     prd.m_nAntenna,
858                     prd.m_nRadiotapFlags);
859
860                 Dump(pu8Payload, bytes);
861
862         do_tx:
863
864                 // transmit
865
866                 memcpy(u8aSendBuffer, u8aRadiotapHeader,
867                         sizeof (u8aRadiotapHeader));
868                 if (flagMarkWithFCS)
869                         pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
870                 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
871                 if (nRateIndex >= sizeof (u8aRatesToUse))
872                         nRateIndex = 0;
873                 pu8 += sizeof (u8aRadiotapHeader);
874
875                 memcpy(pu8, u8aIeeeHeader, sizeof (u8aIeeeHeader));
876                 pu8 += sizeof (u8aIeeeHeader);
877
878                 pu8 += sprintf((char *)u8aSendBuffer,
879                     "Packetspammer %02d"
880                     "broadcast packet"
881                     "#%05d -- :-D --%s ----",
882                     nRate/2, nOrdinal++, szHostname);
883                 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
884                 if (r != (pu8-u8aSendBuffer)) {
885                         perror("Trouble injecting packet");
886                         return (1);
887                 }
888                 if (nDelay)
889                         usleep(nDelay);
890         }
891
892
893 #endif
894         return (0);
895 }
896