2 static char sfs_svc_tcp_id[] = "@(#)svc_tcp.c 2.1 97/10/23";
4 /* @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC */
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
12 * This product contains benchmarks acquired from several sources who
13 * understand and agree with SPEC's goal of creating fair and objective
14 * benchmarks to measure computer performance.
16 * This copyright notice is placed here only to protect SPEC in the
17 * event the source is misused in any manner that is contrary to the
18 * spirit, the goals and the intent of SPEC.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
24 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25 * unrestricted use provided that this legend is included on all tape
26 * media and as a part of the software program in whole or part. Users
27 * may copy or modify Sun RPC without charge, but are not authorized
28 * to license or distribute it to anyone else except as part of a product or
29 * program developed by the user.
31 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 * Sun RPC is provided with no support and without any obligation on the
36 * part of Sun Microsystems, Inc. to assist in its use, correction,
37 * modification or enhancement.
39 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41 * OR ANY PART THEREOF.
43 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44 * or profits or other special, indirect and consequential damages, even if
45 * Sun has been advised of the possibility of such damages.
47 * Sun Microsystems, Inc.
49 * Mountain View, California 94043
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
56 * svc_tcp.c, Server side for TCP/IP based RPC.
58 * Copyright (C) 1984, Sun Microsystems, Inc.
60 * Actually implements two flavors of transporter -
61 * a tcp rendezvouser (a listner and connection establisher)
62 * and a record/tcp stream.
70 #include "rpc/osdep.h"
74 * Ops vector for TCP/IP based rpc service handle
76 static bool_t svctcp_recv(SVCXPRT *, struct rpc_msg *);
77 static enum xprt_stat svctcp_stat(SVCXPRT *);
78 static bool_t svctcp_getargs(SVCXPRT *, xdrproc_t, void *);
79 static bool_t svctcp_reply(SVCXPRT *, struct rpc_msg *);
80 static bool_t svctcp_freeargs(SVCXPRT *, xdrproc_t, void *);
81 static void svctcp_destroy(SVCXPRT *);
82 static bool_t svctcp_abortrop(SVCXPRT *, struct rpc_msg *);
83 static bool_t svctcp_abortgop(SVCXPRT *, xdrproc_t, void *);
85 static struct xp_ops svctcp_op = {
95 * Ops vector for TCP/IP rendezvous handler
97 static bool_t rendezvous_request(SVCXPRT *xprt, struct rpc_msg *);
98 static enum xprt_stat rendezvous_stat(SVCXPRT *);
100 static struct xp_ops svctcp_rendezvous_op = {
109 static int readtcp(), writetcp();
110 static int readtcp(SVCXPRT *, char *, int);
111 static int writetcp(SVCXPRT *, char *, int);
112 static SVCXPRT *makefd_xprt(int, uint_t, uint_t);
114 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
119 struct tcp_conn { /* kept in xprt->xp_p1 */
120 enum xprt_stat strm_stat;
123 char verf_body[MAX_AUTH_BYTES];
128 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
130 * Creates, registers, and returns a (rpc) tcp based transporter.
131 * Once *xprt is initialized, it is registered as a transporter
132 * see (svc.h, xprt_register). This routine returns
133 * a NULL if a problem occurred.
135 * If sock<0 then a socket is created, else sock is used.
136 * If the socket, sock is not bound to a port then svctcp_create
137 * binds it to an arbitrary port. The routine then starts a tcp
138 * listener on the socket's associated port. In any (successful) case,
139 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
140 * associated port number.
142 * Since tcp streams do buffered io similar to stdio, the caller can specify
143 * how big the send and receive buffers are via the second and third parms;
144 * 0 => use the system default.
152 bool_t madesock = FALSE;
154 struct tcp_rendezvous *r;
155 struct sockaddr_in addr;
162 len = sizeof(struct sockaddr_in);
164 if (sock == RPC_ANYSOCK) {
165 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
166 perror("svctcp_.c - udp socket creation problem");
167 return ((SVCXPRT *)NULL);
171 memset((char *)&addr, '\0', sizeof (addr));
172 addr.sin_family = AF_INET;
173 if (bindresvport(sock, &addr)) {
175 (void)bind(sock, (struct sockaddr *)&addr, len);
177 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
178 (listen(sock, 2) != 0)) {
179 perror("svctcp_.c - cannot getsockname or listen");
182 return ((SVCXPRT *)NULL);
184 r = (struct tcp_rendezvous *)mem_alloc(sizeof(struct tcp_rendezvous));
186 (void) fprintf(stderr, "svctcp_create: out of memory\n");
189 r->sendsize = sendsize;
190 r->recvsize = recvsize;
191 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
193 (void) fprintf(stderr, "svctcp_create: out of memory\n");
197 xprt->xp_p1 = (void *)r;
198 xprt->xp_verf = _null_auth;
199 xprt->xp_ops = &svctcp_rendezvous_op;
200 xprt->xp_port = ntohs(addr.sin_port);
201 xprt->xp_sock = sock;
207 * Like svtcp_create(), except the routine takes any *open* UNIX file
208 * descriptor as its first input.
217 return (makefd_xprt(fd, sendsize, recvsize));
229 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
230 if (xprt == (SVCXPRT *)NULL) {
231 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
234 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
235 if (cd == (struct tcp_conn *)NULL) {
236 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
237 mem_free((char *) xprt, sizeof(SVCXPRT));
238 xprt = (SVCXPRT *)NULL;
241 cd->strm_stat = XPRT_IDLE;
242 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
243 (void *)xprt, readtcp, writetcp);
245 xprt->xp_p1 = (void *)cd;
246 xprt->xp_verf.oa_base = cd->verf_body;
247 xprt->xp_addrlen = 0;
248 xprt->xp_ops = &svctcp_op; /* truely deals with calls */
249 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
263 struct tcp_rendezvous *r;
264 struct sockaddr_in addr;
271 r = (struct tcp_rendezvous *)xprt->xp_p1;
273 len = sizeof(struct sockaddr_in);
274 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
281 * make a new transporter (re-uses xprt)
283 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
284 xprt->xp_raddr = addr;
285 xprt->xp_addrlen = len;
286 return (FALSE); /* there is never an rpc msg to be processed */
290 static enum xprt_stat
291 rendezvous_stat(SVCXPRT *x)
301 struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
303 xprt_unregister(xprt);
304 (void)close(xprt->xp_sock);
305 if (xprt->xp_port != 0) {
306 /* a rendezvouser socket */
309 /* an actual connection socket */
310 XDR_DESTROY(&(cd->xdrs));
312 mem_free((void *)cd, sizeof(struct tcp_conn));
313 mem_free((void *)xprt, sizeof(SVCXPRT));
317 * All read operations timeout after 35 seconds.
318 * A timeout is fatal for the connection.
320 static struct timeval wait_per_try = { 35, 0 };
323 * reads data from the tcp conection.
324 * any error is fatal and the connection is closed.
325 * (And a read of zero bytes is a half closed stream => error.)
333 int sock = xprt->xp_sock;
341 int mask = 1 << sock;
343 #endif /* def FD_SETSIZE */
346 if (select(_rpc_dtablesize(), &readfds, NULL, NULL,
347 &wait_per_try) <= 0) {
348 if (errno == EINTR) {
354 } while (!FD_ISSET(sock, &readfds));
356 } while (readfds != mask);
357 #endif /* def FD_SETSIZE */
358 if ((len = read(sock, buf, len)) > 0) {
362 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
367 * writes data to the tcp connection.
368 * Any error is fatal and the connection is closed.
378 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
379 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
380 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
388 static enum xprt_stat
392 struct tcp_conn *cd =
393 (struct tcp_conn *)(xprt->xp_p1);
395 if (cd->strm_stat == XPRT_DIED)
397 if (! xdrrec_eof(&(cd->xdrs)))
398 return (XPRT_MOREREQS);
407 struct tcp_conn *cd =
408 (struct tcp_conn *)(xprt->xp_p1);
409 XDR *xdrs = &(cd->xdrs);
411 xdrs->x_op = XDR_DECODE;
412 (void)xdrrec_skiprecord(xdrs);
413 if (xdr_callmsg(xdrs, msg)) {
414 cd->x_id = msg->rm_xid;
427 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
437 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
439 xdrs->x_op = XDR_FREE;
440 return ((*xdr_args)(xdrs, args_ptr));
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 (void)xdrrec_endofrecord(xdrs, TRUE);