can be used instead of doing direct fiddling around with struct in_addr.
Use it in a few cases where it makes the code more similar between INET
and INET6 case.
OK denis@
-/* $OpenBSD: bgpd.h,v 1.331 2018/08/29 19:47:47 claudio Exp $ */
+/* $OpenBSD: bgpd.h,v 1.332 2018/09/04 12:00:29 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
int prefix_compare(const struct bgpd_addr *,
const struct bgpd_addr *, int);
in_addr_t prefixlen2mask(u_int8_t);
+void inet4applymask(struct in_addr *, const struct in_addr *, int);
void inet6applymask(struct in6_addr *, const struct in6_addr *,
int);
const char *aid2str(u_int8_t);
-/* $OpenBSD: rde_prefix.c,v 1.33 2017/01/24 04:22:42 benno Exp $ */
+/* $OpenBSD: rde_prefix.c,v 1.34 2018/09/04 12:00:29 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
static struct pt_entry4 pte4;
static struct pt_entry6 pte6;
static struct pt_entry_vpn4 pte_vpn4;
- in_addr_t addr_hbo;
switch (prefix->aid) {
case AID_INET:
pte4.aid = prefix->aid;
if (prefixlen > 32)
fatalx("pt_fill: bad IPv4 prefixlen");
- addr_hbo = ntohl(prefix->v4.s_addr);
- pte4.prefix4.s_addr = htonl(addr_hbo &
- prefixlen2mask(prefixlen));
+ inet4applymask(&pte4.prefix4, &prefix->v4, prefixlen);
pte4.prefixlen = prefixlen;
return ((struct pt_entry *)&pte4);
case AID_INET6:
pte6.aid = prefix->aid;
if (prefixlen > 128)
fatalx("pt_get: bad IPv6 prefixlen");
- pte6.prefixlen = prefixlen;
inet6applymask(&pte6.prefix6, &prefix->v6, prefixlen);
+ pte6.prefixlen = prefixlen;
return ((struct pt_entry *)&pte6);
case AID_VPN_IPv4:
bzero(&pte_vpn4, sizeof(pte_vpn4));
pte_vpn4.aid = prefix->aid;
if (prefixlen > 32)
fatalx("pt_fill: bad IPv4 prefixlen");
- addr_hbo = ntohl(prefix->vpn4.addr.s_addr);
- pte_vpn4.prefix4.s_addr = htonl(addr_hbo &
- prefixlen2mask(prefixlen));
+ inet4applymask(&pte_vpn4.prefix4, &prefix->vpn4.addr,
+ prefixlen);
pte_vpn4.prefixlen = prefixlen;
pte_vpn4.rd = prefix->vpn4.rd;
pte_vpn4.labellen = prefix->vpn4.labellen;
-/* $OpenBSD: session.c,v 1.365 2018/07/11 16:34:36 claudio Exp $ */
+/* $OpenBSD: session.c,v 1.366 2018/09/04 12:00:29 claudio Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org>
int
session_match_mask(struct peer *p, struct bgpd_addr *a)
{
- in_addr_t v4mask;
- struct in6_addr masked;
+ struct in_addr v4masked;
+ struct in6_addr v6masked;
switch (p->conf.remote_addr.aid) {
case AID_INET:
- v4mask = htonl(prefixlen2mask(p->conf.remote_masklen));
- if (p->conf.remote_addr.v4.s_addr == (a->v4.s_addr & v4mask))
+ inet4applymask(&v4masked, &a->v4, p->conf.remote_masklen);
+ if (p->conf.remote_addr.v4.s_addr == v4masked.s_addr)
return (1);
return (0);
case AID_INET6:
- inet6applymask(&masked, &a->v6, p->conf.remote_masklen);
+ inet6applymask(&v6masked, &a->v6, p->conf.remote_masklen);
- if (!memcmp(&masked, &p->conf.remote_addr.v6, sizeof(masked)))
+ if (memcmp(&v6masked, &p->conf.remote_addr.v6,
+ sizeof(v6masked)) == 0)
return (1);
return (0);
}
-/* $OpenBSD: util.c,v 1.31 2018/08/29 11:43:15 claudio Exp $ */
+/* $OpenBSD: util.c,v 1.32 2018/09/04 12:00:29 claudio Exp $ */
/*
* Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org>
return (0xffffffff << (32 - prefixlen));
}
+void
+inet4applymask(struct in_addr *dest, const struct in_addr *src, int prefixlen)
+{
+ struct in_addr mask;
+
+ mask.s_addr = htonl(prefixlen2mask(prefixlen));
+ dest->s_addr = src->s_addr & mask.s_addr;
+}
+
void
inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen)
{