2 * svc_tcp.c, Server side for TCP/IP based RPC.
4 * Copyright (c) 2010, Oracle America, 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 the "Oracle America, 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>
49 #include <libio/iolibio.h>
52 * Ops vector for TCP/IP based rpc service handle
54 static bool_t
svctcp_recv (SVCXPRT
*, struct rpc_msg
*);
55 static enum xprt_stat
svctcp_stat (SVCXPRT
*);
56 static bool_t
svctcp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
57 static bool_t
svctcp_reply (SVCXPRT
*, struct rpc_msg
*);
58 static bool_t
svctcp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
59 static void svctcp_destroy (SVCXPRT
*);
61 static const struct xp_ops svctcp_op
=
72 * Ops vector for TCP/IP rendezvous handler
74 static bool_t
rendezvous_request (SVCXPRT
*, struct rpc_msg
*);
75 static enum xprt_stat
rendezvous_stat (SVCXPRT
*);
76 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__
));
78 /* This function makes sure abort() relocation goes through PLT
79 and thus can be lazy bound. */
81 svctcp_rendezvous_abort (void)
86 static const struct xp_ops svctcp_rendezvous_op
=
90 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
91 (bool_t (*) (SVCXPRT
*, struct rpc_msg
*)) svctcp_rendezvous_abort
,
92 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
96 static int readtcp (char*, char *, int);
97 static int writetcp (char *, char *, int);
98 static SVCXPRT
*makefd_xprt (int, u_int
, u_int
) internal_function
;
100 struct tcp_rendezvous
101 { /* kept in xprt->xp_p1 */
107 { /* kept in xprt->xp_p1 */
108 enum xprt_stat strm_stat
;
111 char verf_body
[MAX_AUTH_BYTES
];
116 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
118 * Creates, registers, and returns a (rpc) tcp based transporter.
119 * Once *xprt is initialized, it is registered as a transporter
120 * see (svc.h, xprt_register). This routine returns
121 * a NULL if a problem occurred.
123 * If sock<0 then a socket is created, else sock is used.
124 * If the socket, sock is not bound to a port then svctcp_create
125 * binds it to an arbitrary port. The routine then starts a tcp
126 * listener on the socket's associated port. In any (successful) case,
127 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
128 * associated port number.
130 * Since tcp streams do buffered io similar to stdio, the caller can specify
131 * how big the send and receive buffers are via the second and third parms;
132 * 0 => use the system default.
135 svctcp_create (int sock
, u_int sendsize
, u_int recvsize
)
137 bool_t madesock
= FALSE
;
139 struct tcp_rendezvous
*r
;
140 struct sockaddr_in addr
;
141 socklen_t len
= sizeof (struct sockaddr_in
);
143 if (sock
== RPC_ANYSOCK
)
145 if ((sock
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
147 perror (_("svc_tcp.c - tcp socket creation problem"));
148 return (SVCXPRT
*) NULL
;
152 __bzero ((char *) &addr
, sizeof (addr
));
153 addr
.sin_family
= AF_INET
;
154 if (bindresvport (sock
, &addr
))
157 (void) __bind (sock
, (struct sockaddr
*) &addr
, len
);
159 if ((__getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0) ||
160 (__listen (sock
, SOMAXCONN
) != 0))
162 perror (_("svc_tcp.c - cannot getsockname or listen"));
164 (void) __close (sock
);
165 return (SVCXPRT
*) NULL
;
167 r
= (struct tcp_rendezvous
*) mem_alloc (sizeof (*r
));
168 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
169 if (r
== NULL
|| xprt
== NULL
)
171 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
172 mem_free (r
, sizeof (*r
));
173 mem_free (xprt
, sizeof (SVCXPRT
));
176 r
->sendsize
= sendsize
;
177 r
->recvsize
= recvsize
;
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
);
187 #ifdef EXPORT_RPC_SYMBOLS
188 libc_hidden_def (svctcp_create
)
190 libc_hidden_nolink_sunrpc (svctcp_create
, GLIBC_2_0
)
194 * Like svtcp_create(), except the routine takes any *open* UNIX file
195 * descriptor as its first input.
198 svcfd_create (int fd
, u_int sendsize
, u_int recvsize
)
200 return makefd_xprt (fd
, sendsize
, recvsize
);
202 libc_hidden_nolink_sunrpc (svcfd_create
, GLIBC_2_0
)
206 makefd_xprt (int fd
, u_int sendsize
, u_int recvsize
)
211 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
212 cd
= (struct tcp_conn
*) mem_alloc (sizeof (struct tcp_conn
));
213 if (xprt
== (SVCXPRT
*) NULL
|| cd
== NULL
)
215 (void) __fxprintf (NULL
, "%s: %s", "svc_tcp: makefd_xprt",
216 _("out of memory\n"));
217 mem_free (xprt
, sizeof (SVCXPRT
));
218 mem_free (cd
, sizeof (struct tcp_conn
));
221 cd
->strm_stat
= XPRT_IDLE
;
222 xdrrec_create (&(cd
->xdrs
), sendsize
, recvsize
,
223 (caddr_t
) xprt
, readtcp
, writetcp
);
225 xprt
->xp_p1
= (caddr_t
) cd
;
226 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
227 xprt
->xp_addrlen
= 0;
228 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
229 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
231 xprt_register (xprt
);
236 rendezvous_request (SVCXPRT
*xprt
, struct rpc_msg
*errmsg
)
239 struct tcp_rendezvous
*r
;
240 struct sockaddr_in addr
;
243 r
= (struct tcp_rendezvous
*) xprt
->xp_p1
;
245 len
= sizeof (struct sockaddr_in
);
246 if ((sock
= accept (xprt
->xp_sock
, (struct sockaddr
*) &addr
, &len
)) < 0)
253 * make a new transporter (re-uses xprt)
255 xprt
= makefd_xprt (sock
, r
->sendsize
, r
->recvsize
);
256 memcpy (&xprt
->xp_raddr
, &addr
, sizeof (addr
));
257 xprt
->xp_addrlen
= len
;
258 return FALSE
; /* there is never an rpc msg to be processed */
261 static enum xprt_stat
262 rendezvous_stat (SVCXPRT
*xprt
)
268 svctcp_destroy (SVCXPRT
*xprt
)
270 struct tcp_conn
*cd
= (struct tcp_conn
*) xprt
->xp_p1
;
272 xprt_unregister (xprt
);
273 (void) __close (xprt
->xp_sock
);
274 if (xprt
->xp_port
!= 0)
276 /* a rendezvouser socket */
281 /* an actual connection socket */
282 XDR_DESTROY (&(cd
->xdrs
));
284 mem_free ((caddr_t
) cd
, sizeof (struct tcp_conn
));
285 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
290 * reads data from the tcp connection.
291 * any error is fatal and the connection is closed.
292 * (And a read of zero bytes is a half closed stream => error.)
295 readtcp (char *xprtptr
, char *buf
, int len
)
297 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
298 int sock
= xprt
->xp_sock
;
299 int milliseconds
= 35 * 1000;
300 struct pollfd pollfd
;
305 pollfd
.events
= POLLIN
;
306 switch (__poll (&pollfd
, 1, milliseconds
))
315 if ((pollfd
.revents
& POLLERR
) || (pollfd
.revents
& POLLHUP
)
316 || (pollfd
.revents
& POLLNVAL
))
321 while ((pollfd
.revents
& POLLIN
) == 0);
323 if ((len
= __read (sock
, buf
, len
)) > 0)
327 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
332 * writes data to the tcp connection.
333 * Any error is fatal and the connection is closed.
336 writetcp (char *xprtptr
, char * buf
, int len
)
338 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
341 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
343 if ((i
= __write (xprt
->xp_sock
, buf
, cnt
)) < 0)
345 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
352 static enum xprt_stat
353 svctcp_stat (SVCXPRT
*xprt
)
355 struct tcp_conn
*cd
=
356 (struct tcp_conn
*) (xprt
->xp_p1
);
358 if (cd
->strm_stat
== XPRT_DIED
)
360 if (!xdrrec_eof (&(cd
->xdrs
)))
361 return XPRT_MOREREQS
;
366 svctcp_recv (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
368 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
369 XDR
*xdrs
= &(cd
->xdrs
);
371 xdrs
->x_op
= XDR_DECODE
;
372 (void) xdrrec_skiprecord (xdrs
);
373 if (xdr_callmsg (xdrs
, msg
))
375 cd
->x_id
= msg
->rm_xid
;
378 cd
->strm_stat
= XPRT_DIED
; /* XXXX */
383 svctcp_getargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
385 return ((*xdr_args
) (&(((struct tcp_conn
*)
386 (xprt
->xp_p1
))->xdrs
), args_ptr
));
390 svctcp_freeargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
392 XDR
*xdrs
= &(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
);
394 xdrs
->x_op
= XDR_FREE
;
395 return ((*xdr_args
) (xdrs
, args_ptr
));
399 svctcp_reply (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
401 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
402 XDR
*xdrs
= &(cd
->xdrs
);
405 xdrs
->x_op
= XDR_ENCODE
;
406 msg
->rm_xid
= cd
->x_id
;
407 stat
= xdr_replymsg (xdrs
, msg
);
408 (void) xdrrec_endofrecord (xdrs
, TRUE
);