]> Pileus Git - ~andy/fetchmail/blob - interface.c
Some configuration cleanup.
[~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
49         /* initialize result */
50         memset((char *) ifinfo, 0, sizeof(ifinfo_t));
51
52         /* see if the interface is up */
53         strcpy(request.ifr_name, ifname);
54         if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
55                 return(FALSE);
56         if (!(request.ifr_flags & IFF_RUNNING))
57                 return(FALSE);
58
59         /* get the IP address */
60         strcpy(request.ifr_name, ifname);
61         if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
62                 return(FALSE);
63         ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
64
65         /* get the PPP destination IP address */
66         strcpy(request.ifr_name, ifname);
67         if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
68                 ifinfo->dstaddr = ((struct sockaddr_in *)
69                                         (&request.ifr_dstaddr))->sin_addr;
70
71         /* get the netmask */
72         strcpy(request.ifr_name, ifname);
73         if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
74                 ifinfo->netmask = ((struct sockaddr_in *)
75                                         (&request.ifr_netmask))->sin_addr;
76
77         /* get the packet I/O counts */
78         while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
79                 for (cp = buffer; *cp && *cp == ' '; ++cp);
80                 if (!strncmp(cp, ifname, namelen) &&
81                                 cp[namelen] == ':') {
82                         cp += namelen + 1;
83                         sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
84                                 &ifinfo->rx_packets, &ifinfo->tx_packets);
85                         return(TRUE);
86                 }
87         }
88         return(FALSE);
89 }
90
91 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
92 {
93         int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
94         FILE *stats_file = fopen("/proc/net/dev", "r");
95         int result;
96
97         if (socket_fd < 0 || !stats_file)
98                 result = FALSE;
99         else
100                 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
101         if (socket_fd >= 0)
102                 close(socket_fd);
103         if (stats_file)
104                 fclose(stats_file);
105         return(result);
106 }
107
108 void interface_parse(char *buf, struct hostdata *hp)
109 /* parse 'interface' specification */
110 {
111         char *cp1, *cp2;
112
113         /* find and isolate just the IP address */
114         if (!(cp1 = strchr(buf, '/')))
115                 (void) error(PS_SYNTAX, 0, "missing IP interface address");
116         *cp1++ = '\000';
117         hp->interface = xstrdup(buf);
118
119         /* find and isolate just the netmask */
120         if (!(cp2 = strchr(cp1, '/')))
121                 cp2 = "255.255.255.255";
122         else
123                 *cp2++ = '\000';
124
125         /* convert IP address and netmask */
126         hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
127         if (!inet_aton(cp1, &hp->interface_pair->interface_address))
128                 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
129         if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
130                 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
131         /* apply the mask now to the IP address (range) required */
132         hp->interface_pair->interface_address.s_addr &=
133                 hp->interface_pair->interface_mask.s_addr;
134         return;
135 }
136
137 void interface_note_activity(struct hostdata *hp)
138 /* save interface I/O counts */
139 {
140         ifinfo_t ifinfo;
141         struct query *ctl;
142
143         /* if not monitoring link, all done */
144         if (!hp->monitor)
145                 return;
146
147         /* get the current I/O stats for the monitored link */
148         if (get_ifinfo(hp->monitor, &ifinfo))
149                 /* update this and preceeding host entries using the link
150                    (they were already set during this pass but the I/O
151                    count has now changed and they need to be re-updated)
152                 */
153                 for (ctl = querylist; ctl; ctl = ctl->next) {
154                         if (!strcmp(hp->monitor, ctl->server.monitor))
155                                 ctl->server.monitor_io =
156                                         ifinfo.rx_packets + ifinfo.tx_packets;
157                         /* do NOT update host entries following this one */
158                         if (&ctl->server == hp)
159                                 break;
160                 }
161
162 #ifdef  ACTIVITY_DEBUG
163         (void) error(0, 0, "activity on %s -noted- as %d", 
164                 hp->monitor, hp->monitor_io);
165 #endif
166 }
167
168 int interface_approve(struct hostdata *hp)
169 /* return TRUE if OK to poll, FALSE otherwise */
170 {
171         ifinfo_t ifinfo;
172
173         /* check interface IP address (range), if specified */
174         if (hp->interface) {
175                 /* get interface info */
176                 if (!get_ifinfo(hp->interface, &ifinfo)) {
177                         (void) error(0, 0, "skipping poll of %s, %s down",
178                                 hp->names->id, hp->interface);
179                         return(FALSE);
180                 }
181                 /* check the IP address (range) */
182                 if ((ifinfo.addr.s_addr &
183                                 hp->interface_pair->interface_mask.s_addr) !=
184                                 hp->interface_pair->interface_address.s_addr) {
185                         (void) error(0, 0,
186                                 "skipping poll of %s, %s IP address excluded",
187                                 hp->names->id, hp->interface);
188                         return(FALSE);
189                 }
190         }
191
192         /* if not monitoring link, all done */
193         if (!hp->monitor)
194                 return(TRUE);
195
196 #ifdef  ACTIVITY_DEBUG
197         (void) error(0, 0, "activity on %s checked as %d", 
198                 hp->monitor, hp->monitor_io);
199 #endif
200         /* if monitoring, check link for activity if it is up */
201         if (get_ifinfo(hp->monitor, &ifinfo) &&
202                         hp->monitor_io == ifinfo.rx_packets +
203                                 ifinfo.tx_packets) {
204                 (void) error(0, 0, "skipping poll of %s, %s inactive",
205                         hp->names->id, hp->monitor);
206                 return(FALSE);
207         }
208
209         return(TRUE);
210 }
211 #endif /* linux */