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 #ifdef HAVE_NEWPROCNETDEV
45 /* Linux 2.2 /proc/net/dev format -- transmit packet count in 10th field */
46 #define PROCNETDEV "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d"
48 /* pre-linux-2.2 format -- transmit packet count in 8th field */
49 #define PROCNETDEV "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d"
52 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
54 /* get active network interface information - return non-zero upon success */
56 int namelen = strlen(ifname);
58 char *cp, buffer[256];
62 /* initialize result */
63 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
65 /* get the packet I/O counts */
66 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
67 for (cp = buffer; *cp && *cp == ' '; ++cp);
68 if (!strncmp(cp, ifname, namelen) &&
71 if (sscanf(cp, PROCNETDEV,
72 counts, counts+1, counts+2,
73 counts+3,&found)>4) { /* found = dummy */
74 /* newer kernel with byte counts */
75 ifinfo->rx_packets=counts[1];
76 ifinfo->tx_packets=counts[3];
78 /* older kernel, no byte counts */
79 ifinfo->rx_packets=counts[0];
80 ifinfo->tx_packets=counts[2];
85 if (!found) return (FALSE);
87 /* see if the interface is up */
88 strcpy(request.ifr_name, ifname);
89 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
91 if (!(request.ifr_flags & IFF_RUNNING))
94 /* get the IP address */
95 strcpy(request.ifr_name, ifname);
96 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
98 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
100 /* get the PPP destination IP address */
101 strcpy(request.ifr_name, ifname);
102 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
103 ifinfo->dstaddr = ((struct sockaddr_in *)
104 (&request.ifr_dstaddr))->sin_addr;
106 /* get the netmask */
107 strcpy(request.ifr_name, ifname);
108 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
109 ifinfo->netmask = ((struct sockaddr_in *)
110 (&request.ifr_netmask))->sin_addr;
117 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
119 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
120 FILE *stats_file = fopen("/proc/net/dev", "r");
123 if (socket_fd < 0 || !stats_file)
127 char *sp = strchr(ifname, '/');
131 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
142 #ifndef HAVE_INET_ATON
144 * Note: This is not a true replacement for inet_aton(), as it won't
145 * do the right thing on "255.255.255.255" (which translates to -1 on
146 * most machines). Fortunately this code will be used only if you're
147 * on an older Linux that lacks a real implementation.
149 #ifdef HAVE_NETINET_IN_SYSTM_H
150 # include <sys/types.h>
151 # include <netinet/in_systm.h>
154 #include <netinet/in.h>
155 #include <netinet/ip.h>
156 #include <arpa/inet.h>
159 static int inet_aton(const char *cp, struct in_addr *inp) {
162 addr = inet_addr(cp);
163 if (addr == ((long) -1)) return 0;
165 memcpy(inp, &addr, sizeof(addr));
168 #endif /* HAVE_INET_ATON */
170 void interface_parse(char *buf, struct hostdata *hp)
171 /* parse 'interface' specification */
175 hp->interface = xstrdup(buf);
177 /* find and isolate just the IP address */
178 if (!(cp1 = strchr(buf, '/')))
179 (void) report(stderr, PS_SYNTAX, _("missing IP interface address"));
182 /* find and isolate just the netmask */
183 if (!(cp2 = strchr(cp1, '/')))
184 cp2 = "255.255.255.255";
188 /* convert IP address and netmask */
189 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
190 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
191 (void) report(stderr, PS_SYNTAX, _("invalid IP interface address"));
192 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
193 (void) report(stderr, PS_SYNTAX, _("invalid IP interface mask"));
194 /* apply the mask now to the IP address (range) required */
195 hp->interface_pair->interface_address.s_addr &=
196 hp->interface_pair->interface_mask.s_addr;
198 /* restore original interface string (for configuration dumper) */
203 void interface_note_activity(struct hostdata *hp)
204 /* save interface I/O counts */
209 /* if not monitoring link, all done */
213 /* get the current I/O stats for the monitored link */
214 if (get_ifinfo(hp->monitor, &ifinfo))
215 /* update this and preceeding host entries using the link
216 (they were already set during this pass but the I/O
217 count has now changed and they need to be re-updated)
219 for (ctl = querylist; ctl; ctl = ctl->next) {
220 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
221 ctl->server.monitor_io =
222 ifinfo.rx_packets + ifinfo.tx_packets;
223 /* do NOT update host entries following this one */
224 if (&ctl->server == hp)
228 #ifdef ACTIVITY_DEBUG
229 (void) report(stdout, 0, _("activity on %s -noted- as %d"),
230 hp->monitor, hp->monitor_io);
234 int interface_approve(struct hostdata *hp)
235 /* return TRUE if OK to poll, FALSE otherwise */
239 /* check interface IP address (range), if specified */
241 /* get interface info */
242 if (!get_ifinfo(hp->interface, &ifinfo)) {
243 (void) report(stdout, 0, _("skipping poll of %s, %s down"),
244 hp->pollname, hp->interface);
247 /* check the IP address (range) */
248 if ((ifinfo.addr.s_addr &
249 hp->interface_pair->interface_mask.s_addr) !=
250 hp->interface_pair->interface_address.s_addr) {
251 (void) report(stdout, 0,
252 _("skipping poll of %s, %s IP address excluded"),
253 hp->pollname, hp->interface);
258 /* if not monitoring link, all done */
262 #ifdef ACTIVITY_DEBUG
263 (void) report(stdout, 0, _("activity on %s checked as %d"),
264 hp->monitor, hp->monitor_io);
266 /* if monitoring, check link for activity if it is up */
267 if (get_ifinfo(hp->monitor, &ifinfo) &&
268 hp->monitor_io == ifinfo.rx_packets +
270 (void) report(stdout, 0, _("skipping poll of %s, %s inactive"),
271 hp->pollname, hp->monitor);
275 #ifdef ACTIVITY_DEBUG
276 error(0, 0, _("activity on %s was %d, is %d"),
277 hp->monitor, hp->monitor_io,
278 ifinfo.rx_packets + ifinfo.tx_packets);
283 #endif /* defined(linux) && !defined(INET6) */