Daniele Lacamera / PicoTCP-Experimental_CDC_ECM_Branch

Fork of PicoTCP by Daniele Lacamera

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pico_dhcp_common.c Source File

pico_dhcp_common.c

00001 /*********************************************************************
00002 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
00003 See LICENSE and COPYING for usage.
00004 
00005 .
00006 
00007 Authors: Frederik Van Slycken
00008 *********************************************************************/
00009 
00010 #include "pico_config.h"
00011 #include "pico_stack.h"
00012 #include "pico_dhcp_common.h"
00013 
00014 #if defined (PICO_SUPPORT_DHCPC) || defined (PICO_SUPPORT_DHCPD)
00015 //this function should only be used after you checked if the options are valid! otherwise it could read from bad memory!
00016 uint8_t dhcp_get_next_option(uint8_t *begin, uint8_t *data, int *len, uint8_t **nextopt)
00017 {
00018     uint8_t *p;
00019     uint8_t type;
00020     uint8_t opt_len;
00021 
00022     if (!begin)
00023         p = *nextopt;
00024     else
00025         p = begin;
00026 
00027     type = *p;
00028     *nextopt = ++p;
00029     if ((type == PICO_DHCPOPT_END) || (type == PICO_DHCPOPT_PAD)) {
00030         memset(data, 0, *len);
00031         len = 0;
00032         return type;
00033     }
00034     opt_len = *p;
00035     p++;
00036     if (*len > opt_len)
00037         *len = opt_len;
00038     memcpy(data, p, *len);
00039     *nextopt = p + opt_len;
00040     return type;
00041 }
00042 
00043 int is_options_valid(uint8_t *opt_buffer, int len)
00044 {
00045     uint8_t *p = opt_buffer;
00046     while (len > 0) {
00047         if (*p == PICO_DHCPOPT_END)
00048             return 1;
00049         else if (*p == PICO_DHCPOPT_PAD) {
00050             p++;
00051             len--;
00052         } else {
00053             uint8_t opt_len;
00054             p++;
00055             len--;
00056             if(len > 0) {
00057                 opt_len = *p;
00058                 p += opt_len + 1;
00059                 len -= opt_len;
00060             }else
00061                 return 0;
00062         }
00063     }
00064     return 0;
00065 }
00066 
00067 #endif