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.
14 #if defined(linux) && !defined(INET6)
19 #if defined(STDC_HEADERS)
22 #if defined(HAVE_UNISTD_H)
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
31 #include "fetchmail.h"
35 struct in_addr addr, dstaddr, netmask;
36 int rx_packets, tx_packets;
39 struct interface_pair_s {
40 struct in_addr interface_address;
41 struct in_addr interface_mask;
44 static char *netdevfmt;
46 void interface_init(void)
47 /* figure out which /roc/dev/net format to use */
49 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
51 /* pre-linux-2.2 format -- transmit packet count in 8th field */
52 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
60 if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
63 if (major >= 2 && minor >= 2)
64 /* Linux 2.2 -- transmit packet count in 10th field */
65 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d";
69 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
71 /* get active network interface information - return non-zero upon success */
73 int namelen = strlen(ifname);
75 char *cp, buffer[256];
79 /* initialize result */
80 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
82 /* get the packet I/O counts */
83 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
84 for (cp = buffer; *cp && *cp == ' '; ++cp);
85 if (!strncmp(cp, ifname, namelen) &&
88 if (sscanf(cp, netdevfmt,
89 counts, counts+1, counts+2,
90 counts+3,&found)>4) { /* found = dummy */
91 /* newer kernel with byte counts */
92 ifinfo->rx_packets=counts[1];
93 ifinfo->tx_packets=counts[3];
95 /* older kernel, no byte counts */
96 ifinfo->rx_packets=counts[0];
97 ifinfo->tx_packets=counts[2];
102 if (!found) return (FALSE);
104 /* see if the interface is up */
105 strcpy(request.ifr_name, ifname);
106 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
108 if (!(request.ifr_flags & IFF_RUNNING))
111 /* get the IP address */
112 strcpy(request.ifr_name, ifname);
113 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
115 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
117 /* get the PPP destination IP address */
118 strcpy(request.ifr_name, ifname);
119 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
120 ifinfo->dstaddr = ((struct sockaddr_in *)
121 (&request.ifr_dstaddr))->sin_addr;
123 /* get the netmask */
124 strcpy(request.ifr_name, ifname);
125 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
126 ifinfo->netmask = ((struct sockaddr_in *)
127 (&request.ifr_netmask))->sin_addr;
134 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
136 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
137 FILE *stats_file = fopen("/proc/net/dev", "r");
140 if (socket_fd < 0 || !stats_file)
144 char *sp = strchr(ifname, '/');
148 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
159 #ifndef HAVE_INET_ATON
161 * Note: This is not a true replacement for inet_aton(), as it won't
162 * do the right thing on "255.255.255.255" (which translates to -1 on
163 * most machines). Fortunately this code will be used only if you're
164 * on an older Linux that lacks a real implementation.
166 #ifdef HAVE_NETINET_IN_SYSTM_H
167 # include <sys/types.h>
168 # include <netinet/in_systm.h>
171 #include <netinet/in.h>
172 #include <netinet/ip.h>
173 #include <arpa/inet.h>
176 static int inet_aton(const char *cp, struct in_addr *inp) {
179 addr = inet_addr(cp);
180 if (addr == ((long) -1)) return 0;
182 memcpy(inp, &addr, sizeof(addr));
185 #endif /* HAVE_INET_ATON */
187 void interface_parse(char *buf, struct hostdata *hp)
188 /* parse 'interface' specification */
192 hp->interface = xstrdup(buf);
194 /* find and isolate just the IP address */
195 if (!(cp1 = strchr(buf, '/')))
196 (void) report(stderr, PS_SYNTAX,
197 _("missing IP interface address\n"));
200 /* find and isolate just the netmask */
201 if (!(cp2 = strchr(cp1, '/')))
202 cp2 = "255.255.255.255";
206 /* convert IP address and netmask */
207 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
208 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
209 (void) report(stderr, PS_SYNTAX,
210 _("invalid IP interface address\n"));
211 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
212 (void) report(stderr, PS_SYNTAX,
213 _("invalid IP interface mask\n"));
214 /* apply the mask now to the IP address (range) required */
215 hp->interface_pair->interface_address.s_addr &=
216 hp->interface_pair->interface_mask.s_addr;
218 /* restore original interface string (for configuration dumper) */
223 void interface_note_activity(struct hostdata *hp)
224 /* save interface I/O counts */
229 /* if not monitoring link, all done */
233 /* get the current I/O stats for the monitored link */
234 if (get_ifinfo(hp->monitor, &ifinfo))
235 /* update this and preceeding host entries using the link
236 (they were already set during this pass but the I/O
237 count has now changed and they need to be re-updated)
239 for (ctl = querylist; ctl; ctl = ctl->next) {
240 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
241 ctl->server.monitor_io =
242 ifinfo.rx_packets + ifinfo.tx_packets;
243 /* do NOT update host entries following this one */
244 if (&ctl->server == hp)
248 #ifdef ACTIVITY_DEBUG
249 (void) report(stdout, 0,
250 _("activity on %s -noted- as %d\n"),
251 hp->monitor, hp->monitor_io);
255 int interface_approve(struct hostdata *hp)
256 /* return TRUE if OK to poll, FALSE otherwise */
260 /* check interface IP address (range), if specified */
262 /* get interface info */
263 if (!get_ifinfo(hp->interface, &ifinfo)) {
264 (void) report(stdout, 0,
265 _("skipping poll of %s, %s down\n"),
266 hp->pollname, hp->interface);
269 /* check the IP address (range) */
270 if ((ifinfo.addr.s_addr &
271 hp->interface_pair->interface_mask.s_addr) !=
272 hp->interface_pair->interface_address.s_addr) {
273 (void) report(stdout, 0,
274 _("skipping poll of %s, %s IP address excluded\n"),
275 hp->pollname, hp->interface);
280 /* if not monitoring link, all done */
284 #ifdef ACTIVITY_DEBUG
285 (void) report(stdout, 0,
286 _("activity on %s checked as %d\n"),
287 hp->monitor, hp->monitor_io);
289 /* if monitoring, check link for activity if it is up */
290 if (get_ifinfo(hp->monitor, &ifinfo) &&
291 hp->monitor_io == ifinfo.rx_packets +
293 (void) report(stdout, 0,
294 _("skipping poll of %s, %s inactive\n"),
295 hp->pollname, hp->monitor);
299 #ifdef ACTIVITY_DEBUG
300 error(0, 0, _("activity on %s was %d, is %d"),
301 hp->monitor, hp->monitor_io,
302 ifinfo.rx_packets + ifinfo.tx_packets);
307 #endif /* defined(linux) && !defined(INET6) */