5 #include <basictypes.h> 6 #include <netinterface.h> 12 #define OPT_SIZE 255 // Size of options area 13 #define DHCP_OPT_OFFS 236 // Offset into DHCP Message for options data 14 #define DHCP_CLIENT_PORT 68 // Standard port assignment for client 15 #define DHCP_SERVER_PORT 67 // Standard port assignment for server 18 #define DHCPDISCOVER 1 33 #define DHCPOPT_SUBNET_MASK 1 // Client subnet mask 34 #define DHCPOPT_ROUTER 3 // Routers 35 #define DHCPOPT_TIME_SERVER 4 // Time Servers 36 #define DHCPOPT_DNS_SERVER 6 // DNS Server 37 #define DHCPOPT_LOG_SERVER 7 // Log Server 38 #define DHCPOPT_HOST_NAME 12 // Specifies name of client (Host Name Opt) 39 #define DHCPOPT_DOMAIN_NAME 15 // Domain Name for hosts 40 #define DHCPOPT_NTP_SERVER 42 // NTP Servers (different from tim) 41 #define DHCPOPT_REQ_IPADDR4 50 // Client requested IP address 42 #define DHCPOPT_LEASE_TIME 51 // IP addr lease time assigned by server 43 #define DHCPOPT_MSG_TYPE 53 // Type of DHCP message 44 #define DHCPOPT_SERVER_ID 54 // DHCP Server identifier 45 #define DHCPOPT_PARAM_REQ 55 // Parameter request list 46 #define DHCPOPT_RENEW_TIME 58 // # of seconds until client renewal state 47 #define DHCPOPT_REBIND_TIME 59 // # of seconds until client rebinding state 48 #define DHCPOPT_CLIENT_ID 61 // Unique client identifier 49 #define DHCPOPT_TFTP_SERVER 66 // TFTP Server name/IP 50 #define DHCPOPT_BOOTFILE 67 // TFTP Bootfile name 51 #define DHCPOPT_SMTP_SERVER 69 // SMTP Server name/IP 53 #define DHCPOPT_END 255 // End of options marker 55 #define DHCPCOOKIE0 (uint8_t)99 // DHCP message 4-byte cookie values 56 #define DHCPCOOKIE1 (uint8_t)130 57 #define DHCPCOOKIE2 (uint8_t)83 58 #define DHCPCOOKIE3 (uint8_t)99 70 typedef struct dhcp_msg
86 uint8_t options[OPT_SIZE];
87 } __attribute__((packed)) DHCP_MESSAGE;
100 DHCPMessage(puint8_t pData);
103 void SetOp(uint8_t n) { Msg.op = n; }
105 uint8_t GetMsgType() {
return msg_type; }
107 void SetHtype(uint8_t n) { Msg.htype = n; }
108 void SetHlen(uint8_t n) { Msg.hlen = n; }
109 void SetHops(uint8_t n) { Msg.hops = n; }
111 void SetXid(uint32_t n) { Msg.xid = n; }
113 void SetRandomXid(
const MACADR &ma);
115 void SetSecs(uint16_t n) { Msg.secs = n; }
116 void SetFlags(uint16_t n) { Msg.flags = n; }
117 void SetCiaddr(IPADDR4 i) { Msg.ciaddr = i; }
118 void SetYiaddr(IPADDR4 i) { Msg.yiaddr = i; }
119 void SetSiaddr(IPADDR4 i) { Msg.siaddr = i; }
120 void SetGiaddr(IPADDR4 i) { Msg.giaddr = i; }
121 void SetChaddr(MACADR ma)
123 memset(Msg.chaddr, 0x0, 16);
124 for (
int i = 0; i < 6; i++)
126 Msg.chaddr[i] = ma.GetByte(i);
130 void SetSname(
char *s) { memcpy(Msg.sname, s, 64); }
131 void SetFile(
char *s) { memcpy(Msg.file, s, 128); }
132 void SetOptions(
char *s) { memcpy(Msg.options, s, OPT_SIZE); }
134 void SetOptionsIp(
int &offset, IPADDR4 &ip4)
136 uint32_t u32 = (uint32_t)ip4;
137 Msg.options[offset++] = ((u32 >> 24) & 0xff);
138 Msg.options[offset++] = ((u32 >> 16) & 0xff);
139 Msg.options[offset++] = ((u32 >> 8) & 0xff);
140 Msg.options[offset++] = (u32 & 0xff);
143 void SetOptions(
int i, uint8_t n) { Msg.options[i] = n; }
145 uint32_t GetXid() {
return Msg.xid; }
146 uint16_t GetSecs() {
return Msg.secs; }
147 uint16_t GetFlags() {
return Msg.flags; }
148 IPADDR4 GetCiaddr() {
return Msg.ciaddr; }
149 IPADDR4 GetYiaddr() {
return Msg.yiaddr; }
150 IPADDR4 GetGiaddr() {
return Msg.giaddr; }
151 IPADDR4 GetSiaddr() {
return Msg.siaddr; }
152 uint8_t GetChaddr(
int i) {
return Msg.chaddr[i]; }
154 puint8_t GetDataPtr() {
return (uint8_t *)(&Msg); }
155 int GetDataLen() {
return sizeof(DHCP_MESSAGE); }
157 void CreateReply(uint32_t xid, uint32_t yip);
160 puint8_t GetOptionData(uint8_t code);
161 puint8_t GetOptionData(uint8_t code,
int &length);
162 void SendMsg(IPADDR4 IpAddr, InterfaceBlock *pIfb);
163 void SendServerMsg(IPADDR4 IpAddr, InterfaceBlock *pIfb);
164 BOOL MsgForInterface(InterfaceBlock *pIfb);
169 void CreateDhcpDiscoverMsg(DHCPMessage &NewMsg, InterfaceBlock *pIfb);
170 void CreateDhcpRequestMsg(DHCPMessage &OfferMsg, DHCPMessage &NewMsg, InterfaceBlock *pIfb);
171 void CreateDhcpReleaseMsg(DHCPMessage &NewMsg, InterfaceBlock *pIfb);
172 void ShowDhcpConfig();
173 void UpdateIPRuntimeVars(BOOLEAN release);
175 int ExecDHCPClient(
int DhcpState);
176 BOOLEAN ValidDhcpMsg(puint8_t pData);
177 BOOLEAN DhcpConfig(DHCPMessage &Msg);
180 extern void (*AddDhcpOptionsFunc)(DHCPMessage &NewMsg,
int &opt,
int MsgType);
181 extern void (*AddDhcpFieldsFunc)(DHCPMessage &NewMsg,
int &opt,
int MsgType);
182 extern void (*ParseDhcpOptions)(DHCPMessage &Msg);