Clean up some minor nits:

+ style (KNF).
  . don't cast NULL
  . white space clean up
+ use consistent format for error messages.
+ plug memory leaks and avoid malloc(0).
+ verbose message should display even when routing table is empty
  (flushroutes).
This commit is contained in:
ginsbach 2005-08-30 19:01:25 +00:00
parent 85be4ce6e8
commit ba49deed2a
2 changed files with 44 additions and 36 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: route.c,v 1.91 2005/08/15 19:28:08 ginsbach Exp $ */ /* $NetBSD: route.c,v 1.92 2005/08/30 19:01:25 ginsbach Exp $ */
/* /*
* Copyright (c) 1983, 1989, 1991, 1993 * Copyright (c) 1983, 1989, 1991, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: route.c,v 1.91 2005/08/15 19:28:08 ginsbach Exp $"); __RCSID("$NetBSD: route.c,v 1.92 2005/08/30 19:01:25 ginsbach Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -296,18 +296,21 @@ bad: usage(*argv);
mib[5] = 0; /* no flags */ mib[5] = 0; /* no flags */
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
err(1, "route-sysctl-estimate"); err(1, "route-sysctl-estimate");
if (needed == 0) buf = lim = NULL;
return 0; if (needed) {
if ((buf = malloc(needed)) == NULL) if ((buf = malloc(needed)) == NULL)
err(1, "malloc"); err(1, "malloc");
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
err(1, "actual retrieval of routing table"); err(1, "actual retrieval of routing table");
lim = buf + needed; lim = buf + needed;
}
if (verbose) { if (verbose) {
(void)printf("Examining routing table from sysctl\n"); (void)printf("Examining routing table from sysctl\n");
if (af) if (af)
printf("(address family %s)\n", (*argv + 1)); printf("(address family %s)\n", (*argv + 1));
} }
if (needed == 0)
return 0;
seqno = 0; /* ??? */ seqno = 0; /* ??? */
for (next = buf; next < lim; next += rtm->rtm_msglen) { for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next; rtm = (struct rt_msghdr *)next;
@ -350,6 +353,7 @@ bad: usage(*argv);
(void)printf("done\n"); (void)printf("done\n");
} }
} }
free(buf);
return 0; return 0;
} }
@ -381,7 +385,6 @@ any_ntoa(const struct sockaddr *sa)
return obuf; return obuf;
} }
int int
netmask_length(struct sockaddr *nm, int family) netmask_length(struct sockaddr *nm, int family)
{ {
@ -481,7 +484,6 @@ netmask_string(struct sockaddr *mask, int len, int family)
return smask; return smask;
} }
const char * const char *
routename(struct sockaddr *sa, struct sockaddr *nm, int flags) routename(struct sockaddr *sa, struct sockaddr *nm, int flags)
{ {
@ -533,7 +535,7 @@ routename(struct sockaddr *sa, struct sockaddr *nm, int flags)
char *ccp; char *ccp;
if ((ccp = strchr(hp->h_name, '.')) && if ((ccp = strchr(hp->h_name, '.')) &&
!strcmp(ccp + 1, domain)) !strcmp(ccp + 1, domain))
*ccp = 0; *ccp = '\0';
cp = hp->h_name; cp = hp->h_name;
} }
} }
@ -587,7 +589,7 @@ routename(struct sockaddr *sa, struct sockaddr *nm, int flags)
char *ccp; char *ccp;
if (!nflag && (ccp = strchr(nihost, '.')) && if (!nflag && (ccp = strchr(nihost, '.')) &&
strcmp(ccp + 1, domain) == 0) strcmp(ccp + 1, domain) == 0)
*ccp = 0; *ccp = '\0';
strlcpy(line, nihost, sizeof(line)); strlcpy(line, nihost, sizeof(line));
} }
break; break;
@ -740,7 +742,6 @@ netname(struct sockaddr *sa, struct sockaddr *nm)
/* noncontiguous never happens in ipv6 */ /* noncontiguous never happens in ipv6 */
snprintf(line, sizeof(line), "::/%d", nml); snprintf(line, sizeof(line), "::/%d", nml);
} }
else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
line, sizeof(line), NULL, 0, niflags) != 0) line, sizeof(line), NULL, 0, niflags) != 0)
strlcpy(line, "invalid", sizeof(line)); strlcpy(line, "invalid", sizeof(line));
@ -990,7 +991,7 @@ newroute(int argc, char **argv)
"assuming route to if"); "assuming route to if");
} }
} else } else
usage((char *)NULL); usage(NULL);
iflag = 1; iflag = 1;
continue; continue;
} else if (ret > 0 && ret < 10) { } else if (ret > 0 && ret < 10) {
@ -1220,14 +1221,14 @@ getaddr(int which, char *s, struct hostent **hpp)
slash = 0; slash = 0;
} }
if (getaddrinfo(s, "0", &hints, &res) != 0) if (getaddrinfo(s, "0", &hints, &res) != 0)
errx(1, "bad value: %s", s); errx(1, "%s: bad value", s);
} }
if (slash) if (slash)
*slash = '/'; *slash = '/';
if (sizeof(su->sin6) != res->ai_addrlen) if (sizeof(su->sin6) != res->ai_addrlen)
errx(1, "%s: bad value", s); errx(1, "%s: bad value", s);
if (res->ai_next) if (res->ai_next)
errx(1, "address resolved to multiple values: %s", s); errx(1, "%s: address resolved to multiple values", s);
memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6)); memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
freeaddrinfo(res); freeaddrinfo(res);
#ifdef __KAME__ #ifdef __KAME__
@ -1353,7 +1354,7 @@ netdone:
memmove(&su->sin.sin_addr, hp->h_addr, hp->h_length); memmove(&su->sin.sin_addr, hp->h_addr, hp->h_length);
return (1); return (1);
} }
errx(1, "bad value: %s", s); errx(1, "%s: bad value", s);
/*NOTREACHED*/ /*NOTREACHED*/
} }
@ -1374,11 +1375,12 @@ prefixlen(const char *s)
#endif #endif
default: default:
errx(1, "prefixlen is not supported with af %d", af); errx(1, "prefixlen is not supported with af %d", af);
/*NOTREACHED*/
} }
rtm_addrs |= RTA_NETMASK; rtm_addrs |= RTA_NETMASK;
if (len < -1 || len > max) if (len < -1 || len > max)
errx(1, "bad value: %s", s); errx(1, "%s: bad value", s);
q = len >> 3; q = len >> 3;
r = len & 7; r = len & 7;
@ -1483,14 +1485,17 @@ interfaces(void)
mib[5] = 0; /* no flags */ mib[5] = 0; /* no flags */
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
err(1, "route-sysctl-estimate"); err(1, "route-sysctl-estimate");
if ((buf = malloc(needed)) == NULL) if (needed) {
err(1, "malloc"); if ((buf = malloc(needed)) == NULL)
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) err(1, "malloc");
err(1, "actual retrieval of interface table"); if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
lim = buf + needed; err(1, "actual retrieval of interface table");
for (next = buf; next < lim; next += rtm->rtm_msglen) { lim = buf + needed;
rtm = (struct rt_msghdr *)next; for (next = buf; next < lim; next += rtm->rtm_msglen) {
print_rtmsg(rtm, rtm->rtm_msglen); rtm = (struct rt_msghdr *)next;
print_rtmsg(rtm, rtm->rtm_msglen);
}
free(buf);
} }
} }

