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 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 #include <sys/ioctl.h>
20 #include <netinet/in.h>
21 #include <linux/netdevice.h>
22 #include "fetchmail.h"
24 static struct in_addr interface_address;
25 static struct in_addr interface_mask;
27 static int monitor_io = 0;
30 struct in_addr addr, dstaddr, netmask;
31 int rx_packets, tx_packets;
34 /* Get active network interface information. Return non-zero upon success. */
36 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
39 int namelen = strlen(ifname);
41 char *cp, buffer[256];
43 /* initialize result */
44 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
46 /* see if the interface is up */
47 strcpy(request.ifr_name, ifname);
48 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
50 if (!(request.ifr_flags & IFF_RUNNING))
53 /* get the IP address */
54 strcpy(request.ifr_name, ifname);
55 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
57 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
59 /* get the PPP destination IP address */
60 strcpy(request.ifr_name, ifname);
61 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
62 ifinfo->dstaddr = ((struct sockaddr_in *)
63 (&request.ifr_dstaddr))->sin_addr;
66 strcpy(request.ifr_name, ifname);
67 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
68 ifinfo->netmask = ((struct sockaddr_in *)
69 (&request.ifr_netmask))->sin_addr;
71 /* get the packet I/O counts */
72 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
73 for (cp = buffer; *cp && *cp == ' '; ++cp);
74 if (!strncmp(cp, ifname, namelen) &&
77 sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
78 &ifinfo->rx_packets, &ifinfo->tx_packets);
85 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
87 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
88 FILE *stats_file = fopen("/proc/net/dev", "r");
91 if (socket_fd < 0 || !stats_file)
94 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
102 /* Parse 'interface' specification. */
104 void interface_parse(void)
108 /* in the event we point to a null string, make pointer null */
109 if (interface && !*interface)
111 if (monitor && !*monitor)
114 /* if no interface specification present, all done */
118 /* find and isolate just the IP address */
119 if (!(cp1 = strchr(interface, '/')))
120 (void) error(PS_SYNTAX, 0, "missing IP interface address");
123 /* find and isolate just the netmask */
124 if (!(cp2 = strchr(cp1, '/')))
125 cp2 = "255.255.255.255";
129 /* convert IP address and netmask */
130 if (!inet_aton(cp1, &interface_address))
131 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
132 if (!inet_aton(cp2, &interface_mask))
133 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
134 /* apply the mask now to the IP address (range) required */
135 interface_address.s_addr &= interface_mask.s_addr;
139 /* Save interface I/O counts. */
141 void interface_note_activity(void)
145 sleep(3); /* allow some time for the link to quiesce */
147 /* get the current I/O stats for the monitored link */
148 if (monitor && get_ifinfo(monitor, &ifinfo))
149 monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
152 /* Return TRUE if OK to poll, FALSE otherwise. */
154 int interface_approve(void)
158 /* check interface IP address (range), if specified */
160 /* get interface info */
161 if (!get_ifinfo(interface, &ifinfo)) {
162 (void) error(0, 0, "skipping poll, %s down",
166 /* check the IP address (range) */
167 if ((ifinfo.addr.s_addr & interface_mask.s_addr) !=
168 interface_address.s_addr) {
170 "skipping poll, %s IP address excluded",
176 /* if monitoring, check link for activity if it is up */
177 if (monitor && get_ifinfo(monitor, &ifinfo) &&
178 monitor_io == ifinfo.rx_packets + ifinfo.tx_packets) {
179 (void) error(0, 0, "skipping poll, %s inactive", monitor);