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