2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro
30 * @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/svc_tcp.c,v 1.18.2.3 2001/09/05 22:29:23 dec Exp $
32 * $DragonFly: src/lib/libc/rpc/svc_tcp.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
36 * svc_tcp.c, Server side for TCP/IP based RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * Actually implements two flavors of transporter -
41 * a tcp rendezvouser (a listner and connection establisher)
42 * and a record/tcp stream.
45 #include "namespace.h"
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
54 #include "un-namespace.h"
57 * Ops vector for TCP/IP based rpc service handle
59 static bool_t svctcp_recv();
60 static enum xprt_stat svctcp_stat();
61 static bool_t svctcp_getargs();
62 static bool_t svctcp_reply();
63 static bool_t svctcp_freeargs();
64 static void svctcp_destroy();
66 static struct xp_ops svctcp_op = {
76 * Ops vector for TCP/IP rendezvous handler
78 static bool_t rendezvous_request();
79 static enum xprt_stat rendezvous_stat();
81 static struct xp_ops svctcp_rendezvous_op = {
90 static int readtcp(), writetcp();
91 static SVCXPRT *makefd_xprt();
93 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
98 struct tcp_conn { /* kept in xprt->xp_p1 */
99 enum xprt_stat strm_stat;
102 char verf_body[MAX_AUTH_BYTES];
107 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
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.
114 * If sock<0 then a socket is created, else sock is used.
115 * If the socket, sock is not bound to a port then svctcp_create
116 * binds it to an arbitrary port. The routine then starts a tcp
117 * listener on the socket's associated port. In any (successful) case,
118 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
119 * associated port number.
121 * Since tcp streams do buffered io similar to stdio, the caller can specify
122 * how big the send and receive buffers are via the second and third parms;
123 * 0 => use the system default.
126 svctcp_create(int sock, u_int sendsize, u_int recvsize)
128 bool_t madesock = FALSE;
130 struct tcp_rendezvous *r;
131 struct sockaddr_in addr;
132 int len = sizeof(struct sockaddr_in);
135 if (sock == RPC_ANYSOCK) {
136 if ((sock = _socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
137 perror("svctcp_.c - tcp socket creation problem");
138 return ((SVCXPRT *)NULL);
143 if (_ioctl(sock, FIONBIO, &on) < 0) {
144 perror("svc_tcp.c - cannot turn on non-blocking mode");
147 return ((SVCXPRT *)NULL);
149 memset(&addr, 0, sizeof (addr));
150 addr.sin_len = sizeof(struct sockaddr_in);
151 addr.sin_family = AF_INET;
152 if (bindresvport(sock, &addr)) {
154 _bind(sock, (struct sockaddr *)&addr, len);
156 if ((_getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
157 (_listen(sock, 2) != 0)) {
158 perror("svctcp_.c - cannot getsockname or listen");
161 return ((SVCXPRT *)NULL);
163 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
165 fprintf(stderr, "svctcp_create: out of memory\n");
168 r->sendsize = sendsize;
169 r->recvsize = recvsize;
170 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
172 fprintf(stderr, "svctcp_create: out of memory\n");
176 xprt->xp_p1 = (caddr_t)r;
177 xprt->xp_verf = _null_auth;
178 xprt->xp_ops = &svctcp_rendezvous_op;
179 xprt->xp_port = ntohs(addr.sin_port);
180 xprt->xp_sock = sock;
186 * Like svtcp_create(), except the routine takes any *open* UNIX file
187 * descriptor as its first input.
190 svcfd_create(int fd, u_int sendsize, u_int recvsize)
193 return (makefd_xprt(fd, sendsize, recvsize));
197 makefd_xprt(int fd, u_int sendsize, u_int recvsize)
202 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
203 if (xprt == (SVCXPRT *)NULL) {
204 fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
207 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
208 if (cd == (struct tcp_conn *)NULL) {
209 fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
210 mem_free((char *) xprt, sizeof(SVCXPRT));
211 xprt = (SVCXPRT *)NULL;
214 cd->strm_stat = XPRT_IDLE;
215 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
216 (caddr_t)xprt, readtcp, writetcp);
218 xprt->xp_p1 = (caddr_t)cd;
219 xprt->xp_verf.oa_base = cd->verf_body;
220 xprt->xp_addrlen = 0;
221 xprt->xp_ops = &svctcp_op; /* truely deals with calls */
222 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
230 rendezvous_request(SVCXPRT *xprt)
233 struct tcp_rendezvous *r;
234 struct sockaddr_in addr;
238 r = (struct tcp_rendezvous *)xprt->xp_p1;
240 len = sizeof(struct sockaddr_in);
241 if ((sock = _accept(xprt->xp_sock, (struct sockaddr *)&addr,
248 * Guard against FTP bounce attacks.
250 if (addr.sin_port == htons(20)) {
255 * The listening socket is in FIONBIO mode and we inherit it.
258 if (_ioctl(sock, FIONBIO, &off) < 0) {
263 * make a new transporter (re-uses xprt)
265 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
266 xprt->xp_raddr = addr;
267 xprt->xp_addrlen = len;
268 return (FALSE); /* there is never an rpc msg to be processed */
271 static enum xprt_stat
272 rendezvous_stat(void)
279 svctcp_destroy(SVCXPRT *xprt)
281 struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
283 xprt_unregister(xprt);
284 _close(xprt->xp_sock);
285 if (xprt->xp_port != 0) {
286 /* a rendezvouser socket */
289 /* an actual connection socket */
290 XDR_DESTROY(&(cd->xdrs));
292 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
293 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
297 * All read operations timeout after 35 seconds.
298 * A timeout is fatal for the connection.
300 static struct timeval wait_per_try = { 35, 0 };
303 * reads data from the tcp conection.
304 * any error is fatal and the connection is closed.
305 * (And a read of zero bytes is a half closed stream => error.)
307 * Note: we have to be careful here not to allow ourselves to become
308 * blocked too long in this routine. While we're waiting for data from one
309 * client, another client may be trying to connect. To avoid this situation,
310 * some code from svc_run() is transplanted here: the _select() loop checks
311 * all RPC descriptors including the one we want and calls svc_getreqset2()
312 * to handle new requests if any are detected.
315 readtcp(SVCXPRT *xprt, caddr_t buf, int len)
317 int sock = xprt->xp_sock;
318 struct timeval start, delta, tv;
319 struct timeval tmp1, tmp2;
321 extern fd_set *__svc_fdset;
322 extern int __svc_fdsetsize;
324 delta = wait_per_try;
326 gettimeofday(&start, NULL);
328 int bytes = howmany(__svc_fdsetsize, NFDBITS) *
332 fds = (fd_set *)malloc(bytes);
335 memcpy(fds, __svc_fdset, bytes);
337 /* XXX we know the other bits are still clear */
339 tv = delta; /* in case _select() implements writeback */
340 switch (_select(svc_maxfd + 1, fds, NULL, NULL, &tv)) {
342 memset(fds, 0, bytes);
345 gettimeofday(&tmp1, NULL);
346 timersub(&tmp1, &start, &tmp2);
347 timersub(&wait_per_try, &tmp2, &tmp1);
348 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
355 if (!FD_ISSET(sock, fds)) {
356 svc_getreqset2(fds, svc_maxfd + 1);
357 gettimeofday(&tmp1, NULL);
358 timersub(&tmp1, &start, &tmp2);
359 timersub(&wait_per_try, &tmp2, &tmp1);
360 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
366 } while (!FD_ISSET(sock, fds));
367 if ((len = _read(sock, buf, len)) > 0) {
373 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
380 * writes data to the tcp connection.
381 * Any error is fatal and the connection is closed.
384 writetcp(SVCXPRT *xprt, caddr_t buf, int len)
388 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
389 if ((i = _write(xprt->xp_sock, buf, cnt)) < 0) {
390 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
398 static enum xprt_stat
399 svctcp_stat(SVCXPRT *xprt)
401 struct tcp_conn *cd =
402 (struct tcp_conn *)(xprt->xp_p1);
404 if (cd->strm_stat == XPRT_DIED)
406 if (! xdrrec_eof(&(cd->xdrs)))
407 return (XPRT_MOREREQS);
412 svctcp_recv(SVCXPRT *xprt, struct rpc_msg *msg)
414 struct tcp_conn *cd =
415 (struct tcp_conn *)(xprt->xp_p1);
416 XDR *xdrs = &(cd->xdrs);
418 xdrs->x_op = XDR_DECODE;
419 xdrrec_skiprecord(xdrs);
420 if (xdr_callmsg(xdrs, msg)) {
421 cd->x_id = msg->rm_xid;
424 cd->strm_stat = XPRT_DIED; /* XXXX */
429 svctcp_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
432 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
436 svctcp_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
439 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
441 xdrs->x_op = XDR_FREE;
442 return ((*xdr_args)(xdrs, args_ptr));
446 svctcp_reply(SVCXPRT *xprt, struct rpc_msg *msg)
448 struct tcp_conn *cd =
449 (struct tcp_conn *)(xprt->xp_p1);
450 XDR *xdrs = &(cd->xdrs);
453 xdrs->x_op = XDR_ENCODE;
454 msg->rm_xid = cd->x_id;
455 stat = xdr_replymsg(xdrs, msg);
456 xdrrec_endofrecord(xdrs, TRUE);