Yet another minor bgpd diff from a much bigger diff. This adds
inet4applymask() similar to inet6applymask() and starts using it in a few
places. This makes some of the INET vs INET6 cases more similar.

OK?
-- 
:wq Claudio

Index: bgpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v
retrieving revision 1.331
diff -u -p -r1.331 bgpd.h
--- bgpd.h      29 Aug 2018 19:47:47 -0000      1.331
+++ bgpd.h      3 Sep 2018 14:29:39 -0000
@@ -1174,6 +1177,7 @@ int                nlri_get_vpn4(u_char *, u_int16_t,
 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);
Index: rde_prefix.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_prefix.c,v
retrieving revision 1.33
diff -u -p -r1.33 rde_prefix.c
--- rde_prefix.c        24 Jan 2017 04:22:42 -0000      1.33
+++ rde_prefix.c        4 Sep 2018 10:15:42 -0000
@@ -101,7 +101,6 @@ pt_fill(struct bgpd_addr *prefix, int pr
        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:
@@ -109,9 +108,7 @@ pt_fill(struct bgpd_addr *prefix, int pr
                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:
@@ -119,17 +116,16 @@ pt_fill(struct bgpd_addr *prefix, int pr
                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;
Index: session.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/session.c,v
retrieving revision 1.365
diff -u -p -r1.365 session.c
--- session.c   11 Jul 2018 16:34:36 -0000      1.365
+++ session.c   4 Sep 2018 10:07:34 -0000
@@ -3150,19 +3150,20 @@ session_template_clone(struct peer *p, s
 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);
        }
Index: util.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/util.c,v
retrieving revision 1.31
diff -u -p -r1.31 util.c
--- util.c      29 Aug 2018 11:43:15 -0000      1.31
+++ util.c      30 Aug 2018 12:12:05 -0000
@@ -732,6 +732,15 @@ prefixlen2mask(u_int8_t 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)
 {
        struct in6_addr mask;

Reply via email to