2 * svc_tcp.c, Server side for TCP/IP based RPC.
4 * Copyright (C) 1984, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Actually implements two flavors of transporter -
34 * a tcp rendezvouser (a listener and connection establisher)
35 * and a record/tcp stream.
43 #include <sys/socket.h>
50 # include <libio/iolibio.h>
54 * Ops vector for TCP/IP based rpc service handle
56 static bool_t
svctcp_recv (SVCXPRT
*, struct rpc_msg
*);
57 static enum xprt_stat
svctcp_stat (SVCXPRT
*);
58 static bool_t
svctcp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
59 static bool_t
svctcp_reply (SVCXPRT
*, struct rpc_msg
*);
60 static bool_t
svctcp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
61 static void svctcp_destroy (SVCXPRT
*);
63 static const struct xp_ops svctcp_op
=
74 * Ops vector for TCP/IP rendezvous handler
76 static bool_t
rendezvous_request (SVCXPRT
*, struct rpc_msg
*);
77 static enum xprt_stat
rendezvous_stat (SVCXPRT
*);
78 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__
));
80 /* This function makes sure abort() relocation goes through PLT
81 and thus can be lazy bound. */
83 svctcp_rendezvous_abort (void)
88 static const struct xp_ops svctcp_rendezvous_op
=
92 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
93 (bool_t (*) (SVCXPRT
*, struct rpc_msg
*)) svctcp_rendezvous_abort
,
94 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
98 static int readtcp (char*, char *, int);
99 static int writetcp (char *, char *, int);
100 static SVCXPRT
*makefd_xprt (int, u_int
, u_int
) internal_function
;
102 struct tcp_rendezvous
103 { /* kept in xprt->xp_p1 */
109 { /* kept in xprt->xp_p1 */
110 enum xprt_stat strm_stat
;
113 char verf_body
[MAX_AUTH_BYTES
];
118 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
120 * Creates, registers, and returns a (rpc) tcp based transporter.
121 * Once *xprt is initialized, it is registered as a transporter
122 * see (svc.h, xprt_register). This routine returns
123 * a NULL if a problem occurred.
125 * If sock<0 then a socket is created, else sock is used.
126 * If the socket, sock is not bound to a port then svctcp_create
127 * binds it to an arbitrary port. The routine then starts a tcp
128 * listener on the socket's associated port. In any (successful) case,
129 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
130 * associated port number.
132 * Since tcp streams do buffered io similar to stdio, the caller can specify
133 * how big the send and receive buffers are via the second and third parms;
134 * 0 => use the system default.
137 svctcp_create (int sock
, u_int sendsize
, u_int recvsize
)
139 bool_t madesock
= FALSE
;
141 struct tcp_rendezvous
*r
;
142 struct sockaddr_in addr
;
143 socklen_t len
= sizeof (struct sockaddr_in
);
145 if (sock
== RPC_ANYSOCK
)
147 if ((sock
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
149 perror (_("svc_tcp.c - tcp socket creation problem"));
150 return (SVCXPRT
*) NULL
;
154 __bzero ((char *) &addr
, sizeof (addr
));
155 addr
.sin_family
= AF_INET
;
156 if (bindresvport (sock
, &addr
))
159 (void) __bind (sock
, (struct sockaddr
*) &addr
, len
);
161 if ((__getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0) ||
162 (__listen (sock
, SOMAXCONN
) != 0))
164 perror (_("svc_tcp.c - cannot getsockname or listen"));
166 (void) __close (sock
);
167 return (SVCXPRT
*) NULL
;
169 r
= (struct tcp_rendezvous
*) mem_alloc (sizeof (*r
));
170 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
171 if (r
== NULL
|| xprt
== NULL
)
173 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
174 mem_free (r
, sizeof (*r
));
175 mem_free (xprt
, sizeof (SVCXPRT
));
178 r
->sendsize
= sendsize
;
179 r
->recvsize
= recvsize
;
181 xprt
->xp_p1
= (caddr_t
) r
;
182 xprt
->xp_verf
= _null_auth
;
183 xprt
->xp_ops
= &svctcp_rendezvous_op
;
184 xprt
->xp_port
= ntohs (addr
.sin_port
);
185 xprt
->xp_sock
= sock
;
186 xprt_register (xprt
);
191 * Like svtcp_create(), except the routine takes any *open* UNIX file
192 * descriptor as its first input.
195 svcfd_create (int fd
, u_int sendsize
, u_int recvsize
)
197 return makefd_xprt (fd
, sendsize
, recvsize
);
202 makefd_xprt (int fd
, u_int sendsize
, u_int recvsize
)
207 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
208 cd
= (struct tcp_conn
*) mem_alloc (sizeof (struct tcp_conn
));
209 if (xprt
== (SVCXPRT
*) NULL
|| cd
== NULL
)
211 (void) __fxprintf (NULL
, "%s: %s", "svc_tcp: makefd_xprt",
212 _("out of memory\n"));
213 mem_free (xprt
, sizeof (SVCXPRT
));
214 mem_free (cd
, sizeof (struct tcp_conn
));
217 cd
->strm_stat
= XPRT_IDLE
;
218 INTUSE(xdrrec_create
) (&(cd
->xdrs
), sendsize
, recvsize
,
219 (caddr_t
) xprt
, readtcp
, writetcp
);
221 xprt
->xp_p1
= (caddr_t
) cd
;
222 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
223 xprt
->xp_addrlen
= 0;
224 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
225 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
227 xprt_register (xprt
);
232 rendezvous_request (SVCXPRT
*xprt
, struct rpc_msg
*errmsg
)
235 struct tcp_rendezvous
*r
;
236 struct sockaddr_in addr
;
239 r
= (struct tcp_rendezvous
*) xprt
->xp_p1
;
241 len
= sizeof (struct sockaddr_in
);
242 if ((sock
= accept (xprt
->xp_sock
, (struct sockaddr
*) &addr
, &len
)) < 0)
249 * make a new transporter (re-uses xprt)
251 xprt
= makefd_xprt (sock
, r
->sendsize
, r
->recvsize
);
252 memcpy (&xprt
->xp_raddr
, &addr
, sizeof (addr
));
253 xprt
->xp_addrlen
= len
;
254 return FALSE
; /* there is never an rpc msg to be processed */
257 static enum xprt_stat
258 rendezvous_stat (SVCXPRT
*xprt
)
264 svctcp_destroy (SVCXPRT
*xprt
)
266 struct tcp_conn
*cd
= (struct tcp_conn
*) xprt
->xp_p1
;
268 xprt_unregister (xprt
);
269 (void) __close (xprt
->xp_sock
);
270 if (xprt
->xp_port
!= 0)
272 /* a rendezvouser socket */
277 /* an actual connection socket */
278 XDR_DESTROY (&(cd
->xdrs
));
280 mem_free ((caddr_t
) cd
, sizeof (struct tcp_conn
));
281 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
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 (char *xprtptr
, char *buf
, int len
)
293 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
294 int sock
= xprt
->xp_sock
;
295 int milliseconds
= 35 * 1000;
296 struct pollfd pollfd
;
301 pollfd
.events
= POLLIN
;
302 switch (__poll (&pollfd
, 1, milliseconds
))
311 if ((pollfd
.revents
& POLLERR
) || (pollfd
.revents
& POLLHUP
)
312 || (pollfd
.revents
& POLLNVAL
))
317 while ((pollfd
.revents
& POLLIN
) == 0);
319 if ((len
= __read (sock
, buf
, len
)) > 0)
323 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
328 * writes data to the tcp connection.
329 * Any error is fatal and the connection is closed.
332 writetcp (char *xprtptr
, char * buf
, int len
)
334 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
337 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
339 if ((i
= __write (xprt
->xp_sock
, buf
, cnt
)) < 0)
341 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
348 static enum xprt_stat
349 svctcp_stat (SVCXPRT
*xprt
)
351 struct tcp_conn
*cd
=
352 (struct tcp_conn
*) (xprt
->xp_p1
);
354 if (cd
->strm_stat
== XPRT_DIED
)
356 if (!INTUSE(xdrrec_eof
) (&(cd
->xdrs
)))
357 return XPRT_MOREREQS
;
362 svctcp_recv (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
364 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
365 XDR
*xdrs
= &(cd
->xdrs
);
367 xdrs
->x_op
= XDR_DECODE
;
368 (void) INTUSE(xdrrec_skiprecord
) (xdrs
);
369 if (INTUSE(xdr_callmsg
) (xdrs
, msg
))
371 cd
->x_id
= msg
->rm_xid
;
374 cd
->strm_stat
= XPRT_DIED
; /* XXXX */
379 svctcp_getargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
381 return ((*xdr_args
) (&(((struct tcp_conn
*)
382 (xprt
->xp_p1
))->xdrs
), args_ptr
));
386 svctcp_freeargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
388 XDR
*xdrs
= &(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
);
390 xdrs
->x_op
= XDR_FREE
;
391 return ((*xdr_args
) (xdrs
, args_ptr
));
395 svctcp_reply (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
397 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
398 XDR
*xdrs
= &(cd
->xdrs
);
401 xdrs
->x_op
= XDR_ENCODE
;
402 msg
->rm_xid
= cd
->x_id
;
403 stat
= INTUSE(xdr_replymsg
) (xdrs
, msg
);
404 (void) INTUSE(xdrrec_endofrecord
) (xdrs
, TRUE
);