]> Pileus Git - ~andy/fetchmail/blob - interface.c
Better error reporting.
[~andy/fetchmail] / interface.c
1 /*
2  * interface.c -- implements fetchmail 'interface' and 'monitor' commands
3  *
4  * This module was implemented by George M. Sipe <gsipe@mindspring.com>
5  * or <gsipe@acm.org> and is:
6  *
7  *      Copyright (c) 1996 by George M. Sipe - ALL RIGHTS RESERVED
8  *
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.
12  */
13
14 #ifdef  linux
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20 #include <netinet/in.h>
21 #include <linux/netdevice.h>
22 #include <errno.h>
23 #include "fetchmail.h"
24
25 static struct in_addr interface_address;
26 static struct in_addr interface_mask;
27
28 static int monitor_io = 0;
29
30 typedef struct {
31         struct in_addr addr, dstaddr, netmask;
32         int rx_packets, tx_packets;
33 } ifinfo_t;
34
35 /* Get active network interface information.  Return non-zero upon success. */
36
37 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
38                 ifinfo_t *ifinfo)
39 {
40         int namelen = strlen(ifname);
41         struct ifreq request;
42         char *cp, buffer[256];
43
44         /* initialize result */
45         memset((char *) ifinfo, 0, sizeof(ifinfo_t));
46
47         /* see if the interface is up */
48         strcpy(request.ifr_name, ifname);
49         errno = 0;
50         if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
51                 error(PS_IOERR, errno, "interface status check failed");
52         if (!(request.ifr_flags & IFF_RUNNING))
53                 return(FALSE);
54
55         /* get the IP address */
56         strcpy(request.ifr_name, ifname);
57         if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
58                 return(FALSE);
59         ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
60
61         /* get the PPP destination IP address */
62         strcpy(request.ifr_name, ifname);
63         if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
64                 ifinfo->dstaddr = ((struct sockaddr_in *)
65                                         (&request.ifr_dstaddr))->sin_addr;
66
67         /* get the netmask */
68         strcpy(request.ifr_name, ifname);
69         if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
70                 ifinfo->netmask = ((struct sockaddr_in *)
71                                         (&request.ifr_netmask))->sin_addr;
72
73         /* get the packet I/O counts */
74         while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
75                 for (cp = buffer; *cp && *cp == ' '; ++cp);
76                 if (!strncmp(cp, ifname, namelen) &&
77                                 cp[namelen] == ':') {
78                         cp += namelen + 1;
79                         sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
80                                 &ifinfo->rx_packets, &ifinfo->tx_packets);
81                         return(TRUE);
82                 }
83         }
84         return(FALSE);
85 }
86
87 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
88 {
89         int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
90         FILE *stats_file = fopen("/proc/net/dev", "r");
91         int result;
92
93         if (socket_fd < 0 || !stats_file)
94                 result = -1;
95         else
96                 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
97         if (socket_fd >= 0)
98                 close(socket_fd);
99         if (stats_file)
100                 fclose(stats_file);
101         return(result);
102 }
103
104 /* Parse 'interface' specification. */
105
106 void interface_parse(void)
107 {
108         char *cp1, *cp2;
109
110         /* in the event we point to a null string, make pointer null */
111         if (interface && !*interface)
112                 interface = NULL;
113         if (monitor && !*monitor)
114                 monitor = NULL;
115
116         /* if no interface specification present, all done */
117         if (!interface)
118                 return;
119
120         /* find and isolate just the IP address */
121         if (!(cp1 = strchr(interface, '/')))
122                 (void) error(PS_SYNTAX, 0, "missing IP interface address");
123         *cp1++ = '\000';
124
125         /* find and isolate just the netmask */
126         if (!(cp2 = strchr(cp1, '/')))
127                 cp2 = "255.255.255.255";
128         else
129                 *cp2++ = '\000';
130
131         /* convert IP address and netmask */
132         if (!inet_aton(cp1, &interface_address))
133                 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
134         if (!inet_aton(cp2, &interface_mask))
135                 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
136         /* apply the mask now to the IP address (range) required */
137         interface_address.s_addr &= interface_mask.s_addr;
138         return;
139 }
140
141 /* Save interface I/O counts. */
142
143 void interface_note_activity(void)
144 {
145         ifinfo_t ifinfo;
146
147         sleep(3);       /* allow some time for the link to quiesce */
148
149         /* get the current I/O stats for the monitored link */
150         if (monitor && get_ifinfo(monitor, &ifinfo))
151                 monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
152 }
153
154 /* Return TRUE if OK to poll, FALSE otherwise. */
155
156 int interface_approve(void)
157 {
158         ifinfo_t ifinfo;
159
160         /* check interface IP address (range), if specified */
161         if (interface) {
162                 /* get interface info */
163                 if (!get_ifinfo(interface, &ifinfo)) {
164                         (void) error(0, 0, "skipping poll, %s down",
165                                 interface);
166                         return(FALSE);
167                 }
168                 /* check the IP address (range) */
169                 if ((ifinfo.addr.s_addr & interface_mask.s_addr) !=
170                                 interface_address.s_addr) {
171                         (void) error(0, 0,
172                            "skipping poll, %s IP address excluded",
173                            interface);
174                         return(FALSE);
175                 }
176         }
177
178         /* if monitoring, check link for activity if it is up */
179         if (monitor && get_ifinfo(monitor, &ifinfo) &&
180                         monitor_io == ifinfo.rx_packets + ifinfo.tx_packets) {
181                 (void) error(0, 0, "skipping poll, %s inactive", monitor);
182                 return(FALSE);
183         }
184
185         return(TRUE);
186 }
187 #endif  /* linux */