1 /* @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid
[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
35 * svc_tcp.c, Server side for TCP/IP based RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * Actually implements two flavors of transporter -
40 * a tcp rendezvouser (a listener and connection establisher)
41 * and a record/tcp stream.
46 #include <sys/socket.h>
48 #define abort ((bool_t (*) ()) abort)
54 * Ops vector for TCP/IP based rpc service handle
56 static bool_t
svctcp_recv();
57 static enum xprt_stat
svctcp_stat();
58 static bool_t
svctcp_getargs();
59 static bool_t
svctcp_reply();
60 static bool_t
svctcp_freeargs();
61 static void svctcp_destroy();
63 static struct xp_ops svctcp_op
= {
73 * Ops vector for TCP/IP rendezvous handler
75 static bool_t
rendezvous_request();
76 static enum xprt_stat
rendezvous_stat();
78 static struct xp_ops svctcp_rendezvous_op
= {
87 static int readtcp(), writetcp();
88 static SVCXPRT
*makefd_xprt();
90 struct tcp_rendezvous
{ /* kept in xprt->xp_p1 */
95 struct tcp_conn
{ /* kept in xprt->xp_p1 */
96 enum xprt_stat strm_stat
;
99 char verf_body
[MAX_AUTH_BYTES
];
104 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
106 * Creates, registers, and returns a (rpc) tcp based transporter.
107 * Once *xprt is initialized, it is registered as a transporter
108 * see (svc.h, xprt_register). This routine returns
109 * a NULL if a problem occurred.
111 * If sock<0 then a socket is created, else sock is used.
112 * If the socket, sock is not bound to a port then svctcp_create
113 * binds it to an arbitrary port. The routine then starts a tcp
114 * listener on the socket's associated port. In any (successful) case,
115 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
116 * associated port number.
118 * Since tcp streams do buffered io similar to stdio, the caller can specify
119 * how big the send and receive buffers are via the second and third parms;
120 * 0 => use the system default.
123 svctcp_create(sock
, sendsize
, recvsize
)
128 bool_t madesock
= FALSE
;
129 register SVCXPRT
*xprt
;
130 register struct tcp_rendezvous
*r
;
131 struct sockaddr_in addr
;
132 int len
= sizeof(struct sockaddr_in
);
134 if (sock
== RPC_ANYSOCK
) {
135 if ((sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0) {
136 perror(_("svctcp_.c - udp socket creation problem"));
137 return ((SVCXPRT
*)NULL
);
141 bzero((char *)&addr
, sizeof (addr
));
142 addr
.sin_family
= AF_INET
;
143 if (bindresvport(sock
, &addr
)) {
145 (void)bind(sock
, (struct sockaddr
*)&addr
, len
);
147 if ((getsockname(sock
, (struct sockaddr
*)&addr
, &len
) != 0) ||
148 (listen(sock
, 2) != 0)) {
149 perror(_("svctcp_.c - cannot getsockname or listen"));
152 return ((SVCXPRT
*)NULL
);
154 r
= (struct tcp_rendezvous
*)mem_alloc(sizeof(*r
));
156 (void) fprintf(stderr
, _("svctcp_create: out of memory\n"));
159 r
->sendsize
= sendsize
;
160 r
->recvsize
= recvsize
;
161 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
163 (void) fprintf(stderr
, _("svctcp_create: out of memory\n"));
167 xprt
->xp_p1
= (caddr_t
)r
;
168 xprt
->xp_verf
= _null_auth
;
169 xprt
->xp_ops
= &svctcp_rendezvous_op
;
170 xprt
->xp_port
= ntohs(addr
.sin_port
);
171 xprt
->xp_sock
= sock
;
177 * Like svtcp_create(), except the routine takes any *open* UNIX file
178 * descriptor as its first input.
181 svcfd_create(fd
, sendsize
, recvsize
)
187 return (makefd_xprt(fd
, sendsize
, recvsize
));
191 makefd_xprt(fd
, sendsize
, recvsize
)
196 register SVCXPRT
*xprt
;
197 register struct tcp_conn
*cd
;
199 xprt
= (SVCXPRT
*)mem_alloc(sizeof(SVCXPRT
));
200 if (xprt
== (SVCXPRT
*)NULL
) {
201 (void) fprintf(stderr
, _("svc_tcp: makefd_xprt: out of memory\n"));
204 cd
= (struct tcp_conn
*)mem_alloc(sizeof(struct tcp_conn
));
205 if (cd
== (struct tcp_conn
*)NULL
) {
206 (void) fprintf(stderr
, _("svc_tcp: makefd_xprt: out of memory\n"));
207 mem_free((char *) xprt
, sizeof(SVCXPRT
));
208 xprt
= (SVCXPRT
*)NULL
;
211 cd
->strm_stat
= XPRT_IDLE
;
212 xdrrec_create(&(cd
->xdrs
), sendsize
, recvsize
,
213 (caddr_t
)xprt
, readtcp
, writetcp
);
215 xprt
->xp_p1
= (caddr_t
)cd
;
216 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
217 xprt
->xp_addrlen
= 0;
218 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
219 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
227 rendezvous_request(xprt
)
228 register SVCXPRT
*xprt
;
231 struct tcp_rendezvous
*r
;
232 struct sockaddr_in addr
;
235 r
= (struct tcp_rendezvous
*)xprt
->xp_p1
;
237 len
= sizeof(struct sockaddr_in
);
238 if ((sock
= accept(xprt
->xp_sock
, (struct sockaddr
*)&addr
,
245 * make a new transporter (re-uses xprt)
247 xprt
= makefd_xprt(sock
, r
->sendsize
, r
->recvsize
);
248 xprt
->xp_raddr
= addr
;
249 xprt
->xp_addrlen
= len
;
250 return (FALSE
); /* there is never an rpc msg to be processed */
253 static enum xprt_stat
262 register SVCXPRT
*xprt
;
264 register struct tcp_conn
*cd
= (struct tcp_conn
*)xprt
->xp_p1
;
266 xprt_unregister(xprt
);
267 (void)close(xprt
->xp_sock
);
268 if (xprt
->xp_port
!= 0) {
269 /* a rendezvouser socket */
272 /* an actual connection socket */
273 XDR_DESTROY(&(cd
->xdrs
));
275 mem_free((caddr_t
)cd
, sizeof(struct tcp_conn
));
276 mem_free((caddr_t
)xprt
, sizeof(SVCXPRT
));
280 * All read operations timeout after 35 seconds.
281 * A timeout is fatal for the connection.
283 static struct timeval wait_per_try
= { 35, 0 };
286 * reads data from the tcp connection.
287 * any error is fatal and the connection is closed.
288 * (And a read of zero bytes is a half closed stream => error.)
291 readtcp(xprt
, buf
, len
)
292 register SVCXPRT
*xprt
;
296 register int sock
= xprt
->xp_sock
;
304 register int mask
= 1 << sock
;
306 #endif /* def FD_SETSIZE */
308 struct timeval timeout
= wait_per_try
;
310 if (select(_rpc_dtablesize(), &readfds
, (int*)NULL
, (int*)NULL
,
312 if (errno
== EINTR
) {
318 } while (!FD_ISSET(sock
, &readfds
));
320 } while (readfds
!= mask
);
321 #endif /* def FD_SETSIZE */
322 if ((len
= read(sock
, buf
, len
)) > 0) {
326 ((struct tcp_conn
*)(xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
331 * writes data to the tcp connection.
332 * Any error is fatal and the connection is closed.
335 writetcp(xprt
, buf
, len
)
336 register SVCXPRT
*xprt
;
342 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
) {
343 if ((i
= write(xprt
->xp_sock
, buf
, cnt
)) < 0) {
344 ((struct tcp_conn
*)(xprt
->xp_p1
))->strm_stat
=
352 static enum xprt_stat
356 register struct tcp_conn
*cd
=
357 (struct tcp_conn
*)(xprt
->xp_p1
);
359 if (cd
->strm_stat
== XPRT_DIED
)
361 if (! xdrrec_eof(&(cd
->xdrs
)))
362 return (XPRT_MOREREQS
);
367 svctcp_recv(xprt
, msg
)
369 register struct rpc_msg
*msg
;
371 register struct tcp_conn
*cd
=
372 (struct tcp_conn
*)(xprt
->xp_p1
);
373 register XDR
*xdrs
= &(cd
->xdrs
);
375 xdrs
->x_op
= XDR_DECODE
;
376 (void)xdrrec_skiprecord(xdrs
);
377 if (xdr_callmsg(xdrs
, msg
)) {
378 cd
->x_id
= msg
->rm_xid
;
385 svctcp_getargs(xprt
, xdr_args
, args_ptr
)
391 return ((*xdr_args
)(&(((struct tcp_conn
*)(xprt
->xp_p1
))->xdrs
), args_ptr
));
395 svctcp_freeargs(xprt
, xdr_args
, args_ptr
)
401 &(((struct tcp_conn
*)(xprt
->xp_p1
))->xdrs
);
403 xdrs
->x_op
= XDR_FREE
;
404 return ((*xdr_args
)(xdrs
, args_ptr
));
408 svctcp_reply(xprt
, msg
)
410 register struct rpc_msg
*msg
;
412 register struct tcp_conn
*cd
=
413 (struct tcp_conn
*)(xprt
->xp_p1
);
414 register XDR
*xdrs
= &(cd
->xdrs
);
415 register bool_t stat
;
417 xdrs
->x_op
= XDR_ENCODE
;
418 msg
->rm_xid
= cd
->x_id
;
419 stat
= xdr_replymsg(xdrs
, msg
);
420 (void)xdrrec_endofrecord(xdrs
, TRUE
);