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 #if defined(STDC_HEADERS)
22 #if defined(HAVE_UNISTD_H)
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
29 #include "fetchmail.h"
32 struct in_addr addr, dstaddr, netmask;
33 int rx_packets, tx_packets;
36 struct interface_pair_s {
37 struct in_addr interface_address;
38 struct in_addr interface_mask;
41 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
43 /* get active network interface information - return non-zero upon success */
45 int namelen = strlen(ifname);
47 char *cp, buffer[256];
50 /* initialize result */
51 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
53 /* get the packet I/O counts */
54 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
55 for (cp = buffer; *cp && *cp == ' '; ++cp);
56 if (!strncmp(cp, ifname, namelen) &&
59 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
60 &ifinfo->rx_packets, &ifinfo->tx_packets);
64 if (!found) return (FALSE);
66 /* see if the interface is up */
67 strcpy(request.ifr_name, ifname);
68 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
70 if (!(request.ifr_flags & IFF_RUNNING))
73 /* get the IP address */
74 strcpy(request.ifr_name, ifname);
75 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
77 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
79 /* get the PPP destination IP address */
80 strcpy(request.ifr_name, ifname);
81 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
82 ifinfo->dstaddr = ((struct sockaddr_in *)
83 (&request.ifr_dstaddr))->sin_addr;
86 strcpy(request.ifr_name, ifname);
87 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
88 ifinfo->netmask = ((struct sockaddr_in *)
89 (&request.ifr_netmask))->sin_addr;
96 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
98 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
99 FILE *stats_file = fopen("/proc/net/dev", "r");
102 if (socket_fd < 0 || !stats_file)
105 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
113 #ifndef HAVE_INET_ATON
115 * Note: This is not a true replacement for inet_aton(), as it won't
116 * do the right thing on "255.255.255.255" (which translates to -1 on
117 * most machines). Fortunately this code will be used only if you're
118 * on an older Linux that lacks a real implementation.
120 #ifdef HAVE_NETINET_IN_SYSTM_H
121 # include <sys/types.h>
122 # include <netinet/in_systm.h>
125 #include <netinet/in.h>
126 #include <netinet/ip.h>
127 #include <arpa/inet.h>
130 static int inet_aton(const char *cp, struct in_addr *inp) {
133 addr = inet_addr(cp);
134 if (addr == ((long) -1)) return 0;
136 memcpy(inp, &addr, sizeof(addr));
139 #endif HAVE_INET_ATON
141 void interface_parse(char *buf, struct hostdata *hp)
142 /* parse 'interface' specification */
146 /* find and isolate just the IP address */
147 if (!(cp1 = strchr(buf, '/')))
148 (void) error(PS_SYNTAX, 0, "missing IP interface address");
150 hp->interface = xstrdup(buf);
152 /* find and isolate just the netmask */
153 if (!(cp2 = strchr(cp1, '/')))
154 cp2 = "255.255.255.255";
158 /* convert IP address and netmask */
159 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
160 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
161 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
162 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
163 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
164 /* apply the mask now to the IP address (range) required */
165 hp->interface_pair->interface_address.s_addr &=
166 hp->interface_pair->interface_mask.s_addr;
170 void interface_note_activity(struct hostdata *hp)
171 /* save interface I/O counts */
176 /* if not monitoring link, all done */
180 /* get the current I/O stats for the monitored link */
181 if (get_ifinfo(hp->monitor, &ifinfo))
182 /* update this and preceeding host entries using the link
183 (they were already set during this pass but the I/O
184 count has now changed and they need to be re-updated)
186 for (ctl = querylist; ctl; ctl = ctl->next) {
187 if (!strcmp(hp->monitor, ctl->server.monitor))
188 ctl->server.monitor_io =
189 ifinfo.rx_packets + ifinfo.tx_packets;
190 /* do NOT update host entries following this one */
191 if (&ctl->server == hp)
195 #ifdef ACTIVITY_DEBUG
196 (void) error(0, 0, "activity on %s -noted- as %d",
197 hp->monitor, hp->monitor_io);
201 int interface_approve(struct hostdata *hp)
202 /* return TRUE if OK to poll, FALSE otherwise */
206 /* check interface IP address (range), if specified */
208 /* get interface info */
209 if (!get_ifinfo(hp->interface, &ifinfo)) {
210 (void) error(0, 0, "skipping poll of %s, %s down",
211 hp->pollname, hp->interface);
214 /* check the IP address (range) */
215 if ((ifinfo.addr.s_addr &
216 hp->interface_pair->interface_mask.s_addr) !=
217 hp->interface_pair->interface_address.s_addr) {
219 "skipping poll of %s, %s IP address excluded",
220 hp->pollname, hp->interface);
225 /* if not monitoring link, all done */
229 #ifdef ACTIVITY_DEBUG
230 (void) error(0, 0, "activity on %s checked as %d",
231 hp->monitor, hp->monitor_io);
233 /* if monitoring, check link for activity if it is up */
234 if (get_ifinfo(hp->monitor, &ifinfo) &&
235 hp->monitor_io == ifinfo.rx_packets +
237 (void) error(0, 0, "skipping poll of %s, %s inactive",
238 hp->pollname, hp->monitor);