Skip to content

Commit d0e7a66

Browse files
committed
Introduce IPv6, unit tests pass
1 parent 5a1ff12 commit d0e7a66

File tree

10 files changed

+2059
-616
lines changed

10 files changed

+2059
-616
lines changed

mongoose.c

Lines changed: 721 additions & 177 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
@@ -1368,6 +1388,16 @@ void mg_delayms(unsigned int ms);
13681388

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

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

14041453
uint16_t mg_ntohs(uint16_t net);
14051454
uint32_t mg_ntohl(uint32_t net);
1455+
uint64_t mg_ntohll(uint64_t net);
14061456
#define mg_htons(x) mg_ntohs(x)
14071457
#define mg_htonl(x) mg_ntohl(x)
1458+
#define mg_htonll(x) mg_ntohll(x)
14081459

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

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

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

31253182
// Internal state, user can use it but should not change it
31263183
uint8_t gwmac[6]; // Router's MAC
@@ -3133,14 +3190,17 @@ struct mg_tcpip_if {
31333190
volatile uint32_t nrecv; // Number of received frames
31343191
volatile uint32_t nsent; // Number of transmitted frames
31353192
volatile uint32_t nerr; // Number of driver errors
3136-
uint8_t state; // Current state
3193+
uint8_t state; // Current link and IPv4 state
31373194
#define MG_TCPIP_STATE_DOWN 0 // Interface is down
31383195
#define MG_TCPIP_STATE_UP 1 // Interface is up
31393196
#define MG_TCPIP_STATE_REQ 2 // Interface is up, DHCP REQUESTING state
31403197
#define MG_TCPIP_STATE_IP 3 // Interface is up and has an IP assigned
31413198
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
3199+
#if MG_ENABLE_IPV6
3200+
uint8_t gw6mac[6]; // IPv6 Router's MAC
3201+
uint8_t state6; // Current IPv6 state
3202+
#endif
31423203
};
3143-
31443204
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
31453205
void mg_tcpip_free(struct mg_tcpip_if *);
31463206
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3637,6 +3697,18 @@ struct mg_tcpip_driver_stm32h_data {
36373697
#define MG_ENABLE_ETH_IRQ()
36383698
#endif
36393699

3700+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
3701+
#define MG_IPV6_INIT(mif) \
3702+
do { \
3703+
memcpy(mif.ip6ll, (uint8_t[16]) MG_TCPIP_LINK_LOCAL, 16); \
3704+
memcpy(mif.ip6, (uint8_t[16]) MG_TCPIP_GLOBAL, 16); \
3705+
memcpy(mif.gw6, (uint8_t[16]) MG_TCPIP_GW6, 16); \
3706+
mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
3707+
} while(0)
3708+
#else
3709+
#define MG_IPV6_INIT(mif)
3710+
#endif
3711+
36403712
#define MG_TCPIP_DRIVER_INIT(mgr) \
36413713
do { \
36423714
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -3650,6 +3722,7 @@ struct mg_tcpip_driver_stm32h_data {
36503722
mif_.driver = &mg_tcpip_driver_stm32h; \
36513723
mif_.driver_data = &driver_data_; \
36523724
MG_SET_MAC_ADDRESS(mif_.mac); \
3725+
MG_IPV6_INIT(mif_); \
36533726
mg_tcpip_init(mgr, &mif_); \
36543727
MG_ENABLE_ETH_IRQ(); \
36553728
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
@@ -49,6 +49,18 @@ struct mg_tcpip_driver_stm32h_data {
4949
#define MG_ENABLE_ETH_IRQ()
5050
#endif
5151

52+
#if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
53+
#define MG_IPV6_INIT(mif) \
54+
do { \
55+
memcpy(mif.ip6ll, (uint8_t[16]) MG_TCPIP_LINK_LOCAL, 16); \
56+
memcpy(mif.ip6, (uint8_t[16]) MG_TCPIP_GLOBAL, 16); \
57+
memcpy(mif.gw6, (uint8_t[16]) MG_TCPIP_GW6, 16); \
58+
mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
59+
} while(0)
60+
#else
61+
#define MG_IPV6_INIT(mif)
62+
#endif
63+
5264
#define MG_TCPIP_DRIVER_INIT(mgr) \
5365
do { \
5466
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -62,6 +74,7 @@ struct mg_tcpip_driver_stm32h_data {
6274
mif_.driver = &mg_tcpip_driver_stm32h; \
6375
mif_.driver_data = &driver_data_; \
6476
MG_SET_MAC_ADDRESS(mif_.mac); \
77+
MG_IPV6_INIT(mif_); \
6578
mg_tcpip_init(mgr, &mif_); \
6679
MG_ENABLE_ETH_IRQ(); \
6780
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \

0 commit comments

Comments
 (0)