forked from pool/libtirpc
158 lines
4.9 KiB
Diff
158 lines
4.9 KiB
Diff
From d94b92d5125242ce595c1baf42a1e6d1004b7756 Mon Sep 17 00:00:00 2001
|
|
From: Olaf Kirch <okir@suse.de>
|
|
Date: Tue, 30 Sep 2008 15:06:54 -0400
|
|
Subject: [PATCH] Introduce __rpc_set_netbuf helper
|
|
|
|
The RPC code contains a number of places where a netbuf
|
|
is initialized with some data. All the mem_alloc/memcpy
|
|
stuff is open-coded. Introduce a helper function and
|
|
convert the code.
|
|
|
|
Signed-off-by: Olaf Kirch <okir@suse.de>
|
|
Signed-off-by: Steve Dickson <steved@redhat.com>
|
|
---
|
|
src/rpc_com.h | 1 +
|
|
src/rpc_generic.c | 19 +++++++++++++++++++
|
|
src/svc_dg.c | 14 +++-----------
|
|
src/svc_vc.c | 24 ++++++------------------
|
|
4 files changed, 29 insertions(+), 29 deletions(-)
|
|
|
|
diff --git a/src/rpc_com.h b/src/rpc_com.h
|
|
index a935080..0981471 100644
|
|
--- a/src/rpc_com.h
|
|
+++ b/src/rpc_com.h
|
|
@@ -61,6 +61,7 @@ extern u_int __rpc_get_a_size(int);
|
|
extern int __rpc_dtbsize(void);
|
|
extern struct netconfig * __rpcgettp(int);
|
|
extern int __rpc_get_default_domain(char **);
|
|
+struct netbuf *__rpc_set_netbuf(struct netbuf *, const void *, size_t);
|
|
|
|
char *__rpc_taddr2uaddr_af(int, const struct netbuf *);
|
|
struct netbuf *__rpc_uaddr2taddr_af(int, const char *);
|
|
diff --git a/src/rpc_generic.c b/src/rpc_generic.c
|
|
index b436e3a..9ada668 100644
|
|
--- a/src/rpc_generic.c
|
|
+++ b/src/rpc_generic.c
|
|
@@ -833,3 +833,22 @@ __rpc_sockisbound(int fd)
|
|
|
|
return 0;
|
|
}
|
|
+
|
|
+/*
|
|
+ * Helper function to set up a netbuf
|
|
+ */
|
|
+struct netbuf *
|
|
+__rpc_set_netbuf(struct netbuf *nb, const void *ptr, size_t len)
|
|
+{
|
|
+ if (nb->len != len) {
|
|
+ if (nb->len)
|
|
+ mem_free(nb->buf, nb->len);
|
|
+ nb->buf = mem_alloc(len);
|
|
+ if (nb->buf == NULL)
|
|
+ return NULL;
|
|
+
|
|
+ nb->maxlen = nb->len = len;
|
|
+ }
|
|
+ memcpy(nb->buf, ptr, len);
|
|
+ return nb;
|
|
+}
|
|
diff --git a/src/svc_dg.c b/src/svc_dg.c
|
|
index 76a480e..7df470e 100644
|
|
--- a/src/svc_dg.c
|
|
+++ b/src/svc_dg.c
|
|
@@ -140,10 +140,7 @@ svc_dg_create(fd, sendsize, recvsize)
|
|
slen = sizeof ss;
|
|
if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
|
|
goto freedata;
|
|
- xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
|
|
- xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
|
|
- xprt->xp_ltaddr.len = slen;
|
|
- memcpy(xprt->xp_ltaddr.buf, &ss, slen);
|
|
+ __rpc_set_netbuf(&xprt->xp_ltaddr, &ss, slen);
|
|
|
|
xprt_register(xprt);
|
|
return (xprt);
|
|
@@ -186,13 +183,8 @@ again:
|
|
goto again;
|
|
if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
|
|
return (FALSE);
|
|
- if (xprt->xp_rtaddr.len < alen) {
|
|
- if (xprt->xp_rtaddr.len != 0)
|
|
- mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
|
|
- xprt->xp_rtaddr.buf = mem_alloc(alen);
|
|
- xprt->xp_rtaddr.len = alen;
|
|
- }
|
|
- memcpy(xprt->xp_rtaddr.buf, &ss, alen);
|
|
+ __rpc_set_netbuf(&xprt->xp_rtaddr, &ss, alen);
|
|
+
|
|
__xprt_set_raddr(xprt, &ss);
|
|
xdrs->x_op = XDR_DECODE;
|
|
XDR_SETPOS(xdrs, 0);
|
|
diff --git a/src/svc_vc.c b/src/svc_vc.c
|
|
index 0d532a0..44d3497 100644
|
|
--- a/src/svc_vc.c
|
|
+++ b/src/svc_vc.c
|
|
@@ -184,14 +184,10 @@ svc_vc_create(fd, sendsize, recvsize)
|
|
goto cleanup_svc_vc_create;
|
|
}
|
|
|
|
- xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sizeof(sslocal);
|
|
- xprt->xp_ltaddr.buf = mem_alloc((size_t)sizeof(sslocal));
|
|
- if (xprt->xp_ltaddr.buf == NULL) {
|
|
+ if (!__rpc_set_netbuf(&xprt->xp_ltaddr, &sslocal, sizeof(sslocal))) {
|
|
warnx("svc_vc_create: no mem for local addr");
|
|
goto cleanup_svc_vc_create;
|
|
}
|
|
- memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sizeof(sslocal));
|
|
- xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
|
|
xprt_register(xprt);
|
|
return (xprt);
|
|
cleanup_svc_vc_create:
|
|
@@ -225,25 +221,20 @@ svc_fd_create(fd, sendsize, recvsize)
|
|
warnx("svc_fd_create: could not retrieve local addr");
|
|
goto freedata;
|
|
}
|
|
- ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = sizeof(ss);
|
|
- ret->xp_ltaddr.buf = mem_alloc((size_t)sizeof(ss));
|
|
- if (ret->xp_ltaddr.buf == NULL) {
|
|
+ if (!__rpc_set_netbuf(&ret->xp_ltaddr, &ss, sizeof(ss))) {
|
|
warnx("svc_fd_create: no mem for local addr");
|
|
goto freedata;
|
|
}
|
|
- memcpy(ret->xp_ltaddr.buf, &ss, (size_t)sizeof(ss));
|
|
+
|
|
slen = sizeof (struct sockaddr_storage);
|
|
if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
|
|
warnx("svc_fd_create: could not retrieve remote addr");
|
|
goto freedata;
|
|
}
|
|
- ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = sizeof(ss);
|
|
- ret->xp_rtaddr.buf = mem_alloc((size_t)sizeof(ss));
|
|
- if (ret->xp_rtaddr.buf == NULL) {
|
|
+ if (!__rpc_set_netbuf(&ret->xp_rtaddr, &ss, sizeof(ss))) {
|
|
warnx("svc_fd_create: no mem for local addr");
|
|
goto freedata;
|
|
}
|
|
- memcpy(ret->xp_rtaddr.buf, &ss, (size_t)sizeof(ss));
|
|
|
|
/* Set xp_raddr for compatibility */
|
|
__xprt_set_raddr(ret, &ss);
|
|
@@ -340,12 +331,9 @@ again:
|
|
*/
|
|
|
|
newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
|
|
- newxprt->xp_rtaddr.buf = mem_alloc(len);
|
|
- if (newxprt->xp_rtaddr.buf == NULL)
|
|
- return (FALSE);
|
|
|
|
- memcpy(newxprt->xp_rtaddr.buf, &addr, len);
|
|
- newxprt->xp_rtaddr.maxlen = newxprt->xp_rtaddr.len = len;
|
|
+ if (!__rpc_set_netbuf(&newxprt->xp_rtaddr, &addr, len))
|
|
+ return (FALSE);
|
|
|
|
__xprt_set_raddr(newxprt, &addr);
|
|
|
|
--
|
|
1.5.6
|
|
|