1 /*********************************************************************
2 * RPC for the Windows NT Operating System
3 * 1993 by Martin F. Gergeleit
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating
5 * System according to the Sun copyright below.
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10 *********************************************************************/
12 /* @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC */
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15 * unrestricted use provided that this legend is included on all tape
16 * media and as a part of the software program in whole or part. Users
17 * may copy or modify Sun RPC without charge, but are not authorized
18 * to license or distribute it to anyone else except as part of a product or
19 * program developed by the user.
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
25 * Sun RPC is provided with no support and without any obligation on the
26 * part of Sun Microsystems, Inc. to assist in its use, correction,
27 * modification or enhancement.
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31 * OR ANY PART THEREOF.
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34 * or profits or other special, indirect and consequential damages, even if
35 * Sun has been advised of the possibility of such damages.
37 * Sun Microsystems, Inc.
39 * Mountain View, California 94043
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
46 * svc_tcp.c, Server side for TCP/IP based RPC.
48 * Copyright (C) 1984, Sun Microsystems, Inc.
50 * Actually implements two flavors of transporter -
51 * a tcp rendezvouser (a listner and connection establisher)
52 * and a record/tcp stream.
59 #include <sys/socket.h>
65 extern bool_t abort();
69 * Ops vector for TCP/IP based rpc service handle
71 static bool_t svctcp_recv();
72 static enum xprt_stat svctcp_stat();
73 static bool_t svctcp_getargs();
74 static bool_t svctcp_reply();
75 static bool_t svctcp_freeargs();
76 static void svctcp_destroy();
78 static struct xp_ops svctcp_op = {
88 * Ops vector for TCP/IP rendezvous handler
90 static bool_t rendezvous_request();
91 static enum xprt_stat rendezvous_stat();
93 static struct xp_ops svctcp_rendezvous_op = {
108 static int readtcp(), writetcp();
109 static SVCXPRT *makefd_xprt();
111 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
116 struct tcp_conn { /* kept in xprt->xp_p1 */
117 enum xprt_stat strm_stat;
120 char verf_body[MAX_AUTH_BYTES];
125 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
127 * Creates, registers, and returns a (rpc) tcp based transporter.
128 * Once *xprt is initialized, it is registered as a transporter
129 * see (svc.h, xprt_register). This routine returns
130 * a NULL if a problem occurred.
132 * If sock<0 then a socket is created, else sock is used.
133 * If the socket, sock is not bound to a port then svctcp_create
134 * binds it to an arbitrary port. The routine then starts a tcp
135 * listener on the socket's associated port. In any (successful) case,
136 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
137 * associated port number.
139 * Since tcp streams do buffered io similar to stdio, the caller can specify
140 * how big the send and receive buffers are via the second and third parms;
141 * 0 => use the system default.
144 svctcp_create(sock, sendsize, recvsize)
149 bool_t madesock = FALSE;
150 register SVCXPRT *xprt;
151 register struct tcp_rendezvous *r;
152 struct sockaddr_in addr;
153 int len = sizeof(struct sockaddr_in);
155 if (sock == RPC_ANYSOCK) {
157 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
159 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
161 perror("svctcp_.c - udp socket creation problem");
162 return ((SVCXPRT *)NULL);
166 bzero((char *)&addr, sizeof (addr));
167 addr.sin_family = AF_INET;
168 if (bindresvport(sock, &addr)) {
170 (void)bind(sock, (struct sockaddr *)&addr, len);
172 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
173 (listen(sock, 2) != 0)) {
174 perror("svctcp_.c - cannot getsockname or listen");
177 (void)closesocket(sock);
181 return ((SVCXPRT *)NULL);
183 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
186 nt_rpc_report("svctcp_create: out of memory\n");
188 (void) fprintf(stderr, "svctcp_create: out of memory\n");
192 r->sendsize = sendsize;
193 r->recvsize = recvsize;
194 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
197 nt_rpc_report("svctcp_create: out of memory\n");
199 (void) fprintf(stderr, "svctcp_create: out of memory\n");
204 xprt->xp_p1 = (caddr_t)r;
205 xprt->xp_verf = _null_auth;
206 xprt->xp_ops = &svctcp_rendezvous_op;
207 xprt->xp_port = ntohs(addr.sin_port);
208 xprt->xp_sock = sock;
214 * Like svtcp_create(), except the routine takes any *open* UNIX file
215 * descriptor as its first input.
218 svcfd_create(fd, sendsize, recvsize)
224 return (makefd_xprt(fd, sendsize, recvsize));
228 makefd_xprt(fd, sendsize, recvsize)
233 register SVCXPRT *xprt;
234 register struct tcp_conn *cd;
236 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
237 if (xprt == (SVCXPRT *)NULL) {
239 nt_rpc_report("svc_tcp: makefd_xprt: out of memory\n");
241 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
245 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
246 if (cd == (struct tcp_conn *)NULL) {
248 nt_rpc_report("svc_tcp: makefd_xprt: out of memory\n");
250 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
252 mem_free((char *) xprt, sizeof(SVCXPRT));
253 xprt = (SVCXPRT *)NULL;
256 cd->strm_stat = XPRT_IDLE;
257 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
258 (caddr_t)xprt, readtcp, writetcp);
260 xprt->xp_p1 = (caddr_t)cd;
261 xprt->xp_verf.oa_base = cd->verf_body;
262 xprt->xp_addrlen = 0;
263 xprt->xp_ops = &svctcp_op; /* truely deals with calls */
264 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
272 rendezvous_request(xprt)
273 register SVCXPRT *xprt;
276 struct tcp_rendezvous *r;
277 struct sockaddr_in addr;
280 r = (struct tcp_rendezvous *)xprt->xp_p1;
282 len = sizeof(struct sockaddr_in);
283 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
286 if (WSAerrno == WSAEINTR)
294 * make a new transporter (re-uses xprt)
296 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
297 xprt->xp_raddr = addr;
298 xprt->xp_addrlen = len;
299 return (FALSE); /* there is never an rpc msg to be processed */
302 static enum xprt_stat
311 register SVCXPRT *xprt;
313 register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
315 xprt_unregister(xprt);
317 (void)closesocket(xprt->xp_sock);
319 (void)close(xprt->xp_sock);
321 if (xprt->xp_port != 0) {
322 /* a rendezvouser socket */
325 /* an actual connection socket */
326 XDR_DESTROY(&(cd->xdrs));
328 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
329 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
333 * All read operations timeout after 35 seconds.
334 * A timeout is fatal for the connection.
336 static struct timeval wait_per_try = { 35, 0 };
339 * reads data from the tcp conection.
340 * any error is fatal and the connection is closed.
341 * (And a read of zero bytes is a half closed stream => error.)
344 readtcp(xprt, buf, len)
345 register SVCXPRT *xprt;
349 register int sock = xprt->xp_sock;
357 register int mask = 1 << sock;
359 #endif /* def FD_SETSIZE */
363 if (select(0 /* unused in winsock */, &readfds, (int*)NULL, (int*)NULL,
365 if (select(FD_SETSIZE, &readfds, NULL, NULL,
367 &wait_per_try) <= 0) {
369 if (WSAerrno == WSAEINTR) {
371 if (errno == EINTR) {
378 } while (!FD_ISSET(sock, &readfds));
380 } while (readfds != mask);
381 #endif /* def FD_SETSIZE */
383 if ((len = recv(sock, buf, len, 0)) > 0) {
385 if ((len = read(sock, buf, len)) > 0) {
390 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
395 * writes data to the tcp connection.
396 * Any error is fatal and the connection is closed.
399 writetcp(xprt, buf, len)
400 register SVCXPRT *xprt;
406 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
408 if ((i = send(xprt->xp_sock, buf, cnt, 0)) < 0) {
410 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
412 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
420 static enum xprt_stat
424 register struct tcp_conn *cd =
425 (struct tcp_conn *)(xprt->xp_p1);
427 if (cd->strm_stat == XPRT_DIED)
429 if (! xdrrec_eof(&(cd->xdrs)))
430 return (XPRT_MOREREQS);
435 svctcp_recv(xprt, msg)
437 register struct rpc_msg *msg;
439 register struct tcp_conn *cd =
440 (struct tcp_conn *)(xprt->xp_p1);
441 register XDR *xdrs = &(cd->xdrs);
443 xdrs->x_op = XDR_DECODE;
444 (void)xdrrec_skiprecord(xdrs);
445 if (xdr_callmsg(xdrs, msg)) {
446 cd->x_id = msg->rm_xid;
453 svctcp_getargs(xprt, xdr_args, args_ptr)
459 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
463 svctcp_freeargs(xprt, xdr_args, args_ptr)
469 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
471 xdrs->x_op = XDR_FREE;
472 return ((*xdr_args)(xdrs, args_ptr));
476 svctcp_reply(xprt, msg)
478 register struct rpc_msg *msg;
480 register struct tcp_conn *cd =
481 (struct tcp_conn *)(xprt->xp_p1);
482 register XDR *xdrs = &(cd->xdrs);
483 register bool_t stat;
485 xdrs->x_op = XDR_ENCODE;
486 msg->rm_xid = cd->x_id;
487 stat = xdr_replymsg(xdrs, msg);
488 (void)xdrrec_endofrecord(xdrs, TRUE);