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