diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-29 08:17:45 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-29 08:17:45 +0000 |
commit | b539c8452f823a377bf629cf0e44ccda4a16c6c4 (patch) | |
tree | e131460f8227136ba2a76b1cc57efb491bd2f548 | |
parent | 64309f8669f08f2c3c16a3b5bf82d9cae84ec388 (diff) | |
download | busybox-w32-b539c8452f823a377bf629cf0e44ccda4a16c6c4.tar.gz busybox-w32-b539c8452f823a377bf629cf0e44ccda4a16c6c4.tar.bz2 busybox-w32-b539c8452f823a377bf629cf0e44ccda4a16c6c4.zip |
dhcp: heed TODO item - divorced options from their string descriptions
code shrink while at it.
function old new delta
dhcp_option_strings - 258 +258
udhcp_run_script 1135 1174 +39
dhcp_option_lengths - 11 +11
udhcp_add_simple_option 93 92 -1
packet_num 4 - -4
read_opt 746 739 -7
udhcp_option_lengths 11 - -11
udhcpc_main 2590 2494 -96
dhcp_options 490 70 -420
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 1/4 up/down: 308/-539) Total: -231 bytes
text data bss dec hex filename
775309 929 9100 785338 bfbba busybox_old
775098 929 9084 785111 bfad7 busybox_unstripped
-rw-r--r-- | networking/udhcp/common.h | 1 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.c | 24 | ||||
-rw-r--r-- | networking/udhcp/dhcpd.h | 2 | ||||
-rw-r--r-- | networking/udhcp/files.c | 26 | ||||
-rw-r--r-- | networking/udhcp/leases.c | 4 | ||||
-rw-r--r-- | networking/udhcp/options.c | 138 | ||||
-rw-r--r-- | networking/udhcp/options.h | 6 | ||||
-rw-r--r-- | networking/udhcp/script.c | 22 |
8 files changed, 135 insertions, 88 deletions
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index eecb72ca2..55782b51b 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h | |||
@@ -77,7 +77,6 @@ void udhcp_run_script(struct dhcpMessage *packet, const char *name); | |||
77 | #define end_option udhcp_end_option | 77 | #define end_option udhcp_end_option |
78 | #define add_option_string udhcp_add_option_string | 78 | #define add_option_string udhcp_add_option_string |
79 | #define add_simple_option udhcp_add_simple_option | 79 | #define add_simple_option udhcp_add_simple_option |
80 | #define option_lengths udhcp_option_lengths | ||
81 | /* from socket.h */ | 80 | /* from socket.h */ |
82 | #define listen_socket udhcp_listen_socket | 81 | #define listen_socket udhcp_listen_socket |
83 | #define read_interface udhcp_read_interface | 82 | #define read_interface udhcp_read_interface |
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 6ade1ee34..d8077f7e8 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -19,15 +19,9 @@ | |||
19 | #include "options.h" | 19 | #include "options.h" |
20 | 20 | ||
21 | 21 | ||
22 | /* Something is definitely wrong here. IPv4 addresses | ||
23 | * in variables of type long?? BTW, we use inet_ntoa() | ||
24 | * in the code. Manpage says that struct in_addr has a member of type long (!) | ||
25 | * which holds IPv4 address, and the struct is passed by value (!!) | ||
26 | */ | ||
27 | static int timeout; /* = 0. Must be signed */ | 22 | static int timeout; /* = 0. Must be signed */ |
28 | static uint32_t requested_ip; /* = 0 */ | 23 | static uint32_t requested_ip; /* = 0 */ |
29 | static uint32_t server_addr; | 24 | static uint32_t server_addr; |
30 | static int packet_num; /* = 0 */ | ||
31 | static int sockfd = -1; | 25 | static int sockfd = -1; |
32 | 26 | ||
33 | #define LISTEN_NONE 0 | 27 | #define LISTEN_NONE 0 |
@@ -74,12 +68,6 @@ static void perform_renew(void) | |||
74 | case INIT_SELECTING: | 68 | case INIT_SELECTING: |
75 | break; | 69 | break; |
76 | } | 70 | } |
77 | |||
78 | /* start things over */ | ||
79 | packet_num = 0; | ||
80 | |||
81 | /* Kill any timeouts because the user wants this to hurry along */ | ||
82 | timeout = 0; | ||
83 | } | 71 | } |
84 | 72 | ||
85 | 73 | ||
@@ -153,8 +141,11 @@ int udhcpc_main(int argc, char **argv) | |||
153 | int discover_retries = 3; | 141 | int discover_retries = 3; |
154 | uint32_t xid = 0; | 142 | uint32_t xid = 0; |
155 | uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */ | 143 | uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */ |
156 | unsigned t1 = 0, t2 = 0; /* what a wonderful names */ | 144 | int packet_num; |
157 | unsigned timestamp_got_lease = 0; /* for gcc */ | 145 | /* t1, t2... what a wonderful names... */ |
146 | unsigned t1 = t1; /* for gcc */ | ||
147 | unsigned t2 = t2; | ||
148 | unsigned timestamp_got_lease = timestamp_got_lease; | ||
158 | unsigned opt; | 149 | unsigned opt; |
159 | int max_fd; | 150 | int max_fd; |
160 | int retval; | 151 | int retval; |
@@ -315,6 +306,7 @@ int udhcpc_main(int argc, char **argv) | |||
315 | state = INIT_SELECTING; | 306 | state = INIT_SELECTING; |
316 | udhcp_run_script(NULL, "deconfig"); | 307 | udhcp_run_script(NULL, "deconfig"); |
317 | change_listen_mode(LISTEN_RAW); | 308 | change_listen_mode(LISTEN_RAW); |
309 | packet_num = 0; | ||
318 | 310 | ||
319 | /* Main event loop. select() waits on signal pipe and possibly | 311 | /* Main event loop. select() waits on signal pipe and possibly |
320 | * on sockfd. | 312 | * on sockfd. |
@@ -585,6 +577,10 @@ int udhcpc_main(int argc, char **argv) | |||
585 | switch (signo) { | 577 | switch (signo) { |
586 | case SIGUSR1: | 578 | case SIGUSR1: |
587 | perform_renew(); | 579 | perform_renew(); |
580 | /* start things over */ | ||
581 | packet_num = 0; | ||
582 | /* Kill any timeouts because the user wants this to hurry along */ | ||
583 | timeout = 0; | ||
588 | break; | 584 | break; |
589 | case SIGUSR2: | 585 | case SIGUSR2: |
590 | perform_release(); | 586 | perform_release(); |
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h index 28106d306..216b7ab64 100644 --- a/networking/udhcp/dhcpd.h +++ b/networking/udhcp/dhcpd.h | |||
@@ -100,7 +100,7 @@ int send_inform(struct dhcpMessage *oldpacket); | |||
100 | int read_config(const char *file); | 100 | int read_config(const char *file); |
101 | void write_leases(void); | 101 | void write_leases(void); |
102 | void read_leases(const char *file); | 102 | void read_leases(const char *file); |
103 | struct option_set *find_option(struct option_set *opt_list, char code); | 103 | struct option_set *find_option(struct option_set *opt_list, uint8_t code); |
104 | 104 | ||
105 | 105 | ||
106 | #endif | 106 | #endif |
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 5026598d7..63c90647d 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c | |||
@@ -71,7 +71,7 @@ static int read_yn(const char *line, void *arg) | |||
71 | 71 | ||
72 | 72 | ||
73 | /* find option 'code' in opt_list */ | 73 | /* find option 'code' in opt_list */ |
74 | struct option_set *find_option(struct option_set *opt_list, char code) | 74 | struct option_set *find_option(struct option_set *opt_list, uint8_t code) |
75 | { | 75 | { |
76 | while (opt_list && opt_list->data[OPT_CODE] < code) | 76 | while (opt_list && opt_list->data[OPT_CODE] < code) |
77 | opt_list = opt_list->next; | 77 | opt_list = opt_list->next; |
@@ -154,31 +154,29 @@ static int read_opt(const char *const_line, void *arg) | |||
154 | { | 154 | { |
155 | struct option_set **opt_list = arg; | 155 | struct option_set **opt_list = arg; |
156 | char *opt, *val, *endptr; | 156 | char *opt, *val, *endptr; |
157 | const struct dhcp_option *option; | ||
158 | int retval = 0, length; | ||
159 | char buffer[8]; | ||
160 | char *line; | 157 | char *line; |
158 | const struct dhcp_option *option; | ||
159 | int retval, length, idx; | ||
160 | char buffer[8] __attribute__((aligned(4))); | ||
161 | uint16_t *result_u16 = (uint16_t *) buffer; | 161 | uint16_t *result_u16 = (uint16_t *) buffer; |
162 | uint32_t *result_u32 = (uint32_t *) buffer; | 162 | uint32_t *result_u32 = (uint32_t *) buffer; |
163 | 163 | ||
164 | /* Cheat, the only const line we'll actually get is "" */ | 164 | /* Cheat, the only const line we'll actually get is "" */ |
165 | line = (char *) const_line; | 165 | line = (char *) const_line; |
166 | opt = strtok(line, " \t="); | 166 | opt = strtok(line, " \t="); |
167 | if (!opt) return 0; | 167 | if (!opt) |
168 | return 0; | ||
168 | 169 | ||
169 | option = dhcp_options; | 170 | idx = index_in_strings(opt, dhcp_option_strings); /* NB: was strcasecmp! */ |
170 | while (1) { | 171 | if (idx < 0) |
171 | if (!option->code) | 172 | return 0; |
172 | return 0; | 173 | option = &dhcp_options[idx]; |
173 | if (!strcasecmp(option->opt_name, opt)) | ||
174 | break; | ||
175 | option++; | ||
176 | } | ||
177 | 174 | ||
175 | retval = 0; | ||
178 | do { | 176 | do { |
179 | val = strtok(NULL, ", \t"); | 177 | val = strtok(NULL, ", \t"); |
180 | if (!val) break; | 178 | if (!val) break; |
181 | length = option_lengths[option->flags & TYPE_MASK]; | 179 | length = dhcp_option_lengths[option->flags & TYPE_MASK]; |
182 | retval = 0; | 180 | retval = 0; |
183 | opt = buffer; /* new meaning for variable opt */ | 181 | opt = buffer; /* new meaning for variable opt */ |
184 | switch (option->flags & TYPE_MASK) { | 182 | switch (option->flags & TYPE_MASK) { |
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c index 60e9edc68..1745fee03 100644 --- a/networking/udhcp/leases.c +++ b/networking/udhcp/leases.c | |||
@@ -98,7 +98,9 @@ struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr) | |||
98 | /* check is an IP is taken, if it is, add it to the lease table */ | 98 | /* check is an IP is taken, if it is, add it to the lease table */ |
99 | static int nobody_responds_to_arp(uint32_t addr) | 99 | static int nobody_responds_to_arp(uint32_t addr) |
100 | { | 100 | { |
101 | static const uint8_t blank_chaddr[16]; /* 16 zero bytes */ | 101 | /* 16 zero bytes */ |
102 | static const uint8_t blank_chaddr[16] = { 0 }; | ||
103 | /* = { 0 } helps gcc to put it in rodata, not bss */ | ||
102 | 104 | ||
103 | struct in_addr temp; | 105 | struct in_addr temp; |
104 | int r; | 106 | int r; |
diff --git a/networking/udhcp/options.c b/networking/udhcp/options.c index ffc0ed127..c224f3670 100644 --- a/networking/udhcp/options.c +++ b/networking/udhcp/options.c | |||
@@ -11,54 +11,97 @@ | |||
11 | 11 | ||
12 | /* Supported options are easily added here */ | 12 | /* Supported options are easily added here */ |
13 | const struct dhcp_option dhcp_options[] = { | 13 | const struct dhcp_option dhcp_options[] = { |
14 | /* opt_name[12] flags code */ | 14 | /* flags code */ |
15 | {"subnet", OPTION_IP | OPTION_REQ, 0x01}, /* DHCP_SUBNET */ | 15 | { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ |
16 | {"timezone", OPTION_S32, 0x02}, /* DHCP_TIME_OFFSET */ | 16 | { OPTION_S32, 0x02 }, /* DHCP_TIME_OFFSET */ |
17 | {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03}, /* DHCP_ROUTER */ | 17 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ |
18 | {"timesvr", OPTION_IP | OPTION_LIST, 0x04}, /* DHCP_TIME_SERVER */ | 18 | { OPTION_IP | OPTION_LIST, 0x04 }, /* DHCP_TIME_SERVER */ |
19 | {"namesvr", OPTION_IP | OPTION_LIST, 0x05}, /* DHCP_NAME_SERVER */ | 19 | { OPTION_IP | OPTION_LIST, 0x05 }, /* DHCP_NAME_SERVER */ |
20 | {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06}, /* DHCP_DNS_SERVER */ | 20 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ |
21 | {"logsvr", OPTION_IP | OPTION_LIST, 0x07}, /* DHCP_LOG_SERVER */ | 21 | { OPTION_IP | OPTION_LIST, 0x07 }, /* DHCP_LOG_SERVER */ |
22 | {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08}, /* DHCP_COOKIE_SERVER */ | 22 | { OPTION_IP | OPTION_LIST, 0x08 }, /* DHCP_COOKIE_SERVER */ |
23 | {"lprsvr", OPTION_IP | OPTION_LIST, 0x09}, /* DHCP_LPR_SERVER */ | 23 | { OPTION_IP | OPTION_LIST, 0x09 }, /* DHCP_LPR_SERVER */ |
24 | {"hostname", OPTION_STRING | OPTION_REQ, 0x0c}, /* DHCP_HOST_NAME */ | 24 | { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ |
25 | {"bootsize", OPTION_U16, 0x0d}, /* DHCP_BOOT_SIZE */ | 25 | { OPTION_U16, 0x0d }, /* DHCP_BOOT_SIZE */ |
26 | {"domain", OPTION_STRING | OPTION_LIST | OPTION_REQ, 0x0f}, /* DHCP_DOMAIN_NAME */ | 26 | { OPTION_STRING | OPTION_LIST | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ |
27 | {"swapsvr", OPTION_IP, 0x10}, /* DHCP_SWAP_SERVER */ | 27 | { OPTION_IP, 0x10 }, /* DHCP_SWAP_SERVER */ |
28 | {"rootpath", OPTION_STRING, 0x11}, /* DHCP_ROOT_PATH */ | 28 | { OPTION_STRING, 0x11 }, /* DHCP_ROOT_PATH */ |
29 | {"ipttl", OPTION_U8, 0x17}, /* DHCP_IP_TTL */ | 29 | { OPTION_U8, 0x17 }, /* DHCP_IP_TTL */ |
30 | {"mtu", OPTION_U16, 0x1a}, /* DHCP_MTU */ | 30 | { OPTION_U16, 0x1a }, /* DHCP_MTU */ |
31 | {"broadcast", OPTION_IP | OPTION_REQ, 0x1c}, /* DHCP_BROADCAST */ | 31 | { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ |
32 | {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28}, /* DHCP_NTP_SERVER */ | 32 | { OPTION_STRING | OPTION_REQ, 0x28 }, /* DHCP_NTP_SERVER */ |
33 | {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29}, /* DHCP_WINS_SERVER */ | 33 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29 }, /* DHCP_WINS_SERVER */ |
34 | {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a}, /* DHCP_REQUESTED_IP */ | 34 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_REQUESTED_IP */ |
35 | {"wins", OPTION_IP | OPTION_LIST, 0x2c}, /* DHCP_LEASE_TIME */ | 35 | { OPTION_IP | OPTION_LIST, 0x2c }, /* DHCP_LEASE_TIME */ |
36 | {"requestip", OPTION_IP, 0x32}, /* DHCP_OPTION_OVER */ | 36 | { OPTION_IP, 0x32 }, /* DHCP_OPTION_OVER */ |
37 | {"lease", OPTION_U32, 0x33}, /* DHCP_MESSAGE_TYPE */ | 37 | { OPTION_U32, 0x33 }, /* DHCP_MESSAGE_TYPE */ |
38 | {"dhcptype", OPTION_U8, 0x35}, /* DHCP_SERVER_ID */ | 38 | { OPTION_U8, 0x35 }, /* DHCP_SERVER_ID */ |
39 | {"serverid", OPTION_IP, 0x36}, /* DHCP_PARAM_REQ */ | 39 | { OPTION_IP, 0x36 }, /* DHCP_PARAM_REQ */ |
40 | {"message", OPTION_STRING, 0x38}, /* DHCP_MESSAGE */ | 40 | { OPTION_STRING, 0x38 }, /* DHCP_MESSAGE */ |
41 | // TODO: 1) some options should not be parsed & passed to script - | 41 | { OPTION_STRING, 0x3C }, /* DHCP_VENDOR */ |
42 | // maxsize sure should not, since it cannot appear in server responses! | 42 | { OPTION_STRING, 0x3D }, /* DHCP_CLIENT_ID */ |
43 | // grep for opt_name is fix the mess. | 43 | { OPTION_STRING, 0x42 }, /* "tftp" */ |
44 | // 2) Using fixed-sized char[] vector wastes space. | 44 | { OPTION_STRING, 0x43 }, /* "bootfile" */ |
45 | {"maxsize", OPTION_U16, 0x39}, /* DHCP_MAX_SIZE */ | 45 | { OPTION_STRING, 0x4D }, /* "userclass" */ |
46 | {"vendorclass", OPTION_STRING, 0x3C}, /* DHCP_VENDOR */ | ||
47 | {"clientid", OPTION_STRING, 0x3D}, /* DHCP_CLIENT_ID */ | ||
48 | {"tftp", OPTION_STRING, 0x42}, | ||
49 | {"bootfile", OPTION_STRING, 0x43}, | ||
50 | {"userclass", OPTION_STRING, 0x4D}, | ||
51 | #if ENABLE_FEATURE_RFC3397 | 46 | #if ENABLE_FEATURE_RFC3397 |
52 | {"search", OPTION_STR1035 | OPTION_LIST | OPTION_REQ, 0x77}, | 47 | { OPTION_STR1035 | OPTION_LIST | OPTION_REQ, 0x77 }, /* "search" */ |
53 | #endif | 48 | #endif |
54 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ | 49 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ |
55 | {"wpad", OPTION_STRING, 0xfc}, | 50 | { OPTION_STRING, 0xfc }, /* "wpad" */ |
56 | {} /* zero-padded terminating entry */ | 51 | |
52 | /* Options below have no match in dhcp_option_strings[], | ||
53 | * are not passed to dhcpc scripts, and cannot be specified | ||
54 | * with "option XXX YYY" syntax in dhcpd config file. */ | ||
55 | |||
56 | { OPTION_U16, 0x39 }, /* DHCP_MAX_SIZE */ | ||
57 | { } /* zeroed terminating entry */ | ||
57 | }; | 58 | }; |
58 | 59 | ||
60 | /* Used for converting options from incoming packets to env variables | ||
61 | * for udhcpc stript */ | ||
62 | /* Must match dhcp_options[] order */ | ||
63 | const char dhcp_option_strings[] ALIGN1 = | ||
64 | "subnet" "\0" /* DHCP_SUBNET */ | ||
65 | "timezone" "\0" /* DHCP_TIME_OFFSET */ | ||
66 | "router" "\0" /* DHCP_ROUTER */ | ||
67 | "timesvr" "\0" /* DHCP_TIME_SERVER */ | ||
68 | "namesvr" "\0" /* DHCP_NAME_SERVER */ | ||
69 | "dns" "\0" /* DHCP_DNS_SERVER */ | ||
70 | "logsvr" "\0" /* DHCP_LOG_SERVER */ | ||
71 | "cookiesvr" "\0" /* DHCP_COOKIE_SERVER */ | ||
72 | "lprsvr" "\0" /* DHCP_LPR_SERVER */ | ||
73 | "hostname" "\0" /* DHCP_HOST_NAME */ | ||
74 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ | ||
75 | "domain" "\0" /* DHCP_DOMAIN_NAME */ | ||
76 | "swapsvr" "\0" /* DHCP_SWAP_SERVER */ | ||
77 | "rootpath" "\0" /* DHCP_ROOT_PATH */ | ||
78 | "ipttl" "\0" /* DHCP_IP_TTL */ | ||
79 | "mtu" "\0" /* DHCP_MTU */ | ||
80 | "broadcast" "\0" /* DHCP_BROADCAST */ | ||
81 | "nisdomain" "\0" /* DHCP_NTP_SERVER */ | ||
82 | "nissrv" "\0" /* DHCP_WINS_SERVER */ | ||
83 | "ntpsrv" "\0" /* DHCP_REQUESTED_IP */ | ||
84 | "wins" "\0" /* DHCP_LEASE_TIME */ | ||
85 | "requestip" "\0" /* DHCP_OPTION_OVER */ | ||
86 | "lease" "\0" /* DHCP_MESSAGE_TYPE */ | ||
87 | "dhcptype" "\0" /* DHCP_SERVER_ID */ | ||
88 | "serverid" "\0" /* DHCP_PARAM_REQ */ | ||
89 | "message" "\0" /* DHCP_MESSAGE */ | ||
90 | "vendorclass" "\0" /* DHCP_VENDOR */ | ||
91 | "clientid" "\0" /* DHCP_CLIENT_ID */ | ||
92 | "tftp" "\0" | ||
93 | "bootfile" "\0" | ||
94 | "userclass" "\0" | ||
95 | #if ENABLE_FEATURE_RFC3397 | ||
96 | "search" "\0" | ||
97 | #endif | ||
98 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ | ||
99 | "wpad" "\0" | ||
100 | ; | ||
101 | |||
59 | 102 | ||
60 | /* Lengths of the different option types */ | 103 | /* Lengths of the different option types */ |
61 | const unsigned char option_lengths[] ALIGN1 = { | 104 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
62 | [OPTION_IP] = 4, | 105 | [OPTION_IP] = 4, |
63 | [OPTION_IP_PAIR] = 8, | 106 | [OPTION_IP_PAIR] = 8, |
64 | [OPTION_BOOLEAN] = 1, | 107 | [OPTION_BOOLEAN] = 1, |
@@ -137,8 +180,10 @@ int end_option(uint8_t *optionptr) | |||
137 | int i = 0; | 180 | int i = 0; |
138 | 181 | ||
139 | while (optionptr[i] != DHCP_END) { | 182 | while (optionptr[i] != DHCP_END) { |
140 | if (optionptr[i] == DHCP_PADDING) i++; | 183 | if (optionptr[i] == DHCP_PADDING) |
141 | else i += optionptr[i + OPT_LEN] + 2; | 184 | i++; |
185 | else | ||
186 | i += optionptr[i + OPT_LEN] + 2; | ||
142 | } | 187 | } |
143 | return i; | 188 | return i; |
144 | } | 189 | } |
@@ -173,10 +218,11 @@ int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) | |||
173 | uint8_t option[6], len; | 218 | uint8_t option[6], len; |
174 | 219 | ||
175 | option[OPT_CODE] = code; | 220 | option[OPT_CODE] = code; |
176 | len = option_lengths[dh->flags & TYPE_MASK]; | 221 | len = dhcp_option_lengths[dh->flags & TYPE_MASK]; |
177 | option[OPT_LEN] = len; | 222 | option[OPT_LEN] = len; |
178 | if (BB_BIG_ENDIAN) data <<= 8 * (4 - len); | 223 | if (BB_BIG_ENDIAN) |
179 | /* This memcpy is for broken processors which can't | 224 | data <<= 8 * (4 - len); |
225 | /* This memcpy is for processors which can't | ||
180 | * handle a simple unaligned 32-bit assignment */ | 226 | * handle a simple unaligned 32-bit assignment */ |
181 | memcpy(&option[OPT_DATA], &data, 4); | 227 | memcpy(&option[OPT_DATA], &data, 4); |
182 | return add_option_string(optionptr, option); | 228 | return add_option_string(optionptr, option); |
diff --git a/networking/udhcp/options.h b/networking/udhcp/options.h index 4bf73a17e..c98aec48f 100644 --- a/networking/udhcp/options.h +++ b/networking/udhcp/options.h | |||
@@ -98,13 +98,13 @@ enum { | |||
98 | #define OPT_DATA 2 | 98 | #define OPT_DATA 2 |
99 | 99 | ||
100 | struct dhcp_option { | 100 | struct dhcp_option { |
101 | char opt_name[12]; | 101 | uint8_t flags; |
102 | char flags; | ||
103 | uint8_t code; | 102 | uint8_t code; |
104 | }; | 103 | }; |
105 | 104 | ||
106 | extern const struct dhcp_option dhcp_options[]; | 105 | extern const struct dhcp_option dhcp_options[]; |
107 | extern const unsigned char option_lengths[]; | 106 | extern const char dhcp_option_strings[]; |
107 | extern const uint8_t dhcp_option_lengths[]; | ||
108 | 108 | ||
109 | uint8_t *get_option(struct dhcpMessage *packet, int code); | 109 | uint8_t *get_option(struct dhcpMessage *packet, int code); |
110 | int end_option(uint8_t *optionptr); | 110 | int end_option(uint8_t *optionptr); |
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c index 33d96e687..8a188988e 100644 --- a/networking/udhcp/script.c +++ b/networking/udhcp/script.c | |||
@@ -33,7 +33,7 @@ static const uint8_t max_option_length[] = { | |||
33 | static inline int upper_length(int length, int opt_index) | 33 | static inline int upper_length(int length, int opt_index) |
34 | { | 34 | { |
35 | return max_option_length[opt_index] * | 35 | return max_option_length[opt_index] * |
36 | (length / option_lengths[opt_index]); | 36 | (length / dhcp_option_lengths[opt_index]); |
37 | } | 37 | } |
38 | 38 | ||
39 | 39 | ||
@@ -57,7 +57,7 @@ static int mton(uint32_t mask) | |||
57 | 57 | ||
58 | 58 | ||
59 | /* Allocate and fill with the text of option 'option'. */ | 59 | /* Allocate and fill with the text of option 'option'. */ |
60 | static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p) | 60 | static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p, const char *opt_name) |
61 | { | 61 | { |
62 | int len, type, optlen; | 62 | int len, type, optlen; |
63 | uint16_t val_u16; | 63 | uint16_t val_u16; |
@@ -68,10 +68,10 @@ static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p) | |||
68 | 68 | ||
69 | len = option[OPT_LEN - 2]; | 69 | len = option[OPT_LEN - 2]; |
70 | type = type_p->flags & TYPE_MASK; | 70 | type = type_p->flags & TYPE_MASK; |
71 | optlen = option_lengths[type]; | 71 | optlen = dhcp_option_lengths[type]; |
72 | 72 | ||
73 | dest = ret = xmalloc(upper_length(len, type) + strlen(type_p->opt_name) + 2); | 73 | dest = ret = xmalloc(upper_length(len, type) + strlen(opt_name) + 2); |
74 | dest += sprintf(ret, "%s=", type_p->opt_name); | 74 | dest += sprintf(ret, "%s=", opt_name); |
75 | 75 | ||
76 | for (;;) { | 76 | for (;;) { |
77 | switch (type) { | 77 | switch (type) { |
@@ -133,6 +133,7 @@ static char **fill_envp(struct dhcpMessage *packet) | |||
133 | int i, j; | 133 | int i, j; |
134 | char **envp; | 134 | char **envp; |
135 | char *var; | 135 | char *var; |
136 | const char *opt_name; | ||
136 | uint8_t *temp; | 137 | uint8_t *temp; |
137 | char over = 0; | 138 | char over = 0; |
138 | 139 | ||
@@ -171,11 +172,13 @@ static char **fill_envp(struct dhcpMessage *packet) | |||
171 | envp[j] = xmalloc(sizeof("ip=255.255.255.255")); | 172 | envp[j] = xmalloc(sizeof("ip=255.255.255.255")); |
172 | sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr); | 173 | sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr); |
173 | 174 | ||
174 | for (i = 0; dhcp_options[i].code; i++) { | 175 | opt_name = dhcp_option_strings; |
176 | i = 0; | ||
177 | while (*opt_name) { | ||
175 | temp = get_option(packet, dhcp_options[i].code); | 178 | temp = get_option(packet, dhcp_options[i].code); |
176 | if (!temp) | 179 | if (!temp) |
177 | continue; | 180 | goto next; |
178 | envp[j++] = alloc_fill_opts(temp, &dhcp_options[i]); | 181 | envp[j++] = alloc_fill_opts(temp, &dhcp_options[i], opt_name); |
179 | 182 | ||
180 | /* Fill in a subnet bits option for things like /24 */ | 183 | /* Fill in a subnet bits option for things like /24 */ |
181 | if (dhcp_options[i].code == DHCP_SUBNET) { | 184 | if (dhcp_options[i].code == DHCP_SUBNET) { |
@@ -183,6 +186,9 @@ static char **fill_envp(struct dhcpMessage *packet) | |||
183 | memcpy(&subnet, temp, 4); | 186 | memcpy(&subnet, temp, 4); |
184 | envp[j++] = xasprintf("mask=%d", mton(subnet)); | 187 | envp[j++] = xasprintf("mask=%d", mton(subnet)); |
185 | } | 188 | } |
189 | next: | ||
190 | opt_name += strlen(opt_name) + 1; | ||
191 | i++; | ||
186 | } | 192 | } |
187 | if (packet->siaddr) { | 193 | if (packet->siaddr) { |
188 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); | 194 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |