Skip to content

Commit a71471b

Browse files
robert-hhdpgeorge
authored andcommitted
extmod/network_lwip: Allow using the CIDR notation for addr4.
There was a little omisssion in the code. Signed-off-by: robert-hh <[email protected]>
1 parent 7e7cc2b commit a71471b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

extmod/network_lwip.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,23 @@ mp_obj_t mod_network_nic_ipconfig(struct netif *netif, size_t n_args, const mp_o
297297
char plain_ip[IPADDR_STRLEN_MAX];
298298
char *split = strchr(input_str, '/');
299299
const char *addr_str = input_str;
300+
int to_copy = MIN(sizeof(plain_ip) - 1, addr_len);
301+
memcpy(plain_ip, addr_str, to_copy);
300302
if (split) {
301-
int to_copy = sizeof(plain_ip) - 1;
302303
if (split - addr_str < to_copy) {
303304
to_copy = split - addr_str;
304305
}
305-
memcpy(plain_ip, addr_str, to_copy);
306306
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
307307
prefix_bits = mp_obj_get_int(prefix_obj);
308+
if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
309+
uint32_t mask = -(1u << (32 - prefix_bits));
310+
ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
311+
}
312+
} else {
313+
netmask = netif->netmask;
308314
}
309-
if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
310-
uint32_t mask = -(1u << (32 - prefix_bits));
311-
ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
312-
}
313-
if (!ipaddr_aton(addr_str, &ip_addr)) {
315+
plain_ip[to_copy] = '\0';
316+
if (!ipaddr_aton(plain_ip, &ip_addr)) {
314317
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments"));
315318
}
316319
if ((mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr6) != IP_IS_V6(&ip_addr)

0 commit comments

Comments
 (0)