mbed OS5
Fork of UIPEthernet by
Dhcp.h@9:e55652bed36c, 2017-08-06 (annotated)
- Committer:
- pilotak
- Date:
- Sun Aug 06 16:01:26 2017 +0000
- Revision:
- 9:e55652bed36c
- Parent:
- 8:4acb22344932
mBed OS5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:5350a66d5279 | 1 | // DHCP Library v0.3 - April 25, 2009 |
hudakz | 0:5350a66d5279 | 2 | // Author: Jordan Terrell - blog.jordanterrell.com |
hudakz | 0:5350a66d5279 | 3 | #ifndef Dhcp_h |
hudakz | 8:4acb22344932 | 4 | #define Dhcp_h |
hudakz | 0:5350a66d5279 | 5 | |
hudakz | 8:4acb22344932 | 6 | #include "UIPUdp.h" |
hudakz | 0:5350a66d5279 | 7 | |
hudakz | 0:5350a66d5279 | 8 | /* DHCP state machine. */ |
hudakz | 0:5350a66d5279 | 9 | |
hudakz | 8:4acb22344932 | 10 | #define STATE_DHCP_START 0 |
hudakz | 8:4acb22344932 | 11 | #define STATE_DHCP_DISCOVER 1 |
hudakz | 8:4acb22344932 | 12 | #define STATE_DHCP_REQUEST 2 |
hudakz | 8:4acb22344932 | 13 | #define STATE_DHCP_LEASED 3 |
hudakz | 8:4acb22344932 | 14 | #define STATE_DHCP_REREQUEST 4 |
hudakz | 8:4acb22344932 | 15 | #define STATE_DHCP_RELEASE 5 |
hudakz | 0:5350a66d5279 | 16 | |
hudakz | 8:4acb22344932 | 17 | #define DHCP_FLAGSBROADCAST 0x8000 |
hudakz | 0:5350a66d5279 | 18 | |
hudakz | 0:5350a66d5279 | 19 | /* UDP port numbers for DHCP */ |
hudakz | 0:5350a66d5279 | 20 | |
hudakz | 8:4acb22344932 | 21 | #define DHCP_SERVER_PORT 67 /* from server to client */ |
hudakz | 0:5350a66d5279 | 22 | |
hudakz | 8:4acb22344932 | 23 | #define DHCP_CLIENT_PORT 68 /* from client to server */ |
hudakz | 0:5350a66d5279 | 24 | |
hudakz | 0:5350a66d5279 | 25 | /* DHCP message OP code */ |
hudakz | 0:5350a66d5279 | 26 | |
hudakz | 8:4acb22344932 | 27 | #define DHCP_BOOTREQUEST 1 |
hudakz | 8:4acb22344932 | 28 | #define DHCP_BOOTREPLY 2 |
hudakz | 0:5350a66d5279 | 29 | |
hudakz | 0:5350a66d5279 | 30 | /* DHCP message type */ |
hudakz | 0:5350a66d5279 | 31 | |
hudakz | 8:4acb22344932 | 32 | #define DHCP_DISCOVER 1 |
hudakz | 8:4acb22344932 | 33 | #define DHCP_OFFER 2 |
hudakz | 8:4acb22344932 | 34 | #define DHCP_REQUEST 3 |
hudakz | 8:4acb22344932 | 35 | #define DHCP_DECLINE 4 |
hudakz | 8:4acb22344932 | 36 | #define DHCP_ACK 5 |
hudakz | 8:4acb22344932 | 37 | #define DHCP_NAK 6 |
hudakz | 8:4acb22344932 | 38 | #define DHCP_RELEASE 7 |
hudakz | 8:4acb22344932 | 39 | #define DHCP_INFORM 8 |
hudakz | 0:5350a66d5279 | 40 | |
hudakz | 8:4acb22344932 | 41 | #define DHCP_HTYPE10MB 1 |
hudakz | 8:4acb22344932 | 42 | #define DHCP_HTYPE100MB 2 |
hudakz | 0:5350a66d5279 | 43 | |
hudakz | 8:4acb22344932 | 44 | #define DHCP_HLENETHERNET 6 |
hudakz | 8:4acb22344932 | 45 | #define DHCP_HOPS 0 |
hudakz | 8:4acb22344932 | 46 | #define DHCP_SECS 0 |
hudakz | 0:5350a66d5279 | 47 | |
hudakz | 8:4acb22344932 | 48 | #define MAGIC_COOKIE 0x63825363 |
hudakz | 8:4acb22344932 | 49 | #define MAX_DHCP_OPT 16 |
hudakz | 0:5350a66d5279 | 50 | |
hudakz | 8:4acb22344932 | 51 | #define HOST_NAME "ENC28J" |
hudakz | 8:4acb22344932 | 52 | #define DEFAULT_LEASE (900) //default lease time in seconds |
hudakz | 0:5350a66d5279 | 53 | |
hudakz | 8:4acb22344932 | 54 | #define DHCP_CHECK_NONE (0) |
hudakz | 8:4acb22344932 | 55 | #define DHCP_CHECK_RENEW_FAIL (1) |
hudakz | 8:4acb22344932 | 56 | #define DHCP_CHECK_RENEW_OK (2) |
hudakz | 8:4acb22344932 | 57 | #define DHCP_CHECK_REBIND_FAIL (3) |
hudakz | 8:4acb22344932 | 58 | #define DHCP_CHECK_REBIND_OK (4) |
hudakz | 0:5350a66d5279 | 59 | |
hudakz | 0:5350a66d5279 | 60 | enum |
hudakz | 0:5350a66d5279 | 61 | { |
hudakz | 0:5350a66d5279 | 62 | padOption = 0, |
hudakz | 0:5350a66d5279 | 63 | subnetMask = 1, |
hudakz | 0:5350a66d5279 | 64 | timerOffset = 2, |
hudakz | 0:5350a66d5279 | 65 | routersOnSubnet = 3, |
hudakz | 0:5350a66d5279 | 66 | |
hudakz | 0:5350a66d5279 | 67 | /* timeServer = 4, |
hudakz | 0:5350a66d5279 | 68 | nameServer = 5,*/ |
hudakz | 0:5350a66d5279 | 69 | dns = 6, |
hudakz | 0:5350a66d5279 | 70 | |
hudakz | 0:5350a66d5279 | 71 | /*logServer = 7, |
hudakz | 0:5350a66d5279 | 72 | cookieServer = 8, |
hudakz | 0:5350a66d5279 | 73 | lprServer = 9, |
hudakz | 0:5350a66d5279 | 74 | impressServer = 10, |
hudakz | 0:5350a66d5279 | 75 | resourceLocationServer = 11,*/ |
hudakz | 0:5350a66d5279 | 76 | hostName = 12, |
hudakz | 0:5350a66d5279 | 77 | |
hudakz | 0:5350a66d5279 | 78 | /*bootFileSize = 13, |
hudakz | 0:5350a66d5279 | 79 | meritDumpFile = 14,*/ |
hudakz | 0:5350a66d5279 | 80 | domainName = 15, |
hudakz | 0:5350a66d5279 | 81 | |
hudakz | 0:5350a66d5279 | 82 | /*swapServer = 16, |
hudakz | 0:5350a66d5279 | 83 | rootPath = 17, |
hudakz | 0:5350a66d5279 | 84 | extentionsPath = 18, |
hudakz | 0:5350a66d5279 | 85 | IPforwarding = 19, |
hudakz | 0:5350a66d5279 | 86 | nonLocalSourceRouting = 20, |
hudakz | 0:5350a66d5279 | 87 | policyFilter = 21, |
hudakz | 0:5350a66d5279 | 88 | maxDgramReasmSize = 22, |
hudakz | 0:5350a66d5279 | 89 | defaultIPTTL = 23, |
hudakz | 0:5350a66d5279 | 90 | pathMTUagingTimeout = 24, |
hudakz | 0:5350a66d5279 | 91 | pathMTUplateauTable = 25, |
hudakz | 0:5350a66d5279 | 92 | ifMTU = 26, |
hudakz | 0:5350a66d5279 | 93 | allSubnetsLocal = 27, |
hudakz | 0:5350a66d5279 | 94 | broadcastAddr = 28, |
hudakz | 0:5350a66d5279 | 95 | performMaskDiscovery = 29, |
hudakz | 0:5350a66d5279 | 96 | maskSupplier = 30, |
hudakz | 0:5350a66d5279 | 97 | performRouterDiscovery = 31, |
hudakz | 0:5350a66d5279 | 98 | routerSolicitationAddr = 32, |
hudakz | 0:5350a66d5279 | 99 | staticRoute = 33, |
hudakz | 0:5350a66d5279 | 100 | trailerEncapsulation = 34, |
hudakz | 0:5350a66d5279 | 101 | arpCacheTimeout = 35, |
hudakz | 0:5350a66d5279 | 102 | ethernetEncapsulation = 36, |
hudakz | 0:5350a66d5279 | 103 | tcpDefaultTTL = 37, |
hudakz | 0:5350a66d5279 | 104 | tcpKeepaliveInterval = 38, |
hudakz | 0:5350a66d5279 | 105 | tcpKeepaliveGarbage = 39, |
hudakz | 0:5350a66d5279 | 106 | nisDomainName = 40, |
hudakz | 0:5350a66d5279 | 107 | nisServers = 41, |
hudakz | 0:5350a66d5279 | 108 | ntpServers = 42, |
hudakz | 0:5350a66d5279 | 109 | vendorSpecificInfo = 43, |
hudakz | 0:5350a66d5279 | 110 | netBIOSnameServer = 44, |
hudakz | 0:5350a66d5279 | 111 | netBIOSdgramDistServer = 45, |
hudakz | 0:5350a66d5279 | 112 | netBIOSnodeType = 46, |
hudakz | 0:5350a66d5279 | 113 | netBIOSscope = 47, |
hudakz | 0:5350a66d5279 | 114 | xFontServer = 48, |
hudakz | 0:5350a66d5279 | 115 | xDisplayManager = 49,*/ |
hudakz | 0:5350a66d5279 | 116 | dhcpRequestedIPaddr = 50, |
hudakz | 0:5350a66d5279 | 117 | dhcpIPaddrLeaseTime = 51, |
hudakz | 0:5350a66d5279 | 118 | |
hudakz | 0:5350a66d5279 | 119 | /*dhcpOptionOverload = 52,*/ |
hudakz | 0:5350a66d5279 | 120 | dhcpMessageType = 53, |
hudakz | 0:5350a66d5279 | 121 | dhcpServerIdentifier= 54, |
hudakz | 0:5350a66d5279 | 122 | dhcpParamRequest = 55, |
hudakz | 0:5350a66d5279 | 123 | |
hudakz | 0:5350a66d5279 | 124 | /*dhcpMsg = 56, |
hudakz | 0:5350a66d5279 | 125 | dhcpMaxMsgSize = 57,*/ |
hudakz | 0:5350a66d5279 | 126 | dhcpT1value = 58, |
hudakz | 0:5350a66d5279 | 127 | dhcpT2value = 59, |
hudakz | 0:5350a66d5279 | 128 | |
hudakz | 0:5350a66d5279 | 129 | /*dhcpClassIdentifier = 60,*/ |
hudakz | 0:5350a66d5279 | 130 | dhcpClientIdentifier= 61, |
hudakz | 0:5350a66d5279 | 131 | endOption = 255 |
hudakz | 0:5350a66d5279 | 132 | }; |
hudakz | 0:5350a66d5279 | 133 | |
hudakz | 0:5350a66d5279 | 134 | typedef struct _RIP_MSG_FIXED |
hudakz | 0:5350a66d5279 | 135 | { |
hudakz | 0:5350a66d5279 | 136 | uint8_t op; |
hudakz | 0:5350a66d5279 | 137 | uint8_t htype; |
hudakz | 0:5350a66d5279 | 138 | uint8_t hlen; |
hudakz | 0:5350a66d5279 | 139 | uint8_t hops; |
hudakz | 0:5350a66d5279 | 140 | uint32_t xid; |
hudakz | 0:5350a66d5279 | 141 | uint16_t secs; |
hudakz | 0:5350a66d5279 | 142 | uint16_t flags; |
hudakz | 0:5350a66d5279 | 143 | uint8_t ciaddr[4]; |
hudakz | 0:5350a66d5279 | 144 | uint8_t yiaddr[4]; |
hudakz | 0:5350a66d5279 | 145 | uint8_t siaddr[4]; |
hudakz | 0:5350a66d5279 | 146 | uint8_t giaddr[4]; |
hudakz | 0:5350a66d5279 | 147 | uint8_t chaddr[6]; |
hudakz | 0:5350a66d5279 | 148 | } RIP_MSG_FIXED; |
hudakz | 0:5350a66d5279 | 149 | |
hudakz | 0:5350a66d5279 | 150 | class DhcpClass |
hudakz | 0:5350a66d5279 | 151 | { |
hudakz | 0:5350a66d5279 | 152 | private: |
hudakz | 0:5350a66d5279 | 153 | uint32_t _dhcpInitialTransactionId; |
hudakz | 0:5350a66d5279 | 154 | uint32_t _dhcpTransactionId; |
hudakz | 0:5350a66d5279 | 155 | uint8_t _dhcpMacAddr[6]; |
hudakz | 0:5350a66d5279 | 156 | uint8_t _dhcpLocalIp[4]; |
hudakz | 0:5350a66d5279 | 157 | uint8_t _dhcpSubnetMask[4]; |
hudakz | 0:5350a66d5279 | 158 | uint8_t _dhcpGatewayIp[4]; |
hudakz | 0:5350a66d5279 | 159 | uint8_t _dhcpDhcpServerIp[4]; |
hudakz | 0:5350a66d5279 | 160 | uint8_t _dhcpDnsServerIp[4]; |
hudakz | 0:5350a66d5279 | 161 | uint32_t _dhcpLeaseTime; |
hudakz | 0:5350a66d5279 | 162 | uint32_t _dhcpT1, _dhcpT2; |
hudakz | 0:5350a66d5279 | 163 | signed long _renewInSec; |
hudakz | 0:5350a66d5279 | 164 | signed long _rebindInSec; |
hudakz | 0:5350a66d5279 | 165 | signed long _lastCheck; |
hudakz | 0:5350a66d5279 | 166 | unsigned long _timeout; |
hudakz | 0:5350a66d5279 | 167 | unsigned long _responseTimeout; |
hudakz | 0:5350a66d5279 | 168 | unsigned long _secTimeout; |
hudakz | 0:5350a66d5279 | 169 | uint8_t _dhcp_state; |
hudakz | 0:5350a66d5279 | 170 | UIPUDP _dhcpUdpSocket; |
hudakz | 0:5350a66d5279 | 171 | |
hudakz | 0:5350a66d5279 | 172 | int request_DHCP_lease(void); |
hudakz | 0:5350a66d5279 | 173 | void reset_DHCP_lease(void); |
hudakz | 0:5350a66d5279 | 174 | void presend_DHCP(void); |
hudakz | 0:5350a66d5279 | 175 | void send_DHCP_MESSAGE(uint8_t, uint16_t); |
hudakz | 0:5350a66d5279 | 176 | void printByte(char* , uint8_t); |
hudakz | 0:5350a66d5279 | 177 | |
hudakz | 0:5350a66d5279 | 178 | uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); |
hudakz | 0:5350a66d5279 | 179 | public: |
hudakz | 0:5350a66d5279 | 180 | IPAddress getLocalIp(void); |
hudakz | 0:5350a66d5279 | 181 | IPAddress getSubnetMask(void); |
hudakz | 0:5350a66d5279 | 182 | IPAddress getGatewayIp(void); |
hudakz | 0:5350a66d5279 | 183 | IPAddress getDhcpServerIp(void); |
hudakz | 0:5350a66d5279 | 184 | IPAddress getDnsServerIp(void); |
hudakz | 0:5350a66d5279 | 185 | |
hudakz | 0:5350a66d5279 | 186 | int beginWithDHCP(uint8_t* , unsigned long timeout = 60000, unsigned long responseTimeout = 4000); |
hudakz | 0:5350a66d5279 | 187 | int checkLease(void); |
hudakz | 0:5350a66d5279 | 188 | }; |
hudakz | 0:5350a66d5279 | 189 | #endif |