diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-30 23:41:23 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-30 23:41:23 +0200 |
commit | b7d19cc400f14ccd64a1fedebe14022fe115029a (patch) | |
tree | cd4a50e2694628413a2460d3e1d2a706462d0e2b /networking/udhcp/packet.c | |
parent | c88c1a01904b5791dfd1ca5342c805a9f392e17f (diff) | |
download | busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.tar.gz busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.tar.bz2 busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.zip |
dhcp: readability cleanups and small code shrink
function old new delta
udhcp_run_script 654 617 -37
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp/packet.c')
-rw-r--r-- | networking/udhcp/packet.c | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 1bfe12041..c01fb7d7c 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c | |||
@@ -167,44 +167,40 @@ uint16_t FAST_FUNC udhcp_checksum(void *addr, int count) | |||
167 | 167 | ||
168 | /* Construct a ip/udp header for a packet, send packet */ | 168 | /* Construct a ip/udp header for a packet, send packet */ |
169 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | 169 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, |
170 | uint32_t source_ip, int source_port, | 170 | uint32_t source_nip, int source_port, |
171 | uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, | 171 | uint32_t dest_nip, int dest_port, const uint8_t *dest_arp, |
172 | int ifindex) | 172 | int ifindex) |
173 | { | 173 | { |
174 | struct sockaddr_ll dest; | 174 | struct sockaddr_ll dest_sll; |
175 | struct ip_udp_dhcp_packet packet; | 175 | struct ip_udp_dhcp_packet packet; |
176 | int fd; | 176 | int fd; |
177 | int result = -1; | 177 | int result = -1; |
178 | const char *msg; | 178 | const char *msg; |
179 | 179 | ||
180 | enum { | ||
181 | IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, | ||
182 | UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct ip_udp_dhcp_packet, udp), | ||
183 | }; | ||
184 | |||
185 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); | 180 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
186 | if (fd < 0) { | 181 | if (fd < 0) { |
187 | msg = "socket(%s)"; | 182 | msg = "socket(%s)"; |
188 | goto ret_msg; | 183 | goto ret_msg; |
189 | } | 184 | } |
190 | 185 | ||
191 | memset(&dest, 0, sizeof(dest)); | 186 | memset(&dest_sll, 0, sizeof(dest_sll)); |
192 | memset(&packet, 0, sizeof(packet)); | 187 | memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data)); |
193 | packet.data = *dhcp_pkt; /* struct copy */ | 188 | packet.data = *dhcp_pkt; /* struct copy */ |
194 | 189 | ||
195 | dest.sll_family = AF_PACKET; | 190 | dest_sll.sll_family = AF_PACKET; |
196 | dest.sll_protocol = htons(ETH_P_IP); | 191 | dest_sll.sll_protocol = htons(ETH_P_IP); |
197 | dest.sll_ifindex = ifindex; | 192 | dest_sll.sll_ifindex = ifindex; |
198 | dest.sll_halen = 6; | 193 | dest_sll.sll_halen = 6; |
199 | memcpy(dest.sll_addr, dest_arp, 6); | 194 | memcpy(dest_sll.sll_addr, dest_arp, 6); |
200 | if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) { | 195 | |
196 | if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) { | ||
201 | msg = "bind(%s)"; | 197 | msg = "bind(%s)"; |
202 | goto ret_close; | 198 | goto ret_close; |
203 | } | 199 | } |
204 | 200 | ||
205 | packet.ip.protocol = IPPROTO_UDP; | 201 | packet.ip.protocol = IPPROTO_UDP; |
206 | packet.ip.saddr = source_ip; | 202 | packet.ip.saddr = source_nip; |
207 | packet.ip.daddr = dest_ip; | 203 | packet.ip.daddr = dest_nip; |
208 | packet.udp.source = htons(source_port); | 204 | packet.udp.source = htons(source_port); |
209 | packet.udp.dest = htons(dest_port); | 205 | packet.udp.dest = htons(dest_port); |
210 | /* size, excluding IP header: */ | 206 | /* size, excluding IP header: */ |
@@ -225,7 +221,7 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | |||
225 | */ | 221 | */ |
226 | udhcp_dump_packet(dhcp_pkt); | 222 | udhcp_dump_packet(dhcp_pkt); |
227 | result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, | 223 | result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, |
228 | (struct sockaddr *) &dest, sizeof(dest)); | 224 | (struct sockaddr *) &dest_sll, sizeof(dest_sll)); |
229 | msg = "sendto"; | 225 | msg = "sendto"; |
230 | ret_close: | 226 | ret_close: |
231 | close(fd); | 227 | close(fd); |
@@ -239,18 +235,14 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | |||
239 | 235 | ||
240 | /* Let the kernel do all the work for packet generation */ | 236 | /* Let the kernel do all the work for packet generation */ |
241 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, | 237 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, |
242 | uint32_t source_ip, int source_port, | 238 | uint32_t source_nip, int source_port, |
243 | uint32_t dest_ip, int dest_port) | 239 | uint32_t dest_nip, int dest_port) |
244 | { | 240 | { |
245 | struct sockaddr_in client; | 241 | struct sockaddr_in client; |
246 | int fd; | 242 | int fd; |
247 | int result = -1; | 243 | int result = -1; |
248 | const char *msg; | 244 | const char *msg; |
249 | 245 | ||
250 | enum { | ||
251 | DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, | ||
252 | }; | ||
253 | |||
254 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | 246 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
255 | if (fd < 0) { | 247 | if (fd < 0) { |
256 | msg = "socket(%s)"; | 248 | msg = "socket(%s)"; |
@@ -261,7 +253,7 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, | |||
261 | memset(&client, 0, sizeof(client)); | 253 | memset(&client, 0, sizeof(client)); |
262 | client.sin_family = AF_INET; | 254 | client.sin_family = AF_INET; |
263 | client.sin_port = htons(source_port); | 255 | client.sin_port = htons(source_port); |
264 | client.sin_addr.s_addr = source_ip; | 256 | client.sin_addr.s_addr = source_nip; |
265 | if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { | 257 | if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
266 | msg = "bind(%s)"; | 258 | msg = "bind(%s)"; |
267 | goto ret_close; | 259 | goto ret_close; |
@@ -270,7 +262,7 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, | |||
270 | memset(&client, 0, sizeof(client)); | 262 | memset(&client, 0, sizeof(client)); |
271 | client.sin_family = AF_INET; | 263 | client.sin_family = AF_INET; |
272 | client.sin_port = htons(dest_port); | 264 | client.sin_port = htons(dest_port); |
273 | client.sin_addr.s_addr = dest_ip; | 265 | client.sin_addr.s_addr = dest_nip; |
274 | if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { | 266 | if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
275 | msg = "connect"; | 267 | msg = "connect"; |
276 | goto ret_close; | 268 | goto ret_close; |