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"
34 struct in_addr addr, dstaddr, netmask;
35 int rx_packets, tx_packets;
38 struct interface_pair_s {
39 struct in_addr interface_address;
40 struct in_addr interface_mask;
43 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
45 /* get active network interface information - return non-zero upon success */
47 int namelen = strlen(ifname);
49 char *cp, buffer[256];
53 /* initialize result */
54 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
56 /* get the packet I/O counts */
57 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
58 for (cp = buffer; *cp && *cp == ' '; ++cp);
59 if (!strncmp(cp, ifname, namelen) &&
62 if (sscanf(cp, "%d %d %*d %*d %*d %d %*d %d %*d %*d"
63 " %*d %*d %d",counts, counts+1, counts+2,
64 counts+3,&found)>4) { /* found = dummy */
65 /* newer kernel with byte counts */
66 ifinfo->rx_packets=counts[1];
67 ifinfo->tx_packets=counts[3];
69 /* older kernel, no byte counts */
70 ifinfo->rx_packets=counts[0];
71 ifinfo->tx_packets=counts[2];
76 if (!found) return (FALSE);
78 /* see if the interface is up */
79 strcpy(request.ifr_name, ifname);
80 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
82 if (!(request.ifr_flags & IFF_RUNNING))
85 /* get the IP address */
86 strcpy(request.ifr_name, ifname);
87 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
89 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
91 /* get the PPP destination IP address */
92 strcpy(request.ifr_name, ifname);
93 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
94 ifinfo->dstaddr = ((struct sockaddr_in *)
95 (&request.ifr_dstaddr))->sin_addr;
98 strcpy(request.ifr_name, ifname);
99 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
100 ifinfo->netmask = ((struct sockaddr_in *)
101 (&request.ifr_netmask))->sin_addr;
108 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
110 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
111 FILE *stats_file = fopen("/proc/net/dev", "r");
114 if (socket_fd < 0 || !stats_file)
117 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
125 #ifndef HAVE_INET_ATON
127 * Note: This is not a true replacement for inet_aton(), as it won't
128 * do the right thing on "255.255.255.255" (which translates to -1 on
129 * most machines). Fortunately this code will be used only if you're
130 * on an older Linux that lacks a real implementation.
132 #ifdef HAVE_NETINET_IN_SYSTM_H
133 # include <sys/types.h>
134 # include <netinet/in_systm.h>
137 #include <netinet/in.h>
138 #include <netinet/ip.h>
139 #include <arpa/inet.h>
142 static int inet_aton(const char *cp, struct in_addr *inp) {
145 addr = inet_addr(cp);
146 if (addr == ((long) -1)) return 0;
148 memcpy(inp, &addr, sizeof(addr));
151 #endif HAVE_INET_ATON
153 void interface_parse(char *buf, struct hostdata *hp)
154 /* parse 'interface' specification */
158 /* find and isolate just the IP address */
159 if (!(cp1 = strchr(buf, '/')))
160 (void) error(PS_SYNTAX, 0, "missing IP interface address");
162 hp->interface = xstrdup(buf);
164 /* find and isolate just the netmask */
165 if (!(cp2 = strchr(cp1, '/')))
166 cp2 = "255.255.255.255";
170 /* convert IP address and netmask */
171 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
172 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
173 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
174 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
175 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
176 /* apply the mask now to the IP address (range) required */
177 hp->interface_pair->interface_address.s_addr &=
178 hp->interface_pair->interface_mask.s_addr;
182 void interface_note_activity(struct hostdata *hp)
183 /* save interface I/O counts */
188 /* if not monitoring link, all done */
192 /* get the current I/O stats for the monitored link */
193 if (get_ifinfo(hp->monitor, &ifinfo))
194 /* update this and preceeding host entries using the link
195 (they were already set during this pass but the I/O
196 count has now changed and they need to be re-updated)
198 for (ctl = querylist; ctl; ctl = ctl->next) {
199 if (!strcmp(hp->monitor, ctl->server.monitor))
200 ctl->server.monitor_io =
201 ifinfo.rx_packets + ifinfo.tx_packets;
202 /* do NOT update host entries following this one */
203 if (&ctl->server == hp)
207 #ifdef ACTIVITY_DEBUG
208 (void) error(0, 0, "activity on %s -noted- as %d",
209 hp->monitor, hp->monitor_io);
213 int interface_approve(struct hostdata *hp)
214 /* return TRUE if OK to poll, FALSE otherwise */
218 /* check interface IP address (range), if specified */
220 /* get interface info */
221 if (!get_ifinfo(hp->interface, &ifinfo)) {
222 (void) error(0, 0, "skipping poll of %s, %s down",
223 hp->pollname, hp->interface);
226 /* check the IP address (range) */
227 if ((ifinfo.addr.s_addr &
228 hp->interface_pair->interface_mask.s_addr) !=
229 hp->interface_pair->interface_address.s_addr) {
231 "skipping poll of %s, %s IP address excluded",
232 hp->pollname, hp->interface);
237 /* if not monitoring link, all done */
241 #ifdef ACTIVITY_DEBUG
242 (void) error(0, 0, "activity on %s checked as %d",
243 hp->monitor, hp->monitor_io);
245 /* if monitoring, check link for activity if it is up */
246 if (get_ifinfo(hp->monitor, &ifinfo) &&
247 hp->monitor_io == ifinfo.rx_packets +
249 (void) error(0, 0, "skipping poll of %s, %s inactive",
250 hp->pollname, hp->monitor);
256 #endif /* defined(linux) && !defined(INET6) */