Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dhcp_common.h Source File

dhcp_common.h

Go to the documentation of this file.
00001 /**
00002  * @file dhcp_common.h
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _DHCP_COMMON_H
00030 #define _DHCP_COMMON_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "core/ethernet.h"
00035 #include "ipv4/ipv4.h"
00036 
00037 //UDP ports used by DHCP servers and clients
00038 #define DHCP_SERVER_PORT 67
00039 #define DHCP_CLIENT_PORT 68
00040 
00041 //Minimum size of DHCP messages
00042 #define DHCP_MIN_MSG_SIZE 300
00043 //Maximum size of DHCP messages
00044 #define DHCP_MAX_MSG_SIZE 548
00045 
00046 //Hardware type
00047 #define DHCP_HARDWARE_TYPE_ETH 1
00048 //DHCP magic cookie
00049 #define DHCP_MAGIC_COOKIE 0x63825363
00050 //Infinite lifetime representation
00051 #define DHCP_INFINITE_TIME 0xFFFFFFFF
00052 
00053 
00054 /**
00055  * @brief DHCP opcodes
00056  **/
00057 
00058 typedef enum
00059 {
00060    DHCP_OPCODE_BOOTREQUEST = 1,
00061    DHCP_OPCODE_BOOTREPLY   = 2
00062 } DhcpOpcode;
00063 
00064 
00065 /**
00066  * @brief DHCP flags
00067  **/
00068 
00069 typedef enum
00070 {
00071    DHCP_FLAG_UNICAST   = 0x0000,
00072    DHCP_FLAG_BROADCAST = 0x8000
00073 } DhcpFlags;
00074 
00075 
00076 /**
00077  * @brief DHCP message types
00078  **/
00079 
00080 typedef enum
00081 {
00082    DHCP_MESSAGE_TYPE_DISCOVER = 1,
00083    DHCP_MESSAGE_TYPE_OFFER    = 2,
00084    DHCP_MESSAGE_TYPE_REQUEST  = 3,
00085    DHCP_MESSAGE_TYPE_DECLINE  = 4,
00086    DHCP_MESSAGE_TYPE_ACK      = 5,
00087    DHCP_MESSAGE_TYPE_NAK      = 6,
00088    DHCP_MESSAGE_TYPE_RELEASE  = 7,
00089    DHCP_MESSAGE_TYPE_INFORM   = 8
00090 } DhcpMessageType;
00091 
00092 
00093 /**
00094  * @brief DHCP option codes
00095  **/
00096 
00097 typedef enum
00098 {
00099    DHCP_OPT_PAD                          = 0,
00100    DHCP_OPT_SUBNET_MASK                  = 1,
00101    DHCP_OPT_TIME_OFFSET                  = 2,
00102    DHCP_OPT_ROUTER                       = 3,
00103    DHCP_OPT_TIME_SERVER                  = 4,
00104    DHCP_OPT_NAME_SERVER                  = 5,
00105    DHCP_OPT_DNS_SERVER                   = 6,
00106    DHCP_OPT_LOG_SERVER                   = 7,
00107    DHCP_OPT_COOKIE_SERVER                = 8,
00108    DHCP_OPT_LPR_SERVER                   = 9,
00109    DHCP_OPT_IMPRESS_SERVER               = 10,
00110    DHCP_OPT_RESOURCE_LOCATION_SERVER     = 11,
00111    DHCP_OPT_HOST_NAME                    = 12,
00112    DHCP_OPT_BOOT_FILE_SIZE               = 13,
00113    DHCP_OPT_MERIT_DUMP_FILE              = 14,
00114    DHCP_OPT_DOMAIN_NAME                  = 15,
00115    DHCP_OPT_SWAP_SERVER                  = 16,
00116    DHCP_OPT_ROOT_PATH                    = 17,
00117    DHCP_OPT_EXTENSIONS_PATH              = 18,
00118    DHCP_OPT_IP_FORWARDING                = 19,
00119    DHCP_OPT_NON_LOCAL_SOURCE_ROUTING     = 20,
00120    DHCP_OPT_POLICY_FILTER                = 21,
00121    DHCP_OPT_MAX_DATAGRAM_REASSEMBLY_SIZE = 22,
00122    DHCP_OPT_DEFAULT_IP_TTL               = 23,
00123    DHCP_OPT_PATH_MTU_AGING_TIMEOUT       = 24,
00124    DHCP_OPT_PATH_MTU_PLATEAU_TABLE       = 25,
00125    DHCP_OPT_INTERFACE_MTU                = 26,
00126    DHCP_OPT_ALL_SUBNETS_ARE_LOCAL        = 27,
00127    DHCP_OPT_BROADCAST_ADDRESS            = 28,
00128    DHCP_OPT_PERFORM_MASK_DISCOVERY       = 29,
00129    DHCP_OPT_MASK_SUPPLIER                = 30,
00130    DHCP_OPT_PERFORM_ROUTER_DISCOVERY     = 31,
00131    DHCP_OPT_ROUTER_SOLICITATION_ADDRESS  = 32,
00132    DHCP_OPT_STATIC_ROUTE                 = 33,
00133    DHCP_OPT_TRAILER_ENCAPSULATION        = 34,
00134    DHCP_OPT_ARP_CACHE_TIMEOUT            = 35,
00135    DHCP_OPT_ETHERNET_ENCAPSULATION       = 36,
00136    DHCP_OPT_TCP_DEFAULT_TTL              = 37,
00137    DHCP_OPT_TCP_KEEPALIVE_INTERVAL       = 38,
00138    DHCP_OPT_TCP_KEEPALIVE_GARBAGE        = 39,
00139    DHCP_OPT_NIS_DOMAIN                   = 40,
00140    DHCP_OPT_NIS_SERVER                   = 41,
00141    DHCP_OPT_NTP_SERVER                   = 42,
00142    DHCP_OPT_VENDOR_SPECIFIC_INFO         = 43,
00143    DHCP_OPT_NETBIOS_NBNS_SERVER          = 44,
00144    DHCP_OPT_NETBIOS_NBDD_SERVER          = 45,
00145    DHCP_OPT_NETBIOS_NODE_TYPE            = 46,
00146    DHCP_OPT_NETBIOS_SCOPE                = 47,
00147    DHCP_OPT_X11_FONT_SERVER              = 48,
00148    DHCP_OPT_X11_DISPLAY_MANAGER          = 49,
00149    DHCP_OPT_REQUESTED_IP_ADDRESS         = 50,
00150    DHCP_OPT_IP_ADDRESS_LEASE_TIME        = 51,
00151    DHCP_OPT_OPTION_OVERLOAD              = 52,
00152    DHCP_OPT_DHCP_MESSAGE_TYPE            = 53,
00153    DHCP_OPT_SERVER_IDENTIFIER            = 54,
00154    DHCP_OPT_PARAM_REQUEST_LIST           = 55,
00155    DHCP_OPT_MESSAGE                      = 56,
00156    DHCP_OPT_MAX_DHCP_MESSAGE_SIZE        = 57,
00157    DHCP_OPT_RENEWAL_TIME_VALUE           = 58,
00158    DHCP_OPT_REBINDING_TIME_VALUE         = 59,
00159    DHCP_OPT_VENDOR_CLASS_IDENTIFIER      = 60,
00160    DHCP_OPT_CLIENT_IDENTIFIER            = 61,
00161    DHCP_OPT_NISP_DOMAIN                  = 64,
00162    DHCP_OPT_NISP_SERVER                  = 65,
00163    DHCP_OPT_TFTP_SERVER_NAME             = 66,
00164    DHCP_OPT_BOOTFILE_NAME                = 67,
00165    DHCP_OPT_MOBILE_IP_HOME_AGENT         = 68,
00166    DHCP_OPT_SMTP_SERVER                  = 69,
00167    DHCP_OPT_POP3_SERVER                  = 70,
00168    DHCP_OPT_NNTP_SERVER                  = 71,
00169    DHCP_OPT_DEFAULT_WWW_SERVER           = 72,
00170    DHCP_OPT_DEFAULT_FINGER_SERVER        = 73,
00171    DHCP_OPT_DEFAULT_IRC_SERVER           = 74,
00172    DHCP_OPT_STREETTALK_SERVER            = 75,
00173    DHCP_OPT_STDA_SERVER                  = 76,
00174    DHCP_OPT_RAPID_COMMIT                 = 80,
00175    DHCP_OPT_END                          = 255
00176 } DhcpOptionCode;
00177 
00178 
00179 //CodeWarrior or Win32 compiler?
00180 #if defined(__CWCC__) || defined(_WIN32)
00181    #pragma pack(push, 1)
00182 #endif
00183 
00184 
00185 /**
00186  * @brief DHCP message
00187  **/
00188 
00189 typedef __start_packed struct
00190 {
00191    uint8_t op;           //0
00192    uint8_t htype;        //1
00193    uint8_t hlen;         //2
00194    uint8_t hops;         //3
00195    uint32_t xid;         //4-7
00196    uint16_t secs;        //8-9
00197    uint16_t flags;       //10-11
00198    Ipv4Addr ciaddr;      //12-15
00199    Ipv4Addr yiaddr;      //16-19
00200    Ipv4Addr siaddr;      //20-23
00201    Ipv4Addr giaddr;      //24-27
00202    MacAddr chaddr;       //28-33
00203    uint8_t unused[10];   //34-43
00204    uint8_t sname[64];    //44-107
00205    uint8_t file[128];    //108-235
00206    uint32_t magicCookie; //236-239
00207    uint8_t options[];    //240
00208 } __end_packed DhcpMessage;
00209 
00210 
00211 /**
00212  * @brief DHCP option
00213  **/
00214 
00215 typedef __start_packed struct
00216 {
00217    uint8_t code;    //0
00218    uint8_t length;  //1
00219    uint8_t value[]; //2
00220 } __end_packed DhcpOption;
00221 
00222 
00223 //CodeWarrior or Win32 compiler?
00224 #if defined(__CWCC__) || defined(_WIN32)
00225    #pragma pack(pop)
00226 #endif
00227 
00228 
00229 //DHCP related functions
00230 void dhcpAddOption(DhcpMessage *message, uint8_t optionCode,
00231    const void *optionValue, size_t optionLength);
00232 
00233 DhcpOption *dhcpGetOption(const DhcpMessage *message,
00234    size_t length, uint8_t optionCode);
00235 
00236 #endif
00237