@@ -109,11 +109,9 @@ static void dhcp_socket_free(struct udp_pcb **udp) {
109
109
}
110
110
}
111
111
112
- static int dhcp_socket_bind (struct udp_pcb * * udp , uint32_t ip , uint16_t port ) {
113
- ip_addr_t addr ;
114
- IP4_ADDR (& addr , ip >> 24 & 0xff , ip >> 16 & 0xff , ip >> 8 & 0xff , ip & 0xff );
112
+ static int dhcp_socket_bind (struct udp_pcb * * udp , uint16_t port ) {
115
113
// TODO convert lwIP errors to errno
116
- return udp_bind (* udp , & addr , port );
114
+ return udp_bind (* udp , IP_ANY_TYPE , port );
117
115
}
118
116
119
117
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,
129
127
memcpy (p -> payload , buf , len );
130
128
131
129
ip_addr_t dest ;
132
- IP4_ADDR (& dest , ip >> 24 & 0xff , ip >> 16 & 0xff , ip >> 8 & 0xff , ip & 0xff );
130
+ IP4_ADDR (ip_2_ip4 ( & dest ) , ip >> 24 & 0xff , ip >> 16 & 0xff , ip >> 8 & 0xff , ip & 0xff );
133
131
err_t err = udp_sendto (* udp , p , & dest , port );
134
132
135
133
pbuf_free (p );
@@ -151,7 +149,7 @@ static uint8_t *opt_find(uint8_t *opt, uint8_t cmd) {
151
149
return NULL ;
152
150
}
153
151
154
- static void opt_write_n (uint8_t * * opt , uint8_t cmd , size_t n , void * data ) {
152
+ static void opt_write_n (uint8_t * * opt , uint8_t cmd , size_t n , const void * data ) {
155
153
uint8_t * o = * opt ;
156
154
* o ++ = cmd ;
157
155
* o ++ = n ;
@@ -198,7 +196,7 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
198
196
}
199
197
200
198
dhcp_msg .op = DHCPOFFER ;
201
- memcpy (& dhcp_msg .yiaddr , & d -> ip . addr , 4 );
199
+ memcpy (& dhcp_msg .yiaddr , & ip4_addr_get_u32 ( ip_2_ip4 ( & d -> ip )) , 4 );
202
200
203
201
uint8_t * opt = (uint8_t * )& dhcp_msg .options ;
204
202
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,
241
239
// Should be NACK
242
240
goto ignore_request ;
243
241
}
244
- if (memcmp (o + 2 , & d -> ip . addr , 3 ) != 0 ) {
242
+ if (memcmp (o + 2 , & ip4_addr_get_u32 ( ip_2_ip4 ( & d -> ip )) , 3 ) != 0 ) {
245
243
// Should be NACK
246
244
goto ignore_request ;
247
245
}
@@ -273,9 +271,9 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
273
271
goto ignore_request ;
274
272
}
275
273
276
- opt_write_n (& opt , DHCP_OPT_SERVER_ID , 4 , & d -> ip . addr );
277
- opt_write_n (& opt , DHCP_OPT_SUBNET_MASK , 4 , & d -> nm . addr );
278
- opt_write_n (& opt , DHCP_OPT_ROUTER , 4 , & d -> ip . addr ); // aka gateway; can have mulitple addresses
274
+ opt_write_n (& opt , DHCP_OPT_SERVER_ID , 4 , & ip4_addr_get_u32 ( ip_2_ip4 ( & d -> ip )) );
275
+ opt_write_n (& opt , DHCP_OPT_SUBNET_MASK , 4 , & ip4_addr_get_u32 ( ip_2_ip4 ( & d -> nm )) );
276
+ opt_write_n (& opt , DHCP_OPT_ROUTER , 4 , & ip4_addr_get_u32 ( ip_2_ip4 ( & d -> ip )) ); // aka gateway; can have mulitple addresses
279
277
opt_write_u32 (& opt , DHCP_OPT_DNS , DEFAULT_DNS ); // can have mulitple addresses
280
278
opt_write_u32 (& opt , DHCP_OPT_IP_LEASE_TIME , DEFAULT_LEASE_TIME_S );
281
279
* opt ++ = DHCP_OPT_END ;
@@ -292,7 +290,7 @@ void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm) {
292
290
if (dhcp_socket_new_dgram (& d -> udp , d , dhcp_server_process ) != 0 ) {
293
291
return ;
294
292
}
295
- dhcp_socket_bind (& d -> udp , 0 , PORT_DHCP_SERVER );
293
+ dhcp_socket_bind (& d -> udp , PORT_DHCP_SERVER );
296
294
}
297
295
298
296
void dhcp_server_deinit (dhcp_server_t * d ) {
0 commit comments