NetBurner 3.5.6
PDF Version
dhcpinternals.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5/* @file dhcpinternals.h
6 * @brief IPv4 DHCP Client Header File for Internal Features
7 */
8
9/* @addtogroup groupDhcpIPv4 DHCP - IPv4 DHCP Client
10 * @ingroup groupDhcp
11 * \IncludeFileName \n\n
12 * @{
13 */
14
15#include <basictypes.h>
16#include <netinterface.h>
17#include <string.h>
18
19#ifndef _NB_DHCP_H
20#define _NB_DHCP_H
21
22#define OPT_SIZE 255 //< Size of options area
23#define DHCP_OPT_OFFS 236 //< Offset into DHCP Message for options data
24#define DHCP_CLIENT_PORT 68 //< Standard port assignment for client
25#define DHCP_SERVER_PORT 67 //< Standard port assignment for server
26
30#define DHCPDISCOVER 1 //< @b
31#define DHCPOFFER 2 //< @b
32#define DHCPREQUEST 3 //< @b
33#define DHCPDECLINE 4 //< @b
34#define DHCPACK 5 //< @b
35#define DHCPNAK 6 //< @b
36#define DHCPRELEASE 7 //< @b
37#define DHCPINFORM 8 //< @b
38#define DHCPERROR 99 //< @b
44#define BOOTREQUEST 1 //< @b
45#define BOOTREPLY 2 //< @b
51#define DHCPOPT_SUBNET_MASK 1 //< Client subnet mask
52#define DHCPOPT_ROUTER 3 //< Routers
53#define DHCPOPT_TIME_SERVER 4 //< Time Servers
54#define DHCPOPT_DNS_SERVER 6 //< DNS Server
55#define DHCPOPT_LOG_SERVER 7 //< Log Server
56#define DHCPOPT_HOST_NAME 12 //< Specifies name of client (Host Name Opt)
57#define DHCPOPT_DOMAIN_NAME 15 //< Domain Name for hosts
58#define DHCPOPT_NTP_SERVER 42 //< NTP Servers (different from tim)
59#define DHCPOPT_REQ_IPADDR4 50 //< Client requested IP address
60#define DHCPOPT_LEASE_TIME 51 //< IP addr lease time assigned by server
61#define DHCPOPT_MSG_TYPE 53 //< Type of DHCP message
62#define DHCPOPT_SERVER_ID 54 //< DHCP Server identifier
63#define DHCPOPT_PARAM_REQ 55 //< Parameter request list
64#define DHCPOPT_RENEW_TIME 58 //< # of seconds until client renewal state
65#define DHCPOPT_REBIND_TIME 59 //< # of seconds until client rebinding state
66#define DHCPOPT_CLIENT_ID 61 //< Unique client identifier
67#define DHCPOPT_TFTP_SERVER 66 //< TFTP Server name/IP
68#define DHCPOPT_BOOTFILE 67 //< TFTP Bootfile name
69#define DHCPOPT_SMTP_SERVER 69 //< SMTP Server name/IP
70
71#define DHCPOPT_END 255 //< End of options marker
77#define DHCPCOOKIE0 (uint8_t)99 //< @b
78#define DHCPCOOKIE1 (uint8_t)130 //< @b
79#define DHCPCOOKIE2 (uint8_t)83 //< @b
80#define DHCPCOOKIE3 (uint8_t)99 //< @b
86#define PDM_DHCPOFFER 1 //< Received DHCPOFFER message
87#define PDM_DHCPACK 2 //< Received DHCPACK message
88#define PDM_DHCPNAK 3 //< Received DHCPNAK message
89#define PDM_COMPLETE 4 //< DHCP configuration complete
90#define PDM_ERROR 10 //< Received unknown message
96typedef struct dhcp_msg
97{
98 uint8_t op; //< Message opcode; 1=bootrequest, 2=bootreply
99 uint8_t htype; //< Hardware address type; 1=10Mb ethernet
100 uint8_t hlen; //< Hardware address length; 6=10Mb ethernet
101 uint8_t hops; //< Client sets this to 0
102 beuint32_t xid; //< Transaction ID; random number
103 beuint16_t secs; //< Seconds elapsed since client request
104 beuint16_t flags; //< @b
105 IPADDR4 ciaddr; //< Clinet IP address if renew; filled by client
106 IPADDR4 yiaddr; //< Assigned client IP address
107 IPADDR4 siaddr; //< IP address of next server to use in bootstrap
108 IPADDR4 giaddr; //< Relay agent IP address (Gateway)
109 uint8_t chaddr[16]; //< Client hardware address
110 uint8_t sname[64]; //< Optional server host name
111 uint8_t file[128]; //< Boot file name, null terminated string
112 uint8_t options[OPT_SIZE]; //< Optional parameters field
113} __attribute__((packed)) DHCP_MESSAGE;
116class InterfaceBlock;
117
121class DHCPMessage
122{
123 private:
124 DHCP_MESSAGE Msg;
125
126 public:
127 // constructors
128 DHCPMessage();
129 DHCPMessage(puint8_t pData);
130
131 // member functions
132 void SetOp(uint8_t n) { Msg.op = n; }
133
134 uint8_t GetMsgType() { return msg_type; }
135
136 void SetHtype(uint8_t n) { Msg.htype = n; }
137 void SetHlen(uint8_t n) { Msg.hlen = n; }
138 void SetHops(uint8_t n) { Msg.hops = n; }
139
140 void SetXid(uint32_t n) { Msg.xid = n; }
141
142 void SetRandomXid(const MACADR &ma);
143
144 void SetSecs(uint16_t n) { Msg.secs = n; }
145 void SetFlags(uint16_t n) { Msg.flags = n; }
146 void SetCiaddr(IPADDR4 i) { Msg.ciaddr = i; }
147 void SetYiaddr(IPADDR4 i) { Msg.yiaddr = i; }
148 void SetSiaddr(IPADDR4 i) { Msg.siaddr = i; }
149 void SetGiaddr(IPADDR4 i) { Msg.giaddr = i; }
150 void SetChaddr(MACADR ma)
151 {
152 memset(Msg.chaddr, 0x0, 16);
153 for (int i = 0; i < 6; i++)
154 {
155 Msg.chaddr[i] = ma.GetByte(i);
156 }
157 }
158
159 void SetSname(char *s) { memcpy(Msg.sname, s, 64); }
160 void SetFile(char *s) { memcpy(Msg.file, s, 128); }
161 void SetOptions(char *s) { memcpy(Msg.options, s, OPT_SIZE); }
162
163 void SetOptionsIp(int &offset, IPADDR4 &ip4)
164 {
165 uint32_t u32 = (uint32_t)ip4;
166 Msg.options[offset++] = ((u32 >> 24) & 0xff);
167 Msg.options[offset++] = ((u32 >> 16) & 0xff);
168 Msg.options[offset++] = ((u32 >> 8) & 0xff);
169 Msg.options[offset++] = (u32 & 0xff);
170 }
171
172 void SetOptions(int i, uint8_t n) { Msg.options[i] = n; }
173
174 uint32_t GetXid() { return Msg.xid; }
175 uint16_t GetSecs() { return Msg.secs; }
176 uint16_t GetFlags() { return Msg.flags; }
177 IPADDR4 GetCiaddr() { return Msg.ciaddr; }
178 IPADDR4 GetYiaddr() { return Msg.yiaddr; }
179 IPADDR4 GetGiaddr() { return Msg.giaddr; }
180 IPADDR4 GetSiaddr() { return Msg.siaddr; }
181 uint8_t GetChaddr(int i) { return Msg.chaddr[i]; }
182
183 puint8_t GetDataPtr() { return (uint8_t *)(&Msg); }
184 int GetDataLen() { return sizeof(DHCP_MESSAGE); }
185 void ShowMsg(); //< Displays DHCP message for debug purposes
186 void CreateReply(uint32_t xid, uint32_t yip); //< Sets the message as a blank BOOTP reply
187 void ClearOptions(); //< Clears options memory space and adds cookie
188 void ResetBytes(); //< Clear chaddr, sname, file, and option fields
189 puint8_t GetOptionData(uint8_t code); //< Returns option data for specified op code
190 puint8_t GetOptionData(uint8_t code, int &length);
191 void SendMsg(IPADDR4 IpAddr, InterfaceBlock *pIfb); //< Send a DHPC message as a UDP packet
192 void SendServerMsg(IPADDR4 IpAddr, InterfaceBlock *pIfb); //< Send a DHPC message as a UDP packet, as server
193 BOOL MsgForInterface(InterfaceBlock *pIfb);
194 uint8_t msg_type;
195};
201void CreateDhcpDiscoverMsg(DHCPMessage &NewMsg, InterfaceBlock *pIfb);
202void CreateDhcpRequestMsg(DHCPMessage &OfferMsg, DHCPMessage &NewMsg, InterfaceBlock *pIfb);
203void CreateDhcpReleaseMsg(DHCPMessage &NewMsg, InterfaceBlock *pIfb);
204void ShowDhcpConfig(); //< Show DHCP configuration paramters
205void UpdateIPRuntimeVars(BOOLEAN release); //< Update DHCP & IP runtime vars
206void DHCPTimer(); //< Tracks lease time
207int ExecDHCPClient(int DhcpState); //< Function that does the work
208BOOLEAN ValidDhcpMsg(puint8_t pData); //< Verify the UDP packet contains a DHCP message
209BOOLEAN DhcpConfig(DHCPMessage &Msg); //< Configure client from DHCPACK message
215extern void (*AddDhcpOptionsFunc)(DHCPMessage &NewMsg, int &opt, int MsgType);
216extern void (*AddDhcpFieldsFunc)(DHCPMessage &NewMsg, int &opt, int MsgType);
217extern void (*ParseDhcpOptions)(DHCPMessage &Msg);
220#endif
221
222/* @} */
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
Network interface configuration block class for interface control and configuration.
Definition netinterface.h:245
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
uint8_t GetByte(int n) const
Get the nth byte of the MAC.
Definition nettypes.h:91
NetBurner Network Interface Header File.