Skip to content

Commit 571b902

Browse files
committed
Introduce IPv6, unit tests pass
1 parent 2c5a6d4 commit 571b902

File tree

10 files changed

+2053
-617
lines changed

10 files changed

+2053
-617
lines changed

mongoose.c

Lines changed: 723 additions & 179 deletions
Large diffs are not rendered by default.

mongoose.h

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,26 @@ struct timeval {
10571057
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
10581058
#endif
10591059

1060+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
1061+
1062+
#ifndef MG_TCPIP_GLOBAL
1063+
#define MG_TCPIP_GLOBAL MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
1064+
#endif
1065+
1066+
#ifndef MG_TCPIP_LINK_LOCAL
1067+
#define MG_TCPIP_LINK_LOCAL MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
1068+
#endif
1069+
1070+
#ifndef MG_TCPIP_PREFIX_LEN
1071+
#define MG_TCPIP_PREFIX_LEN 0
1072+
#endif
1073+
1074+
#ifndef MG_TCPIP_GW6
1075+
#define MG_TCPIP_GW6 MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
1076+
#endif
1077+
1078+
#endif
1079+
10601080
#ifndef MG_SET_MAC_ADDRESS
10611081
#define MG_SET_MAC_ADDRESS(mac)
10621082
#endif
@@ -1369,6 +1389,16 @@ void mg_delayms(unsigned int ms);
13691389

13701390
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
13711391

1392+
#define MG_IPV6(a, b, c, d, e, f, g ,h) \
1393+
{ (uint8_t)((a)>>8),(uint8_t)(a), \
1394+
(uint8_t)((b)>>8),(uint8_t)(b), \
1395+
(uint8_t)((c)>>8),(uint8_t)(c), \
1396+
(uint8_t)((d)>>8),(uint8_t)(d), \
1397+
(uint8_t)((e)>>8),(uint8_t)(e), \
1398+
(uint8_t)((f)>>8),(uint8_t)(f), \
1399+
(uint8_t)((g)>>8),(uint8_t)(g), \
1400+
(uint8_t)((h)>>8),(uint8_t)(h) }
1401+
13721402
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
13731403
#define MG_U8P(ADDR) ((uint8_t *) (ADDR))
13741404
#define MG_IPADDR_PARTS(ADDR) \
@@ -1383,6 +1413,14 @@ void mg_delayms(unsigned int ms);
13831413
((uint32_t) (((uint32_t) MG_U8P(p)[0] << 24U) | \
13841414
((uint32_t) MG_U8P(p)[1] << 16U) | \
13851415
((uint32_t) MG_U8P(p)[2] << 8U) | MG_U8P(p)[3]))
1416+
#define MG_LOAD_BE64(p) \
1417+
((uint64_t) (((uint64_t) MG_U8P(p)[0] << 56U) | \
1418+
((uint64_t) MG_U8P(p)[1] << 48U) | \
1419+
((uint64_t) MG_U8P(p)[2] << 40U) | \
1420+
((uint64_t) MG_U8P(p)[3] << 32U) | \
1421+
((uint64_t) MG_U8P(p)[4] << 24U) | \
1422+
((uint64_t) MG_U8P(p)[5] << 16U) | \
1423+
((uint64_t) MG_U8P(p)[6] << 8U) | MG_U8P(p)[7]))
13861424
#define MG_STORE_BE16(p, n) \
13871425
do { \
13881426
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
@@ -1401,11 +1439,24 @@ void mg_delayms(unsigned int ms);
14011439
MG_U8P(p)[2] = ((n) >> 8U) & 255; \
14021440
MG_U8P(p)[3] = (n) &255; \
14031441
} while (0)
1442+
#define MG_STORE_BE64(p, n) \
1443+
do { \
1444+
MG_U8P(p)[0] = ((n) >> 56U) & 255; \
1445+
MG_U8P(p)[1] = ((n) >> 48U) & 255; \
1446+
MG_U8P(p)[2] = ((n) >> 40U) & 255; \
1447+
MG_U8P(p)[3] = ((n) >> 32U) & 255; \
1448+
MG_U8P(p)[4] = ((n) >> 24U) & 255; \
1449+
MG_U8P(p)[5] = ((n) >> 16U) & 255; \
1450+
MG_U8P(p)[6] = ((n) >> 8U) & 255; \
1451+
MG_U8P(p)[7] = (n) &255; \
1452+
} while (0)
14041453

14051454
uint16_t mg_ntohs(uint16_t net);
14061455
uint32_t mg_ntohl(uint32_t net);
1456+
uint64_t mg_ntohll(uint64_t net);
14071457
#define mg_htons(x) mg_ntohs(x)
14081458
#define mg_htonl(x) mg_ntohl(x)
1459+
#define mg_htonll(x) mg_ntohll(x)
14091460

14101461
#define MG_REG(x) ((volatile uint32_t *) (x))[0]
14111462
#define MG_BIT(x) (((uint32_t) 1U) << (x))
@@ -3086,18 +3137,17 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
30863137
void *ev_data);
30873138

30883139
enum {
3089-
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3090-
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3091-
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3092-
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3093-
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3094-
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
3095-
// mg_wifi_scan_bss_data *
3096-
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3097-
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
3098-
// chip specific
3099-
MG_TCPIP_EV_DRIVER, // Driver event driver specific
3100-
MG_TCPIP_EV_USER // Starting ID for user events
3140+
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3141+
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3142+
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3143+
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3144+
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3145+
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
3146+
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3147+
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
3148+
MG_TCPIP_EV_DRIVER, // Driver event driver specific
3149+
MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
3150+
MG_TCPIP_EV_USER // Starting ID for user events
31013151
};
31023152

