Skip to content

Commit

Permalink
Simplify handling of multicast addresses.
Browse files Browse the repository at this point in the history
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@
  • Loading branch information
rwestphal committed Sep 2, 2016
1 parent 3928a7e commit 54fe48e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
9 changes: 3 additions & 6 deletions eigrp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 2 additions & 0 deletions eigrpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
5 changes: 5 additions & 0 deletions eigrpe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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");
Expand Down
10 changes: 4 additions & 6 deletions packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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))
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 54fe48e

Please sign in to comment.