Deleted Added
full compact
svc_vc.c (95658) svc_vc.c (109359)
1/* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
35#endif
36#include <sys/cdefs.h>
1/* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
35#endif
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libc/rpc/svc_vc.c 95658 2002-04-28 15:18:50Z des $");
37__FBSDID("$FreeBSD: head/lib/libc/rpc/svc_vc.c 109359 2003-01-16 07:13:51Z mbr $");
38
39/*
40 * svc_vc.c, Server side for Connection Oriented based RPC.
41 *
42 * Actually implements two flavors of transporter -
43 * a tcp rendezvouser (a listner and connection establisher)
44 * and a record/tcp stream.
45 */
46
47#include "namespace.h"
48#include "reentrant.h"
49#include <sys/types.h>
50#include <sys/param.h>
51#include <sys/poll.h>
52#include <sys/socket.h>
53#include <sys/un.h>
38
39/*
40 * svc_vc.c, Server side for Connection Oriented based RPC.
41 *
42 * Actually implements two flavors of transporter -
43 * a tcp rendezvouser (a listner and connection establisher)
44 * and a record/tcp stream.
45 */
46
47#include "namespace.h"
48#include "reentrant.h"
49#include <sys/types.h>
50#include <sys/param.h>
51#include <sys/poll.h>
52#include <sys/socket.h>
53#include <sys/un.h>
54#include <sys/time.h>
54#include <sys/uio.h>
55#include <netinet/in.h>
56#include <netinet/tcp.h>
57
58#include <assert.h>
59#include <err.h>
60#include <errno.h>
55#include <sys/uio.h>
56#include <netinet/in.h>
57#include <netinet/tcp.h>
58
59#include <assert.h>
60#include <err.h>
61#include <errno.h>
62#include <fcntl.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65
66#include <rpc/rpc.h>
67
68#include "rpc_com.h"
69#include "un-namespace.h"
70
71struct cmessage {
72 struct cmsghdr cmsg;
73 struct cmsgcred cmcred;
74};
75
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67
68#include <rpc/rpc.h>
69
70#include "rpc_com.h"
71#include "un-namespace.h"
72
73struct cmessage {
74 struct cmsghdr cmsg;
75 struct cmsgcred cmcred;
76};
77
78extern rwlock_t svc_fd_lock;
79
76static SVCXPRT *makefd_xprt(int, u_int, u_int);
77static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
78static enum xprt_stat rendezvous_stat(SVCXPRT *);
79static void svc_vc_destroy(SVCXPRT *);
80static SVCXPRT *makefd_xprt(int, u_int, u_int);
81static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
82static enum xprt_stat rendezvous_stat(SVCXPRT *);
83static void svc_vc_destroy(SVCXPRT *);
84static void __svc_vc_dodestroy (SVCXPRT *);
80static int read_vc(void *, void *, int);
81static int write_vc(void *, void *, int);
82static enum xprt_stat svc_vc_stat(SVCXPRT *);
83static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
84static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
85static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
86static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
87static void svc_vc_rendezvous_ops(SVCXPRT *);
88static void svc_vc_ops(SVCXPRT *);
89static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
85static int read_vc(void *, void *, int);
86static int write_vc(void *, void *, int);
87static enum xprt_stat svc_vc_stat(SVCXPRT *);
88static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
89static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
90static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
91static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
92static void svc_vc_rendezvous_ops(SVCXPRT *);
93static void svc_vc_ops(SVCXPRT *);
94static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
95static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
96 void *in);
90static int __msgread_withcred(int, void *, size_t, struct cmessage *);
91static int __msgwrite(int, void *, size_t);
92
93struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
94 u_int sendsize;
95 u_int recvsize;
97static int __msgread_withcred(int, void *, size_t, struct cmessage *);
98static int __msgwrite(int, void *, size_t);
99
100struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
101 u_int sendsize;
102 u_int recvsize;
103 int maxrec;
96};
97
98struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
99 enum xprt_stat strm_stat;
100 u_int32_t x_id;
101 XDR xdrs;
102 char verf_body[MAX_AUTH_BYTES];
104};
105
106struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
107 enum xprt_stat strm_stat;
108 u_int32_t x_id;
109 XDR xdrs;
110 char verf_body[MAX_AUTH_BYTES];
111 u_int sendsize;
112 u_int recvsize;
113 int maxrec;
114 bool_t nonblock;
115 struct timeval last_recv_time;
103};
104
105/*
106 * Usage:
107 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
108 *
109 * Creates, registers, and returns a (rpc) tcp based transporter.
110 * Once *xprt is initialized, it is registered as a transporter
111 * see (svc.h, xprt_register). This routine returns
112 * a NULL if a problem occurred.
113 *
114 * The filedescriptor passed in is expected to refer to a bound, but
115 * not yet connected socket.
116 *
117 * Since streams do buffered io similar to stdio, the caller can specify
118 * how big the send and receive buffers are via the second and third parms;
119 * 0 => use the system default.
120 */
121SVCXPRT *
122svc_vc_create(fd, sendsize, recvsize)
123 int fd;
124 u_int sendsize;
125 u_int recvsize;
126{
127 SVCXPRT *xprt;
128 struct cf_rendezvous *r = NULL;
129 struct __rpc_sockinfo si;
130 struct sockaddr_storage sslocal;
131 socklen_t slen;
132
133 r = mem_alloc(sizeof(*r));
134 if (r == NULL) {
135 warnx("svc_vc_create: out of memory");
136 goto cleanup_svc_vc_create;
137 }
138 if (!__rpc_fd2sockinfo(fd, &si))
139 return NULL;
140 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
141 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
116};
117
118/*
119 * Usage:
120 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
121 *
122 * Creates, registers, and returns a (rpc) tcp based transporter.
123 * Once *xprt is initialized, it is registered as a transporter
124 * see (svc.h, xprt_register). This routine returns
125 * a NULL if a problem occurred.
126 *
127 * The filedescriptor passed in is expected to refer to a bound, but
128 * not yet connected socket.
129 *
130 * Since streams do buffered io similar to stdio, the caller can specify
131 * how big the send and receive buffers are via the second and third parms;
132 * 0 => use the system default.
133 */
134SVCXPRT *
135svc_vc_create(fd, sendsize, recvsize)
136 int fd;
137 u_int sendsize;
138 u_int recvsize;
139{
140 SVCXPRT *xprt;
141 struct cf_rendezvous *r = NULL;
142 struct __rpc_sockinfo si;
143 struct sockaddr_storage sslocal;
144 socklen_t slen;
145
146 r = mem_alloc(sizeof(*r));
147 if (r == NULL) {
148 warnx("svc_vc_create: out of memory");
149 goto cleanup_svc_vc_create;
150 }
151 if (!__rpc_fd2sockinfo(fd, &si))
152 return NULL;
153 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
154 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
155 r->maxrec = __svc_maxrec;
142 xprt = mem_alloc(sizeof(SVCXPRT));
143 if (xprt == NULL) {
144 warnx("svc_vc_create: out of memory");
145 goto cleanup_svc_vc_create;
146 }
147 xprt->xp_tp = NULL;
148 xprt->xp_p1 = r;
149 xprt->xp_p2 = NULL;
150 xprt->xp_p3 = NULL;
151 xprt->xp_verf = _null_auth;
152 svc_vc_rendezvous_ops(xprt);
153 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
154 xprt->xp_fd = fd;
155
156 slen = sizeof (struct sockaddr_storage);
157 if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
158 warnx("svc_vc_create: could not retrieve local addr");
159 goto cleanup_svc_vc_create;
160 }
161
162 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
163 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
164 if (xprt->xp_ltaddr.buf == NULL) {
165 warnx("svc_vc_create: no mem for local addr");
166 goto cleanup_svc_vc_create;
167 }
168 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
169
170 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
171 xprt_register(xprt);
172 return (xprt);
173cleanup_svc_vc_create:
174 if (r != NULL)
175 mem_free(r, sizeof(*r));
176 return (NULL);
177}
178
179/*
180 * Like svtcp_create(), except the routine takes any *open* UNIX file
181 * descriptor as its first input.
182 */
183SVCXPRT *
184svc_fd_create(fd, sendsize, recvsize)
185 int fd;
186 u_int sendsize;
187 u_int recvsize;
188{
189 struct sockaddr_storage ss;
190 socklen_t slen;
191 SVCXPRT *ret;
192
193 assert(fd != -1);
194
195 ret = makefd_xprt(fd, sendsize, recvsize);
196 if (ret == NULL)
197 return NULL;
198
199 slen = sizeof (struct sockaddr_storage);
200 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
201 warnx("svc_fd_create: could not retrieve local addr");
202 goto freedata;
203 }
204 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
205 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
206 if (ret->xp_ltaddr.buf == NULL) {
207 warnx("svc_fd_create: no mem for local addr");
208 goto freedata;
209 }
210 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
211
212 slen = sizeof (struct sockaddr_storage);
213 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
214 warnx("svc_fd_create: could not retrieve remote addr");
215 goto freedata;
216 }
217 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
218 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
219 if (ret->xp_rtaddr.buf == NULL) {
220 warnx("svc_fd_create: no mem for local addr");
221 goto freedata;
222 }
223 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
224#ifdef PORTMAP
225 if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
226 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
227 ret->xp_addrlen = sizeof (struct sockaddr_in);
228 }
229#endif /* PORTMAP */
230
231 return ret;
232
233freedata:
234 if (ret->xp_ltaddr.buf != NULL)
235 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
236
237 return NULL;
238}
239
240static SVCXPRT *
241makefd_xprt(fd, sendsize, recvsize)
242 int fd;
243 u_int sendsize;
244 u_int recvsize;
245{
246 SVCXPRT *xprt;
247 struct cf_conn *cd;
248 const char *netid;
249 struct __rpc_sockinfo si;
250
251 assert(fd != -1);
252
253 xprt = mem_alloc(sizeof(SVCXPRT));
254 if (xprt == NULL) {
255 warnx("svc_vc: makefd_xprt: out of memory");
256 goto done;
257 }
258 memset(xprt, 0, sizeof *xprt);
259 cd = mem_alloc(sizeof(struct cf_conn));
260 if (cd == NULL) {
261 warnx("svc_tcp: makefd_xprt: out of memory");
262 mem_free(xprt, sizeof(SVCXPRT));
263 xprt = NULL;
264 goto done;
265 }
266 cd->strm_stat = XPRT_IDLE;
267 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
268 xprt, read_vc, write_vc);
269 xprt->xp_p1 = cd;
270 xprt->xp_verf.oa_base = cd->verf_body;
271 svc_vc_ops(xprt); /* truely deals with calls */
272 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
273 xprt->xp_fd = fd;
274 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
275 xprt->xp_netid = strdup(netid);
276
277 xprt_register(xprt);
278done:
279 return (xprt);
280}
281
282/*ARGSUSED*/
283static bool_t
284rendezvous_request(xprt, msg)
285 SVCXPRT *xprt;
286 struct rpc_msg *msg;
287{
156 xprt = mem_alloc(sizeof(SVCXPRT));
157 if (xprt == NULL) {
158 warnx("svc_vc_create: out of memory");
159 goto cleanup_svc_vc_create;
160 }
161 xprt->xp_tp = NULL;
162 xprt->xp_p1 = r;
163 xprt->xp_p2 = NULL;
164 xprt->xp_p3 = NULL;
165 xprt->xp_verf = _null_auth;
166 svc_vc_rendezvous_ops(xprt);
167 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
168 xprt->xp_fd = fd;
169
170 slen = sizeof (struct sockaddr_storage);
171 if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
172 warnx("svc_vc_create: could not retrieve local addr");
173 goto cleanup_svc_vc_create;
174 }
175
176 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
177 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
178 if (xprt->xp_ltaddr.buf == NULL) {
179 warnx("svc_vc_create: no mem for local addr");
180 goto cleanup_svc_vc_create;
181 }
182 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
183
184 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
185 xprt_register(xprt);
186 return (xprt);
187cleanup_svc_vc_create:
188 if (r != NULL)
189 mem_free(r, sizeof(*r));
190 return (NULL);
191}
192
193/*
194 * Like svtcp_create(), except the routine takes any *open* UNIX file
195 * descriptor as its first input.
196 */
197SVCXPRT *
198svc_fd_create(fd, sendsize, recvsize)
199 int fd;
200 u_int sendsize;
201 u_int recvsize;
202{
203 struct sockaddr_storage ss;
204 socklen_t slen;
205 SVCXPRT *ret;
206
207 assert(fd != -1);
208
209 ret = makefd_xprt(fd, sendsize, recvsize);
210 if (ret == NULL)
211 return NULL;
212
213 slen = sizeof (struct sockaddr_storage);
214 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
215 warnx("svc_fd_create: could not retrieve local addr");
216 goto freedata;
217 }
218 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
219 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
220 if (ret->xp_ltaddr.buf == NULL) {
221 warnx("svc_fd_create: no mem for local addr");
222 goto freedata;
223 }
224 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
225
226 slen = sizeof (struct sockaddr_storage);
227 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
228 warnx("svc_fd_create: could not retrieve remote addr");
229 goto freedata;
230 }
231 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
232 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
233 if (ret->xp_rtaddr.buf == NULL) {
234 warnx("svc_fd_create: no mem for local addr");
235 goto freedata;
236 }
237 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
238#ifdef PORTMAP
239 if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
240 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
241 ret->xp_addrlen = sizeof (struct sockaddr_in);
242 }
243#endif /* PORTMAP */
244
245 return ret;
246
247freedata:
248 if (ret->xp_ltaddr.buf != NULL)
249 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
250
251 return NULL;
252}
253
254static SVCXPRT *
255makefd_xprt(fd, sendsize, recvsize)
256 int fd;
257 u_int sendsize;
258 u_int recvsize;
259{
260 SVCXPRT *xprt;
261 struct cf_conn *cd;
262 const char *netid;
263 struct __rpc_sockinfo si;
264
265 assert(fd != -1);
266
267 xprt = mem_alloc(sizeof(SVCXPRT));
268 if (xprt == NULL) {
269 warnx("svc_vc: makefd_xprt: out of memory");
270 goto done;
271 }
272 memset(xprt, 0, sizeof *xprt);
273 cd = mem_alloc(sizeof(struct cf_conn));
274 if (cd == NULL) {
275 warnx("svc_tcp: makefd_xprt: out of memory");
276 mem_free(xprt, sizeof(SVCXPRT));
277 xprt = NULL;
278 goto done;
279 }
280 cd->strm_stat = XPRT_IDLE;
281 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
282 xprt, read_vc, write_vc);
283 xprt->xp_p1 = cd;
284 xprt->xp_verf.oa_base = cd->verf_body;
285 svc_vc_ops(xprt); /* truely deals with calls */
286 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
287 xprt->xp_fd = fd;
288 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
289 xprt->xp_netid = strdup(netid);
290
291 xprt_register(xprt);
292done:
293 return (xprt);
294}
295
296/*ARGSUSED*/
297static bool_t
298rendezvous_request(xprt, msg)
299 SVCXPRT *xprt;
300 struct rpc_msg *msg;
301{
288 int sock;
302 int sock, flags;
289 struct cf_rendezvous *r;
303 struct cf_rendezvous *r;
304 struct cf_conn *cd;
290 struct sockaddr_storage addr;
291 socklen_t len;
292 struct __rpc_sockinfo si;
305 struct sockaddr_storage addr;
306 socklen_t len;
307 struct __rpc_sockinfo si;
308 SVCXPRT *newxprt;
309 fd_set cleanfds;
293
294 assert(xprt != NULL);
295 assert(msg != NULL);
296
297 r = (struct cf_rendezvous *)xprt->xp_p1;
298again:
299 len = sizeof addr;
300 if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
301 &len)) < 0) {
302 if (errno == EINTR)
303 goto again;
310
311 assert(xprt != NULL);
312 assert(msg != NULL);
313
314 r = (struct cf_rendezvous *)xprt->xp_p1;
315again:
316 len = sizeof addr;
317 if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
318 &len)) < 0) {
319 if (errno == EINTR)
320 goto again;
304 return (FALSE);
321 /*
322 * Clean out the most idle file descriptor when we're
323 * running out.
324 */
325 if (errno == EMFILE || errno == ENFILE) {
326 cleanfds = svc_fdset;
327 __svc_clean_idle(&cleanfds, 0, FALSE);
328 goto again;
329 }
330 return (FALSE);
305 }
306 /*
307 * make a new transporter (re-uses xprt)
308 */
331 }
332 /*
333 * make a new transporter (re-uses xprt)
334 */
309 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
310 xprt->xp_rtaddr.buf = mem_alloc(len);
311 if (xprt->xp_rtaddr.buf == NULL)
335 newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
336 newxprt->xp_rtaddr.buf = mem_alloc(len);
337 if (newxprt->xp_rtaddr.buf == NULL)
312 return (FALSE);
338 return (FALSE);
313 memcpy(xprt->xp_rtaddr.buf, &addr, len);
314 xprt->xp_rtaddr.len = len;
339 memcpy(newxprt->xp_rtaddr.buf, &addr, len);
340 newxprt->xp_rtaddr.len = len;
315#ifdef PORTMAP
316 if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
341#ifdef PORTMAP
342 if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
317 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
318 xprt->xp_addrlen = sizeof (struct sockaddr_in);
343 newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
344 newxprt->xp_addrlen = sizeof (struct sockaddr_in);
319 }
320#endif /* PORTMAP */
321 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
322 len = 1;
323 /* XXX fvdl - is this useful? */
324 _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
325 }
345 }
346#endif /* PORTMAP */
347 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
348 len = 1;
349 /* XXX fvdl - is this useful? */
350 _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
351 }
352
353 cd = (struct cf_conn *)newxprt->xp_p1;
354
355 cd->recvsize = r->recvsize;
356 cd->sendsize = r->sendsize;
357 cd->maxrec = r->maxrec;
358
359 if (cd->maxrec != 0) {
360 flags = fcntl(sock, F_GETFL, 0);
361 if (flags == -1)
362 return (FALSE);
363 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
364 return (FALSE);
365 if (cd->recvsize > cd->maxrec)
366 cd->recvsize = cd->maxrec;
367 cd->nonblock = TRUE;
368 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
369 } else
370 cd->nonblock = FALSE;
371
372 gettimeofday(&cd->last_recv_time, NULL);
373
326 return (FALSE); /* there is never an rpc msg to be processed */
327}
328
329/*ARGSUSED*/
330static enum xprt_stat
331rendezvous_stat(xprt)
332 SVCXPRT *xprt;
333{
334
335 return (XPRT_IDLE);
336}
337
338static void
339svc_vc_destroy(xprt)
340 SVCXPRT *xprt;
341{
374 return (FALSE); /* there is never an rpc msg to be processed */
375}
376
377/*ARGSUSED*/
378static enum xprt_stat
379rendezvous_stat(xprt)
380 SVCXPRT *xprt;
381{
382
383 return (XPRT_IDLE);
384}
385
386static void
387svc_vc_destroy(xprt)
388 SVCXPRT *xprt;
389{
390 assert(xprt != NULL);
391
392 xprt_unregister(xprt);
393 __svc_vc_dodestroy(xprt);
394}
395
396static void
397__svc_vc_dodestroy(xprt)
398 SVCXPRT *xprt;
399{
342 struct cf_conn *cd;
343 struct cf_rendezvous *r;
344
400 struct cf_conn *cd;
401 struct cf_rendezvous *r;
402
345 assert(xprt != NULL);
346
347 cd = (struct cf_conn *)xprt->xp_p1;
348
403 cd = (struct cf_conn *)xprt->xp_p1;
404
349 xprt_unregister(xprt);
350 if (xprt->xp_fd != RPC_ANYFD)
351 (void)_close(xprt->xp_fd);
352 if (xprt->xp_port != 0) {
353 /* a rendezvouser socket */
354 r = (struct cf_rendezvous *)xprt->xp_p1;
355 mem_free(r, sizeof (struct cf_rendezvous));
356 xprt->xp_port = 0;
357 } else {
358 /* an actual connection socket */
359 XDR_DESTROY(&(cd->xdrs));
360 mem_free(cd, sizeof(struct cf_conn));
361 }
362 if (xprt->xp_rtaddr.buf)
363 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
364 if (xprt->xp_ltaddr.buf)
365 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
366 if (xprt->xp_tp)
367 free(xprt->xp_tp);
368 if (xprt->xp_netid)
369 free(xprt->xp_netid);
370 mem_free(xprt, sizeof(SVCXPRT));
371}
372
373/*ARGSUSED*/
374static bool_t
375svc_vc_control(xprt, rq, in)
376 SVCXPRT *xprt;
377 const u_int rq;
378 void *in;
379{
380 return (FALSE);
381}
382
405 if (xprt->xp_fd != RPC_ANYFD)
406 (void)_close(xprt->xp_fd);
407 if (xprt->xp_port != 0) {
408 /* a rendezvouser socket */
409 r = (struct cf_rendezvous *)xprt->xp_p1;
410 mem_free(r, sizeof (struct cf_rendezvous));
411 xprt->xp_port = 0;
412 } else {
413 /* an actual connection socket */
414 XDR_DESTROY(&(cd->xdrs));
415 mem_free(cd, sizeof(struct cf_conn));
416 }
417 if (xprt->xp_rtaddr.buf)
418 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
419 if (xprt->xp_ltaddr.buf)
420 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
421 if (xprt->xp_tp)
422 free(xprt->xp_tp);
423 if (xprt->xp_netid)
424 free(xprt->xp_netid);
425 mem_free(xprt, sizeof(SVCXPRT));
426}
427
428/*ARGSUSED*/
429static bool_t
430svc_vc_control(xprt, rq, in)
431 SVCXPRT *xprt;
432 const u_int rq;
433 void *in;
434{
435 return (FALSE);
436}
437
438static bool_t
439svc_vc_rendezvous_control(xprt, rq, in)
440 SVCXPRT *xprt;
441 const u_int rq;
442 void *in;
443{
444 struct cf_rendezvous *cfp;
445
446 cfp = (struct cf_rendezvous *)xprt->xp_p1;
447 if (cfp == NULL)
448 return (FALSE);
449 switch (rq) {
450 case SVCGET_CONNMAXREC:
451 *(int *)in = cfp->maxrec;
452 break;
453 case SVCSET_CONNMAXREC:
454 cfp->maxrec = *(int *)in;
455 break;
456 default:
457 return (FALSE);
458 }
459 return (TRUE);
460}
461
383/*
384 * reads data from the tcp or uip connection.
385 * any error is fatal and the connection is closed.
386 * (And a read of zero bytes is a half closed stream => error.)
387 * All read operations timeout after 35 seconds. A timeout is
388 * fatal for the connection.
389 */
390static int
391read_vc(xprtp, buf, len)
392 void *xprtp;
393 void *buf;
394 int len;
395{
396 SVCXPRT *xprt;
397 int sock;
398 int milliseconds = 35 * 1000;
399 struct pollfd pollfd;
400 struct sockaddr *sa;
401 struct cmessage *cm;
462/*
463 * reads data from the tcp or uip connection.
464 * any error is fatal and the connection is closed.
465 * (And a read of zero bytes is a half closed stream => error.)
466 * All read operations timeout after 35 seconds. A timeout is
467 * fatal for the connection.
468 */
469static int
470read_vc(xprtp, buf, len)
471 void *xprtp;
472 void *buf;
473 int len;
474{
475 SVCXPRT *xprt;
476 int sock;
477 int milliseconds = 35 * 1000;
478 struct pollfd pollfd;
479 struct sockaddr *sa;
480 struct cmessage *cm;
481 struct cf_conn *cfp;
402
403 xprt = (SVCXPRT *)xprtp;
404 assert(xprt != NULL);
405
406 sock = xprt->xp_fd;
407
482
483 xprt = (SVCXPRT *)xprtp;
484 assert(xprt != NULL);
485
486 sock = xprt->xp_fd;
487
488 cfp = (struct cf_conn *)xprt->xp_p1;
489
490 if (cfp->nonblock) {
491 len = read(sock, buf, (size_t)len);
492 if (len < 0) {
493 if (errno == EAGAIN)
494 len = 0;
495 else
496 goto fatal_err;
497 }
498 if (len != 0)
499 gettimeofday(&cfp->last_recv_time, NULL);
500 return len;
501 }
502
408 do {
409 pollfd.fd = sock;
410 pollfd.events = POLLIN;
411 pollfd.revents = 0;
412 switch (_poll(&pollfd, 1, milliseconds)) {
413 case -1:
414 if (errno == EINTR)
415 continue;
416 /*FALLTHROUGH*/
417 case 0:
418 goto fatal_err;
419
420 default:
421 break;
422 }
423 } while ((pollfd.revents & POLLIN) == 0);
424
425 cm = NULL;
426 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
427 if (sa->sa_family == AF_LOCAL) {
428 cm = (struct cmessage *)xprt->xp_verf.oa_base;
429 if ((len = __msgread_withcred(sock, buf, len, cm)) > 0) {
430 xprt->xp_p2 = &cm->cmcred;
431 return (len);
432 } else
433 goto fatal_err;
434 } else {
503 do {
504 pollfd.fd = sock;
505 pollfd.events = POLLIN;
506 pollfd.revents = 0;
507 switch (_poll(&pollfd, 1, milliseconds)) {
508 case -1:
509 if (errno == EINTR)
510 continue;
511 /*FALLTHROUGH*/
512 case 0:
513 goto fatal_err;
514
515 default:
516 break;
517 }
518 } while ((pollfd.revents & POLLIN) == 0);
519
520 cm = NULL;
521 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
522 if (sa->sa_family == AF_LOCAL) {
523 cm = (struct cmessage *)xprt->xp_verf.oa_base;
524 if ((len = __msgread_withcred(sock, buf, len, cm)) > 0) {
525 xprt->xp_p2 = &cm->cmcred;
526 return (len);
527 } else
528 goto fatal_err;
529 } else {
435 if ((len = _read(sock, buf, (size_t)len)) > 0)
530 if ((len = read(sock, buf, (size_t)len)) > 0) {
531 gettimeofday(&cfp->last_recv_time, NULL);
436 return (len);
532 return (len);
533 }
437 }
438
439fatal_err:
440 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
441 return (-1);
442}
443
444/*
445 * writes data to the tcp connection.
446 * Any error is fatal and the connection is closed.
447 */
448static int
449write_vc(xprtp, buf, len)
450 void *xprtp;
451 void *buf;
452 int len;
453{
454 SVCXPRT *xprt;
455 int i, cnt;
456 struct sockaddr *sa;
534 }
535
536fatal_err:
537 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
538 return (-1);
539}
540
541/*
542 * writes data to the tcp connection.
543 * Any error is fatal and the connection is closed.
544 */
545static int
546write_vc(xprtp, buf, len)
547 void *xprtp;
548 void *buf;
549 int len;
550{
551 SVCXPRT *xprt;
552 int i, cnt;
553 struct sockaddr *sa;
554 struct cf_conn *cd;
555 struct timeval tv0, tv1;
457
458 xprt = (SVCXPRT *)xprtp;
459 assert(xprt != NULL);
556
557 xprt = (SVCXPRT *)xprtp;
558 assert(xprt != NULL);
559
560 cd = (struct cf_conn *)xprt->xp_p1;
561
562 if (cd->nonblock)
563 gettimeofday(&tv0, NULL);
460
461 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
564
565 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
462 if (sa->sa_family == AF_LOCAL) {
463 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
464 if ((i = __msgwrite(xprt->xp_fd, buf,
465 (size_t)cnt)) < 0) {
466 ((struct cf_conn *)(xprt->xp_p1))->strm_stat =
467 XPRT_DIED;
566 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
567 if (sa->sa_family == AF_LOCAL)
568 i = __msgwrite(xprt->xp_fd, buf, (size_t)cnt);
569 else
570 i = _write(xprt->xp_fd, buf, (size_t)cnt);
571 if (i < 0) {
572 if (errno != EAGAIN || !cd->nonblock) {
573 cd->strm_stat = XPRT_DIED;
468 return (-1);
469 }
574 return (-1);
575 }
470 }
471 } else {
472 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
473 if ((i = _write(xprt->xp_fd, buf,
474 (size_t)cnt)) < 0) {
475 ((struct cf_conn *)(xprt->xp_p1))->strm_stat =
476 XPRT_DIED;
477 return (-1);
576 if (cd->nonblock && i != cnt) {
577 /*
578 * For non-blocking connections, do not
579 * take more than 2 seconds writing the
580 * data out.
581 *
582 * XXX 2 is an arbitrary amount.
583 */
584 gettimeofday(&tv1, NULL);
585 if (tv1.tv_sec - tv0.tv_sec >= 2) {
586 cd->strm_stat = XPRT_DIED;
587 return (-1);
588 }
478 }
479 }
480 }
481
482 return (len);
483}
484
485static enum xprt_stat
486svc_vc_stat(xprt)
487 SVCXPRT *xprt;
488{
489 struct cf_conn *cd;
490
491 assert(xprt != NULL);
492
493 cd = (struct cf_conn *)(xprt->xp_p1);
494
495 if (cd->strm_stat == XPRT_DIED)
496 return (XPRT_DIED);
497 if (! xdrrec_eof(&(cd->xdrs)))
498 return (XPRT_MOREREQS);
499 return (XPRT_IDLE);
500}
501
502static bool_t
503svc_vc_recv(xprt, msg)
504 SVCXPRT *xprt;
505 struct rpc_msg *msg;
506{
507 struct cf_conn *cd;
508 XDR *xdrs;
509
510 assert(xprt != NULL);
511 assert(msg != NULL);
512
513 cd = (struct cf_conn *)(xprt->xp_p1);
514 xdrs = &(cd->xdrs);
515
589 }
590 }
591 }
592
593 return (len);
594}
595
596static enum xprt_stat
597svc_vc_stat(xprt)
598 SVCXPRT *xprt;
599{
600 struct cf_conn *cd;
601
602 assert(xprt != NULL);
603
604 cd = (struct cf_conn *)(xprt->xp_p1);
605
606 if (cd->strm_stat == XPRT_DIED)
607 return (XPRT_DIED);
608 if (! xdrrec_eof(&(cd->xdrs)))
609 return (XPRT_MOREREQS);
610 return (XPRT_IDLE);
611}
612
613static bool_t
614svc_vc_recv(xprt, msg)
615 SVCXPRT *xprt;
616 struct rpc_msg *msg;
617{
618 struct cf_conn *cd;
619 XDR *xdrs;
620
621 assert(xprt != NULL);
622 assert(msg != NULL);
623
624 cd = (struct cf_conn *)(xprt->xp_p1);
625 xdrs = &(cd->xdrs);
626
627 if (cd->nonblock) {
628 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
629 return FALSE;
630 }
631
516 xdrs->x_op = XDR_DECODE;
517 (void)xdrrec_skiprecord(xdrs);
518 if (xdr_callmsg(xdrs, msg)) {
519 cd->x_id = msg->rm_xid;
520 return (TRUE);
521 }
522 cd->strm_stat = XPRT_DIED;
523 return (FALSE);
524}
525
526static bool_t
527svc_vc_getargs(xprt, xdr_args, args_ptr)
528 SVCXPRT *xprt;
529 xdrproc_t xdr_args;
530 void *args_ptr;
531{
532
533 assert(xprt != NULL);
534 /* args_ptr may be NULL */
535 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
536 args_ptr));
537}
538
539static bool_t
540svc_vc_freeargs(xprt, xdr_args, args_ptr)
541 SVCXPRT *xprt;
542 xdrproc_t xdr_args;
543 void *args_ptr;
544{
545 XDR *xdrs;
546
547 assert(xprt != NULL);
548 /* args_ptr may be NULL */
549
550 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
551
552 xdrs->x_op = XDR_FREE;
553 return ((*xdr_args)(xdrs, args_ptr));
554}
555
556static bool_t
557svc_vc_reply(xprt, msg)
558 SVCXPRT *xprt;
559 struct rpc_msg *msg;
560{
561 struct cf_conn *cd;
562 XDR *xdrs;
632 xdrs->x_op = XDR_DECODE;
633 (void)xdrrec_skiprecord(xdrs);
634 if (xdr_callmsg(xdrs, msg)) {
635 cd->x_id = msg->rm_xid;
636 return (TRUE);
637 }
638 cd->strm_stat = XPRT_DIED;
639 return (FALSE);
640}
641
642static bool_t
643svc_vc_getargs(xprt, xdr_args, args_ptr)
644 SVCXPRT *xprt;
645 xdrproc_t xdr_args;
646 void *args_ptr;
647{
648
649 assert(xprt != NULL);
650 /* args_ptr may be NULL */
651 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
652 args_ptr));
653}
654
655static bool_t
656svc_vc_freeargs(xprt, xdr_args, args_ptr)
657 SVCXPRT *xprt;
658 xdrproc_t xdr_args;
659 void *args_ptr;
660{
661 XDR *xdrs;
662
663 assert(xprt != NULL);
664 /* args_ptr may be NULL */
665
666 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
667
668 xdrs->x_op = XDR_FREE;
669 return ((*xdr_args)(xdrs, args_ptr));
670}
671
672static bool_t
673svc_vc_reply(xprt, msg)
674 SVCXPRT *xprt;
675 struct rpc_msg *msg;
676{
677 struct cf_conn *cd;
678 XDR *xdrs;
563 bool_t stat;
679 bool_t rstat;
564
565 assert(xprt != NULL);
566 assert(msg != NULL);
567
568 cd = (struct cf_conn *)(xprt->xp_p1);
569 xdrs = &(cd->xdrs);
570
571 xdrs->x_op = XDR_ENCODE;
572 msg->rm_xid = cd->x_id;
680
681 assert(xprt != NULL);
682 assert(msg != NULL);
683
684 cd = (struct cf_conn *)(xprt->xp_p1);
685 xdrs = &(cd->xdrs);
686
687 xdrs->x_op = XDR_ENCODE;
688 msg->rm_xid = cd->x_id;
573 stat = xdr_replymsg(xdrs, msg);
689 rstat = xdr_replymsg(xdrs, msg);
574 (void)xdrrec_endofrecord(xdrs, TRUE);
690 (void)xdrrec_endofrecord(xdrs, TRUE);
575 return (stat);
691 return (rstat);
576}
577
578static void
579svc_vc_ops(xprt)
580 SVCXPRT *xprt;
581{
582 static struct xp_ops ops;
583 static struct xp_ops2 ops2;
584 extern mutex_t ops_lock;
585
586/* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
587
588 mutex_lock(&ops_lock);
589 if (ops.xp_recv == NULL) {
590 ops.xp_recv = svc_vc_recv;
591 ops.xp_stat = svc_vc_stat;
592 ops.xp_getargs = svc_vc_getargs;
593 ops.xp_reply = svc_vc_reply;
594 ops.xp_freeargs = svc_vc_freeargs;
595 ops.xp_destroy = svc_vc_destroy;
596 ops2.xp_control = svc_vc_control;
597 }
598 xprt->xp_ops = &ops;
599 xprt->xp_ops2 = &ops2;
600 mutex_unlock(&ops_lock);
601}
602
603static void
604svc_vc_rendezvous_ops(xprt)
605 SVCXPRT *xprt;
606{
607 static struct xp_ops ops;
608 static struct xp_ops2 ops2;
609 extern mutex_t ops_lock;
610
611 mutex_lock(&ops_lock);
612 if (ops.xp_recv == NULL) {
613 ops.xp_recv = rendezvous_request;
614 ops.xp_stat = rendezvous_stat;
615 ops.xp_getargs =
616 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
617 ops.xp_reply =
618 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
619 ops.xp_freeargs =
620 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
621 ops.xp_destroy = svc_vc_destroy;
692}
693
694static void
695svc_vc_ops(xprt)
696 SVCXPRT *xprt;
697{
698 static struct xp_ops ops;
699 static struct xp_ops2 ops2;
700 extern mutex_t ops_lock;
701
702/* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
703
704 mutex_lock(&ops_lock);
705 if (ops.xp_recv == NULL) {
706 ops.xp_recv = svc_vc_recv;
707 ops.xp_stat = svc_vc_stat;
708 ops.xp_getargs = svc_vc_getargs;
709 ops.xp_reply = svc_vc_reply;
710 ops.xp_freeargs = svc_vc_freeargs;
711 ops.xp_destroy = svc_vc_destroy;
712 ops2.xp_control = svc_vc_control;
713 }
714 xprt->xp_ops = &ops;
715 xprt->xp_ops2 = &ops2;
716 mutex_unlock(&ops_lock);
717}
718
719static void
720svc_vc_rendezvous_ops(xprt)
721 SVCXPRT *xprt;
722{
723 static struct xp_ops ops;
724 static struct xp_ops2 ops2;
725 extern mutex_t ops_lock;
726
727 mutex_lock(&ops_lock);
728 if (ops.xp_recv == NULL) {
729 ops.xp_recv = rendezvous_request;
730 ops.xp_stat = rendezvous_stat;
731 ops.xp_getargs =
732 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
733 ops.xp_reply =
734 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
735 ops.xp_freeargs =
736 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
737 ops.xp_destroy = svc_vc_destroy;
622 ops2.xp_control = svc_vc_control;
738 ops2.xp_control = svc_vc_rendezvous_control;
623 }
624 xprt->xp_ops = &ops;
625 xprt->xp_ops2 = &ops2;
626 mutex_unlock(&ops_lock);
627}
628
629int
630__msgread_withcred(sock, buf, cnt, cmp)
631 int sock;
632 void *buf;
633 size_t cnt;
634 struct cmessage *cmp;
635{
636 struct iovec iov[1];
637 struct msghdr msg;
638 union {
639 struct cmsghdr cmsg;
640 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
641 } cm;
642 int ret;
643
644
645 bzero(&cm, sizeof(cm));
646 iov[0].iov_base = buf;
647 iov[0].iov_len = cnt;
648
649 msg.msg_iov = iov;
650 msg.msg_iovlen = 1;
651 msg.msg_name = NULL;
652 msg.msg_namelen = 0;
653 msg.msg_control = &cm;
654 msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
655 msg.msg_flags = 0;
656
657 ret = _recvmsg(sock, &msg, 0);
658 bcopy(&cm.cmsg, &cmp->cmsg, sizeof(cmp->cmsg));
659 bcopy(CMSG_DATA(&cm), &cmp->cmcred, sizeof(cmp->cmcred));
660
661 if (msg.msg_controllen == 0 ||
662 (msg.msg_flags & MSG_CTRUNC) != 0)
663 return (-1);
664
665 return (ret);
666}
667
668static int
669__msgwrite(sock, buf, cnt)
670 int sock;
671 void *buf;
672 size_t cnt;
673{
674 struct iovec iov[1];
675 struct msghdr msg;
676 struct cmessage cm;
677
678 bzero((char *)&cm, sizeof(cm));
679 iov[0].iov_base = buf;
680 iov[0].iov_len = cnt;
681
682 cm.cmsg.cmsg_type = SCM_CREDS;
683 cm.cmsg.cmsg_level = SOL_SOCKET;
684 cm.cmsg.cmsg_len = sizeof(struct cmessage);
685
686 msg.msg_iov = iov;
687 msg.msg_iovlen = 1;
688 msg.msg_name = NULL;
689 msg.msg_namelen = 0;
690 msg.msg_control = &cm;
691 msg.msg_controllen = sizeof(struct cmessage);
692 msg.msg_flags = 0;
693
694 return(_sendmsg(sock, &msg, 0));
695}
696
697/*
698 * Get the effective UID of the sending process. Used by rpcbind and keyserv
699 * (AF_LOCAL).
700 */
701int
702__rpc_get_local_uid(SVCXPRT *transp, uid_t *uid)
703{
704 struct cmsgcred *cmcred;
705 struct cmessage *cm;
706 struct cmsghdr *cmp;
707
708 cm = (struct cmessage *)transp->xp_verf.oa_base;
709
710 if (cm == NULL)
711 return (-1);
712 cmp = &cm->cmsg;
713 if (cmp == NULL || cmp->cmsg_level != SOL_SOCKET ||
714 cmp->cmsg_type != SCM_CREDS)
715 return (-1);
716
717 cmcred = __svc_getcallercreds(transp);
718 if (cmcred == NULL)
719 return (-1);
720 *uid = cmcred->cmcred_euid;
721 return (0);
722}
739 }
740 xprt->xp_ops = &ops;
741 xprt->xp_ops2 = &ops2;
742 mutex_unlock(&ops_lock);
743}
744
745int
746__msgread_withcred(sock, buf, cnt, cmp)
747 int sock;
748 void *buf;
749 size_t cnt;
750 struct cmessage *cmp;
751{
752 struct iovec iov[1];
753 struct msghdr msg;
754 union {
755 struct cmsghdr cmsg;
756 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
757 } cm;
758 int ret;
759
760
761 bzero(&cm, sizeof(cm));
762 iov[0].iov_base = buf;
763 iov[0].iov_len = cnt;
764
765 msg.msg_iov = iov;
766 msg.msg_iovlen = 1;
767 msg.msg_name = NULL;
768 msg.msg_namelen = 0;
769 msg.msg_control = &cm;
770 msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
771 msg.msg_flags = 0;
772
773 ret = _recvmsg(sock, &msg, 0);
774 bcopy(&cm.cmsg, &cmp->cmsg, sizeof(cmp->cmsg));
775 bcopy(CMSG_DATA(&cm), &cmp->cmcred, sizeof(cmp->cmcred));
776
777 if (msg.msg_controllen == 0 ||
778 (msg.msg_flags & MSG_CTRUNC) != 0)
779 return (-1);
780
781 return (ret);
782}
783
784static int
785__msgwrite(sock, buf, cnt)
786 int sock;
787 void *buf;
788 size_t cnt;
789{
790 struct iovec iov[1];
791 struct msghdr msg;
792 struct cmessage cm;
793
794 bzero((char *)&cm, sizeof(cm));
795 iov[0].iov_base = buf;
796 iov[0].iov_len = cnt;
797
798 cm.cmsg.cmsg_type = SCM_CREDS;
799 cm.cmsg.cmsg_level = SOL_SOCKET;
800 cm.cmsg.cmsg_len = sizeof(struct cmessage);
801
802 msg.msg_iov = iov;
803 msg.msg_iovlen = 1;
804 msg.msg_name = NULL;
805 msg.msg_namelen = 0;
806 msg.msg_control = &cm;
807 msg.msg_controllen = sizeof(struct cmessage);
808 msg.msg_flags = 0;
809
810 return(_sendmsg(sock, &msg, 0));
811}
812
813/*
814 * Get the effective UID of the sending process. Used by rpcbind and keyserv
815 * (AF_LOCAL).
816 */
817int
818__rpc_get_local_uid(SVCXPRT *transp, uid_t *uid)
819{
820 struct cmsgcred *cmcred;
821 struct cmessage *cm;
822 struct cmsghdr *cmp;
823
824 cm = (struct cmessage *)transp->xp_verf.oa_base;
825
826 if (cm == NULL)
827 return (-1);
828 cmp = &cm->cmsg;
829 if (cmp == NULL || cmp->cmsg_level != SOL_SOCKET ||
830 cmp->cmsg_type != SCM_CREDS)
831 return (-1);
832
833 cmcred = __svc_getcallercreds(transp);
834 if (cmcred == NULL)
835 return (-1);
836 *uid = cmcred->cmcred_euid;
837 return (0);
838}
839
840/*
841 * Destroy xprts that have not have had any activity in 'timeout' seconds.
842 * If 'cleanblock' is true, blocking connections (the default) are also
843 * cleaned. If timeout is 0, the least active connection is picked.
844 */
845bool_t
846__svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
847{
848 int i, ncleaned;
849 SVCXPRT *xprt, *least_active;
850 struct timeval tv, tdiff, tmax;
851 struct cf_conn *cd;
852
853 gettimeofday(&tv, NULL);
854 tmax.tv_sec = tmax.tv_usec = 0;
855 least_active = NULL;
856 rwlock_wrlock(&svc_fd_lock);
857 for (i = ncleaned = 0; i <= svc_maxfd; i++) {
858 if (FD_ISSET(i, fds)) {
859 xprt = __svc_xports[i];
860 if (xprt == NULL || xprt->xp_ops == NULL ||
861 xprt->xp_ops->xp_recv != svc_vc_recv)
862 continue;
863 cd = (struct cf_conn *)xprt->xp_p1;
864 if (!cleanblock && !cd->nonblock)
865 continue;
866 if (timeout == 0) {
867 timersub(&tv, &cd->last_recv_time, &tdiff);
868 if (timercmp(&tdiff, &tmax, >)) {
869 tmax = tdiff;
870 least_active = xprt;
871 }
872 continue;
873 }
874 if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
875 __xprt_unregister_unlocked(xprt);
876 __svc_vc_dodestroy(xprt);
877 ncleaned++;
878 }
879 }
880 }
881 if (timeout == 0 && least_active != NULL) {
882 __xprt_unregister_unlocked(least_active);
883 __svc_vc_dodestroy(least_active);
884 ncleaned++;
885 }
886 rwlock_unlock(&svc_fd_lock);
887 return ncleaned > 0 ? TRUE : FALSE;
888}