Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame^] | 2 | /* |
| 3 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame^] | 5 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 6 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 9 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 10 | unsigned dhcp_verbose; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 11 | #endif |
| 12 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13 | const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = { |
| 14 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 15 | }; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame^] | 16 | |
| 17 | /* Supported options are easily added here. |
| 18 | * See RFC2132 for more options. |
| 19 | * OPTION_REQ: these options are requested by udhcpc (unless -o). |
| 20 | */ |
| 21 | const struct dhcp_option dhcp_options[] = { |
| 22 | /* flags code */ |
| 23 | { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ |
| 24 | { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ |
| 25 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ |
| 26 | // { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ |
| 27 | // { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ |
| 28 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ |
| 29 | // { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ |
| 30 | // { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ |
| 31 | { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ |
| 32 | { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ |
| 33 | { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */ |
| 34 | { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ |
| 35 | { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */ |
| 36 | { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */ |
| 37 | { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ |
| 38 | { OPTION_U16 , 0x1a }, /* DHCP_MTU */ |
| 39 | { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ |
| 40 | { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */ |
| 41 | { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */ |
| 42 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ |
| 43 | { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ |
| 44 | { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ |
| 45 | { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ |
| 46 | { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */ |
| 47 | //TODO: must be combined with 'sname' and 'file' handling: |
| 48 | { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */ |
| 49 | { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */ |
| 50 | //TODO: not a string, but a set of LASCII strings: |
| 51 | // { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */ |
| 52 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 53 | { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */ |
| 54 | #endif |
| 55 | { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */ |
| 56 | { OPTION_STRING , 0xfc }, /* DHCP_WPAD */ |
| 57 | |
| 58 | /* Options below have no match in dhcp_option_strings[], |
| 59 | * are not passed to dhcpc scripts, and cannot be specified |
| 60 | * with "option XXX YYY" syntax in dhcpd config file. |
| 61 | * These entries are only used internally by udhcp[cd] |
| 62 | * to correctly encode options into packets. |
| 63 | */ |
| 64 | |
| 65 | { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ |
| 66 | { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ |
| 67 | { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ |
| 68 | { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ |
| 69 | //FIXME: handling of this option is not exactly correct: |
| 70 | { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ |
| 71 | { 0, 0 } /* zeroed terminating entry */ |
| 72 | }; |
| 73 | |
| 74 | /* Used for converting options from incoming packets to env variables |
| 75 | * for udhcpc stript, and for setting options for udhcpd via |
| 76 | * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file. |
| 77 | */ |
| 78 | /* Must match dhcp_options[] order */ |
| 79 | const char dhcp_option_strings[] ALIGN1 = |
| 80 | "subnet" "\0" /* DHCP_SUBNET */ |
| 81 | "timezone" "\0" /* DHCP_TIME_OFFSET */ |
| 82 | "router" "\0" /* DHCP_ROUTER */ |
| 83 | // "timesrv" "\0" /* DHCP_TIME_SERVER */ |
| 84 | // "namesrv" "\0" /* DHCP_NAME_SERVER */ |
| 85 | "dns" "\0" /* DHCP_DNS_SERVER */ |
| 86 | // "logsrv" "\0" /* DHCP_LOG_SERVER */ |
| 87 | // "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ |
| 88 | "lprsrv" "\0" /* DHCP_LPR_SERVER */ |
| 89 | "hostname" "\0" /* DHCP_HOST_NAME */ |
| 90 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ |
| 91 | "domain" "\0" /* DHCP_DOMAIN_NAME */ |
| 92 | "swapsrv" "\0" /* DHCP_SWAP_SERVER */ |
| 93 | "rootpath" "\0" /* DHCP_ROOT_PATH */ |
| 94 | "ipttl" "\0" /* DHCP_IP_TTL */ |
| 95 | "mtu" "\0" /* DHCP_MTU */ |
| 96 | "broadcast" "\0" /* DHCP_BROADCAST */ |
| 97 | "nisdomain" "\0" /* DHCP_NIS_DOMAIN */ |
| 98 | "nissrv" "\0" /* DHCP_NIS_SERVER */ |
| 99 | "ntpsrv" "\0" /* DHCP_NTP_SERVER */ |
| 100 | "wins" "\0" /* DHCP_WINS_SERVER */ |
| 101 | "lease" "\0" /* DHCP_LEASE_TIME */ |
| 102 | "serverid" "\0" /* DHCP_SERVER_ID */ |
| 103 | "message" "\0" /* DHCP_ERR_MESSAGE */ |
| 104 | "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */ |
| 105 | "bootfile" "\0" /* DHCP_BOOT_FILE */ |
| 106 | // "userclass" "\0" /* DHCP_USER_CLASS */ |
| 107 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 108 | "search" "\0" /* DHCP_DOMAIN_SEARCH */ |
| 109 | #endif |
| 110 | // "staticroutes" is only used to set udhcpc environment, it doesn't work |
| 111 | // in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet |
| 112 | // by "string->option" conversion code: |
| 113 | "staticroutes" "\0"/* DHCP_STATIC_ROUTES */ |
| 114 | "wpad" "\0" /* DHCP_WPAD */ |
| 115 | ; |
| 116 | |
| 117 | /* Lengths of the different option types */ |
| 118 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
| 119 | [OPTION_IP] = 4, |
| 120 | [OPTION_IP_PAIR] = 8, |
| 121 | // [OPTION_BOOLEAN] = 1, |
| 122 | [OPTION_STRING] = 1, |
| 123 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 124 | [OPTION_STR1035] = 1, |
| 125 | #endif |
| 126 | [OPTION_U8] = 1, |
| 127 | [OPTION_U16] = 2, |
| 128 | // [OPTION_S16] = 2, |
| 129 | [OPTION_U32] = 4, |
| 130 | [OPTION_S32] = 4, |
| 131 | /* Just like OPTION_STRING, we use minimum length here */ |
| 132 | [OPTION_STATIC_ROUTES] = 5, |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
| 137 | static void log_option(const char *pfx, const uint8_t *opt) |
| 138 | { |
| 139 | if (dhcp_verbose >= 2) { |
| 140 | char buf[256 * 2 + 2]; |
| 141 | *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; |
| 142 | bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); |
| 143 | } |
| 144 | } |
| 145 | #else |
| 146 | # define log_option(pfx, opt) ((void)0) |
| 147 | #endif |
| 148 | |
| 149 | /* get an option with bounds checking (warning, result is not aligned). */ |
| 150 | uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code) |
| 151 | { |
| 152 | uint8_t *optionptr; |
| 153 | int len; |
| 154 | int rem; |
| 155 | int overload = 0; |
| 156 | enum { |
| 157 | FILE_FIELD101 = FILE_FIELD * 0x101, |
| 158 | SNAME_FIELD101 = SNAME_FIELD * 0x101, |
| 159 | }; |
| 160 | |
| 161 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
| 162 | optionptr = packet->options; |
| 163 | rem = sizeof(packet->options); |
| 164 | while (1) { |
| 165 | if (rem <= 0) { |
| 166 | bb_error_msg("bad packet, malformed option field"); |
| 167 | return NULL; |
| 168 | } |
| 169 | if (optionptr[OPT_CODE] == DHCP_PADDING) { |
| 170 | rem--; |
| 171 | optionptr++; |
| 172 | continue; |
| 173 | } |
| 174 | if (optionptr[OPT_CODE] == DHCP_END) { |
| 175 | if ((overload & FILE_FIELD101) == FILE_FIELD) { |
| 176 | /* can use packet->file, and didn't look at it yet */ |
| 177 | overload |= FILE_FIELD101; /* "we looked at it" */ |
| 178 | optionptr = packet->file; |
| 179 | rem = sizeof(packet->file); |
| 180 | continue; |
| 181 | } |
| 182 | if ((overload & SNAME_FIELD101) == SNAME_FIELD) { |
| 183 | /* can use packet->sname, and didn't look at it yet */ |
| 184 | overload |= SNAME_FIELD101; /* "we looked at it" */ |
| 185 | optionptr = packet->sname; |
| 186 | rem = sizeof(packet->sname); |
| 187 | continue; |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | len = 2 + optionptr[OPT_LEN]; |
| 192 | rem -= len; |
| 193 | if (rem < 0) |
| 194 | continue; /* complain and return NULL */ |
| 195 | |
| 196 | if (optionptr[OPT_CODE] == code) { |
| 197 | log_option("Option found", optionptr); |
| 198 | return optionptr + OPT_DATA; |
| 199 | } |
| 200 | |
| 201 | if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) { |
| 202 | overload |= optionptr[OPT_DATA]; |
| 203 | /* fall through */ |
| 204 | } |
| 205 | optionptr += len; |
| 206 | } |
| 207 | |
| 208 | /* log3 because udhcpc uses it a lot - very noisy */ |
| 209 | log3("Option 0x%02x not found", code); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | /* return the position of the 'end' option (no bounds checking) */ |
| 214 | int FAST_FUNC udhcp_end_option(uint8_t *optionptr) |
| 215 | { |
| 216 | int i = 0; |
| 217 | |
| 218 | while (optionptr[i] != DHCP_END) { |
| 219 | if (optionptr[i] != DHCP_PADDING) |
| 220 | i += optionptr[i + OPT_LEN] + OPT_DATA-1; |
| 221 | i++; |
| 222 | } |
| 223 | return i; |
| 224 | } |
| 225 | |
| 226 | /* Add an option (supplied in binary form) to the options. |
| 227 | * Option format: [code][len][data1][data2]..[dataLEN] |
| 228 | */ |
| 229 | void FAST_FUNC udhcp_add_binary_option(uint8_t *optionptr, uint8_t *addopt) |
| 230 | { |
| 231 | unsigned len; |
| 232 | unsigned end = udhcp_end_option(optionptr); |
| 233 | |
| 234 | /* end position + option code/length + addopt length + end option */ |
| 235 | len = OPT_DATA + addopt[OPT_LEN]; |
| 236 | if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) { |
| 237 | bb_error_msg("option 0x%02x did not fit into the packet", |
| 238 | addopt[OPT_CODE]); |
| 239 | return; |
| 240 | } |
| 241 | log_option("Adding option", addopt); |
| 242 | memcpy(optionptr + end, addopt, len); |
| 243 | optionptr[end + len] = DHCP_END; |
| 244 | } |
| 245 | |
| 246 | /* Add a one to four byte option to a packet */ |
| 247 | void FAST_FUNC udhcp_add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) |
| 248 | { |
| 249 | const struct dhcp_option *dh; |
| 250 | |
| 251 | for (dh = dhcp_options; dh->code; dh++) { |
| 252 | if (dh->code == code) { |
| 253 | uint8_t option[6], len; |
| 254 | |
| 255 | option[OPT_CODE] = code; |
| 256 | len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK]; |
| 257 | option[OPT_LEN] = len; |
| 258 | if (BB_BIG_ENDIAN) |
| 259 | data <<= 8 * (4 - len); |
| 260 | /* Assignment is unaligned! */ |
| 261 | move_to_unaligned32(&option[OPT_DATA], data); |
| 262 | udhcp_add_binary_option(optionptr, option); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bb_error_msg("can't add option 0x%02x", code); |
| 268 | } |
| 269 | |
| 270 | /* Find option 'code' in opt_list */ |
| 271 | struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code) |
| 272 | { |
| 273 | while (opt_list && opt_list->data[OPT_CODE] < code) |
| 274 | opt_list = opt_list->next; |
| 275 | |
| 276 | if (opt_list && opt_list->data[OPT_CODE] == code) |
| 277 | return opt_list; |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | /* Parse string to IP in network order */ |
| 282 | int FAST_FUNC udhcp_str2nip(const char *str, void *arg) |
| 283 | { |
| 284 | len_and_sockaddr *lsa; |
| 285 | |
| 286 | lsa = host_and_af2sockaddr(str, 0, AF_INET); |
| 287 | if (!lsa) |
| 288 | return 0; |
| 289 | *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr; |
| 290 | free(lsa); |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | /* udhcp_str2optset: |
| 295 | * Parse string option representation to binary form |
| 296 | * and add it to opt_list |
| 297 | */ |
| 298 | /* helper: add an option to the opt_list */ |
| 299 | static NOINLINE void attach_option( |
| 300 | struct option_set **opt_list, |
| 301 | const struct dhcp_option *option, |
| 302 | char *buffer, |
| 303 | int length) |
| 304 | { |
| 305 | struct option_set *existing, *new, **curr; |
| 306 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 307 | char *allocated = NULL; |
| 308 | #endif |
| 309 | |
| 310 | existing = find_option(*opt_list, option->code); |
| 311 | if (!existing) { |
| 312 | log2("Attaching option %02x to list", option->code); |
| 313 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 314 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) { |
| 315 | /* reuse buffer and length for RFC1035-formatted string */ |
| 316 | allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length); |
| 317 | } |
| 318 | #endif |
| 319 | /* make a new option */ |
| 320 | new = xmalloc(sizeof(*new)); |
| 321 | new->data = xmalloc(length + OPT_DATA); |
| 322 | new->data[OPT_CODE] = option->code; |
| 323 | new->data[OPT_LEN] = length; |
| 324 | memcpy(new->data + OPT_DATA, buffer, length); |
| 325 | |
| 326 | curr = opt_list; |
| 327 | while (*curr && (*curr)->data[OPT_CODE] < option->code) |
| 328 | curr = &(*curr)->next; |
| 329 | |
| 330 | new->next = *curr; |
| 331 | *curr = new; |
| 332 | goto ret; |
| 333 | } |
| 334 | |
| 335 | if (option->flags & OPTION_LIST) { |
| 336 | unsigned old_len; |
| 337 | |
| 338 | /* add it to an existing option */ |
| 339 | log1("Attaching option %02x to existing member of list", option->code); |
| 340 | old_len = existing->data[OPT_LEN]; |
| 341 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 342 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) { |
| 343 | /* reuse buffer and length for RFC1035-formatted string */ |
| 344 | allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length); |
| 345 | } |
| 346 | #endif |
| 347 | if (old_len + length < 255) { |
| 348 | /* actually 255 is ok too, but adding a space can overlow it */ |
| 349 | |
| 350 | existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length); |
| 351 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_STRING) { |
| 352 | /* add space separator between STRING options in a list */ |
| 353 | existing->data[OPT_DATA + old_len] = ' '; |
| 354 | old_len++; |
| 355 | } |
| 356 | memcpy(existing->data + OPT_DATA + old_len, buffer, length); |
| 357 | existing->data[OPT_LEN] = old_len + length; |
| 358 | } /* else, ignore the data, we could put this in a second option in the future */ |
| 359 | } /* else, ignore the new data */ |
| 360 | |
| 361 | ret: ; |
| 362 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 363 | free(allocated); |
| 364 | #endif |
| 365 | } |
| 366 | |
| 367 | int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg) |
| 368 | { |
| 369 | struct option_set **opt_list = arg; |
| 370 | char *opt, *val, *endptr; |
| 371 | char *str; |
| 372 | const struct dhcp_option *option; |
| 373 | int retval, length, idx; |
| 374 | char buffer[8] ALIGNED(4); |
| 375 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 376 | uint32_t *result_u32 = (uint32_t *) buffer; |
| 377 | |
| 378 | /* Cheat, the only *const* str possible is "" */ |
| 379 | str = (char *) const_str; |
| 380 | opt = strtok(str, " \t="); |
| 381 | if (!opt) |
| 382 | return 0; |
| 383 | |
| 384 | idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */ |
| 385 | if (idx < 0) |
| 386 | return 0; |
| 387 | option = &dhcp_options[idx]; |
| 388 | |
| 389 | retval = 0; |
| 390 | do { |
| 391 | val = strtok(NULL, ", \t"); |
| 392 | if (!val) |
| 393 | break; |
| 394 | length = dhcp_option_lengths[option->flags & OPTION_TYPE_MASK]; |
| 395 | retval = 0; |
| 396 | opt = buffer; /* new meaning for variable opt */ |
| 397 | switch (option->flags & OPTION_TYPE_MASK) { |
| 398 | case OPTION_IP: |
| 399 | retval = udhcp_str2nip(val, buffer); |
| 400 | break; |
| 401 | case OPTION_IP_PAIR: |
| 402 | retval = udhcp_str2nip(val, buffer); |
| 403 | val = strtok(NULL, ", \t/-"); |
| 404 | if (!val) |
| 405 | retval = 0; |
| 406 | if (retval) |
| 407 | retval = udhcp_str2nip(val, buffer + 4); |
| 408 | break; |
| 409 | case OPTION_STRING: |
| 410 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 411 | case OPTION_STR1035: |
| 412 | #endif |
| 413 | length = strnlen(val, 254); |
| 414 | if (length > 0) { |
| 415 | opt = val; |
| 416 | retval = 1; |
| 417 | } |
| 418 | break; |
| 419 | // case OPTION_BOOLEAN: { |
| 420 | // static const char noyes[] ALIGN1 = "no\0yes\0"; |
| 421 | // buffer[0] = retval = index_in_strings(noyes, val); |
| 422 | // retval++; /* 0 - bad; 1: "no" 2: "yes" */ |
| 423 | // break; |
| 424 | // } |
| 425 | case OPTION_U8: |
| 426 | buffer[0] = strtoul(val, &endptr, 0); |
| 427 | retval = (endptr[0] == '\0'); |
| 428 | break; |
| 429 | /* htonX are macros in older libc's, using temp var |
| 430 | * in code below for safety */ |
| 431 | /* TODO: use bb_strtoX? */ |
| 432 | case OPTION_U16: { |
| 433 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 434 | *result_u16 = htons(tmp); |
| 435 | retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/); |
| 436 | break; |
| 437 | } |
| 438 | // case OPTION_S16: { |
| 439 | // long tmp = strtol(val, &endptr, 0); |
| 440 | // *result_u16 = htons(tmp); |
| 441 | // retval = (endptr[0] == '\0'); |
| 442 | // break; |
| 443 | // } |
| 444 | case OPTION_U32: { |
| 445 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 446 | *result_u32 = htonl(tmp); |
| 447 | retval = (endptr[0] == '\0'); |
| 448 | break; |
| 449 | } |
| 450 | case OPTION_S32: { |
| 451 | long tmp = strtol(val, &endptr, 0); |
| 452 | *result_u32 = htonl(tmp); |
| 453 | retval = (endptr[0] == '\0'); |
| 454 | break; |
| 455 | } |
| 456 | default: |
| 457 | break; |
| 458 | } |
| 459 | if (retval) |
| 460 | attach_option(opt_list, option, opt, length); |
| 461 | } while (retval && option->flags & OPTION_LIST); |
| 462 | |
| 463 | return retval; |
| 464 | } |