Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions pico_w/access_point/dhcpserver/dhcpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pico_w/access_point/picow_access_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pico_w/freertos/ping/picow_freertos_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pico_w/ntp_client/picow_ntp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down