route.h: introduce a macro, SA_SIZE(struct sockaddr *) which returns

the space occupied by a struct sockaddr when passed through a
routing socket.
Use it to replace the macro ROUNDUP(int), that does the same but
is redefined by every file which uses it, courtesy of
the School of Cut'n'Paste Programming(TM).

(partial) userland changes to follow.
This commit is contained in:
Luigi Rizzo 2004-04-13 11:22:22 +00:00
parent 9869addc51
commit e74642df71
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=128185
3 changed files with 19 additions and 14 deletions

View File

@ -1001,15 +1001,13 @@ rt_fixchange(struct radix_node *rn, void *vp)
rt_mask(rt), rt->rt_flags, (struct rtentry **)0);
}
#define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
int
rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
{
/* XXX dst may be overwritten, can we move this to below */
struct radix_node_head *rnh = rt_tables[dst->sa_family];
caddr_t new, old;
int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
RT_LOCK_ASSERT(rt);
@ -1037,7 +1035,7 @@ rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
* if we need to malloc a new chunk, then keep the old one around
* till we don't need it any more.
*/
if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
if (rt->rt_gateway == 0 || glen > SA_SIZE(rt->rt_gateway)) {
old = (caddr_t)rt_key(rt);
R_Malloc(new, caddr_t, dlen + glen);
if (new == 0)

View File

@ -260,6 +260,18 @@ struct rt_addrinfo {
struct ifnet *rti_ifp;
};
/*
* This macro returns the size of a struct sockaddr when passed
* through a routing socket. Basically we round up sa_len to
* a multiple of sizeof(long), with a minimum of sizeof(long).
* The check for a NULL pointer is just a convenience, probably never used.
* The case sa_len == 0 should only apply to empty structures.
*/
#define SA_SIZE(sa) \
( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \
sizeof(long) : \
1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
#ifdef _KERNEL
#define RT_LOCK_INIT(_rt) \

View File

@ -556,9 +556,6 @@ rt_getmetrics(struct rt_metrics_lite *in, struct rt_metrics *out)
#undef metric
}
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
/*
* Extract the addresses of the passed sockaddrs.
* Do a little sanity checking so as to avoid bad memory references.
@ -567,9 +564,8 @@ rt_getmetrics(struct rt_metrics_lite *in, struct rt_metrics *out)
static int
rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
{
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
register struct sockaddr *sa;
register int i;
struct sockaddr *sa;
int i;
for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
if ((rtinfo->rti_addrs & (1 << i)) == 0)
@ -593,10 +589,9 @@ rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
}
/* accept it */
rtinfo->rti_info[i] = sa;
ADVANCE(cp, sa);
cp += SA_SIZE(sa);
}
return (0);
#undef ADVANCE
}
static struct mbuf *
@ -651,7 +646,7 @@ rt_msg1(int type, struct rt_addrinfo *rtinfo)
if ((sa = rtinfo->rti_info[i]) == NULL)
continue;
rtinfo->rti_addrs |= (1 << i);
dlen = ROUNDUP(sa->sa_len);
dlen = SA_SIZE(sa);
m_copyback(m, len, dlen, (caddr_t)sa);
len += dlen;
}
@ -701,7 +696,7 @@ rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
if ((sa = rtinfo->rti_info[i]) == 0)
continue;
rtinfo->rti_addrs |= (1 << i);
dlen = ROUNDUP(sa->sa_len);
dlen = SA_SIZE(sa);
if (cp) {
bcopy((caddr_t)sa, cp, (unsigned)dlen);
cp += dlen;