2 * interface.c -- implements fetchmail 'interface' and 'monitor' commands
4 * This module was implemented by George M. Sipe <gsipe@mindspring.com>
5 * or <gsipe@acm.org> and is:
7 * Copyright (c) 1996,1997 by George M. Sipe
9 * FreeBSD specific portions written by and Copyright (c) 1999
10 * Andy Doran <ad@psn.ie>.
12 * This is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; version 2, or (at your option) any later version.
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #if (defined(linux) && !defined(INET6)) || defined(__FreeBSD__)
24 #if defined(STDC_HEADERS)
27 #if defined(HAVE_UNISTD_H)
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #if defined(__FreeBSD__)
36 #if __FreeBSD_version >= 300001
37 #include <net/if_var.h>
41 #include <sys/fcntl.h>
44 #include "fetchmail.h"
48 struct in_addr addr, dstaddr, netmask;
49 int rx_packets, tx_packets;
52 struct interface_pair_s {
53 struct in_addr interface_address;
54 struct in_addr interface_mask;
57 static char *netdevfmt;
59 void interface_init(void)
60 /* figure out which /roc/dev/net format to use */
62 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
64 /* pre-linux-2.2 format -- transmit packet count in 8th field */
65 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
73 if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
76 if (major >= 2 && minor >= 2)
77 /* Linux 2.2 -- transmit packet count in 10th field */
78 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d";
85 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
87 /* get active network interface information - return non-zero upon success */
89 int namelen = strlen(ifname);
91 char *cp, buffer[256];
95 /* initialize result */
96 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
98 /* get the packet I/O counts */
99 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
100 for (cp = buffer; *cp && *cp == ' '; ++cp);
101 if (!strncmp(cp, ifname, namelen) &&
102 cp[namelen] == ':') {
104 if (sscanf(cp, netdevfmt,
105 counts, counts+1, counts+2,
106 counts+3,&found)>4) { /* found = dummy */
107 /* newer kernel with byte counts */
108 ifinfo->rx_packets=counts[1];
109 ifinfo->tx_packets=counts[3];
111 /* older kernel, no byte counts */
112 ifinfo->rx_packets=counts[0];
113 ifinfo->tx_packets=counts[2];
118 if (!found) return (FALSE);
120 /* see if the interface is up */
121 strcpy(request.ifr_name, ifname);
122 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
124 if (!(request.ifr_flags & IFF_RUNNING))
127 /* get the IP address */
128 strcpy(request.ifr_name, ifname);
129 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
131 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
133 /* get the PPP destination IP address */
134 strcpy(request.ifr_name, ifname);
135 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
136 ifinfo->dstaddr = ((struct sockaddr_in *)
137 (&request.ifr_dstaddr))->sin_addr;
139 /* get the netmask */
140 strcpy(request.ifr_name, ifname);
141 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
142 ifinfo->netmask = ((struct sockaddr_in *)
143 (&request.ifr_netmask))->sin_addr;
150 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
152 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
153 FILE *stats_file = fopen("/proc/net/dev", "r");
156 if (socket_fd < 0 || !stats_file)
160 char *sp = strchr(ifname, '/');
164 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
175 #elif defined __FreeBSD__
178 static struct nlist symbols[] =
183 static u_long ifnet_savedaddr;
184 static gid_t if_rgid;
185 static gid_t if_egid;
188 interface_set_gids(gid_t egid, gid_t rgid)
197 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
200 if (kvm_nlist(kvmfd, symbols) < 0)
203 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
210 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
215 unsigned long ifnet_addr = ifnet_savedaddr;
216 #if __FreeBSD_version >= 300001
217 struct ifnethead ifnethead;
218 struct ifaddrhead ifaddrhead;
220 struct ifaddr ifaddr;
221 unsigned long ifaddr_addr;
223 unsigned long sa_addr;
229 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
230 iname[i] = ifname[i];
238 report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
245 #if __FreeBSD_version >= 300001
246 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
247 ifnet_addr = (u_long) ifnethead.tqh_first;
249 ifnet_addr = ifnet_savedaddr;
254 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
255 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
256 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
258 if (!strcmp(tname, iname))
260 if (!(ifnet.if_flags & IFF_UP))
267 ifinfo->rx_packets = ifnet.if_ipackets;
268 ifinfo->tx_packets = ifnet.if_opackets;
270 #if __FreeBSD_version >= 300001
271 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
273 ifaddr_addr = (u_long) ifnet.if_addrlist;
278 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
279 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
281 if (sa.sa_family != AF_INET)
283 #if __FreeBSD_version >= 300001
284 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
286 ifaddr_addr = (u_long) ifaddr.ifa_next;
291 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
292 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
293 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
294 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
295 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
309 #if __FreeBSD_version >= 300001
310 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
312 ifnet_addr = (unsigned long) ifnet.if_next;
321 #endif /* defined __FreeBSD__ */
324 #ifndef HAVE_INET_ATON
326 * Note: This is not a true replacement for inet_aton(), as it won't
327 * do the right thing on "255.255.255.255" (which translates to -1 on
328 * most machines). Fortunately this code will be used only if you're
329 * on an older Linux that lacks a real implementation.
331 #ifdef HAVE_NETINET_IN_SYSTM_H
332 # include <sys/types.h>
333 # include <netinet/in_systm.h>
336 #include <netinet/in.h>
337 #include <netinet/ip.h>
338 #include <arpa/inet.h>
341 static int inet_aton(const char *cp, struct in_addr *inp) {
344 addr = inet_addr(cp);
345 if (addr == ((long) -1)) return 0;
347 memcpy(inp, &addr, sizeof(addr));
350 #endif /* HAVE_INET_ATON */
352 void interface_parse(char *buf, struct hostdata *hp)
353 /* parse 'interface' specification */
357 hp->interface = xstrdup(buf);
359 /* find and isolate just the IP address */
360 if (!(cp1 = strchr(buf, '/')))
362 (void) report(stderr,
363 _("missing IP interface address\n"));
368 /* find and isolate just the netmask */
369 if (!(cp2 = strchr(cp1, '/')))
370 cp2 = "255.255.255.255";
374 /* convert IP address and netmask */
375 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
376 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
378 (void) report(stderr,
379 _("invalid IP interface address\n"));
382 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
384 (void) report(stderr,
385 _("invalid IP interface mask\n"));
388 /* apply the mask now to the IP address (range) required */
389 hp->interface_pair->interface_address.s_addr &=
390 hp->interface_pair->interface_mask.s_addr;
392 /* restore original interface string (for configuration dumper) */
397 void interface_note_activity(struct hostdata *hp)
398 /* save interface I/O counts */
403 /* if not monitoring link, all done */
407 /* get the current I/O stats for the monitored link */
408 if (get_ifinfo(hp->monitor, &ifinfo))
409 /* update this and preceeding host entries using the link
410 (they were already set during this pass but the I/O
411 count has now changed and they need to be re-updated)
413 for (ctl = querylist; ctl; ctl = ctl->next) {
414 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
415 ctl->server.monitor_io =
416 ifinfo.rx_packets + ifinfo.tx_packets;
417 /* do NOT update host entries following this one */
418 if (&ctl->server == hp)
422 #ifdef ACTIVITY_DEBUG
423 (void) report(stdout,
424 _("activity on %s -noted- as %d\n"),
425 hp->monitor, hp->monitor_io);
429 int interface_approve(struct hostdata *hp)
430 /* return TRUE if OK to poll, FALSE otherwise */
434 /* check interface IP address (range), if specified */
436 /* get interface info */
437 if (!get_ifinfo(hp->interface, &ifinfo)) {
438 (void) report(stdout,
439 _("skipping poll of %s, %s down\n"),
440 hp->pollname, hp->interface);
443 /* check the IP address (range) */
444 if ((ifinfo.addr.s_addr &
445 hp->interface_pair->interface_mask.s_addr) !=
446 hp->interface_pair->interface_address.s_addr) {
447 (void) report(stdout,
448 _("skipping poll of %s, %s IP address excluded\n"),
449 hp->pollname, hp->interface);
454 /* if not monitoring link, all done */
458 #ifdef ACTIVITY_DEBUG
459 (void) report(stdout,
460 _("activity on %s checked as %d\n"),
461 hp->monitor, hp->monitor_io);
463 /* if monitoring, check link for activity if it is up */
464 if (get_ifinfo(hp->monitor, &ifinfo) &&
465 hp->monitor_io == ifinfo.rx_packets +
467 (void) report(stdout,
468 _("skipping poll of %s, %s inactive\n"),
469 hp->pollname, hp->monitor);
473 #ifdef ACTIVITY_DEBUG
474 error(0, 0, _("activity on %s was %d, is %d"),
475 hp->monitor, hp->monitor_io,
476 ifinfo.rx_packets + ifinfo.tx_packets);
481 #endif /* (defined(linux) && !defined(INET6)) || defined(__FreeBSD__) */