]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* |
a7ab6ec8 | 2 | * svc_tcp.c, Server side for TCP/IP based RPC. |
cbd3dceb | 3 | * |
a7ab6ec8 | 4 | * Copyright (c) 2010, Oracle America, Inc. |
cbd3dceb | 5 | * |
a7ab6ec8 UD |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
cbd3dceb | 9 | * |
a7ab6ec8 UD |
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. | |
cb636bb2 | 19 | * |
a7ab6ec8 UD |
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. | |
28f540f4 RM |
32 | * |
33 | * Actually implements two flavors of transporter - | |
6d52618b | 34 | * a tcp rendezvouser (a listener and connection establisher) |
28f540f4 RM |
35 | * and a record/tcp stream. |
36 | */ | |
37 | ||
38 | #include <stdio.h> | |
e7fd8a39 UD |
39 | #include <unistd.h> |
40 | #include <string.h> | |
4360eafd | 41 | #include <libintl.h> |
28f540f4 RM |
42 | #include <rpc/rpc.h> |
43 | #include <sys/socket.h> | |
090ca000 | 44 | #include <sys/poll.h> |
28f540f4 | 45 | #include <errno.h> |
e7fd8a39 | 46 | #include <stdlib.h> |
28f540f4 | 47 | |
3ce1f295 UD |
48 | #include <wchar.h> |
49 | #include <libio/iolibio.h> | |
50304ef0 | 50 | |
28f540f4 RM |
51 | /* |
52 | * Ops vector for TCP/IP based rpc service handle | |
53 | */ | |
e7fd8a39 UD |
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 *); | |
60 | ||
61 | static const struct xp_ops svctcp_op = | |
62 | { | |
63 | svctcp_recv, | |
64 | svctcp_stat, | |
65 | svctcp_getargs, | |
66 | svctcp_reply, | |
67 | svctcp_freeargs, | |
68 | svctcp_destroy | |
28f540f4 RM |
69 | }; |
70 | ||
71 | /* | |
72 | * Ops vector for TCP/IP rendezvous handler | |
73 | */ | |
e7fd8a39 UD |
74 | static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *); |
75 | static enum xprt_stat rendezvous_stat (SVCXPRT *); | |
a49d0179 | 76 | static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__)); |
4b9afc43 UD |
77 | |
78 | /* This function makes sure abort() relocation goes through PLT | |
79 | and thus can be lazy bound. */ | |
80 | static void | |
81 | svctcp_rendezvous_abort (void) | |
82 | { | |
83 | abort (); | |
84 | }; | |
e7fd8a39 UD |
85 | |
86 | static const struct xp_ops svctcp_rendezvous_op = | |
87 | { | |
88 | rendezvous_request, | |
89 | rendezvous_stat, | |
4b9afc43 UD |
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, | |
e7fd8a39 | 93 | svctcp_destroy |
28f540f4 RM |
94 | }; |
95 | ||
e7fd8a39 UD |
96 | static int readtcp (char*, char *, int); |
97 | static int writetcp (char *, char *, int); | |
dfd2257a | 98 | static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function; |
28f540f4 | 99 | |
e7fd8a39 UD |
100 | struct tcp_rendezvous |
101 | { /* kept in xprt->xp_p1 */ | |
102 | u_int sendsize; | |
103 | u_int recvsize; | |
104 | }; | |
28f540f4 | 105 | |
e7fd8a39 UD |
106 | struct tcp_conn |
107 | { /* kept in xprt->xp_p1 */ | |
108 | enum xprt_stat strm_stat; | |
109 | u_long x_id; | |
110 | XDR xdrs; | |
111 | char verf_body[MAX_AUTH_BYTES]; | |
112 | }; | |
28f540f4 RM |
113 | |
114 | /* | |
115 | * Usage: | |
e7fd8a39 | 116 | * xprt = svctcp_create(sock, send_buf_size, recv_buf_size); |
28f540f4 RM |
117 | * |
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. | |
122 | * | |
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. | |
129 | * | |
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. | |
133 | */ | |
134 | SVCXPRT * | |
e7fd8a39 | 135 | svctcp_create (int sock, u_int sendsize, u_int recvsize) |
28f540f4 | 136 | { |
e7fd8a39 UD |
137 | bool_t madesock = FALSE; |
138 | SVCXPRT *xprt; | |
139 | struct tcp_rendezvous *r; | |
140 | struct sockaddr_in addr; | |
41df5ed4 | 141 | socklen_t len = sizeof (struct sockaddr_in); |
e7fd8a39 UD |
142 | |
143 | if (sock == RPC_ANYSOCK) | |
144 | { | |
50304ef0 | 145 | if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
e7fd8a39 | 146 | { |
e852e889 | 147 | perror (_("svc_tcp.c - tcp socket creation problem")); |
e7fd8a39 | 148 | return (SVCXPRT *) NULL; |
28f540f4 | 149 | } |
e7fd8a39 UD |
150 | madesock = TRUE; |
151 | } | |
50304ef0 | 152 | __bzero ((char *) &addr, sizeof (addr)); |
e7fd8a39 | 153 | addr.sin_family = AF_INET; |
a585ba22 | 154 | if (bindresvport (sock, &addr)) |
e7fd8a39 UD |
155 | { |
156 | addr.sin_port = 0; | |
b2bffca2 | 157 | (void) __bind (sock, (struct sockaddr *) &addr, len); |
e7fd8a39 | 158 | } |
b2bffca2 | 159 | if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) || |
a3a449c1 | 160 | (__listen (sock, SOMAXCONN) != 0)) |
e7fd8a39 | 161 | { |
e852e889 | 162 | perror (_("svc_tcp.c - cannot getsockname or listen")); |
e7fd8a39 | 163 | if (madesock) |
50304ef0 | 164 | (void) __close (sock); |
e7fd8a39 UD |
165 | return (SVCXPRT *) NULL; |
166 | } | |
167 | r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r)); | |
51028f34 UD |
168 | xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT)); |
169 | if (r == NULL || xprt == NULL) | |
e7fd8a39 | 170 | { |
1d20f7f8 | 171 | (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n")); |
2e3e5db6 UD |
172 | mem_free (r, sizeof (*r)); |
173 | mem_free (xprt, sizeof (SVCXPRT)); | |
e7fd8a39 UD |
174 | return NULL; |
175 | } | |
176 | r->sendsize = sendsize; | |
177 | r->recvsize = recvsize; | |
e7fd8a39 UD |
178 | xprt->xp_p2 = NULL; |
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); | |
185 | return xprt; | |
28f540f4 | 186 | } |
7b57bfe5 UD |
187 | #ifdef EXPORT_RPC_SYMBOLS |
188 | libc_hidden_def (svctcp_create) | |
189 | #else | |
190 | libc_hidden_nolink (svctcp_create, GLIBC_2_0) | |
191 | #endif | |
28f540f4 RM |
192 | |
193 | /* | |
194 | * Like svtcp_create(), except the routine takes any *open* UNIX file | |
195 | * descriptor as its first input. | |
196 | */ | |
197 | SVCXPRT * | |
e7fd8a39 | 198 | svcfd_create (int fd, u_int sendsize, u_int recvsize) |
28f540f4 | 199 | { |
e7fd8a39 | 200 | return makefd_xprt (fd, sendsize, recvsize); |
28f540f4 | 201 | } |
7b57bfe5 | 202 | libc_hidden_nolink (svcfd_create, GLIBC_2_0) |
28f540f4 RM |
203 | |
204 | static SVCXPRT * | |
dfd2257a | 205 | internal_function |
e7fd8a39 | 206 | makefd_xprt (int fd, u_int sendsize, u_int recvsize) |
28f540f4 | 207 | { |
e7fd8a39 UD |
208 | SVCXPRT *xprt; |
209 | struct tcp_conn *cd; | |
210 | ||
211 | xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT)); | |
e7fd8a39 | 212 | cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn)); |
51028f34 | 213 | if (xprt == (SVCXPRT *) NULL || cd == NULL) |
e7fd8a39 | 214 | { |
1d20f7f8 UD |
215 | (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt", |
216 | _("out of memory\n")); | |
2e3e5db6 UD |
217 | mem_free (xprt, sizeof (SVCXPRT)); |
218 | mem_free (cd, sizeof (struct tcp_conn)); | |
51028f34 | 219 | return NULL; |
e7fd8a39 UD |
220 | } |
221 | cd->strm_stat = XPRT_IDLE; | |
7b57bfe5 UD |
222 | xdrrec_create (&(cd->xdrs), sendsize, recvsize, |
223 | (caddr_t) xprt, readtcp, writetcp); | |
e7fd8a39 UD |
224 | xprt->xp_p2 = NULL; |
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 */ | |
230 | xprt->xp_sock = fd; | |
231 | xprt_register (xprt); | |
e7fd8a39 | 232 | return xprt; |
28f540f4 RM |
233 | } |
234 | ||
235 | static bool_t | |
e7fd8a39 | 236 | rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg) |
28f540f4 | 237 | { |
e7fd8a39 UD |
238 | int sock; |
239 | struct tcp_rendezvous *r; | |
240 | struct sockaddr_in addr; | |
41df5ed4 | 241 | socklen_t len; |
e7fd8a39 UD |
242 | |
243 | r = (struct tcp_rendezvous *) xprt->xp_p1; | |
244 | again: | |
245 | len = sizeof (struct sockaddr_in); | |
50304ef0 | 246 | if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0) |
e7fd8a39 UD |
247 | { |
248 | if (errno == EINTR) | |
249 | goto again; | |
250 | return FALSE; | |
251 | } | |
252 | /* | |
253 | * make a new transporter (re-uses xprt) | |
254 | */ | |
255 | xprt = makefd_xprt (sock, r->sendsize, r->recvsize); | |
c1301d9a | 256 | memcpy (&xprt->xp_raddr, &addr, sizeof (addr)); |
e7fd8a39 UD |
257 | xprt->xp_addrlen = len; |
258 | return FALSE; /* there is never an rpc msg to be processed */ | |
28f540f4 RM |
259 | } |
260 | ||
261 | static enum xprt_stat | |
e7fd8a39 | 262 | rendezvous_stat (SVCXPRT *xprt) |
28f540f4 | 263 | { |
e7fd8a39 | 264 | return XPRT_IDLE; |
28f540f4 RM |
265 | } |
266 | ||
267 | static void | |
e7fd8a39 | 268 | svctcp_destroy (SVCXPRT *xprt) |
28f540f4 | 269 | { |
e7fd8a39 UD |
270 | struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1; |
271 | ||
272 | xprt_unregister (xprt); | |
50304ef0 | 273 | (void) __close (xprt->xp_sock); |
e7fd8a39 UD |
274 | if (xprt->xp_port != 0) |
275 | { | |
276 | /* a rendezvouser socket */ | |
277 | xprt->xp_port = 0; | |
278 | } | |
279 | else | |
280 | { | |
281 | /* an actual connection socket */ | |
282 | XDR_DESTROY (&(cd->xdrs)); | |
283 | } | |
284 | mem_free ((caddr_t) cd, sizeof (struct tcp_conn)); | |
285 | mem_free ((caddr_t) xprt, sizeof (SVCXPRT)); | |
28f540f4 RM |
286 | } |
287 | ||
28f540f4 RM |
288 | |
289 | /* | |
6d52618b | 290 | * reads data from the tcp connection. |
28f540f4 RM |
291 | * any error is fatal and the connection is closed. |
292 | * (And a read of zero bytes is a half closed stream => error.) | |
293 | */ | |
294 | static int | |
e7fd8a39 | 295 | readtcp (char *xprtptr, char *buf, int len) |
28f540f4 | 296 | { |
e7fd8a39 UD |
297 | SVCXPRT *xprt = (SVCXPRT *)xprtptr; |
298 | int sock = xprt->xp_sock; | |
090ca000 UD |
299 | int milliseconds = 35 * 1000; |
300 | struct pollfd pollfd; | |
301 | ||
302 | do | |
e7fd8a39 | 303 | { |
090ca000 UD |
304 | pollfd.fd = sock; |
305 | pollfd.events = POLLIN; | |
306 | switch (__poll (&pollfd, 1, milliseconds)) | |
e7fd8a39 | 307 | { |
090ca000 | 308 | case -1: |
e7fd8a39 | 309 | if (errno == EINTR) |
fed8f7f7 | 310 | continue; |
090ca000 UD |
311 | /*FALLTHROUGH*/ |
312 | case 0: | |
e7fd8a39 | 313 | goto fatal_err; |
090ca000 | 314 | default: |
7b57bfe5 UD |
315 | if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP) |
316 | || (pollfd.revents & POLLNVAL)) | |
317 | goto fatal_err; | |
090ca000 | 318 | break; |
7d1de115 | 319 | } |
e7fd8a39 | 320 | } |
090ca000 | 321 | while ((pollfd.revents & POLLIN) == 0); |
fed8f7f7 | 322 | |
50304ef0 | 323 | if ((len = __read (sock, buf, len)) > 0) |
fed8f7f7 UD |
324 | return len; |
325 | ||
090ca000 | 326 | fatal_err: |
e7fd8a39 UD |
327 | ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED; |
328 | return -1; | |
28f540f4 RM |
329 | } |
330 | ||
331 | /* | |
332 | * writes data to the tcp connection. | |
333 | * Any error is fatal and the connection is closed. | |
334 | */ | |
335 | static int | |
e7fd8a39 | 336 | writetcp (char *xprtptr, char * buf, int len) |
28f540f4 | 337 | { |
e7fd8a39 UD |
338 | SVCXPRT *xprt = (SVCXPRT *)xprtptr; |
339 | int i, cnt; | |
340 | ||
341 | for (cnt = len; cnt > 0; cnt -= i, buf += i) | |
342 | { | |
50304ef0 | 343 | if ((i = __write (xprt->xp_sock, buf, cnt)) < 0) |
e7fd8a39 | 344 | { |
090ca000 UD |
345 | ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED; |
346 | return -1; | |
28f540f4 | 347 | } |
e7fd8a39 | 348 | } |
090ca000 | 349 | return len; |
28f540f4 RM |
350 | } |
351 | ||
352 | static enum xprt_stat | |
e7fd8a39 | 353 | svctcp_stat (SVCXPRT *xprt) |
28f540f4 | 354 | { |
e7fd8a39 UD |
355 | struct tcp_conn *cd = |
356 | (struct tcp_conn *) (xprt->xp_p1); | |
357 | ||
358 | if (cd->strm_stat == XPRT_DIED) | |
090ca000 | 359 | return XPRT_DIED; |
7b57bfe5 | 360 | if (!xdrrec_eof (&(cd->xdrs))) |
090ca000 UD |
361 | return XPRT_MOREREQS; |
362 | return XPRT_IDLE; | |
28f540f4 RM |
363 | } |
364 | ||
365 | static bool_t | |
090ca000 | 366 | svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg) |
28f540f4 | 367 | { |
090ca000 | 368 | struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1); |
e7fd8a39 UD |
369 | XDR *xdrs = &(cd->xdrs); |
370 | ||
371 | xdrs->x_op = XDR_DECODE; | |
7b57bfe5 UD |
372 | (void) xdrrec_skiprecord (xdrs); |
373 | if (xdr_callmsg (xdrs, msg)) | |
e7fd8a39 UD |
374 | { |
375 | cd->x_id = msg->rm_xid; | |
090ca000 | 376 | return TRUE; |
e7fd8a39 | 377 | } |
41aa20c2 | 378 | cd->strm_stat = XPRT_DIED; /* XXXX */ |
090ca000 | 379 | return FALSE; |
28f540f4 RM |
380 | } |
381 | ||
382 | static bool_t | |
090ca000 | 383 | svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) |
28f540f4 | 384 | { |
090ca000 UD |
385 | return ((*xdr_args) (&(((struct tcp_conn *) |
386 | (xprt->xp_p1))->xdrs), args_ptr)); | |
28f540f4 RM |
387 | } |
388 | ||
389 | static bool_t | |
090ca000 | 390 | svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) |
28f540f4 | 391 | { |
090ca000 | 392 | XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs); |
28f540f4 | 393 | |
e7fd8a39 UD |
394 | xdrs->x_op = XDR_FREE; |
395 | return ((*xdr_args) (xdrs, args_ptr)); | |
28f540f4 RM |
396 | } |
397 | ||
398 | static bool_t | |
090ca000 | 399 | svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg) |
28f540f4 | 400 | { |
090ca000 | 401 | struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1); |
e7fd8a39 UD |
402 | XDR *xdrs = &(cd->xdrs); |
403 | bool_t stat; | |
404 | ||
405 | xdrs->x_op = XDR_ENCODE; | |
406 | msg->rm_xid = cd->x_id; | |
7b57bfe5 UD |
407 | stat = xdr_replymsg (xdrs, msg); |
408 | (void) xdrrec_endofrecord (xdrs, TRUE); | |
090ca000 | 409 | return stat; |
28f540f4 | 410 | } |