common.c 17 KB

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