summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2024-03-28 13:19:51 +0100
committerDamien George <damien@micropython.org>2024-06-04 12:45:01 +1000
commita71471be66300eb9415f05e030cd92649f810b34 (patch)
tree505c106559616b4be67dc061b1cebc6d294a0f0f
parent7e7cc2b427f588559d50bae35d49c305201c2365 (diff)
extmod/network_lwip: Allow using the CIDR notation for addr4.
There was a little omisssion in the code. Signed-off-by: robert-hh <robert@hammelrath.com>
-rw-r--r--extmod/network_lwip.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/extmod/network_lwip.c b/extmod/network_lwip.c
index 97cb43902..329a45c46 100644
--- a/extmod/network_lwip.c
+++ b/extmod/network_lwip.c
@@ -297,20 +297,23 @@ mp_obj_t mod_network_nic_ipconfig(struct netif *netif, size_t n_args, const mp_o
char plain_ip[IPADDR_STRLEN_MAX];
char *split = strchr(input_str, '/');
const char *addr_str = input_str;
+ int to_copy = MIN(sizeof(plain_ip) - 1, addr_len);
+ memcpy(plain_ip, addr_str, to_copy);
if (split) {
- int to_copy = sizeof(plain_ip) - 1;
if (split - addr_str < to_copy) {
to_copy = split - addr_str;
}
- memcpy(plain_ip, addr_str, to_copy);
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
prefix_bits = mp_obj_get_int(prefix_obj);
+ if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
+ uint32_t mask = -(1u << (32 - prefix_bits));
+ ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
+ }
+ } else {
+ netmask = netif->netmask;
}
- if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
- uint32_t mask = -(1u << (32 - prefix_bits));
- ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
- }
- if (!ipaddr_aton(addr_str, &ip_addr)) {
+ plain_ip[to_copy] = '\0';
+ if (!ipaddr_aton(plain_ip, &ip_addr)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments"));
}
if ((mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr6) != IP_IS_V6(&ip_addr)