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,1997 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.
19 #include <sys/ioctl.h>
20 #include <netinet/in.h>
21 #include <linux/netdevice.h>
22 #include "fetchmail.h"
25 struct in_addr addr, dstaddr, netmask;
26 int rx_packets, tx_packets;
29 struct interface_pair_s {
30 struct in_addr interface_address;
31 struct in_addr interface_mask;
34 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
36 /* get active network interface information - return non-zero upon success */
38 int namelen = strlen(ifname);
40 char *cp, buffer[256];
42 /* initialize result */
43 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
45 /* see if the interface is up */
46 strcpy(request.ifr_name, ifname);
47 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
49 if (!(request.ifr_flags & IFF_RUNNING))
52 /* get the IP address */
53 strcpy(request.ifr_name, ifname);
54 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
56 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
58 /* get the PPP destination IP address */
59 strcpy(request.ifr_name, ifname);
60 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
61 ifinfo->dstaddr = ((struct sockaddr_in *)
62 (&request.ifr_dstaddr))->sin_addr;
65 strcpy(request.ifr_name, ifname);
66 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
67 ifinfo->netmask = ((struct sockaddr_in *)
68 (&request.ifr_netmask))->sin_addr;
70 /* get the packet I/O counts */
71 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
72 for (cp = buffer; *cp && *cp == ' '; ++cp);
73 if (!strncmp(cp, ifname, namelen) &&
76 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
77 &ifinfo->rx_packets, &ifinfo->tx_packets);
84 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
86 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
87 FILE *stats_file = fopen("/proc/net/dev", "r");
90 if (socket_fd < 0 || !stats_file)
93 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
101 void interface_parse(struct hostdata *hp)
102 /* parse 'interface' specification */
106 /* if no interface specification present, all done */
110 /* find and isolate just the IP address */
111 if (!(cp1 = strchr(hp->interface, '/')))
112 (void) error(PS_SYNTAX, 0, "missing IP interface address");
115 /* find and isolate just the netmask */
116 if (!(cp2 = strchr(cp1, '/')))
117 cp2 = "255.255.255.255";
121 /* convert IP address and netmask */
122 hp->interface_pair = xmalloc(sizeof(struct interface_pair_s));
123 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
124 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
125 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
126 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
127 /* apply the mask now to the IP address (range) required */
128 hp->interface_pair->interface_address.s_addr &=
129 hp->interface_pair->interface_mask.s_addr;
133 void interface_note_activity(struct hostdata *hp)
134 /* save interface I/O counts */
138 /* if not monitoring link, all done */
142 /* get the current I/O stats for the monitored link */
143 if (get_ifinfo(hp->monitor, &ifinfo))
144 hp->monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
146 #ifdef ACTIVITY_DEBUG
147 (void) error(0, 0, "activity on %s -noted- as %d",
148 hp->names->id, hp->monitor_io);
152 int interface_approve(struct hostdata *hp)
153 /* return TRUE if OK to poll, FALSE otherwise */
157 /* check interface IP address (range), if specified */
159 /* get interface info */
160 if (!get_ifinfo(hp->interface, &ifinfo)) {
161 (void) error(0, 0, "skipping poll of %s, %s down",
162 hp->names->id, hp->interface);
165 /* check the IP address (range) */
166 if ((ifinfo.addr.s_addr &
167 hp->interface_pair->interface_mask.s_addr) !=
168 hp->interface_pair->interface_address.s_addr) {
170 "skipping poll of %s, %s IP address excluded",
171 hp->names->id, hp->interface);
176 /* if not monitoring link, all done */
180 #ifdef ACTIVITY_DEBUG
181 (void) error(0, 0, "activity on %s checked as %d",
182 hp->names->id, hp->monitor_io);
184 /* if monitoring, check link for activity if it is up */
185 if (get_ifinfo(hp->monitor, &ifinfo) &&
186 hp->monitor_io == ifinfo.rx_packets +
188 (void) error(0, 0, "skipping poll of %s, %s inactive",
189 hp->names->id, hp->monitor);