root / arduino-1.0 / libraries / Ethernet / Dhcp.h @ 58d82c77
History | View | Annotate | Download (3.58 KB)
1 |
// DHCP Library v0.3 - April 25, 2009
|
---|---|
2 |
// Author: Jordan Terrell - blog.jordanterrell.com
|
3 |
|
4 |
#ifndef Dhcp_h
|
5 |
#define Dhcp_h
|
6 |
|
7 |
#include "EthernetUdp.h" |
8 |
|
9 |
/* DHCP state machine. */
|
10 |
#define STATE_DHCP_START 0 |
11 |
#define STATE_DHCP_DISCOVER 1 |
12 |
#define STATE_DHCP_REQUEST 2 |
13 |
#define STATE_DHCP_LEASED 3 |
14 |
#define STATE_DHCP_REREQUEST 4 |
15 |
#define STATE_DHCP_RELEASE 5 |
16 |
|
17 |
#define DHCP_FLAGSBROADCAST 0x8000 |
18 |
|
19 |
/* UDP port numbers for DHCP */
|
20 |
#define DHCP_SERVER_PORT 67 /* from server to client */ |
21 |
#define DHCP_CLIENT_PORT 68 /* from client to server */ |
22 |
|
23 |
/* DHCP message OP code */
|
24 |
#define DHCP_BOOTREQUEST 1 |
25 |
#define DHCP_BOOTREPLY 2 |
26 |
|
27 |
/* DHCP message type */
|
28 |
#define DHCP_DISCOVER 1 |
29 |
#define DHCP_OFFER 2 |
30 |
#define DHCP_REQUEST 3 |
31 |
#define DHCP_DECLINE 4 |
32 |
#define DHCP_ACK 5 |
33 |
#define DHCP_NAK 6 |
34 |
#define DHCP_RELEASE 7 |
35 |
#define DHCP_INFORM 8 |
36 |
|
37 |
#define DHCP_HTYPE10MB 1 |
38 |
#define DHCP_HTYPE100MB 2 |
39 |
|
40 |
#define DHCP_HLENETHERNET 6 |
41 |
#define DHCP_HOPS 0 |
42 |
#define DHCP_SECS 0 |
43 |
|
44 |
#define MAGIC_COOKIE 0x63825363 |
45 |
#define MAX_DHCP_OPT 16 |
46 |
|
47 |
#define HOST_NAME "WIZnet" |
48 |
|
49 |
enum
|
50 |
{ |
51 |
padOption = 0,
|
52 |
subnetMask = 1,
|
53 |
timerOffset = 2,
|
54 |
routersOnSubnet = 3,
|
55 |
/* timeServer = 4,
|
56 |
nameServer = 5,*/
|
57 |
dns = 6,
|
58 |
/*logServer = 7,
|
59 |
cookieServer = 8,
|
60 |
lprServer = 9,
|
61 |
impressServer = 10,
|
62 |
resourceLocationServer = 11,*/
|
63 |
hostName = 12,
|
64 |
/*bootFileSize = 13,
|
65 |
meritDumpFile = 14,*/
|
66 |
domainName = 15,
|
67 |
/*swapServer = 16,
|
68 |
rootPath = 17,
|
69 |
extentionsPath = 18,
|
70 |
IPforwarding = 19,
|
71 |
nonLocalSourceRouting = 20,
|
72 |
policyFilter = 21,
|
73 |
maxDgramReasmSize = 22,
|
74 |
defaultIPTTL = 23,
|
75 |
pathMTUagingTimeout = 24,
|
76 |
pathMTUplateauTable = 25,
|
77 |
ifMTU = 26,
|
78 |
allSubnetsLocal = 27,
|
79 |
broadcastAddr = 28,
|
80 |
performMaskDiscovery = 29,
|
81 |
maskSupplier = 30,
|
82 |
performRouterDiscovery = 31,
|
83 |
routerSolicitationAddr = 32,
|
84 |
staticRoute = 33,
|
85 |
trailerEncapsulation = 34,
|
86 |
arpCacheTimeout = 35,
|
87 |
ethernetEncapsulation = 36,
|
88 |
tcpDefaultTTL = 37,
|
89 |
tcpKeepaliveInterval = 38,
|
90 |
tcpKeepaliveGarbage = 39,
|
91 |
nisDomainName = 40,
|
92 |
nisServers = 41,
|
93 |
ntpServers = 42,
|
94 |
vendorSpecificInfo = 43,
|
95 |
netBIOSnameServer = 44,
|
96 |
netBIOSdgramDistServer = 45,
|
97 |
netBIOSnodeType = 46,
|
98 |
netBIOSscope = 47,
|
99 |
xFontServer = 48,
|
100 |
xDisplayManager = 49,*/
|
101 |
dhcpRequestedIPaddr = 50,
|
102 |
dhcpIPaddrLeaseTime = 51,
|
103 |
/*dhcpOptionOverload = 52,*/
|
104 |
dhcpMessageType = 53,
|
105 |
dhcpServerIdentifier = 54,
|
106 |
dhcpParamRequest = 55,
|
107 |
/*dhcpMsg = 56,
|
108 |
dhcpMaxMsgSize = 57,*/
|
109 |
dhcpT1value = 58,
|
110 |
dhcpT2value = 59,
|
111 |
/*dhcpClassIdentifier = 60,*/
|
112 |
dhcpClientIdentifier = 61,
|
113 |
endOption = 255
|
114 |
}; |
115 |
|
116 |
typedef struct _RIP_MSG_FIXED |
117 |
{ |
118 |
uint8_t op; |
119 |
uint8_t htype; |
120 |
uint8_t hlen; |
121 |
uint8_t hops; |
122 |
uint32_t xid; |
123 |
uint16_t secs; |
124 |
uint16_t flags; |
125 |
uint8_t ciaddr[4];
|
126 |
uint8_t yiaddr[4];
|
127 |
uint8_t siaddr[4];
|
128 |
uint8_t giaddr[4];
|
129 |
uint8_t chaddr[6];
|
130 |
}RIP_MSG_FIXED; |
131 |
|
132 |
class DhcpClass { |
133 |
private:
|
134 |
uint32_t _dhcpInitialTransactionId; |
135 |
uint32_t _dhcpTransactionId; |
136 |
uint8_t _dhcpMacAddr[6];
|
137 |
uint8_t _dhcpLocalIp[4];
|
138 |
uint8_t _dhcpSubnetMask[4];
|
139 |
uint8_t _dhcpGatewayIp[4];
|
140 |
uint8_t _dhcpDhcpServerIp[4];
|
141 |
uint8_t _dhcpDnsServerIp[4];
|
142 |
EthernetUDP _dhcpUdpSocket; |
143 |
|
144 |
void presend_DHCP();
|
145 |
void send_DHCP_MESSAGE(uint8_t, uint16_t);
|
146 |
|
147 |
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); |
148 |
public:
|
149 |
IPAddress getLocalIp(); |
150 |
IPAddress getSubnetMask(); |
151 |
IPAddress getGatewayIp(); |
152 |
IPAddress getDhcpServerIp(); |
153 |
IPAddress getDnsServerIp(); |
154 |
|
155 |
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); |
156 |
}; |
157 |
|
158 |
#endif
|