diff options
author | ng0 <ng0@n0.is> | 2019-09-08 12:33:09 +0000 |
---|---|---|
committer | ng0 <ng0@n0.is> | 2019-09-08 12:33:09 +0000 |
commit | d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch) | |
tree | 9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/transport/gnunet-helper-transport-wlan.c | |
parent | a0fce305c565c0937d917a92712f15e9c5736260 (diff) | |
download | gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip |
uncrustify as demanded.
Diffstat (limited to 'src/transport/gnunet-helper-transport-wlan.c')
-rw-r--r-- | src/transport/gnunet-helper-transport-wlan.c | 1607 |
1 files changed, 804 insertions, 803 deletions
diff --git a/src/transport/gnunet-helper-transport-wlan.c b/src/transport/gnunet-helper-transport-wlan.c index 6cd45ead8..885edb1b0 100644 --- a/src/transport/gnunet-helper-transport-wlan.c +++ b/src/transport/gnunet-helper-transport-wlan.c | |||
@@ -13,12 +13,12 @@ | |||
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Affero General Public License for more details. | 15 | Affero General Public License for more details. |
16 | 16 | ||
17 | You should have received a copy of the GNU Affero General Public License | 17 | You should have received a copy of the GNU Affero General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 | ||
20 | SPDX-License-Identifier: AGPL3.0-or-later | 20 | SPDX-License-Identifier: AGPL3.0-or-later |
21 | */ | 21 | */ |
22 | /** | 22 | /** |
23 | * @file src/transport/gnunet-helper-transport-wlan.c | 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) | 24 | * @brief mediator between the wlan interface and gnunet; must run as root (SUID will do) |
@@ -234,8 +234,7 @@ | |||
234 | /** | 234 | /** |
235 | * Values in the 'struct PrismHeader'. All in host byte order (!). | 235 | * Values in the 'struct PrismHeader'. All in host byte order (!). |
236 | */ | 236 | */ |
237 | struct PrismValue | 237 | struct PrismValue { |
238 | { | ||
239 | /** | 238 | /** |
240 | * This has a different ID for each parameter, see | 239 | * This has a different ID for each parameter, see |
241 | * PRISM_DID_* constants. | 240 | * PRISM_DID_* constants. |
@@ -258,15 +257,13 @@ struct PrismValue | |||
258 | * The data value | 257 | * The data value |
259 | */ | 258 | */ |
260 | uint32_t data; | 259 | uint32_t data; |
261 | |||
262 | } __attribute__ ((packed)); | 260 | } __attribute__ ((packed)); |
263 | 261 | ||
264 | 262 | ||
265 | /** | 263 | /** |
266 | * Prism header format ('struct p80211msg' in Linux). All in host byte order (!). | 264 | * Prism header format ('struct p80211msg' in Linux). All in host byte order (!). |
267 | */ | 265 | */ |
268 | struct PrismHeader | 266 | struct PrismHeader { |
269 | { | ||
270 | /** | 267 | /** |
271 | * We expect this to be a PRISM_MSGCODE_*. | 268 | * We expect this to be a PRISM_MSGCODE_*. |
272 | */ | 269 | */ |
@@ -286,7 +283,6 @@ struct PrismHeader | |||
286 | are typically the hosttime, mactime, channel, rssi, sq, signal, noise, | 283 | are typically the hosttime, mactime, channel, rssi, sq, signal, noise, |
287 | rate, istx and frmlen values, but documentation is sparse. So we | 284 | rate, istx and frmlen values, but documentation is sparse. So we |
288 | will use the 'did' fields to find out what we actually got. */ | 285 | will use the 'did' fields to find out what we actually got. */ |
289 | |||
290 | } __attribute__ ((packed)); | 286 | } __attribute__ ((packed)); |
291 | 287 | ||
292 | 288 | ||
@@ -305,9 +301,7 @@ struct PrismHeader | |||
305 | * reliable indicator of alignment requirement. See also | 301 | * reliable indicator of alignment requirement. See also |
306 | * 'man 9 ieee80211_radiotap'. | 302 | * 'man 9 ieee80211_radiotap'. |
307 | */ | 303 | */ |
308 | enum RadiotapType | 304 | enum RadiotapType { |
309 | { | ||
310 | |||
311 | /** | 305 | /** |
312 | * IEEE80211_RADIOTAP_TSFT __le64 microseconds | 306 | * IEEE80211_RADIOTAP_TSFT __le64 microseconds |
313 | * | 307 | * |
@@ -477,7 +471,7 @@ enum RadiotapType | |||
477 | * | 471 | * |
478 | * Frame was sent/received during CFP (Contention Free Period) | 472 | * Frame was sent/received during CFP (Contention Free Period) |
479 | */ | 473 | */ |
480 | #define IEEE80211_RADIOTAP_F_CFP 0x01 | 474 | #define IEEE80211_RADIOTAP_F_CFP 0x01 |
481 | 475 | ||
482 | /** | 476 | /** |
483 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get | 477 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get |
@@ -487,7 +481,7 @@ enum RadiotapType | |||
487 | * | 481 | * |
488 | * Frame was sent/received with short preamble | 482 | * Frame was sent/received with short preamble |
489 | */ | 483 | */ |
490 | #define IEEE80211_RADIOTAP_F_SHORTPRE 0x02 | 484 | #define IEEE80211_RADIOTAP_F_SHORTPRE 0x02 |
491 | 485 | ||
492 | /** | 486 | /** |
493 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get | 487 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get |
@@ -497,7 +491,7 @@ enum RadiotapType | |||
497 | * | 491 | * |
498 | * Frame was sent/received with WEP encryption | 492 | * Frame was sent/received with WEP encryption |
499 | */ | 493 | */ |
500 | #define IEEE80211_RADIOTAP_F_WEP 0x04 | 494 | #define IEEE80211_RADIOTAP_F_WEP 0x04 |
501 | 495 | ||
502 | /** | 496 | /** |
503 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get | 497 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get |
@@ -507,7 +501,7 @@ enum RadiotapType | |||
507 | * | 501 | * |
508 | * Frame was sent/received with fragmentation | 502 | * Frame was sent/received with fragmentation |
509 | */ | 503 | */ |
510 | #define IEEE80211_RADIOTAP_F_FRAG 0x08 | 504 | #define IEEE80211_RADIOTAP_F_FRAG 0x08 |
511 | 505 | ||
512 | /** | 506 | /** |
513 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get | 507 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get |
@@ -517,7 +511,7 @@ enum RadiotapType | |||
517 | * | 511 | * |
518 | * Frame includes FCS (CRC at the end that needs to be removeD). | 512 | * Frame includes FCS (CRC at the end that needs to be removeD). |
519 | */ | 513 | */ |
520 | #define IEEE80211_RADIOTAP_F_FCS 0x10 | 514 | #define IEEE80211_RADIOTAP_F_FCS 0x10 |
521 | 515 | ||
522 | /** | 516 | /** |
523 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get | 517 | * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get |
@@ -528,44 +522,44 @@ enum RadiotapType | |||
528 | * Frame has padding between 802.11 header and payload | 522 | * Frame has padding between 802.11 header and payload |
529 | * (to 32-bit boundary) | 523 | * (to 32-bit boundary) |
530 | */ | 524 | */ |
531 | #define IEEE80211_RADIOTAP_F_DATAPAD 0x20 | 525 | #define IEEE80211_RADIOTAP_F_DATAPAD 0x20 |
532 | 526 | ||
533 | 527 | ||
534 | /** | 528 | /** |
535 | * For IEEE80211_RADIOTAP_RX_FLAGS: | 529 | * For IEEE80211_RADIOTAP_RX_FLAGS: |
536 | * frame failed crc check | 530 | * frame failed crc check |
537 | */ | 531 | */ |
538 | #define IEEE80211_RADIOTAP_F_RX_BADFCS 0x0001 | 532 | #define IEEE80211_RADIOTAP_F_RX_BADFCS 0x0001 |
539 | 533 | ||
540 | /** | 534 | /** |
541 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): | 535 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): |
542 | * failed due to excessive retries | 536 | * failed due to excessive retries |
543 | */ | 537 | */ |
544 | #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001 | 538 | #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001 |
545 | 539 | ||
546 | /** | 540 | /** |
547 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): | 541 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): |
548 | * used cts 'protection' | 542 | * used cts 'protection' |
549 | */ | 543 | */ |
550 | #define IEEE80211_RADIOTAP_F_TX_CTS 0x0002 | 544 | #define IEEE80211_RADIOTAP_F_TX_CTS 0x0002 |
551 | 545 | ||
552 | /** | 546 | /** |
553 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): | 547 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): |
554 | * used rts/cts handshake | 548 | * used rts/cts handshake |
555 | */ | 549 | */ |
556 | #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004 | 550 | #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004 |
557 | 551 | ||
558 | /** | 552 | /** |
559 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): | 553 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): |
560 | * frame should not be ACKed | 554 | * frame should not be ACKed |
561 | */ | 555 | */ |
562 | #define IEEE80211_RADIOTAP_F_TX_NOACK 0x0008 | 556 | #define IEEE80211_RADIOTAP_F_TX_NOACK 0x0008 |
563 | 557 | ||
564 | /** | 558 | /** |
565 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): | 559 | * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'): |
566 | * sequence number handled by userspace | 560 | * sequence number handled by userspace |
567 | */ | 561 | */ |
568 | #define IEEE80211_RADIOTAP_F_TX_NOSEQ 0x0010 | 562 | #define IEEE80211_RADIOTAP_F_TX_NOSEQ 0x0010 |
569 | 563 | ||
570 | 564 | ||
571 | /** | 565 | /** |
@@ -582,8 +576,7 @@ enum RadiotapType | |||
582 | * The radio capture header precedes the 802.11 header. | 576 | * The radio capture header precedes the 802.11 header. |
583 | * All data in the header is little endian on all platforms. | 577 | * All data in the header is little endian on all platforms. |
584 | */ | 578 | */ |
585 | struct Ieee80211RadiotapHeader | 579 | struct Ieee80211RadiotapHeader { |
586 | { | ||
587 | /** | 580 | /** |
588 | * Version 0. Only increases for drastic changes, introduction of | 581 | * Version 0. Only increases for drastic changes, introduction of |
589 | * compatible new fields does not count. | 582 | * compatible new fields does not count. |
@@ -614,9 +607,7 @@ struct Ieee80211RadiotapHeader | |||
614 | * Format of the header we need to prepend to messages to be sent to the | 607 | * Format of the header we need to prepend to messages to be sent to the |
615 | * Kernel. | 608 | * Kernel. |
616 | */ | 609 | */ |
617 | struct RadiotapTransmissionHeader | 610 | struct RadiotapTransmissionHeader { |
618 | { | ||
619 | |||
620 | /** | 611 | /** |
621 | * First we begin with the 'generic' header we also get when receiving | 612 | * First we begin with the 'generic' header we also get when receiving |
622 | * messages. | 613 | * messages. |
@@ -640,7 +631,6 @@ struct RadiotapTransmissionHeader | |||
640 | * Transmission flags from on the IEEE80211_RADIOTAP_F_TX_* constant family. | 631 | * Transmission flags from on the IEEE80211_RADIOTAP_F_TX_* constant family. |
641 | */ | 632 | */ |
642 | uint16_t txflags; | 633 | uint16_t txflags; |
643 | |||
644 | }; | 634 | }; |
645 | 635 | ||
646 | /** | 636 | /** |
@@ -656,8 +646,7 @@ struct RadiotapTransmissionHeader | |||
656 | * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments | 646 | * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments |
657 | * in the radiotap header. Used when we parse radiotap packets received from the kernel. | 647 | * in the radiotap header. Used when we parse radiotap packets received from the kernel. |
658 | */ | 648 | */ |
659 | struct Ieee80211RadiotapHeaderIterator | 649 | struct Ieee80211RadiotapHeaderIterator { |
660 | { | ||
661 | /** | 650 | /** |
662 | * pointer to the radiotap header we are walking through | 651 | * pointer to the radiotap header we are walking through |
663 | */ | 652 | */ |
@@ -698,7 +687,6 @@ struct Ieee80211RadiotapHeaderIterator | |||
698 | * internal next argument index | 687 | * internal next argument index |
699 | */ | 688 | */ |
700 | unsigned int arg_index; | 689 | unsigned int arg_index; |
701 | |||
702 | }; | 690 | }; |
703 | 691 | ||
704 | 692 | ||
@@ -710,9 +698,7 @@ struct Ieee80211RadiotapHeaderIterator | |||
710 | * struct for storing the information of the hardware. There is only | 698 | * struct for storing the information of the hardware. There is only |
711 | * one of these. | 699 | * one of these. |
712 | */ | 700 | */ |
713 | struct HardwareInfos | 701 | struct HardwareInfos { |
714 | { | ||
715 | |||
716 | /** | 702 | /** |
717 | * file descriptor for the raw socket | 703 | * file descriptor for the raw socket |
718 | */ | 704 | */ |
@@ -739,8 +725,7 @@ struct HardwareInfos | |||
739 | /** | 725 | /** |
740 | * IO buffer used for buffering data in transit (to wireless or to stdout). | 726 | * IO buffer used for buffering data in transit (to wireless or to stdout). |
741 | */ | 727 | */ |
742 | struct SendBuffer | 728 | struct SendBuffer { |
743 | { | ||
744 | /** | 729 | /** |
745 | * How many bytes of data are stored in 'buf' for transmission right now? | 730 | * How many bytes of data are stored in 'buf' for transmission right now? |
746 | * Data always starts at offset 0 and extends to 'size'. | 731 | * Data always starts at offset 0 and extends to 'size'. |
@@ -784,7 +769,7 @@ static struct SendBuffer write_std; | |||
784 | /** | 769 | /** |
785 | * Smallest supported message. | 770 | * Smallest supported message. |
786 | */ | 771 | */ |
787 | #define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader) | 772 | #define MIN_BUFFER_SIZE sizeof(struct GNUNET_MessageHeader) |
788 | 773 | ||
789 | 774 | ||
790 | /** | 775 | /** |
@@ -795,16 +780,14 @@ static struct SendBuffer write_std; | |||
795 | * @param message the actual message | 780 | * @param message the actual message |
796 | */ | 781 | */ |
797 | typedef void (*MessageTokenizerCallback) (void *cls, | 782 | typedef void (*MessageTokenizerCallback) (void *cls, |
798 | const struct | 783 | const struct |
799 | GNUNET_MessageHeader * | 784 | GNUNET_MessageHeader * |
800 | message); | 785 | message); |
801 | 786 | ||
802 | /** | 787 | /** |
803 | * Handle to a message stream tokenizer. | 788 | * Handle to a message stream tokenizer. |
804 | */ | 789 | */ |
805 | struct MessageStreamTokenizer | 790 | struct MessageStreamTokenizer { |
806 | { | ||
807 | |||
808 | /** | 791 | /** |
809 | * Function to call on completed messages. | 792 | * Function to call on completed messages. |
810 | */ | 793 | */ |
@@ -834,7 +817,6 @@ struct MessageStreamTokenizer | |||
834 | * Beginning of the buffer. Typed like this to force alignment. | 817 | * Beginning of the buffer. Typed like this to force alignment. |
835 | */ | 818 | */ |
836 | struct GNUNET_MessageHeader *hdr; | 819 | struct GNUNET_MessageHeader *hdr; |
837 | |||
838 | }; | 820 | }; |
839 | 821 | ||
840 | 822 | ||
@@ -846,23 +828,23 @@ struct MessageStreamTokenizer | |||
846 | * @return handle to tokenizer | 828 | * @return handle to tokenizer |
847 | */ | 829 | */ |
848 | static struct MessageStreamTokenizer * | 830 | static struct MessageStreamTokenizer * |
849 | mst_create (MessageTokenizerCallback cb, | 831 | mst_create(MessageTokenizerCallback cb, |
850 | void *cb_cls) | 832 | void *cb_cls) |
851 | { | 833 | { |
852 | struct MessageStreamTokenizer *ret; | 834 | struct MessageStreamTokenizer *ret; |
853 | 835 | ||
854 | ret = malloc (sizeof (struct MessageStreamTokenizer)); | 836 | ret = malloc(sizeof(struct MessageStreamTokenizer)); |
855 | if (NULL == ret) | 837 | if (NULL == ret) |
856 | { | 838 | { |
857 | fprintf (stderr, "Failed to allocate buffer for tokenizer\n"); | 839 | fprintf(stderr, "Failed to allocate buffer for tokenizer\n"); |
858 | exit (1); | 840 | exit(1); |
859 | } | 841 | } |
860 | ret->hdr = malloc (MIN_BUFFER_SIZE); | 842 | ret->hdr = malloc(MIN_BUFFER_SIZE); |
861 | if (NULL == ret->hdr) | 843 | if (NULL == ret->hdr) |
862 | { | 844 | { |
863 | fprintf (stderr, "Failed to allocate buffer for alignment\n"); | 845 | fprintf(stderr, "Failed to allocate buffer for alignment\n"); |
864 | exit (1); | 846 | exit(1); |
865 | } | 847 | } |
866 | ret->curr_buf = MIN_BUFFER_SIZE; | 848 | ret->curr_buf = MIN_BUFFER_SIZE; |
867 | ret->cb = cb; | 849 | ret->cb = cb; |
868 | ret->cb_cls = cb_cls; | 850 | ret->cb_cls = cb_cls; |
@@ -881,8 +863,8 @@ mst_create (MessageTokenizerCallback cb, | |||
881 | * GNUNET_SYSERR if the data stream is corrupt | 863 | * GNUNET_SYSERR if the data stream is corrupt |
882 | */ | 864 | */ |
883 | static int | 865 | static int |
884 | mst_receive (struct MessageStreamTokenizer *mst, | 866 | mst_receive(struct MessageStreamTokenizer *mst, |
885 | const char *buf, size_t size) | 867 | const char *buf, size_t size) |
886 | { | 868 | { |
887 | const struct GNUNET_MessageHeader *hdr; | 869 | const struct GNUNET_MessageHeader *hdr; |
888 | size_t delta; | 870 | size_t delta; |
@@ -893,132 +875,132 @@ mst_receive (struct MessageStreamTokenizer *mst, | |||
893 | int ret; | 875 | int ret; |
894 | 876 | ||
895 | ret = GNUNET_OK; | 877 | ret = GNUNET_OK; |
896 | ibuf = (char *) mst->hdr; | 878 | ibuf = (char *)mst->hdr; |
897 | while (mst->pos > 0) | 879 | 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 | { | 880 | { |
951 | delta = GNUNET_MIN (want - (mst->pos - mst->off), size); | 881 | do_align: |
952 | GNUNET_memcpy (&ibuf[mst->pos], buf, delta); | 882 | if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) || |
953 | mst->pos += delta; | 883 | (0 != (mst->off % ALIGN_FACTOR))) |
954 | buf += delta; | 884 | { |
955 | size -= delta; | 885 | /* need to align or need more space */ |
956 | } | 886 | mst->pos -= mst->off; |
957 | if (mst->pos - mst->off < want) | 887 | memmove(ibuf, &ibuf[mst->off], mst->pos); |
958 | { | 888 | mst->off = 0; |
959 | return GNUNET_OK; | 889 | } |
960 | } | 890 | if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) |
961 | mst->cb (mst->cb_cls, hdr); | 891 | { |
962 | mst->off += want; | 892 | delta = |
963 | if (mst->off == mst->pos) | 893 | GNUNET_MIN(sizeof(struct GNUNET_MessageHeader) - |
964 | { | 894 | (mst->pos - mst->off), size); |
965 | /* reset to beginning of buffer, it's free right now! */ | 895 | GNUNET_memcpy(&ibuf[mst->pos], buf, delta); |
966 | mst->off = 0; | 896 | mst->pos += delta; |
967 | mst->pos = 0; | 897 | buf += delta; |
898 | size -= delta; | ||
899 | } | ||
900 | if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) | ||
901 | { | ||
902 | return GNUNET_OK; | ||
903 | } | ||
904 | hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off]; | ||
905 | want = ntohs(hdr->size); | ||
906 | if (want < sizeof(struct GNUNET_MessageHeader)) | ||
907 | { | ||
908 | fprintf(stderr, | ||
909 | "Received invalid message from stdin\n"); | ||
910 | exit(1); | ||
911 | } | ||
912 | if (mst->curr_buf - mst->off < want) | ||
913 | { | ||
914 | /* need more space */ | ||
915 | mst->pos -= mst->off; | ||
916 | memmove(ibuf, &ibuf[mst->off], mst->pos); | ||
917 | mst->off = 0; | ||
918 | } | ||
919 | if (want > mst->curr_buf) | ||
920 | { | ||
921 | mst->hdr = realloc(mst->hdr, want); | ||
922 | if (NULL == mst->hdr) | ||
923 | { | ||
924 | fprintf(stderr, "Failed to allocate buffer for alignment\n"); | ||
925 | exit(1); | ||
926 | } | ||
927 | ibuf = (char *)mst->hdr; | ||
928 | mst->curr_buf = want; | ||
929 | } | ||
930 | hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off]; | ||
931 | if (mst->pos - mst->off < want) | ||
932 | { | ||
933 | delta = GNUNET_MIN(want - (mst->pos - mst->off), size); | ||
934 | GNUNET_memcpy(&ibuf[mst->pos], buf, delta); | ||
935 | mst->pos += delta; | ||
936 | buf += delta; | ||
937 | size -= delta; | ||
938 | } | ||
939 | if (mst->pos - mst->off < want) | ||
940 | { | ||
941 | return GNUNET_OK; | ||
942 | } | ||
943 | mst->cb(mst->cb_cls, hdr); | ||
944 | mst->off += want; | ||
945 | if (mst->off == mst->pos) | ||
946 | { | ||
947 | /* reset to beginning of buffer, it's free right now! */ | ||
948 | mst->off = 0; | ||
949 | mst->pos = 0; | ||
950 | } | ||
968 | } | 951 | } |
969 | } | ||
970 | while (size > 0) | 952 | 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 | { | 953 | { |
978 | /* can try to do zero-copy and process directly from original buffer */ | 954 | if (size < sizeof(struct GNUNET_MessageHeader)) |
979 | hdr = (const struct GNUNET_MessageHeader *) buf; | 955 | break; |
980 | want = ntohs (hdr->size); | 956 | offset = (unsigned long)buf; |
981 | if (want < sizeof (struct GNUNET_MessageHeader)) | 957 | need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO; |
982 | { | 958 | if (GNUNET_NO == need_align) |
983 | fprintf (stderr, | 959 | { |
984 | "Received invalid message from stdin\n"); | 960 | /* can try to do zero-copy and process directly from original buffer */ |
985 | exit (1); | 961 | hdr = (const struct GNUNET_MessageHeader *)buf; |
986 | } | 962 | want = ntohs(hdr->size); |
987 | if (size < want) | 963 | if (want < sizeof(struct GNUNET_MessageHeader)) |
988 | break; /* or not, buffer incomplete, so copy to private buffer... */ | 964 | { |
989 | mst->cb (mst->cb_cls, hdr); | 965 | fprintf(stderr, |
990 | buf += want; | 966 | "Received invalid message from stdin\n"); |
991 | size -= want; | 967 | exit(1); |
992 | } | 968 | } |
993 | else | 969 | if (size < want) |
994 | { | 970 | break; /* or not, buffer incomplete, so copy to private buffer... */ |
995 | /* need to copy to private buffer to align; | 971 | mst->cb(mst->cb_cls, hdr); |
996 | * yes, we go a bit more spagetti than usual here */ | 972 | buf += want; |
997 | goto do_align; | 973 | size -= want; |
974 | } | ||
975 | else | ||
976 | { | ||
977 | /* need to copy to private buffer to align; | ||
978 | * yes, we go a bit more spagetti than usual here */ | ||
979 | goto do_align; | ||
980 | } | ||
998 | } | 981 | } |
999 | } | ||
1000 | if (size > 0) | 982 | if (size > 0) |
1001 | { | ||
1002 | if (size + mst->pos > mst->curr_buf) | ||
1003 | { | 983 | { |
1004 | mst->hdr = realloc (mst->hdr, size + mst->pos); | 984 | if (size + mst->pos > mst->curr_buf) |
1005 | if (NULL == mst->hdr) | 985 | { |
1006 | { | 986 | mst->hdr = realloc(mst->hdr, size + mst->pos); |
1007 | fprintf (stderr, "Failed to allocate buffer for alignment\n"); | 987 | if (NULL == mst->hdr) |
1008 | exit (1); | 988 | { |
1009 | } | 989 | fprintf(stderr, "Failed to allocate buffer for alignment\n"); |
1010 | ibuf = (char *) mst->hdr; | 990 | exit(1); |
1011 | mst->curr_buf = size + mst->pos; | 991 | } |
992 | ibuf = (char *)mst->hdr; | ||
993 | mst->curr_buf = size + mst->pos; | ||
994 | } | ||
995 | if (mst->pos + size > mst->curr_buf) | ||
996 | { | ||
997 | fprintf(stderr, | ||
998 | "Assertion failed\n"); | ||
999 | exit(1); | ||
1000 | } | ||
1001 | GNUNET_memcpy(&ibuf[mst->pos], buf, size); | ||
1002 | mst->pos += size; | ||
1012 | } | 1003 | } |
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; | 1004 | return ret; |
1023 | } | 1005 | } |
1024 | 1006 | ||
@@ -1029,10 +1011,10 @@ do_align: | |||
1029 | * @param mst tokenizer to destroy | 1011 | * @param mst tokenizer to destroy |
1030 | */ | 1012 | */ |
1031 | static void | 1013 | static void |
1032 | mst_destroy (struct MessageStreamTokenizer *mst) | 1014 | mst_destroy(struct MessageStreamTokenizer *mst) |
1033 | { | 1015 | { |
1034 | free (mst->hdr); | 1016 | free(mst->hdr); |
1035 | free (mst); | 1017 | free(mst); |
1036 | } | 1018 | } |
1037 | 1019 | ||
1038 | /* ***************** end of server_mst.c clone ***************** **/ | 1020 | /* ***************** end of server_mst.c clone ***************** **/ |
@@ -1058,12 +1040,12 @@ mst_destroy (struct MessageStreamTokenizer *mst) | |||
1058 | * @return 0 on success, -1 on error | 1040 | * @return 0 on success, -1 on error |
1059 | */ | 1041 | */ |
1060 | static int | 1042 | static int |
1061 | ieee80211_radiotap_iterator_init (struct Ieee80211RadiotapHeaderIterator *iterator, | 1043 | ieee80211_radiotap_iterator_init(struct Ieee80211RadiotapHeaderIterator *iterator, |
1062 | const struct Ieee80211RadiotapHeader *radiotap_header, | 1044 | const struct Ieee80211RadiotapHeader *radiotap_header, |
1063 | size_t max_length) | 1045 | size_t max_length) |
1064 | { | 1046 | { |
1065 | if ( (iterator == NULL) || | 1047 | if ((iterator == NULL) || |
1066 | (radiotap_header == NULL) ) | 1048 | (radiotap_header == NULL)) |
1067 | return -1; | 1049 | return -1; |
1068 | 1050 | ||
1069 | /* Linux only supports version 0 radiotap format */ | 1051 | /* Linux only supports version 0 radiotap format */ |
@@ -1071,37 +1053,37 @@ ieee80211_radiotap_iterator_init (struct Ieee80211RadiotapHeaderIterator *iterat | |||
1071 | return -1; | 1053 | return -1; |
1072 | 1054 | ||
1073 | /* sanity check for allowed length and radiotap length field */ | 1055 | /* sanity check for allowed length and radiotap length field */ |
1074 | if ( (max_length < sizeof (struct Ieee80211RadiotapHeader)) || | 1056 | if ((max_length < sizeof(struct Ieee80211RadiotapHeader)) || |
1075 | (max_length < (GNUNET_le16toh (radiotap_header->it_len))) ) | 1057 | (max_length < (GNUNET_le16toh(radiotap_header->it_len)))) |
1076 | return -1; | 1058 | return -1; |
1077 | 1059 | ||
1078 | memset (iterator, 0, sizeof (struct Ieee80211RadiotapHeaderIterator)); | 1060 | memset(iterator, 0, sizeof(struct Ieee80211RadiotapHeaderIterator)); |
1079 | iterator->rtheader = radiotap_header; | 1061 | iterator->rtheader = radiotap_header; |
1080 | iterator->max_length = GNUNET_le16toh (radiotap_header->it_len); | 1062 | iterator->max_length = GNUNET_le16toh(radiotap_header->it_len); |
1081 | iterator->bitmap_shifter = GNUNET_le32toh (radiotap_header->it_present); | 1063 | iterator->bitmap_shifter = GNUNET_le32toh(radiotap_header->it_present); |
1082 | iterator->arg = ((uint8_t *) radiotap_header) + sizeof (struct Ieee80211RadiotapHeader); | 1064 | iterator->arg = ((uint8_t *)radiotap_header) + sizeof(struct Ieee80211RadiotapHeader); |
1083 | 1065 | ||
1084 | /* find payload start allowing for extended bitmap(s) */ | 1066 | /* find payload start allowing for extended bitmap(s) */ |
1085 | if (0 != (iterator->bitmap_shifter & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK)) | 1067 | 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 | { | 1068 | { |
1089 | iterator->arg += sizeof (uint32_t); | 1069 | while (GNUNET_le32toh(*((uint32_t *)iterator->arg)) & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK) |
1070 | { | ||
1071 | iterator->arg += sizeof(uint32_t); | ||
1072 | /* | ||
1073 | * check for insanity where the present bitmaps | ||
1074 | * keep claiming to extend up to or even beyond the | ||
1075 | * stated radiotap header length | ||
1076 | */ | ||
1077 | if (iterator->arg - ((uint8_t*)iterator->rtheader) > iterator->max_length) | ||
1078 | return -1; | ||
1079 | } | ||
1080 | iterator->arg += sizeof(uint32_t); | ||
1090 | /* | 1081 | /* |
1091 | * check for insanity where the present bitmaps | 1082 | * no need to check again for blowing past stated radiotap |
1092 | * keep claiming to extend up to or even beyond the | 1083 | * header length, becuase ieee80211_radiotap_iterator_next |
1093 | * stated radiotap header length | 1084 | * checks it before it is dereferenced |
1094 | */ | 1085 | */ |
1095 | if (iterator->arg - ((uint8_t*) iterator->rtheader) > iterator->max_length) | ||
1096 | return -1; | ||
1097 | } | 1086 | } |
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 */ | 1087 | /* we are all initialized happily */ |
1106 | return 0; | 1088 | return 0; |
1107 | } | 1089 | } |
@@ -1120,7 +1102,7 @@ ieee80211_radiotap_iterator_init (struct Ieee80211RadiotapHeaderIterator *iterat | |||
1120 | * @return next present arg index on success or -1 if no more or error | 1102 | * @return next present arg index on success or -1 if no more or error |
1121 | */ | 1103 | */ |
1122 | static int | 1104 | static int |
1123 | ieee80211_radiotap_iterator_next (struct Ieee80211RadiotapHeaderIterator *iterator) | 1105 | ieee80211_radiotap_iterator_next(struct Ieee80211RadiotapHeaderIterator *iterator) |
1124 | { | 1106 | { |
1125 | /* | 1107 | /* |
1126 | * small length lookup table for all radiotap types we heard of | 1108 | * small length lookup table for all radiotap types we heard of |
@@ -1156,105 +1138,105 @@ ieee80211_radiotap_iterator_next (struct Ieee80211RadiotapHeaderIterator *iterat | |||
1156 | [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22, | 1138 | [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22, |
1157 | [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11, | 1139 | [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11, |
1158 | [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11 | 1140 | [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11 |
1159 | /* | 1141 | /* |
1160 | * add more here as they are defined in | 1142 | * add more here as they are defined in |
1161 | * include/net/ieee80211_radiotap.h | 1143 | * include/net/ieee80211_radiotap.h |
1162 | */ | 1144 | */ |
1163 | }; | 1145 | }; |
1164 | 1146 | ||
1165 | /* | 1147 | /* |
1166 | * for every radiotap entry we can at | 1148 | * for every radiotap entry we can at |
1167 | * least skip (by knowing the length)... | 1149 | * least skip (by knowing the length)... |
1168 | */ | 1150 | */ |
1169 | while (iterator->arg_index < sizeof (rt_sizes)) | 1151 | while (iterator->arg_index < sizeof(rt_sizes)) |
1170 | { | ||
1171 | int hit = (0 != (iterator->bitmap_shifter & 1)); | ||
1172 | |||
1173 | if (hit) | ||
1174 | { | 1152 | { |
1175 | unsigned int wanted_alignment; | 1153 | int hit = (0 != (iterator->bitmap_shifter & 1)); |
1176 | unsigned int unalignment; | 1154 | |
1177 | /* | 1155 | if (hit) |
1178 | * arg is present, account for alignment padding | 1156 | { |
1179 | * 8-bit args can be at any alignment | 1157 | unsigned int wanted_alignment; |
1180 | * 16-bit args must start on 16-bit boundary | 1158 | unsigned int unalignment; |
1181 | * 32-bit args must start on 32-bit boundary | 1159 | /* |
1182 | * 64-bit args must start on 64-bit boundary | 1160 | * arg is present, account for alignment padding |
1183 | * | 1161 | * 8-bit args can be at any alignment |
1184 | * note that total arg size can differ from alignment of | 1162 | * 16-bit args must start on 16-bit boundary |
1185 | * elements inside arg, so we use upper nybble of length table | 1163 | * 32-bit args must start on 32-bit boundary |
1186 | * to base alignment on. First, 'wanted_alignment' is set to be | 1164 | * 64-bit args must start on 64-bit boundary |
1187 | * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit | 1165 | * |
1188 | * arguments. Then, we calculate the 'unalignment' (how many | 1166 | * note that total arg size can differ from alignment of |
1189 | * bytes we are over by taking the difference of 'arg' and the | 1167 | * elements inside arg, so we use upper nybble of length table |
1190 | * overall starting point modulo the desired alignment. As | 1168 | * to base alignment on. First, 'wanted_alignment' is set to be |
1191 | * desired alignments are powers of two, we can do modulo with | 1169 | * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit |
1192 | * binary "&" (and also avoid the possibility of a division by | 1170 | * arguments. Then, we calculate the 'unalignment' (how many |
1193 | * zero if the 'rt_sizes' table contains bogus entries). | 1171 | * bytes we are over by taking the difference of 'arg' and the |
1194 | * | 1172 | * overall starting point modulo the desired alignment. As |
1195 | * also note: these alignments are relative to the start of the | 1173 | * desired alignments are powers of two, we can do modulo with |
1196 | * radiotap header. There is no guarantee that the radiotap | 1174 | * binary "&" (and also avoid the possibility of a division by |
1197 | * header itself is aligned on any kind of boundary, thus we | 1175 | * zero if the 'rt_sizes' table contains bogus entries). |
1198 | * need to really look at the delta here. | 1176 | * |
1199 | */ | 1177 | * also note: these alignments are relative to the start of the |
1200 | wanted_alignment = rt_sizes[iterator->arg_index] >> 4; | 1178 | * radiotap header. There is no guarantee that the radiotap |
1201 | unalignment = (((void *) iterator->arg) - ((void *) iterator->rtheader)) & (wanted_alignment - 1); | 1179 | * header itself is aligned on any kind of boundary, thus we |
1202 | if (0 != unalignment) | 1180 | * need to really look at the delta here. |
1203 | { | 1181 | */ |
1204 | /* need padding (by 'wanted_alignment - unalignment') */ | 1182 | wanted_alignment = rt_sizes[iterator->arg_index] >> 4; |
1205 | iterator->arg_index += wanted_alignment - unalignment; | 1183 | unalignment = (((void *)iterator->arg) - ((void *)iterator->rtheader)) & (wanted_alignment - 1); |
1206 | } | 1184 | if (0 != unalignment) |
1207 | 1185 | { | |
1208 | /* | 1186 | /* need padding (by 'wanted_alignment - unalignment') */ |
1209 | * this is what we will return to user, but we need to | 1187 | iterator->arg_index += wanted_alignment - unalignment; |
1210 | * move on first so next call has something fresh to test | 1188 | } |
1211 | */ | 1189 | |
1212 | iterator->this_arg_index = iterator->arg_index; | 1190 | /* |
1213 | iterator->this_arg = iterator->arg; | 1191 | * this is what we will return to user, but we need to |
1214 | 1192 | * move on first so next call has something fresh to test | |
1215 | /* internally move on the size of this arg (using lower nybble from | 1193 | */ |
1216 | the table) */ | 1194 | iterator->this_arg_index = iterator->arg_index; |
1217 | iterator->arg += rt_sizes[iterator->arg_index] & 0x0f; | 1195 | iterator->this_arg = iterator->arg; |
1218 | 1196 | ||
1219 | /* | 1197 | /* internally move on the size of this arg (using lower nybble from |
1220 | * check for insanity where we are given a bitmap that | 1198 | the table) */ |
1221 | * claims to have more arg content than the length of the | 1199 | iterator->arg += rt_sizes[iterator->arg_index] & 0x0f; |
1222 | * radiotap section. We will normally end up equalling this | 1200 | |
1223 | * max_length on the last arg, never exceeding it. | 1201 | /* |
1224 | */ | 1202 | * check for insanity where we are given a bitmap that |
1225 | if ((((void *) iterator->arg) - ((void *) iterator->rtheader)) > iterator->max_length) | 1203 | * claims to have more arg content than the length of the |
1226 | return -1; | 1204 | * radiotap section. We will normally end up equalling this |
1227 | } | 1205 | * max_length on the last arg, never exceeding it. |
1228 | 1206 | */ | |
1229 | /* Now, move on to next bit / next entry */ | 1207 | if ((((void *)iterator->arg) - ((void *)iterator->rtheader)) > iterator->max_length) |
1230 | iterator->arg_index++; | 1208 | return -1; |
1231 | 1209 | } | |
1232 | if (0 == (iterator->arg_index % 32)) | 1210 | |
1233 | { | 1211 | /* Now, move on to next bit / next entry */ |
1234 | /* completed current uint32_t bitmap */ | 1212 | iterator->arg_index++; |
1235 | if (0 != (iterator->bitmap_shifter & 1)) | 1213 | |
1236 | { | 1214 | if (0 == (iterator->arg_index % 32)) |
1237 | /* bit 31 was set, there is more; move to next uint32_t bitmap */ | 1215 | { |
1238 | iterator->bitmap_shifter = GNUNET_le32toh (*iterator->next_bitmap); | 1216 | /* completed current uint32_t bitmap */ |
1239 | iterator->next_bitmap++; | 1217 | if (0 != (iterator->bitmap_shifter & 1)) |
1240 | } | 1218 | { |
1219 | /* bit 31 was set, there is more; move to next uint32_t bitmap */ | ||
1220 | iterator->bitmap_shifter = GNUNET_le32toh(*iterator->next_bitmap); | ||
1221 | iterator->next_bitmap++; | ||
1222 | } | ||
1223 | else | ||
1224 | { | ||
1225 | /* no more bitmaps: end (by setting arg_index to high, unsupported value) */ | ||
1226 | iterator->arg_index = sizeof(rt_sizes); | ||
1227 | } | ||
1228 | } | ||
1241 | else | 1229 | else |
1242 | { | 1230 | { |
1243 | /* no more bitmaps: end (by setting arg_index to high, unsupported value) */ | 1231 | /* just try the next bit (while loop will move on) */ |
1244 | iterator->arg_index = sizeof (rt_sizes); | 1232 | iterator->bitmap_shifter >>= 1; |
1245 | } | 1233 | } |
1246 | } | 1234 | |
1247 | else | 1235 | /* if we found a valid arg earlier, return it now */ |
1248 | { | 1236 | if (hit) |
1249 | /* just try the next bit (while loop will move on) */ | 1237 | return iterator->this_arg_index; |
1250 | iterator->bitmap_shifter >>= 1; | ||
1251 | } | 1238 | } |
1252 | 1239 | ||
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), | 1240 | /* 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) */ | 1241 | so we're done (this is not an error) */ |
1260 | return -1; | 1242 | return -1; |
@@ -1269,7 +1251,7 @@ ieee80211_radiotap_iterator_next (struct Ieee80211RadiotapHeaderIterator *iterat | |||
1269 | * @return crc sum | 1251 | * @return crc sum |
1270 | */ | 1252 | */ |
1271 | static unsigned long | 1253 | static unsigned long |
1272 | calc_crc_osdep (const unsigned char *buf, size_t len) | 1254 | calc_crc_osdep(const unsigned char *buf, size_t len) |
1273 | { | 1255 | { |
1274 | static const unsigned long int crc_tbl_osdep[256] = { | 1256 | static const unsigned long int crc_tbl_osdep[256] = { |
1275 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, | 1257 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, |
@@ -1342,7 +1324,7 @@ calc_crc_osdep (const unsigned char *buf, size_t len) | |||
1342 | 1324 | ||
1343 | for (; len > 0; len--, buf++) | 1325 | for (; len > 0; len--, buf++) |
1344 | crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8); | 1326 | crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8); |
1345 | return (~crc); | 1327 | return(~crc); |
1346 | } | 1328 | } |
1347 | 1329 | ||
1348 | 1330 | ||
@@ -1355,11 +1337,11 @@ calc_crc_osdep (const unsigned char *buf, size_t len) | |||
1355 | * @return 0 on success (checksum matches), 1 on error | 1337 | * @return 0 on success (checksum matches), 1 on error |
1356 | */ | 1338 | */ |
1357 | static int | 1339 | static int |
1358 | check_crc_buf_osdep (const unsigned char *buf, size_t len) | 1340 | check_crc_buf_osdep(const unsigned char *buf, size_t len) |
1359 | { | 1341 | { |
1360 | unsigned long crc; | 1342 | unsigned long crc; |
1361 | 1343 | ||
1362 | crc = calc_crc_osdep (buf, len); | 1344 | crc = calc_crc_osdep(buf, len); |
1363 | buf += len; | 1345 | buf += len; |
1364 | if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && | 1346 | if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && |
1365 | ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]) | 1347 | ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]) |
@@ -1380,7 +1362,7 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len) | |||
1380 | * @return number of the channel | 1362 | * @return number of the channel |
1381 | */ | 1363 | */ |
1382 | static int | 1364 | static int |
1383 | get_channel_from_frequency (int32_t frequency) | 1365 | get_channel_from_frequency(int32_t frequency) |
1384 | { | 1366 | { |
1385 | if (frequency >= 2412 && frequency <= 2472) | 1367 | if (frequency >= 2412 && frequency <= 2472) |
1386 | return (frequency - 2407) / 5; | 1368 | return (frequency - 2407) / 5; |
@@ -1399,14 +1381,14 @@ get_channel_from_frequency (int32_t frequency) | |||
1399 | * @return channel number, -1 on error | 1381 | * @return channel number, -1 on error |
1400 | */ | 1382 | */ |
1401 | static int | 1383 | static int |
1402 | linux_get_channel (const struct HardwareInfos *dev) | 1384 | linux_get_channel(const struct HardwareInfos *dev) |
1403 | { | 1385 | { |
1404 | struct iwreq wrq; | 1386 | struct iwreq wrq; |
1405 | int32_t frequency; | 1387 | int32_t frequency; |
1406 | 1388 | ||
1407 | memset (&wrq, 0, sizeof (struct iwreq)); | 1389 | memset(&wrq, 0, sizeof(struct iwreq)); |
1408 | strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ); | 1390 | strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ); |
1409 | if (0 > ioctl (dev->fd_raw, SIOCGIWFREQ, &wrq)) | 1391 | if (0 > ioctl(dev->fd_raw, SIOCGIWFREQ, &wrq)) |
1410 | return -1; | 1392 | return -1; |
1411 | frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */ | 1393 | frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */ |
1412 | if (100000000 < frequency) | 1394 | if (100000000 < frequency) |
@@ -1414,7 +1396,7 @@ linux_get_channel (const struct HardwareInfos *dev) | |||
1414 | else if (1000000 < frequency) | 1396 | else if (1000000 < frequency) |
1415 | frequency /= 1000; | 1397 | frequency /= 1000; |
1416 | if (1000 < frequency) | 1398 | if (1000 < frequency) |
1417 | return get_channel_from_frequency (frequency); | 1399 | return get_channel_from_frequency(frequency); |
1418 | return frequency; | 1400 | return frequency; |
1419 | } | 1401 | } |
1420 | 1402 | ||
@@ -1431,9 +1413,9 @@ linux_get_channel (const struct HardwareInfos *dev) | |||
1431 | * @return number of bytes written to 'buf' | 1413 | * @return number of bytes written to 'buf' |
1432 | */ | 1414 | */ |
1433 | static ssize_t | 1415 | static ssize_t |
1434 | linux_read (struct HardwareInfos *dev, | 1416 | linux_read(struct HardwareInfos *dev, |
1435 | unsigned char *buf, size_t buf_size, | 1417 | unsigned char *buf, size_t buf_size, |
1436 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri) | 1418 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri) |
1437 | { | 1419 | { |
1438 | unsigned char tmpbuf[buf_size]; | 1420 | unsigned char tmpbuf[buf_size]; |
1439 | ssize_t caplen; | 1421 | ssize_t caplen; |
@@ -1443,190 +1425,207 @@ linux_read (struct HardwareInfos *dev, | |||
1443 | int got_channel = 0; | 1425 | int got_channel = 0; |
1444 | int fcs_removed = 0; | 1426 | int fcs_removed = 0; |
1445 | 1427 | ||
1446 | caplen = read (dev->fd_raw, tmpbuf, buf_size); | 1428 | caplen = read(dev->fd_raw, tmpbuf, buf_size); |
1447 | if (0 > caplen) | 1429 | if (0 > caplen) |
1448 | { | 1430 | { |
1449 | if (EAGAIN == errno) | 1431 | if (EAGAIN == errno) |
1450 | return 0; | 1432 | return 0; |
1451 | fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno)); | 1433 | fprintf(stderr, "Failed to read from RAW socket: %s\n", strerror(errno)); |
1452 | return -1; | 1434 | return -1; |
1453 | } | 1435 | } |
1454 | 1436 | ||
1455 | memset (ri, 0, sizeof (*ri)); | 1437 | memset(ri, 0, sizeof(*ri)); |
1456 | switch (dev->arptype_in) | 1438 | switch (dev->arptype_in) |
1457 | { | 1439 | { |
1458 | case ARPHRD_IEEE80211_PRISM: | 1440 | case ARPHRD_IEEE80211_PRISM: |
1459 | { | 1441 | { |
1460 | const struct PrismHeader *ph; | 1442 | const struct PrismHeader *ph; |
1461 | 1443 | ||
1462 | ph = (const struct PrismHeader*) tmpbuf; | 1444 | ph = (const struct PrismHeader*)tmpbuf; |
1463 | n = ph->msglen; | 1445 | n = ph->msglen; |
1464 | if ( (n < 8) || (n >= caplen) ) | 1446 | if ((n < 8) || (n >= caplen)) |
1465 | return 0; /* invalid format */ | 1447 | return 0; /* invalid format */ |
1466 | if ( (PRISM_MSGCODE_MONITOR == ph->msgcode) && | 1448 | if ((PRISM_MSGCODE_MONITOR == ph->msgcode) && |
1467 | (n >= sizeof (struct PrismHeader)) ) | 1449 | (n >= sizeof(struct PrismHeader))) |
1468 | { | 1450 | { |
1469 | const char *pos; | 1451 | const char *pos; |
1470 | size_t left; | 1452 | size_t left; |
1471 | struct PrismValue pv; | 1453 | struct PrismValue pv; |
1472 | 1454 | ||
1473 | left = n - sizeof (struct PrismHeader); | 1455 | left = n - sizeof(struct PrismHeader); |
1474 | pos = (const char *) &ph[1]; | 1456 | pos = (const char *)&ph[1]; |
1475 | while (left > sizeof (struct PrismValue)) | 1457 | while (left > sizeof(struct PrismValue)) |
1476 | { | 1458 | { |
1477 | left -= sizeof (struct PrismValue); | 1459 | left -= sizeof(struct PrismValue); |
1478 | GNUNET_memcpy (&pv, pos, sizeof (struct PrismValue)); | 1460 | GNUNET_memcpy(&pv, pos, sizeof(struct PrismValue)); |
1479 | pos += sizeof (struct PrismValue); | 1461 | pos += sizeof(struct PrismValue); |
1480 | 1462 | ||
1481 | switch (pv.did) | 1463 | switch (pv.did) |
1482 | { | 1464 | { |
1483 | case PRISM_DID_NOISE: | 1465 | case PRISM_DID_NOISE: |
1484 | if (PRISM_STATUS_OK == pv.status) | 1466 | if (PRISM_STATUS_OK == pv.status) |
1485 | { | 1467 | { |
1486 | ri->ri_noise = pv.data; | 1468 | ri->ri_noise = pv.data; |
1487 | /* got_noise = 1; */ | 1469 | /* got_noise = 1; */ |
1488 | } | 1470 | } |
1489 | break; | 1471 | break; |
1490 | case PRISM_DID_RATE: | 1472 | |
1491 | if (PRISM_STATUS_OK == pv.status) | 1473 | case PRISM_DID_RATE: |
1492 | ri->ri_rate = pv.data * 500000; | 1474 | if (PRISM_STATUS_OK == pv.status) |
1493 | break; | 1475 | ri->ri_rate = pv.data * 500000; |
1494 | case PRISM_DID_CHANNEL: | 1476 | break; |
1495 | if (PRISM_STATUS_OK == pv.status) | 1477 | |
1496 | { | 1478 | case PRISM_DID_CHANNEL: |
1497 | ri->ri_channel = pv.data; | 1479 | if (PRISM_STATUS_OK == pv.status) |
1498 | got_channel = 1; | 1480 | { |
1499 | } | 1481 | ri->ri_channel = pv.data; |
1500 | break; | 1482 | got_channel = 1; |
1501 | case PRISM_DID_MACTIME: | 1483 | } |
1502 | if (PRISM_STATUS_OK == pv.status) | 1484 | break; |
1503 | ri->ri_mactime = pv.data; | 1485 | |
1504 | break; | 1486 | case PRISM_DID_MACTIME: |
1505 | case PRISM_DID_SIGNAL: | 1487 | if (PRISM_STATUS_OK == pv.status) |
1506 | if (PRISM_STATUS_OK == pv.status) | 1488 | ri->ri_mactime = pv.data; |
1507 | { | 1489 | break; |
1508 | ri->ri_power = pv.data; | 1490 | |
1509 | /* got_signal = 1; */ | 1491 | case PRISM_DID_SIGNAL: |
1510 | } | 1492 | if (PRISM_STATUS_OK == pv.status) |
1511 | break; | 1493 | { |
1512 | } | 1494 | ri->ri_power = pv.data; |
1513 | } | 1495 | /* got_signal = 1; */ |
1514 | } | 1496 | } |
1515 | if ( (n < 8) || (n >= caplen) ) | 1497 | break; |
1516 | return 0; /* invalid format */ | 1498 | } |
1499 | } | ||
1500 | } | ||
1501 | if ((n < 8) || (n >= caplen)) | ||
1502 | return 0; /* invalid format */ | ||
1517 | } | 1503 | } |
1518 | break; | 1504 | break; |
1519 | case ARPHRD_IEEE80211_FULL: | 1505 | |
1506 | case ARPHRD_IEEE80211_FULL: | ||
1520 | { | 1507 | { |
1521 | struct Ieee80211RadiotapHeaderIterator iterator; | 1508 | struct Ieee80211RadiotapHeaderIterator iterator; |
1522 | struct Ieee80211RadiotapHeader *rthdr; | 1509 | struct Ieee80211RadiotapHeader *rthdr; |
1523 | 1510 | ||
1524 | memset (&iterator, 0, sizeof (iterator)); | 1511 | memset(&iterator, 0, sizeof(iterator)); |
1525 | rthdr = (struct Ieee80211RadiotapHeader *) tmpbuf; | 1512 | rthdr = (struct Ieee80211RadiotapHeader *)tmpbuf; |
1526 | n = GNUNET_le16toh (rthdr->it_len); | 1513 | n = GNUNET_le16toh(rthdr->it_len); |
1527 | if ( (n < sizeof (struct Ieee80211RadiotapHeader)) || (n >= caplen)) | 1514 | if ((n < sizeof(struct Ieee80211RadiotapHeader)) || (n >= caplen)) |
1528 | return 0; /* invalid 'it_len' */ | 1515 | return 0; /* invalid 'it_len' */ |
1529 | if (0 != ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen)) | 1516 | if (0 != ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen)) |
1530 | return 0; | 1517 | return 0; |
1531 | /* go through the radiotap arguments we have been given by the driver */ | 1518 | /* go through the radiotap arguments we have been given by the driver */ |
1532 | while (0 <= ieee80211_radiotap_iterator_next (&iterator)) | 1519 | while (0 <= ieee80211_radiotap_iterator_next(&iterator)) |
1533 | { | 1520 | { |
1534 | switch (iterator.this_arg_index) | 1521 | switch (iterator.this_arg_index) |
1535 | { | 1522 | { |
1536 | case IEEE80211_RADIOTAP_TSFT: | 1523 | case IEEE80211_RADIOTAP_TSFT: |
1537 | ri->ri_mactime = GNUNET_le64toh (*((uint64_t *) iterator.this_arg)); | 1524 | ri->ri_mactime = GNUNET_le64toh(*((uint64_t *)iterator.this_arg)); |
1538 | break; | 1525 | break; |
1539 | case IEEE80211_RADIOTAP_DBM_ANTSIGNAL: | 1526 | |
1540 | if (!got_signal) | 1527 | case IEEE80211_RADIOTAP_DBM_ANTSIGNAL: |
1541 | { | 1528 | if (!got_signal) |
1542 | ri->ri_power = * ((int8_t*) iterator.this_arg); | 1529 | { |
1543 | got_signal = 1; | 1530 | ri->ri_power = *((int8_t*)iterator.this_arg); |
1544 | } | 1531 | got_signal = 1; |
1545 | break; | 1532 | } |
1546 | case IEEE80211_RADIOTAP_DB_ANTSIGNAL: | 1533 | break; |
1547 | if (!got_signal) | 1534 | |
1548 | { | 1535 | case IEEE80211_RADIOTAP_DB_ANTSIGNAL: |
1549 | ri->ri_power = * ((int8_t*) iterator.this_arg); | 1536 | if (!got_signal) |
1550 | got_signal = 1; | 1537 | { |
1551 | } | 1538 | ri->ri_power = *((int8_t*)iterator.this_arg); |
1552 | break; | 1539 | got_signal = 1; |
1553 | case IEEE80211_RADIOTAP_DBM_ANTNOISE: | 1540 | } |
1554 | if (!got_noise) | 1541 | break; |
1555 | { | 1542 | |
1556 | ri->ri_noise = * ((int8_t*) iterator.this_arg); | 1543 | case IEEE80211_RADIOTAP_DBM_ANTNOISE: |
1557 | got_noise = 1; | 1544 | if (!got_noise) |
1558 | } | 1545 | { |
1559 | break; | 1546 | ri->ri_noise = *((int8_t*)iterator.this_arg); |
1560 | case IEEE80211_RADIOTAP_DB_ANTNOISE: | 1547 | got_noise = 1; |
1561 | if (!got_noise) | 1548 | } |
1562 | { | 1549 | break; |
1563 | ri->ri_noise = * ((int8_t*) iterator.this_arg); | 1550 | |
1564 | got_noise = 1; | 1551 | case IEEE80211_RADIOTAP_DB_ANTNOISE: |
1565 | } | 1552 | if (!got_noise) |
1566 | break; | 1553 | { |
1567 | case IEEE80211_RADIOTAP_ANTENNA: | 1554 | ri->ri_noise = *((int8_t*)iterator.this_arg); |
1568 | ri->ri_antenna = *iterator.this_arg; | 1555 | got_noise = 1; |
1569 | break; | 1556 | } |
1570 | case IEEE80211_RADIOTAP_CHANNEL: | 1557 | break; |
1571 | ri->ri_channel = *iterator.this_arg; | 1558 | |
1572 | got_channel = 1; | 1559 | case IEEE80211_RADIOTAP_ANTENNA: |
1573 | break; | 1560 | ri->ri_antenna = *iterator.this_arg; |
1574 | case IEEE80211_RADIOTAP_RATE: | 1561 | break; |
1575 | ri->ri_rate = (*iterator.this_arg) * 500000; | 1562 | |
1576 | break; | 1563 | case IEEE80211_RADIOTAP_CHANNEL: |
1577 | case IEEE80211_RADIOTAP_FLAGS: | 1564 | ri->ri_channel = *iterator.this_arg; |
1578 | { | 1565 | got_channel = 1; |
1579 | uint8_t flags = *iterator.this_arg; | 1566 | break; |
1580 | /* is the CRC visible at the end? if so, remove */ | 1567 | |
1581 | if (0 != (flags & IEEE80211_RADIOTAP_F_FCS)) | 1568 | case IEEE80211_RADIOTAP_RATE: |
1582 | { | 1569 | ri->ri_rate = (*iterator.this_arg) * 500000; |
1583 | fcs_removed = 1; | 1570 | break; |
1584 | caplen -= sizeof (uint32_t); | 1571 | |
1585 | } | 1572 | case IEEE80211_RADIOTAP_FLAGS: |
1586 | break; | 1573 | { |
1587 | } | 1574 | uint8_t flags = *iterator.this_arg; |
1588 | case IEEE80211_RADIOTAP_RX_FLAGS: | 1575 | /* is the CRC visible at the end? if so, remove */ |
1589 | { | 1576 | if (0 != (flags & IEEE80211_RADIOTAP_F_FCS)) |
1590 | uint16_t flags = ntohs (* ((uint16_t *) iterator.this_arg)); | 1577 | { |
1591 | if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS)) | 1578 | fcs_removed = 1; |
1592 | return 0; | 1579 | caplen -= sizeof(uint32_t); |
1593 | } | 1580 | } |
1594 | break; | 1581 | break; |
1595 | } /* end of 'switch' */ | 1582 | } |
1596 | } /* end of the 'while' loop */ | 1583 | |
1584 | case IEEE80211_RADIOTAP_RX_FLAGS: | ||
1585 | { | ||
1586 | uint16_t flags = ntohs(*((uint16_t *)iterator.this_arg)); | ||
1587 | if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS)) | ||
1588 | return 0; | ||
1589 | } | ||
1590 | break; | ||
1591 | } /* end of 'switch' */ | ||
1592 | } /* end of the 'while' loop */ | ||
1597 | } | 1593 | } |
1598 | break; | 1594 | break; |
1599 | case ARPHRD_IEEE80211: | 1595 | |
1600 | n = 0; /* no header */ | 1596 | case ARPHRD_IEEE80211: |
1601 | break; | 1597 | n = 0; /* no header */ |
1602 | case ARPHRD_ETHER: | 1598 | break; |
1599 | |||
1600 | case ARPHRD_ETHER: | ||
1603 | { | 1601 | { |
1604 | if (sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen) | 1602 | if (sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen) |
1605 | return 0; /* invalid */ | 1603 | return 0; /* invalid */ |
1606 | GNUNET_memcpy (&buf[sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)], | 1604 | GNUNET_memcpy(&buf[sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)], |
1607 | tmpbuf + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame), | 1605 | tmpbuf + sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame), |
1608 | caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4 /* 4 byte FCS */); | 1606 | caplen - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4 /* 4 byte FCS */); |
1609 | return caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4; | 1607 | return caplen - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4; |
1610 | } | 1608 | } |
1611 | default: | 1609 | |
1612 | errno = ENOTSUP; /* unsupported format */ | 1610 | default: |
1613 | return -1; | 1611 | errno = ENOTSUP; /* unsupported format */ |
1614 | } | 1612 | return -1; |
1613 | } | ||
1615 | caplen -= n; | 1614 | caplen -= n; |
1616 | if (! got_channel) | 1615 | if (!got_channel) |
1617 | ri->ri_channel = linux_get_channel (dev); | 1616 | ri->ri_channel = linux_get_channel(dev); |
1618 | 1617 | ||
1619 | /* detect CRC32 at the end, even if the flag wasn't set and remove it */ | 1618 | /* detect CRC32 at the end, even if the flag wasn't set and remove it */ |
1620 | if ( (0 == fcs_removed) && | 1619 | if ((0 == fcs_removed) && |
1621 | (0 == check_crc_buf_osdep (tmpbuf + n, caplen - sizeof (uint32_t))) ) | 1620 | (0 == check_crc_buf_osdep(tmpbuf + n, caplen - sizeof(uint32_t)))) |
1622 | { | 1621 | { |
1623 | /* NOTE: this heuristic can of course fail if there happens to | 1622 | /* NOTE: this heuristic can of course fail if there happens to |
1624 | be a matching checksum at the end. Would be good to have | 1623 | be a matching checksum at the end. Would be good to have |
1625 | some data to see how often this heuristic actually works. */ | 1624 | some data to see how often this heuristic actually works. */ |
1626 | caplen -= sizeof (uint32_t); | 1625 | caplen -= sizeof(uint32_t); |
1627 | } | 1626 | } |
1628 | /* copy payload to target buffer */ | 1627 | /* copy payload to target buffer */ |
1629 | GNUNET_memcpy (buf, tmpbuf + n, caplen); | 1628 | GNUNET_memcpy(buf, tmpbuf + n, caplen); |
1630 | return caplen; | 1629 | return caplen; |
1631 | } | 1630 | } |
1632 | 1631 | ||
@@ -1643,7 +1642,7 @@ linux_read (struct HardwareInfos *dev, | |||
1643 | * @return 0 on success | 1642 | * @return 0 on success |
1644 | */ | 1643 | */ |
1645 | static int | 1644 | static int |
1646 | open_device_raw (struct HardwareInfos *dev) | 1645 | open_device_raw(struct HardwareInfos *dev) |
1647 | { | 1646 | { |
1648 | struct ifreq ifr; | 1647 | struct ifreq ifr; |
1649 | struct iwreq wrq; | 1648 | struct iwreq wrq; |
@@ -1651,112 +1650,112 @@ open_device_raw (struct HardwareInfos *dev) | |||
1651 | struct sockaddr_ll sll; | 1650 | struct sockaddr_ll sll; |
1652 | 1651 | ||
1653 | /* find the interface index */ | 1652 | /* find the interface index */ |
1654 | memset (&ifr, 0, sizeof (ifr)); | 1653 | memset(&ifr, 0, sizeof(ifr)); |
1655 | strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ); | 1654 | strncpy(ifr.ifr_name, dev->iface, IFNAMSIZ); |
1656 | if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr)) | 1655 | if (-1 == ioctl(dev->fd_raw, SIOCGIFINDEX, &ifr)) |
1657 | { | 1656 | { |
1658 | fprintf (stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n", | 1657 | fprintf(stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n", |
1659 | IFNAMSIZ, dev->iface, strerror (errno)); | 1658 | IFNAMSIZ, dev->iface, strerror(errno)); |
1660 | return 1; | 1659 | return 1; |
1661 | } | 1660 | } |
1662 | 1661 | ||
1663 | /* lookup the hardware type */ | 1662 | /* lookup the hardware type */ |
1664 | memset (&sll, 0, sizeof (sll)); | 1663 | memset(&sll, 0, sizeof(sll)); |
1665 | sll.sll_family = AF_PACKET; | 1664 | sll.sll_family = AF_PACKET; |
1666 | sll.sll_ifindex = ifr.ifr_ifindex; | 1665 | sll.sll_ifindex = ifr.ifr_ifindex; |
1667 | sll.sll_protocol = htons (ETH_P_ALL); | 1666 | sll.sll_protocol = htons(ETH_P_ALL); |
1668 | if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr)) | 1667 | if (-1 == ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) |
1669 | { | 1668 | { |
1670 | fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", | 1669 | fprintf(stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", |
1671 | IFNAMSIZ, dev->iface, strerror (errno)); | 1670 | IFNAMSIZ, dev->iface, strerror(errno)); |
1672 | return 1; | 1671 | return 1; |
1673 | } | 1672 | } |
1674 | if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && | 1673 | if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && |
1675 | (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && | 1674 | (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && |
1676 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && | 1675 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && |
1677 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) ) | 1676 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))) |
1678 | { | 1677 | { |
1679 | fprintf (stderr, "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n", | 1678 | fprintf(stderr, "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n", |
1680 | IFNAMSIZ, dev->iface, | 1679 | IFNAMSIZ, dev->iface, |
1681 | ifr.ifr_hwaddr.sa_family); | 1680 | ifr.ifr_hwaddr.sa_family); |
1682 | return 1; | 1681 | return 1; |
1683 | } | 1682 | } |
1684 | 1683 | ||
1685 | /* lookup iw mode */ | 1684 | /* lookup iw mode */ |
1686 | memset (&wrq, 0, sizeof (struct iwreq)); | 1685 | memset(&wrq, 0, sizeof(struct iwreq)); |
1687 | strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ); | 1686 | strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ); |
1688 | if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq)) | 1687 | if (-1 == ioctl(dev->fd_raw, SIOCGIWMODE, &wrq)) |
1689 | { | 1688 | { |
1690 | /* most probably not supported (ie for rtap ipw interface) * | 1689 | /* most probably not supported (ie for rtap ipw interface) * |
1691 | * so just assume its correctly set... */ | 1690 | * so just assume its correctly set... */ |
1692 | wrq.u.mode = IW_MODE_MONITOR; | 1691 | wrq.u.mode = IW_MODE_MONITOR; |
1693 | } | 1692 | } |
1694 | 1693 | ||
1695 | if ( (wrq.u.mode != IW_MODE_MONITOR) && | 1694 | if ((wrq.u.mode != IW_MODE_MONITOR) && |
1696 | (wrq.u.mode != IW_MODE_ADHOC) ) | 1695 | (wrq.u.mode != IW_MODE_ADHOC)) |
1697 | { | 1696 | { |
1698 | fprintf (stderr, "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n", | 1697 | fprintf(stderr, "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n", |
1699 | IFNAMSIZ, dev->iface, | 1698 | IFNAMSIZ, dev->iface, |
1700 | wrq.u.mode); | 1699 | wrq.u.mode); |
1701 | return 1; | 1700 | return 1; |
1702 | } | 1701 | } |
1703 | 1702 | ||
1704 | /* Is interface st to up, broadcast & running ? */ | 1703 | /* Is interface st to up, broadcast & running ? */ |
1705 | if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags) | 1704 | 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 | { | 1705 | { |
1712 | fprintf (stderr, "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n", | 1706 | /* Bring interface up */ |
1713 | IFNAMSIZ, dev->iface, strerror (errno)); | 1707 | ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING; |
1714 | return 1; | 1708 | |
1709 | if (-1 == ioctl(dev->fd_raw, SIOCSIFFLAGS, &ifr)) | ||
1710 | { | ||
1711 | fprintf(stderr, "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n", | ||
1712 | IFNAMSIZ, dev->iface, strerror(errno)); | ||
1713 | return 1; | ||
1714 | } | ||
1715 | } | 1715 | } |
1716 | } | ||
1717 | 1716 | ||
1718 | /* bind the raw socket to the interface */ | 1717 | /* bind the raw socket to the interface */ |
1719 | if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll))) | 1718 | if (-1 == bind(dev->fd_raw, (struct sockaddr *)&sll, sizeof(sll))) |
1720 | { | 1719 | { |
1721 | fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ, | 1720 | fprintf(stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ, |
1722 | dev->iface, strerror (errno)); | 1721 | dev->iface, strerror(errno)); |
1723 | return 1; | 1722 | return 1; |
1724 | } | 1723 | } |
1725 | 1724 | ||
1726 | /* lookup the hardware type */ | 1725 | /* lookup the hardware type */ |
1727 | if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr)) | 1726 | if (-1 == ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) |
1728 | { | 1727 | { |
1729 | fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", | 1728 | fprintf(stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", |
1730 | IFNAMSIZ, dev->iface, strerror (errno)); | 1729 | IFNAMSIZ, dev->iface, strerror(errno)); |
1731 | return 1; | 1730 | return 1; |
1732 | } | 1731 | } |
1733 | 1732 | ||
1734 | GNUNET_memcpy (&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE); | 1733 | GNUNET_memcpy(&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE); |
1735 | dev->arptype_in = ifr.ifr_hwaddr.sa_family; | 1734 | dev->arptype_in = ifr.ifr_hwaddr.sa_family; |
1736 | if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && | 1735 | if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && |
1737 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && | 1736 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && |
1738 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && | 1737 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && |
1739 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) | 1738 | (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) |
1740 | { | 1739 | { |
1741 | fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n", | 1740 | fprintf(stderr, "Unsupported hardware link type %d on interface `%.*s'\n", |
1742 | ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface); | 1741 | ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface); |
1743 | return 1; | 1742 | return 1; |
1744 | } | 1743 | } |
1745 | 1744 | ||
1746 | /* enable promiscuous mode */ | 1745 | /* enable promiscuous mode */ |
1747 | memset (&mr, 0, sizeof (mr)); | 1746 | memset(&mr, 0, sizeof(mr)); |
1748 | mr.mr_ifindex = sll.sll_ifindex; | 1747 | mr.mr_ifindex = sll.sll_ifindex; |
1749 | mr.mr_type = PACKET_MR_PROMISC; | 1748 | mr.mr_type = PACKET_MR_PROMISC; |
1750 | if (0 != | 1749 | if (0 != |
1751 | setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, | 1750 | setsockopt(dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, |
1752 | sizeof (mr))) | 1751 | sizeof(mr))) |
1753 | { | 1752 | { |
1754 | fprintf (stderr, | 1753 | fprintf(stderr, |
1755 | "Failed to enable promiscuous mode on interface `%.*s'\n", | 1754 | "Failed to enable promiscuous mode on interface `%.*s'\n", |
1756 | IFNAMSIZ, | 1755 | IFNAMSIZ, |
1757 | dev->iface); | 1756 | dev->iface); |
1758 | return 1; | 1757 | return 1; |
1759 | } | 1758 | } |
1760 | return 0; | 1759 | return 0; |
1761 | } | 1760 | } |
1762 | 1761 | ||
@@ -1769,22 +1768,22 @@ open_device_raw (struct HardwareInfos *dev) | |||
1769 | * @return 0 on success, 1 on error | 1768 | * @return 0 on success, 1 on error |
1770 | */ | 1769 | */ |
1771 | static int | 1770 | static int |
1772 | test_wlan_interface (const char *iface) | 1771 | test_wlan_interface(const char *iface) |
1773 | { | 1772 | { |
1774 | char strbuf[512]; | 1773 | char strbuf[512]; |
1775 | struct stat sbuf; | 1774 | struct stat sbuf; |
1776 | int ret; | 1775 | int ret; |
1777 | 1776 | ||
1778 | ret = snprintf (strbuf, sizeof (strbuf), | 1777 | ret = snprintf(strbuf, sizeof(strbuf), |
1779 | "/sys/class/net/%s/phy80211/subsystem", | 1778 | "/sys/class/net/%s/phy80211/subsystem", |
1780 | iface); | 1779 | iface); |
1781 | if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf))) | 1780 | if ((ret < 0) || (ret >= sizeof(strbuf)) || (0 != stat(strbuf, &sbuf))) |
1782 | { | 1781 | { |
1783 | fprintf (stderr, | 1782 | fprintf(stderr, |
1784 | "Did not find 802.11 interface `%s'. Exiting.\n", | 1783 | "Did not find 802.11 interface `%s'. Exiting.\n", |
1785 | iface); | 1784 | iface); |
1786 | exit (1); | 1785 | exit(1); |
1787 | } | 1786 | } |
1788 | return 0; | 1787 | return 0; |
1789 | } | 1788 | } |
1790 | 1789 | ||
@@ -1797,19 +1796,19 @@ test_wlan_interface (const char *iface) | |||
1797 | * @return 0 if mac belongs to us, 1 if mac is for another target | 1796 | * @return 0 if mac belongs to us, 1 if mac is for another target |
1798 | */ | 1797 | */ |
1799 | static int | 1798 | static int |
1800 | mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, | 1799 | mac_test(const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, |
1801 | const struct HardwareInfos *dev) | 1800 | const struct HardwareInfos *dev) |
1802 | { | 1801 | { |
1803 | static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros; | 1802 | static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros; |
1804 | 1803 | ||
1805 | if ( (0 == memcmp (&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) || | 1804 | if ((0 == memcmp(&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) || |
1806 | (0 == memcmp (&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE)) ) | 1805 | (0 == memcmp(&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE))) |
1807 | return 0; /* some drivers set no Macs, then assume it is all for us! */ | 1806 | return 0; /* some drivers set no Macs, then assume it is all for us! */ |
1808 | 1807 | ||
1809 | if (0 != memcmp (&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE)) | 1808 | if (0 != memcmp(&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE)) |
1810 | return 1; /* not a GNUnet ad-hoc package */ | 1809 | return 1; /* not a GNUnet ad-hoc package */ |
1811 | if ( (0 == memcmp (&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) || | 1810 | if ((0 == memcmp(&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) || |
1812 | (0 == memcmp (&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE)) ) | 1811 | (0 == memcmp(&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE))) |
1813 | return 0; /* for us, or broadcast */ | 1812 | return 0; /* for us, or broadcast */ |
1814 | return 1; /* not for us */ | 1813 | return 1; /* not for us */ |
1815 | } | 1814 | } |
@@ -1822,10 +1821,10 @@ mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, | |||
1822 | * @param dev pointer to the Hardware_Infos struct | 1821 | * @param dev pointer to the Hardware_Infos struct |
1823 | */ | 1822 | */ |
1824 | static void | 1823 | static void |
1825 | mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, | 1824 | mac_set(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, |
1826 | const struct HardwareInfos *dev) | 1825 | const struct HardwareInfos *dev) |
1827 | { | 1826 | { |
1828 | taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA); | 1827 | taIeeeHeader->frame_control = htons(IEEE80211_FC0_TYPE_DATA); |
1829 | taIeeeHeader->addr2 = dev->pl_mac; | 1828 | taIeeeHeader->addr2 = dev->pl_mac; |
1830 | taIeeeHeader->addr3 = mac_bssid_gnunet; | 1829 | taIeeeHeader->addr3 = mac_bssid_gnunet; |
1831 | } | 1830 | } |
@@ -1840,7 +1839,7 @@ mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, | |||
1840 | * @param hdr pointer to the start of the packet | 1839 | * @param hdr pointer to the start of the packet |
1841 | */ | 1840 | */ |
1842 | static void | 1841 | static void |
1843 | stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr) | 1842 | stdin_send_hw(void *cls, const struct GNUNET_MessageHeader *hdr) |
1844 | { | 1843 | { |
1845 | struct HardwareInfos *dev = cls; | 1844 | struct HardwareInfos *dev = cls; |
1846 | const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header; | 1845 | const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header; |
@@ -1849,56 +1848,58 @@ stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr) | |||
1849 | struct RadiotapTransmissionHeader rtheader; | 1848 | struct RadiotapTransmissionHeader rtheader; |
1850 | struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader; | 1849 | struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader; |
1851 | 1850 | ||
1852 | sendsize = ntohs (hdr->size); | 1851 | sendsize = ntohs(hdr->size); |
1853 | if ( (sendsize < | 1852 | if ((sendsize < |
1854 | sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) || | 1853 | sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) || |
1855 | (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ) | 1854 | (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs(hdr->type))) |
1856 | { | 1855 | { |
1857 | fprintf (stderr, "Received malformed message\n"); | 1856 | fprintf(stderr, "Received malformed message\n"); |
1858 | exit (1); | 1857 | exit(1); |
1859 | } | 1858 | } |
1860 | sendsize -= (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); | 1859 | sendsize -= (sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); |
1861 | if (MAXLINE < sendsize) | 1860 | if (MAXLINE < sendsize) |
1862 | { | 1861 | { |
1863 | fprintf (stderr, "Packet too big for buffer\n"); | 1862 | fprintf(stderr, "Packet too big for buffer\n"); |
1864 | exit (1); | 1863 | exit(1); |
1865 | } | 1864 | } |
1866 | header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr; | 1865 | header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *)hdr; |
1867 | switch (dev->arptype_in) | 1866 | switch (dev->arptype_in) |
1868 | { | 1867 | { |
1869 | case ARPHRD_IEEE80211_PRISM: | 1868 | case ARPHRD_IEEE80211_PRISM: |
1870 | case ARPHRD_IEEE80211_FULL: | 1869 | case ARPHRD_IEEE80211_FULL: |
1871 | case ARPHRD_IEEE80211: | 1870 | case ARPHRD_IEEE80211: |
1872 | rtheader.header.it_version = 0; | 1871 | rtheader.header.it_version = 0; |
1873 | rtheader.header.it_pad = 0; | 1872 | rtheader.header.it_pad = 0; |
1874 | rtheader.header.it_len = GNUNET_htole16 (sizeof (rtheader)); | 1873 | rtheader.header.it_len = GNUNET_htole16(sizeof(rtheader)); |
1875 | rtheader.header.it_present = GNUNET_htole16 (IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK); | 1874 | rtheader.header.it_present = GNUNET_htole16(IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK); |
1876 | rtheader.rate = header->rate; | 1875 | rtheader.rate = header->rate; |
1877 | rtheader.pad1 = 0; | 1876 | rtheader.pad1 = 0; |
1878 | rtheader.txflags = GNUNET_htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ); | 1877 | rtheader.txflags = GNUNET_htole16(IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ); |
1879 | GNUNET_memcpy (write_pout.buf, &rtheader, sizeof (rtheader)); | 1878 | GNUNET_memcpy(write_pout.buf, &rtheader, sizeof(rtheader)); |
1880 | GNUNET_memcpy (&write_pout.buf[sizeof (rtheader)], &header->frame, sendsize); | 1879 | GNUNET_memcpy(&write_pout.buf[sizeof(rtheader)], &header->frame, sendsize); |
1881 | wlanheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) &write_pout.buf[sizeof (rtheader)]; | 1880 | wlanheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *)&write_pout.buf[sizeof(rtheader)]; |
1882 | 1881 | ||
1883 | /* payload contains MAC address, but we don't trust it, so we'll | 1882 | /* payload contains MAC address, but we don't trust it, so we'll |
1884 | * overwrite it with OUR MAC address to prevent mischief */ | 1883 | * overwrite it with OUR MAC address to prevent mischief */ |
1885 | mac_set (wlanheader, dev); | 1884 | mac_set(wlanheader, dev); |
1886 | write_pout.size = sendsize + sizeof (rtheader); | 1885 | write_pout.size = sendsize + sizeof(rtheader); |
1887 | break; | 1886 | break; |
1888 | case ARPHRD_ETHER: | 1887 | |
1889 | etheader.dst = header->frame.addr1; | 1888 | case ARPHRD_ETHER: |
1890 | /* etheader.src = header->frame.addr2; --- untrusted input */ | 1889 | etheader.dst = header->frame.addr1; |
1891 | etheader.src = dev->pl_mac; | 1890 | /* etheader.src = header->frame.addr2; --- untrusted input */ |
1892 | etheader.type = htons (ETH_P_IP); | 1891 | etheader.src = dev->pl_mac; |
1893 | GNUNET_memcpy (write_pout.buf, ðeader, sizeof (etheader)); | 1892 | etheader.type = htons(ETH_P_IP); |
1894 | GNUNET_memcpy (&write_pout.buf[sizeof (etheader)], &header[1], sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); | 1893 | GNUNET_memcpy(write_pout.buf, ðeader, sizeof(etheader)); |
1895 | write_pout.size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) + sizeof (etheader); | 1894 | GNUNET_memcpy(&write_pout.buf[sizeof(etheader)], &header[1], sendsize - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); |
1896 | break; | 1895 | write_pout.size = sendsize - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) + sizeof(etheader); |
1897 | default: | 1896 | break; |
1898 | fprintf (stderr, | 1897 | |
1899 | "Unsupported ARPTYPE!\n"); | 1898 | default: |
1900 | break; | 1899 | fprintf(stderr, |
1901 | } | 1900 | "Unsupported ARPTYPE!\n"); |
1901 | break; | ||
1902 | } | ||
1902 | } | 1903 | } |
1903 | 1904 | ||
1904 | 1905 | ||
@@ -1913,7 +1914,7 @@ stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr) | |||
1913 | * @return 0 on success (never happens, as we don't return unless aborted), 1 on error | 1914 | * @return 0 on success (never happens, as we don't return unless aborted), 1 on error |
1914 | */ | 1915 | */ |
1915 | int | 1916 | int |
1916 | main (int argc, char *argv[]) | 1917 | main(int argc, char *argv[]) |
1917 | { | 1918 | { |
1918 | struct HardwareInfos dev; | 1919 | struct HardwareInfos dev; |
1919 | char readbuf[MAXLINE]; | 1920 | char readbuf[MAXLINE]; |
@@ -1927,83 +1928,83 @@ main (int argc, char *argv[]) | |||
1927 | /* assert privs so we can modify the firewall rules! */ | 1928 | /* assert privs so we can modify the firewall rules! */ |
1928 | { | 1929 | { |
1929 | #ifdef HAVE_SETRESUID | 1930 | #ifdef HAVE_SETRESUID |
1930 | uid_t uid = getuid (); | 1931 | uid_t uid = getuid(); |
1931 | 1932 | ||
1932 | if (0 != setresuid (uid, 0, 0)) | 1933 | if (0 != setresuid(uid, 0, 0)) |
1933 | { | 1934 | { |
1934 | fprintf (stderr, | 1935 | fprintf(stderr, |
1935 | "Failed to setresuid to root: %s\n", | 1936 | "Failed to setresuid to root: %s\n", |
1936 | strerror (errno)); | 1937 | strerror(errno)); |
1937 | return 254; | 1938 | return 254; |
1938 | } | 1939 | } |
1939 | #else | 1940 | #else |
1940 | if (0 != seteuid (0)) | 1941 | if (0 != seteuid(0)) |
1941 | { | 1942 | { |
1942 | fprintf (stderr, | 1943 | fprintf(stderr, |
1943 | "Failed to seteuid back to root: %s\n", strerror (errno)); | 1944 | "Failed to seteuid back to root: %s\n", strerror(errno)); |
1944 | return 254; | 1945 | return 254; |
1945 | } | 1946 | } |
1946 | #endif | 1947 | #endif |
1947 | } | 1948 | } |
1948 | 1949 | ||
1949 | /* make use of SGID capabilities on POSIX */ | 1950 | /* make use of SGID capabilities on POSIX */ |
1950 | memset (&dev, 0, sizeof (dev)); | 1951 | memset(&dev, 0, sizeof(dev)); |
1951 | dev.fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL)); | 1952 | dev.fd_raw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
1952 | raw_eno = errno; /* remember for later */ | 1953 | raw_eno = errno; /* remember for later */ |
1953 | 1954 | ||
1954 | /* now that we've dropped root rights, we can do error checking */ | 1955 | /* now that we've dropped root rights, we can do error checking */ |
1955 | if (2 != argc) | 1956 | if (2 != argc) |
1956 | { | 1957 | { |
1957 | fprintf (stderr, | 1958 | fprintf(stderr, |
1958 | "You must specify the name of the interface as the first and only argument to this program.\n"); | 1959 | "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 | if (-1 != dev.fd_raw) |
1960 | (void) close (dev.fd_raw); | 1961 | (void)close(dev.fd_raw); |
1961 | return 1; | 1962 | return 1; |
1962 | } | 1963 | } |
1963 | 1964 | ||
1964 | if (-1 == dev.fd_raw) | 1965 | if (-1 == dev.fd_raw) |
1965 | { | 1966 | { |
1966 | fprintf (stderr, "Failed to create raw socket: %s\n", strerror (raw_eno)); | 1967 | fprintf(stderr, "Failed to create raw socket: %s\n", strerror(raw_eno)); |
1967 | return 1; | 1968 | return 1; |
1968 | } | 1969 | } |
1969 | if (dev.fd_raw >= FD_SETSIZE) | 1970 | 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 | { | 1971 | { |
1994 | fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); | 1972 | fprintf(stderr, "File descriptor too large for select (%d > %d)\n", |
1995 | if (-1 != dev.fd_raw) | 1973 | dev.fd_raw, FD_SETSIZE); |
1996 | (void) close (dev.fd_raw); | 1974 | (void)close(dev.fd_raw); |
1997 | return 1; | 1975 | return 1; |
1998 | } | 1976 | } |
1999 | #else | 1977 | if (0 != test_wlan_interface(argv[1])) |
2000 | if (0 != (setuid (uid) | seteuid (uid))) | ||
2001 | { | 1978 | { |
2002 | fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); | 1979 | (void)close(dev.fd_raw); |
2003 | if (-1 != dev.fd_raw) | ||
2004 | (void) close (dev.fd_raw); | ||
2005 | return 1; | 1980 | return 1; |
2006 | } | 1981 | } |
1982 | strncpy(dev.iface, argv[1], IFNAMSIZ); | ||
1983 | if (0 != open_device_raw(&dev)) | ||
1984 | { | ||
1985 | (void)close(dev.fd_raw); | ||
1986 | return 1; | ||
1987 | } | ||
1988 | |||
1989 | /* drop privs */ | ||
1990 | { | ||
1991 | uid_t uid = getuid(); | ||
1992 | #ifdef HAVE_SETRESUID | ||
1993 | if (0 != setresuid(uid, uid, uid)) | ||
1994 | { | ||
1995 | fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno)); | ||
1996 | if (-1 != dev.fd_raw) | ||
1997 | (void)close(dev.fd_raw); | ||
1998 | return 1; | ||
1999 | } | ||
2000 | #else | ||
2001 | if (0 != (setuid(uid) | seteuid(uid))) | ||
2002 | { | ||
2003 | fprintf(stderr, "Failed to setuid: %s\n", strerror(errno)); | ||
2004 | if (-1 != dev.fd_raw) | ||
2005 | (void)close(dev.fd_raw); | ||
2006 | return 1; | ||
2007 | } | ||
2007 | #endif | 2008 | #endif |
2008 | } | 2009 | } |
2009 | 2010 | ||
@@ -2012,141 +2013,141 @@ main (int argc, char *argv[]) | |||
2012 | { | 2013 | { |
2013 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; | 2014 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; |
2014 | 2015 | ||
2015 | macmsg.hdr.size = htons (sizeof (macmsg)); | 2016 | macmsg.hdr.size = htons(sizeof(macmsg)); |
2016 | macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); | 2017 | 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(&macmsg.mac, &dev.pl_mac, sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress)); |
2018 | GNUNET_memcpy (write_std.buf, &macmsg, sizeof (macmsg)); | 2019 | GNUNET_memcpy(write_std.buf, &macmsg, sizeof(macmsg)); |
2019 | write_std.size = sizeof (macmsg); | 2020 | write_std.size = sizeof(macmsg); |
2020 | } | 2021 | } |
2021 | 2022 | ||
2022 | stdin_mst = mst_create (&stdin_send_hw, &dev); | 2023 | stdin_mst = mst_create(&stdin_send_hw, &dev); |
2023 | stdin_open = 1; | 2024 | stdin_open = 1; |
2024 | while (1) | 2025 | 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 | { | 2026 | { |
2050 | int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL); | 2027 | maxfd = -1; |
2051 | if ((-1 == retval) && (EINTR == errno)) | 2028 | FD_ZERO(&rfds); |
2052 | continue; | 2029 | if ((0 == write_pout.size) && (1 == stdin_open)) |
2053 | if (0 > retval) | 2030 | { |
2031 | FD_SET(STDIN_FILENO, &rfds); | ||
2032 | maxfd = MAX(maxfd, STDIN_FILENO); | ||
2033 | } | ||
2034 | if (0 == write_std.size) | ||
2035 | { | ||
2036 | FD_SET(dev.fd_raw, &rfds); | ||
2037 | maxfd = MAX(maxfd, dev.fd_raw); | ||
2038 | } | ||
2039 | FD_ZERO(&wfds); | ||
2040 | if (0 < write_std.size) | ||
2041 | { | ||
2042 | FD_SET(STDOUT_FILENO, &wfds); | ||
2043 | maxfd = MAX(maxfd, STDOUT_FILENO); | ||
2044 | } | ||
2045 | if (0 < write_pout.size) | ||
2046 | { | ||
2047 | FD_SET(dev.fd_raw, &wfds); | ||
2048 | maxfd = MAX(maxfd, dev.fd_raw); | ||
2049 | } | ||
2054 | { | 2050 | { |
2055 | fprintf (stderr, "select failed: %s\n", strerror (errno)); | 2051 | int retval = select(maxfd + 1, &rfds, &wfds, NULL, NULL); |
2056 | break; | 2052 | if ((-1 == retval) && (EINTR == errno)) |
2053 | continue; | ||
2054 | if (0 > retval) | ||
2055 | { | ||
2056 | fprintf(stderr, "select failed: %s\n", strerror(errno)); | ||
2057 | break; | ||
2058 | } | ||
2057 | } | 2059 | } |
2060 | if (FD_ISSET(STDOUT_FILENO, &wfds)) | ||
2061 | { | ||
2062 | ssize_t ret = | ||
2063 | write(STDOUT_FILENO, write_std.buf + write_std.pos, | ||
2064 | write_std.size - write_std.pos); | ||
2065 | if (0 > ret) | ||
2066 | { | ||
2067 | fprintf(stderr, "Failed to write to STDOUT: %s\n", strerror(errno)); | ||
2068 | break; | ||
2069 | } | ||
2070 | write_std.pos += ret; | ||
2071 | if (write_std.pos == write_std.size) | ||
2072 | { | ||
2073 | write_std.pos = 0; | ||
2074 | write_std.size = 0; | ||
2075 | } | ||
2076 | } | ||
2077 | if (FD_ISSET(dev.fd_raw, &wfds)) | ||
2078 | { | ||
2079 | ssize_t ret = | ||
2080 | write(dev.fd_raw, write_pout.buf + write_pout.pos, | ||
2081 | write_pout.size - write_pout.pos); | ||
2082 | if (0 > ret) | ||
2083 | { | ||
2084 | fprintf(stderr, "Failed to write to WLAN device: %s\n", | ||
2085 | strerror(errno)); | ||
2086 | break; | ||
2087 | } | ||
2088 | write_pout.pos += ret; | ||
2089 | if ((write_pout.pos != write_pout.size) && (0 != ret)) | ||
2090 | { | ||
2091 | /* we should not get partial sends with packet-oriented devices... */ | ||
2092 | fprintf(stderr, "Write error, partial send: %u/%u\n", | ||
2093 | (unsigned int)write_pout.pos, | ||
2094 | (unsigned int)write_pout.size); | ||
2095 | break; | ||
2096 | } | ||
2097 | if (write_pout.pos == write_pout.size) | ||
2098 | { | ||
2099 | write_pout.pos = 0; | ||
2100 | write_pout.size = 0; | ||
2101 | } | ||
2102 | } | ||
2103 | |||
2104 | if (FD_ISSET(STDIN_FILENO, &rfds)) | ||
2105 | { | ||
2106 | ssize_t ret = | ||
2107 | read(STDIN_FILENO, readbuf, sizeof(readbuf)); | ||
2108 | if (0 > ret) | ||
2109 | { | ||
2110 | fprintf(stderr, "Read error from STDIN: %s\n", strerror(errno)); | ||
2111 | break; | ||
2112 | } | ||
2113 | if (0 == ret) | ||
2114 | { | ||
2115 | /* stop reading... */ | ||
2116 | stdin_open = 0; | ||
2117 | } | ||
2118 | mst_receive(stdin_mst, readbuf, ret); | ||
2119 | } | ||
2120 | |||
2121 | if (FD_ISSET(dev.fd_raw, &rfds)) | ||
2122 | { | ||
2123 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm; | ||
2124 | ssize_t ret; | ||
2125 | |||
2126 | rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *)write_std.buf; | ||
2127 | ret = | ||
2128 | linux_read(&dev, (unsigned char *)&rrm->frame, | ||
2129 | sizeof(write_std.buf) | ||
2130 | - sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | ||
2131 | + sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), | ||
2132 | rrm); | ||
2133 | if (0 > ret) | ||
2134 | { | ||
2135 | fprintf(stderr, "Read error from raw socket: %s\n", strerror(errno)); | ||
2136 | break; | ||
2137 | } | ||
2138 | if ((0 < ret) && (0 == mac_test(&rrm->frame, &dev))) | ||
2139 | { | ||
2140 | write_std.size = ret | ||
2141 | + sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | ||
2142 | - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); | ||
2143 | rrm->header.size = htons(write_std.size); | ||
2144 | rrm->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); | ||
2145 | } | ||
2146 | } | ||
2058 | } | 2147 | } |
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_pout.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 | /* Error handling, try to clean up a bit at least */ |
2148 | mst_destroy (stdin_mst); | 2149 | mst_destroy(stdin_mst); |
2149 | (void) close (dev.fd_raw); | 2150 | (void)close(dev.fd_raw); |
2150 | return 1; /* we never exit 'normally' */ | 2151 | return 1; /* we never exit 'normally' */ |
2151 | } | 2152 | } |
2152 | 2153 | ||