Skip to content

Commit 4005af1

Browse files
The dhcp server should reply to the right netif (#392)
The DHCP server broadcasts messages. This are being sent via the default netif which might be completely the wrong network. We want to send messages to the netif we got the original message from.
1 parent 5ba4cfc commit 4005af1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pico_w/wifi/access_point/dhcpserver/dhcpserver.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static int dhcp_socket_bind(struct udp_pcb **udp, uint16_t port) {
113113
return udp_bind(*udp, IP_ANY_TYPE, port);
114114
}
115115

116-
static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len, uint32_t ip, uint16_t port) {
116+
static int dhcp_socket_sendto(struct udp_pcb **udp, struct netif *nif, const void *buf, size_t len, uint32_t ip, uint16_t port) {
117117
if (len > 0xffff) {
118118
len = 0xffff;
119119
}
@@ -127,7 +127,12 @@ static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len,
127127

128128
ip_addr_t dest;
129129
IP4_ADDR(ip_2_ip4(&dest), ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
130-
err_t err = udp_sendto(*udp, p, &dest, port);
130+
err_t err;
131+
if (nif != NULL) {
132+
err = udp_sendto_if(*udp, p, &dest, port, nif);
133+
} else {
134+
err = udp_sendto(*udp, p, &dest, port);
135+
}
131136

132137
pbuf_free(p);
133138

@@ -282,7 +287,8 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
282287
opt_write_n(&opt, DHCP_OPT_DNS, 4, &ip4_addr_get_u32(ip_2_ip4(&d->ip))); // this server is the dns
283288
opt_write_u32(&opt, DHCP_OPT_IP_LEASE_TIME, DEFAULT_LEASE_TIME_S);
284289
*opt++ = DHCP_OPT_END;
285-
dhcp_socket_sendto(&d->udp, &dhcp_msg, opt - (uint8_t *)&dhcp_msg, 0xffffffff, PORT_DHCP_CLIENT);
290+
struct netif *nif = ip_current_input_netif();
291+
dhcp_socket_sendto(&d->udp, nif, &dhcp_msg, opt - (uint8_t *)&dhcp_msg, 0xffffffff, PORT_DHCP_CLIENT);
286292

287293
ignore_request:
288294
pbuf_free(p);

0 commit comments

Comments
 (0)