in: add in_mask2len()

Similar to the existing in6_mask2len() function, but for IPv4. This will be used
by pf's nat64 code.

Obtained from:	OpenBSD
Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D47785
This commit is contained in:
Kristof Provost 2024-10-22 10:45:06 +02:00
parent 2d7e68d5cd
commit e4e0f49742
2 changed files with 22 additions and 0 deletions

View file

@ -441,6 +441,27 @@ in_control_ioctl(u_long cmd, void *data, struct ifnet *ifp,
return (error);
}
int
in_mask2len(struct in_addr *mask)
{
int x, y;
u_char *p;
p = (u_char *)mask;
for (x = 0; x < sizeof(*mask); x++) {
if (p[x] != 0xff)
break;
}
y = 0;
if (x < sizeof(*mask)) {
for (y = 0; y < 8; y++) {
if ((p[x] & (0x80 >> y)) == 0)
break;
}
}
return (x * 8 + y);
}
int
in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
struct thread *td)

View file

@ -459,6 +459,7 @@ int in_joingroup_locked(struct ifnet *, const struct in_addr *,
int in_leavegroup(struct in_multi *, /*const*/ struct in_mfilter *);
int in_leavegroup_locked(struct in_multi *,
/*const*/ struct in_mfilter *);
int in_mask2len(struct in_addr *);
int in_control(struct socket *, u_long, void *, struct ifnet *,
struct thread *);
int in_control_ioctl(u_long, void *, struct ifnet *,