nshlib/ifconfig: Generate default IPv4 gateway with netmask

Generate default gateway using configured netmask instead of fixed
255.255.255.0, which may be more flexible under certain situations.
e.g. A subnet like 10.0.23.16/28 may not contain the .1 address.

Before:

ifconfig eth0 10.0.23.22 netmask 255.255.255.252 => DRaddr:10.0.23.1
ifconfig eth0 10.0.23.22 netmask 255.255.255.240 => DRaddr:10.0.23.1
ifconfig eth0 10.0.23.22 netmask 255.255.255.0   => DRaddr:10.0.23.1
ifconfig eth0 10.0.23.22 netmask 255.255.0.0     => DRaddr:10.0.23.1

ifconfig eth0 10.0.23.22 => Mask:255.255.255.0      DRaddr:10.0.23.1

After:

ifconfig eth0 10.0.23.22 netmask 255.255.255.252 => DRaddr:10.0.23.21
ifconfig eth0 10.0.23.22 netmask 255.255.255.240 => DRaddr:10.0.23.17
ifconfig eth0 10.0.23.22 netmask 255.255.255.0   => DRaddr:10.0.23.1
ifconfig eth0 10.0.23.22 netmask 255.255.0.0     => DRaddr:10.0.0.1

ifconfig eth0 10.0.23.22 => Mask:255.255.255.0      DRaddr:10.0.23.1

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-12-06 16:27:31 +08:00 committed by Xiang Xiao
parent fb72849089
commit 9abc82c792

View File

@ -550,6 +550,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#ifdef CONFIG_NET_IPv4
struct in_addr addr;
in_addr_t gip = INADDR_ANY;
in_addr_t mip;
#endif
#ifdef CONFIG_NET_IPv6
struct in6_addr addr6;
@ -923,6 +924,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
addr.s_addr = inet_addr("255.255.255.0");
}
mip = addr.s_addr;
netlib_set_ipv4netmask(ifname, &addr);
}
#endif /* CONFIG_NET_IPv4 */
@ -959,13 +961,13 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
}
else
{
if (gip != 0)
if (gip != INADDR_ANY)
{
ninfo("Gateway: default\n");
gip = NTOHL(gip);
gip &= ~0x000000ff;
gip = ntohl(gip);
gip &= ntohl(mip);
gip |= 0x00000001;
gip = HTONL(gip);
gip = htonl(gip);
}
addr.s_addr = gip;