]> Pileus Git - ~andy/fetchmail/blob - interface.c
glibc2 fix.
[~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 <stdio.h>
17 #include <string.h>
18 #if defined(STDC_HEADERS)
19 #include <stdlib.h>
20 #endif
21 #if defined(HAVE_UNISTD_H)
22 #include <unistd.h>
23 #endif
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <net/if.h>
27 #include "fetchmail.h"
28
29 typedef struct {
30         struct in_addr addr, dstaddr, netmask;
31         int rx_packets, tx_packets;
32 } ifinfo_t;
33
34 struct interface_pair_s {
35         struct in_addr interface_address;
36         struct in_addr interface_mask;
37 } *interface_pair;
38
39 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
40                 ifinfo_t *ifinfo)
41 /* get active network interface information - return non-zero upon success */
42 {
43         int namelen = strlen(ifname);
44         struct ifreq request;
45         char *cp, buffer[256];
46
47         /* initialize result */
48         memset((char *) ifinfo, 0, sizeof(ifinfo_t));
49
50         /* see if the interface is up */
51         strcpy(request.ifr_name, ifname);
52         if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
53                 return(FALSE);
54         if (!(request.ifr_flags & IFF_RUNNING))
55                 return(FALSE);
56
57         /* get the IP address */
58         strcpy(request.ifr_name, ifname);
59         if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
60                 return(FALSE);
61         ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
62
63         /* get the PPP destination IP address */
64         strcpy(request.ifr_name, ifname);
65         if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
66                 ifinfo->dstaddr = ((struct sockaddr_in *)
67                                         (&request.ifr_dstaddr))->sin_addr;
68
69         /* get the netmask */
70         strcpy(request.ifr_name, ifname);
71         if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0)
72                 ifinfo->netmask = ((struct sockaddr_in *)
73                                         (&request.ifr_netmask))->sin_addr;
74
75         /* get the packet I/O counts */
76         while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
77                 for (cp = buffer; *cp && *cp == ' '; ++cp);
78                 if (!strncmp(cp, ifname, namelen) &&
79                                 cp[namelen] == ':') {
80                         cp += namelen + 1;
81                         sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d",
82                                 &ifinfo->rx_packets, &ifinfo->tx_packets);
83                         return(TRUE);
84                 }
85         }
86         return(FALSE);
87 }
88
89 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
90 {
91         int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
92         FILE *stats_file = fopen("/proc/net/dev", "r");
93         int result;
94
95         if (socket_fd < 0 || !stats_file)
96                 result = -1;
97         else
98                 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
99         if (socket_fd >= 0)
100                 close(socket_fd);
101         if (stats_file)
102                 fclose(stats_file);
103         return(result);
104 }
105
106 void interface_parse(char *buf, struct hostdata *hp)
107 /* parse 'interface' specification */
108 {
109         char *cp1, *cp2;
110
111         /* find and isolate just the IP address */
112         if (!(cp1 = strchr(buf, '/')))
113                 (void) error(PS_SYNTAX, 0, "missing IP interface address");
114         *cp1++ = '\000';
115         hp->interface = xstrdup(buf);
116
117         /* find and isolate just the netmask */
118         if (!(cp2 = strchr(cp1, '/')))
119                 cp2 = "255.255.255.255";
120         else
121                 *cp2++ = '\000';
122
123         /* convert IP address and netmask */
124         hp->interface_pair = xmalloc(sizeof(struct interface_pair_s));
125         if (!inet_aton(cp1, &hp->interface_pair->interface_address))
126                 (void) error(PS_SYNTAX, 0, "invalid IP interface address");
127         if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
128                 (void) error(PS_SYNTAX, 0, "invalid IP interface mask");
129         /* apply the mask now to the IP address (range) required */
130         hp->interface_pair->interface_address.s_addr &=
131                 hp->interface_pair->interface_mask.s_addr;
132         return;
133 }
134
135 void interface_note_activity(struct hostdata *hp)
136 /* save interface I/O counts */
137 {
138         ifinfo_t ifinfo;
139
140         /* if not monitoring link, all done */
141         if (!hp->monitor)
142                 return;
143
144         /* get the current I/O stats for the monitored link */
145         if (get_ifinfo(hp->monitor, &ifinfo))
146                 hp->monitor_io = ifinfo.rx_packets + ifinfo.tx_packets;
147
148 #ifdef  ACTIVITY_DEBUG
149         (void) error(0, 0, "activity on %s -noted- as %d", 
150                 hp->names->id, hp->monitor_io);
151 #endif
152 }
153
154 int interface_approve(struct hostdata *hp)
155 /* return TRUE if OK to poll, FALSE otherwise */
156 {
157         ifinfo_t ifinfo;
158
159         /* check interface IP address (range), if specified */
160         if (hp->interface) {
161                 /* get interface info */
162                 if (!get_ifinfo(hp->interface, &ifinfo)) {
163                         (void) error(0, 0, "skipping poll of %s, %s down",
164                                 hp->names->id, hp->interface);
165                         return(FALSE);
166                 }
167                 /* check the IP address (range) */
168                 if ((ifinfo.addr.s_addr &
169                                 hp->interface_pair->interface_mask.s_addr) !=
170                                 hp->interface_pair->interface_address.s_addr) {
171                         (void) error(0, 0,
172                                 "skipping poll of %s, %s IP address excluded",
173                                 hp->names->id, hp->interface);
174                         return(FALSE);
175                 }
176         }
177
178         /* if not monitoring link, all done */
179         if (!hp->monitor)
180                 return(TRUE);
181
182 #ifdef  ACTIVITY_DEBUG
183         (void) error(0, 0, "activity on %s checked as %d", 
184                 hp->names->id, hp->monitor_io);
185 #endif
186         /* if monitoring, check link for activity if it is up */
187         if (get_ifinfo(hp->monitor, &ifinfo) &&
188                         hp->monitor_io == ifinfo.rx_packets +
189                                 ifinfo.tx_packets) {
190                 (void) error(0, 0, "skipping poll of %s, %s inactive",
191                         hp->names->id, hp->monitor);
192                 return(FALSE);
193         }
194
195         return(TRUE);
196 }
197 #endif /* linux */