]> Pileus Git - ~andy/fetchmail/blobdiff - interface.c
The RPM now has an icon.
[~andy/fetchmail] / interface.c
index 39b399223e738315ec464a0baa30c6bf43ba5fa8..370d078d0ad6c6ce833a58d08c6e46cf91237083 100644 (file)
@@ -4,51 +4,81 @@
  * This module was implemented by George M. Sipe <gsipe@mindspring.com>
  * or <gsipe@acm.org> and is:
  *
- *     Copyright (c) 1996 by George M. Sipe - ALL RIGHTS RESERVED
+ *     Copyright (c) 1996,1997 by George M. Sipe - ALL RIGHTS RESERVED
  *
  * This is free software; you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free
  * Software Foundation; version 2, or (at your option) any later version.
  */
 
-#ifdef linux
+#ifdef linux
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
+#if defined(STDC_HEADERS)
+#include <stdlib.h>
+#endif
+#if defined(HAVE_UNISTD_H)
 #include <unistd.h>
+#endif
 #include <sys/ioctl.h>
+#include <sys/socket.h>
 #include <netinet/in.h>
-#include <linux/netdevice.h>
-#include <errno.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include "config.h"
 #include "fetchmail.h"
 
-static struct in_addr interface_address;
-static struct in_addr interface_mask;
-
-static int monitor_io = 0;
-
 typedef struct {
        struct in_addr addr, dstaddr, netmask;
        int rx_packets, tx_packets;
 } ifinfo_t;
 
-/* Get active network interface information.  Return non-zero upon success. */
+struct interface_pair_s {
+       struct in_addr interface_address;
+       struct in_addr interface_mask;
+} *interface_pair;
 
 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
                ifinfo_t *ifinfo)
+/* get active network interface information - return non-zero upon success */
 {
        int namelen = strlen(ifname);
        struct ifreq request;
        char *cp, buffer[256];
+       int found = 0;
+       int counts[4];
 
        /* initialize result */
        memset((char *) ifinfo, 0, sizeof(ifinfo_t));
 
+       /* get the packet I/O counts */
+       while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
+               for (cp = buffer; *cp && *cp == ' '; ++cp);
+               if (!strncmp(cp, ifname, namelen) &&
+                               cp[namelen] == ':') {
+                       cp += namelen + 1;
+                       if (sscanf(cp, "%d %d %*d %*d %*d %d %*d %d %*d %*d"
+                              " %*d %*d %d",counts, counts+1, counts+2, 
+                                  counts+3,&found)>4) { /* found = dummy */
+                               /* newer kernel with byte counts */
+                               ifinfo->rx_packets=counts[1];
+                               ifinfo->tx_packets=counts[3];
+                       } else {
+                               /* older kernel, no byte counts */
+                               ifinfo->rx_packets=counts[0];
+                               ifinfo->tx_packets=counts[2];
+                       }
+                        found = 1;
+               }
+       }
+        if (!found) return (FALSE);
+
        /* see if the interface is up */
        strcpy(request.ifr_name, ifname);
-       errno = 0;
        if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
-               error(PS_IOERR, errno, "interface status check failed");
+               return(FALSE);
        if (!(request.ifr_flags & IFF_RUNNING))
                return(FALSE);
 
@@ -66,21 +96,12 @@ static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
 
        /* get the netmask */
        strcpy(request.ifr_name, ifname);
-       if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
-               ifinfo->netmask = ((struct sockaddr_in *)
-                                       (&request.ifr_netmask))->sin_addr;
+       if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
+          ifinfo->netmask = ((struct sockaddr_in *)
+                             (&request.ifr_netmask))->sin_addr;
+          return (TRUE);
+        }
 
-       /* get the packet I/O counts */
-       while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
-               for (cp = buffer; *cp && *cp == ' '; ++cp);
-               if (!strncmp(cp, ifname, namelen) &&
-                               cp[namelen] == ':') {
-                       cp += namelen + 1;
-                       sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
-                               &ifinfo->rx_packets, &ifinfo->tx_packets);
-                       return(TRUE);
-               }
-       }
        return(FALSE);
 }
 
@@ -91,7 +112,7 @@ static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
        int result;
 
        if (socket_fd < 0 || !stats_file)
-               result = -1;
+               result = FALSE;
        else
                result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
        if (socket_fd >= 0)
@@ -101,26 +122,44 @@ static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
        return(result);
 }
 
-/* Parse 'interface' specification. */
+#ifndef HAVE_INET_ATON
+/*
+ * Note: This is not a true replacement for inet_aton(), as it won't
+ * do the right thing on "255.255.255.255" (which translates to -1 on
+ * most machines).  Fortunately this code will be used only if you're
+ * on an older Linux that lacks a real implementation.
+ */
+#ifdef HAVE_NETINET_IN_SYSTM_H
+# include <sys/types.h>
+# include <netinet/in_systm.h>
+#endif
 
