X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=interface.c;h=f9445cb8aba1a01948dc50beb25aed0445146ae3;hb=f16d8d23439b5569f0c2e1af22494708b507f277;hp=f01fb1ba74e43e495fa11cd41554e623a127aad9;hpb=ae4dda9f0c40945e4ccdf925d1897c4b739ff932;p=~andy%2Ffetchmail diff --git a/interface.c b/interface.c index f01fb1ba..f9445cb8 100644 --- a/interface.c +++ b/interface.c @@ -24,12 +24,8 @@ #include #include -#if defined(STDC_HEADERS) #include -#endif -#if defined(HAVE_UNISTD_H) #include -#endif #include #include #include @@ -37,20 +33,18 @@ #include #if defined(__FreeBSD__) #if defined __FreeBSD_USE_KVM -#if __FreeBSD_version >= 300001 #include -#endif #include #include #include -#else +#else /* !defined __FreeBSD_USE_KVM */ #include #include #include -#endif -#endif +#endif /* defined __FreeBSD_USE_KVM */ +#endif /* defined __FreeBSD__ */ #include "socket.h" -#include "i18n.h" +#include "gettext.h" #include "tunable.h" typedef struct { @@ -63,8 +57,6 @@ struct interface_pair_s { struct in_addr interface_mask; } *interface_pair; -static char *netdevfmt; - /* * Count of packets to see on an interface before monitor considers it up. * Needed because when pppd shuts down the link, the packet counts go up @@ -76,6 +68,8 @@ static char *netdevfmt; #ifdef linux #define have_interface_init +static const char *netdevfmt; + void interface_init(void) /* figure out which /proc/net/dev format to use */ { @@ -173,13 +167,13 @@ static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo) result = FALSE; else { - char *sp = strchr(ifname, '/'); - + char *tmp = xstrdup(ifname); + char *sp = strchr(tmp, '/'); + /* hide slash and trailing info from ifname */ if (sp) *sp = '\0'; - result = _get_ifinfoGT_(socket_fd, stats_file, ifname, ifinfo); - if (sp) - *sp = '/'; + result = _get_ifinfoGT_(socket_fd, stats_file, tmp, ifinfo); + free(tmp); } if (socket_fd >= 0) SockClose(socket_fd); @@ -231,14 +225,11 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) char iname[16]; struct ifnet ifnet; unsigned long ifnet_addr = ifnet_savedaddr; -#if __FreeBSD_version >= 300001 struct ifnethead ifnethead; struct ifaddrhead ifaddrhead; -#endif struct ifaddr ifaddr; unsigned long ifaddr_addr; struct sockaddr sa; - unsigned long sa_addr; uint i; if (if_egid) @@ -260,18 +251,14 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) } } -#if __FreeBSD_version >= 300001 kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead); ifnet_addr = (u_long) ifnethead.tqh_first; -#else - ifnet_addr = ifnet_savedaddr; -#endif while (ifnet_addr) { kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet)); kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname); - snprintf(tname, sizeof tname, "%s%d", tname, ifnet.if_unit); + snprintf(tname + strlen(tname), sizeof(tname) - strlen(tname), "%d", ifnet.if_unit); if (!strcmp(tname, iname)) { @@ -285,11 +272,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) ifinfo->rx_packets = ifnet.if_ipackets; ifinfo->tx_packets = ifnet.if_opackets; -#if __FreeBSD_version >= 300001 ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first; -#else - ifaddr_addr = (u_long) ifnet.if_addrlist; -#endif while(ifaddr_addr) { @@ -298,11 +281,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) if (sa.sa_family != AF_INET) { -#if __FreeBSD_version >= 300001 ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next; -#else - ifaddr_addr = (u_long) ifaddr.ifa_next; -#endif continue; } @@ -324,11 +303,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) return 0; } -#if __FreeBSD_version >= 300001 ifnet_addr = (u_long) ifnet.if_link.tqe_next; -#else - ifnet_addr = (unsigned long) ifnet.if_next; -#endif } if (if_egid) @@ -384,7 +359,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) char iname[16]; int mib[6]; - memset(ifinfo, 0, sizeof(ifinfo)); + memset(ifinfo, 0, sizeof(*ifinfo)); /* trim interface name */ @@ -418,7 +393,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) GT_("get_ifinfo: sysctl (iflist estimate) failed")); exit(1); } - if ((buf = malloc(needed)) == NULL) + if ((buf = (char *)malloc(needed)) == NULL) { report(stderr, GT_("get_ifinfo: malloc failed")); @@ -514,7 +489,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) } sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK]; - if (!sin) + if (sin) { ifinfo->netmask = sin->sin_addr; } @@ -524,7 +499,7 @@ get_ifinfo(const char *ifname, ifinfo_t *ifinfo) * of non point-to-point link */ sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD]; - if (!sin) + if (sin) { ifinfo->dstaddr = sin->sin_addr; } @@ -547,11 +522,6 @@ get_ifinfo_end: #endif -#ifndef have_interface_init -void interface_init(void) {}; -#endif - - #ifndef HAVE_INET_ATON /* * Note: This is not a true replacement for inet_aton(), as it won't @@ -584,6 +554,7 @@ void interface_parse(char *buf, struct hostdata *hp) /* parse 'interface' specification */ { char *cp1, *cp2; + char mask1[] = "255.255.255.255"; hp->interface = xstrdup(buf); @@ -598,7 +569,7 @@ void interface_parse(char *buf, struct hostdata *hp) /* find and isolate just the netmask */ if (!(cp2 = strchr(cp1, '/'))) - cp2 = "255.255.255.255"; + cp2 = mask1; else *cp2++ = '\000'; @@ -739,3 +710,7 @@ int interface_approve(struct hostdata *hp, flag domonitor) return(TRUE); } #endif /* CAN_MONITOR */ + +#ifndef have_interface_init +void interface_init(void) {} +#endif