Skip to content

Commit d7ed38b

Browse files
committed
fix build errors
1 parent dfa5f8e commit d7ed38b

File tree

7 files changed

+694
-2
lines changed

7 files changed

+694
-2
lines changed

py/circuitpy_mpconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern void common_hal_mcu_enable_interrupts(void);
4646
#define MICROPY_PY_BLUETOOTH (0)
4747
#define MICROPY_PY_LWIP_SLIP (0)
4848
#define MICROPY_PY_OS_DUPTERM (0)
49+
#define MICROPY_PY_PYEXEC_COMPILE_ONLY (0)
4950
#define MICROPY_ROM_TEXT_COMPRESSION (0)
5051
#define MICROPY_VFS_LFS1 (0)
5152
#define MICROPY_VFS_LFS2 (0)

shared/netutils/dhcpserver.c

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// For DHCP specs see:
28+
// https://www.ietf.org/rfc/rfc2131.txt
29+
// https://tools.ietf.org/html/rfc2132 -- DHCP Options and BOOTP Vendor Extensions
30+
31+
#include <stdio.h>
32+
#include <string.h>
33+
#include "py/mperrno.h"
34+
#include "py/mphal.h"
35+
#include "lwip/opt.h"
36+
37+
// CIRCUITPY-CHANGE: comment
38+
// Used in CIRCUITPY without MICROPY_PY_LWIP
39+
40+
#if LWIP_UDP
41+
42+
#include "shared/netutils/dhcpserver.h"
43+
#include "lwip/udp.h"
44+
45+
#define DHCPDISCOVER (1)
46+
#define DHCPOFFER (2)
47+
#define DHCPREQUEST (3)
48+
#define DHCPDECLINE (4)
49+
#define DHCPACK (5)
50+
#define DHCPNACK (6)
51+
#define DHCPRELEASE (7)
52+
#define DHCPINFORM (8)
53+
54+
#define DHCP_OPT_PAD (0)
55+
#define DHCP_OPT_SUBNET_MASK (1)
56+
#define DHCP_OPT_ROUTER (3)
57+
#define DHCP_OPT_DNS (6)
58+
#define DHCP_OPT_HOST_NAME (12)
59+
#define DHCP_OPT_REQUESTED_IP (50)
60+
#define DHCP_OPT_IP_LEASE_TIME (51)
61+
#define DHCP_OPT_MSG_TYPE (53)
62+
#define DHCP_OPT_SERVER_ID (54)
63+
#define DHCP_OPT_PARAM_REQUEST_LIST (55)
64+
#define DHCP_OPT_MAX_MSG_SIZE (57)
65+
#define DHCP_OPT_VENDOR_CLASS_ID (60)
66+
#define DHCP_OPT_CLIENT_ID (61)
67+
#define DHCP_OPT_END (255)
68+
69+
#define PORT_DHCP_SERVER (67)
70+
#define PORT_DHCP_CLIENT (68)
71+
72+
#define DEFAULT_DNS MAKE_IP4(192, 168, 4, 1)
73+
#define DEFAULT_LEASE_TIME_S (24 * 60 * 60) // in seconds
74+
75+
#define MAC_LEN (6)
76+
#define MAKE_IP4(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
77+
78+
typedef struct {
79+
uint8_t op; // message opcode
80+
uint8_t htype; // hardware address type
81+
uint8_t hlen; // hardware address length
82+
uint8_t hops;
83+
uint32_t xid; // transaction id, chosen by client
84+
uint16_t secs; // client seconds elapsed
85+
uint16_t flags;
86+
uint8_t ciaddr[4]; // client IP address
87+
uint8_t yiaddr[4]; // your IP address
88+
uint8_t siaddr[4]; // next server IP address
89+
uint8_t giaddr[4]; // relay agent IP address
90+
uint8_t chaddr[16]; // client hardware address
91+
uint8_t sname[64]; // server host name
92+
uint8_t file[128]; // boot file name
93+
uint8_t options[312]; // optional parameters, variable, starts with magic
94+
} dhcp_msg_t;
95+
96+
static int dhcp_socket_new_dgram(struct udp_pcb **udp, void *cb_data, udp_recv_fn cb_udp_recv) {
97+
// family is AF_INET
98+
// type is SOCK_DGRAM
99+
100+
*udp = udp_new();
101+
if (*udp == NULL) {
102+
return -MP_ENOMEM;
103+
}
104+
105+
// Register callback
106+
udp_recv(*udp, cb_udp_recv, (void *)cb_data);
107+
108+
return 0; // success
109+
}
110+
111+
static void dhcp_socket_free(struct udp_pcb **udp) {
112+
if (*udp != NULL) {
113+
udp_remove(*udp);
114+
*udp = NULL;
115+
}
116+
}
117+
118+
static int dhcp_socket_bind(struct udp_pcb **udp, uint32_t ip, uint16_t port) {
119+
ip_addr_t addr;
120+
IP_ADDR4(&addr, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
121+
// TODO convert lwIP errors to errno
122+
return udp_bind(*udp, &addr, port);
123+
}
124+
125+
static int dhcp_socket_sendto(struct udp_pcb **udp, struct netif *netif, const void *buf, size_t len, uint32_t ip, uint16_t port) {
126+
if (len > 0xffff) {
127+
len = 0xffff;
128+
}
129+
130+
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
131+
if (p == NULL) {
132+
return -MP_ENOMEM;
133+
}
134+
135+
memcpy(p->payload, buf, len);
136+
137+
ip_addr_t dest;
138+
IP_ADDR4(&dest, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
139+
err_t err;
140+
if (netif != NULL) {
141+
err = udp_sendto_if(*udp, p, &dest, port, netif);
142+
} else {
143+
err = udp_sendto(*udp, p, &dest, port);
144+
}
145+
146+
pbuf_free(p);
147+
148+
if (err != ERR_OK) {
149+
return err;
150+
}
151+
152+
return len;
153+
}
154+
155+
static uint8_t *opt_find(uint8_t *opt, uint8_t cmd) {
156+
for (int i = 0; i < 308 && opt[i] != DHCP_OPT_END;) {
157+
if (opt[i] == cmd) {
158+
return &opt[i];
159+
}
160+
i += 2 + opt[i + 1];
161+
}
162+
return NULL;
163+
}
164+
165+
static void opt_write_n(uint8_t **opt, uint8_t cmd, size_t n, const void *data) {
166+
uint8_t *o = *opt;
167+
*o++ = cmd;
168+
*o++ = n;
169+
memcpy(o, data, n);
170+
*opt = o + n;
171+
}
172+
173+
static void opt_write_u8(uint8_t **opt, uint8_t cmd, uint8_t val) {
174+
uint8_t *o = *opt;
175+
*o++ = cmd;
176+
*o++ = 1;
177+
*o++ = val;
178+
*opt = o;
179+
}
180+
181+
static void opt_write_u32(uint8_t **opt, uint8_t cmd, uint32_t val) {
182+
uint8_t *o = *opt;
183+
*o++ = cmd;
184+
*o++ = 4;
185+
*o++ = val >> 24;
186+
*o++ = val >> 16;
187+
*o++ = val >> 8;
188+
*o++ = val;
189+
*opt = o;
190+
}
191+
192+
static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *src_addr, u16_t src_port) {
193+
dhcp_server_t *d = arg;
194+
(void)upcb;
195+
(void)src_addr;
196+
(void)src_port;
197+
198+
// This is around 548 bytes
199+
dhcp_msg_t dhcp_msg;
200+
201+
#define DHCP_MIN_SIZE (240 + 3)
202+
if (p->tot_len < DHCP_MIN_SIZE) {
203+
goto ignore_request;
204+
}
205+
206+
size_t len = pbuf_copy_partial(p, &dhcp_msg, sizeof(dhcp_msg), 0);
207+
if (len < DHCP_MIN_SIZE) {
208+
goto ignore_request;
209+
}
210+
211+
dhcp_msg.op = DHCPOFFER;
212+
memcpy(&dhcp_msg.yiaddr, &ip_2_ip4(&d->ip)->addr, 4);
213+
214+
uint8_t *opt = (uint8_t *)&dhcp_msg.options;
215+
opt += 4; // assume magic cookie: 99, 130, 83, 99
216+
217+
switch (opt[2]) {
218+
case DHCPDISCOVER: {
219+
int yi = DHCPS_MAX_IP;
220+
for (int i = 0; i < DHCPS_MAX_IP; ++i) {
221+
if (memcmp(d->lease[i].mac, dhcp_msg.chaddr, MAC_LEN) == 0) {
222+
// MAC match, use this IP address
223+
yi = i;
224+
break;
225+
}
226+
if (yi == DHCPS_MAX_IP) {
227+
// Look for a free IP address
228+
if (memcmp(d->lease[i].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) {
229+
// IP available
230+
yi = i;
231+
}
232+
uint32_t expiry = d->lease[i].expiry << 16 | 0xffff;
233+
if ((int32_t)(expiry - mp_hal_ticks_ms()) < 0) {
234+
// IP expired, reuse it
235+
memset(d->lease[i].mac, 0, MAC_LEN);
236+
yi = i;
237+
}
238+
}
239+
}
240+
if (yi == DHCPS_MAX_IP) {
241+
// No more IP addresses left
242+
goto ignore_request;
243+
}
244+
dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi;
245+
opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPOFFER);
246+
break;
247+
}
248+
249+
case DHCPREQUEST: {
250+
uint8_t *o = opt_find(opt, DHCP_OPT_REQUESTED_IP);
251+
if (o == NULL) {
252+
// Should be NACK
253+
goto ignore_request;
254+
}
255+
if (memcmp(o + 2, &ip_2_ip4(&d->ip)->addr, 3) != 0) {
256+
// Should be NACK
257+
goto ignore_request;
258+
}
259+
uint8_t yi = o[5] - DHCPS_BASE_IP;
260+
if (yi >= DHCPS_MAX_IP) {
261+
// Should be NACK
262+
goto ignore_request;
263+
}
264+
if (memcmp(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN) == 0) {
265+
// MAC match, ok to use this IP address
266+
} else if (memcmp(d->lease[yi].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) {
267+
// IP unused, ok to use this IP address
268+
memcpy(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN);
269+
} else {
270+
// IP already in use
271+
// Should be NACK
272+
goto ignore_request;
273+
}
274+
d->lease[yi].expiry = (mp_hal_ticks_ms() + DEFAULT_LEASE_TIME_S * 1000) >> 16;
275+
dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi;
276+
opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPACK);
277+
// CIRCUITPY-CHANGE: use LWIP_DEBUGF instead of printf
278+
LWIP_DEBUGF(DHCP_DEBUG, ("DHCPS: client connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x IP=%u.%u.%u.%u\n",
279+
dhcp_msg.chaddr[0], dhcp_msg.chaddr[1], dhcp_msg.chaddr[2], dhcp_msg.chaddr[3], dhcp_msg.chaddr[4], dhcp_msg.chaddr[5],
280+
dhcp_msg.yiaddr[0], dhcp_msg.yiaddr[1], dhcp_msg.yiaddr[2], dhcp_msg.yiaddr[3]));
281+
break;
282+
}
283+
284+
default:
285+
goto ignore_request;
286+
}
287+
288+
opt_write_n(&opt, DHCP_OPT_SERVER_ID, 4, &ip_2_ip4(&d->ip)->addr);
289+
opt_write_n(&opt, DHCP_OPT_SUBNET_MASK, 4, &ip_2_ip4(&d->nm)->addr);
290+
opt_write_n(&opt, DHCP_OPT_ROUTER, 4, &ip_2_ip4(&d->ip)->addr); // aka gateway; can have multiple addresses
291+
opt_write_u32(&opt, DHCP_OPT_DNS, DEFAULT_DNS); // can have multiple addresses
292+
opt_write_u32(&opt, DHCP_OPT_IP_LEASE_TIME, DEFAULT_LEASE_TIME_S);
293+
*opt++ = DHCP_OPT_END;
294+
struct netif *netif = ip_current_input_netif();
295+
dhcp_socket_sendto(&d->udp, netif, &dhcp_msg, opt - (uint8_t *)&dhcp_msg, 0xffffffff, PORT_DHCP_CLIENT);
296+
297+
ignore_request:
298+
pbuf_free(p);
299+
}
300+
301+
void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm) {
302+
ip_addr_copy(d->ip, *ip);
303+
ip_addr_copy(d->nm, *nm);
304+
memset(d->lease, 0, sizeof(d->lease));
305+
if (dhcp_socket_new_dgram(&d->udp, d, dhcp_server_process) != 0) {
306+
return;
307+
}
308+
dhcp_socket_bind(&d->udp, 0, PORT_DHCP_SERVER);
309+
}
310+
311+
void dhcp_server_deinit(dhcp_server_t *d) {
312+
dhcp_socket_free(&d->udp);
313+
}
314+
315+
#endif // MICROPY_PY_LWIP

shared/netutils/dhcpserver.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H
27+
#define MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H
28+
29+
#include "lwip/ip_addr.h"
30+
31+
#define DHCPS_BASE_IP (16)
32+
#define DHCPS_MAX_IP (8)
33+
34+
typedef struct _dhcp_server_lease_t {
35+
uint8_t mac[6];
36+
uint16_t expiry;
37+
} dhcp_server_lease_t;
38+
39+
typedef struct _dhcp_server_t {
40+
ip_addr_t ip;
41+
ip_addr_t nm;
42+
dhcp_server_lease_t lease[DHCPS_MAX_IP];
43+
struct udp_pcb *udp;
44+
} dhcp_server_t;
45+
46+
void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm);
47+
void dhcp_server_deinit(dhcp_server_t *d);
48+
49+
#endif // MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H

0 commit comments

Comments
 (0)