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