35 DHCP::send(uint8_t type)
40 uint8_t BROADCAST[4] = { 0xff, 0xff, 0xff, 0xff };
41 int res = m_sock->
datagram(BROADCAST, SERVER_PORT);
46 memset(&header, 0,
sizeof(header));
48 header.HTYPE = HTYPE_10MB;
49 header.HLEN = HLEN_ETHERNET;
52 header.FLAGS =
hton((int16_t) FLAGS_BROADCAST);
54 res = m_sock->
write(&header,
sizeof(header));
59 memset(buf, 0,
sizeof(buf));
60 for (uint8_t i = 0; i < 6; i++) m_sock->
write(buf,
sizeof(buf));
63 uint32_t magic =
hton((int32_t) MAGIC_COOKIE);
64 res = m_sock->
write(&magic,
sizeof(magic));
65 if (res < 0)
return (res);
68 uint8_t len =
strlen_P(m_hostname) + 1;
69 buf[0] = MESSAGE_TYPE;
72 buf[3] = CLIENT_IDENTIFIER;
78 memcpy_P(&buf[14], m_hostname, len);
79 res = m_sock->
write(buf, 14 + len);
83 if (type == DHCP_REQUEST) {
84 buf[0] = REQUESTED_IP_ADDR;
87 buf[6] = SERVER_IDENTIFIER;
95 static const uint8_t param[]
__PROGMEM = {
105 res = m_sock->
write_P(param,
sizeof(param));
106 if (
UNLIKELY(res < 0))
return (res);
107 return (m_sock->
flush());
111 DHCP::recv(uint8_t type, uint16_t ms)
117 for (uint16_t i = 0; i < ms; i += 32) {
118 if ((res = m_sock->
available()) != 0)
break;
126 res = m_sock->
recv(&header,
sizeof(header), m_dhcp, port);
131 memcpy(m_ip, header.YIADDR,
sizeof(m_ip));
135 for (uint8_t i = 0; i < 6; i++) m_sock->
read(buf,
sizeof(buf));
139 res = m_sock->
read(&magic,
sizeof(magic));
141 magic =
ntoh((int32_t) magic);
148 while (m_sock->
read(&op,
sizeof(op)) ==
sizeof(op)) {
149 if (op == END_OPTION)
break;
150 if (op == PAD_OPTION)
continue;
151 m_sock->
read(&len,
sizeof(len));
152 m_sock->
read(buf, len);
155 if (buf[0] != type) res = -6;
158 memcpy(m_subnet, buf,
sizeof(m_subnet));
161 memcpy(m_dns, buf,
sizeof(m_dns));
163 case ROUTERS_ON_SUBNET:
164 memcpy(m_gateway, buf,
sizeof(m_gateway));
166 case IP_ADDR_LEASE_TIME:
168 int32_t* expire_p = (int32_t*) buf;
169 m_lease_expires =
ntoh(*expire_p) + m_lease_obtained;
175 while (m_sock->
available() > 0) m_sock->
read(buf,
sizeof(buf));
200 int res = send(DHCP_DISCOVER);
201 if (
UNLIKELY(res < 0))
return (res);
202 return (recv(DHCP_OFFER));
209 int res = send(DHCP_REQUEST);
210 if (
UNLIKELY(res < 0))
return (res);
211 res = recv(DHCP_ACK);
212 if (
UNLIKELY(res < 0))
return (res);
213 memcpy(ip, m_ip,
sizeof(m_ip));
214 memcpy(subnet, m_subnet,
sizeof(m_subnet));
215 memcpy(gateway, m_gateway,
sizeof(m_gateway));
225 int res = send(DHCP_REQUEST);
226 if (
UNLIKELY(res < 0))
return (res);
227 res = recv(DHCP_ACK);
228 if (
UNLIKELY(res < 0))
return (res);
239 int res = send(DHCP_RELEASE);
240 if (
UNLIKELY(res < 0))
return (res);
241 res = recv(DHCP_ACK);
242 if (
UNLIKELY(res < 0))
return (res);
245 memset(m_ip, 0,
sizeof(m_ip));
246 m_lease_obtained = 0L;
247 m_lease_expires = 0L;
virtual int recv(void *buf, size_t len)=0
int request(uint8_t ip[4], uint8_t subnet[4], uint8_t gateway[4])
virtual int write(const void *buf, size_t size)
virtual int datagram(uint8_t addr[4], uint16_t port)=0
void(* delay)(uint32_t ms)
virtual int write_P(const void *buf, size_t size)
static const uint8_t IP_MAX
int release(Socket *sock)
static const uint8_t MAC_MAX
DHCP(const char *hostname, const uint8_t *mac)
virtual int read(void *buf, size_t size)