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