-void interface_parse(void)
-{
-       char *cp1, *cp2;
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <arpa/inet.h>
+#include <string.h>
 
-       /* in the event we point to a null string, make pointer null */
-       if (interface && !*interface)
-               interface = NULL;
-       if (monitor && !*monitor)
-               monitor = NULL;
+static int inet_aton(const char *cp, struct in_addr *inp) {
+    long addr;
 
-       /* if no interface specification present, all done */
-       if (!interface)
-               return;
+    addr = inet_addr(cp);
+    if (addr == ((long) -1)) return 0;
+
+    memcpy(inp, &addr, sizeof(addr));
+    return 1;
+}
+#endif HAVE_INET_ATON
+
+void interface_parse(char *buf, struct hostdata *hp)
+/* parse 'interface' specification */
+{
+       char *cp1, *cp2;
 
        /* find and isolate just the IP address */
-       if (!(cp1 = strchr(interface, '/')))
+       if (!(cp1 = strchr(buf, '/')))
                (void) error(PS_SYNTAX, 0, "missing IP interface address");
        *cp1++ = '\000';
+       hp->interface = xstrdup(buf);
 
        /* find and isolate just the netmask */
        if (!(cp2 = strchr(cp1, '/')))
@@ -129,59 +168,89 @@ void interface_parse(void)
                *cp2++ = '\000';
 
        /* convert IP address and netmask */
-       if (!inet_aton(cp1, &interface_address))
+       hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
+       if (!inet_aton(cp1, &hp->interface_pair->interface_address))
                (void) error(PS_SYNTAX, 0, "invalid IP interface address");
-       if (!inet_aton(cp2, &interface_mask))
+       if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
                (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
        /* apply the mask now to the IP address (range) required */
-       interface_address.s_addr &= interface_mask.s_addr;
+       hp->interface_pair->interface_address.s_addr &=
+               hp->interface_pair->interface_mask.s_addr;
        return;
 }
 
-/* Save interface I/O counts. */
-
-void interface_note_activity(void)
+void interface_note_activity(struct hostdata *hp)
+/* save interface I/O counts */
 {
        ifinfo_t ifinfo;
+       struct query *ctl;
 
-       sleep(3);       /* allow some time for the link to quiesce */
+       /* if not monitoring link, all done */
+       if (!hp->monitor)
+               return;
 
        /* get the current I/O stats for the monitored link */
-       if (monitor && get_ifinfo(monitor, &ifinfo))
-               monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
-}
+       if (get_ifinfo(hp->monitor, &ifinfo))
+               /* update this and preceeding host entries using the link
+                  (they were already set during this pass but the I/O
+                  count has now changed and they need to be re-updated)
+               */
+               for (ctl = querylist; ctl; ctl = ctl->next) {
+                       if (!strcmp(hp->monitor, ctl->server.monitor))
+                               ctl->server.monitor_io =
+                                       ifinfo.rx_packets + ifinfo.tx_packets;
+                       /* do NOT update host entries following this one */
+                       if (&ctl->server == hp)
+                               break;
+               }
 
-/* Return TRUE if OK to poll, FALSE otherwise. */
+#ifdef ACTIVITY_DEBUG
+       (void) error(0, 0, "activity on %s -noted- as %d", 
+               hp->monitor, hp->monitor_io);
+#endif
+}
 
-int interface_approve(void)
+int interface_approve(struct hostdata *hp)
+/* return TRUE if OK to poll, FALSE otherwise */
 {
        ifinfo_t ifinfo;
 
        /* check interface IP address (range), if specified */
-       if (interface) {
+       if (hp->interface) {
                /* get interface info */
-               if (!get_ifinfo(interface, &ifinfo)) {
-                       (void) error(0, 0, "skipping poll, %s down",
-                               interface);
+               if (!get_ifinfo(hp->interface, &ifinfo)) {
+                       (void) error(0, 0, "skipping poll of %s, %s down",
+                               hp->pollname, hp->interface);
                        return(FALSE);
                }
                /* check the IP address (range) */
-               if ((ifinfo.addr.s_addr & interface_mask.s_addr) !=
-                               interface_address.s_addr) {
+               if ((ifinfo.addr.s_addr &
+                               hp->interface_pair->interface_mask.s_addr) !=
+                               hp->interface_pair->interface_address.s_addr) {
                        (void) error(0, 0,
-                          "skipping poll, %s IP address excluded",
-                          interface);
+                               "skipping poll of %s, %s IP address excluded",
+                               hp->pollname, hp->interface);
                        return(FALSE);
                }
        }
 
+       /* if not monitoring link, all done */
+       if (!hp->monitor)
+               return(TRUE);
+
+#ifdef ACTIVITY_DEBUG
+       (void) error(0, 0, "activity on %s checked as %d", 
+               hp->monitor, hp->monitor_io);
+#endif
        /* if monitoring, check link for activity if it is up */
-       if (monitor && get_ifinfo(monitor, &ifinfo) &&
-                       monitor_io == ifinfo.rx_packets + ifinfo.tx_packets) {
-               (void) error(0, 0, "skipping poll, %s inactive", monitor);
+       if (get_ifinfo(hp->monitor, &ifinfo) &&
+                       hp->monitor_io == ifinfo.rx_packets +
+                               ifinfo.tx_packets) {
+               (void) error(0, 0, "skipping poll of %s, %s inactive",
+                       hp->pollname, hp->monitor);
                return(FALSE);
        }
 
        return(TRUE);
 }
-#endif /* linux */
+#endif /* linux */