From: jca Date: Wed, 29 Jun 2016 14:19:38 +0000 (+0000) Subject: Spring cleanup X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c74c57d42b99c0f4a61a5e28aba68b6ca891c1e6;p=openbsd Spring cleanup - pointless casts, kill caddr_t or replace it with char * - signed counters - simplify if_getmtu, only one method is needed and SIOCGIFMTU is the cheapest - we no longer have drivers for IFT_FDDI - hide details of iflist management - if (dflag) log_debug -> log_debug - dead code and comments - etc etc Input from and ok florian@ --- diff --git a/usr.sbin/rtadvd/config.c b/usr.sbin/rtadvd/config.c index 367b46bed0a..6c1a5181987 100644 --- a/usr.sbin/rtadvd/config.c +++ b/usr.sbin/rtadvd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.56 2016/03/01 20:51:05 jca Exp $ */ +/* $OpenBSD: config.c,v 1.57 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: config.c,v 1.62 2002/05/29 10:13:10 itojun Exp $ */ /* @@ -502,10 +502,9 @@ getconfig(char *intface) tmp->linkmtu = tmp->phymtu; } else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) { - log_warnx("advertised link mtu (%lu) on %s is invalid (must" + log_warnx("advertised link mtu (%u) on %s is invalid (must" " be between least MTU (%d) and physical link MTU (%d)", - (unsigned long)tmp->linkmtu, intface, - IPV6_MMTU, tmp->phymtu); + tmp->linkmtu, intface, IPV6_MMTU, tmp->phymtu); exit(1); } @@ -523,7 +522,7 @@ getconfig(char *intface) /* set timer */ tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update, tmp, tmp); - ra_timer_update((void *)tmp, &tmp->timer->tm); + ra_timer_update(tmp, &tmp->timer->tm); rtadvd_set_timer(&tmp->timer->tm, tmp->timer); } @@ -655,7 +654,7 @@ make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen) * reset the timer so that the new prefix will be advertised quickly. */ rai->initcounter = 0; - ra_timer_update((void *)rai, &rai->timer->tm); + ra_timer_update(rai, &rai->timer->tm); rtadvd_set_timer(&rai->timer->tm, rai->timer); } @@ -907,7 +906,6 @@ getinet6sysctl(int code) < 0) { log_warn("failed to get ip6 sysctl(%d)", code); return(-1); - } - else + } else return(value); } diff --git a/usr.sbin/rtadvd/dump.c b/usr.sbin/rtadvd/dump.c index 57a94b88f51..82d83f7a601 100644 --- a/usr.sbin/rtadvd/dump.c +++ b/usr.sbin/rtadvd/dump.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dump.c,v 1.19 2016/02/08 23:19:00 jca Exp $ */ +/* $OpenBSD: dump.c,v 1.20 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: dump.c,v 1.27 2002/05/29 14:23:55 itojun Exp $ */ /* @@ -112,7 +112,7 @@ rtadvd_dump(void) char *origin, *vltime, *pltime, *flags; char *vltimexpire=NULL, *pltimexpire=NULL; - gettimeofday(&now, NULL); /* XXX: unused in most cases */ + gettimeofday(&now, NULL); SLIST_FOREACH(rai, &ralist, entry) { log_info("%s:", rai->ifname); @@ -124,16 +124,14 @@ rtadvd_dump(void) time_t t = rai->lastsent.tv_sec; /* note that ctime() appends CR by itself */ log_info(" Last RA sent: %s", ctime(&t)); - } if (rai->timer) { time_t t = rai->timer->tm.tv_sec; log_info(" Next RA will be sent: %s", ctime(&t)); } else log_info(" RA timer is stopped"); - log_info(" waits: %d, initcount: %d", - - rai->waiting, rai->initcounter); + log_info(" waits: %u, initcount: %u", + rai->waiting, rai->initcounter); /* statistics */ log_info(" statistics: RA(out/in/inconsistent): " diff --git a/usr.sbin/rtadvd/if.c b/usr.sbin/rtadvd/if.c index 54de6c670e8..2e4c7a60959 100644 --- a/usr.sbin/rtadvd/if.c +++ b/usr.sbin/rtadvd/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.39 2016/03/01 20:51:05 jca Exp $ */ +/* $OpenBSD: if.c,v 1.40 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */ /* @@ -46,6 +46,7 @@ #include #include #include + #include "rtadvd.h" #include "if.h" #include "log.h" @@ -54,15 +55,11 @@ (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) #define NEXT_SA(ap) (ap) = (struct sockaddr *) \ - ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\ + ((char *)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\ sizeof(u_long)) :\ sizeof(u_long))) struct if_msghdr **iflist; -int iflist_init_ok; -size_t ifblock_size; -char *ifblock; - static void get_iflist(char **buf, size_t *size); static void parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize); @@ -113,46 +110,22 @@ if_nametosdl(char *name) int if_getmtu(char *name) { - struct ifaddrs *ifap, *ifa; - struct if_data *ifd; - u_long mtu = 0; - - if (getifaddrs(&ifap) < 0) - return(0); - for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (strcmp(ifa->ifa_name, name) == 0) { - ifd = ifa->ifa_data; - if (ifd) - mtu = ifd->ifi_mtu; - break; - } - } - freeifaddrs(ifap); - -#ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */ - if (mtu == 0) { - struct ifreq ifr; - int s; - - if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) - return(0); + int s; + struct ifreq ifr; + u_long mtu = 0; + if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0) { memset(&ifr, 0, sizeof(ifr)); ifr.ifr_addr.sa_family = AF_INET6; if (strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)) >= sizeof(ifr.ifr_name)) fatalx("strlcpy"); - if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) { - close(s); - return(0); - } + if (ioctl(s, SIOCGIFMTU, (char *)&ifr) >= 0) + mtu = ifr.ifr_mtu; close(s); - - mtu = ifr.ifr_mtu; } -#endif - return(mtu); + return (mtu); } /* give interface index and its old flags, then new flags returned */ @@ -168,7 +141,7 @@ if_getflags(int ifindex, int oifflags) } if_indextoname(ifindex, ifr.ifr_name); - if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { + if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) { log_warn("ioctl:SIOCGIFFLAGS: failed for %s", ifr.ifr_name); close(s); return (oifflags & ~IFF_UP); @@ -184,7 +157,6 @@ lladdropt_length(struct sockaddr_dl *sdl) switch (sdl->sdl_type) { case IFT_CARP: case IFT_ETHER: - case IFT_FDDI: return(ROUNDUP8(ETHER_ADDR_LEN + 2)); default: return(0); @@ -201,7 +173,6 @@ lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt) switch (sdl->sdl_type) { case IFT_CARP: case IFT_ETHER: - case IFT_FDDI: ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; addr = (char *)(ndopt + 1); memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); @@ -499,6 +470,9 @@ parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize) void init_iflist(void) { + static size_t ifblock_size; + static char *ifblock; + if (ifblock) { free(ifblock); ifblock_size = 0; diff --git a/usr.sbin/rtadvd/if.h b/usr.sbin/rtadvd/if.h index 077012ff8b4..afa3637a995 100644 --- a/usr.sbin/rtadvd/if.h +++ b/usr.sbin/rtadvd/if.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if.h,v 1.12 2016/02/08 23:19:00 jca Exp $ */ +/* $OpenBSD: if.h,v 1.13 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: if.h,v 1.6 2001/01/21 15:37:14 itojun Exp $ */ /* @@ -33,8 +33,6 @@ #define RTADV_TYPE2BITMASK(type) (0x1 << type) extern struct if_msghdr **iflist; -extern size_t ifblock_size; -extern char *ifblock; struct nd_opt_hdr; struct sockaddr_dl *if_nametosdl(char *); diff --git a/usr.sbin/rtadvd/rtadvd.c b/usr.sbin/rtadvd/rtadvd.c index 01520455506..19d914222ed 100644 --- a/usr.sbin/rtadvd/rtadvd.c +++ b/usr.sbin/rtadvd/rtadvd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rtadvd.c,v 1.73 2016/03/01 20:51:05 jca Exp $ */ +/* $OpenBSD: rtadvd.c,v 1.74 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: rtadvd.c,v 1.66 2002/05/29 14:18:36 itojun Exp $ */ /* @@ -38,7 +38,6 @@ #include #include -#include #include #include #include @@ -147,6 +146,7 @@ static void free_ndopts(union nd_opts *); static void ra_output(struct rainfo *); static void rtmsg_input(void); static void rtadvd_set_dump(int); +static struct rainfo *if_indextorainfo(int); int main(int argc, char *argv[]) @@ -203,9 +203,8 @@ main(int argc, char *argv[]) sock_open(); - if (sflag == 0) { + if (sflag == 0) rtsock_open(); - } if ((pw = getpwnam(RTADVD_USER)) == NULL) fatal("getpwnam(" RTADVD_USER ")"); @@ -299,8 +298,7 @@ die(void) int i; const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS; - if (dflag) - log_debug("cease to be an advertising router"); + log_debug("cease to be an advertising router"); SLIST_FOREACH(ra, &ralist, entry) { ra->lifetime = 0; @@ -328,19 +326,17 @@ rtmsg_input(void) char addrbuf[INET6_ADDRSTRLEN]; n = read(rtsock, msg, sizeof(msg)); - if (dflag) - log_debug("received a routing message " - "(type = %d, len = %d)", rtmsg_type(msg), n); + log_debug("received a routing message " + "(type = %d, len = %d)", rtmsg_type(msg), n); if (n > rtmsg_len(msg)) { /* * This usually won't happen for messages received on * a routing socket. */ - if (dflag) - log_debug("received data length is larger than " - "1st routing message len. multiple messages? " - "read %d bytes, but 1st msg len = %d", - n, rtmsg_len(msg)); + log_debug("received data length is larger than " + "1st routing message len. multiple messages? " + "read %d bytes, but 1st msg len = %d", + n, rtmsg_len(msg)); #if 0 /* adjust length */ n = rtmsg_len(msg); @@ -370,17 +366,15 @@ rtmsg_input(void) break; default: /* should not reach here */ - if (dflag) - log_debug("unknown rtmsg %d on %s", - type, if_indextoname(ifindex, ifname)); + log_debug("unknown rtmsg %d on %s", + type, if_indextoname(ifindex, ifname)); continue; } if ((rai = if_indextorainfo(ifindex)) == NULL) { - if (dflag) - log_debug("route changed on " - "non advertising interface(%s)", - if_indextoname(ifindex, ifname)); + log_debug("route changed on " + "non advertising interface(%s)", + if_indextoname(ifindex, ifname)); continue; } oldifflags = iflist[ifindex]->ifm_flags; @@ -405,13 +399,12 @@ rtmsg_input(void) } prefix = find_prefix(rai, addr, plen); if (prefix) { - if (dflag) - log_debug("new prefix(%s/%d) " - "added on %s, " - "but it was already in list", - inet_ntop(AF_INET6, addr, - (char *)addrbuf, INET6_ADDRSTRLEN), - plen, rai->ifname); + log_debug("new prefix(%s/%d) " + "added on %s, " + "but it was already in list", + inet_ntop(AF_INET6, addr, + addrbuf, INET6_ADDRSTRLEN), + plen, rai->ifname); break; } make_prefix(rai, ifindex, addr, plen); @@ -435,13 +428,12 @@ rtmsg_input(void) } prefix = find_prefix(rai, addr, plen); if (prefix == NULL) { - if (dflag) - log_debug("prefix(%s/%d) was " - "deleted on %s, " - "but it was not in list", - inet_ntop(AF_INET6, addr, - (char *)addrbuf, INET6_ADDRSTRLEN), - plen, rai->ifname); + log_debug("prefix(%s/%d) was " + "deleted on %s, " + "but it was not in list", + inet_ntop(AF_INET6, addr, + addrbuf, INET6_ADDRSTRLEN), + plen, rai->ifname); break; } delete_prefix(rai, prefix); @@ -457,9 +449,8 @@ rtmsg_input(void) break; default: /* should not reach here */ - if (dflag) - log_debug("unknown rtmsg %d on %s", - type, if_indextoname(ifindex, ifname)); + log_debug("unknown rtmsg %d on %s", + type, if_indextoname(ifindex, ifname)); return; } @@ -478,7 +469,7 @@ rtmsg_input(void) rai->waiting = 0; /* XXX */ rai->timer = rtadvd_add_timer(ra_timeout, ra_timer_update, rai, rai); - ra_timer_update((void *)rai, &rai->timer->tm); + ra_timer_update(rai, &rai->timer->tm); rtadvd_set_timer(&rai->timer->tm, rai->timer); } } @@ -989,7 +980,7 @@ find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen) bytelen = plen / 8; bitlen = plen % 8; bitmask = 0xff << (8 - bitlen); - if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen)) + if (memcmp(prefix, &pp->prefix, bytelen)) continue; if (bitlen == 0 || ((prefix->s6_addr[bytelen] & bitmask) == @@ -1013,7 +1004,7 @@ nd6_options(struct nd_opt_hdr *hdr, int limit, goto bad; } - hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen); + hdr = (struct nd_opt_hdr *)((char *)hdr + optlen); if (hdr->nd_opt_len == 0) { log_warnx("bad ND option length(0) (type = %d)", hdr->nd_opt_type); @@ -1115,7 +1106,7 @@ free_ndopts(union nd_opts *ndopts) } } -void +static void sock_open(void) { struct rainfo *ra; @@ -1164,7 +1155,7 @@ sock_open(void) */ if (inet_pton(AF_INET6, ALLROUTERS_LINK, &mreq.ipv6mr_multiaddr.s6_addr) != 1) - fatal("inet_pton failed(library bug?)"); + fatal("inet_pton"); SLIST_FOREACH(ra, &ralist, entry) { mreq.ipv6mr_interface = ra->ifindex; if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, @@ -1174,23 +1165,21 @@ sock_open(void) } } - ra = SLIST_FIRST(&ralist); - /* initialize msghdr for receiving packets */ - rcviov[0].iov_base = (caddr_t)answer; + rcviov[0].iov_base = answer; rcviov[0].iov_len = sizeof(answer); - rcvmhdr.msg_name = (caddr_t)&from; + rcvmhdr.msg_name = &from; rcvmhdr.msg_namelen = sizeof(from); rcvmhdr.msg_iov = rcviov; rcvmhdr.msg_iovlen = 1; - rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; + rcvmhdr.msg_control = rcvcmsgbuf; rcvmhdr.msg_controllen = rcvcmsgbuflen; /* initialize msghdr for sending packets */ sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); sndmhdr.msg_iov = sndiov; sndmhdr.msg_iovlen = 1; - sndmhdr.msg_control = (caddr_t)sndcmsgbuf; + sndmhdr.msg_control = sndcmsgbuf; sndmhdr.msg_controllen = sndcmsgbuflen; } @@ -1215,7 +1204,7 @@ rtsock_open(void) fatal("setsockopt(ROUTE_MSGFILTER)"); } -struct rainfo * +static struct rainfo * if_indextorainfo(int index) { struct rainfo *rai; @@ -1231,9 +1220,9 @@ if_indextorainfo(int index) static void ra_output(struct rainfo *rainfo) { - int i; struct cmsghdr *cm; struct in6_pktinfo *pi; + size_t len; if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) { log_debug("%s is not up, skip sending RA", rainfo->ifname); @@ -1242,8 +1231,8 @@ ra_output(struct rainfo *rainfo) make_packet(rainfo); /* XXX: inefficient */ - sndmhdr.msg_name = (caddr_t)&sin6_allnodes; - sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data; + sndmhdr.msg_name = &sin6_allnodes; + sndmhdr.msg_iov[0].iov_base = rainfo->ra_data; sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen; cm = CMSG_FIRSTHDR(&sndmhdr); @@ -1266,14 +1255,13 @@ ra_output(struct rainfo *rainfo) memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); } - log_debug("send RA on %s, # of waitings = %d", + log_debug("send RA on %s, # of waitings = %u", rainfo->ifname, rainfo->waiting); - i = sendmsg(sock, &sndmhdr, 0); + len = sendmsg(sock, &sndmhdr, 0); - if (i < 0 || i != rainfo->ra_datalen) - if (i < 0) - log_warn("sendmsg on %s", rainfo->ifname); + if (len < 0) + log_warn("sendmsg on %s", rainfo->ifname); /* update counter */ if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS) @@ -1293,10 +1281,6 @@ ra_timeout(void *data) { struct rainfo *rai = (struct rainfo *)data; -#ifdef notyet - /* if necessary, reconstruct the packet. */ -#endif - log_debug("RA timer on %s is expired", rai->ifname); ra_output(rai); diff --git a/usr.sbin/rtadvd/rtadvd.h b/usr.sbin/rtadvd/rtadvd.h index e13cdf6ce42..c3c94b089ef 100644 --- a/usr.sbin/rtadvd/rtadvd.h +++ b/usr.sbin/rtadvd/rtadvd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rtadvd.h,v 1.24 2016/03/01 12:51:34 jca Exp $ */ +/* $OpenBSD: rtadvd.h,v 1.25 2016/06/29 14:19:38 jca Exp $ */ /* $KAME: rtadvd.h,v 1.20 2002/05/29 10:13:10 itojun Exp $ */ /* @@ -120,9 +120,9 @@ struct rainfo { /* timer related parameters */ struct rtadvd_timer *timer; - int initcounter; /* counter for the first few advertisements */ + unsigned int initcounter; /* counter for the first few advertisements */ struct timeval lastsent; /* timestamp when the latest RA was sent */ - int waiting; /* number of RS waiting for RA */ + unsigned int waiting; /* number of RS waiting for RA */ /* interface information */ int ifindex; @@ -165,5 +165,4 @@ SLIST_HEAD(ralist, rainfo); void ra_timeout(void *); void ra_timer_update(void *, struct timeval *); -struct rainfo *if_indextorainfo(int); struct prefix *find_prefix(struct rainfo *, struct in6_addr *, int);