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 void interface_parse(char *buf, struct hostdata *hp)
114 /* parse 'interface' specification */
118 /* find and isolate just the IP address */
119 if (!(cp1 = strchr(buf, '/')))
120 (void) error(PS_SYNTAX, 0, "missing IP interface address");
122 hp->interface = xstrdup(buf);
124 /* find and isolate just the netmask */
125 if (!(cp2 = strchr(cp1, '/')))
126 cp2 = "255.255.255.255";
130 /* convert IP address and netmask */
131 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
132 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
133 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
134 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
135 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
136 /* apply the mask now to the IP address (range) required */
137 hp->interface_pair->interface_address.s_addr &=
138 hp->interface_pair->interface_mask.s_addr;
142 void interface_note_activity(struct hostdata *hp)
143 /* save interface I/O counts */
148 /* if not monitoring link, all done */
152 /* get the current I/O stats for the monitored link */
153 if (get_ifinfo(hp->monitor, &ifinfo))
154 /* update this and preceeding host entries using the link
155 (they were already set during this pass but the I/O
156 count has now changed and they need to be re-updated)
158 for (ctl = querylist; ctl; ctl = ctl->next) {
159 if (!strcmp(hp->monitor, ctl->server.monitor))
160 ctl->server.monitor_io =
161 ifinfo.rx_packets + ifinfo.tx_packets;
162 /* do NOT update host entries following this one */
163 if (&ctl->server == hp)
167 #ifdef ACTIVITY_DEBUG
168 (void) error(0, 0, "activity on %s -noted- as %d",
169 hp->monitor, hp->monitor_io);
173 int interface_approve(struct hostdata *hp)
174 /* return TRUE if OK to poll, FALSE otherwise */
178 /* check interface IP address (range), if specified */
180 /* get interface info */
181 if (!get_ifinfo(hp->interface, &ifinfo)) {
182 (void) error(0, 0, "skipping poll of %s, %s down",
183 hp->pollname, hp->interface);
186 /* check the IP address (range) */
187 if ((ifinfo.addr.s_addr &
188 hp->interface_pair->interface_mask.s_addr) !=
189 hp->interface_pair->interface_address.s_addr) {
191 "skipping poll of %s, %s IP address excluded",
192 hp->pollname, hp->interface);
197 /* if not monitoring link, all done */
201 #ifdef ACTIVITY_DEBUG
202 (void) error(0, 0, "activity on %s checked as %d",
203 hp->monitor, hp->monitor_io);
205 /* if monitoring, check link for activity if it is up */
206 if (get_ifinfo(hp->monitor, &ifinfo) &&
207 hp->monitor_io == ifinfo.rx_packets +
209 (void) error(0, 0, "skipping poll of %s, %s inactive",
210 hp->pollname, hp->monitor);