Merge tag 'upstream/3.0.717'
[darkstat-debian] / acct.c
diff --git a/acct.c b/acct.c
index 62ad076..17aa4f0 100644 (file)
--- a/acct.c
+++ b/acct.c
@@ -133,34 +133,23 @@ acct_init_localnet(const char *spec)
    verbosef("   local network mask: %s", addr_to_str(&localmask));
 }
 
-static int
-addr_is_local(const struct addr * const a)
-{
-   if (a->family == IPv4) {
-      if (using_localnet4) {
-         if (addr_inside(a, &localnet4, &localmask4))
-            return 1;
-      } else {
-         if (addr_equal(a, &localip4))
-            return 1;
-      }
-   } else {
-      assert(a->family == IPv6);
-      if (using_localnet6) {
-         if (addr_inside(a, &localnet6, &localmask6))
-            return 1;
-      } else {
-         if (addr_equal(a, &localip6))
-            return 1;
-      }
+static int addr_is_local(const struct addr * const a,
+                         const struct local_ips *local_ips) {
+   if (is_localip(a, local_ips))
+      return 1;
+   if (a->family == IPv4 && using_localnet4) {
+      if (addr_inside(a, &localnet4, &localmask4))
+         return 1;
+   } else if (a->family == IPv6 && using_localnet6) {
+      if (addr_inside(a, &localnet6, &localmask6))
+         return 1;
    }
    return 0;
 }
 
 /* Account for the given packet summary. */
-void
-acct_for(const struct pktsummary * const sm)
-{
+void acct_for(const struct pktsummary * const sm,
+              const struct local_ips * const local_ips) {
    struct bucket *hs = NULL, *hd = NULL;
    struct bucket *ps, *pd;
    int dir_in, dir_out;
@@ -189,18 +178,15 @@ acct_for(const struct pktsummary * const sm)
    acct_total_bytes += sm->len;
 
    /* Graphs. */
-   dir_out = addr_is_local(&(sm->src));
-   dir_in  = addr_is_local(&(sm->dst));
+   dir_out = addr_is_local(&sm->src, local_ips);
+   dir_in  = addr_is_local(&sm->dst, local_ips);
 
    /* Traffic staying within the network isn't counted. */
-   if (dir_in == 1 && dir_out == 1)
-      dir_in = dir_out = 0;
-
-   if (dir_out) {
+   if (dir_out && !dir_in) {
       daylog_acct((uint64_t)sm->len, GRAPH_OUT);
       graph_acct((uint64_t)sm->len, GRAPH_OUT);
    }
-   if (dir_in) {
+   if (dir_in && !dir_out) {
       daylog_acct((uint64_t)sm->len, GRAPH_IN);
       graph_acct((uint64_t)sm->len, GRAPH_IN);
    }
@@ -209,15 +195,15 @@ acct_for(const struct pktsummary * const sm)
 
    /* Hosts. */
    hosts_db_reduce();
-   if (!opt_want_local_only || addr_is_local(&sm->src)) {
+   if (!opt_want_local_only || dir_out) {
       hs = host_get(&(sm->src));
       hs->out   += sm->len;
       hs->total += sm->len;
       memcpy(hs->u.host.mac_addr, sm->src_mac, sizeof(sm->src_mac));
-      hs->u.host.lastseen = now;
+      hs->u.host.last_seen_mono = now_mono();
    }
 
-   if (!opt_want_local_only || addr_is_local(&sm->dst)) {
+   if (!opt_want_local_only || dir_in) {
       hd = host_get(&(sm->dst));
       hd->in    += sm->len;
       hd->total += sm->len;
@@ -274,16 +260,12 @@ acct_for(const struct pktsummary * const sm)
       }
       break;
 
-   case IPPROTO_ICMP:
-   case IPPROTO_ICMPV6:
-   case IPPROTO_AH:
-   case IPPROTO_ESP:
-   case IPPROTO_OSPF:
-      /* known protocol, don't complain about it */
+   case IPPROTO_INVALID:
+      /* proto decoding failed, don't complain in accounting */
       break;
 
    default:
-      verbosef("unknown IP proto (%04x)", sm->proto);
+      verbosef("acct_for: unknown IP protocol 0x%02x", sm->proto);
    }
 }