]> Pileus Git - ~andy/fetchmail/blob - interface.c
Simplify error reporting further.
[~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,1997 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 #if defined(linux) && !defined(INET6)
15
16 #include "config.h"
17 #include <stdio.h>
18 #include <string.h>
19 #if defined(STDC_HEADERS)
20 #include <stdlib.h>
21 #endif
22 #if defined(HAVE_UNISTD_H)
23 #include <unistd.h>
24 #endif
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <net/if.h>
30 #include "config.h"
31 #include "fetchmail.h"
32 #include "i18n.h"
33
34 typedef struct {
35         struct in_addr addr, dstaddr, netmask;
36         int rx_packets, tx_packets;
37 } ifinfo_t;
38
39 struct interface_pair_s {
40         struct in_addr interface_address;
41         struct in_addr interface_mask;
42 } *interface_pair;
43
44 static char *netdevfmt;
45
46 void interface_init(void)
47 /* figure out which /roc/dev/net format to use */
48 {
49     FILE *fp = popen("uname -r", "r");  /* still wins if /proc is out */
50
51     /* pre-linux-2.2 format -- transmit packet count in 8th field */
52     netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
53
54     if (!fp)
55         return;
56     else
57     {
58         int major, minor;
59
60         if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
61             return;
62
63         if (major >= 2 && minor >= 2)
64             /* Linux 2.2 -- transmit packet count in 10th field */
65             netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d";
66     }
67 }
68
69 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
70                 ifinfo_t *ifinfo)
71 /* get active network interface information - return non-zero upon success */
72 {
73         int namelen = strlen(ifname);
74         struct ifreq request;
75         char *cp, buffer[256];
76         int found = 0;
77         int counts[4];
78
79         /* initialize result */
80         memset((char *) ifinfo, 0, sizeof(ifinfo_t));
81
82         /* get the packet I/O counts */
83         while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
84                 for (cp = buffer; *cp && *cp == ' '; ++cp);
85                 if (!strncmp(cp, ifname, namelen) &&
86                                 cp[namelen] == ':') {
87                         cp += namelen + 1;
88                         if (sscanf(cp, netdevfmt,
89                                    counts, counts+1, counts+2, 
90                                    counts+3,&found)>4) { /* found = dummy */
91                                 /* newer kernel with byte counts */
92                                 ifinfo->rx_packets=counts[1];
93                                 ifinfo->tx_packets=counts[3];
94                         } else {
95                                 /* older kernel, no byte counts */
96                                 ifinfo->rx_packets=counts[0];
97                                 ifinfo->tx_packets=counts[2];
98                         }
99                         found = 1;
100                 }
101         }
102         if (!found) return (FALSE);
103
104         /* see if the interface is up */
105         strcpy(request.ifr_name, ifname);
106         if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
107                 return(FALSE);
108         if (!(request.ifr_flags & IFF_RUNNING))
109                 return(FALSE);
110
111         /* get the IP address */
112         strcpy(request.ifr_name, ifname);
113         if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
114                 return(FALSE);
115         ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
116
117         /* get the PPP destination IP address */
118         strcpy(request.ifr_name, ifname);
119         if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
120                 ifinfo->dstaddr = ((struct sockaddr_in *)
121                                         (&request.ifr_dstaddr))->sin_addr;
122
123         /* get the netmask */
124         strcpy(request.ifr_name, ifname);
125         if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
126           ifinfo->netmask = ((struct sockaddr_in *)
127                              (&request.ifr_netmask))->sin_addr;
128           return (TRUE);
129         }
130
131         return(FALSE);
132 }
133
134 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
135 {
136         int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
137         FILE *stats_file = fopen("/proc/net/dev", "r");
138         int result;
139
140         if (socket_fd < 0 || !stats_file)
141                 result = FALSE;
142         else
143         {
144             char        *sp = strchr(ifname, '/');
145
146             if (sp)
147                 *sp = '\0';
148             result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
149             if (sp)
150                 *sp = '/';
151         }
152         if (socket_fd >= 0)
153                 close(socket_fd);
154         if (stats_file)
155                 fclose(stats_file);
156         return(result);
157 }
158
159 #ifndef HAVE_INET_ATON
160 /*
161  * Note: This is not a true replacement for inet_aton(), as it won't
162  * do the right thing on "255.255.255.255" (which translates to -1 on
163  * most machines).  Fortunately this code will be used only if you're
164  * on an older Linux that lacks a real implementation.
165  */
166 #ifdef HAVE_NETINET_IN_SYSTM_H
167 # include <sys/types.h>
168 # include <netinet/in_systm.h>
169 #endif
170
171 #include <netinet/in.h>
172 #include <netinet/ip.h>
173 #include <arpa/inet.h>
174 #include <string.h>
175
176 static int inet_aton(const char *cp, struct in_addr *inp) {
177     long addr;
178
179     addr = inet_addr(cp);
180     if (addr == ((long) -1)) return 0;
181
182     memcpy(inp, &addr, sizeof(addr));
183     return 1;
184 }
185 #endif /* HAVE_INET_ATON */
186
187 void interface_parse(char *buf, struct hostdata *hp)
188 /* parse 'interface' specification */
189 {
190         char *cp1, *cp2;
191
192         hp->interface = xstrdup(buf);
193
194         /* find and isolate just the IP address */
195         if (!(cp1 = strchr(buf, '/')))
196         {
197                 (void) report(stderr,
198                               _("missing IP interface address\n"));
199                 exit(PS_SYNTAX);
200         }
201         *cp1++ = '\000';
202
203         /* find and isolate just the netmask */
204         if (!(cp2 = strchr(cp1, '/')))
205                 cp2 = "255.255.255.255";
206         else
207                 *cp2++ = '\000';
208
209         /* convert IP address and netmask */
210         hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
211         if (!inet_aton(cp1, &hp->interface_pair->interface_address))
212         {
213                 (void) report(stderr,
214                               _("invalid IP interface address\n"));
215                 exit(PS_SYNTAX);
216         }
217         if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
218         {
219                 (void) report(stderr,
220                               _("invalid IP interface mask\n"));
221                 exit(PS_SYNTAX);
222         }
223         /* apply the mask now to the IP address (range) required */
224         hp->interface_pair->interface_address.s_addr &=
225                 hp->interface_pair->interface_mask.s_addr;
226
227         /* restore original interface string (for configuration dumper) */
228         *--cp1 = '/';
229         return;
230 }
231
232 void interface_note_activity(struct hostdata *hp)
233 /* save interface I/O counts */
234 {
235         ifinfo_t ifinfo;
236         struct query *ctl;
237
238         /* if not monitoring link, all done */
239         if (!hp->monitor)
240                 return;
241
242         /* get the current I/O stats for the monitored link */
243         if (get_ifinfo(hp->monitor, &ifinfo))
244                 /* update this and preceeding host entries using the link
245                    (they were already set during this pass but the I/O
246                    count has now changed and they need to be re-updated)
247                 */
248                 for (ctl = querylist; ctl; ctl = ctl->next) {
249                         if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
250                                 ctl->server.monitor_io =
251                                         ifinfo.rx_packets + ifinfo.tx_packets;
252                         /* do NOT update host entries following this one */
253                         if (&ctl->server == hp)
254                                 break;
255                 }
256
257 #ifdef  ACTIVITY_DEBUG
258         (void) report(stdout, 
259                       _("activity on %s -noted- as %d\n"), 
260                       hp->monitor, hp->monitor_io);
261 #endif
262 }
263
264 int interface_approve(struct hostdata *hp)
265 /* return TRUE if OK to poll, FALSE otherwise */
266 {
267         ifinfo_t ifinfo;
268
269         /* check interface IP address (range), if specified */
270         if (hp->interface) {
271                 /* get interface info */
272                 if (!get_ifinfo(hp->interface, &ifinfo)) {
273                         (void) report(stdout, 
274                                       _("skipping poll of %s, %s down\n"),
275                                       hp->pollname, hp->interface);
276                         return(FALSE);
277                 }
278                 /* check the IP address (range) */
279                 if ((ifinfo.addr.s_addr &
280                                 hp->interface_pair->interface_mask.s_addr) !=
281                                 hp->interface_pair->interface_address.s_addr) {
282                         (void) report(stdout,
283                                 _("skipping poll of %s, %s IP address excluded\n"),
284                                 hp->pollname, hp->interface);
285                         return(FALSE);
286                 }
287         }
288
289         /* if not monitoring link, all done */
290         if (!hp->monitor)
291                 return(TRUE);
292
293 #ifdef  ACTIVITY_DEBUG
294         (void) report(stdout, 
295                       _("activity on %s checked as %d\n"), 
296                       hp->monitor, hp->monitor_io);
297 #endif
298         /* if monitoring, check link for activity if it is up */
299         if (get_ifinfo(hp->monitor, &ifinfo) &&
300                         hp->monitor_io == ifinfo.rx_packets +
301                                 ifinfo.tx_packets) {
302                 (void) report(stdout, 
303                               _("skipping poll of %s, %s inactive\n"),
304                               hp->pollname, hp->monitor);
305                 return(FALSE);
306         }
307
308 #ifdef ACTIVITY_DEBUG
309        error(0, 0, _("activity on %s was %d, is %d"),
310              hp->monitor, hp->monitor_io,
311              ifinfo.rx_packets + ifinfo.tx_packets);
312 #endif
313
314         return(TRUE);
315 }
316 #endif /* defined(linux) && !defined(INET6) */