Fix issues building examples with LWIP_IPV6 (#268)
Fixes https://github.com/raspberrypi/pico-examples/issues/265
This commit is contained in:
@@ -109,11 +109,9 @@ static void dhcp_socket_free(struct udp_pcb **udp) {
|
||||
}
|
||||
}
|
||||
|
||||
static int dhcp_socket_bind(struct udp_pcb **udp, uint32_t ip, uint16_t port) {
|
||||
ip_addr_t addr;
|
||||
IP4_ADDR(&addr, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
|
||||
static int dhcp_socket_bind(struct udp_pcb **udp, uint16_t port) {
|
||||
// TODO convert lwIP errors to errno
|
||||
return udp_bind(*udp, &addr, port);
|
||||
return udp_bind(*udp, IP_ANY_TYPE, port);
|
||||
}
|
||||
|
||||
static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len, uint32_t ip, uint16_t port) {
|
||||
@@ -129,7 +127,7 @@ static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len,
|
||||
memcpy(p->payload, buf, len);
|
||||
|
||||
ip_addr_t dest;
|
||||
IP4_ADDR(&dest, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
|
||||
IP4_ADDR(ip_2_ip4(&dest), ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
|
||||
err_t err = udp_sendto(*udp, p, &dest, port);
|
||||
|
||||
pbuf_free(p);
|
||||
@@ -151,7 +149,7 @@ static uint8_t *opt_find(uint8_t *opt, uint8_t cmd) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void opt_write_n(uint8_t **opt, uint8_t cmd, size_t n, void *data) {
|
||||
static void opt_write_n(uint8_t **opt, uint8_t cmd, size_t n, const void *data) {
|
||||
uint8_t *o = *opt;
|
||||
*o++ = cmd;
|
||||
*o++ = n;
|
||||
@@ -198,7 +196,7 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
}
|
||||
|
||||
dhcp_msg.op = DHCPOFFER;
|
||||
memcpy(&dhcp_msg.yiaddr, &d->ip.addr, 4);
|
||||
memcpy(&dhcp_msg.yiaddr, &ip4_addr_get_u32(ip_2_ip4(&d->ip)), 4);
|
||||
|
||||
uint8_t *opt = (uint8_t *)&dhcp_msg.options;
|
||||
opt += 4; // assume magic cookie: 99, 130, 83, 99
|
||||
@@ -241,7 +239,7 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
// Should be NACK
|
||||
goto ignore_request;
|
||||
}
|
||||
if (memcmp(o + 2, &d->ip.addr, 3) != 0) {
|
||||
if (memcmp(o + 2, &ip4_addr_get_u32(ip_2_ip4(&d->ip)), 3) != 0) {
|
||||
// Should be NACK
|
||||
goto ignore_request;
|
||||
}
|
||||
@@ -273,9 +271,9 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
goto ignore_request;
|
||||
}
|
||||
|
||||
opt_write_n(&opt, DHCP_OPT_SERVER_ID, 4, &d->ip.addr);
|
||||
opt_write_n(&opt, DHCP_OPT_SUBNET_MASK, 4, &d->nm.addr);
|
||||
opt_write_n(&opt, DHCP_OPT_ROUTER, 4, &d->ip.addr); // aka gateway; can have mulitple addresses
|
||||
opt_write_n(&opt, DHCP_OPT_SERVER_ID, 4, &ip4_addr_get_u32(ip_2_ip4(&d->ip)));
|
||||
opt_write_n(&opt, DHCP_OPT_SUBNET_MASK, 4, &ip4_addr_get_u32(ip_2_ip4(&d->nm)));
|
||||
opt_write_n(&opt, DHCP_OPT_ROUTER, 4, &ip4_addr_get_u32(ip_2_ip4(&d->ip))); // aka gateway; can have mulitple addresses
|
||||
opt_write_u32(&opt, DHCP_OPT_DNS, DEFAULT_DNS); // can have mulitple addresses
|
||||
opt_write_u32(&opt, DHCP_OPT_IP_LEASE_TIME, DEFAULT_LEASE_TIME_S);
|
||||
*opt++ = DHCP_OPT_END;
|
||||
@@ -292,7 +290,7 @@ void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm) {
|
||||
if (dhcp_socket_new_dgram(&d->udp, d, dhcp_server_process) != 0) {
|
||||
return;
|
||||
}
|
||||
dhcp_socket_bind(&d->udp, 0, PORT_DHCP_SERVER);
|
||||
dhcp_socket_bind(&d->udp, PORT_DHCP_SERVER);
|
||||
}
|
||||
|
||||
void dhcp_server_deinit(dhcp_server_t *d) {
|
||||
|
||||
@@ -93,7 +93,7 @@ static bool tcp_server_open(void *arg) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err_t err = tcp_bind(pcb, NULL, TCP_PORT);
|
||||
err_t err = tcp_bind(pcb, IP_ANY_TYPE, TCP_PORT);
|
||||
if (err) {
|
||||
DEBUG_printf("failed to bind to port %d\n");
|
||||
return false;
|
||||
@@ -136,9 +136,9 @@ int main() {
|
||||
|
||||
cyw43_arch_enable_ap_mode(ap_name, password, CYW43_AUTH_WPA2_AES_PSK);
|
||||
|
||||
ip4_addr_t gw, mask;
|
||||
IP4_ADDR(&gw, 192, 168, 4, 1);
|
||||
IP4_ADDR(&mask, 255, 255, 255, 0);
|
||||
ip_addr_t gw, mask;
|
||||
IP4_ADDR(ip_2_ip4(&gw), 192, 168, 4, 1);
|
||||
IP4_ADDR(ip_2_ip4(&mask), 255, 255, 255, 0);
|
||||
|
||||
// Start the dhcp server
|
||||
dhcp_server_t dhcp_server;
|
||||
|
||||
@@ -37,7 +37,7 @@ void main_task(__unused void *params) {
|
||||
}
|
||||
|
||||
ip_addr_t ping_addr;
|
||||
ip4_addr_set_u32(&ping_addr, ipaddr_addr(PING_ADDR));
|
||||
ipaddr_aton(PING_ADDR, &ping_addr);
|
||||
ping_init(&ping_addr);
|
||||
|
||||
while(true) {
|
||||
|
||||
@@ -76,7 +76,7 @@ static void ntp_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *a
|
||||
NTP_T *state = (NTP_T*)arg;
|
||||
if (ipaddr) {
|
||||
state->ntp_server_address = *ipaddr;
|
||||
printf("ntp address %s\n", ip4addr_ntoa(ipaddr));
|
||||
printf("ntp address %s\n", ipaddr_ntoa(ipaddr));
|
||||
ntp_request(state);
|
||||
} else {
|
||||
printf("ntp dns request failed\n");
|
||||
|
||||
Reference in New Issue
Block a user