friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/DHCP.h
Go to the documentation of this file.
1 // EtherDune DHCP Service
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Implements the basics of DHCP so as to obtain and maintain an IP lease
4 // along with DNS, gateway IP and netmask.
5 //
6 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
7 //
8 // This source is subject to the GPLv2 license.
9 // Please see the License.txt file for more information.
10 // All other rights reserved.
11 //
12 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
13 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
15 // PARTICULAR PURPOSE.
16 
34 
35 #ifndef _DHCP_H_
36 #define _DHCP_H_
37 
38 #include <ACross.h>
39 #include "UDPSocket.h"
40 #include "Stateful.h"
41 
42 
43 static const uint8_t DHCP_DISCOVER = 1;
44 static const uint8_t DHCP_OFFER = 2;
45 static const uint8_t DHCP_REQUEST = 3;
46 static const uint8_t DHCP_DECLINE = 4;
47 static const uint8_t DHCP_ACK = 5;
48 static const uint8_t DHCP_NACK = 6;
49 static const uint8_t DHCP_RELEASE = 7;
50 static const uint8_t DHCP_INFORM = 8;
51 
52 static const uint8_t DHCP_OPTIONS_PAD = 0;
53 static const uint8_t DHCP_OPTIONS_SUBNET = 1;
54 static const uint8_t DHCP_OPTIONS_ROUTER = 3;
55 static const uint8_t DHCP_OPTIONS_DNS = 6;
56 static const uint8_t DHCP_OPTIONS_HOSTNAME = 12;
57 static const uint8_t DHCP_OPTIONS_REQUESTED_IP = 50;
58 static const uint8_t DHCP_OPTIONS_MESSAGETYPE = 53;
59 static const uint8_t DHCP_OPTIONS_SERVER_IDENTIFIER = 54;
60 static const uint8_t DHCP_OPTIONS_RENEWAL_TIME = 58;
61 static const uint8_t DHCP_OPTIONS_CLIENT_IDENTIFIER = 61;
62 static const uint8_t DHCP_OPTIONS_END = 255;
63 
64 static const uint8_t DHCP_HARDWARE_TYPE_ETHERNET = 1;
65 
67 struct DHCPOptionHeader
68 {
69  uint8_t code;
70  uint8_t length;
71  DHCPOptionHeader(uint8_t optionCode, uint8_t optionLength) : code(optionCode), length(optionLength){}
72  DHCPOptionHeader(){}
73 
74 };
75 
76 template<uint8_t CODE, uint8_t LENGTH>
77 struct DHCPOption : public DHCPOptionHeader
78 {
79  DHCPOption() : DHCPOptionHeader(CODE, LENGTH){}
80 
81 };
82 
83 struct DHCPIPOption : DHCPOptionHeader
84 {
85  IPAddress ip;
86 };
87 
88 struct DHCPRequestedIPOption : public DHCPOption < DHCP_OPTIONS_REQUESTED_IP, sizeof(IPAddress) >
89 {
90  IPAddress ip;
91 };
92 
93 struct DHCPTimerOption :DHCPOptionHeader
94 {
95  nint32_t timer;
96 };
97 
98 struct DHCPClientIdentifierOptionHeader : DHCPOption<DHCP_OPTIONS_CLIENT_IDENTIFIER, sizeof(uint8_t) + sizeof(MACAddress)>
99 {
100  uint8_t hardwareType;
101  DHCPClientIdentifierOptionHeader() :hardwareType(DHCP_HARDWARE_TYPE_ETHERNET){}
102 };
103 
104 
105 
106 
107 template <uint8_t MESSAGETYPE>
108 struct DHCPMessageTypeOption : public DHCPOption <DHCP_OPTIONS_MESSAGETYPE, 1>
109 {
110  uint8_t messageType;
111  DHCPMessageTypeOption() : messageType(MESSAGETYPE){}
112 
113 };
114 
115 typedef DHCPMessageTypeOption<DHCP_DISCOVER> DHCPDiscoverMessageTypeOption;
116 typedef DHCPMessageTypeOption<DHCP_REQUEST> DHCPRequestMessageTypeOption;
117 
118 
119 
121 
122 class DHCP : private UDPSocket, Stateful
123 {
124 private:
125 
126  uint8_t attempts;
127  uint16_t renewalTimer;
128 
129  void onReceive(uint16_t len);
130  void setMagicCookie();
131  void sendDHCPDiscover();
132  void initDHCP();
133  void writeAdditionalFields();
134  NOINLINE DHCPOptionHeader* findOption(uint8_t searchCode);
135  void prepareDHCPRequest();
136  uint8_t getMessageType();
137  __FlashStringHelper* getStateString();
138  void tick();
139 
140 public:
141 
142  DHCP();
143  bool dhcpSetup();
144 
145 };
146 
147 
148 
149 
150 
151 
152 
153 #endif
static const uint8_t DHCP_OPTIONS_SUBNET
Definition: DHCP.h:53
static const uint8_t DHCP_OPTIONS_REQUESTED_IP
Definition: DHCP.h:57
static const uint8_t DHCP_OPTIONS_CLIENT_IDENTIFIER
Definition: DHCP.h:61
static const uint8_t DHCP_REQUEST
Definition: DHCP.h:45
static const uint8_t DHCP_OPTIONS_HOSTNAME
Definition: DHCP.h:56
static const uint8_t DHCP_OPTIONS_PAD
Definition: DHCP.h:52
Implements the UDP protocol.
Definition: UDPSocket.h:34
static const uint8_t DHCP_INFORM
Definition: DHCP.h:50
EtherDune DHCP Service.
Definition: DHCP.h:122
static const uint8_t DHCP_OPTIONS_RENEWAL_TIME
Definition: DHCP.h:60
static const uint8_t DHCP_RELEASE
Definition: DHCP.h:49
static const uint8_t DHCP_OPTIONS_END
Definition: DHCP.h:62
represents an IP address in memory
Definition: inet.h:181
Represents a network byte order 32 bit integer.
Definition: inet.h:109
static const uint8_t DHCP_OPTIONS_SERVER_IDENTIFIER
Definition: DHCP.h:59
bool dhcpSetup()
Attempts to configure the IP settings: local IP, subnet mask, gateway and DNS via DHCP...
Definition: DHCP.cpp:50
static const uint8_t DHCP_DISCOVER
Definition: DHCP.h:43
DHCP()
Definition: DHCP.cpp:25
Maintains a state variable and allows to pull out a state string representation for debugging...
Definition: Stateful.h:31
static const uint8_t DHCP_OFFER
Definition: DHCP.h:44
static const uint8_t DHCP_DECLINE
Definition: DHCP.h:46
static const uint8_t DHCP_ACK
Definition: DHCP.h:47
static const uint8_t DHCP_OPTIONS_ROUTER
Definition: DHCP.h:54
static const uint8_t DHCP_NACK
Definition: DHCP.h:48
static const uint8_t DHCP_HARDWARE_TYPE_ETHERNET
Definition: DHCP.h:64
static const uint8_t DHCP_OPTIONS_DNS
Definition: DHCP.h:55
static const uint8_t DHCP_OPTIONS_MESSAGETYPE
Definition: DHCP.h:58