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 * i18n by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 7-Nov-1998
11 * This is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License as published by the Free
13 * Software Foundation; version 2, or (at your option) any later version.
16 #if defined(linux) && !defined(INET6)
21 #if defined(STDC_HEADERS)
24 #if defined(HAVE_UNISTD_H)
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
33 #include "fetchmail.h"
37 struct in_addr addr, dstaddr, netmask;
38 int rx_packets, tx_packets;
41 struct interface_pair_s {
42 struct in_addr interface_address;
43 struct in_addr interface_mask;
46 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
48 /* get active network interface information - return non-zero upon success */
50 int namelen = strlen(ifname);
52 char *cp, buffer[256];
56 /* initialize result */
57 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
59 /* get the packet I/O counts */
60 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
61 for (cp = buffer; *cp && *cp == ' '; ++cp);
62 if (!strncmp(cp, ifname, namelen) &&
65 if (sscanf(cp, "%d %d %*d %*d %*d %d %*d %d %*d %*d"
66 " %*d %*d %d",counts, counts+1, counts+2,
67 counts+3,&found)>4) { /* found = dummy */
68 /* newer kernel with byte counts */
69 ifinfo->rx_packets=counts[1];
70 ifinfo->tx_packets=counts[3];
72 /* older kernel, no byte counts */
73 ifinfo->rx_packets=counts[0];
74 ifinfo->tx_packets=counts[2];
79 if (!found) return (FALSE);
81 /* see if the interface is up */
82 strcpy(request.ifr_name, ifname);
83 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
85 if (!(request.ifr_flags & IFF_RUNNING))
88 /* get the IP address */
89 strcpy(request.ifr_name, ifname);
90 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
92 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
94 /* get the PPP destination IP address */
95 strcpy(request.ifr_name, ifname);
96 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
97 ifinfo->dstaddr = ((struct sockaddr_in *)
98 (&request.ifr_dstaddr))->sin_addr;
100 /* get the netmask */
101 strcpy(request.ifr_name, ifname);
102 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
103 ifinfo->netmask = ((struct sockaddr_in *)
104 (&request.ifr_netmask))->sin_addr;
111 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
113 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
114 FILE *stats_file = fopen("/proc/net/dev", "r");
117 if (socket_fd < 0 || !stats_file)
121 char *sp = strchr(ifname, '/');
125 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
136 #ifndef HAVE_INET_ATON
138 * Note: This is not a true replacement for inet_aton(), as it won't
139 * do the right thing on "255.255.255.255" (which translates to -1 on
140 * most machines). Fortunately this code will be used only if you're
141 * on an older Linux that lacks a real implementation.
143 #ifdef HAVE_NETINET_IN_SYSTM_H
144 # include <sys/types.h>
145 # include <netinet/in_systm.h>
148 #include <netinet/in.h>
149 #include <netinet/ip.h>
150 #include <arpa/inet.h>
153 static int inet_aton(const char *cp, struct in_addr *inp) {
156 addr = inet_addr(cp);
157 if (addr == ((long) -1)) return 0;
159 memcpy(inp, &addr, sizeof(addr));
162 #endif /* HAVE_INET_ATON */
164 void interface_parse(char *buf, struct hostdata *hp)
165 /* parse 'interface' specification */
169 hp->interface = xstrdup(buf);
171 /* find and isolate just the IP address */
172 if (!(cp1 = strchr(buf, '/')))
173 (void) error(PS_SYNTAX, 0, _("missing IP interface address"));
176 /* find and isolate just the netmask */
177 if (!(cp2 = strchr(cp1, '/')))
178 cp2 = "255.255.255.255";
182 /* convert IP address and netmask */
183 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
184 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
185 (void) error(PS_SYNTAX, 0, _("invalid IP interface address"));
186 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
187 (void) error(PS_SYNTAX, 0, _("invalid IP interface mask"));
188 /* apply the mask now to the IP address (range) required */
189 hp->interface_pair->interface_address.s_addr &=
190 hp->interface_pair->interface_mask.s_addr;
192 /* restore original interface string (for configuration dumper) */
197 void interface_note_activity(struct hostdata *hp)
198 /* save interface I/O counts */
203 /* if not monitoring link, all done */
207 /* get the current I/O stats for the monitored link */
208 if (get_ifinfo(hp->monitor, &ifinfo))
209 /* update this and preceeding host entries using the link
210 (they were already set during this pass but the I/O
211 count has now changed and they need to be re-updated)
213 for (ctl = querylist; ctl; ctl = ctl->next) {
214 if (!strcmp(hp->monitor, ctl->server.monitor))
215 ctl->server.monitor_io =
216 ifinfo.rx_packets + ifinfo.tx_packets;
217 /* do NOT update host entries following this one */
218 if (&ctl->server == hp)
222 #ifdef ACTIVITY_DEBUG
223 (void) error(0, 0, _("activity on %s -noted- as %d"),
224 hp->monitor, hp->monitor_io);
228 int interface_approve(struct hostdata *hp)
229 /* return TRUE if OK to poll, FALSE otherwise */
233 /* check interface IP address (range), if specified */
235 /* get interface info */
236 if (!get_ifinfo(hp->interface, &ifinfo)) {
237 (void) error(0, 0, _("skipping poll of %s, %s down"),
238 hp->pollname, hp->interface);
241 /* check the IP address (range) */
242 if ((ifinfo.addr.s_addr &
243 hp->interface_pair->interface_mask.s_addr) !=
244 hp->interface_pair->interface_address.s_addr) {
246 _("skipping poll of %s, %s IP address excluded"),
247 hp->pollname, hp->interface);
252 /* if not monitoring link, all done */
256 #ifdef ACTIVITY_DEBUG
257 (void) error(0, 0, _("activity on %s checked as %d"),
258 hp->monitor, hp->monitor_io);
260 /* if monitoring, check link for activity if it is up */
261 if (get_ifinfo(hp->monitor, &ifinfo) &&
262 hp->monitor_io == ifinfo.rx_packets +
264 (void) error(0, 0, _("skipping poll of %s, %s inactive"),
265 hp->pollname, hp->monitor);
271 #endif /* defined(linux) && !defined(INET6) */