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.
48 #include <sys/socket.h>
53 # include <libio/iolibio.h>
54 # define fputs(s, f) _IO_fputs (s, f)
58 * Ops vector for TCP/IP based rpc service handle
60 static bool_t
svctcp_recv (SVCXPRT
*, struct rpc_msg
*);
61 static enum xprt_stat
svctcp_stat (SVCXPRT
*);
62 static bool_t
svctcp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
63 static bool_t
svctcp_reply (SVCXPRT
*, struct rpc_msg
*);
64 static bool_t
svctcp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
65 static void svctcp_destroy (SVCXPRT
*);
67 static const struct xp_ops svctcp_op
=
78 * Ops vector for TCP/IP rendezvous handler
80 static bool_t
rendezvous_request (SVCXPRT
*, struct rpc_msg
*);
81 static enum xprt_stat
rendezvous_stat (SVCXPRT
*);
83 static const struct xp_ops svctcp_rendezvous_op
=
87 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) abort
,
88 (bool_t (*) (SVCXPRT
*, struct rpc_msg
*)) abort
,
89 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) abort
,
93 static int readtcp (char*, char *, int);
94 static int writetcp (char *, char *, int);
95 static SVCXPRT
*makefd_xprt (int, u_int
, u_int
) internal_function
;
98 { /* kept in xprt->xp_p1 */
104 { /* kept in xprt->xp_p1 */
105 enum xprt_stat strm_stat
;
108 char verf_body
[MAX_AUTH_BYTES
];
113 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
115 * Creates, registers, and returns a (rpc) tcp based transporter.
116 * Once *xprt is initialized, it is registered as a transporter
117 * see (svc.h, xprt_register). This routine returns
118 * a NULL if a problem occurred.
120 * If sock<0 then a socket is created, else sock is used.
121 * If the socket, sock is not bound to a port then svctcp_create
122 * binds it to an arbitrary port. The routine then starts a tcp
123 * listener on the socket's associated port. In any (successful) case,
124 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
125 * associated port number.
127 * Since tcp streams do buffered io similar to stdio, the caller can specify
128 * how big the send and receive buffers are via the second and third parms;
129 * 0 => use the system default.
132 svctcp_create (int sock
, u_int sendsize
, u_int recvsize
)
134 bool_t madesock
= FALSE
;
136 struct tcp_rendezvous
*r
;
137 struct sockaddr_in addr
;
138 socklen_t len
= sizeof (struct sockaddr_in
);
140 if (sock
== RPC_ANYSOCK
)
142 if ((sock
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
144 perror (_("svc_tcp.c - tcp socket creation problem"));
145 return (SVCXPRT
*) NULL
;
149 __bzero ((char *) &addr
, sizeof (addr
));
150 addr
.sin_family
= AF_INET
;
151 if (bindresvport (sock
, &addr
))
154 (void) bind (sock
, (struct sockaddr
*) &addr
, len
);
156 if ((getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0) ||
157 (listen (sock
, 2) != 0))
159 perror (_("svc_tcp.c - cannot getsockname or listen"));
161 (void) __close (sock
);
162 return (SVCXPRT
*) NULL
;
164 r
= (struct tcp_rendezvous
*) mem_alloc (sizeof (*r
));
167 (void) fputs (_("svctcp_create: out of memory\n"), stderr
);
170 r
->sendsize
= sendsize
;
171 r
->recvsize
= recvsize
;
172 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
175 (void) fputs (_("svctcp_create: out of memory\n"), stderr
);
179 xprt
->xp_p1
= (caddr_t
) r
;
180 xprt
->xp_verf
= _null_auth
;
181 xprt
->xp_ops
= &svctcp_rendezvous_op
;
182 xprt
->xp_port
= ntohs (addr
.sin_port
);
183 xprt
->xp_sock
= sock
;
184 xprt_register (xprt
);
189 * Like svtcp_create(), except the routine takes any *open* UNIX file
190 * descriptor as its first input.
193 svcfd_create (int fd
, u_int sendsize
, u_int recvsize
)
195 return makefd_xprt (fd
, sendsize
, recvsize
);
200 makefd_xprt (int fd
, u_int sendsize
, u_int recvsize
)
205 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
206 if (xprt
== (SVCXPRT
*) NULL
)
208 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr
);
211 cd
= (struct tcp_conn
*) mem_alloc (sizeof (struct tcp_conn
));
212 if (cd
== (struct tcp_conn
*) NULL
)
214 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr
);
215 mem_free ((char *) xprt
, sizeof (SVCXPRT
));
216 xprt
= (SVCXPRT
*) NULL
;
219 cd
->strm_stat
= XPRT_IDLE
;
220 xdrrec_create (&(cd
->xdrs
), sendsize
, recvsize
,
221 (caddr_t
) xprt
, readtcp
, writetcp
);
223 xprt
->xp_p1
= (caddr_t
) cd
;
224 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
225 xprt
->xp_addrlen
= 0;
226 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
227 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
229 xprt_register (xprt
);
235 rendezvous_request (SVCXPRT
*xprt
, struct rpc_msg
*errmsg
)
238 struct tcp_rendezvous
*r
;
239 struct sockaddr_in addr
;
242 r
= (struct tcp_rendezvous
*) xprt
->xp_p1
;
244 len
= sizeof (struct sockaddr_in
);
245 if ((sock
= accept (xprt
->xp_sock
, (struct sockaddr
*) &addr
, &len
)) < 0)
252 * make a new transporter (re-uses xprt)
254 xprt
= makefd_xprt (sock
, r
->sendsize
, r
->recvsize
);
255 xprt
->xp_raddr
= addr
;
256 xprt
->xp_addrlen
= len
;
257 return FALSE
; /* there is never an rpc msg to be processed */
260 static enum xprt_stat
261 rendezvous_stat (SVCXPRT
*xprt
)
267 svctcp_destroy (SVCXPRT
*xprt
)
269 struct tcp_conn
*cd
= (struct tcp_conn
*) xprt
->xp_p1
;
271 xprt_unregister (xprt
);
272 (void) __close (xprt
->xp_sock
);
273 if (xprt
->xp_port
!= 0)
275 /* a rendezvouser socket */
280 /* an actual connection socket */
281 XDR_DESTROY (&(cd
->xdrs
));
283 mem_free ((caddr_t
) cd
, sizeof (struct tcp_conn
));
284 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
288 * All read operations timeout after 35 seconds.
289 * A timeout is fatal for the connection.
291 static struct timeval wait_per_try
= {35, 0};
294 * reads data from the tcp connection.
295 * any error is fatal and the connection is closed.
296 * (And a read of zero bytes is a half closed stream => error.)
298 * Note: we have to be careful here not to allow ourselves to become
299 * blocked too long in this routine. While we're waiting for data from one
300 * client, another client may be trying to connect. To avoid this situation,
301 * some code from svc_run() is transplanted here: the select() loop checks
302 * all RPC descriptors including the one we want and calls svc_getreqset2()
303 * to handle new requests if any are detected.
306 readtcp (char *xprtptr
, char *buf
, int len
)
308 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
309 int sock
= xprt
->xp_sock
;
313 int mask
= 1 << sock
;
315 #endif /* def FD_SETSIZE */
318 struct timeval timeout
= wait_per_try
;
321 FD_SET (sock
, &readfds
);
323 readfds
|= (1 << sock
);
324 #endif /* def FD_SETSIZE */
325 if (__select (_rpc_dtablesize (), &readfds
, (fd_set
*) NULL
,
326 (fd_set
*) NULL
, &timeout
) <= 0)
334 if (FD_ISSET (sock
, &readfds
))
337 #endif /* def FD_SETSIZE */
340 svc_getreqset (&readfds
);
343 if ((len
= __read (sock
, buf
, len
)) > 0)
347 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
352 * writes data to the tcp connection.
353 * Any error is fatal and the connection is closed.
356 writetcp (char *xprtptr
, char * buf
, int len
)
358 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
361 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
363 if ((i
= __write (xprt
->xp_sock
, buf
, cnt
)) < 0)
365 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
=
373 static enum xprt_stat
374 svctcp_stat (SVCXPRT
*xprt
)
376 struct tcp_conn
*cd
=
377 (struct tcp_conn
*) (xprt
->xp_p1
);
379 if (cd
->strm_stat
== XPRT_DIED
)
381 if (!xdrrec_eof (&(cd
->xdrs
)))
382 return (XPRT_MOREREQS
);
387 svctcp_recv (xprt
, msg
)
391 struct tcp_conn
*cd
=
392 (struct tcp_conn
*) (xprt
->xp_p1
);
393 XDR
*xdrs
= &(cd
->xdrs
);
395 xdrs
->x_op
= XDR_DECODE
;
396 (void) xdrrec_skiprecord (xdrs
);
397 if (xdr_callmsg (xdrs
, msg
))
399 cd
->x_id
= msg
->rm_xid
;
402 cd
->strm_stat
= XPRT_DIED
; /* XXXX */
407 svctcp_getargs (xprt
, xdr_args
, args_ptr
)
413 return ((*xdr_args
) (&(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
), args_ptr
));
417 svctcp_freeargs (xprt
, xdr_args
, args_ptr
)
423 &(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
);
425 xdrs
->x_op
= XDR_FREE
;
426 return ((*xdr_args
) (xdrs
, args_ptr
));
430 svctcp_reply (xprt
, msg
)
434 struct tcp_conn
*cd
=
435 (struct tcp_conn
*) (xprt
->xp_p1
);
436 XDR
*xdrs
= &(cd
->xdrs
);
439 xdrs
->x_op
= XDR_ENCODE
;
440 msg
->rm_xid
= cd
->x_id
;
441 stat
= xdr_replymsg (xdrs
, msg
);
442 (void) xdrrec_endofrecord (xdrs
, TRUE
);