fix chillispot endianness bugs (thx, SeG), fixes #2404
[openwrt/svn-archive/archive.git] / net / chillispot / patches / 001-endian_fix.patch
1 Index: chillispot-1.1.0/src/dhcp.c
2 ===================================================================
3 --- chillispot-1.1.0.orig/src/dhcp.c 2006-09-24 19:48:25.000000000 +0200
4 +++ chillispot-1.1.0/src/dhcp.c 2007-09-25 15:35:12.521055038 +0200
5 @@ -116,6 +116,24 @@
6 #endif
7
8
9 +/*
10 + * BrainSlayer:
11 + * wrapper for fixing the big endian bugs within dhcp server code.its surelly not the best.
12 + * all dhcp packet fields must be handled in little endian
13 + */
14 +
15 +static uint16_t swap16(uint16_t word) {
16 +#if __BYTE_ORDER == __BIG_ENDIAN
17 + unsigned char low = word>>8;
18 + unsigned char high = word&0xff;
19 + return ((uint16_t)(high<<8))|low;
20 +#elif __BYTE_ORDER == __LITTLE_ENDIAN
21 + return word;
22 +#else
23 +#error "Could not determine the system's endianness"
24 +#endif
25 +}
26 +
27 /**
28 * dhcp_ip_check()
29 * Generates an IPv4 header checksum.
30 @@ -125,11 +143,11 @@
31 uint32_t sum = 0;
32 pack->iph.check = 0;
33 for (i=0; i<(pack->iph.ihl * 2); i++) {
34 - sum += ((uint16_t*) &pack->iph)[i];
35 + sum += swap16(((uint16_t*) &pack->iph)[i]); /* brainslayer */
36 }
37 while (sum>>16)
38 sum = (sum & 0xFFFF)+(sum >> 16);
39 - pack->iph.check = ~sum;
40 + pack->iph.check = swap16(~sum); /* brainslayer */
41 return 0;
42 }
43
44 @@ -152,27 +170,28 @@
45 }
46
47 /* Sum UDP header and payload */
48 +
49 for (i=0; i<(udp_len/2); i++) {
50 - sum += ((uint16_t*) &pack->udph)[i];
51 + sum += swap16(((uint16_t*) &pack->udph)[i]); /* brainslayer */
52 }
53
54 - /* Sum any uneven payload octet */
55 +
56 if (udp_len & 0x01) {
57 sum += ((uint8_t*) &pack->udph)[udp_len-1];
58 }
59
60 /* Sum both source and destination address */
61 for (i=0; i<4; i++) {
62 - sum += ((uint16_t*) &pack->iph.saddr)[i];
63 + sum += swap16(((uint16_t*) &pack->iph.saddr)[i]); /* brainslayer */
64 }
65
66 /* Sum both protocol and udp_len (again) */
67 - sum = sum + pack->udph.len + ((pack->iph.protocol<<8)&0xFF00);
68 + sum = sum + swap16(pack->udph.len) + ((pack->iph.protocol<<8)&0xFF00); /* brainslayer */
69
70 while (sum>>16)
71 sum = (sum & 0xFFFF)+(sum >> 16);
72
73 - pack->udph.check = ~sum;
74 + pack->udph.check = swap16(~sum); /* brainslayer */
75
76 return 0;
77 }
78 @@ -201,7 +220,7 @@
79
80 /* Sum TCP header and payload */
81 for (i=0; i<(tcp_len/2); i++) {
82 - sum += ((uint16_t*) pack->payload)[i];
83 + sum += swap16(((uint16_t*) pack->payload)[i]); /* brainslayer */
84 }
85
86 /* Sum any uneven payload octet */
87 @@ -211,16 +230,16 @@
88
89 /* Sum both source and destination address */
90 for (i=0; i<4; i++) {
91 - sum += ((uint16_t*) &pack->iph.saddr)[i];
92 + sum += swap16(((uint16_t*) &pack->iph.saddr)[i]); /* brainslayer */
93 }
94
95 /* Sum both protocol and tcp_len */
96 - sum = sum + htons(tcp_len) + ((pack->iph.protocol<<8)&0xFF00);
97 + sum = sum + swap16(htons(tcp_len)) + ((pack->iph.protocol<<8)&0xFF00); /* brainslayer */
98
99 while (sum>>16)
100 sum = (sum & 0xFFFF)+(sum >> 16);
101
102 - tcph->check = ~sum;
103 + tcph->check = swap16(~sum); /* brainslayer */
104
105 return 0;
106 }
107 Index: chillispot-1.1.0/src/dhcp.h
108 ===================================================================
109 --- chillispot-1.1.0.orig/src/dhcp.h 2006-09-24 19:48:25.000000000 +0200
110 +++ chillispot-1.1.0/src/dhcp.h 2007-09-25 15:35:12.533055724 +0200
111 @@ -119,6 +119,8 @@
112 uint16_t prot;
113 };
114
115 +#include <endian.h>
116 +
117 /* Constants for IP packet */
118 #define DHCP_IP_ALEN 4
119 #define DHCP_IP_HLEN 20
120 @@ -127,8 +129,15 @@
121 #define DHCP_IP_UDP 17 /* UDP Protocol number */
122
123 struct dhcp_iphdr_t {
124 +#if __BYTE_ORDER == __LITTLE_ENDIAN /* nbd fix for swapped version and length field */
125 uint8_t ihl:4;
126 uint8_t version:4;
127 +#elif __BYTE_ORDER == __BIG_ENDIAN
128 + uint8_t version:4;
129 + uint8_t ihl:4;
130 +#else
131 +#error "Could not determine the system's endianness"
132 +#endif
133 uint8_t tos;
134 uint16_t tot_len;
135 uint16_t id;