31033153
// Network interface
@@ -3122,6 +3172,13 @@ struct mg_tcpip_if {
31223172
char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
31233173
uint16_t mtu; // Interface MTU
31243174
#define MG_TCPIP_MTU_DEFAULT 1500
3175+
#if MG_ENABLE_IPV6
3176+
uint64_t ip6ll[2], ip6[2]; // IPv6 link-local and global addresses
3177+
uint8_t prefix_len; // Prefix length
3178+
uint64_t gw6[2]; // Default gateway
3179+
bool enable_slaac; // Enable IPv6 address autoconfiguration
3180+
bool enable_dhcp6_client; // Enable DCHPv6 client
3181+
#endif
31253182

31263183
// Internal state, user can use it but should not change it
31273184
uint8_t gwmac[6]; // Router's MAC
@@ -3134,14 +3191,17 @@ struct mg_tcpip_if {
31343191
volatile uint32_t nrecv; // Number of received frames
31353192
volatile uint32_t nsent; // Number of transmitted frames
31363193
volatile uint32_t nerr; // Number of driver errors
3137-
uint8_t state; // Current state
3194+
uint8_t state; // Current link and IPv4 state
31383195
#define MG_TCPIP_STATE_DOWN 0 // Interface is down
31393196
#define MG_TCPIP_STATE_UP 1 // Interface is up
31403197
#define MG_TCPIP_STATE_REQ 2 // Interface is up, DHCP REQUESTING state
31413198
#define MG_TCPIP_STATE_IP 3 // Interface is up and has an IP assigned
31423199
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
3200+
#if MG_ENABLE_IPV6
3201+
uint8_t gw6mac[6]; // IPv6 Router's MAC
3202+
uint8_t state6; // Current IPv6 state
3203+
#endif
31433204
};
3144-
31453205
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
31463206
void mg_tcpip_free(struct mg_tcpip_if *);
31473207
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3635,6 +3695,18 @@ struct mg_tcpip_driver_stm32h_data {
36353695
#define MG_ENABLE_ETH_IRQ()
36363696
#endif
36373697

3698+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
3699+
#define MG_IPV6_INIT(mif) \
3700+
do { \
3701+
memcpy(mif.ip6ll, (uint8_t[16]) MG_TCPIP_LINK_LOCAL, 16); \
3702+
memcpy(mif.ip6, (uint8_t[16]) MG_TCPIP_GLOBAL, 16); \
3703+
memcpy(mif.gw6, (uint8_t[16]) MG_TCPIP_GW6, 16); \
3704+
mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
3705+
} while(0)
3706+
#else
3707+
#define MG_IPV6_INIT(mif)
3708+
#endif
3709+
36383710
#define MG_TCPIP_DRIVER_INIT(mgr) \
36393711
do { \
36403712
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -3648,6 +3720,7 @@ struct mg_tcpip_driver_stm32h_data {
36483720
mif_.driver = &mg_tcpip_driver_stm32h; \
36493721
mif_.driver_data = &driver_data_; \
36503722
MG_SET_MAC_ADDRESS(mif_.mac); \
3723+
MG_IPV6_INIT(mif_); \
36513724
mg_tcpip_init(mgr, &mif_); \
36523725
MG_ENABLE_ETH_IRQ(); \
36533726
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \

src/config.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@
175175
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
176176
#endif
177177

178+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
179+
180+
#ifndef MG_TCPIP_GLOBAL
181+
#define MG_TCPIP_GLOBAL MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
182+
#endif
183+
184+
#ifndef MG_TCPIP_LINK_LOCAL
185+
#define MG_TCPIP_LINK_LOCAL MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
186+
#endif
187+
188+
#ifndef MG_TCPIP_PREFIX_LEN
189+
#define MG_TCPIP_PREFIX_LEN 0
190+
#endif
191+
192+
#ifndef MG_TCPIP_GW6
193+
#define MG_TCPIP_GW6 MG_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
194+
#endif
195+
196+
#endif
197+
178198
#ifndef MG_SET_MAC_ADDRESS
179199
#define MG_SET_MAC_ADDRESS(mac)
180200
#endif

src/drivers/stm32h.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ struct mg_tcpip_driver_stm32h_data {
4646
#define MG_ENABLE_ETH_IRQ()
4747
#endif
4848

49+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
50+
#define MG_IPV6_INIT(mif) \
51+
do { \
52+
memcpy(mif.ip6ll, (uint8_t[16]) MG_TCPIP_LINK_LOCAL, 16); \
53+
memcpy(mif.ip6, (uint8_t[16]) MG_TCPIP_GLOBAL, 16); \
54+
memcpy(mif.gw6, (uint8_t[16]) MG_TCPIP_GW6, 16); \
55+
mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
56+
} while(0)
57+
#else
58+
#define MG_IPV6_INIT(mif)
59+
#endif
60+
4961
#define MG_TCPIP_DRIVER_INIT(mgr) \
5062
do { \
5163
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -59,6 +71,7 @@ struct mg_tcpip_driver_stm32h_data {
5971
mif_.driver = &mg_tcpip_driver_stm32h; \
6072
mif_.driver_data = &driver_data_; \
6173
MG_SET_MAC_ADDRESS(mif_.mac); \
74+
MG_IPV6_INIT(mif_); \
6275
mg_tcpip_init(mgr, &mif_); \
6376
MG_ENABLE_ETH_IRQ(); \
6477
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \

0 commit comments

Comments
 (0)