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