diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-23 00:08:54 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-23 00:08:54 +0000 |
commit | 6884f665bd7bc101f56ff9047afaffbc06dc99e2 (patch) | |
tree | 9eb497068cdee02c112c2f55bd0def8ae6354e92 | |
parent | 68af8e7a084317191effa9b257483a50d994b11c (diff) | |
download | busybox-w32-6884f665bd7bc101f56ff9047afaffbc06dc99e2.tar.gz busybox-w32-6884f665bd7bc101f56ff9047afaffbc06dc99e2.tar.bz2 busybox-w32-6884f665bd7bc101f56ff9047afaffbc06dc99e2.zip |
dhcp: stop using magic constants; use (htonl(CONST) != a) - it's smaller
function old new delta
udhcp_get_packet 146 134 -12
get_raw_packet 368 353 -15
-rw-r--r-- | networking/udhcp/clientpacket.c | 8 | ||||
-rw-r--r-- | networking/udhcp/common.h | 8 | ||||
-rw-r--r-- | networking/udhcp/options.c | 18 | ||||
-rw-r--r-- | networking/udhcp/options.h | 2 | ||||
-rw-r--r-- | networking/udhcp/packet.c | 6 |
5 files changed, 23 insertions, 19 deletions
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c index e7eeb58b1..4a5c627f4 100644 --- a/networking/udhcp/clientpacket.c +++ b/networking/udhcp/clientpacket.c | |||
@@ -171,7 +171,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |||
171 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); | 171 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); |
172 | if (bytes < 0) { | 172 | if (bytes < 0) { |
173 | DEBUG("Cannot read on raw listening socket - ignoring"); | 173 | DEBUG("Cannot read on raw listening socket - ignoring"); |
174 | usleep(500000); /* possible down interface, looping condition */ | 174 | sleep(1); /* possible down interface, looping condition */ |
175 | return -1; | 175 | return -1; |
176 | } | 176 | } |
177 | 177 | ||
@@ -190,7 +190,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |||
190 | 190 | ||
191 | /* Make sure its the right packet for us, and that it passes sanity checks */ | 191 | /* Make sure its the right packet for us, and that it passes sanity checks */ |
192 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION | 192 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION |
193 | || packet.ip.ihl != sizeof(packet.ip) >> 2 | 193 | || packet.ip.ihl != (sizeof(packet.ip) >> 2) |
194 | || packet.udp.dest != htons(CLIENT_PORT) | 194 | || packet.udp.dest != htons(CLIENT_PORT) |
195 | || bytes > (int) sizeof(struct udp_dhcp_packet) | 195 | || bytes > (int) sizeof(struct udp_dhcp_packet) |
196 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) | 196 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) |
@@ -207,7 +207,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |||
207 | return -1; | 207 | return -1; |
208 | } | 208 | } |
209 | 209 | ||
210 | /* verify the UDP checksum by replacing the header with a psuedo header */ | 210 | /* verify the UDP checksum by replacing the header with a pseudo header */ |
211 | source = packet.ip.saddr; | 211 | source = packet.ip.saddr; |
212 | dest = packet.ip.daddr; | 212 | dest = packet.ip.daddr; |
213 | check = packet.udp.check; | 213 | check = packet.udp.check; |
@@ -225,7 +225,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |||
225 | 225 | ||
226 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); | 226 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
227 | 227 | ||
228 | if (ntohl(payload->cookie) != DHCP_MAGIC) { | 228 | if (payload->cookie != htonl(DHCP_MAGIC)) { |
229 | bb_error_msg("received bogus message (bad magic) - ignoring"); | 229 | bb_error_msg("received bogus message (bad magic) - ignoring"); |
230 | return -2; | 230 | return -2; |
231 | } | 231 | } |
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 179c21fdb..4f2d31c98 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h | |||
@@ -38,12 +38,18 @@ struct dhcpMessage { | |||
38 | uint8_t file[128]; | 38 | uint8_t file[128]; |
39 | uint32_t cookie; | 39 | uint32_t cookie; |
40 | uint8_t options[308]; /* 312 - cookie */ | 40 | uint8_t options[308]; /* 312 - cookie */ |
41 | }; | 41 | } ATTRIBUTE_PACKED; |
42 | 42 | ||
43 | struct udp_dhcp_packet { | 43 | struct udp_dhcp_packet { |
44 | struct iphdr ip; | 44 | struct iphdr ip; |
45 | struct udphdr udp; | 45 | struct udphdr udp; |
46 | struct dhcpMessage data; | 46 | struct dhcpMessage data; |
47 | } ATTRIBUTE_PACKED; | ||
48 | |||
49 | /* Let's see whether compiler understood us right */ | ||
50 | struct BUG_bad_sizeof_struct_udp_dhcp_packet { | ||
51 | char BUG_bad_sizeof_struct_udp_dhcp_packet | ||
52 | [sizeof(struct udp_dhcp_packet) != 576 ? -1 : 1]; | ||
47 | }; | 53 | }; |
48 | 54 | ||
49 | void udhcp_init_header(struct dhcpMessage *packet, char type); | 55 | void udhcp_init_header(struct dhcpMessage *packet, char type); |
diff --git a/networking/udhcp/options.c b/networking/udhcp/options.c index 3168fc69a..2b4f1644f 100644 --- a/networking/udhcp/options.c +++ b/networking/udhcp/options.c | |||
@@ -73,12 +73,13 @@ uint8_t *get_option(struct dhcpMessage *packet, int code) | |||
73 | { | 73 | { |
74 | int i, length; | 74 | int i, length; |
75 | uint8_t *optionptr; | 75 | uint8_t *optionptr; |
76 | int over = 0, done = 0, curr = OPTION_FIELD; | 76 | int over = 0; |
77 | int curr = OPTION_FIELD; | ||
77 | 78 | ||
78 | optionptr = packet->options; | 79 | optionptr = packet->options; |
79 | i = 0; | 80 | i = 0; |
80 | length = 308; | 81 | length = sizeof(packet->options); |
81 | while (!done) { | 82 | while (1) { |
82 | if (i >= length) { | 83 | if (i >= length) { |
83 | bb_error_msg("bogus packet, option fields too long"); | 84 | bb_error_msg("bogus packet, option fields too long"); |
84 | return NULL; | 85 | return NULL; |
@@ -103,17 +104,18 @@ uint8_t *get_option(struct dhcpMessage *packet, int code) | |||
103 | i += optionptr[OPT_LEN] + 2; | 104 | i += optionptr[OPT_LEN] + 2; |
104 | break; | 105 | break; |
105 | case DHCP_END: | 106 | case DHCP_END: |
106 | if (curr == OPTION_FIELD && over & FILE_FIELD) { | 107 | if (curr == OPTION_FIELD && (over & FILE_FIELD)) { |
107 | optionptr = packet->file; | 108 | optionptr = packet->file; |
108 | i = 0; | 109 | i = 0; |
109 | length = 128; | 110 | length = sizeof(packet->file); |
110 | curr = FILE_FIELD; | 111 | curr = FILE_FIELD; |
111 | } else if (curr == FILE_FIELD && over & SNAME_FIELD) { | 112 | } else if (curr == FILE_FIELD && (over & SNAME_FIELD)) { |
112 | optionptr = packet->sname; | 113 | optionptr = packet->sname; |
113 | i = 0; | 114 | i = 0; |
114 | length = 64; | 115 | length = sizeof(packet->sname); |
115 | curr = SNAME_FIELD; | 116 | curr = SNAME_FIELD; |
116 | } else done = 1; | 117 | } else |
118 | return NULL; | ||
117 | break; | 119 | break; |
118 | default: | 120 | default: |
119 | i += optionptr[OPT_LEN + i] + 2; | 121 | i += optionptr[OPT_LEN + i] + 2; |
diff --git a/networking/udhcp/options.h b/networking/udhcp/options.h index 5e9adf881..33a968ce9 100644 --- a/networking/udhcp/options.h +++ b/networking/udhcp/options.h | |||
@@ -6,7 +6,7 @@ | |||
6 | #define TYPE_MASK 0x0F | 6 | #define TYPE_MASK 0x0F |
7 | 7 | ||
8 | enum { | 8 | enum { |
9 | OPTION_IP=1, | 9 | OPTION_IP = 1, |
10 | OPTION_IP_PAIR, | 10 | OPTION_IP_PAIR, |
11 | OPTION_STRING, | 11 | OPTION_STRING, |
12 | #if ENABLE_FEATURE_RFC3397 | 12 | #if ENABLE_FEATURE_RFC3397 |
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 41cd32135..0abe851a4 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c | |||
@@ -57,7 +57,7 @@ int udhcp_get_packet(struct dhcpMessage *packet, int fd) | |||
57 | return -1; | 57 | return -1; |
58 | } | 58 | } |
59 | 59 | ||
60 | if (ntohl(packet->cookie) != DHCP_MAGIC) { | 60 | if (packet->cookie != htonl(DHCP_MAGIC)) { |
61 | bb_error_msg("received bogus message, ignoring"); | 61 | bb_error_msg("received bogus message, ignoring"); |
62 | return -2; | 62 | return -2; |
63 | } | 63 | } |
@@ -123,7 +123,6 @@ uint16_t udhcp_checksum(void *addr, int count) | |||
123 | 123 | ||
124 | 124 | ||
125 | /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */ | 125 | /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */ |
126 | void BUG_sizeof_struct_udp_dhcp_packet_must_be_576(void); | ||
127 | int udhcp_raw_packet(struct dhcpMessage *payload, | 126 | int udhcp_raw_packet(struct dhcpMessage *payload, |
128 | uint32_t source_ip, int source_port, | 127 | uint32_t source_ip, int source_port, |
129 | uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex) | 128 | uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex) |
@@ -169,9 +168,6 @@ int udhcp_raw_packet(struct dhcpMessage *payload, | |||
169 | packet.ip.ttl = IPDEFTTL; | 168 | packet.ip.ttl = IPDEFTTL; |
170 | packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip)); | 169 | packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip)); |
171 | 170 | ||
172 | if (sizeof(struct udp_dhcp_packet) != 576) | ||
173 | BUG_sizeof_struct_udp_dhcp_packet_must_be_576(); | ||
174 | |||
175 | result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, | 171 | result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, |
176 | (struct sockaddr *) &dest, sizeof(dest)); | 172 | (struct sockaddr *) &dest, sizeof(dest)); |
177 | if (result <= 0) { | 173 | if (result <= 0) { |