From 54fe48ed23bff8d28a696d63b7a6a660bad58ace Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Fri, 2 Sep 2016 16:34:20 +0000 Subject: [PATCH] Simplify handling of multicast addresses. During the initialization of the eigrpe process, use inet_pton() to store the EIGRP multicast addresses in two global variables: * global.mcast_addr_v4 (in_addr); * global.mcast_addr_v6 (in6_addr). This way we don't need to create temporary in_addr/in6_addr variables everytime we need to use these multicast addresses for something. "I like this" claudio@ ok benno@ --- eigrp.h | 9 +++------ eigrpd.h | 2 ++ eigrpe.c | 5 +++++ interface.c | 14 ++++---------- packet.c | 10 ++++------ 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/eigrp.h b/eigrp.h index d36e5de..c052fb9 100644 --- a/eigrp.h +++ b/eigrp.h @@ -26,12 +26,9 @@ #define IPPROTO_EIGRP 88 #define EIGRP_IP_TTL 2 -/* 224.0.0.10 */ -#define AllEIGRPRouters_v4 0xa0000e0 /* network byte order */ -/* ff02::a */ -#define AllEIGRPRouters_v6 \ - {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a }}} +/* EIGRP multicast group addresses */ +#define AllEIGRPRouters_v4 "224.0.0.10" +#define AllEIGRPRouters_v6 "ff02::a" #define EIGRP_INFINITE_METRIC ((uint32_t )(~0)) diff --git a/eigrpd.h b/eigrpd.h index bac1a2c..9a2500e 100644 --- a/eigrpd.h +++ b/eigrpd.h @@ -325,6 +325,8 @@ struct eigrpd_global { time_t uptime; int eigrp_socket_v4; int eigrp_socket_v6; + struct in_addr mcast_addr_v4; + struct in6_addr mcast_addr_v6; char *csock; }; diff --git a/eigrpe.c b/eigrpe.c index 2b2abed..97bc755 100644 --- a/eigrpe.c +++ b/eigrpe.c @@ -83,6 +83,11 @@ eigrpe(int debug, int verbose, char *sockname) if (control_init(global.csock) == -1) fatalx("control socket setup failed"); + if (inet_pton(AF_INET, AllEIGRPRouters_v4, &global.mcast_addr_v4) != 1) + fatal("inet_pton"); + if (inet_pton(AF_INET6, AllEIGRPRouters_v6, &global.mcast_addr_v6) != 1) + fatal("inet_pton"); + /* create the raw ipv4 socket */ if ((global.eigrp_socket_v4 = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_EIGRP)) == -1) diff --git a/interface.c b/interface.c index 3759795..fefb9f2 100644 --- a/interface.c +++ b/interface.c @@ -376,8 +376,6 @@ eigrp_if_start(struct eigrp_iface *ei) struct timeval now; struct if_addr *if_addr; union eigrpd_addr addr; - struct in_addr addr4; - struct in6_addr addr6 = AllEIGRPRouters_v6; log_debug("%s: %s as %u family %s", __func__, ei->iface->name, eigrp->as, af_name(eigrp->af)); @@ -402,12 +400,11 @@ eigrp_if_start(struct eigrp_iface *ei) switch (eigrp->af) { case AF_INET: - addr4.s_addr = AllEIGRPRouters_v4; - if (if_join_ipv4_group(ei->iface, &addr4)) + if (if_join_ipv4_group(ei->iface, &global.mcast_addr_v4)) return; break; case AF_INET6: - if (if_join_ipv6_group(ei->iface, &addr6)) + if (if_join_ipv6_group(ei->iface, &global.mcast_addr_v6)) return; break; default: @@ -422,8 +419,6 @@ void eigrp_if_reset(struct eigrp_iface *ei) { struct eigrp *eigrp = ei->eigrp; - struct in_addr addr4; - struct in6_addr addr6 = AllEIGRPRouters_v6; struct nbr *nbr; log_debug("%s: %s as %u family %s", __func__, ei->iface->name, @@ -440,11 +435,10 @@ eigrp_if_reset(struct eigrp_iface *ei) /* try to cleanup */ switch (eigrp->af) { case AF_INET: - addr4.s_addr = AllEIGRPRouters_v4; - if_leave_ipv4_group(ei->iface, &addr4); + if_leave_ipv4_group(ei->iface, &global.mcast_addr_v4); break; case AF_INET6: - if_leave_ipv6_group(ei->iface, &addr6); + if_leave_ipv6_group(ei->iface, &global.mcast_addr_v6); break; default: fatalx("eigrp_if_reset: unknown af"); diff --git a/packet.c b/packet.c index e42e4c4..792d577 100644 --- a/packet.c +++ b/packet.c @@ -73,7 +73,7 @@ send_packet_v4(struct iface *iface, struct nbr *nbr, struct ibuf *buf) if (nbr) dst.sin_addr = nbr->addr.v4; else - dst.sin_addr.s_addr = AllEIGRPRouters_v4; + dst.sin_addr = global.mcast_addr_v4; /* setup IP hdr */ memset(&ip_hdr, 0, sizeof(ip_hdr)); @@ -121,7 +121,6 @@ static int send_packet_v6(struct iface *iface, struct nbr *nbr, struct ibuf *buf) { struct sockaddr_in6 sa6; - struct in6_addr maddr = AllEIGRPRouters_v6; /* setup sockaddr */ memset(&sa6, 0, sizeof(sa6)); @@ -131,7 +130,7 @@ send_packet_v6(struct iface *iface, struct nbr *nbr, struct ibuf *buf) sa6.sin6_addr = nbr->addr.v6; addscope(&sa6, iface->ifindex); } else - sa6.sin6_addr = maddr; + sa6.sin6_addr = global.mcast_addr_v6; /* set outgoing interface for multicast traffic */ if (IN6_IS_ADDR_MULTICAST(&sa6.sin6_addr)) @@ -532,7 +531,7 @@ recv_packet_v4(int fd, short event, void *bula) * Packet needs to be sent to AllEIGRPRouters_v4 or to one * of the interface addresses. */ - if (ip_hdr.ip_dst.s_addr != AllEIGRPRouters_v4) { + if (ip_hdr.ip_dst.s_addr != global.mcast_addr_v4.s_addr) { struct if_addr *if_addr; int found = 0; @@ -577,7 +576,6 @@ recv_packet_v6(int fd, short event, void *bula) uint16_t len; unsigned int ifindex = 0; union eigrpd_addr src, dest; - struct in6_addr maddr = AllEIGRPRouters_v6; if (event != EV_READ) return; @@ -627,7 +625,7 @@ recv_packet_v6(int fd, short event, void *bula) * Packet needs to be sent to AllEIGRPRouters_v6 or to the * link local address of the interface. */ - if (!IN6_ARE_ADDR_EQUAL(&dest.v6, &maddr) && + if (!IN6_ARE_ADDR_EQUAL(&dest.v6, &global.mcast_addr_v6) && !IN6_ARE_ADDR_EQUAL(&dest.v6, &iface->linklocal)) { log_debug("%s: packet sent to wrong address %s, interface %s", __func__, log_in6addr(&dest.v6), iface->name);