Free (GPLv2) TCP/IP stack developed by TASS Belgium
Fork of PicoTCP by
Diff: modules/pico_dhcp_common.c
- Revision:
- 51:18637a3d071f
- Parent:
- 3:b4047e8a0123
diff -r c3b337c38feb -r 18637a3d071f modules/pico_dhcp_common.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/pico_dhcp_common.c Sat Aug 03 08:50:27 2013 +0000 @@ -0,0 +1,67 @@ +/********************************************************************* +PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. +See LICENSE and COPYING for usage. + +. + +Authors: Frederik Van Slycken +*********************************************************************/ + +#include "pico_config.h" +#include "pico_stack.h" +#include "pico_dhcp_common.h" + +#if defined (PICO_SUPPORT_DHCPC) || defined (PICO_SUPPORT_DHCPD) +//this function should only be used after you checked if the options are valid! otherwise it could read from bad memory! +uint8_t dhcp_get_next_option(uint8_t *begin, uint8_t *data, int *len, uint8_t **nextopt) +{ + uint8_t *p; + uint8_t type; + uint8_t opt_len; + + if (!begin) + p = *nextopt; + else + p = begin; + + type = *p; + *nextopt = ++p; + if ((type == PICO_DHCPOPT_END) || (type == PICO_DHCPOPT_PAD)) { + memset(data, 0, *len); + len = 0; + return type; + } + opt_len = *p; + p++; + if (*len > opt_len) + *len = opt_len; + memcpy(data, p, *len); + *nextopt = p + opt_len; + return type; +} + +int is_options_valid(uint8_t *opt_buffer, int len) +{ + uint8_t *p = opt_buffer; + while (len > 0) { + if (*p == PICO_DHCPOPT_END) + return 1; + else if (*p == PICO_DHCPOPT_PAD) { + p++; + len--; + } else { + uint8_t opt_len; + p++; + len--; + if(len > 0) { + opt_len = *p; + p += opt_len + 1; + len -= opt_len; + }else + return 0; + } + } + return 0; +} + +#endif