2 * interface.c -- implements fetchmail 'interface' and 'monitor' commands
4 * This module was implemented by George M. Sipe <gsipe@pobox.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;
61 void interface_init(void)
62 /* figure out which /roc/dev/net format to use */
64 FILE *fp = popen("uname -r", "r"); /* still wins if /proc is out */
66 /* pre-linux-2.2 format -- transmit packet count in 8th field */
67 netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
75 if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
78 if (major >= 2 && minor >= 2)
79 /* Linux 2.2 -- transmit packet count in 10th field */
80 netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d";
84 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
86 /* get active network interface information - return non-zero upon success */
88 int namelen = strlen(ifname);
90 char *cp, buffer[256];
94 /* initialize result */
95 memset((char *) ifinfo, 0, sizeof(ifinfo_t));
97 /* get the packet I/O counts */
98 while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
99 for (cp = buffer; *cp && *cp == ' '; ++cp);
100 if (!strncmp(cp, ifname, namelen) &&
101 cp[namelen] == ':') {
103 if (sscanf(cp, netdevfmt,
104 counts, counts+1, counts+2,
105 counts+3,&found)>4) { /* found = dummy */
106 /* newer kernel with byte counts */
107 ifinfo->rx_packets=counts[1];
108 ifinfo->tx_packets=counts[3];
110 /* older kernel, no byte counts */
111 ifinfo->rx_packets=counts[0];
112 ifinfo->tx_packets=counts[2];
117 if (!found) return (FALSE);
119 /* see if the interface is up */
120 strcpy(request.ifr_name, ifname);
121 if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
123 if (!(request.ifr_flags & IFF_RUNNING))
126 /* get the IP address */
127 strcpy(request.ifr_name, ifname);
128 if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
130 ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
132 /* get the PPP destination IP address */
133 strcpy(request.ifr_name, ifname);
134 if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
135 ifinfo->dstaddr = ((struct sockaddr_in *)
136 (&request.ifr_dstaddr))->sin_addr;
138 /* get the netmask */
139 strcpy(request.ifr_name, ifname);
140 if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
141 ifinfo->netmask = ((struct sockaddr_in *)
142 (&request.ifr_netmask))->sin_addr;
149 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
151 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
152 FILE *stats_file = fopen("/proc/net/dev", "r");
155 if (socket_fd < 0 || !stats_file)
159 char *sp = strchr(ifname, '/');
163 result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
174 #elif defined __FreeBSD__
177 static struct nlist symbols[] =
182 static u_long ifnet_savedaddr;
183 static gid_t if_rgid;
184 static gid_t if_egid;
187 interface_set_gids(gid_t egid, gid_t rgid)
196 if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
199 if (kvm_nlist(kvmfd, symbols) < 0)
202 if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
209 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
214 unsigned long ifnet_addr = ifnet_savedaddr;
215 #if __FreeBSD_version >= 300001
216 struct ifnethead ifnethead;
217 struct ifaddrhead ifaddrhead;
219 struct ifaddr ifaddr;
220 unsigned long ifaddr_addr;
222 unsigned long sa_addr;
228 for (i = 0; ifname[i] && ifname[i] != '/'; i++)
229 iname[i] = ifname[i];
237 report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
244 #if __FreeBSD_version >= 300001
245 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
246 ifnet_addr = (u_long) ifnethead.tqh_first;
248 ifnet_addr = ifnet_savedaddr;
253 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
254 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
255 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
257 if (!strcmp(tname, iname))
259 if (!(ifnet.if_flags & IFF_UP))
266 ifinfo->rx_packets = ifnet.if_ipackets;
267 ifinfo->tx_packets = ifnet.if_opackets;
269 #if __FreeBSD_version >= 300001
270 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
272 ifaddr_addr = (u_long) ifnet.if_addrlist;
277 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
278 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
280 if (sa.sa_family != AF_INET)
282 #if __FreeBSD_version >= 300001
283 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
285 ifaddr_addr = (u_long) ifaddr.ifa_next;
290 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
291 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
292 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
293 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
294 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
308 #if __FreeBSD_version >= 300001
309 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
311 ifnet_addr = (unsigned long) ifnet.if_next;
320 #endif /* defined __FreeBSD__ */
323 #ifndef HAVE_INET_ATON
325 * Note: This is not a true replacement for inet_aton(), as it won't
326 * do the right thing on "255.255.255.255" (which translates to -1 on
327 * most machines). Fortunately this code will be used only if you're
328 * on an older Linux that lacks a real implementation.
330 #ifdef HAVE_NETINET_IN_SYSTM_H
331 # include <sys/types.h>
332 # include <netinet/in_systm.h>
335 #include <netinet/in.h>
336 #include <netinet/ip.h>
337 #include <arpa/inet.h>
340 static int inet_aton(const char *cp, struct in_addr *inp) {
343 addr = inet_addr(cp);
344 if (addr == ((long) -1)) return 0;
346 memcpy(inp, &addr, sizeof(addr));
349 #endif /* HAVE_INET_ATON */
351 void interface_parse(char *buf, struct hostdata *hp)
352 /* parse 'interface' specification */
356 hp->interface = xstrdup(buf);
358 /* find and isolate just the IP address */
359 if (!(cp1 = strchr(buf, '/')))
361 (void) report(stderr,
362 _("missing IP interface address\n"));
367 /* find and isolate just the netmask */
368 if (!(cp2 = strchr(cp1, '/')))
369 cp2 = "255.255.255.255";
373 /* convert IP address and netmask */
374 hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
375 if (!inet_aton(cp1, &hp->interface_pair->interface_address))
377 (void) report(stderr,
378 _("invalid IP interface address\n"));
381 if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
383 (void) report(stderr,
384 _("invalid IP interface mask\n"));
387 /* apply the mask now to the IP address (range) required */
388 hp->interface_pair->interface_address.s_addr &=
389 hp->interface_pair->interface_mask.s_addr;
391 /* restore original interface string (for configuration dumper) */
396 void interface_note_activity(struct hostdata *hp)
397 /* save interface I/O counts */
402 /* if not monitoring link, all done */
406 /* get the current I/O stats for the monitored link */
407 if (get_ifinfo(hp->monitor, &ifinfo))
408 /* update this and preceeding host entries using the link
409 (they were already set during this pass but the I/O
410 count has now changed and they need to be re-updated)
412 for (ctl = querylist; ctl; ctl = ctl->next) {
413 if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
414 ctl->server.monitor_io =
415 ifinfo.rx_packets + ifinfo.tx_packets;
416 /* do NOT update host entries following this one */
417 if (&ctl->server == hp)
421 #ifdef ACTIVITY_DEBUG
422 (void) report(stdout,
423 _("activity on %s -noted- as %d\n"),
424 hp->monitor, hp->monitor_io);
428 int interface_approve(struct hostdata *hp)
429 /* return TRUE if OK to poll, FALSE otherwise */
433 /* check interface IP address (range), if specified */
435 /* get interface info */
436 if (!get_ifinfo(hp->interface, &ifinfo)) {
437 (void) report(stdout,
438 _("skipping poll of %s, %s down\n"),
439 hp->pollname, hp->interface);
442 /* check the IP address (range) */
443 if ((ifinfo.addr.s_addr &
444 hp->interface_pair->interface_mask.s_addr) !=
445 hp->interface_pair->interface_address.s_addr) {
446 (void) report(stdout,
447 _("skipping poll of %s, %s IP address excluded\n"),
448 hp->pollname, hp->interface);
453 /* if not monitoring link, all done */
457 #ifdef ACTIVITY_DEBUG
458 (void) report(stdout,
459 _("activity on %s checked as %d\n"),
460 hp->monitor, hp->monitor_io);
462 /* if monitoring, check link for activity if it is up */
463 if (get_ifinfo(hp->monitor, &ifinfo) &&
464 hp->monitor_io == ifinfo.rx_packets +
466 (void) report(stdout,
467 _("skipping poll of %s, %s inactive\n"),
468 hp->pollname, hp->monitor);
472 #ifdef ACTIVITY_DEBUG
473 error(0, 0, _("activity on %s was %d, is %d"),
474 hp->monitor, hp->monitor_io,
475 ifinfo.rx_packets + ifinfo.tx_packets);
480 #endif /* (defined(linux) && !defined(INET6)) || defined(__FreeBSD__) */