1 /* @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
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.
9 *
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.
13 *
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.
17 *
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.
21 *
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.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30 #if 0
31 static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35 * svc_tcp.c, Server side for TCP/IP based RPC.
36 *
37 * Copyright (C) 1984, Sun Microsystems, Inc.
38 *
39 * Actually implements two flavors of transporter -
40 * a tcp rendezvouser (a listener and connection establisher)
41 * and a record/tcp stream.
42 */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include "rpc_private.h"
48 #include <sys/socket.h>
49 #include <sys/poll.h>
50 #include <errno.h>
51 #include <stdlib.h>
52
53 /*
54 * Ops vector for TCP/IP based rpc service handle
55 */
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 *);
62
63 static const struct xp_ops svctcp_op =
64 {
65 svctcp_recv,
66 svctcp_stat,
67 svctcp_getargs,
68 svctcp_reply,
69 svctcp_freeargs,
70 svctcp_destroy
71 };
72
73 /*
74 * Ops vector for TCP/IP rendezvous handler
75 */
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;
79
80 /* This function makes sure abort() relocation goes through PLT
81 and thus can be lazy bound. */
82 static void
svctcp_rendezvous_abort(void)83 svctcp_rendezvous_abort (void)
84 {
85 abort ();
86 };
87
88 static const struct xp_ops svctcp_rendezvous_op =
89 {
90 rendezvous_request,
91 rendezvous_stat,
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,
95 svctcp_destroy
96 };
97
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;
101
102 struct tcp_rendezvous
103 { /* kept in xprt->xp_p1 */
104 u_int sendsize;
105 u_int recvsize;
106 };
107
108 struct tcp_conn
109 { /* kept in xprt->xp_p1 */
110 enum xprt_stat strm_stat;
111 u_long x_id;
112 XDR xdrs;
113 char verf_body[MAX_AUTH_BYTES];
114 };
115
116 /*
117 * Usage:
118 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
119 *
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.
124 *
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.
131 *
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.
135 */
136 SVCXPRT *
svctcp_create(int sock,u_int sendsize,u_int recvsize)137 svctcp_create (int sock, u_int sendsize, u_int recvsize)
138 {
139 bool_t madesock = FALSE;
140 SVCXPRT *xprt;
141 struct tcp_rendezvous *r;
142 struct sockaddr_in addr;
143 socklen_t len = sizeof (struct sockaddr_in);
144
145 if (sock == RPC_ANYSOCK)
146 {
147 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
148 {
149 perror (_("svc_tcp.c - tcp socket creation problem"));
150 return (SVCXPRT *) NULL;
151 }
152 madesock = TRUE;
153 }
154 memset ((char *) &addr, 0, sizeof (addr));
155 addr.sin_family = AF_INET;
156 if (bindresvport (sock, &addr))
157 {
158 addr.sin_port = 0;
159 (void) bind (sock, (struct sockaddr *) &addr, len);
160 }
161 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
162 (listen (sock, 2) != 0))
163 {
164 perror (_("svc_tcp.c - cannot getsockname or listen"));
165 if (madesock)
166 (void) close (sock);
167 return (SVCXPRT *) NULL;
168 }
169 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
170 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
171 if (r == NULL || xprt == NULL)
172 {
173 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
174 mem_free (r, sizeof (*r));
175 mem_free (xprt, sizeof (SVCXPRT));
176 return NULL;
177 }
178 r->sendsize = sendsize;
179 r->recvsize = recvsize;
180 xprt->xp_p2 = NULL;
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);
187 return xprt;
188 }
189
190 /*
191 * Like svtcp_create(), except the routine takes any *open* UNIX file
192 * descriptor as its first input.
193 */
194 SVCXPRT *
195 svcfd_create (int fd, u_int sendsize, u_int recvsize);
196 SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)197 svcfd_create (int fd, u_int sendsize, u_int recvsize)
198 {
199 return makefd_xprt (fd, sendsize, recvsize);
200 }
201
202 static SVCXPRT *
203 internal_function
makefd_xprt(int fd,u_int sendsize,u_int recvsize)204 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
205 {
206 SVCXPRT *xprt;
207 struct tcp_conn *cd;
208
209 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
210 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
211 if (xprt == (SVCXPRT *) NULL || cd == NULL)
212 {
213 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
214 mem_free (xprt, sizeof (SVCXPRT));
215 mem_free (cd, sizeof (struct tcp_conn));
216 return NULL;
217 }
218 cd->strm_stat = XPRT_IDLE;
219 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
220 (caddr_t) xprt, readtcp, writetcp);
221 xprt->xp_p2 = NULL;
222 xprt->xp_p1 = (caddr_t) cd;
223 xprt->xp_verf.oa_base = cd->verf_body;
224 xprt->xp_addrlen = 0;
225 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
226 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
227 xprt->xp_sock = fd;
228 xprt_register (xprt);
229 return xprt;
230 }
231
232 static bool_t
rendezvous_request(SVCXPRT * xprt,struct rpc_msg * errmsg attribute_unused)233 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg attribute_unused)
234 {
235 int sock;
236 struct tcp_rendezvous *r;
237 struct sockaddr_in addr;
238 socklen_t len;
239
240 r = (struct tcp_rendezvous *) xprt->xp_p1;
241 again:
242 len = sizeof (struct sockaddr_in);
243 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
244 {
245 if (errno == EINTR)
246 goto again;
247 return FALSE;
248 }
249 /*
250 * make a new transporter (re-uses xprt)
251 */
252 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
253 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
254 xprt->xp_addrlen = len;
255 return FALSE; /* there is never an rpc msg to be processed */
256 }
257
258 static enum xprt_stat
rendezvous_stat(SVCXPRT * xprt attribute_unused)259 rendezvous_stat (SVCXPRT *xprt attribute_unused)
260 {
261 return XPRT_IDLE;
262 }
263
264 static void
svctcp_destroy(SVCXPRT * xprt)265 svctcp_destroy (SVCXPRT *xprt)
266 {
267 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
268
269 xprt_unregister (xprt);
270 (void) close (xprt->xp_sock);
271 if (xprt->xp_port != 0)
272 {
273 /* a rendezvouser socket */
274 xprt->xp_port = 0;
275 }
276 else
277 {
278 /* an actual connection socket */
279 XDR_DESTROY (&(cd->xdrs));
280 }
281 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
282 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
283 }
284
285
286 /*
287 * reads data from the tcp connection.
288 * any error is fatal and the connection is closed.
289 * (And a read of zero bytes is a half closed stream => error.)
290 */
291 static int
readtcp(char * xprtptr,char * buf,int len)292 readtcp (char *xprtptr, char *buf, int len)
293 {
294 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
295 int sock = xprt->xp_sock;
296 int milliseconds = 35 * 1000;
297 struct pollfd pollfd;
298
299 do
300 {
301 pollfd.fd = sock;
302 pollfd.events = POLLIN;
303 switch (poll (&pollfd, 1, milliseconds))
304 {
305 case -1:
306 if (errno == EINTR)
307 continue;
308 /*FALLTHROUGH*/
309 case 0:
310 goto fatal_err;
311 default:
312 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
313 || (pollfd.revents & POLLNVAL))
314 goto fatal_err;
315 break;
316 }
317 }
318 while ((pollfd.revents & POLLIN) == 0);
319
320 if ((len = read (sock, buf, len)) > 0)
321 return len;
322
323 fatal_err:
324 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
325 return -1;
326 }
327
328 /*
329 * writes data to the tcp connection.
330 * Any error is fatal and the connection is closed.
331 */
332 static int
writetcp(char * xprtptr,char * buf,int len)333 writetcp (char *xprtptr, char * buf, int len)
334 {
335 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
336 int i, cnt;
337
338 for (cnt = len; cnt > 0; cnt -= i, buf += i)
339 {
340 if ((i = write (xprt->xp_sock, buf, cnt)) < 0)
341 {
342 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
343 return -1;
344 }
345 }
346 return len;
347 }
348
349 static enum xprt_stat
svctcp_stat(SVCXPRT * xprt)350 svctcp_stat (SVCXPRT *xprt)
351 {
352 struct tcp_conn *cd =
353 (struct tcp_conn *) (xprt->xp_p1);
354
355 if (cd->strm_stat == XPRT_DIED)
356 return XPRT_DIED;
357 if (!xdrrec_eof (&(cd->xdrs)))
358 return XPRT_MOREREQS;
359 return XPRT_IDLE;
360 }
361
362 static bool_t
svctcp_recv(SVCXPRT * xprt,struct rpc_msg * msg)363 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
364 {
365 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
366 XDR *xdrs = &(cd->xdrs);
367
368 xdrs->x_op = XDR_DECODE;
369 (void) xdrrec_skiprecord (xdrs);
370 if (xdr_callmsg (xdrs, msg))
371 {
372 cd->x_id = msg->rm_xid;
373 return TRUE;
374 }
375 cd->strm_stat = XPRT_DIED; /* XXXX */
376 return FALSE;
377 }
378
379 static bool_t
svctcp_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)380 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
381 {
382 return ((*xdr_args) (&(((struct tcp_conn *)
383 (xprt->xp_p1))->xdrs), args_ptr));
384 }
385
386 static bool_t
svctcp_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)387 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
388 {
389 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
390
391 xdrs->x_op = XDR_FREE;
392 return ((*xdr_args) (xdrs, args_ptr));
393 }
394
395 static bool_t
svctcp_reply(SVCXPRT * xprt,struct rpc_msg * msg)396 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
397 {
398 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
399 XDR *xdrs = &(cd->xdrs);
400 bool_t stat;
401
402 xdrs->x_op = XDR_ENCODE;
403 msg->rm_xid = cd->x_id;
404 stat = xdr_replymsg (xdrs, msg);
405 (void) xdrrec_endofrecord (xdrs, TRUE);
406 return stat;
407 }
408