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.
18 #if defined(STDC_HEADERS)
21 #if defined(HAVE_UNISTD_H)
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
28 #include "fetchmail.h"
31 struct in_addr addr, dstaddr, netmask;
32 int rx_packets, tx_packets;
35 struct interface_pair_s {
36 struct in_addr interface_address;
37 struct in_addr interface_mask;
40 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
42 /* get active network interface information - return non-zero upon success */
44 int namelen = strlen(ifname);
46 char *cp, buffer[256];
48 /* initialize result */
49 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
51 /* see if the interface is up */
52 strcpy(request.ifr_name, ifname);
53 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
55 if (!(request.ifr_flags & IFF_RUNNING))
58 /* get the IP address */
59 strcpy(request.ifr_name, ifname);
60 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
62 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
64 /* get the PPP destination IP address */
65 strcpy(request.ifr_name, ifname);
66 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
67 ifinfo->dstaddr = ((struct sockaddr_in *)
68 (&request.ifr_dstaddr))->sin_addr;
71 strcpy(request.ifr_name, ifname);
72 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
73 ifinfo->netmask = ((struct sockaddr_in *)
74 (&request.ifr_netmask))->sin_addr;
76 /* get the packet I/O counts */
77 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
78 for (cp = buffer; *cp && *cp == ' '; ++cp);
79 if (!strncmp(cp, ifname, namelen) &&
82 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
83 &ifinfo->rx_packets, &ifinfo->tx_packets);
90 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
92 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
93 FILE *stats_file = fopen("/proc/net/dev", "r");
96 if (socket_fd < 0 || !stats_file)
99 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
107 void interface_parse(char *buf, struct hostdata *hp)
108 /* parse 'interface' specification */
112 /* find and isolate just the IP address */
113 if (!(cp1 = strchr(buf, '/')))
114 (void) error(PS_SYNTAX, 0, "missing IP interface address");
116 hp->interface = xstrdup(buf);
118 /* find and isolate just the netmask */
119 if (!(cp2 = strchr(cp1, '/')))
120 cp2 = "255.255.255.255";
124 /* convert IP address and netmask */
125 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
126 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
127 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
128 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
129 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
130 /* apply the mask now to the IP address (range) required */
131 hp->interface_pair->interface_address.s_addr &=
132 hp->interface_pair->interface_mask.s_addr;
136 void interface_note_activity(struct hostdata *hp)
137 /* save interface I/O counts */
141 /* if not monitoring link, all done */
145 /* get the current I/O stats for the monitored link */
146 if (get_ifinfo(hp->monitor, &ifinfo))
147 hp->monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
149 #ifdef ACTIVITY_DEBUG
150 (void) error(0, 0, "activity on %s -noted- as %d",
151 hp->names->id, hp->monitor_io);
155 int interface_approve(struct hostdata *hp)
156 /* return TRUE if OK to poll, FALSE otherwise */
160 /* check interface IP address (range), if specified */
162 /* get interface info */
163 if (!get_ifinfo(hp->interface, &ifinfo)) {
164 (void) error(0, 0, "skipping poll of %s, %s down",
165 hp->names->id, hp->interface);
168 /* check the IP address (range) */
169 if ((ifinfo.addr.s_addr &
170 hp->interface_pair->interface_mask.s_addr) !=
171 hp->interface_pair->interface_address.s_addr) {
173 "skipping poll of %s, %s IP address excluded",
174 hp->names->id, hp->interface);
179 /* if not monitoring link, all done */
183 #ifdef ACTIVITY_DEBUG
184 (void) error(0, 0, "activity on %s checked as %d",
185 hp->names->id, hp->monitor_io);
187 /* if monitoring, check link for activity if it is up */
188 if (get_ifinfo(hp->monitor, &ifinfo) &&
189 hp->monitor_io == ifinfo.rx_packets +
191 (void) error(0, 0, "skipping poll of %s, %s inactive",
192 hp->names->id, hp->monitor);