common.c 19 KB

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