Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dhcp_common.c Source File

dhcp_common.c

Go to the documentation of this file.
00001 /**
00002  * @file dhcp_common.c
00003  * @brief Functions common to DHCP client, server and BOOTP relay agent
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @section Description
00026  *
00027  * The Dynamic Host Configuration Protocol is used to provide configuration
00028  * parameters to hosts. Refer to the following RFCs for complete details:
00029  * - RFC 2131: Dynamic Host Configuration Protocol
00030  * - RFC 2132: DHCP Options and BOOTP Vendor Extensions
00031  * - RFC 4039: Rapid Commit Option for the DHCP version 4
00032  *
00033  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00034  * @version 1.7.6
00035  **/
00036 
00037 //Switch to the appropriate trace level
00038 #define TRACE_LEVEL DHCP_TRACE_LEVEL
00039 
00040 //Dependencies
00041 #include "core/net.h"
00042 #include "dhcp/dhcp_common.h"
00043 #include "debug.h"
00044 
00045 //Check TCP/IP stack configuration
00046 #if (IPV4_SUPPORT == ENABLED)
00047 
00048 
00049 /**
00050  * @brief Append an option to a DHCP message
00051  * @param[in] message Pointer to the DHCP message
00052  * @param[in] optionCode Option code
00053  * @param[in] optionValue Option value
00054  * @param[in] optionLength Length of the option value
00055  **/
00056 
00057 void dhcpAddOption(DhcpMessage *message, uint8_t optionCode,
00058    const void *optionValue, size_t optionLength)
00059 {
00060    size_t n;
00061    DhcpOption *option;
00062 
00063    //Point to the very first option
00064    n = 0;
00065 
00066    //Parse DHCP options
00067    while(1)
00068    {
00069       //Point to the current option
00070       option = (DhcpOption *) (message->options + n);
00071 
00072       //End option detected?
00073       if(option->code == DHCP_OPT_END)
00074          break;
00075 
00076       //Jump to next the next option
00077       n += sizeof(DhcpOption) + option->length;
00078    }
00079 
00080    //Sanity check
00081    if(optionLength <= UINT8_MAX)
00082    {
00083       //Point to the buffer where the option is to be written
00084       option = (DhcpOption *) (message->options + n);
00085 
00086       //Option code
00087       option->code = optionCode;
00088       //Option length
00089       option->length = (uint8_t) optionLength;
00090       //Option value
00091       memcpy(option->value, optionValue, optionLength);
00092 
00093       //Jump to next the next option
00094       n += sizeof(DhcpOption) + option->length;
00095 
00096       //Point to the buffer where the option is to be written
00097       option = (DhcpOption *) (message->options + n);
00098 
00099       //Always terminate the options field with 255
00100       option->code = DHCP_OPT_END;
00101    }
00102 }
00103 
00104 
00105 /**
00106  * @brief Find the specified option in a DHCP message
00107  * @param[in] message Pointer to the DHCP message
00108  * @param[in] length Length of the message
00109  * @param[in] optionCode Code of the option to find
00110  * @return If the specified option is found, a pointer to the corresponding
00111  *   option is returned. Otherwise NULL pointer is returned
00112  **/
00113 
00114 DhcpOption *dhcpGetOption(const DhcpMessage *message,
00115    size_t length, uint8_t optionCode)
00116 {
00117    uint_t i;
00118    DhcpOption *option;
00119 
00120    //Make sure the DHCP header is valid
00121    if(length < sizeof(DhcpMessage))
00122       return NULL;
00123    //Get the length of the options field
00124    length -= sizeof(DhcpMessage);
00125 
00126    //Parse DHCP options
00127    for(i = 0; i < length; i++)
00128    {
00129       //Point to the current option
00130       option = (DhcpOption *) (message->options + i);
00131 
00132       //Pad option detected?
00133       if(option->code == DHCP_OPT_PAD)
00134          continue;
00135       //End option detected?
00136       if(option->code == DHCP_OPT_END)
00137          break;
00138       //Check option length
00139       if((i + 1) >= length || (i + 1 + option->length) >= length)
00140          break;
00141 
00142       //Current option code matches the specified one?
00143       if(option->code == optionCode)
00144          return option;
00145 
00146       //Jump to the next option
00147       i += option->length + 1;
00148    }
00149 
00150    //Specified option code not found
00151    return NULL;
00152 }
00153 
00154 #endif
00155