2 * svc_tcp.c, Server side for TCP/IP based RPC.
4 * Copyright (C) 2012-2015 Free Software Foundation, Inc.
5 * This file is part of the GNU C Library.
7 * The GNU C Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the GNU C Library; if not, see
19 * <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2010, Oracle America, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are
27 * * Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * * Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials
32 * provided with the distribution.
33 * * Neither the name of the "Oracle America, Inc." nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
40 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
41 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
46 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
47 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 * Actually implements two flavors of transporter -
51 * a tcp rendezvouser (a listener and connection establisher)
52 * and a record/tcp stream.
60 #include <sys/socket.h>
66 #include <libio/iolibio.h>
69 * Ops vector for TCP/IP based rpc service handle
71 static bool_t
svctcp_recv (SVCXPRT
*, struct rpc_msg
*);
72 static enum xprt_stat
svctcp_stat (SVCXPRT
*);
73 static bool_t
svctcp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
74 static bool_t
svctcp_reply (SVCXPRT
*, struct rpc_msg
*);
75 static bool_t
svctcp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
76 static void svctcp_destroy (SVCXPRT
*);
78 static const struct xp_ops svctcp_op
=
89 * Ops vector for TCP/IP rendezvous handler
91 static bool_t
rendezvous_request (SVCXPRT
*, struct rpc_msg
*);
92 static enum xprt_stat
rendezvous_stat (SVCXPRT
*);
93 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__
));
95 /* This function makes sure abort() relocation goes through PLT
96 and thus can be lazy bound. */
98 svctcp_rendezvous_abort (void)
103 static const struct xp_ops svctcp_rendezvous_op
=
107 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
108 (bool_t (*) (SVCXPRT
*, struct rpc_msg
*)) svctcp_rendezvous_abort
,
109 (bool_t (*) (SVCXPRT
*, xdrproc_t
, caddr_t
)) svctcp_rendezvous_abort
,
113 static int readtcp (char*, char *, int);
114 static int writetcp (char *, char *, int);
115 static SVCXPRT
*makefd_xprt (int, u_int
, u_int
) internal_function
;
117 struct tcp_rendezvous
118 { /* kept in xprt->xp_p1 */
124 { /* kept in xprt->xp_p1 */
125 enum xprt_stat strm_stat
;
128 char verf_body
[MAX_AUTH_BYTES
];
133 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
135 * Creates, registers, and returns a (rpc) tcp based transporter.
136 * Once *xprt is initialized, it is registered as a transporter
137 * see (svc.h, xprt_register). This routine returns
138 * a NULL if a problem occurred.
140 * If sock<0 then a socket is created, else sock is used.
141 * If the socket, sock is not bound to a port then svctcp_create
142 * binds it to an arbitrary port. The routine then starts a tcp
143 * listener on the socket's associated port. In any (successful) case,
144 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
145 * associated port number.
147 * Since tcp streams do buffered io similar to stdio, the caller can specify
148 * how big the send and receive buffers are via the second and third parms;
149 * 0 => use the system default.
152 svctcp_create (int sock
, u_int sendsize
, u_int recvsize
)
154 bool_t madesock
= FALSE
;
156 struct tcp_rendezvous
*r
;
157 struct sockaddr_in addr
;
158 socklen_t len
= sizeof (struct sockaddr_in
);
160 if (sock
== RPC_ANYSOCK
)
162 if ((sock
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
164 perror (_("svc_tcp.c - tcp socket creation problem"));
165 return (SVCXPRT
*) NULL
;
169 __bzero ((char *) &addr
, sizeof (addr
));
170 addr
.sin_family
= AF_INET
;
171 if (bindresvport (sock
, &addr
))
174 (void) __bind (sock
, (struct sockaddr
*) &addr
, len
);
176 if ((__getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0) ||
177 (__listen (sock
, SOMAXCONN
) != 0))
179 perror (_("svc_tcp.c - cannot getsockname or listen"));
181 (void) __close (sock
);
182 return (SVCXPRT
*) NULL
;
184 r
= (struct tcp_rendezvous
*) mem_alloc (sizeof (*r
));
185 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
186 if (r
== NULL
|| xprt
== NULL
)
188 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
189 mem_free (r
, sizeof (*r
));
190 mem_free (xprt
, sizeof (SVCXPRT
));
193 r
->sendsize
= sendsize
;
194 r
->recvsize
= recvsize
;
196 xprt
->xp_p1
= (caddr_t
) r
;
197 xprt
->xp_verf
= _null_auth
;
198 xprt
->xp_ops
= &svctcp_rendezvous_op
;
199 xprt
->xp_port
= ntohs (addr
.sin_port
);
200 xprt
->xp_sock
= sock
;
201 xprt_register (xprt
);
204 #ifdef EXPORT_RPC_SYMBOLS
205 libc_hidden_def (svctcp_create
)
207 libc_hidden_nolink_sunrpc (svctcp_create
, GLIBC_2_0
)
211 * Like svtcp_create(), except the routine takes any *open* UNIX file
212 * descriptor as its first input.
215 svcfd_create (int fd
, u_int sendsize
, u_int recvsize
)
217 return makefd_xprt (fd
, sendsize
, recvsize
);
219 libc_hidden_nolink_sunrpc (svcfd_create
, GLIBC_2_0
)
223 makefd_xprt (int fd
, u_int sendsize
, u_int recvsize
)
228 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
229 cd
= (struct tcp_conn
*) mem_alloc (sizeof (struct tcp_conn
));
230 if (xprt
== (SVCXPRT
*) NULL
|| cd
== NULL
)
232 (void) __fxprintf (NULL
, "%s: %s", "svc_tcp: makefd_xprt",
233 _("out of memory\n"));
234 mem_free (xprt
, sizeof (SVCXPRT
));
235 mem_free (cd
, sizeof (struct tcp_conn
));
238 cd
->strm_stat
= XPRT_IDLE
;
239 xdrrec_create (&(cd
->xdrs
), sendsize
, recvsize
,
240 (caddr_t
) xprt
, readtcp
, writetcp
);
242 xprt
->xp_p1
= (caddr_t
) cd
;
243 xprt
->xp_verf
.oa_base
= cd
->verf_body
;
244 xprt
->xp_addrlen
= 0;
245 xprt
->xp_ops
= &svctcp_op
; /* truly deals with calls */
246 xprt
->xp_port
= 0; /* this is a connection, not a rendezvouser */
248 xprt_register (xprt
);
253 rendezvous_request (SVCXPRT
*xprt
, struct rpc_msg
*errmsg
)
256 struct tcp_rendezvous
*r
;
257 struct sockaddr_in addr
;
260 r
= (struct tcp_rendezvous
*) xprt
->xp_p1
;
262 len
= sizeof (struct sockaddr_in
);
263 if ((sock
= accept (xprt
->xp_sock
, (struct sockaddr
*) &addr
, &len
)) < 0)
267 __svc_accept_failed ();
271 * make a new transporter (re-uses xprt)
273 xprt
= makefd_xprt (sock
, r
->sendsize
, r
->recvsize
);
274 memcpy (&xprt
->xp_raddr
, &addr
, sizeof (addr
));
275 xprt
->xp_addrlen
= len
;
276 return FALSE
; /* there is never an rpc msg to be processed */
279 static enum xprt_stat
280 rendezvous_stat (SVCXPRT
*xprt
)
286 svctcp_destroy (SVCXPRT
*xprt
)
288 struct tcp_conn
*cd
= (struct tcp_conn
*) xprt
->xp_p1
;
290 xprt_unregister (xprt
);
291 (void) __close (xprt
->xp_sock
);
292 if (xprt
->xp_port
!= 0)
294 /* a rendezvouser socket */
299 /* an actual connection socket */
300 XDR_DESTROY (&(cd
->xdrs
));
302 mem_free ((caddr_t
) cd
, sizeof (struct tcp_conn
));
303 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
308 * reads data from the tcp connection.
309 * any error is fatal and the connection is closed.
310 * (And a read of zero bytes is a half closed stream => error.)
313 readtcp (char *xprtptr
, char *buf
, int len
)
315 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
316 int sock
= xprt
->xp_sock
;
317 int milliseconds
= 35 * 1000;
318 struct pollfd pollfd
;
323 pollfd
.events
= POLLIN
;
324 switch (__poll (&pollfd
, 1, milliseconds
))
333 if ((pollfd
.revents
& POLLERR
) || (pollfd
.revents
& POLLHUP
)
334 || (pollfd
.revents
& POLLNVAL
))
339 while ((pollfd
.revents
& POLLIN
) == 0);
341 if ((len
= __read (sock
, buf
, len
)) > 0)
345 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
350 * writes data to the tcp connection.
351 * Any error is fatal and the connection is closed.
354 writetcp (char *xprtptr
, char * buf
, int len
)
356 SVCXPRT
*xprt
= (SVCXPRT
*)xprtptr
;
359 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
361 if ((i
= __write (xprt
->xp_sock
, buf
, cnt
)) < 0)
363 ((struct tcp_conn
*) (xprt
->xp_p1
))->strm_stat
= XPRT_DIED
;
370 static enum xprt_stat
371 svctcp_stat (SVCXPRT
*xprt
)
373 struct tcp_conn
*cd
=
374 (struct tcp_conn
*) (xprt
->xp_p1
);
376 if (cd
->strm_stat
== XPRT_DIED
)
378 if (!xdrrec_eof (&(cd
->xdrs
)))
379 return XPRT_MOREREQS
;
384 svctcp_recv (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
386 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
387 XDR
*xdrs
= &(cd
->xdrs
);
389 xdrs
->x_op
= XDR_DECODE
;
390 (void) xdrrec_skiprecord (xdrs
);
391 if (xdr_callmsg (xdrs
, msg
))
393 cd
->x_id
= msg
->rm_xid
;
396 cd
->strm_stat
= XPRT_DIED
; /* XXXX */
401 svctcp_getargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
403 return ((*xdr_args
) (&(((struct tcp_conn
*)
404 (xprt
->xp_p1
))->xdrs
), args_ptr
));
408 svctcp_freeargs (SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
410 XDR
*xdrs
= &(((struct tcp_conn
*) (xprt
->xp_p1
))->xdrs
);
412 xdrs
->x_op
= XDR_FREE
;
413 return ((*xdr_args
) (xdrs
, args_ptr
));
417 svctcp_reply (SVCXPRT
*xprt
, struct rpc_msg
*msg
)
419 struct tcp_conn
*cd
= (struct tcp_conn
*) (xprt
->xp_p1
);
420 XDR
*xdrs
= &(cd
->xdrs
);
423 xdrs
->x_op
= XDR_ENCODE
;
424 msg
->rm_xid
= cd
->x_id
;
425 stat
= xdr_replymsg (xdrs
, msg
);
426 (void) xdrrec_endofrecord (xdrs
, TRUE
);