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