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