]> Pileus Git - ~andy/fetchmail/blob - interface.c
Ian T. Zimmerman's `interface' option.
[~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 void interface_parse(char *buf, struct hostdata *hp)
114 /* parse 'interface' specification */
115 {
116         char *cp1, *cp2;
117
118         /* find and isolate just the IP address */
119         if (!(cp1 = strchr(buf, '/')))
120                 (void) error(PS_SYNTAX, 0, "missing IP interface address");
121         *cp1++ = '\000';
122         hp->interface = xstrdup(buf);
123
124         /* find and isolate just the netmask */
125         if (!(cp2 = strchr(cp1, '/')))
126                 cp2 = "255.255.255.255";
127         else
128                 *cp2++ = '\000';
129
130         /* convert IP address and netmask */
131         hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
132         if (!inet_aton(cp1, &hp->interface_pair->interface_address))
133                 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
134         if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
135                 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
136         /* apply the mask now to the IP address (range) required */
137         hp->interface_pair->interface_address.s_addr &=
138                 hp->interface_pair->interface_mask.s_addr;
139         return;
140 }
141
142 void interface_note_activity(struct hostdata *hp)
143 /* save interface I/O counts */
144 {
145         ifinfo_t ifinfo;
146         struct query *ctl;
147
148         /* if not monitoring link, all done */
149         if (!hp->monitor)
150                 return;
151
152         /* get the current I/O stats for the monitored link */
153         if (get_ifinfo(hp->monitor, &ifinfo))
154                 /* update this and preceeding host entries using the link
155                    (they were already set during this pass but the I/O
156                    count has now changed and they need to be re-updated)
157                 */
158                 for (ctl = querylist; ctl; ctl = ctl->next) {
159                         if (!strcmp(hp->monitor, ctl->server.monitor))
160                                 ctl->server.monitor_io =
161                                         ifinfo.rx_packets + ifinfo.tx_packets;
162                         /* do NOT update host entries following this one */
163                         if (&ctl->server == hp)
164                                 break;
165                 }
166
167 #ifdef  ACTIVITY_DEBUG
168         (void) error(0, 0, "activity on %s -noted- as %d", 
169                 hp->monitor, hp->monitor_io);
170 #endif
171 }
172
173 int interface_approve(struct hostdata *hp)
174 /* return TRUE if OK to poll, FALSE otherwise */
175 {
176         ifinfo_t ifinfo;
177
178         /* check interface IP address (range), if specified */
179         if (hp->interface) {
180                 /* get interface info */
181                 if (!get_ifinfo(hp->interface, &ifinfo)) {
182                         (void) error(0, 0, "skipping poll of %s, %s down",
183                                 hp->pollname, hp->interface);
184                         return(FALSE);
185                 }
186                 /* check the IP address (range) */
187                 if ((ifinfo.addr.s_addr &
188                                 hp->interface_pair->interface_mask.s_addr) !=
189                                 hp->interface_pair->interface_address.s_addr) {
190                         (void) error(0, 0,
191                                 "skipping poll of %s, %s IP address excluded",
192                                 hp->pollname, hp->interface);
193                         return(FALSE);
194                 }
195         }
196
197         /* if not monitoring link, all done */
198         if (!hp->monitor)
199                 return(TRUE);
200
201 #ifdef  ACTIVITY_DEBUG
202         (void) error(0, 0, "activity on %s checked as %d", 
203                 hp->monitor, hp->monitor_io);
204 #endif
205         /* if monitoring, check link for activity if it is up */
206         if (get_ifinfo(hp->monitor, &ifinfo) &&
207                         hp->monitor_io == ifinfo.rx_packets +
208                                 ifinfo.tx_packets) {
209                 (void) error(0, 0, "skipping poll of %s, %s inactive",
210                         hp->pollname, hp->monitor);
211                 return(FALSE);
212         }
213
214         return(TRUE);
215 }
216 #endif /* linux */