View File

@ -1,4 +1,4 @@
/* $NetBSD: show.c,v 1.26 2005/08/09 21:25:42 ginsbach Exp $ */ /* $NetBSD: show.c,v 1.27 2005/08/30 19:01:25 ginsbach Exp $ */
/* /*
* Copyright (c) 1983, 1988, 1993 * Copyright (c) 1983, 1988, 1993
@ -34,7 +34,7 @@
#if 0 #if 0
static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94"; static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
#else #else
__RCSID("$NetBSD: show.c,v 1.26 2005/08/09 21:25:42 ginsbach Exp $"); __RCSID("$NetBSD: show.c,v 1.27 2005/08/30 19:01:25 ginsbach Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -159,22 +159,25 @@ bad: usage(*argv);
mib[5] = 0; mib[5] = 0;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
err(1, "route-sysctl-estimate"); err(1, "route-sysctl-estimate");
if ((buf = malloc(needed)) == 0) buf = lim = NULL;
err(1, "malloc"); if (needed) {
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) if ((buf = malloc(needed)) == 0)
err(1, "sysctl of routing table"); err(1, "malloc");
lim = buf + needed; if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
err(1, "sysctl of routing table");
lim = buf + needed;
}
printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : ""); printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
/* for (i = 0; i <= AF_MAX; i++) ??? */ if (needed) {
{
for (next = buf; next < lim; next += rtm->rtm_msglen) { for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next; rtm = (struct rt_msghdr *)next;
sa = (struct sockaddr *)(rtm + 1); sa = (struct sockaddr *)(rtm + 1);
if (af == AF_UNSPEC || af == sa->sa_family) if (af == AF_UNSPEC || af == sa->sa_family)
p_rtentry(rtm); p_rtentry(rtm);
} }
free(buf);
} }
} }