From 645dadb3fcc466e8880fda4eb23b21ad433631fc Mon Sep 17 00:00:00 2001
From: Marc Fournier <marc.fournier@camptocamp.com>
Date: Tue, 7 Jan 2014 16:06:10 +0100
Subject: [PATCH 04/22] interface.c: FreeBSD-10 support

Quoting @trtrmitya in github issue #506 : "[...] it is broken on
FreeBSD-10, in which getifaddrs() returns not only link level stats for
a particular interface, but also entries for each IP configured on that
interface. As a result if_submit() is called several times for each
interface, which results in incorrect data being logged.

I am attaching a patch which fixes a problem on FreeBSD (9/10), but it
should work for every *BSD because [...] the getifaddrs implementation
first appeared in BSDi BSD/OS."

Many thanks to @trtrmitya for providing the patch !
---
 src/interface.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/interface.c b/src/interface.c
index db998a3..9b566ea 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -213,18 +213,19 @@ static int interface_read (void)
 
 	for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
 	{
-		if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
-			continue;
+		if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
+			if_data = (struct IFA_DATA *) if_ptr->ifa_data;
 
-		if_submit (if_ptr->ifa_name, "if_octets",
+			if_submit (if_ptr->ifa_name, "if_octets",
 				if_data->IFA_RX_BYTES,
 				if_data->IFA_TX_BYTES);
-		if_submit (if_ptr->ifa_name, "if_packets",
+			if_submit (if_ptr->ifa_name, "if_packets",
 				if_data->IFA_RX_PACKT,
 				if_data->IFA_TX_PACKT);
-		if_submit (if_ptr->ifa_name, "if_errors",
+			if_submit (if_ptr->ifa_name, "if_errors",
 				if_data->IFA_RX_ERROR,
 				if_data->IFA_TX_ERROR);
+		}
 	}
 
 	freeifaddrs (if_list);
-- 
1.9.3

