diff -urN chilli-old/src/dhcp.c chillispot-1.0/src/dhcp.c --- chilli-old/src/dhcp.c 2006-06-30 01:52:28.000000000 +0200 +++ chillispot-1.0/src/dhcp.c 2006-06-30 01:55:10.092520000 +0200 @@ -84,6 +84,24 @@ #endif +/* + * BrainSlayer: + * wrapper for fixing the big endian bugs within dhcp server code.its surelly not the best. + * all dhcp packet fields must be handled in little endian + */ + +static uint16_t swap16(uint16_t word) { +#if __BYTE_ORDER == __BIG_ENDIAN + unsigned char low = word>>8; + unsigned char high = word&0xff; + return ((uint16_t)(high<<8))|low; +#elif __BYTE_ORDER == __LITTLE_ENDIAN + return word; +#else +#error "Could not determine the system's endianness" +#endif +} + /** * dhcp_ip_check() * Generates an IPv4 header checksum. @@ -93,11 +111,11 @@ uint32_t sum = 0; pack->iph.check = 0; for (i=0; i<(pack->iph.ihl * 2); i++) { - sum += ((uint16_t*) &pack->iph)[i]; + sum += swap16(((uint16_t*) &pack->iph)[i]); /* brainslayer */ } while (sum>>16) sum = (sum & 0xFFFF)+(sum >> 16); - pack->iph.check = ~sum; + pack->iph.check = swap16(~sum); /* brainslayer */ return 0; } @@ -120,27 +138,28 @@ } /* Sum UDP header and payload */ + for (i=0; i<(udp_len/2); i++) { - sum += ((uint16_t*) &pack->udph)[i]; + sum += swap16(((uint16_t*) &pack->udph)[i]); /* brainslayer */ } - /* Sum any uneven payload octet */ + if (udp_len & 0x01) { sum += ((uint8_t*) &pack->udph)[udp_len-1]; } /* Sum both source and destination address */ for (i=0; i<4; i++) { - sum += ((uint16_t*) &pack->iph.saddr)[i]; + sum += swap16(((uint16_t*) &pack->iph.saddr)[i]); /* brainslayer */ } /* Sum both protocol and udp_len (again) */ - sum = sum + pack->udph.len + ((pack->iph.protocol<<8)&0xFF00); + sum = sum + swap16(pack->udph.len) + ((pack->iph.protocol<<8)&0xFF00); /* brainslayer */ while (sum>>16) sum = (sum & 0xFFFF)+(sum >> 16); - pack->udph.check = ~sum; + pack->udph.check = swap16(~sum); /* brainslayer */ return 0; } @@ -169,7 +188,7 @@ /* Sum TCP header and payload */ for (i=0; i<(tcp_len/2); i++) { - sum += ((uint16_t*) pack->payload)[i]; + sum += swap16(((uint16_t*) pack->payload)[i]); /* brainslayer */ } /* Sum any uneven payload octet */ @@ -179,16 +198,16 @@ /* Sum both source and destination address */ for (i=0; i<4; i++) { - sum += ((uint16_t*) &pack->iph.saddr)[i]; + sum += swap16(((uint16_t*) &pack->iph.saddr)[i]); /* brainslayer */ } /* Sum both protocol and tcp_len */ - sum = sum + htons(tcp_len) + ((pack->iph.protocol<<8)&0xFF00); + sum = sum + swap16(htons(tcp_len)) + ((pack->iph.protocol<<8)&0xFF00); /* brainslayer */ while (sum>>16) sum = (sum & 0xFFFF)+(sum >> 16); - tcph->check = ~sum; + tcph->check = swap16(~sum); /* brainslayer */ return 0; } diff -urN chilli-old/src/dhcp.h chillispot-1.0/src/dhcp.h --- chilli-old/src/dhcp.h 2005-09-19 23:49:28.000000000 +0200 +++ chillispot-1.0/src/dhcp.h 2006-06-30 01:54:04.080394500 +0200 @@ -87,6 +87,8 @@ uint16_t prot; }; +#include + /* Constants for IP packet */ #define DHCP_IP_ALEN 4 #define DHCP_IP_HLEN 20 @@ -94,8 +96,15 @@ #define DHCP_IP_TCP 6 /* TCP Protocol number */ struct dhcp_iphdr_t { +#if __BYTE_ORDER == __LITTLE_ENDIAN /* nbd fix for swapped version and length field */ uint8_t ihl:4; uint8_t version:4; +#elif __BYTE_ORDER == __BIG_ENDIAN + uint8_t version:4; + uint8_t ihl:4; +#else +#error "Could not determine the system's endianness" +#endif uint8_t tos; uint16_t tot_len; uint16_t id;