2 * interface.c -- implements fetchmail 'interface' and 'monitor' commands
4 * This module was implemented by George M. Sipe <gsipe@mindspring.com>
5 * or <gsipe@acm.org> and is:
7 * Copyright (c) 1996 by George M. Sipe - ALL RIGHTS RESERVED
9 * This is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free
11 * Software Foundation; version 2, or (at your option) any later version.
20 #include <sys/ioctl.h>
21 #include <netinet/in.h>
22 #include <linux/netdevice.h>
24 #include "fetchmail.h"
26 static struct in_addr interface_address;
27 static struct in_addr interface_mask;
29 static int monitor_io = 0;
32 struct in_addr addr, dstaddr, netmask;
33 int rx_packets, tx_packets;
36 /* Get active network interface information. Return non-zero upon success. */
38 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
41 int namelen = strlen(ifname);
43 char *cp, buffer[256];
45 /* initialize result */
46 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
48 /* see if the interface is up */
49 strcpy(request.ifr_name, ifname);
51 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
52 error(PS_IOERR, errno, "interface status check failed");
53 if (!(request.ifr_flags & IFF_RUNNING))
56 /* get the IP address */
57 strcpy(request.ifr_name, ifname);
58 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
60 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
62 /* get the PPP destination IP address */
63 strcpy(request.ifr_name, ifname);
64 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
65 ifinfo->dstaddr = ((struct sockaddr_in *)
66 (&request.ifr_dstaddr))->sin_addr;
69 strcpy(request.ifr_name, ifname);
70 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
71 ifinfo->netmask = ((struct sockaddr_in *)
72 (&request.ifr_netmask))->sin_addr;
74 /* get the packet I/O counts */
75 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
76 for (cp = buffer; *cp && *cp == ' '; ++cp);
77 if (!strncmp(cp, ifname, namelen) &&
80 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
81 &ifinfo->rx_packets, &ifinfo->tx_packets);
88 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
90 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
91 FILE *stats_file = fopen("/proc/net/dev", "r");
94 if (socket_fd < 0 || !stats_file)
97 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
105 /* Parse 'interface' specification. */
107 void interface_parse(void)
111 /* in the event we point to a null string, make pointer null */
112 if (interface && !*interface)
114 if (monitor && !*monitor)
117 /* if no interface specification present, all done */
121 /* find and isolate just the IP address */
122 if (!(cp1 = strchr(interface, '/')))
123 (void) error(PS_SYNTAX, 0, "missing IP interface address");
126 /* find and isolate just the netmask */
127 if (!(cp2 = strchr(cp1, '/')))
128 cp2 = "255.255.255.255";
132 /* convert IP address and netmask */
133 if (!inet_aton(cp1, &interface_address))
134 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
135 if (!inet_aton(cp2, &interface_mask))
136 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
137 /* apply the mask now to the IP address (range) required */
138 interface_address.s_addr &= interface_mask.s_addr;
142 /* Save interface I/O counts. */
144 void interface_note_activity(void)
148 /* get the current I/O stats for the monitored link */
149 if (monitor && get_ifinfo(monitor, &ifinfo))
150 monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
153 /* Return TRUE if OK to poll, FALSE otherwise. */
155 int interface_approve(void)
159 /* check interface IP address (range), if specified */
161 /* get interface info */
162 if (!get_ifinfo(interface, &ifinfo)) {
163 (void) error(0, 0, "skipping poll, %s down",
167 /* check the IP address (range) */
168 if ((ifinfo.addr.s_addr & interface_mask.s_addr) !=
169 interface_address.s_addr) {
171 "skipping poll, %s IP address excluded",
177 /* if monitoring, check link for activity if it is up */
178 if (monitor && get_ifinfo(monitor, &ifinfo) &&
179 monitor_io == ifinfo.rx_packets + ifinfo.tx_packets) {
180 (void) error(0, 0, "skipping poll, %s inactive", monitor);