COSA
An Object-Oriented Platform for Arduino Programming
DHCP.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DHCP_HH
22 #define COSA_DHCP_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Socket.hh"
26 
33 class DHCP {
34 public:
36  static const uint16_t PORT = 68;
37 
44  DHCP(const char* hostname, const uint8_t* mac);
45 
52  bool begin(Socket* sock);
53 
59  bool end();
60 
67  int discover();
68 
80  int request(uint8_t ip[4], uint8_t subnet[4], uint8_t gateway[4]);
81 
89  int renew(Socket* sock);
90 
100  int release(Socket* sock);
101 
103  uint32_t lease_obtained() const
104  {
105  return (m_lease_obtained);
106  }
107 
109  uint32_t lease_expires() const
110  {
111  return (m_lease_expires);
112  }
113 
115  const uint8_t* dhcp_addr() const
116  {
117  return (m_dhcp);
118  }
119 
121  const uint8_t* dns_addr() const
122  {
123  return (m_dns);
124  }
125 
127  const uint8_t* gateway_addr() const
128  {
129  return (m_gateway);
130  }
131 
132 private:
134  enum {
135  REQUEST = 1,
136  REPLY = 2
137  } __attribute__((packed));
138 
140  enum {
141  HTYPE_10MB = 1,
142  HTYPE_100MB = 2,
143  HLEN_ETHERNET = 6,
144  FLAGS_BROADCAST = 0x8000
145  };
146 
148  struct header_t {
149  uint8_t OP;
150  uint8_t HTYPE;
151  uint8_t HLEN;
152  uint8_t HOPS;
153  uint32_t XID;
154  uint16_t SECS;
155  uint16_t FLAGS;
156  uint8_t CIADDR[4];
157  uint8_t YIADDR[4];
158  uint8_t SIADDR[4];
159  uint8_t GIADDR[4];
160  uint8_t CHADDRB[16];
161 #ifdef BOOTP_SECTION
162  uint8_t SNAME[64];
163  uint8_t FILE[128];
164 #endif
165  };
166 
168  static const uint32_t MAGIC_COOKIE = 0x63825363;
169 
171  enum {
172  PAD_OPTION = 0,
173  SUBNET_MASK = 1,
174  TIMER_OFFSET = 2,
175  ROUTERS_ON_SUBNET = 3,
176  DNS_SERVER = 6,
177  HOSTNAME = 12,
178  DOMAIN_NAME = 15,
179  REQUESTED_IP_ADDR = 50,
180  IP_ADDR_LEASE_TIME = 51,
181  MESSAGE_TYPE = 53,
182  SERVER_IDENTIFIER = 54,
183  PARAM_REQUEST = 55,
184  T1_VALUE = 58,
185  T2_VALUE = 59,
186  CLIENT_IDENTIFIER = 61,
187  END_OPTION = 255
188  } __attribute__((packed));
189 
191  enum {
192  DHCP_DISCOVER = 1,
193  DHCP_OFFER = 2,
194  DHCP_REQUEST = 3,
195  DHCP_DECLINE = 4,
196  DHCP_ACK = 5,
197  DHCP_NAK = 6,
198  DHCP_RELEASE = 7,
199  DHCP_INFORM = 8
200  } __attribute__((packed));
201 
203  uint8_t m_dhcp[4];
204 
206  uint8_t m_gateway[4];
207 
209  uint8_t m_dns[4];
210 
212  uint8_t m_ip[4];
213 
215  uint8_t m_subnet[4];
216 
218  const char* m_hostname;
219 
221  const uint8_t* m_mac;
222 
224  Socket* m_sock;
225 
227  uint32_t m_lease_obtained;
228 
230  uint32_t m_lease_expires;
231 
233  static const uint16_t SERVER_PORT = 67;
234 
241  int send(uint8_t type);
242 
251  int recv(uint8_t type, uint16_t ms = 2000);
252 };
253 
254 #endif
Definition: Socket.hh:31
uint32_t lease_expires() const
Definition: DHCP.hh:109
int request(uint8_t ip[4], uint8_t subnet[4], uint8_t gateway[4])
Definition: DHCP.cpp:206
Definition: DHCP.hh:33
static const uint16_t PORT
Definition: DHCP.hh:36
int discover()
Definition: DHCP.cpp:197
int renew(Socket *sock)
Definition: DHCP.cpp:220
const uint8_t * gateway_addr() const
Definition: DHCP.hh:127
bool end()
Definition: DHCP.cpp:188
const uint8_t * dhcp_addr() const
Definition: DHCP.hh:115
int release(Socket *sock)
Definition: DHCP.cpp:235
const uint8_t * dns_addr() const
Definition: DHCP.hh:121
DHCP(const char *hostname, const uint8_t *mac)
Definition: DHCP.cpp:25
uint32_t lease_obtained() const
Definition: DHCP.hh:103
bool begin(Socket *sock)
Definition: DHCP.cpp:180