diff options
author | Aaron Lehmann <aaronl@vitelius.com> | 2002-08-21 11:21:19 +0000 |
---|---|---|
committer | Aaron Lehmann <aaronl@vitelius.com> | 2002-08-21 11:21:19 +0000 |
commit | 1ff58b6570cb4085c9168445d3518aa7796c2cba (patch) | |
tree | 29cba404725430c4ccab7162cd3ebc78169cef1a | |
parent | 8eb0dc1d101029620f7ca75a77f04b091b61a212 (diff) | |
download | busybox-w32-1ff58b6570cb4085c9168445d3518aa7796c2cba.tar.gz busybox-w32-1ff58b6570cb4085c9168445d3518aa7796c2cba.tar.bz2 busybox-w32-1ff58b6570cb4085c9168445d3518aa7796c2cba.zip |
Clean up udhcpd a bit. Constify. Staticize. Remove these functions and
inline their contents: print_usage, pidfile_delete, and background.
-rw-r--r-- | networking/udhcpc.c | 120 |
1 files changed, 54 insertions, 66 deletions
diff --git a/networking/udhcpc.c b/networking/udhcpc.c index f13d44d5d..d766ea53b 100644 --- a/networking/udhcpc.c +++ b/networking/udhcpc.c | |||
@@ -248,7 +248,7 @@ struct udp_dhcp_packet { | |||
248 | struct dhcpMessage data; | 248 | struct dhcpMessage data; |
249 | }; | 249 | }; |
250 | 250 | ||
251 | struct dhcp_option options[] = { | 251 | static const struct dhcp_option options[] = { |
252 | /* name[10] flags code */ | 252 | /* name[10] flags code */ |
253 | {"subnet", OPTION_IP | OPTION_REQ, 0x01}, | 253 | {"subnet", OPTION_IP | OPTION_REQ, 0x01}, |
254 | {"timezone", OPTION_S32, 0x02}, | 254 | {"timezone", OPTION_S32, 0x02}, |
@@ -280,7 +280,7 @@ struct dhcp_option options[] = { | |||
280 | }; | 280 | }; |
281 | 281 | ||
282 | /* Lengths of the different option types */ | 282 | /* Lengths of the different option types */ |
283 | int option_lengths[] = { | 283 | static const int option_lengths[] = { |
284 | [OPTION_IP] = 4, | 284 | [OPTION_IP] = 4, |
285 | [OPTION_IP_PAIR] = 8, | 285 | [OPTION_IP_PAIR] = 8, |
286 | [OPTION_BOOLEAN] = 1, | 286 | [OPTION_BOOLEAN] = 1, |
@@ -293,7 +293,7 @@ int option_lengths[] = { | |||
293 | }; | 293 | }; |
294 | 294 | ||
295 | /* get a rough idea of how long an option will be (rounding up...) */ | 295 | /* get a rough idea of how long an option will be (rounding up...) */ |
296 | static int max_option_length[] = { | 296 | static const unsigned char max_option_length[] = { |
297 | [OPTION_IP] = sizeof("255.255.255.255 "), | 297 | [OPTION_IP] = sizeof("255.255.255.255 "), |
298 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, | 298 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
299 | [OPTION_STRING] = 1, | 299 | [OPTION_STRING] = 1, |
@@ -305,26 +305,8 @@ static int max_option_length[] = { | |||
305 | [OPTION_S32] = sizeof("-2147483684 "), | 305 | [OPTION_S32] = sizeof("-2147483684 "), |
306 | }; | 306 | }; |
307 | 307 | ||
308 | static void print_usage(void) | ||
309 | { | ||
310 | printf( | ||
311 | "Usage: udhcpcd [OPTIONS]\n\n" | ||
312 | " -c, --clientid=CLIENTID Client identifier\n" | ||
313 | " -H, --hostname=HOSTNAME Client hostname\n" | ||
314 | " -f, --foreground Do not fork after getting lease\n" | ||
315 | " -i, --interface=INTERFACE Interface to use (default: eth0)\n" | ||
316 | " -n, --now Exit with failure if lease cannot be\n" | ||
317 | " immediately negotiated.\n" | ||
318 | " -p, --pidfile=file Store process ID of daemon in file\n" | ||
319 | " -q, --quit Quit after obtaining lease\n" | ||
320 | " -r, --request=IP IP address to request (default: none)\n" | ||
321 | " -s, --script=file Run file at dhcp events (default:\n" | ||
322 | " " DEFAULT_SCRIPT ")\n" | ||
323 | " -v, --version Display version\n" | ||
324 | ); | ||
325 | } | ||
326 | /* return the position of the 'end' option (no bounds checking) */ | 308 | /* return the position of the 'end' option (no bounds checking) */ |
327 | int end_option(unsigned char *optionptr) | 309 | static int end_option(unsigned char *optionptr) |
328 | { | 310 | { |
329 | int i = 0; | 311 | int i = 0; |
330 | 312 | ||
@@ -337,7 +319,7 @@ int end_option(unsigned char *optionptr) | |||
337 | 319 | ||
338 | /* add an option string to the options (an option string contains an option code, | 320 | /* add an option string to the options (an option string contains an option code, |
339 | * length, then data) */ | 321 | * length, then data) */ |
340 | int add_option_string(unsigned char *optionptr, unsigned char *string) | 322 | static int add_option_string(unsigned char *optionptr, unsigned char *string) |
341 | { | 323 | { |
342 | int end = end_option(optionptr); | 324 | int end = end_option(optionptr); |
343 | 325 | ||
@@ -353,7 +335,7 @@ int add_option_string(unsigned char *optionptr, unsigned char *string) | |||
353 | } | 335 | } |
354 | 336 | ||
355 | /* add a one to four byte option to a packet */ | 337 | /* add a one to four byte option to a packet */ |
356 | int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data) | 338 | static int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data) |
357 | { | 339 | { |
358 | char length = 0; | 340 | char length = 0; |
359 | int i; | 341 | int i; |
@@ -411,7 +393,7 @@ void init_header(struct dhcpMessage *packet, char type) | |||
411 | add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); | 393 | add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); |
412 | } | 394 | } |
413 | #endif | 395 | #endif |
414 | u_int16_t checksum(void *addr, int count) | 396 | static u_int16_t checksum(void *addr, int count) |
415 | { | 397 | { |
416 | /* Compute Internet Checksum for "count" bytes | 398 | /* Compute Internet Checksum for "count" bytes |
417 | * beginning at location "addr". | 399 | * beginning at location "addr". |
@@ -437,7 +419,7 @@ u_int16_t checksum(void *addr, int count) | |||
437 | } | 419 | } |
438 | 420 | ||
439 | /* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */ | 421 | /* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */ |
440 | int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, | 422 | static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, |
441 | u_int32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex) | 423 | u_int32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex) |
442 | { | 424 | { |
443 | int l_fd; | 425 | int l_fd; |
@@ -489,7 +471,7 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port | |||
489 | } | 471 | } |
490 | 472 | ||
491 | /* Let the kernel do all the work for packet generation */ | 473 | /* Let the kernel do all the work for packet generation */ |
492 | int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, | 474 | static int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, |
493 | u_int32_t dest_ip, int dest_port) | 475 | u_int32_t dest_ip, int dest_port) |
494 | { | 476 | { |
495 | int n = 1; | 477 | int n = 1; |
@@ -576,7 +558,7 @@ static void add_requests(struct dhcpMessage *packet) | |||
576 | } | 558 | } |
577 | 559 | ||
578 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ | 560 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ |
579 | int send_discover(unsigned long xid, unsigned long requested) | 561 | static int send_discover(unsigned long xid, unsigned long requested) |
580 | { | 562 | { |
581 | struct dhcpMessage packet; | 563 | struct dhcpMessage packet; |
582 | 564 | ||
@@ -592,7 +574,7 @@ int send_discover(unsigned long xid, unsigned long requested) | |||
592 | } | 574 | } |
593 | 575 | ||
594 | /* Broadcasts a DHCP request message */ | 576 | /* Broadcasts a DHCP request message */ |
595 | int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) | 577 | static int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) |
596 | { | 578 | { |
597 | struct dhcpMessage packet; | 579 | struct dhcpMessage packet; |
598 | struct in_addr addr; | 580 | struct in_addr addr; |
@@ -612,7 +594,7 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques | |||
612 | 594 | ||
613 | 595 | ||
614 | /* Unicasts or broadcasts a DHCP renew message */ | 596 | /* Unicasts or broadcasts a DHCP renew message */ |
615 | int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) | 597 | static int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) |
616 | { | 598 | { |
617 | struct dhcpMessage packet; | 599 | struct dhcpMessage packet; |
618 | int ret = 0; | 600 | int ret = 0; |
@@ -631,7 +613,7 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) | |||
631 | } | 613 | } |
632 | 614 | ||
633 | /* Create a random xid */ | 615 | /* Create a random xid */ |
634 | unsigned long random_xid(void) | 616 | static unsigned long random_xid(void) |
635 | { | 617 | { |
636 | static int initialized; | 618 | static int initialized; |
637 | if (!initialized) { | 619 | if (!initialized) { |
@@ -691,7 +673,7 @@ static void renew_requested(int sig) | |||
691 | } | 673 | } |
692 | 674 | ||
693 | /* get an option with bounds checking (warning, not aligned). */ | 675 | /* get an option with bounds checking (warning, not aligned). */ |
694 | unsigned char *get_option(struct dhcpMessage *packet, int code) | 676 | static unsigned char *get_option(struct dhcpMessage *packet, int code) |
695 | { | 677 | { |
696 | int i, length; | 678 | int i, length; |
697 | unsigned char *optionptr; | 679 | unsigned char *optionptr; |
@@ -757,7 +739,7 @@ static int sprintip(char *dest, char *pre, unsigned char *ip) { | |||
757 | 739 | ||
758 | 740 | ||
759 | /* Fill dest with the text of option 'option'. */ | 741 | /* Fill dest with the text of option 'option'. */ |
760 | static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p) | 742 | static void fill_options(char *dest, unsigned char *option, const struct dhcp_option *type_p) |
761 | { | 743 | { |
762 | int type, optlen; | 744 | int type, optlen; |
763 | u_int16_t val_u16; | 745 | u_int16_t val_u16; |
@@ -891,7 +873,7 @@ static char **fill_envp(struct dhcpMessage *packet) | |||
891 | } | 873 | } |
892 | 874 | ||
893 | /* Call a script with a par file and env vars */ | 875 | /* Call a script with a par file and env vars */ |
894 | void run_script(struct dhcpMessage *packet, const char *name) | 876 | static void run_script(struct dhcpMessage *packet, const char *name) |
895 | { | 877 | { |
896 | int pid; | 878 | int pid; |
897 | char **envp; | 879 | char **envp; |
@@ -946,7 +928,7 @@ static void release_requested(int sig) | |||
946 | } | 928 | } |
947 | 929 | ||
948 | 930 | ||
949 | int pidfile_acquire(char *pidfile) | 931 | static int pidfile_acquire(char *pidfile) |
950 | { | 932 | { |
951 | int pid_fd; | 933 | int pid_fd; |
952 | if (pidfile == NULL) return -1; | 934 | if (pidfile == NULL) return -1; |
@@ -963,7 +945,7 @@ int pidfile_acquire(char *pidfile) | |||
963 | } | 945 | } |
964 | 946 | ||
965 | 947 | ||
966 | void pidfile_write_release(int pid_fd) | 948 | static void pidfile_write_release(int pid_fd) |
967 | { | 949 | { |
968 | FILE *out; | 950 | FILE *out; |
969 | 951 | ||
@@ -977,16 +959,11 @@ void pidfile_write_release(int pid_fd) | |||
977 | close(pid_fd); | 959 | close(pid_fd); |
978 | } | 960 | } |
979 | 961 | ||
980 | |||
981 | void pidfile_delete(char *pidfile) | ||
982 | { | ||
983 | if (pidfile) unlink(pidfile); | ||
984 | } | ||
985 | |||
986 | /* Exit and cleanup */ | 962 | /* Exit and cleanup */ |
987 | static void exit_client(int retval) | 963 | static void exit_client(int retval) |
988 | { | 964 | { |
989 | pidfile_delete(client_config.pidfile); | 965 | unlink(client_config.pidfile); |
966 | if (client_config.pidfile) unlink(client_config.pidfile); | ||
990 | CLOSE_LOG(); | 967 | CLOSE_LOG(); |
991 | exit(retval); | 968 | exit(retval); |
992 | } | 969 | } |
@@ -1001,23 +978,7 @@ static void terminate(int sig) | |||
1001 | } | 978 | } |
1002 | 979 | ||
1003 | 980 | ||
1004 | static void background(void) | 981 | static int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp) |
1005 | { | ||
1006 | int pid_fd; | ||
1007 | if (client_config.quit_after_lease) { | ||
1008 | exit_client(0); | ||
1009 | } else if (!client_config.foreground) { | ||
1010 | pid_fd = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */ | ||
1011 | if (daemon(0, 0) == -1) { | ||
1012 | perror("fork"); | ||
1013 | exit_client(1); | ||
1014 | } | ||
1015 | client_config.foreground = 1; /* Do not fork again. */ | ||
1016 | pidfile_write_release(pid_fd); | ||
1017 | } | ||
1018 | } | ||
1019 | |||
1020 | int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp) | ||
1021 | { | 982 | { |
1022 | int l_fd; | 983 | int l_fd; |
1023 | struct ifreq ifr; | 984 | struct ifreq ifr; |
@@ -1063,7 +1024,7 @@ int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char | |||
1063 | } | 1024 | } |
1064 | 1025 | ||
1065 | 1026 | ||
1066 | int listen_socket(unsigned int ip, int port, char *inf) | 1027 | static int listen_socket(unsigned int ip, int port, char *inf) |
1067 | { | 1028 | { |
1068 | struct ifreq interface; | 1029 | struct ifreq interface; |
1069 | int l_fd; | 1030 | int l_fd; |
@@ -1105,7 +1066,7 @@ int listen_socket(unsigned int ip, int port, char *inf) | |||
1105 | } | 1066 | } |
1106 | 1067 | ||
1107 | 1068 | ||
1108 | int raw_socket(int ifindex) | 1069 | static int raw_socket(int ifindex) |
1109 | { | 1070 | { |
1110 | int l_fd; | 1071 | int l_fd; |
1111 | struct sockaddr_ll sock; | 1072 | struct sockaddr_ll sock; |
@@ -1130,7 +1091,7 @@ int raw_socket(int ifindex) | |||
1130 | } | 1091 | } |
1131 | 1092 | ||
1132 | /* read a packet from socket fd, return -1 on read error, -2 on packet error */ | 1093 | /* read a packet from socket fd, return -1 on read error, -2 on packet error */ |
1133 | int get_packet(struct dhcpMessage *packet, int l_fd) | 1094 | static int get_packet(struct dhcpMessage *packet, int l_fd) |
1134 | { | 1095 | { |
1135 | int bytes; | 1096 | int bytes; |
1136 | int i; | 1097 | int i; |
@@ -1168,7 +1129,7 @@ int get_packet(struct dhcpMessage *packet, int l_fd) | |||
1168 | return bytes; | 1129 | return bytes; |
1169 | } | 1130 | } |
1170 | 1131 | ||
1171 | int get_raw_packet(struct dhcpMessage *payload, int l_fd) | 1132 | static int get_raw_packet(struct dhcpMessage *payload, int l_fd) |
1172 | { | 1133 | { |
1173 | int bytes; | 1134 | int bytes; |
1174 | struct udp_dhcp_packet packet; | 1135 | struct udp_dhcp_packet packet; |
@@ -1298,7 +1259,21 @@ int udhcpc_main(int argc, char *argv[]) | |||
1298 | strncpy(client_config.hostname + 2, optarg, len); | 1259 | strncpy(client_config.hostname + 2, optarg, len); |
1299 | break; | 1260 | break; |
1300 | case 'h': | 1261 | case 'h': |
1301 | print_usage(); | 1262 | puts( |
1263 | "Usage: udhcpcd [OPTIONS]\n\n" | ||
1264 | " -c, --clientid=CLIENTID Client identifier\n" | ||
1265 | " -H, --hostname=HOSTNAME Client hostname\n" | ||
1266 | " -f, --foreground Do not fork after getting lease\n" | ||
1267 | " -i, --interface=INTERFACE Interface to use (default: eth0)\n" | ||
1268 | " -n, --now Exit with failure if lease cannot be\n" | ||
1269 | " immediately negotiated.\n" | ||
1270 | " -p, --pidfile=file Store process ID of daemon in file\n" | ||
1271 | " -q, --quit Quit after obtaining lease\n" | ||
1272 | " -r, --request=IP IP address to request (default: none)\n" | ||
1273 | " -s, --script=file Run file at dhcp events (default:\n" | ||
1274 | " " DEFAULT_SCRIPT ")\n" | ||
1275 | " -v, --version Display version" | ||
1276 | ); | ||
1302 | return 0; | 1277 | return 0; |
1303 | case 'i': | 1278 | case 'i': |
1304 | client_config.interface = optarg; | 1279 | client_config.interface = optarg; |
@@ -1538,7 +1513,20 @@ int udhcpc_main(int argc, char *argv[]) | |||
1538 | 1513 | ||
1539 | state = BOUND; | 1514 | state = BOUND; |
1540 | change_mode(LISTEN_NONE); | 1515 | change_mode(LISTEN_NONE); |
1541 | background(); | 1516 | { |
1517 | int pid_fd2; | ||
1518 | if (client_config.quit_after_lease) { | ||
1519 | exit_client(0); | ||
1520 | } else if (!client_config.foreground) { | ||
1521 | pid_fd2 = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */ | ||
1522 | if (daemon(0, 0) == -1) { | ||
1523 | perror("fork"); | ||
1524 | exit_client(1); | ||
1525 | } | ||
1526 | client_config.foreground = 1; /* Do not fork again. */ | ||
1527 | pidfile_write_release(pid_fd2); | ||
1528 | } | ||
1529 | } | ||
1542 | } | 1530 | } |
1543 | else if (*message == DHCPNAK) { | 1531 | else if (*message == DHCPNAK) { |
1544 | /* return to init state */ | 1532 | /* return to init state */ |