Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/net KNF, simplify expressions for readability.
details: https://anonhg.NetBSD.org/src/rev/052fc10f07cd
branches: trunk
changeset: 350352:052fc10f07cd
user: christos <christos%NetBSD.org@localhost>
date: Tue Jan 10 17:51:01 2017 +0000
description:
KNF, simplify expressions for readability.
diffstat:
lib/libc/net/getaddrinfo.c | 166 ++++++++++++++++++++++----------------------
1 files changed, 84 insertions(+), 82 deletions(-)
diffs (truncated from 467 to 300 lines):
diff -r e3e9d0238220 -r 052fc10f07cd lib/libc/net/getaddrinfo.c
--- a/lib/libc/net/getaddrinfo.c Tue Jan 10 17:50:24 2017 +0000
+++ b/lib/libc/net/getaddrinfo.c Tue Jan 10 17:51:01 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: getaddrinfo.c,v 1.114 2016/02/06 19:33:07 riastradh Exp $ */
+/* $NetBSD: getaddrinfo.c,v 1.115 2017/01/10 17:51:01 christos Exp $ */
/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
/*
@@ -55,7 +55,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getaddrinfo.c,v 1.114 2016/02/06 19:33:07 riastradh Exp $");
+__RCSID("$NetBSD: getaddrinfo.c,v 1.115 2017/01/10 17:51:01 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#ifndef RUMP_ACTION
@@ -109,6 +109,9 @@
#define YES 1
#define NO 0
+#define sa4addr(sa) ((void *)&((struct sockaddr_in *)(void *)sa)->sin_addr)
+#define sa6addr(sa) ((void *)&((struct sockaddr_in6 *)(void *)sa)->sin6_addr)
+
static const char in_addrany[] = { 0, 0, 0, 0 };
static const char in_loopback[] = { 127, 0, 0, 1 };
#ifdef INET6
@@ -711,11 +714,11 @@
* If the number is small enough, we can skip the reordering process.
*/
if (n <= 1)
- return(n);
+ return n;
/* allocate a temporary array for sort and initialization of it. */
if ((aio = malloc(sizeof(*aio) * n)) == NULL)
- return(n); /* give up reordering */
+ return n; /* give up reordering */
memset(aio, 0, sizeof(*aio) * n);
/* retrieve address selection policy from the kernel */
@@ -723,7 +726,7 @@
if (!get_addrselectpolicy(&policyhead)) {
/* no policy is installed into kernel, we don't sort. */
free(aio);
- return (n);
+ return n;
}
for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
@@ -747,31 +750,33 @@
/* cleanup and return */
free(aio);
free_addrselectpolicy(&policyhead);
- return(n);
+ return n;
}
static int
get_addrselectpolicy(struct policyhead *head)
{
#ifdef INET6
- int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
+ static const int mib[] = {
+ CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
+ static const u_int miblen = (u_int)__arraycount(mib);
size_t l;
char *buf;
struct in6_addrpolicy *pol, *ep;
- if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
- return (0);
+ if (sysctl(mib, miblen, NULL, &l, NULL, 0) < 0)
+ return 0;
if (l == 0)
- return (0);
+ return 0;
if ((buf = malloc(l)) == NULL)
- return (0);
- if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
+ return 0;
+ if (sysctl(mib, miblen, buf, &l, NULL, 0) < 0) {
free(buf);
- return (0);
+ return 0;
}
- ep = (struct in6_addrpolicy *)(buf + l);
- for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
+ ep = (void *)(buf + l);
+ for (pol = (void *)buf; pol + 1 <= ep; pol++) {
struct policyqueue *new;
if ((new = malloc(sizeof(*new))) == NULL) {
@@ -783,9 +788,9 @@
}
free(buf);
- return (1);
+ return 1;
#else
- return (0);
+ return 0;
#endif
}
@@ -808,12 +813,13 @@
struct policyqueue *ent, *bestent = NULL;
struct in6_addrpolicy *pol;
int curmatchlen, bestmatchlen = -1;
- u_char *mp, *ep, *k, *p, m;
+ u_char *mp, *ep, *k, *p;
+ u_int m;
struct sockaddr_in6 key;
switch(addr->sa_family) {
case AF_INET6:
- key = *(struct sockaddr_in6 *)addr;
+ memcpy(&key, addr, sizeof(key));
break;
case AF_INET:
/* convert the address into IPv4-mapped IPv6 address. */
@@ -822,21 +828,20 @@
key.sin6_len = sizeof(key);
key.sin6_addr.s6_addr[10] = 0xff;
key.sin6_addr.s6_addr[11] = 0xff;
- memcpy(&key.sin6_addr.s6_addr[12],
- &((struct sockaddr_in *)addr)->sin_addr, 4);
+ memcpy(&key.sin6_addr.s6_addr[12], sa4addr(addr), 4);
break;
default:
- return(NULL);
+ return NULL;
}
for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
pol = &ent->pc_policy;
curmatchlen = 0;
- mp = (u_char *)&pol->addrmask.sin6_addr;
+ mp = (void *)&pol->addrmask.sin6_addr;
ep = mp + 16; /* XXX: scope field? */
- k = (u_char *)&key.sin6_addr;
- p = (u_char *)&pol->addr.sin6_addr;
+ k = (void *)&key.sin6_addr;
+ p = (void *)&pol->addr.sin6_addr;
for (; mp < ep && *mp; mp++, k++, p++) {
m = *mp;
if ((*k & m) != *p)
@@ -861,9 +866,9 @@
continue;
}
- return(bestent);
+ return bestent;
#else
- return(NULL);
+ return NULL;
#endif
}
@@ -897,7 +902,7 @@
ai.ai_next = NULL;
memset(&ss, 0, sizeof(ss));
memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
- ai.ai_addr = (struct sockaddr *)&ss;
+ ai.ai_addr = (void *)&ss;
get_port(&ai, "1", 0, svd);
/* open a socket to get the source address for the given dst */
@@ -939,26 +944,26 @@
{
int match = 0;
u_char *s, *d;
- u_char *lim, r;
- int addrlen;
+ u_char *lim;
+ u_int r, addrlen;
switch (src->sa_family) {
#ifdef INET6
case AF_INET6:
- s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
- d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
+ s = sa6addr(src);
+ d = sa6addr(dst);
addrlen = sizeof(struct in6_addr);
lim = s + addrlen;
break;
#endif
case AF_INET:
- s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
- d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
+ s = sa4addr(src);
+ d = sa4addr(dst);
addrlen = sizeof(struct in_addr);
lim = s + addrlen;
break;
default:
- return(0);
+ return 0;
}
while (s < lim)
@@ -970,7 +975,7 @@
break;
} else
match += 8;
- return(match);
+ return match;
}
static int
@@ -984,21 +989,21 @@
*/
if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
dst2->aio_srcsa.sa_family == AF_UNSPEC) {
- return(-1);
+ return -1;
}
if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
dst2->aio_srcsa.sa_family != AF_UNSPEC) {
- return(1);
+ return 1;
}
/* Rule 2: Prefer matching scope. */
if (dst1->aio_dstscope == dst1->aio_srcscope &&
dst2->aio_dstscope != dst2->aio_srcscope) {
- return(-1);
+ return -1;
}
if (dst1->aio_dstscope != dst1->aio_srcscope &&
dst2->aio_dstscope == dst2->aio_srcscope) {
- return(1);
+ return 1;
}
/* Rule 3: Avoid deprecated addresses. */
@@ -1006,11 +1011,11 @@
dst2->aio_srcsa.sa_family != AF_UNSPEC) {
if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
- return(-1);
+ return -1;
}
if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
!(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
- return(1);
+ return 1;
}
}
@@ -1025,7 +1030,7 @@
(dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
dst2->aio_srcpolicy->pc_policy.label !=
dst2->aio_dstpolicy->pc_policy.label)) {
- return(-1);
+ return -1;
}
if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
dst2->aio_srcpolicy->pc_policy.label ==
@@ -1033,7 +1038,7 @@
(dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
dst1->aio_srcpolicy->pc_policy.label !=
dst1->aio_dstpolicy->pc_policy.label)) {
- return(1);
+ return 1;
}
#endif
@@ -1043,13 +1048,13 @@
(dst2->aio_dstpolicy == NULL ||
dst1->aio_dstpolicy->pc_policy.preced >
dst2->aio_dstpolicy->pc_policy.preced)) {
- return(-1);
+ return -1;
}
if (dst2->aio_dstpolicy &&
(dst1->aio_dstpolicy == NULL ||
dst2->aio_dstpolicy->pc_policy.preced >
dst1->aio_dstpolicy->pc_policy.preced)) {
- return(1);
+ return 1;
}
#endif
@@ -1059,11 +1064,11 @@
/* Rule 8: Prefer smaller scope. */
if (dst1->aio_dstscope >= 0 &&
dst1->aio_dstscope < dst2->aio_dstscope) {
- return(-1);
+ return -1;
}
if (dst2->aio_dstscope >= 0 &&
dst2->aio_dstscope < dst1->aio_dstscope) {
- return(1);
+ return 1;
}
Home |
Main Index |
Thread Index |
Old Index