[136] | 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 "EthernetUdp2.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 | #define DEFAULT_LEASE (900) //default lease time in seconds
|
---|
| 49 |
|
---|
| 50 | #define DHCP_CHECK_NONE (0)
|
---|
| 51 | #define DHCP_CHECK_RENEW_FAIL (1)
|
---|
| 52 | #define DHCP_CHECK_RENEW_OK (2)
|
---|
| 53 | #define DHCP_CHECK_REBIND_FAIL (3)
|
---|
| 54 | #define DHCP_CHECK_REBIND_OK (4)
|
---|
| 55 |
|
---|
| 56 | enum
|
---|
| 57 | {
|
---|
| 58 | padOption = 0,
|
---|
| 59 | subnetMask = 1,
|
---|
| 60 | timerOffset = 2,
|
---|
| 61 | routersOnSubnet = 3,
|
---|
| 62 | /* timeServer = 4,
|
---|
| 63 | nameServer = 5,*/
|
---|
| 64 | dns = 6,
|
---|
| 65 | /*logServer = 7,
|
---|
| 66 | cookieServer = 8,
|
---|
| 67 | lprServer = 9,
|
---|
| 68 | impressServer = 10,
|
---|
| 69 | resourceLocationServer = 11,*/
|
---|
| 70 | hostName = 12,
|
---|
| 71 | /*bootFileSize = 13,
|
---|
| 72 | meritDumpFile = 14,*/
|
---|
| 73 | domainName = 15,
|
---|
| 74 | /*swapServer = 16,
|
---|
| 75 | rootPath = 17,
|
---|
| 76 | extentionsPath = 18,
|
---|
| 77 | IPforwarding = 19,
|
---|
| 78 | nonLocalSourceRouting = 20,
|
---|
| 79 | policyFilter = 21,
|
---|
| 80 | maxDgramReasmSize = 22,
|
---|
| 81 | defaultIPTTL = 23,
|
---|
| 82 | pathMTUagingTimeout = 24,
|
---|
| 83 | pathMTUplateauTable = 25,
|
---|
| 84 | ifMTU = 26,
|
---|
| 85 | allSubnetsLocal = 27,
|
---|
| 86 | broadcastAddr = 28,
|
---|
| 87 | performMaskDiscovery = 29,
|
---|
| 88 | maskSupplier = 30,
|
---|
| 89 | performRouterDiscovery = 31,
|
---|
| 90 | routerSolicitationAddr = 32,
|
---|
| 91 | staticRoute = 33,
|
---|
| 92 | trailerEncapsulation = 34,
|
---|
| 93 | arpCacheTimeout = 35,
|
---|
| 94 | ethernetEncapsulation = 36,
|
---|
| 95 | tcpDefaultTTL = 37,
|
---|
| 96 | tcpKeepaliveInterval = 38,
|
---|
| 97 | tcpKeepaliveGarbage = 39,
|
---|
| 98 | nisDomainName = 40,
|
---|
| 99 | nisServers = 41,
|
---|
| 100 | ntpServers = 42,
|
---|
| 101 | vendorSpecificInfo = 43,
|
---|
| 102 | netBIOSnameServer = 44,
|
---|
| 103 | netBIOSdgramDistServer = 45,
|
---|
| 104 | netBIOSnodeType = 46,
|
---|
| 105 | netBIOSscope = 47,
|
---|
| 106 | xFontServer = 48,
|
---|
| 107 | xDisplayManager = 49,*/
|
---|
| 108 | dhcpRequestedIPaddr = 50,
|
---|
| 109 | dhcpIPaddrLeaseTime = 51,
|
---|
| 110 | /*dhcpOptionOverload = 52,*/
|
---|
| 111 | dhcpMessageType = 53,
|
---|
| 112 | dhcpServerIdentifier = 54,
|
---|
| 113 | dhcpParamRequest = 55,
|
---|
| 114 | /*dhcpMsg = 56,
|
---|
| 115 | dhcpMaxMsgSize = 57,*/
|
---|
| 116 | dhcpT1value = 58,
|
---|
| 117 | dhcpT2value = 59,
|
---|
| 118 | /*dhcpClassIdentifier = 60,*/
|
---|
| 119 | dhcpClientIdentifier = 61,
|
---|
| 120 | endOption = 255
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | typedef struct _RIP_MSG_FIXED
|
---|
| 124 | {
|
---|
| 125 | uint8_t op;
|
---|
| 126 | uint8_t htype;
|
---|
| 127 | uint8_t hlen;
|
---|
| 128 | uint8_t hops;
|
---|
| 129 | uint32_t xid;
|
---|
| 130 | uint16_t secs;
|
---|
| 131 | uint16_t flags;
|
---|
| 132 | uint8_t ciaddr[4];
|
---|
| 133 | uint8_t yiaddr[4];
|
---|
| 134 | uint8_t siaddr[4];
|
---|
| 135 | uint8_t giaddr[4];
|
---|
| 136 | uint8_t chaddr[6];
|
---|
| 137 | }RIP_MSG_FIXED;
|
---|
| 138 |
|
---|
| 139 | class DhcpClass {
|
---|
| 140 |
|
---|
| 141 | private:
|
---|
| 142 | uint32_t _dhcpInitialTransactionId;
|
---|
| 143 | uint32_t _dhcpTransactionId;
|
---|
| 144 | uint8_t _dhcpMacAddr[6];
|
---|
| 145 | uint8_t _dhcpLocalIp[4];
|
---|
| 146 | uint8_t _dhcpSubnetMask[4];
|
---|
| 147 | uint8_t _dhcpGatewayIp[4];
|
---|
| 148 | uint8_t _dhcpDhcpServerIp[4];
|
---|
| 149 | uint8_t _dhcpDnsServerIp[4];
|
---|
| 150 | uint32_t _dhcpLeaseTime;
|
---|
| 151 | uint32_t _dhcpT1, _dhcpT2;
|
---|
| 152 | signed long _renewInSec;
|
---|
| 153 | signed long _rebindInSec;
|
---|
| 154 | signed long _lastCheck;
|
---|
| 155 | unsigned long _timeout;
|
---|
| 156 | unsigned long _responseTimeout;
|
---|
| 157 | unsigned long _secTimeout;
|
---|
| 158 | uint8_t _dhcp_state;
|
---|
| 159 | EthernetUDP _dhcpUdpSocket;
|
---|
| 160 | int request_DHCP_lease();
|
---|
| 161 | void reset_DHCP_lease();
|
---|
| 162 | void presend_DHCP();
|
---|
| 163 | void send_DHCP_MESSAGE(uint8_t, uint16_t);
|
---|
| 164 | void printByte(char *, uint8_t);
|
---|
| 165 |
|
---|
| 166 | uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
|
---|
| 167 | public:
|
---|
| 168 | IPAddress getLocalIp();
|
---|
| 169 | IPAddress getSubnetMask();
|
---|
| 170 | IPAddress getGatewayIp();
|
---|
| 171 | IPAddress getDhcpServerIp();
|
---|
| 172 | IPAddress getDnsServerIp();
|
---|
| 173 |
|
---|
| 174 | int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 5000);
|
---|
| 175 | int checkLease();
|
---|
| 176 | };
|
---|
| 177 |
|
---|
| 178 | #endif
|
---|