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