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.
49 #include <sys/socket.h>
56 # include <libio/iolibio.h>
60 * Ops vector for TCP/IP based rpc service handle
62 static bool_t
svctcp_recv (SVCXPRT
*, struct rpc_msg
*);
63 static enum xprt_stat
svctcp_stat (SVCXPRT
*);
64 static bool_t
svctcp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
65 static bool_t
svctcp_reply (SVCXPRT
*, struct rpc_msg
*);
66 static bool_t
svctcp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
67 static void svctcp_destroy (SVCXPRT
*);
69 static const struct xp_ops svctcp_op
=
80 * Ops vector for TCP/IP rendezvous handler
82 static bool_t
rendezvous_request (SVCXPRT
*, struct rpc_msg
*);
83 static enum xprt_stat
rendezvous_stat (SVCXPRT
*);
84 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__
));
86 /* This function makes sure abort() relocation goes through PLT
87 and thus can be lazy bound. */
89 svctcp_rendezvous_abort (void)
94 static const struct xp_ops svctcp_rendezvous_op
=
98 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
99 (bool_t (*) (SVCXPRT
*, struct rpc_msg
*)) svctcp_rendezvous_abort
,
100 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
104 static int readtcp (char*, char *, int);
105 static int writetcp (char *, char *, int);
106 static SVCXPRT
*makefd_xprt (int, u_int
, u_int
) internal_function
;
108 struct tcp_rendezvous
109 { /* kept in xprt->xp_p1 */
115 { /* kept in xprt->xp_p1 */
116 enum xprt_stat strm_stat
;
119 char verf_body
[MAX_AUTH_BYTES
];
124 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
126 * Creates, registers, and returns a (rpc) tcp based transporter.
127 * Once *xprt is initialized, it is registered as a transporter
128 * see (svc.h, xprt_register). This routine returns
129 * a NULL if a problem occurred.
131 * If sock<0 then a socket is created, else sock is used.
132 * If the socket, sock is not bound to a port then svctcp_create
133 * binds it to an arbitrary port. The routine then starts a tcp
134 * listener on the socket's associated port. In any (successful) case,
135 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
136 * associated port number.
138 * Since tcp streams do buffered io similar to stdio, the caller can specify
139 * how big the send and receive buffers are via the second and third parms;
140 * 0 => use the system default.
143 svctcp_create (int sock
, u_int sendsize
, u_int recvsize
)
145 bool_t madesock
= FALSE
;
147 struct tcp_rendezvous
*r
;
148 struct sockaddr_in addr
;
149 socklen_t len
= sizeof (struct sockaddr_in
);
151 if (sock
== RPC_ANYSOCK
)
153 if ((sock
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
155 perror (_("svc_tcp.c - tcp socket creation problem"));
156 return (SVCXPRT
*) NULL
;
160 __bzero ((char *) &addr
, sizeof (addr
));
161 addr
.sin_family
= AF_INET
;
162 if (bindresvport (sock
, &addr
))
165 (void) __bind (sock
, (struct sockaddr
*) &addr
, len
);
167 if ((__getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0) ||
168 (__listen (sock
, 2) != 0))
170 perror (_("svc_tcp.c - cannot getsockname or listen"));
172 (void) __close (sock
);
173 return (SVCXPRT
*) NULL
;
175 r
= (struct tcp_rendezvous
*) mem_alloc (sizeof (*r
));
176 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
177 if (r
== NULL
|| xprt
== NULL
)
180 if (_IO_fwide (stderr
, 0) > 0)
181 (void) __fwprintf (stderr
, L
"%s", _("svctcp_create: out of memory\n"));
184 (void) fputs (_("svctcp_create: out of memory\n"), stderr
);
185 mem_free (r
, sizeof (*r
));
186 mem_free (xprt
, sizeof (SVCXPRT
));
189 r
->sendsize
= sendsize
;
190 r
->recvsize
= recvsize
;
192 xprt
->xp_p1
= (caddr_t
) r
;
193 xprt
->xp_verf
= _null_auth
;
194 xprt
->xp_ops
= &svctcp_rendezvous_op
;
195 xprt
->xp_port
= ntohs (addr
.sin_port
);
196 xprt
->xp_sock
= sock
;
197 xprt_register (xprt
);
202 * Like svtcp_create(), except the routine takes any *open* UNIX file
203 * descriptor as its first input.
206 svcfd_create (int fd
, u_int sendsize
, u_int recvsize
)
208 return makefd_xprt (fd
, sendsize
, recvsize
);
213 makefd_xprt (int fd
, u_int sendsize
, u_int recvsize
)
218 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
219 cd
= (struct tcp_conn
*) mem_alloc (sizeof (struct tcp_conn
));
220 if (xprt
== (SVCXPRT
*) NULL
|| cd
== NULL
)
223 if (_IO_fwide (stderr
, 0) > 0)
224 (void) __fwprintf (stderr
, L
"%s",
225 _("svc_tcp: makefd_xprt: out of memory\n"));
228 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr
);
229 mem_free (xprt
, sizeof (SVCXPRT
));
230 mem_free (cd
, sizeof (struct tcp_conn
));
233 cd
->strm_stat
= XPRT_IDLE
;
234 INTUSE(xdrrec_create
) (&(cd
->xdrs
), sendsize
, recvsize
,
235 (caddr_t
) xprt
, readtcp
, writetcp
);
237 xprt
->xp_p1
= (caddr_t
) cd
;
238 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
239 xprt
->xp_addrlen
= 0;
240 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
241 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
243 xprt_register (xprt
);
248 rendezvous_request (SVCXPRT
*xprt
, struct rpc_msg
*errmsg
)
251 struct tcp_rendezvous
*r
;
252 struct sockaddr_in addr
;
255 r
= (struct tcp_rendezvous
*) xprt
->xp_p1
;
257 len
= sizeof (struct sockaddr_in
);
258 if ((sock
= accept (xprt
->xp_sock
, (struct sockaddr
*) &addr
, &len
)) < 0)
265 * make a new transporter (re-uses xprt)
267 xprt
= makefd_xprt (sock
, r
->sendsize
, r
->recvsize
);
268 memcpy (&xprt
->xp_raddr
, &addr
, sizeof (addr
));
269 xprt
->xp_addrlen
= len
;
270 return FALSE
; /* there is never an rpc msg to be processed */
273 static enum xprt_stat
274 rendezvous_stat (SVCXPRT
*xprt
)
280 svctcp_destroy (SVCXPRT
*xprt
)
282 struct tcp_conn
*cd
= (struct tcp_conn
*) xprt
->xp_p1
;
284 xprt_unregister (xprt
);
285 (void) __close (xprt
->xp_sock
);
286 if (xprt
->xp_port
!= 0)
288 /* a rendezvouser socket */
293 /* an actual connection socket */
294 XDR_DESTROY (&(cd
->xdrs
));
296 mem_free ((caddr_t
) cd
, sizeof (struct tcp_conn
));
297 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
302 * reads data from the tcp connection.
303 * any error is fatal and the connection is closed.
304 * (And a read of zero bytes is a half closed stream => error.)
307 readtcp (char *xprtptr
, char *buf
, int len
)
309 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
310 int sock
= xprt
->xp_sock
;
311 int milliseconds
= 35 * 1000;
312 struct pollfd pollfd
;
317 pollfd
.events
= POLLIN
;
318 switch (__poll (&pollfd
, 1, milliseconds
))
327 if ((pollfd
.revents
& POLLERR
) || (pollfd
.revents
& POLLHUP
)
328 || (pollfd
.revents
& POLLNVAL
))
333 while ((pollfd
.revents
& POLLIN
) == 0);
335 if ((len
= __read (sock
, buf
, len
)) > 0)
339 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
344 * writes data to the tcp connection.
345 * Any error is fatal and the connection is closed.
348 writetcp (char *xprtptr
, char * buf
, int len
)
350 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
353 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
355 if ((i
= __write (xprt
->xp_sock
, buf
, cnt
)) < 0)
357 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
364 static enum xprt_stat
365 svctcp_stat (SVCXPRT
*xprt
)
367 struct tcp_conn
*cd
=
368 (struct tcp_conn
*) (xprt
->xp_p1
);
370 if (cd
->strm_stat
== XPRT_DIED
)
372 if (!INTUSE(xdrrec_eof
) (&(cd
->xdrs
)))
373 return XPRT_MOREREQS
;
378 svctcp_recv (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
380 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
381 XDR
*xdrs
= &(cd
->xdrs
);
383 xdrs
->x_op
= XDR_DECODE
;
384 (void) INTUSE(xdrrec_skiprecord
) (xdrs
);
385 if (INTUSE(xdr_callmsg
) (xdrs
, msg
))
387 cd
->x_id
= msg
->rm_xid
;
390 cd
->strm_stat
= XPRT_DIED
; /* XXXX */
395 svctcp_getargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
397 return ((*xdr_args
) (&(((struct tcp_conn
*)
398 (xprt
->xp_p1
))->xdrs
), args_ptr
));
402 svctcp_freeargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
404 XDR
*xdrs
= &(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
);
406 xdrs
->x_op
= XDR_FREE
;
407 return ((*xdr_args
) (xdrs
, args_ptr
));
411 svctcp_reply (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
413 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
414 XDR
*xdrs
= &(cd
->xdrs
);
417 xdrs
->x_op
= XDR_ENCODE
;
418 msg
->rm_xid
= cd
->x_id
;
419 stat
= INTUSE(xdr_replymsg
) (xdrs
, msg
);
420 (void) INTUSE(xdrrec_endofrecord
) (xdrs
, TRUE
);