aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-helper-transport-wlan.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/transport/gnunet-helper-transport-wlan.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/transport/gnunet-helper-transport-wlan.c')
-rw-r--r--src/transport/gnunet-helper-transport-wlan.c1514
1 files changed, 777 insertions, 737 deletions
diff --git a/src/transport/gnunet-helper-transport-wlan.c b/src/transport/gnunet-helper-transport-wlan.c
index 885edb1b0..7ad6c5175 100644
--- a/src/transport/gnunet-helper-transport-wlan.c
+++ b/src/transport/gnunet-helper-transport-wlan.c
@@ -234,7 +234,8 @@
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 */
237struct PrismValue { 237struct PrismValue
238{
238 /** 239 /**
239 * This has a different ID for each parameter, see 240 * This has a different ID for each parameter, see
240 * PRISM_DID_* constants. 241 * PRISM_DID_* constants.
@@ -263,7 +264,8 @@ struct PrismValue {
263/** 264/**
264 * Prism header format ('struct p80211msg' in Linux). All in host byte order (!). 265 * Prism header format ('struct p80211msg' in Linux). All in host byte order (!).
265 */ 266 */
266struct PrismHeader { 267struct PrismHeader
268{
267 /** 269 /**
268 * We expect this to be a PRISM_MSGCODE_*. 270 * We expect this to be a PRISM_MSGCODE_*.
269 */ 271 */
@@ -301,7 +303,8 @@ struct PrismHeader {
301 * reliable indicator of alignment requirement. See also 303 * reliable indicator of alignment requirement. See also
302 * 'man 9 ieee80211_radiotap'. 304 * 'man 9 ieee80211_radiotap'.
303 */ 305 */
304enum RadiotapType { 306enum RadiotapType
307{
305 /** 308 /**
306 * IEEE80211_RADIOTAP_TSFT __le64 microseconds 309 * IEEE80211_RADIOTAP_TSFT __le64 microseconds
307 * 310 *
@@ -576,7 +579,8 @@ enum RadiotapType {
576 * The radio capture header precedes the 802.11 header. 579 * The radio capture header precedes the 802.11 header.
577 * All data in the header is little endian on all platforms. 580 * All data in the header is little endian on all platforms.
578 */ 581 */
579struct Ieee80211RadiotapHeader { 582struct Ieee80211RadiotapHeader
583{
580 /** 584 /**
581 * Version 0. Only increases for drastic changes, introduction of 585 * Version 0. Only increases for drastic changes, introduction of
582 * compatible new fields does not count. 586 * compatible new fields does not count.
@@ -607,7 +611,8 @@ struct Ieee80211RadiotapHeader {
607 * Format of the header we need to prepend to messages to be sent to the 611 * Format of the header we need to prepend to messages to be sent to the
608 * Kernel. 612 * Kernel.
609 */ 613 */
610struct RadiotapTransmissionHeader { 614struct RadiotapTransmissionHeader
615{
611 /** 616 /**
612 * First we begin with the 'generic' header we also get when receiving 617 * First we begin with the 'generic' header we also get when receiving
613 * messages. 618 * messages.
@@ -638,7 +643,12 @@ struct RadiotapTransmissionHeader {
638 * following value for 'header.it_present' based on the presence of 643 * following value for 'header.it_present' based on the presence of
639 * the 'rate' and 'txflags' in the overall struct. 644 * the 'rate' and 'txflags' in the overall struct.
640 */ 645 */
641#define IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK ((1 << IEEE80211_RADIOTAP_RATE) | (1 << IEEE80211_RADIOTAP_TX_FLAGS)) 646#define IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK ((1 \
647 << \
648 IEEE80211_RADIOTAP_RATE) \
649 | (1 \
650 << \
651 IEEE80211_RADIOTAP_TX_FLAGS))
642 652
643 653
644 654
@@ -646,7 +656,8 @@ struct RadiotapTransmissionHeader {
646 * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments 656 * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments
647 * in the radiotap header. Used when we parse radiotap packets received from the kernel. 657 * in the radiotap header. Used when we parse radiotap packets received from the kernel.
648 */ 658 */
649struct Ieee80211RadiotapHeaderIterator { 659struct Ieee80211RadiotapHeaderIterator
660{
650 /** 661 /**
651 * pointer to the radiotap header we are walking through 662 * pointer to the radiotap header we are walking through
652 */ 663 */
@@ -698,7 +709,8 @@ struct Ieee80211RadiotapHeaderIterator {
698 * struct for storing the information of the hardware. There is only 709 * struct for storing the information of the hardware. There is only
699 * one of these. 710 * one of these.
700 */ 711 */
701struct HardwareInfos { 712struct HardwareInfos
713{
702 /** 714 /**
703 * file descriptor for the raw socket 715 * file descriptor for the raw socket
704 */ 716 */
@@ -725,7 +737,8 @@ struct HardwareInfos {
725/** 737/**
726 * IO buffer used for buffering data in transit (to wireless or to stdout). 738 * IO buffer used for buffering data in transit (to wireless or to stdout).
727 */ 739 */
728struct SendBuffer { 740struct SendBuffer
741{
729 /** 742 /**
730 * How many bytes of data are stored in 'buf' for transmission right now? 743 * How many bytes of data are stored in 'buf' for transmission right now?
731 * Data always starts at offset 0 and extends to 'size'. 744 * Data always starts at offset 0 and extends to 'size'.
@@ -787,7 +800,8 @@ typedef void (*MessageTokenizerCallback) (void *cls,
787/** 800/**
788 * Handle to a message stream tokenizer. 801 * Handle to a message stream tokenizer.
789 */ 802 */
790struct MessageStreamTokenizer { 803struct MessageStreamTokenizer
804{
791 /** 805 /**
792 * Function to call on completed messages. 806 * Function to call on completed messages.
793 */ 807 */
@@ -828,23 +842,23 @@ struct MessageStreamTokenizer {
828 * @return handle to tokenizer 842 * @return handle to tokenizer
829 */ 843 */
830static struct MessageStreamTokenizer * 844static struct MessageStreamTokenizer *
831mst_create(MessageTokenizerCallback cb, 845mst_create (MessageTokenizerCallback cb,
832 void *cb_cls) 846 void *cb_cls)
833{ 847{
834 struct MessageStreamTokenizer *ret; 848 struct MessageStreamTokenizer *ret;
835 849
836 ret = malloc(sizeof(struct MessageStreamTokenizer)); 850 ret = malloc (sizeof(struct MessageStreamTokenizer));
837 if (NULL == ret) 851 if (NULL == ret)
838 { 852 {
839 fprintf(stderr, "Failed to allocate buffer for tokenizer\n"); 853 fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
840 exit(1); 854 exit (1);
841 } 855 }
842 ret->hdr = malloc(MIN_BUFFER_SIZE); 856 ret->hdr = malloc (MIN_BUFFER_SIZE);
843 if (NULL == ret->hdr) 857 if (NULL == ret->hdr)
844 { 858 {
845 fprintf(stderr, "Failed to allocate buffer for alignment\n"); 859 fprintf (stderr, "Failed to allocate buffer for alignment\n");
846 exit(1); 860 exit (1);
847 } 861 }
848 ret->curr_buf = MIN_BUFFER_SIZE; 862 ret->curr_buf = MIN_BUFFER_SIZE;
849 ret->cb = cb; 863 ret->cb = cb;
850 ret->cb_cls = cb_cls; 864 ret->cb_cls = cb_cls;
@@ -863,8 +877,8 @@ mst_create(MessageTokenizerCallback cb,
863 * GNUNET_SYSERR if the data stream is corrupt 877 * GNUNET_SYSERR if the data stream is corrupt
864 */ 878 */
865static int 879static int
866mst_receive(struct MessageStreamTokenizer *mst, 880mst_receive (struct MessageStreamTokenizer *mst,
867 const char *buf, size_t size) 881 const char *buf, size_t size)
868{ 882{
869 const struct GNUNET_MessageHeader *hdr; 883 const struct GNUNET_MessageHeader *hdr;
870 size_t delta; 884 size_t delta;
@@ -875,132 +889,132 @@ mst_receive(struct MessageStreamTokenizer *mst,
875 int ret; 889 int ret;
876 890
877 ret = GNUNET_OK; 891 ret = GNUNET_OK;
878 ibuf = (char *)mst->hdr; 892 ibuf = (char *) mst->hdr;
879 while (mst->pos > 0) 893 while (mst->pos > 0)
880 { 894 {
881do_align: 895do_align:
882 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) || 896 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
883 (0 != (mst->off % ALIGN_FACTOR))) 897 (0 != (mst->off % ALIGN_FACTOR)))
884 { 898 {
885 /* need to align or need more space */ 899 /* need to align or need more space */
886 mst->pos -= mst->off; 900 mst->pos -= mst->off;
887 memmove(ibuf, &ibuf[mst->off], mst->pos); 901 memmove (ibuf, &ibuf[mst->off], mst->pos);
888 mst->off = 0; 902 mst->off = 0;
889 } 903 }
890 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) 904 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
891 { 905 {
892 delta = 906 delta =
893 GNUNET_MIN(sizeof(struct GNUNET_MessageHeader) - 907 GNUNET_MIN (sizeof(struct GNUNET_MessageHeader)
894 (mst->pos - mst->off), size); 908 - (mst->pos - mst->off), size);
895 GNUNET_memcpy(&ibuf[mst->pos], buf, delta); 909 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
896 mst->pos += delta; 910 mst->pos += delta;
897 buf += delta; 911 buf += delta;
898 size -= delta; 912 size -= delta;
899 } 913 }
900 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader)) 914 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
901 { 915 {
902 return GNUNET_OK; 916 return GNUNET_OK;
903 } 917 }
904 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off]; 918 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
905 want = ntohs(hdr->size); 919 want = ntohs (hdr->size);
906 if (want < sizeof(struct GNUNET_MessageHeader)) 920 if (want < sizeof(struct GNUNET_MessageHeader))
907 { 921 {
908 fprintf(stderr, 922 fprintf (stderr,
909 "Received invalid message from stdin\n"); 923 "Received invalid message from stdin\n");
910 exit(1); 924 exit (1);
911 } 925 }
912 if (mst->curr_buf - mst->off < want) 926 if (mst->curr_buf - mst->off < want)
913 { 927 {
914 /* need more space */ 928 /* need more space */
915 mst->pos -= mst->off; 929 mst->pos -= mst->off;
916 memmove(ibuf, &ibuf[mst->off], mst->pos); 930 memmove (ibuf, &ibuf[mst->off], mst->pos);
917 mst->off = 0; 931 mst->off = 0;
918 } 932 }
919 if (want > mst->curr_buf) 933 if (want > mst->curr_buf)
920 { 934 {
921 mst->hdr = realloc(mst->hdr, want); 935 mst->hdr = realloc (mst->hdr, want);
922 if (NULL == mst->hdr) 936 if (NULL == mst->hdr)
923 { 937 {
924 fprintf(stderr, "Failed to allocate buffer for alignment\n"); 938 fprintf (stderr, "Failed to allocate buffer for alignment\n");
925 exit(1); 939 exit (1);
926 } 940 }
927 ibuf = (char *)mst->hdr; 941 ibuf = (char *) mst->hdr;
928 mst->curr_buf = want; 942 mst->curr_buf = want;
929 } 943 }
930 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off]; 944 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
931 if (mst->pos - mst->off < want) 945 if (mst->pos - mst->off < want)
932 { 946 {
933 delta = GNUNET_MIN(want - (mst->pos - mst->off), size); 947 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
934 GNUNET_memcpy(&ibuf[mst->pos], buf, delta); 948 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
935 mst->pos += delta; 949 mst->pos += delta;
936 buf += delta; 950 buf += delta;
937 size -= delta; 951 size -= delta;
938 } 952 }
939 if (mst->pos - mst->off < want) 953 if (mst->pos - mst->off < want)
940 { 954 {
941 return GNUNET_OK; 955 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 }
951 } 956 }
957 mst->cb (mst->cb_cls, hdr);
958 mst->off += want;
959 if (mst->off == mst->pos)
960 {
961 /* reset to beginning of buffer, it's free right now! */
962 mst->off = 0;
963 mst->pos = 0;
964 }
965 }
952 while (size > 0) 966 while (size > 0)
967 {
968 if (size < sizeof(struct GNUNET_MessageHeader))
969 break;
970 offset = (unsigned long) buf;
971 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
972 if (GNUNET_NO == need_align)
953 { 973 {
954 if (size < sizeof(struct GNUNET_MessageHeader)) 974 /* can try to do zero-copy and process directly from original buffer */
955 break; 975 hdr = (const struct GNUNET_MessageHeader *) buf;
956 offset = (unsigned long)buf; 976 want = ntohs (hdr->size);
957 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO; 977 if (want < sizeof(struct GNUNET_MessageHeader))
958 if (GNUNET_NO == need_align) 978 {
959 { 979 fprintf (stderr,
960 /* can try to do zero-copy and process directly from original buffer */ 980 "Received invalid message from stdin\n");
961 hdr = (const struct GNUNET_MessageHeader *)buf; 981 exit (1);
962 want = ntohs(hdr->size); 982 }
963 if (want < sizeof(struct GNUNET_MessageHeader)) 983 if (size < want)
964 { 984 break; /* or not, buffer incomplete, so copy to private buffer... */
965 fprintf(stderr, 985 mst->cb (mst->cb_cls, hdr);
966 "Received invalid message from stdin\n"); 986 buf += want;
967 exit(1); 987 size -= want;
968 } 988 }
969 if (size < want) 989 else
970 break; /* or not, buffer incomplete, so copy to private buffer... */ 990 {
971 mst->cb(mst->cb_cls, hdr); 991 /* need to copy to private buffer to align;
972 buf += want; 992 * yes, we go a bit more spagetti than usual here */
973 size -= want; 993 goto do_align;
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 }
981 } 994 }
995 }
982 if (size > 0) 996 if (size > 0)
997 {
998 if (size + mst->pos > mst->curr_buf)
983 { 999 {
984 if (size + mst->pos > mst->curr_buf) 1000 mst->hdr = realloc (mst->hdr, size + mst->pos);
985 { 1001 if (NULL == mst->hdr)
986 mst->hdr = realloc(mst->hdr, size + mst->pos); 1002 {
987 if (NULL == mst->hdr) 1003 fprintf (stderr, "Failed to allocate buffer for alignment\n");
988 { 1004 exit (1);
989 fprintf(stderr, "Failed to allocate buffer for alignment\n"); 1005 }
990 exit(1); 1006 ibuf = (char *) mst->hdr;
991 } 1007 mst->curr_buf = size + mst->pos;
992 ibuf = (char *)mst->hdr; 1008 }
993 mst->curr_buf = size + mst->pos; 1009 if (mst->pos + size > mst->curr_buf)
994 } 1010 {
995 if (mst->pos + size > mst->curr_buf) 1011 fprintf (stderr,
996 { 1012 "Assertion failed\n");
997 fprintf(stderr, 1013 exit (1);
998 "Assertion failed\n");
999 exit(1);
1000 }
1001 GNUNET_memcpy(&ibuf[mst->pos], buf, size);
1002 mst->pos += size;
1003 } 1014 }
1015 GNUNET_memcpy (&ibuf[mst->pos], buf, size);
1016 mst->pos += size;
1017 }
1004 return ret; 1018 return ret;
1005} 1019}
1006 1020
@@ -1011,10 +1025,10 @@ do_align:
1011 * @param mst tokenizer to destroy 1025 * @param mst tokenizer to destroy
1012 */ 1026 */
1013static void 1027static void
1014mst_destroy(struct MessageStreamTokenizer *mst) 1028mst_destroy (struct MessageStreamTokenizer *mst)
1015{ 1029{
1016 free(mst->hdr); 1030 free (mst->hdr);
1017 free(mst); 1031 free (mst);
1018} 1032}
1019 1033
1020/* ***************** end of server_mst.c clone ***************** **/ 1034/* ***************** end of server_mst.c clone ***************** **/
@@ -1040,9 +1054,11 @@ mst_destroy(struct MessageStreamTokenizer *mst)
1040 * @return 0 on success, -1 on error 1054 * @return 0 on success, -1 on error
1041 */ 1055 */
1042static int 1056static int
1043ieee80211_radiotap_iterator_init(struct Ieee80211RadiotapHeaderIterator *iterator, 1057ieee80211_radiotap_iterator_init (struct
1044 const struct Ieee80211RadiotapHeader *radiotap_header, 1058 Ieee80211RadiotapHeaderIterator *iterator,
1045 size_t max_length) 1059 const struct
1060 Ieee80211RadiotapHeader *radiotap_header,
1061 size_t max_length)
1046{ 1062{
1047 if ((iterator == NULL) || 1063 if ((iterator == NULL) ||
1048 (radiotap_header == NULL)) 1064 (radiotap_header == NULL))
@@ -1054,36 +1070,39 @@ ieee80211_radiotap_iterator_init(struct Ieee80211RadiotapHeaderIterator *iterato
1054 1070
1055 /* sanity check for allowed length and radiotap length field */ 1071 /* sanity check for allowed length and radiotap length field */
1056 if ((max_length < sizeof(struct Ieee80211RadiotapHeader)) || 1072 if ((max_length < sizeof(struct Ieee80211RadiotapHeader)) ||
1057 (max_length < (GNUNET_le16toh(radiotap_header->it_len)))) 1073 (max_length < (GNUNET_le16toh (radiotap_header->it_len))))
1058 return -1; 1074 return -1;
1059 1075
1060 memset(iterator, 0, sizeof(struct Ieee80211RadiotapHeaderIterator)); 1076 memset (iterator, 0, sizeof(struct Ieee80211RadiotapHeaderIterator));
1061 iterator->rtheader = radiotap_header; 1077 iterator->rtheader = radiotap_header;
1062 iterator->max_length = GNUNET_le16toh(radiotap_header->it_len); 1078 iterator->max_length = GNUNET_le16toh (radiotap_header->it_len);
1063 iterator->bitmap_shifter = GNUNET_le32toh(radiotap_header->it_present); 1079 iterator->bitmap_shifter = GNUNET_le32toh (radiotap_header->it_present);
1064 iterator->arg = ((uint8_t *)radiotap_header) + sizeof(struct Ieee80211RadiotapHeader); 1080 iterator->arg = ((uint8_t *) radiotap_header) + sizeof(struct
1081 Ieee80211RadiotapHeader);
1065 1082
1066 /* find payload start allowing for extended bitmap(s) */ 1083 /* find payload start allowing for extended bitmap(s) */
1067 if (0 != (iterator->bitmap_shifter & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK)) 1084 if (0 != (iterator->bitmap_shifter & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK))
1085 {
1086 while (GNUNET_le32toh (*((uint32_t *) iterator->arg))
1087 & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK)
1068 { 1088 {
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); 1089 iterator->arg += sizeof(uint32_t);
1081 /* 1090 /*
1082 * no need to check again for blowing past stated radiotap 1091 * check for insanity where the present bitmaps
1083 * header length, becuase ieee80211_radiotap_iterator_next 1092 * keep claiming to extend up to or even beyond the
1084 * checks it before it is dereferenced 1093 * stated radiotap header length
1085 */ 1094 */
1095 if (iterator->arg - ((uint8_t*) iterator->rtheader) >
1096 iterator->max_length)
1097 return -1;
1086 } 1098 }
1099 iterator->arg += sizeof(uint32_t);
1100 /*
1101 * no need to check again for blowing past stated radiotap
1102 * header length, becuase ieee80211_radiotap_iterator_next
1103 * checks it before it is dereferenced
1104 */
1105 }
1087 /* we are all initialized happily */ 1106 /* we are all initialized happily */
1088 return 0; 1107 return 0;
1089} 1108}
@@ -1102,7 +1121,8 @@ ieee80211_radiotap_iterator_init(struct Ieee80211RadiotapHeaderIterator *iterato
1102 * @return next present arg index on success or -1 if no more or error 1121 * @return next present arg index on success or -1 if no more or error
1103 */ 1122 */
1104static int 1123static int
1105ieee80211_radiotap_iterator_next(struct Ieee80211RadiotapHeaderIterator *iterator) 1124ieee80211_radiotap_iterator_next (struct
1125 Ieee80211RadiotapHeaderIterator *iterator)
1106{ 1126{
1107 /* 1127 /*
1108 * small length lookup table for all radiotap types we heard of 1128 * small length lookup table for all radiotap types we heard of
@@ -1149,94 +1169,96 @@ ieee80211_radiotap_iterator_next(struct Ieee80211RadiotapHeaderIterator *iterato
1149 * least skip (by knowing the length)... 1169 * least skip (by knowing the length)...
1150 */ 1170 */
1151 while (iterator->arg_index < sizeof(rt_sizes)) 1171 while (iterator->arg_index < sizeof(rt_sizes))
1172 {
1173 int hit = (0 != (iterator->bitmap_shifter & 1));
1174
1175 if (hit)
1152 { 1176 {
1153 int hit = (0 != (iterator->bitmap_shifter & 1)); 1177 unsigned int wanted_alignment;
1178 unsigned int unalignment;
1179 /*
1180 * arg is present, account for alignment padding
1181 * 8-bit args can be at any alignment
1182 * 16-bit args must start on 16-bit boundary
1183 * 32-bit args must start on 32-bit boundary
1184 * 64-bit args must start on 64-bit boundary
1185 *
1186 * note that total arg size can differ from alignment of
1187 * elements inside arg, so we use upper nybble of length table
1188 * to base alignment on. First, 'wanted_alignment' is set to be
1189 * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit
1190 * arguments. Then, we calculate the 'unalignment' (how many
1191 * bytes we are over by taking the difference of 'arg' and the
1192 * overall starting point modulo the desired alignment. As
1193 * desired alignments are powers of two, we can do modulo with
1194 * binary "&" (and also avoid the possibility of a division by
1195 * zero if the 'rt_sizes' table contains bogus entries).
1196 *
1197 * also note: these alignments are relative to the start of the
1198 * radiotap header. There is no guarantee that the radiotap
1199 * header itself is aligned on any kind of boundary, thus we
1200 * need to really look at the delta here.
1201 */
1202 wanted_alignment = rt_sizes[iterator->arg_index] >> 4;
1203 unalignment = (((void *) iterator->arg) - ((void *) iterator->rtheader))
1204 & (wanted_alignment - 1);
1205 if (0 != unalignment)
1206 {
1207 /* need padding (by 'wanted_alignment - unalignment') */
1208 iterator->arg_index += wanted_alignment - unalignment;
1209 }
1154 1210
1155 if (hit) 1211 /*
1156 { 1212 * this is what we will return to user, but we need to
1157 unsigned int wanted_alignment; 1213 * move on first so next call has something fresh to test
1158 unsigned int unalignment; 1214 */
1159 /* 1215 iterator->this_arg_index = iterator->arg_index;
1160 * arg is present, account for alignment padding 1216 iterator->this_arg = iterator->arg;
1161 * 8-bit args can be at any alignment
1162 * 16-bit args must start on 16-bit boundary
1163 * 32-bit args must start on 32-bit boundary
1164 * 64-bit args must start on 64-bit boundary
1165 *
1166 * note that total arg size can differ from alignment of
1167 * elements inside arg, so we use upper nybble of length table
1168 * to base alignment on. First, 'wanted_alignment' is set to be
1169 * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit
1170 * arguments. Then, we calculate the 'unalignment' (how many
1171 * bytes we are over by taking the difference of 'arg' and the
1172 * overall starting point modulo the desired alignment. As
1173 * desired alignments are powers of two, we can do modulo with
1174 * binary "&" (and also avoid the possibility of a division by
1175 * zero if the 'rt_sizes' table contains bogus entries).
1176 *
1177 * also note: these alignments are relative to the start of the
1178 * radiotap header. There is no guarantee that the radiotap
1179 * header itself is aligned on any kind of boundary, thus we
1180 * need to really look at the delta here.
1181 */
1182 wanted_alignment = rt_sizes[iterator->arg_index] >> 4;
1183 unalignment = (((void *)iterator->arg) - ((void *)iterator->rtheader)) & (wanted_alignment - 1);
1184 if (0 != unalignment)
1185 {
1186 /* need padding (by 'wanted_alignment - unalignment') */
1187 iterator->arg_index += wanted_alignment - unalignment;
1188 }
1189 1217
1190 /* 1218 /* internally move on the size of this arg (using lower nybble from
1191 * this is what we will return to user, but we need to 1219 the table) */
1192 * move on first so next call has something fresh to test 1220 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
1193 */
1194 iterator->this_arg_index = iterator->arg_index;
1195 iterator->this_arg = iterator->arg;
1196
1197 /* internally move on the size of this arg (using lower nybble from
1198 the table) */
1199 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
1200
1201 /*
1202 * check for insanity where we are given a bitmap that
1203 * claims to have more arg content than the length of the
1204 * radiotap section. We will normally end up equalling this
1205 * max_length on the last arg, never exceeding it.
1206 */
1207 if ((((void *)iterator->arg) - ((void *)iterator->rtheader)) > iterator->max_length)
1208 return -1;
1209 }
1210 1221
1211 /* Now, move on to next bit / next entry */ 1222 /*
1212 iterator->arg_index++; 1223 * check for insanity where we are given a bitmap that
1224 * claims to have more arg content than the length of the
1225 * radiotap section. We will normally end up equalling this
1226 * max_length on the last arg, never exceeding it.
1227 */
1228 if ((((void *) iterator->arg) - ((void *) iterator->rtheader)) >
1229 iterator->max_length)
1230 return -1;
1231 }
1213 1232
1214 if (0 == (iterator->arg_index % 32)) 1233 /* Now, move on to next bit / next entry */
1215 { 1234 iterator->arg_index++;
1216 /* completed current uint32_t bitmap */
1217 if (0 != (iterator->bitmap_shifter & 1))
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 }
1229 else
1230 {
1231 /* just try the next bit (while loop will move on) */
1232 iterator->bitmap_shifter >>= 1;
1233 }
1234 1235
1235 /* if we found a valid arg earlier, return it now */ 1236 if (0 == (iterator->arg_index % 32))
1236 if (hit) 1237 {
1237 return iterator->this_arg_index; 1238 /* completed current uint32_t bitmap */
1239 if (0 != (iterator->bitmap_shifter & 1))
1240 {
1241 /* bit 31 was set, there is more; move to next uint32_t bitmap */
1242 iterator->bitmap_shifter = GNUNET_le32toh (*iterator->next_bitmap);
1243 iterator->next_bitmap++;
1244 }
1245 else
1246 {
1247 /* no more bitmaps: end (by setting arg_index to high, unsupported value) */
1248 iterator->arg_index = sizeof(rt_sizes);
1249 }
1250 }
1251 else
1252 {
1253 /* just try the next bit (while loop will move on) */
1254 iterator->bitmap_shifter >>= 1;
1238 } 1255 }
1239 1256
1257 /* if we found a valid arg earlier, return it now */
1258 if (hit)
1259 return iterator->this_arg_index;
1260 }
1261
1240 /* we don't know how to handle any more args (or there are no more), 1262 /* we don't know how to handle any more args (or there are no more),
1241 so we're done (this is not an error) */ 1263 so we're done (this is not an error) */
1242 return -1; 1264 return -1;
@@ -1251,7 +1273,7 @@ ieee80211_radiotap_iterator_next(struct Ieee80211RadiotapHeaderIterator *iterato
1251 * @return crc sum 1273 * @return crc sum
1252 */ 1274 */
1253static unsigned long 1275static unsigned long
1254calc_crc_osdep(const unsigned char *buf, size_t len) 1276calc_crc_osdep (const unsigned char *buf, size_t len)
1255{ 1277{
1256 static const unsigned long int crc_tbl_osdep[256] = { 1278 static const unsigned long int crc_tbl_osdep[256] = {
1257 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 1279 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
@@ -1337,14 +1359,14 @@ calc_crc_osdep(const unsigned char *buf, size_t len)
1337 * @return 0 on success (checksum matches), 1 on error 1359 * @return 0 on success (checksum matches), 1 on error
1338 */ 1360 */
1339static int 1361static int
1340check_crc_buf_osdep(const unsigned char *buf, size_t len) 1362check_crc_buf_osdep (const unsigned char *buf, size_t len)
1341{ 1363{
1342 unsigned long crc; 1364 unsigned long crc;
1343 1365
1344 crc = calc_crc_osdep(buf, len); 1366 crc = calc_crc_osdep (buf, len);
1345 buf += len; 1367 buf += len;
1346 if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && 1368 if ((((crc) & 0xFF) == buf[0])&&(((crc >> 8) & 0xFF) == buf[1])&&
1347 ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]) 1369 ( ((crc >> 16) & 0xFF) == buf[2]) &&( ((crc >> 24) & 0xFF) == buf[3]) )
1348 return 0; 1370 return 0;
1349 return 1; 1371 return 1;
1350} 1372}
@@ -1362,13 +1384,13 @@ check_crc_buf_osdep(const unsigned char *buf, size_t len)
1362 * @return number of the channel 1384 * @return number of the channel
1363 */ 1385 */
1364static int 1386static int
1365get_channel_from_frequency(int32_t frequency) 1387get_channel_from_frequency (int32_t frequency)
1366{ 1388{
1367 if (frequency >= 2412 && frequency <= 2472) 1389 if ((frequency >= 2412)&&(frequency <= 2472))
1368 return (frequency - 2407) / 5; 1390 return (frequency - 2407) / 5;
1369 if (frequency == 2484) 1391 if (frequency == 2484)
1370 return 14; 1392 return 14;
1371 if (frequency >= 5000 && frequency <= 6100) 1393 if ((frequency >= 5000)&&(frequency <= 6100))
1372 return (frequency - 5000) / 5; 1394 return (frequency - 5000) / 5;
1373 return -1; 1395 return -1;
1374} 1396}
@@ -1381,14 +1403,14 @@ get_channel_from_frequency(int32_t frequency)
1381 * @return channel number, -1 on error 1403 * @return channel number, -1 on error
1382 */ 1404 */
1383static int 1405static int
1384linux_get_channel(const struct HardwareInfos *dev) 1406linux_get_channel (const struct HardwareInfos *dev)
1385{ 1407{
1386 struct iwreq wrq; 1408 struct iwreq wrq;
1387 int32_t frequency; 1409 int32_t frequency;
1388 1410
1389 memset(&wrq, 0, sizeof(struct iwreq)); 1411 memset (&wrq, 0, sizeof(struct iwreq));
1390 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ); 1412 strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
1391 if (0 > ioctl(dev->fd_raw, SIOCGIWFREQ, &wrq)) 1413 if (0 > ioctl (dev->fd_raw, SIOCGIWFREQ, &wrq))
1392 return -1; 1414 return -1;
1393 frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */ 1415 frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */
1394 if (100000000 < frequency) 1416 if (100000000 < frequency)
@@ -1396,7 +1418,7 @@ linux_get_channel(const struct HardwareInfos *dev)
1396 else if (1000000 < frequency) 1418 else if (1000000 < frequency)
1397 frequency /= 1000; 1419 frequency /= 1000;
1398 if (1000 < frequency) 1420 if (1000 < frequency)
1399 return get_channel_from_frequency(frequency); 1421 return get_channel_from_frequency (frequency);
1400 return frequency; 1422 return frequency;
1401} 1423}
1402 1424
@@ -1413,9 +1435,9 @@ linux_get_channel(const struct HardwareInfos *dev)
1413 * @return number of bytes written to 'buf' 1435 * @return number of bytes written to 'buf'
1414 */ 1436 */
1415static ssize_t 1437static ssize_t
1416linux_read(struct HardwareInfos *dev, 1438linux_read (struct HardwareInfos *dev,
1417 unsigned char *buf, size_t buf_size, 1439 unsigned char *buf, size_t buf_size,
1418 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri) 1440 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri)
1419{ 1441{
1420 unsigned char tmpbuf[buf_size]; 1442 unsigned char tmpbuf[buf_size];
1421 ssize_t caplen; 1443 ssize_t caplen;
@@ -1425,207 +1447,210 @@ linux_read(struct HardwareInfos *dev,
1425 int got_channel = 0; 1447 int got_channel = 0;
1426 int fcs_removed = 0; 1448 int fcs_removed = 0;
1427 1449
1428 caplen = read(dev->fd_raw, tmpbuf, buf_size); 1450 caplen = read (dev->fd_raw, tmpbuf, buf_size);
1429 if (0 > caplen) 1451 if (0 > caplen)
1430 { 1452 {
1431 if (EAGAIN == errno) 1453 if (EAGAIN == errno)
1432 return 0; 1454 return 0;
1433 fprintf(stderr, "Failed to read from RAW socket: %s\n", strerror(errno)); 1455 fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno));
1434 return -1; 1456 return -1;
1435 } 1457 }
1436 1458
1437 memset(ri, 0, sizeof(*ri)); 1459 memset (ri, 0, sizeof(*ri));
1438 switch (dev->arptype_in) 1460 switch (dev->arptype_in)
1439 { 1461 {
1440 case ARPHRD_IEEE80211_PRISM: 1462 case ARPHRD_IEEE80211_PRISM:
1441 { 1463 {
1442 const struct PrismHeader *ph; 1464 const struct PrismHeader *ph;
1443 1465
1444 ph = (const struct PrismHeader*)tmpbuf; 1466 ph = (const struct PrismHeader*) tmpbuf;
1445 n = ph->msglen; 1467 n = ph->msglen;
1446 if ((n < 8) || (n >= caplen)) 1468 if ((n < 8) || (n >= caplen))
1447 return 0; /* invalid format */ 1469 return 0; /* invalid format */
1448 if ((PRISM_MSGCODE_MONITOR == ph->msgcode) && 1470 if ((PRISM_MSGCODE_MONITOR == ph->msgcode) &&
1449 (n >= sizeof(struct PrismHeader))) 1471 (n >= sizeof(struct PrismHeader)))
1472 {
1473 const char *pos;
1474 size_t left;
1475 struct PrismValue pv;
1476
1477 left = n - sizeof(struct PrismHeader);
1478 pos = (const char *) &ph[1];
1479 while (left > sizeof(struct PrismValue))
1450 { 1480 {
1451 const char *pos; 1481 left -= sizeof(struct PrismValue);
1452 size_t left; 1482 GNUNET_memcpy (&pv, pos, sizeof(struct PrismValue));
1453 struct PrismValue pv; 1483 pos += sizeof(struct PrismValue);
1484
1485 switch (pv.did)
1486 {
1487 case PRISM_DID_NOISE:
1488 if (PRISM_STATUS_OK == pv.status)
1489 {
1490 ri->ri_noise = pv.data;
1491 /* got_noise = 1; */
1492 }
1493 break;
1494
1495 case PRISM_DID_RATE:
1496 if (PRISM_STATUS_OK == pv.status)
1497 ri->ri_rate = pv.data * 500000;
1498 break;
1499
1500 case PRISM_DID_CHANNEL:
1501 if (PRISM_STATUS_OK == pv.status)
1502 {
1503 ri->ri_channel = pv.data;
1504 got_channel = 1;
1505 }
1506 break;
1507
1508 case PRISM_DID_MACTIME:
1509 if (PRISM_STATUS_OK == pv.status)
1510 ri->ri_mactime = pv.data;
1511 break;
1454 1512
1455 left = n - sizeof(struct PrismHeader); 1513 case PRISM_DID_SIGNAL:
1456 pos = (const char *)&ph[1]; 1514 if (PRISM_STATUS_OK == pv.status)
1457 while (left > sizeof(struct PrismValue))
1458 { 1515 {
1459 left -= sizeof(struct PrismValue); 1516 ri->ri_power = pv.data;
1460 GNUNET_memcpy(&pv, pos, sizeof(struct PrismValue)); 1517 /* got_signal = 1; */
1461 pos += sizeof(struct PrismValue);
1462
1463 switch (pv.did)
1464 {
1465 case PRISM_DID_NOISE:
1466 if (PRISM_STATUS_OK == pv.status)
1467 {
1468 ri->ri_noise = pv.data;
1469 /* got_noise = 1; */
1470 }
1471 break;
1472
1473 case PRISM_DID_RATE:
1474 if (PRISM_STATUS_OK == pv.status)
1475 ri->ri_rate = pv.data * 500000;
1476 break;
1477
1478 case PRISM_DID_CHANNEL:
1479 if (PRISM_STATUS_OK == pv.status)
1480 {
1481 ri->ri_channel = pv.data;
1482 got_channel = 1;
1483 }
1484 break;
1485
1486 case PRISM_DID_MACTIME:
1487 if (PRISM_STATUS_OK == pv.status)
1488 ri->ri_mactime = pv.data;
1489 break;
1490
1491 case PRISM_DID_SIGNAL:
1492 if (PRISM_STATUS_OK == pv.status)
1493 {
1494 ri->ri_power = pv.data;
1495 /* got_signal = 1; */
1496 }
1497 break;
1498 }
1499 } 1518 }
1519 break;
1520 }
1500 } 1521 }
1522 }
1501 if ((n < 8) || (n >= caplen)) 1523 if ((n < 8) || (n >= caplen))
1502 return 0; /* invalid format */ 1524 return 0; /* invalid format */
1503 } 1525 }
1504 break; 1526 break;
1505 1527
1506 case ARPHRD_IEEE80211_FULL: 1528 case ARPHRD_IEEE80211_FULL:
1507 { 1529 {
1508 struct Ieee80211RadiotapHeaderIterator iterator; 1530 struct Ieee80211RadiotapHeaderIterator iterator;
1509 struct Ieee80211RadiotapHeader *rthdr; 1531 struct Ieee80211RadiotapHeader *rthdr;
1510 1532
1511 memset(&iterator, 0, sizeof(iterator)); 1533 memset (&iterator, 0, sizeof(iterator));
1512 rthdr = (struct Ieee80211RadiotapHeader *)tmpbuf; 1534 rthdr = (struct Ieee80211RadiotapHeader *) tmpbuf;
1513 n = GNUNET_le16toh(rthdr->it_len); 1535 n = GNUNET_le16toh (rthdr->it_len);
1514 if ((n < sizeof(struct Ieee80211RadiotapHeader)) || (n >= caplen)) 1536 if ((n < sizeof(struct Ieee80211RadiotapHeader)) || (n >= caplen))
1515 return 0; /* invalid 'it_len' */ 1537 return 0; /* invalid 'it_len' */
1516 if (0 != ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen)) 1538 if (0 != ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen))
1517 return 0; 1539 return 0;
1518 /* go through the radiotap arguments we have been given by the driver */ 1540 /* go through the radiotap arguments we have been given by the driver */
1519 while (0 <= ieee80211_radiotap_iterator_next(&iterator)) 1541 while (0 <= ieee80211_radiotap_iterator_next (&iterator))
1542 {
1543 switch (iterator.this_arg_index)
1520 { 1544 {
1521 switch (iterator.this_arg_index) 1545 case IEEE80211_RADIOTAP_TSFT:
1522 { 1546 ri->ri_mactime = GNUNET_le64toh (*((uint64_t *) iterator.this_arg));
1523 case IEEE80211_RADIOTAP_TSFT: 1547 break;
1524 ri->ri_mactime = GNUNET_le64toh(*((uint64_t *)iterator.this_arg));
1525 break;
1526
1527 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1528 if (!got_signal)
1529 {
1530 ri->ri_power = *((int8_t*)iterator.this_arg);
1531 got_signal = 1;
1532 }
1533 break;
1534
1535 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1536 if (!got_signal)
1537 {
1538 ri->ri_power = *((int8_t*)iterator.this_arg);
1539 got_signal = 1;
1540 }
1541 break;
1542
1543 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
1544 if (!got_noise)
1545 {
1546 ri->ri_noise = *((int8_t*)iterator.this_arg);
1547 got_noise = 1;
1548 }
1549 break;
1550
1551 case IEEE80211_RADIOTAP_DB_ANTNOISE:
1552 if (!got_noise)
1553 {
1554 ri->ri_noise = *((int8_t*)iterator.this_arg);
1555 got_noise = 1;
1556 }
1557 break;
1558
1559 case IEEE80211_RADIOTAP_ANTENNA:
1560 ri->ri_antenna = *iterator.this_arg;
1561 break;
1562
1563 case IEEE80211_RADIOTAP_CHANNEL:
1564 ri->ri_channel = *iterator.this_arg;
1565 got_channel = 1;
1566 break;
1567 1548
1568 case IEEE80211_RADIOTAP_RATE: 1549 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1569 ri->ri_rate = (*iterator.this_arg) * 500000; 1550 if (! got_signal)
1570 break; 1551 {
1552 ri->ri_power = *((int8_t*) iterator.this_arg);
1553 got_signal = 1;
1554 }
1555 break;
1571 1556
1572 case IEEE80211_RADIOTAP_FLAGS: 1557 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1573 { 1558 if (! got_signal)
1574 uint8_t flags = *iterator.this_arg; 1559 {
1575 /* is the CRC visible at the end? if so, remove */ 1560 ri->ri_power = *((int8_t*) iterator.this_arg);
1576 if (0 != (flags & IEEE80211_RADIOTAP_F_FCS)) 1561 got_signal = 1;
1577 { 1562 }
1578 fcs_removed = 1; 1563 break;
1579 caplen -= sizeof(uint32_t); 1564
1580 } 1565 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
1581 break; 1566 if (! got_noise)
1582 } 1567 {
1568 ri->ri_noise = *((int8_t*) iterator.this_arg);
1569 got_noise = 1;
1570 }
1571 break;
1572
1573 case IEEE80211_RADIOTAP_DB_ANTNOISE:
1574 if (! got_noise)
1575 {
1576 ri->ri_noise = *((int8_t*) iterator.this_arg);
1577 got_noise = 1;
1578 }
1579 break;
1583 1580
1584 case IEEE80211_RADIOTAP_RX_FLAGS: 1581 case IEEE80211_RADIOTAP_ANTENNA:
1582 ri->ri_antenna = *iterator.this_arg;
1583 break;
1584
1585 case IEEE80211_RADIOTAP_CHANNEL:
1586 ri->ri_channel = *iterator.this_arg;
1587 got_channel = 1;
1588 break;
1589
1590 case IEEE80211_RADIOTAP_RATE:
1591 ri->ri_rate = (*iterator.this_arg) * 500000;
1592 break;
1593
1594 case IEEE80211_RADIOTAP_FLAGS:
1595 {
1596 uint8_t flags = *iterator.this_arg;
1597 /* is the CRC visible at the end? if so, remove */
1598 if (0 != (flags & IEEE80211_RADIOTAP_F_FCS))
1585 { 1599 {
1586 uint16_t flags = ntohs(*((uint16_t *)iterator.this_arg)); 1600 fcs_removed = 1;
1587 if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS)) 1601 caplen -= sizeof(uint32_t);
1588 return 0;
1589 } 1602 }
1590 break; 1603 break;
1591 } /* end of 'switch' */ 1604 }
1592 } /* end of the 'while' loop */ 1605
1606 case IEEE80211_RADIOTAP_RX_FLAGS:
1607 {
1608 uint16_t flags = ntohs (*((uint16_t *) iterator.this_arg));
1609 if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS))
1610 return 0;
1611 }
1612 break;
1613 } /* end of 'switch' */
1614 } /* end of the 'while' loop */
1593 } 1615 }
1594 break; 1616 break;
1595 1617
1596 case ARPHRD_IEEE80211: 1618 case ARPHRD_IEEE80211:
1597 n = 0; /* no header */ 1619 n = 0; /* no header */
1598 break; 1620 break;
1599 1621
1600 case ARPHRD_ETHER: 1622 case ARPHRD_ETHER:
1601 { 1623 {
1602 if (sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen) 1624 if (sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen)
1603 return 0; /* invalid */ 1625 return 0; /* invalid */
1604 GNUNET_memcpy(&buf[sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)], 1626 GNUNET_memcpy (&buf[sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)],
1605 tmpbuf + sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame), 1627 tmpbuf + sizeof(struct
1606 caplen - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4 /* 4 byte FCS */); 1628 GNUNET_TRANSPORT_WLAN_Ieee8023Frame),
1629 caplen - sizeof(struct
1630 GNUNET_TRANSPORT_WLAN_Ieee8023Frame)
1631 - 4 /* 4 byte FCS */);
1607 return caplen - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4; 1632 return caplen - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4;
1608 } 1633 }
1609 1634
1610 default: 1635 default:
1611 errno = ENOTSUP; /* unsupported format */ 1636 errno = ENOTSUP; /* unsupported format */
1612 return -1; 1637 return -1;
1613 } 1638 }
1614 caplen -= n; 1639 caplen -= n;
1615 if (!got_channel) 1640 if (! got_channel)
1616 ri->ri_channel = linux_get_channel(dev); 1641 ri->ri_channel = linux_get_channel (dev);
1617 1642
1618 /* detect CRC32 at the end, even if the flag wasn't set and remove it */ 1643 /* detect CRC32 at the end, even if the flag wasn't set and remove it */
1619 if ((0 == fcs_removed) && 1644 if ((0 == fcs_removed) &&
1620 (0 == check_crc_buf_osdep(tmpbuf + n, caplen - sizeof(uint32_t)))) 1645 (0 == check_crc_buf_osdep (tmpbuf + n, caplen - sizeof(uint32_t))))
1621 { 1646 {
1622 /* NOTE: this heuristic can of course fail if there happens to 1647 /* NOTE: this heuristic can of course fail if there happens to
1623 be a matching checksum at the end. Would be good to have 1648 be a matching checksum at the end. Would be good to have
1624 some data to see how often this heuristic actually works. */ 1649 some data to see how often this heuristic actually works. */
1625 caplen -= sizeof(uint32_t); 1650 caplen -= sizeof(uint32_t);
1626 } 1651 }
1627 /* copy payload to target buffer */ 1652 /* copy payload to target buffer */
1628 GNUNET_memcpy(buf, tmpbuf + n, caplen); 1653 GNUNET_memcpy (buf, tmpbuf + n, caplen);
1629 return caplen; 1654 return caplen;
1630} 1655}
1631 1656
@@ -1642,7 +1667,7 @@ linux_read(struct HardwareInfos *dev,
1642 * @return 0 on success 1667 * @return 0 on success
1643 */ 1668 */
1644static int 1669static int
1645open_device_raw(struct HardwareInfos *dev) 1670open_device_raw (struct HardwareInfos *dev)
1646{ 1671{
1647 struct ifreq ifr; 1672 struct ifreq ifr;
1648 struct iwreq wrq; 1673 struct iwreq wrq;
@@ -1650,112 +1675,114 @@ open_device_raw(struct HardwareInfos *dev)
1650 struct sockaddr_ll sll; 1675 struct sockaddr_ll sll;
1651 1676
1652 /* find the interface index */ 1677 /* find the interface index */
1653 memset(&ifr, 0, sizeof(ifr)); 1678 memset (&ifr, 0, sizeof(ifr));
1654 strncpy(ifr.ifr_name, dev->iface, IFNAMSIZ); 1679 strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ);
1655 if (-1 == ioctl(dev->fd_raw, SIOCGIFINDEX, &ifr)) 1680 if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr))
1656 { 1681 {
1657 fprintf(stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n", 1682 fprintf (stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n",
1658 IFNAMSIZ, dev->iface, strerror(errno)); 1683 IFNAMSIZ, dev->iface, strerror (errno));
1659 return 1; 1684 return 1;
1660 } 1685 }
1661 1686
1662 /* lookup the hardware type */ 1687 /* lookup the hardware type */
1663 memset(&sll, 0, sizeof(sll)); 1688 memset (&sll, 0, sizeof(sll));
1664 sll.sll_family = AF_PACKET; 1689 sll.sll_family = AF_PACKET;
1665 sll.sll_ifindex = ifr.ifr_ifindex; 1690 sll.sll_ifindex = ifr.ifr_ifindex;
1666 sll.sll_protocol = htons(ETH_P_ALL); 1691 sll.sll_protocol = htons (ETH_P_ALL);
1667 if (-1 == ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) 1692 if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
1668 { 1693 {
1669 fprintf(stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", 1694 fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
1670 IFNAMSIZ, dev->iface, strerror(errno)); 1695 IFNAMSIZ, dev->iface, strerror (errno));
1671 return 1; 1696 return 1;
1672 } 1697 }
1673 if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && 1698 if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
1674 (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && 1699 (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
1675 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && 1700 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
1676 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))) 1701 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)))
1677 { 1702 {
1678 fprintf(stderr, "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n", 1703 fprintf (stderr,
1679 IFNAMSIZ, dev->iface, 1704 "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n",
1680 ifr.ifr_hwaddr.sa_family); 1705 IFNAMSIZ, dev->iface,
1681 return 1; 1706 ifr.ifr_hwaddr.sa_family);
1682 } 1707 return 1;
1708 }
1683 1709
1684 /* lookup iw mode */ 1710 /* lookup iw mode */
1685 memset(&wrq, 0, sizeof(struct iwreq)); 1711 memset (&wrq, 0, sizeof(struct iwreq));
1686 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ); 1712 strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
1687 if (-1 == ioctl(dev->fd_raw, SIOCGIWMODE, &wrq)) 1713 if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq))
1688 { 1714 {
1689 /* most probably not supported (ie for rtap ipw interface) * 1715 /* most probably not supported (ie for rtap ipw interface) *
1690 * so just assume its correctly set... */ 1716 * so just assume its correctly set... */
1691 wrq.u.mode = IW_MODE_MONITOR; 1717 wrq.u.mode = IW_MODE_MONITOR;
1692 } 1718 }
1693 1719
1694 if ((wrq.u.mode != IW_MODE_MONITOR) && 1720 if ((wrq.u.mode != IW_MODE_MONITOR) &&
1695 (wrq.u.mode != IW_MODE_ADHOC)) 1721 (wrq.u.mode != IW_MODE_ADHOC))
1696 { 1722 {
1697 fprintf(stderr, "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n", 1723 fprintf (stderr,
1698 IFNAMSIZ, dev->iface, 1724 "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n",
1699 wrq.u.mode); 1725 IFNAMSIZ, dev->iface,
1700 return 1; 1726 wrq.u.mode);
1701 } 1727 return 1;
1728 }
1702 1729
1703 /* Is interface st to up, broadcast & running ? */ 1730 /* Is interface st to up, broadcast & running ? */
1704 if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags) 1731 if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
1705 { 1732 {
1706 /* Bring interface up */ 1733 /* Bring interface up */
1707 ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING; 1734 ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
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 }
1716 1735
1717 /* bind the raw socket to the interface */ 1736 if (-1 == ioctl (dev->fd_raw, SIOCSIFFLAGS, &ifr))
1718 if (-1 == bind(dev->fd_raw, (struct sockaddr *)&sll, sizeof(sll)))
1719 { 1737 {
1720 fprintf(stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ, 1738 fprintf (stderr, "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n",
1721 dev->iface, strerror(errno)); 1739 IFNAMSIZ, dev->iface, strerror (errno));
1722 return 1; 1740 return 1;
1723 } 1741 }
1742 }
1743
1744 /* bind the raw socket to the interface */
1745 if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof(sll)))
1746 {
1747 fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
1748 dev->iface, strerror (errno));
1749 return 1;
1750 }
1724 1751
1725 /* lookup the hardware type */ 1752 /* lookup the hardware type */
1726 if (-1 == ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) 1753 if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
1727 { 1754 {
1728 fprintf(stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", 1755 fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
1729 IFNAMSIZ, dev->iface, strerror(errno)); 1756 IFNAMSIZ, dev->iface, strerror (errno));
1730 return 1; 1757 return 1;
1731 } 1758 }
1732 1759
1733 GNUNET_memcpy(&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE); 1760 GNUNET_memcpy (&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE);
1734 dev->arptype_in = ifr.ifr_hwaddr.sa_family; 1761 dev->arptype_in = ifr.ifr_hwaddr.sa_family;
1735 if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) && 1762 if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
1736 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) && 1763 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
1737 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) && 1764 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
1738 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) 1765 (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
1739 { 1766 {
1740 fprintf(stderr, "Unsupported hardware link type %d on interface `%.*s'\n", 1767 fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
1741 ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface); 1768 ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
1742 return 1; 1769 return 1;
1743 } 1770 }
1744 1771
1745 /* enable promiscuous mode */ 1772 /* enable promiscuous mode */
1746 memset(&mr, 0, sizeof(mr)); 1773 memset (&mr, 0, sizeof(mr));
1747 mr.mr_ifindex = sll.sll_ifindex; 1774 mr.mr_ifindex = sll.sll_ifindex;
1748 mr.mr_type = PACKET_MR_PROMISC; 1775 mr.mr_type = PACKET_MR_PROMISC;
1749 if (0 != 1776 if (0 !=
1750 setsockopt(dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, 1777 setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
1751 sizeof(mr))) 1778 sizeof(mr)))
1752 { 1779 {
1753 fprintf(stderr, 1780 fprintf (stderr,
1754 "Failed to enable promiscuous mode on interface `%.*s'\n", 1781 "Failed to enable promiscuous mode on interface `%.*s'\n",
1755 IFNAMSIZ, 1782 IFNAMSIZ,
1756 dev->iface); 1783 dev->iface);
1757 return 1; 1784 return 1;
1758 } 1785 }
1759 return 0; 1786 return 0;
1760} 1787}
1761 1788
@@ -1768,22 +1795,22 @@ open_device_raw(struct HardwareInfos *dev)
1768 * @return 0 on success, 1 on error 1795 * @return 0 on success, 1 on error
1769 */ 1796 */
1770static int 1797static int
1771test_wlan_interface(const char *iface) 1798test_wlan_interface (const char *iface)
1772{ 1799{
1773 char strbuf[512]; 1800 char strbuf[512];
1774 struct stat sbuf; 1801 struct stat sbuf;
1775 int ret; 1802 int ret;
1776 1803
1777 ret = snprintf(strbuf, sizeof(strbuf), 1804 ret = snprintf (strbuf, sizeof(strbuf),
1778 "/sys/class/net/%s/phy80211/subsystem", 1805 "/sys/class/net/%s/phy80211/subsystem",
1779 iface); 1806 iface);
1780 if ((ret < 0) || (ret >= sizeof(strbuf)) || (0 != stat(strbuf, &sbuf))) 1807 if ((ret < 0) || (ret >= sizeof(strbuf)) || (0 != stat (strbuf, &sbuf)))
1781 { 1808 {
1782 fprintf(stderr, 1809 fprintf (stderr,
1783 "Did not find 802.11 interface `%s'. Exiting.\n", 1810 "Did not find 802.11 interface `%s'. Exiting.\n",
1784 iface); 1811 iface);
1785 exit(1); 1812 exit (1);
1786 } 1813 }
1787 return 0; 1814 return 0;
1788} 1815}
1789 1816
@@ -1796,19 +1823,19 @@ test_wlan_interface(const char *iface)
1796 * @return 0 if mac belongs to us, 1 if mac is for another target 1823 * @return 0 if mac belongs to us, 1 if mac is for another target
1797 */ 1824 */
1798static int 1825static int
1799mac_test(const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, 1826mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1800 const struct HardwareInfos *dev) 1827 const struct HardwareInfos *dev)
1801{ 1828{
1802 static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros; 1829 static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros;
1803 1830
1804 if ((0 == memcmp(&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) || 1831 if ((0 == memcmp (&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) ||
1805 (0 == memcmp(&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE))) 1832 (0 == memcmp (&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE)))
1806 return 0; /* some drivers set no Macs, then assume it is all for us! */ 1833 return 0; /* some drivers set no Macs, then assume it is all for us! */
1807 1834
1808 if (0 != memcmp(&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE)) 1835 if (0 != memcmp (&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE))
1809 return 1; /* not a GNUnet ad-hoc package */ 1836 return 1; /* not a GNUnet ad-hoc package */
1810 if ((0 == memcmp(&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) || 1837 if ((0 == memcmp (&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) ||
1811 (0 == memcmp(&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE))) 1838 (0 == memcmp (&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE)))
1812 return 0; /* for us, or broadcast */ 1839 return 0; /* for us, or broadcast */
1813 return 1; /* not for us */ 1840 return 1; /* not for us */
1814} 1841}
@@ -1821,10 +1848,10 @@ mac_test(const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1821 * @param dev pointer to the Hardware_Infos struct 1848 * @param dev pointer to the Hardware_Infos struct
1822 */ 1849 */
1823static void 1850static void
1824mac_set(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, 1851mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1825 const struct HardwareInfos *dev) 1852 const struct HardwareInfos *dev)
1826{ 1853{
1827 taIeeeHeader->frame_control = htons(IEEE80211_FC0_TYPE_DATA); 1854 taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1828 taIeeeHeader->addr2 = dev->pl_mac; 1855 taIeeeHeader->addr2 = dev->pl_mac;
1829 taIeeeHeader->addr3 = mac_bssid_gnunet; 1856 taIeeeHeader->addr3 = mac_bssid_gnunet;
1830} 1857}
@@ -1839,7 +1866,7 @@ mac_set(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1839 * @param hdr pointer to the start of the packet 1866 * @param hdr pointer to the start of the packet
1840 */ 1867 */
1841static void 1868static void
1842stdin_send_hw(void *cls, const struct GNUNET_MessageHeader *hdr) 1869stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr)
1843{ 1870{
1844 struct HardwareInfos *dev = cls; 1871 struct HardwareInfos *dev = cls;
1845 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header; 1872 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header;
@@ -1848,58 +1875,67 @@ stdin_send_hw(void *cls, const struct GNUNET_MessageHeader *hdr)
1848 struct RadiotapTransmissionHeader rtheader; 1875 struct RadiotapTransmissionHeader rtheader;
1849 struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader; 1876 struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader;
1850 1877
1851 sendsize = ntohs(hdr->size); 1878 sendsize = ntohs (hdr->size);
1852 if ((sendsize < 1879 if ((sendsize <
1853 sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) || 1880 sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) ||
1854 (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs(hdr->type))) 1881 (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)))
1855 { 1882 {
1856 fprintf(stderr, "Received malformed message\n"); 1883 fprintf (stderr, "Received malformed message\n");
1857 exit(1); 1884 exit (1);
1858 } 1885 }
1859 sendsize -= (sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); 1886 sendsize -= (sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)
1887 - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
1860 if (MAXLINE < sendsize) 1888 if (MAXLINE < sendsize)
1861 { 1889 {
1862 fprintf(stderr, "Packet too big for buffer\n"); 1890 fprintf (stderr, "Packet too big for buffer\n");
1863 exit(1); 1891 exit (1);
1864 } 1892 }
1865 header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *)hdr; 1893 header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
1866 switch (dev->arptype_in) 1894 switch (dev->arptype_in)
1867 { 1895 {
1868 case ARPHRD_IEEE80211_PRISM: 1896 case ARPHRD_IEEE80211_PRISM:
1869 case ARPHRD_IEEE80211_FULL: 1897 case ARPHRD_IEEE80211_FULL:
1870 case ARPHRD_IEEE80211: 1898 case ARPHRD_IEEE80211:
1871 rtheader.header.it_version = 0; 1899 rtheader.header.it_version = 0;
1872 rtheader.header.it_pad = 0; 1900 rtheader.header.it_pad = 0;
1873 rtheader.header.it_len = GNUNET_htole16(sizeof(rtheader)); 1901 rtheader.header.it_len = GNUNET_htole16 (sizeof(rtheader));
1874 rtheader.header.it_present = GNUNET_htole16(IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK); 1902 rtheader.header.it_present = GNUNET_htole16 (
1875 rtheader.rate = header->rate; 1903 IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK);
1876 rtheader.pad1 = 0; 1904 rtheader.rate = header->rate;
1877 rtheader.txflags = GNUNET_htole16(IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ); 1905 rtheader.pad1 = 0;
1878 GNUNET_memcpy(write_pout.buf, &rtheader, sizeof(rtheader)); 1906 rtheader.txflags = GNUNET_htole16 (IEEE80211_RADIOTAP_F_TX_NOACK
1879 GNUNET_memcpy(&write_pout.buf[sizeof(rtheader)], &header->frame, sendsize); 1907 | IEEE80211_RADIOTAP_F_TX_NOSEQ);
1880 wlanheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *)&write_pout.buf[sizeof(rtheader)]; 1908 GNUNET_memcpy (write_pout.buf, &rtheader, sizeof(rtheader));
1881 1909 GNUNET_memcpy (&write_pout.buf[sizeof(rtheader)], &header->frame, sendsize);
1882 /* payload contains MAC address, but we don't trust it, so we'll 1910 wlanheader = (struct
1883 * overwrite it with OUR MAC address to prevent mischief */ 1911 GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) &write_pout.buf[sizeof(
1884 mac_set(wlanheader, dev); 1912 rtheader)
1885 write_pout.size = sendsize + sizeof(rtheader); 1913 ];
1886 break; 1914
1915 /* payload contains MAC address, but we don't trust it, so we'll
1916 * overwrite it with OUR MAC address to prevent mischief */
1917 mac_set (wlanheader, dev);
1918 write_pout.size = sendsize + sizeof(rtheader);
1919 break;
1887 1920
1888 case ARPHRD_ETHER: 1921 case ARPHRD_ETHER:
1889 etheader.dst = header->frame.addr1; 1922 etheader.dst = header->frame.addr1;
1890 /* etheader.src = header->frame.addr2; --- untrusted input */ 1923 /* etheader.src = header->frame.addr2; --- untrusted input */
1891 etheader.src = dev->pl_mac; 1924 etheader.src = dev->pl_mac;
1892 etheader.type = htons(ETH_P_IP); 1925 etheader.type = htons (ETH_P_IP);
1893 GNUNET_memcpy(write_pout.buf, &etheader, sizeof(etheader)); 1926 GNUNET_memcpy (write_pout.buf, &etheader, sizeof(etheader));
1894 GNUNET_memcpy(&write_pout.buf[sizeof(etheader)], &header[1], sendsize - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)); 1927 GNUNET_memcpy (&write_pout.buf[sizeof(etheader)], &header[1], sendsize
1895 write_pout.size = sendsize - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) + sizeof(etheader); 1928 - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
1896 break; 1929 write_pout.size = sendsize - sizeof(struct
1930 GNUNET_TRANSPORT_WLAN_Ieee80211Frame)
1931 + sizeof(etheader);
1932 break;
1897 1933
1898 default: 1934 default:
1899 fprintf(stderr, 1935 fprintf (stderr,
1900 "Unsupported ARPTYPE!\n"); 1936 "Unsupported ARPTYPE!\n");
1901 break; 1937 break;
1902 } 1938 }
1903} 1939}
1904 1940
1905 1941
@@ -1914,7 +1950,7 @@ stdin_send_hw(void *cls, const struct GNUNET_MessageHeader *hdr)
1914 * @return 0 on success (never happens, as we don't return unless aborted), 1 on error 1950 * @return 0 on success (never happens, as we don't return unless aborted), 1 on error
1915 */ 1951 */
1916int 1952int
1917main(int argc, char *argv[]) 1953main (int argc, char *argv[])
1918{ 1954{
1919 struct HardwareInfos dev; 1955 struct HardwareInfos dev;
1920 char readbuf[MAXLINE]; 1956 char readbuf[MAXLINE];
@@ -1928,83 +1964,83 @@ main(int argc, char *argv[])
1928 /* assert privs so we can modify the firewall rules! */ 1964 /* assert privs so we can modify the firewall rules! */
1929 { 1965 {
1930#ifdef HAVE_SETRESUID 1966#ifdef HAVE_SETRESUID
1931 uid_t uid = getuid(); 1967 uid_t uid = getuid ();
1932 1968
1933 if (0 != setresuid(uid, 0, 0)) 1969 if (0 != setresuid (uid, 0, 0))
1934 { 1970 {
1935 fprintf(stderr, 1971 fprintf (stderr,
1936 "Failed to setresuid to root: %s\n", 1972 "Failed to setresuid to root: %s\n",
1937 strerror(errno)); 1973 strerror (errno));
1938 return 254; 1974 return 254;
1939 } 1975 }
1940#else 1976#else
1941 if (0 != seteuid(0)) 1977 if (0 != seteuid (0))
1942 { 1978 {
1943 fprintf(stderr, 1979 fprintf (stderr,
1944 "Failed to seteuid back to root: %s\n", strerror(errno)); 1980 "Failed to seteuid back to root: %s\n", strerror (errno));
1945 return 254; 1981 return 254;
1946 } 1982 }
1947#endif 1983#endif
1948 } 1984 }
1949 1985
1950 /* make use of SGID capabilities on POSIX */ 1986 /* make use of SGID capabilities on POSIX */
1951 memset(&dev, 0, sizeof(dev)); 1987 memset (&dev, 0, sizeof(dev));
1952 dev.fd_raw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 1988 dev.fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
1953 raw_eno = errno; /* remember for later */ 1989 raw_eno = errno; /* remember for later */
1954 1990
1955 /* now that we've dropped root rights, we can do error checking */ 1991 /* now that we've dropped root rights, we can do error checking */
1956 if (2 != argc) 1992 if (2 != argc)
1957 { 1993 {
1958 fprintf(stderr, 1994 fprintf (stderr,
1959 "You must specify the name of the interface as the first and only argument to this program.\n"); 1995 "You must specify the name of the interface as the first and only argument to this program.\n");
1960 if (-1 != dev.fd_raw) 1996 if (-1 != dev.fd_raw)
1961 (void)close(dev.fd_raw); 1997 (void) close (dev.fd_raw);
1962 return 1; 1998 return 1;
1963 } 1999 }
1964 2000
1965 if (-1 == dev.fd_raw) 2001 if (-1 == dev.fd_raw)
1966 { 2002 {
1967 fprintf(stderr, "Failed to create raw socket: %s\n", strerror(raw_eno)); 2003 fprintf (stderr, "Failed to create raw socket: %s\n", strerror (raw_eno));
1968 return 1; 2004 return 1;
1969 } 2005 }
1970 if (dev.fd_raw >= FD_SETSIZE) 2006 if (dev.fd_raw >= FD_SETSIZE)
2007 {
2008 fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
2009 dev.fd_raw, FD_SETSIZE);
2010 (void) close (dev.fd_raw);
2011 return 1;
2012 }
2013 if (0 != test_wlan_interface (argv[1]))
2014 {
2015 (void) close (dev.fd_raw);
2016 return 1;
2017 }
2018 strncpy (dev.iface, argv[1], IFNAMSIZ);
2019 if (0 != open_device_raw (&dev))
2020 {
2021 (void) close (dev.fd_raw);
2022 return 1;
2023 }
2024
2025 /* drop privs */
2026 {
2027 uid_t uid = getuid ();
2028#ifdef HAVE_SETRESUID
2029 if (0 != setresuid (uid, uid, uid))
1971 { 2030 {
1972 fprintf(stderr, "File descriptor too large for select (%d > %d)\n", 2031 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
1973 dev.fd_raw, FD_SETSIZE); 2032 if (-1 != dev.fd_raw)
1974 (void)close(dev.fd_raw); 2033 (void) close (dev.fd_raw);
1975 return 1;
1976 }
1977 if (0 != test_wlan_interface(argv[1]))
1978 {
1979 (void)close(dev.fd_raw);
1980 return 1; 2034 return 1;
1981 } 2035 }
1982 strncpy(dev.iface, argv[1], IFNAMSIZ); 2036#else
1983 if (0 != open_device_raw(&dev)) 2037 if (0 != (setuid (uid) | seteuid (uid)))
1984 { 2038 {
1985 (void)close(dev.fd_raw); 2039 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
2040 if (-1 != dev.fd_raw)
2041 (void) close (dev.fd_raw);
1986 return 1; 2042 return 1;
1987 } 2043 }
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 }
2008#endif 2044#endif
2009 } 2045 }
2010 2046
@@ -2013,141 +2049,145 @@ main(int argc, char *argv[])
2013 { 2049 {
2014 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; 2050 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
2015 2051
2016 macmsg.hdr.size = htons(sizeof(macmsg)); 2052 macmsg.hdr.size = htons (sizeof(macmsg));
2017 macmsg.hdr.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); 2053 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
2018 GNUNET_memcpy(&macmsg.mac, &dev.pl_mac, sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress)); 2054 GNUNET_memcpy (&macmsg.mac, &dev.pl_mac, sizeof(struct
2019 GNUNET_memcpy(write_std.buf, &macmsg, sizeof(macmsg)); 2055 GNUNET_TRANSPORT_WLAN_MacAddress));
2056 GNUNET_memcpy (write_std.buf, &macmsg, sizeof(macmsg));
2020 write_std.size = sizeof(macmsg); 2057 write_std.size = sizeof(macmsg);
2021 } 2058 }
2022 2059
2023 stdin_mst = mst_create(&stdin_send_hw, &dev); 2060 stdin_mst = mst_create (&stdin_send_hw, &dev);
2024 stdin_open = 1; 2061 stdin_open = 1;
2025 while (1) 2062 while (1)
2063 {
2064 maxfd = -1;
2065 FD_ZERO (&rfds);
2066 if ((0 == write_pout.size) && (1 == stdin_open))
2026 { 2067 {
2027 maxfd = -1; 2068 FD_SET (STDIN_FILENO, &rfds);
2028 FD_ZERO(&rfds); 2069 maxfd = MAX (maxfd, STDIN_FILENO);
2029 if ((0 == write_pout.size) && (1 == stdin_open)) 2070 }
2030 { 2071 if (0 == write_std.size)
2031 FD_SET(STDIN_FILENO, &rfds); 2072 {
2032 maxfd = MAX(maxfd, STDIN_FILENO); 2073 FD_SET (dev.fd_raw, &rfds);
2033 } 2074 maxfd = MAX (maxfd, dev.fd_raw);
2034 if (0 == write_std.size) 2075 }
2035 { 2076 FD_ZERO (&wfds);
2036 FD_SET(dev.fd_raw, &rfds); 2077 if (0 < write_std.size)
2037 maxfd = MAX(maxfd, dev.fd_raw); 2078 {
2038 } 2079 FD_SET (STDOUT_FILENO, &wfds);
2039 FD_ZERO(&wfds); 2080 maxfd = MAX (maxfd, STDOUT_FILENO);
2040 if (0 < write_std.size) 2081 }
2041 { 2082 if (0 < write_pout.size)
2042 FD_SET(STDOUT_FILENO, &wfds); 2083 {
2043 maxfd = MAX(maxfd, STDOUT_FILENO); 2084 FD_SET (dev.fd_raw, &wfds);
2044 } 2085 maxfd = MAX (maxfd, dev.fd_raw);
2045 if (0 < write_pout.size) 2086 }
2046 { 2087 {
2047 FD_SET(dev.fd_raw, &wfds); 2088 int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
2048 maxfd = MAX(maxfd, dev.fd_raw); 2089 if ((-1 == retval) && (EINTR == errno))
2049 } 2090 continue;
2091 if (0 > retval)
2050 { 2092 {
2051 int retval = select(maxfd + 1, &rfds, &wfds, NULL, NULL); 2093 fprintf (stderr, "select failed: %s\n", strerror (errno));
2052 if ((-1 == retval) && (EINTR == errno)) 2094 break;
2053 continue;
2054 if (0 > retval)
2055 {
2056 fprintf(stderr, "select failed: %s\n", strerror(errno));
2057 break;
2058 }
2059 } 2095 }
2060 if (FD_ISSET(STDOUT_FILENO, &wfds)) 2096 }
2061 { 2097 if (FD_ISSET (STDOUT_FILENO, &wfds))
2062 ssize_t ret = 2098 {
2063 write(STDOUT_FILENO, write_std.buf + write_std.pos, 2099 ssize_t ret =
2064 write_std.size - write_std.pos); 2100 write (STDOUT_FILENO, write_std.buf + write_std.pos,
2065 if (0 > ret) 2101 write_std.size - write_std.pos);
2066 { 2102 if (0 > ret)
2067 fprintf(stderr, "Failed to write to STDOUT: %s\n", strerror(errno)); 2103 {
2068 break; 2104 fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
2069 } 2105 break;
2070 write_std.pos += ret; 2106 }
2071 if (write_std.pos == write_std.size) 2107 write_std.pos += ret;
2072 { 2108 if (write_std.pos == write_std.size)
2073 write_std.pos = 0; 2109 {
2074 write_std.size = 0; 2110 write_std.pos = 0;
2075 } 2111 write_std.size = 0;
2076 } 2112 }
2077 if (FD_ISSET(dev.fd_raw, &wfds)) 2113 }
2078 { 2114 if (FD_ISSET (dev.fd_raw, &wfds))
2079 ssize_t ret = 2115 {
2080 write(dev.fd_raw, write_pout.buf + write_pout.pos, 2116 ssize_t ret =
2081 write_pout.size - write_pout.pos); 2117 write (dev.fd_raw, write_pout.buf + write_pout.pos,
2082 if (0 > ret) 2118 write_pout.size - write_pout.pos);
2083 { 2119 if (0 > ret)
2084 fprintf(stderr, "Failed to write to WLAN device: %s\n", 2120 {
2085 strerror(errno)); 2121 fprintf (stderr, "Failed to write to WLAN device: %s\n",
2086 break; 2122 strerror (errno));
2087 } 2123 break;
2088 write_pout.pos += ret; 2124 }
2089 if ((write_pout.pos != write_pout.size) && (0 != ret)) 2125 write_pout.pos += ret;
2090 { 2126 if ((write_pout.pos != write_pout.size) && (0 != ret))
2091 /* we should not get partial sends with packet-oriented devices... */ 2127 {
2092 fprintf(stderr, "Write error, partial send: %u/%u\n", 2128 /* we should not get partial sends with packet-oriented devices... */
2093 (unsigned int)write_pout.pos, 2129 fprintf (stderr, "Write error, partial send: %u/%u\n",
2094 (unsigned int)write_pout.size); 2130 (unsigned int) write_pout.pos,
2095 break; 2131 (unsigned int) write_pout.size);
2096 } 2132 break;
2097 if (write_pout.pos == write_pout.size) 2133 }
2098 { 2134 if (write_pout.pos == write_pout.size)
2099 write_pout.pos = 0; 2135 {
2100 write_pout.size = 0; 2136 write_pout.pos = 0;
2101 } 2137 write_pout.size = 0;
2102 } 2138 }
2139 }
2103 2140
2104 if (FD_ISSET(STDIN_FILENO, &rfds)) 2141 if (FD_ISSET (STDIN_FILENO, &rfds))
2105 { 2142 {
2106 ssize_t ret = 2143 ssize_t ret =
2107 read(STDIN_FILENO, readbuf, sizeof(readbuf)); 2144 read (STDIN_FILENO, readbuf, sizeof(readbuf));
2108 if (0 > ret) 2145 if (0 > ret)
2109 { 2146 {
2110 fprintf(stderr, "Read error from STDIN: %s\n", strerror(errno)); 2147 fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
2111 break; 2148 break;
2112 } 2149 }
2113 if (0 == ret) 2150 if (0 == ret)
2114 { 2151 {
2115 /* stop reading... */ 2152 /* stop reading... */
2116 stdin_open = 0; 2153 stdin_open = 0;
2117 } 2154 }
2118 mst_receive(stdin_mst, readbuf, ret); 2155 mst_receive (stdin_mst, readbuf, ret);
2119 } 2156 }
2120 2157
2121 if (FD_ISSET(dev.fd_raw, &rfds)) 2158 if (FD_ISSET (dev.fd_raw, &rfds))
2122 { 2159 {
2123 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm; 2160 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
2124 ssize_t ret; 2161 ssize_t ret;
2125 2162
2126 rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *)write_std.buf; 2163 rrm = (struct
2127 ret = 2164 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
2128 linux_read(&dev, (unsigned char *)&rrm->frame, 2165 ret =
2129 sizeof(write_std.buf) 2166 linux_read (&dev, (unsigned char *) &rrm->frame,
2130 - sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) 2167 sizeof(write_std.buf)
2131 + sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), 2168 - sizeof(struct
2132 rrm); 2169 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
2133 if (0 > ret) 2170 + sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
2134 { 2171 rrm);
2135 fprintf(stderr, "Read error from raw socket: %s\n", strerror(errno)); 2172 if (0 > ret)
2136 break; 2173 {
2137 } 2174 fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
2138 if ((0 < ret) && (0 == mac_test(&rrm->frame, &dev))) 2175 break;
2139 { 2176 }
2140 write_std.size = ret 2177 if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
2141 + sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) 2178 {
2142 - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); 2179 write_std.size = ret
2143 rrm->header.size = htons(write_std.size); 2180 + sizeof(struct
2144 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); 2181 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
2145 } 2182 - sizeof(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
2146 } 2183 rrm->header.size = htons (write_std.size);
2184 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
2185 }
2147 } 2186 }
2187 }
2148 /* Error handling, try to clean up a bit at least */ 2188 /* Error handling, try to clean up a bit at least */
2149 mst_destroy(stdin_mst); 2189 mst_destroy (stdin_mst);
2150 (void)close(dev.fd_raw); 2190 (void) close (dev.fd_raw);
2151 return 1; /* we never exit 'normally' */ 2191 return 1; /* we never exit 'normally' */
2152} 2192}
2153 2193