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