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];
49 /* initialize result */
50 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
52 /* see if the interface is up */
53 strcpy(request.ifr_name, ifname);
54 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
56 if (!(request.ifr_flags & IFF_RUNNING))
59 /* get the IP address */
60 strcpy(request.ifr_name, ifname);
61 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
63 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
65 /* get the PPP destination IP address */
66 strcpy(request.ifr_name, ifname);
67 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
68 ifinfo->dstaddr = ((struct sockaddr_in *)
69 (&request.ifr_dstaddr))->sin_addr;
72 strcpy(request.ifr_name, ifname);
73 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
74 ifinfo->netmask = ((struct sockaddr_in *)
75 (&request.ifr_netmask))->sin_addr;
77 /* get the packet I/O counts */
78 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
79 for (cp = buffer; *cp && *cp == ' '; ++cp);
80 if (!strncmp(cp, ifname, namelen) &&
83 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
84 &ifinfo->rx_packets, &ifinfo->tx_packets);
91 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
93 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
94 FILE *stats_file = fopen("/proc/net/dev", "r");
97 if (socket_fd < 0 || !stats_file)
100 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
108 void interface_parse(char *buf, struct hostdata *hp)
109 /* parse 'interface' specification */
113 /* find and isolate just the IP address */
114 if (!(cp1 = strchr(buf, '/')))
115 (void) error(PS_SYNTAX, 0, "missing IP interface address");
117 hp->interface = xstrdup(buf);
119 /* find and isolate just the netmask */
120 if (!(cp2 = strchr(cp1, '/')))
121 cp2 = "255.255.255.255";
125 /* convert IP address and netmask */
126 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
127 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
128 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
129 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
130 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
131 /* apply the mask now to the IP address (range) required */
132 hp->interface_pair->interface_address.s_addr &=
133 hp->interface_pair->interface_mask.s_addr;
137 void interface_note_activity(struct hostdata *hp)
138 /* save interface I/O counts */
143 /* if not monitoring link, all done */
147 /* get the current I/O stats for the monitored link */
148 if (get_ifinfo(hp->monitor, &ifinfo))
149 /* update this and preceeding host entries using the link
150 (they were already set during this pass but the I/O
151 count has now changed and they need to be re-updated)
153 for (ctl = querylist; ctl; ctl = ctl->next) {
154 if (!strcmp(hp->monitor, ctl->server.monitor))
155 ctl->server.monitor_io =
156 ifinfo.rx_packets + ifinfo.tx_packets;
157 /* do NOT update host entries following this one */
158 if (&ctl->server == hp)
162 #ifdef ACTIVITY_DEBUG
163 (void) error(0, 0, "activity on %s -noted- as %d",
164 hp->monitor, hp->monitor_io);
168 int interface_approve(struct hostdata *hp)
169 /* return TRUE if OK to poll, FALSE otherwise */
173 /* check interface IP address (range), if specified */
175 /* get interface info */
176 if (!get_ifinfo(hp->interface, &ifinfo)) {
177 (void) error(0, 0, "skipping poll of %s, %s down",
178 hp->names->id, hp->interface);
181 /* check the IP address (range) */
182 if ((ifinfo.addr.s_addr &
183 hp->interface_pair->interface_mask.s_addr) !=
184 hp->interface_pair->interface_address.s_addr) {
186 "skipping poll of %s, %s IP address excluded",
187 hp->names->id, hp->interface);
192 /* if not monitoring link, all done */
196 #ifdef ACTIVITY_DEBUG
197 (void) error(0, 0, "activity on %s checked as %d",
198 hp->monitor, hp->monitor_io);
200 /* if monitoring, check link for activity if it is up */
201 if (get_ifinfo(hp->monitor, &ifinfo) &&
202 hp->monitor_io == ifinfo.rx_packets +
204 (void) error(0, 0, "skipping poll of %s, %s inactive",
205 hp->names->id, hp->monitor);