aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-transport-wlan-helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-transport-wlan-helper.c')
-rw-r--r--src/transport/gnunet-transport-wlan-helper.c548
1 files changed, 23 insertions, 525 deletions
diff --git a/src/transport/gnunet-transport-wlan-helper.c b/src/transport/gnunet-transport-wlan-helper.c
index 2cb14b4c6..633b2776c 100644
--- a/src/transport/gnunet-transport-wlan-helper.c
+++ b/src/transport/gnunet-transport-wlan-helper.c
@@ -131,37 +131,14 @@ struct Hardware_Infos
131 struct sendbuf *write_pout; 131 struct sendbuf *write_pout;
132 int fd_in, arptype_in; 132 int fd_in, arptype_in;
133 int fd_out; 133 int fd_out;
134 //int arptype_out;
135 //int fd_main;
136 //int fd_rtc;
137 134
138 DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */ 135 DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */
139 136
140 //FILE *f_cap_in;
141
142 //struct pcap_file_header pfh_in;
143
144 //int sysfs_inject;
145 //int channel;
146 //int freq;
147 //int rate;
148 //int tx_power;
149 //char *wlanctlng; /* XXX never set */
150 //char *iwpriv;
151 //char *iwconfig;
152 //char *ifconfig;
153 char *iface; 137 char *iface;
154 //char *main_if;
155 unsigned char pl_mac[6]; 138 unsigned char pl_mac[6];
156//int inject_wlanng;
157}; 139};
158 140
159//#include "radiotap.h"
160 141
161// mac of this node
162/*char mac[] =
163 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
164 */
165/* wifi bitrate to use in 500kHz units */ 142/* wifi bitrate to use in 500kHz units */
166 143
167/* 144/*
@@ -171,87 +148,13 @@ struct Hardware_Infos
171 54 * 2, 48 * 2, 36 * 2, 24 * 2, 18 * 2, 12 * 2, 9 * 2, 11 * 2, 11, // 5.5 148 54 * 2, 48 * 2, 36 * 2, 24 * 2, 18 * 2, 12 * 2, 9 * 2, 11 * 2, 11, // 5.5
172 2 * 2, 1 * 2 }; 149 2 * 2, 1 * 2 };
173 150
174 #define OFFSET_FLAGS 0x10 151*/
175 #define OFFSET_RATE 0x11
176 */
177// this is where we store a summary of the
178// information from the radiotap header
179
180/*
181 typedef struct
182 {
183 int m_nChannel;
184 int m_nChannelFlags;
185 int m_nRate;
186 int m_nAntenna;
187 int m_nRadiotapFlags;
188 }__attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
189 */
190static void 152static void
191sigfunc_hw(int sig) 153sigfunc_hw(int sig)
192{ 154{
193 closeprog = 1; 155 closeprog = 1;
194} 156}
195 157
196/*
197 void
198 Dump(u8 * pu8, int nLength)
199 {
200 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
201 unsigned char baaLast[2][16];
202 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
203
204 buf = szBuf;
205 szChar[0] = '\0';
206
207 for (n = 0; n < nLength; n++)
208 {
209 baaLast[(nLine & 1) ^ 1][n & 0xf] = pu8[n];
210 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
211 szChar[n & 0xf] = '.';
212 else
213 szChar[n & 0xf] = pu8[n];
214 szChar[(n & 0xf) + 1] = '\0';
215 nPos += sprintf(&sz[nPos], "%02X ", baaLast[(nLine & 1) ^ 1][n & 0xf]);
216 if ((n & 15) != 15)
217 continue;
218 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst))
219 {
220 nSameCount++;
221 }
222 else
223 {
224 if (nSameCount)
225 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
226 buf += sprintf(buf, "%04x: %s %s\n", nStart, sz, szChar);
227 nSameCount = 0;
228 printf("%s", szBuf);
229 buf = szBuf;
230 }
231 nPos = 0;
232 nStart = n + 1;
233 nLine++;
234 fFirst = 0;
235 sz[0] = '\0';
236 szChar[0] = '\0';
237 }
238 if (nSameCount)
239 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
240
241 buf += sprintf(buf, "%04x: %s", nStart, sz);
242 if (n & 0xf)
243 {
244 *buf++ = ' ';
245 while (n & 0xf)
246 {
247 buf += sprintf(buf, " ");
248 n++;
249 }
250 }
251 buf += sprintf(buf, "%s\n", szChar);
252 printf("%s", szBuf);
253 }
254 */
255static void 158static void
256usage() 159usage()
257{ 160{
@@ -280,7 +183,7 @@ check_crc_buf_osdep(unsigned char *buf, int len)
280{ 183{
281 unsigned long crc; 184 unsigned long crc;
282 185
283 if (len < 0) 186 if (0 > len)
284 return 0; 187 return 0;
285 188
286 crc = calc_crc_osdep(buf, len); 189 crc = calc_crc_osdep(buf, len);
@@ -289,88 +192,6 @@ check_crc_buf_osdep(unsigned char *buf, int len)
289 >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]); 192 >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
290} 193}
291 194
292/* Search a file recursively */
293/*
294 static char *
295 searchInside(const char * dir, const char * filename)
296 {
297 char * ret;
298 char * curfile;
299 struct stat sb;
300 int len, lentot;
301 DIR *dp;
302 struct dirent *ep;
303
304 dp = opendir(dir);
305 if (dp == NULL)
306 {
307 return NULL;
308 }
309
310 len = strlen(filename);
311 lentot = strlen(dir) + 256 + 2;
312 curfile = (char *) calloc(1, lentot);
313
314 while ((ep = readdir(dp)) != NULL)
315 {
316
317 memset(curfile, 0, lentot);
318 sprintf(curfile, "%s/%s", dir, ep->d_name);
319
320 //Checking if it's the good file
321 if ((int) strlen(ep->d_name) == len && !strcmp(ep->d_name, filename))
322 {
323 (void) closedir(dp);
324 return curfile;
325 }
326 lstat(curfile, &sb);
327
328 //If it's a directory and not a link, try to go inside to search
329 if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode))
330 {
331 //Check if the directory isn't "." or ".."
332 if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
333 {
334 //Recursive call
335 ret = searchInside(curfile, filename);
336 if (ret != NULL)
337 {
338 (void) closedir(dp);
339 free(curfile);
340 return ret;
341 }
342 }
343 }
344 }
345 (void) closedir(dp);
346 free(curfile);
347 return NULL;
348 }
349 */
350/* Search a wireless tool and return its path */
351/*
352 static char *
353 wiToolsPath(const char * tool)
354 {
355 char * path;
356 int i, nbelems;
357 static const char * paths[] =
358 { "/sbin", "/usr/sbin", "/usr/local/sbin", "/bin", "/usr/bin",
359 "/usr/local/bin", "/tmp" };
360
361 nbelems = sizeof(paths) / sizeof(char *);
362
363 for (i = 0; i < nbelems; i++)
364 {
365 path = searchInside(paths[i], tool);
366 if (path != NULL)
367 return path;
368 }
369
370 return NULL;
371 }
372 */
373
374static int 195static int
375linux_get_channel(struct Hardware_Infos *dev) 196linux_get_channel(struct Hardware_Infos *dev)
376{ 197{
@@ -380,27 +201,19 @@ linux_get_channel(struct Hardware_Infos *dev)
380 201
381 memset(&wrq, 0, sizeof(struct iwreq)); 202 memset(&wrq, 0, sizeof(struct iwreq));
382 203
383 /*
384 if (dev->main_if)
385 strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ );
386 else*/
387 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ ); 204 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ );
388 205
389 fd = dev->fd_in; 206 fd = dev->fd_in;
390 /* 207 if (0 > ioctl(fd, SIOCGIWFREQ, &wrq))
391 if (dev->drivertype == DT_IPW2200)
392 fd = dev->fd_main;
393 */
394 if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0)
395 return (-1); 208 return (-1);
396 209
397 frequency = wrq.u.freq.m; 210 frequency = wrq.u.freq.m;
398 if (frequency > 100000000) 211 if (100000000 < frequency )
399 frequency /= 100000; 212 frequency /= 100000;
400 else if (frequency > 1000000) 213 else if (1000000 < frequency )
401 frequency /= 1000; 214 frequency /= 1000;
402 215
403 if (frequency > 1000) 216 if (1000 < frequency)
404 chan = getChannelFromFrequency(frequency); 217 chan = getChannelFromFrequency(frequency);
405 else 218 else
406 chan = frequency; 219 chan = frequency;
@@ -423,7 +236,7 @@ linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
423 caplen = read(dev->fd_in, tmpbuf, count); 236 caplen = read(dev->fd_in, tmpbuf, count);
424 if (0 > caplen) 237 if (0 > caplen)
425 { 238 {
426 if (errno == EAGAIN) 239 if (EAGAIN == errno)
427 return (0); 240 return (0);
428 241
429 perror("read failed"); 242 perror("read failed");
@@ -432,11 +245,10 @@ linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
432 245
433 memset(buf, 0, sizeof(buf)); 246 memset(buf, 0, sizeof(buf));
434 247
435 /* XXX */
436 if (ri) 248 if (ri)
437 memset(ri, 0, sizeof(*ri)); 249 memset(ri, 0, sizeof(*ri));
438 250
439 if (dev->arptype_in == ARPHRD_IEEE80211_PRISM) 251 if (ARPHRD_IEEE80211_PRISM == dev->arptype_in )
440 { 252 {
441 /* skip the prism header */ 253 /* skip the prism header */
442 if (tmpbuf[7] == 0x40) 254 if (tmpbuf[7] == 0x40)
@@ -476,7 +288,7 @@ linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
476 return (0); 288 return (0);
477 } 289 }
478 290
479 if (dev->arptype_in == ARPHRD_IEEE80211_FULL) 291 if (ARPHRD_IEEE80211_FULL == dev->arptype_in)
480 { 292 {
481 struct ieee80211_radiotap_iterator iterator; 293 struct ieee80211_radiotap_iterator iterator;
482 struct ieee80211_radiotap_header *rthdr; 294 struct ieee80211_radiotap_header *rthdr;
@@ -588,7 +400,7 @@ linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
588 caplen -= n; 400 caplen -= n;
589 401
590 //detect fcs at the end, even if the flag wasn't set and remove it 402 //detect fcs at the end, even if the flag wasn't set and remove it
591 if (fcs_removed == 0 && check_crc_buf_osdep(tmpbuf + n, caplen - 4) == 1) 403 if (0 == fcs_removed && 1== check_crc_buf_osdep(tmpbuf + n, caplen - 4))
592 { 404 {
593 caplen -= 4; 405 caplen -= 4;
594 } 406 }
@@ -615,7 +427,7 @@ linux_write(struct Hardware_Infos * dev, unsigned char *buf, unsigned int count)
615 //usedrtap = 0; 427 //usedrtap = 0;
616 ret = write(dev->fd_out, buf, count); 428 ret = write(dev->fd_out, buf, count);
617 429
618 if (ret < 0) 430 if (0 > ret)
619 { 431 {
620 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno 432 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
621 == ENOMEM) 433 == ENOMEM)
@@ -632,7 +444,7 @@ linux_write(struct Hardware_Infos * dev, unsigned char *buf, unsigned int count)
632 /*if (usedrtap) 444 /*if (usedrtap)
633 ret -= letoh16(*p_rtlen); 445 ret -= letoh16(*p_rtlen);
634 446
635 if (ret < 0) 447 if (0 > ret)
636 { 448 {
637 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno 449 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
638 == ENOMEM) 450 == ENOMEM)
@@ -662,7 +474,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
662 memset(&ifr, 0, sizeof(ifr)); 474 memset(&ifr, 0, sizeof(ifr));
663 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1); 475 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
664 476
665 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) 477 if (0 > ioctl(fd, SIOCGIFINDEX, &ifr))
666 { 478 {
667 printf("Interface %s: \n", iface); 479 printf("Interface %s: \n", iface);
668 perror("ioctl(SIOCGIFINDEX) failed"); 480 perror("ioctl(SIOCGIFINDEX) failed");
@@ -677,7 +489,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
677 489
678 /* lookup the hardware type */ 490 /* lookup the hardware type */
679 491
680 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) 492 if (0 > ioctl(fd, SIOCGIFHWADDR, &ifr))
681 { 493 {
682 printf("Interface %s: \n", iface); 494 printf("Interface %s: \n", iface);
683 perror("ioctl(SIOCGIFHWADDR) failed"); 495 perror("ioctl(SIOCGIFHWADDR) failed");
@@ -688,7 +500,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
688 memset(&wrq, 0, sizeof(struct iwreq)); 500 memset(&wrq, 0, sizeof(struct iwreq));
689 strncpy(wrq.ifr_name, iface, IFNAMSIZ); 501 strncpy(wrq.ifr_name, iface, IFNAMSIZ);
690 502
691 if (ioctl(fd, SIOCGIWMODE, &wrq) < 0) 503 if (0 > ioctl(fd, SIOCGIWMODE, &wrq))
692 { 504 {
693 /* most probably not supported (ie for rtap ipw interface) * 505 /* most probably not supported (ie for rtap ipw interface) *
694 * so just assume its correctly set... */ 506 * so just assume its correctly set... */
@@ -717,7 +529,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
717 } 529 }
718 /* bind the raw socket to the interface */ 530 /* bind the raw socket to the interface */
719 531
720 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0) 532 if (0 > bind(fd, (struct sockaddr *) &sll, sizeof(sll)))
721 { 533 {
722 printf("Interface %s: \n", iface); 534 printf("Interface %s: \n", iface);
723 perror("bind(ETH_P_ALL) failed"); 535 perror("bind(ETH_P_ALL) failed");
@@ -726,7 +538,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
726 538
727 /* lookup the hardware type */ 539 /* lookup the hardware type */
728 540
729 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) 541 if (0 > ioctl(fd, SIOCGIFHWADDR, &ifr))
730 { 542 {
731 printf("Interface %s: \n", iface); 543 printf("Interface %s: \n", iface);
732 perror("ioctl(SIOCGIFHWADDR) failed"); 544 perror("ioctl(SIOCGIFHWADDR) failed");
@@ -741,7 +553,7 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
741 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family 553 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
742 != ARPHRD_IEEE80211_FULL) 554 != ARPHRD_IEEE80211_FULL)
743 { 555 {
744 if (ifr.ifr_hwaddr.sa_family == 1) 556 if (1 == ifr.ifr_hwaddr.sa_family)
745 fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) "); 557 fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) ");
746 else 558 else
747 fprintf(stderr, "\nUnsupported hardware link type %4d ", 559 fprintf(stderr, "\nUnsupported hardware link type %4d ",
@@ -773,46 +585,8 @@ openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
773int 585int
774wlaninit(struct Hardware_Infos * dev, char *iface) 586wlaninit(struct Hardware_Infos * dev, char *iface)
775{ 587{
776
777 //char *iwpriv;
778 char strbuf[512]; 588 char strbuf[512];
779 //dev->inject_wlanng = 1;
780 //dev->rate = 2; /* default to 1Mbps if nothing is set */
781 589
782 /* open raw socks */
783 /*
784 dev->fd_in = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
785 if (0 > dev->fd_in)
786 {
787 perror("socket(PF_PACKET) failed at fd_in");
788 if (getuid() != 0)
789 fprintf(stderr, "This program requires root privileges.\n");
790 return (1);
791 }
792 */
793 /*
794 dev->fd_main = socket(PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) );
795 if (0 > dev->fd_main)
796 {
797 perror("socket(PF_PACKET) failed at fd_main");
798 if (getuid() != 0)
799 fprintf(stderr, "This program requires root privileges.\n");
800 return (1);
801 }
802 */
803 /* Check iwpriv existence */
804 /*
805 iwpriv = wiToolsPath("iwpriv");
806 dev->iwpriv = iwpriv;
807 dev->iwconfig = wiToolsPath("iwconfig");
808 dev->ifconfig = wiToolsPath("ifconfig");
809
810 if (!iwpriv)
811 {
812 fprintf(stderr, "Can't find wireless tools, exiting.\n");
813 goto close_in;
814 }
815 */
816 dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 590 dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
817 if (0 > dev->fd_out) 591 if (0 > dev->fd_out)
818 { 592 {
@@ -836,7 +610,7 @@ wlaninit(struct Hardware_Infos * dev, char *iface)
836 snprintf(strbuf, sizeof(strbuf) - 1, 610 snprintf(strbuf, sizeof(strbuf) - 1,
837 "ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null", iface); 611 "ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null", iface);
838 612
839 if (system(strbuf) == 0) 613 if (0 == system(strbuf))
840 dev->drivertype = DT_MAC80211_RT; 614 dev->drivertype = DT_MAC80211_RT;
841 615
842 else 616 else
@@ -860,7 +634,6 @@ wlaninit(struct Hardware_Infos * dev, char *iface)
860 dev->iface = GNUNET_malloc(sizeof(char) *6); 634 dev->iface = GNUNET_malloc(sizeof(char) *6);
861 strncpy(dev->iface, iface, sizeof(char) * 6); 635 strncpy(dev->iface, iface, sizeof(char) * 6);
862 636
863 //dev->arptype_out = dev->arptype_in;
864 637
865 return 0; 638 return 0;
866 close_out: close(dev->fd_out); 639 close_out: close(dev->fd_out);
@@ -938,7 +711,7 @@ stdin_send_hw(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
938 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send) 711 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
939 - sizeof(struct GNUNET_MessageHeader); 712 - sizeof(struct GNUNET_MessageHeader);
940 713
941 if ((sendsize) > MAXLINE * 2) 714 if (MAXLINE * 2 < sendsize)
942 { 715 {
943 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n"); 716 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
944 exit(1); 717 exit(1);
@@ -950,8 +723,8 @@ stdin_send_hw(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
950 exit(1); 723 exit(1);
951 } 724 }
952 725
953 if (sendsize < sizeof(struct ieee80211_frame) 726 if ( sizeof(struct ieee80211_frame)
954 + sizeof(struct GNUNET_MessageHeader)) 727 + sizeof(struct GNUNET_MessageHeader) > sendsize)
955 { 728 {
956 fprintf(stderr, "Function stdin_send: packet too small\n"); 729 fprintf(stderr, "Function stdin_send: packet too small\n");
957 exit(1); 730 exit(1);
@@ -1034,7 +807,7 @@ maketest(unsigned char * buf, struct Hardware_Infos * dev)
1034 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid 807 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
1035 0x10, 0x86, //Sequence Control 808 0x10, 0x86, //Sequence Control
1036 }; 809 };
1037 if (first == 0) 810 if (0 == first)
1038 { 811 {
1039 memcpy(&u8aIeeeHeader, u8aIeeeHeader_def, sizeof(struct ieee80211_frame)); 812 memcpy(&u8aIeeeHeader, u8aIeeeHeader_def, sizeof(struct ieee80211_frame));
1040 memcpy(u8aIeeeHeader.i_addr2, dev->pl_mac, 6); 813 memcpy(u8aIeeeHeader.i_addr2, dev->pl_mac, 6);
@@ -1062,7 +835,6 @@ hardwaremode(int argc, char *argv[])
1062 835
1063 uid_t uid; 836 uid_t uid;
1064 struct Hardware_Infos dev; 837 struct Hardware_Infos dev;
1065 //struct ifreq ifreq;
1066 struct Radiotap_rx * rxinfo; 838 struct Radiotap_rx * rxinfo;
1067 uint8_t * mac = dev.pl_mac; 839 uint8_t * mac = dev.pl_mac;
1068 int fdpin, fdpout; 840 int fdpin, fdpout;
@@ -1084,12 +856,6 @@ hardwaremode(int argc, char *argv[])
1084 /* not critical, continue anyway */ 856 /* not critical, continue anyway */
1085 //} 857 //}
1086 858
1087 /*printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",
1088 ifreq.ifr_name, (int) mac[0], (int) mac[1], (int) mac[2], (int) mac[3],
1089 (int) mac[4], (int) mac[5]);*/
1090
1091 //return 0;
1092
1093 unsigned char * datastart; 859 unsigned char * datastart;
1094 char readbuf[MAXLINE]; 860 char readbuf[MAXLINE];
1095 int readsize = 0; 861 int readsize = 0;
@@ -1293,11 +1059,6 @@ hardwaremode(int argc, char *argv[])
1293 1059
1294 } 1060 }
1295 } 1061 }
1296 else
1297 {
1298 //eof
1299 //closeprog = 1;
1300 }
1301 } 1062 }
1302 1063
1303 } 1064 }
@@ -1334,269 +1095,6 @@ main(int argc, char *argv[])
1334 ret = hardwaremode(argc, argv); 1095 ret = hardwaremode(argc, argv);
1335 } 1096 }
1336 1097
1337#if 0
1338 u8 u8aSendBuffer[500];
1339 char szErrbuf[PCAP_ERRBUF_SIZE];
1340 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
1341 int nOrdinal = 0, r, nDelay = 100000;
1342 int nRateIndex = 0, retval, bytes;
1343 pcap_t *ppcap = NULL;
1344 struct bpf_program bpfprogram;
1345 char * szProgram = "", fBrokenSocket = 0;
1346 u16 u16HeaderLen;
1347 char szHostname[PATH_MAX];
1348
1349 if (gethostname(szHostname, sizeof (szHostname) - 1))
1350 {
1351 perror("unable to get hostname");
1352 }
1353 szHostname[sizeof (szHostname) - 1] = '\0';
1354
1355 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
1356
1357 while (1)
1358 {
1359 int nOptionIndex;
1360 static const struct option optiona[] =
1361 {
1362 { "delay", required_argument, NULL, 'd'},
1363 { "fcs", no_argument, &flagMarkWithFCS, 1},
1364 { "help", no_argument, &flagHelp, 1},
1365 { "verbose", no_argument, &flagVerbose, 1},
1366 { 0, 0, 0, 0}
1367 };
1368 int c = getopt_long(argc, argv, "d:hf",
1369 optiona, &nOptionIndex);
1370
1371 if (c == -1)
1372 break;
1373 switch (c)
1374 {
1375 case 0: // long option
1376 break;
1377
1378 case 'h': // help
1379 usage();
1380
1381 case 'd': // delay
1382 nDelay = atoi(optarg);
1383 break;
1384
1385 case 'f': // mark as FCS attached
1386 flagMarkWithFCS = 1;
1387 break;
1388
1389 case 'v': //Verbose / readable output to cout
1390 flagVerbose = 1;
1391 break;
1392
1393 default:
1394 printf("unknown switch %c\n", c);
1395 usage();
1396 break;
1397 }
1398 }
1399
1400 if (optind >= argc)
1401 usage();
1402
1403 // open the interface in pcap
1404
1405 szErrbuf[0] = '\0';
1406 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
1407 if (ppcap == NULL)
1408 {
1409 printf("Unable to open interface %s in pcap: %s\n",
1410 argv[optind], szErrbuf);
1411 return (1);
1412 }
1413
1414 //get mac from interface
1415
1416 /*int sock, j, k;
1417 char mac[32];
1418
1419 sock=socket(PF_INET, SOCK_STREAM, 0);
1420 if (-1==sock) {
1421 perror("can not open socket\n");
1422 return 1;
1423 }
1424
1425 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
1426 perror("ioctl(SIOCGIFHWADDR) ");
1427 return 1;
1428 }
1429 for (j=0, k=0; j<6; j++) {
1430 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
1431 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
1432 }
1433 mac[sizeof(mac)-1]='\0';
1434 */
1435
1436 //get header type
1437 nLinkEncap = pcap_datalink(ppcap);
1438 nCaptureHeaderLength = 0;
1439
1440 switch (nLinkEncap)
1441 {
1442
1443 case DLT_PRISM_HEADER:
1444 printf("DLT_PRISM_HEADER Encap\n");
1445 nCaptureHeaderLength = 0x40;
1446 n80211HeaderLength = 0x20; // ieee80211 comes after this
1447 szProgram = "radio[0x4a:4]==0x13223344";
1448 break;
1449
1450 case DLT_IEEE802_11_RADIO:
1451 printf("DLT_IEEE802_11_RADIO Encap\n");
1452 nCaptureHeaderLength = 0x40;
1453 n80211HeaderLength = 0x18; // ieee80211 comes after this
1454 szProgram = "ether[0x0a:4]==0x13223344";
1455 break;
1456
1457 default:
1458 printf("!!! unknown encapsulation on %s !\n", argv[1]);
1459 return (1);
1460
1461 }
1462
1463 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1)
1464 {
1465 puts(szProgram);
1466 puts(pcap_geterr(ppcap));
1467 return (1);
1468 }
1469 else
1470 {
1471 if (pcap_setfilter(ppcap, &bpfprogram) == -1)
1472 {
1473 puts(szProgram);
1474 puts(pcap_geterr(ppcap));
1475 }
1476 else
1477 {
1478 printf("RX Filter applied\n");
1479 }
1480 pcap_freecode(&bpfprogram);
1481 }
1482
1483 pcap_setnonblock(ppcap, 1, szErrbuf);
1484
1485 printf(" (delay between packets %dus)\n", nDelay);
1486
1487 memset(u8aSendBuffer, 0, sizeof(u8aSendBuffer));
1488
1489 while (!fBrokenSocket)
1490 {
1491 u8 * pu8 = u8aSendBuffer;
1492 struct pcap_pkthdr * ppcapPacketHeader = NULL;
1493 struct ieee80211_radiotap_iterator rti;
1494 PENUMBRA_RADIOTAP_DATA prd;
1495 //init of the values
1496 prd.m_nRate = 255;
1497 prd.m_nChannel = 255;
1498 prd.m_nAntenna = 255;
1499 prd.m_nRadiotapFlags = 255;
1500 u8 * pu8Payload = u8aSendBuffer;
1501 int n, nRate;
1502
1503 // receive
1504
1505 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
1506 (const u_char**) &pu8Payload);
1507
1508 if (retval < 0)
1509 {
1510 fBrokenSocket = 1;
1511 continue;
1512 }
1513
1514 if (retval != 1)
1515 goto do_tx;
1516
1517 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
1518
1519 printf("rtap: ");
1520 Dump(pu8Payload, u16HeaderLen);
1521
1522 if (ppcapPacketHeader->len < (u16HeaderLen + n80211HeaderLength))
1523 continue;
1524
1525 bytes = ppcapPacketHeader->len - (u16HeaderLen + n80211HeaderLength);
1526 if (bytes < 0)
1527 continue;
1528
1529 if (ieee80211_radiotap_iterator_init(&rti,
1530 (struct ieee80211_radiotap_header *) pu8Payload, bytes) < 0)
1531 continue;
1532
1533 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
1534 {
1535
1536 switch (rti.this_arg_index)
1537 {
1538 case IEEE80211_RADIOTAP_RATE:
1539 prd.m_nRate = (*rti.this_arg);
1540 break;
1541
1542 case IEEE80211_RADIOTAP_CHANNEL:
1543 prd.m_nChannel = le16_to_cpu(*((u16 *)rti.this_arg));
1544 prd.m_nChannelFlags = le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
1545 break;
1546
1547 case IEEE80211_RADIOTAP_ANTENNA:
1548 prd.m_nAntenna = (*rti.this_arg) + 1;
1549 break;
1550
1551 case IEEE80211_RADIOTAP_FLAGS:
1552 prd.m_nRadiotapFlags = *rti.this_arg;
1553 break;
1554
1555 }
1556 }
1557
1558 pu8Payload += u16HeaderLen + n80211HeaderLength;
1559
1560 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
1561 bytes -= 4;
1562
1563 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
1564 "Ant: %d, Flags: 0x%X\n", prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
1565 prd.m_nChannel / 1000, prd.m_nChannel - ((prd.m_nChannel / 1000)
1566 * 1000), prd.m_nAntenna, prd.m_nRadiotapFlags);
1567
1568 Dump(pu8Payload, bytes);
1569
1570 do_tx:
1571
1572 // transmit
1573
1574 memcpy(u8aSendBuffer, u8aRadiotapHeader, sizeof(u8aRadiotapHeader));
1575 if (flagMarkWithFCS)
1576 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
1577 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
1578 if (nRateIndex >= sizeof(u8aRatesToUse))
1579 nRateIndex = 0;
1580 pu8 += sizeof(u8aRadiotapHeader);
1581
1582 memcpy(pu8, u8aIeeeHeader, sizeof(u8aIeeeHeader));
1583 pu8 += sizeof(u8aIeeeHeader);
1584
1585 pu8 += sprintf((char *) u8aSendBuffer, "Packetspammer %02d"
1586 "broadcast packet"
1587 "#%05d -- :-D --%s ----", nRate / 2, nOrdinal++, szHostname);
1588 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
1589 if (r != (pu8 - u8aSendBuffer))
1590 {
1591 perror("Trouble injecting packet");
1592 return (1);
1593 }
1594 if (nDelay)
1595 usleep(nDelay);
1596 }
1597
1598#endif
1599
1600 return ret; 1098 return ret;
1601 maketest(NULL, NULL); 1099 maketest(NULL, NULL);
1602} 1100}