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>
27 #include "fetchmail.h"
30 struct in_addr addr, dstaddr, netmask;
31 int rx_packets, tx_packets;
34 struct interface_pair_s {
35 struct in_addr interface_address;
36 struct in_addr interface_mask;
39 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
41 /* get active network interface information - return non-zero upon success */
43 int namelen = strlen(ifname);
45 char *cp, buffer[256];
47 /* initialize result */
48 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
50 /* see if the interface is up */
51 strcpy(request.ifr_name, ifname);
52 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
54 if (!(request.ifr_flags & IFF_RUNNING))
57 /* get the IP address */
58 strcpy(request.ifr_name, ifname);
59 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
61 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
63 /* get the PPP destination IP address */
64 strcpy(request.ifr_name, ifname);
65 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
66 ifinfo->dstaddr = ((struct sockaddr_in *)
67 (&request.ifr_dstaddr))->sin_addr;
70 strcpy(request.ifr_name, ifname);
71 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
72 ifinfo->netmask = ((struct sockaddr_in *)
73 (&request.ifr_netmask))->sin_addr;
75 /* get the packet I/O counts */
76 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
77 for (cp = buffer; *cp && *cp == ' '; ++cp);
78 if (!strncmp(cp, ifname, namelen) &&
81 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
82 &ifinfo->rx_packets, &ifinfo->tx_packets);
89 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
91 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
92 FILE *stats_file = fopen("/proc/net/dev", "r");
95 if (socket_fd < 0 || !stats_file)
98 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
106 void interface_parse(char *buf, struct hostdata *hp)
107 /* parse 'interface' specification */
111 /* find and isolate just the IP address */
112 if (!(cp1 = strchr(buf, '/')))
113 (void) error(PS_SYNTAX, 0, "missing IP interface address");
115 hp->interface = xstrdup(buf);
117 /* find and isolate just the netmask */
118 if (!(cp2 = strchr(cp1, '/')))
119 cp2 = "255.255.255.255";
123 /* convert IP address and netmask */
124 hp->interface_pair = xmalloc(sizeof(struct interface_pair_s));
125 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
126 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
127 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
128 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
129 /* apply the mask now to the IP address (range) required */
130 hp->interface_pair->interface_address.s_addr &=
131 hp->interface_pair->interface_mask.s_addr;
135 void interface_note_activity(struct hostdata *hp)
136 /* save interface I/O counts */
140 /* if not monitoring link, all done */
144 /* get the current I/O stats for the monitored link */
145 if (get_ifinfo(hp->monitor, &ifinfo))
146 hp->monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
148 #ifdef ACTIVITY_DEBUG
149 (void) error(0, 0, "activity on %s -noted- as %d",
150 hp->names->id, hp->monitor_io);
154 int interface_approve(struct hostdata *hp)
155 /* return TRUE if OK to poll, FALSE otherwise */
159 /* check interface IP address (range), if specified */
161 /* get interface info */
162 if (!get_ifinfo(hp->interface, &ifinfo)) {
163 (void) error(0, 0, "skipping poll of %s, %s down",
164 hp->names->id, hp->interface);
167 /* check the IP address (range) */
168 if ((ifinfo.addr.s_addr &
169 hp->interface_pair->interface_mask.s_addr) !=
170 hp->interface_pair->interface_address.s_addr) {
172 "skipping poll of %s, %s IP address excluded",
173 hp->names->id, hp->interface);
178 /* if not monitoring link, all done */
182 #ifdef ACTIVITY_DEBUG
183 (void) error(0, 0, "activity on %s checked as %d",
184 hp->names->id, hp->monitor_io);
186 /* if monitoring, check link for activity if it is up */
187 if (get_ifinfo(hp->monitor, &ifinfo) &&
188 hp->monitor_io == ifinfo.rx_packets +
190 (void) error(0, 0, "skipping poll of %s, %s inactive",
191 hp->names->id, hp->monitor);