| 1 | /* |
| 2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
| 3 | * unrestricted use provided that this legend is included on all tape |
| 4 | * media and as a part of the software program in whole or part. Users |
| 5 | * may copy or modify Sun RPC without charge, but are not authorized |
| 6 | * to license or distribute it to anyone else except as part of a product or |
| 7 | * program developed by the user. |
| 8 | * |
| 9 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
| 10 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
| 11 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
| 12 | * |
| 13 | * Sun RPC is provided with no support and without any obligation on the |
| 14 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
| 15 | * modification or enhancement. |
| 16 | * |
| 17 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
| 18 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
| 19 | * OR ANY PART THEREOF. |
| 20 | * |
| 21 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
| 22 | * or profits or other special, indirect and consequential damages, even if |
| 23 | * Sun has been advised of the possibility of such damages. |
| 24 | * |
| 25 | * Sun Microsystems, Inc. |
| 26 | * 2550 Garcia Avenue |
| 27 | * Mountain View, California 94043 |
| 28 | */ |
| 29 | |
| 30 | #if defined(LIBC_SCCS) && !defined(lint) |
| 31 | /*static char *sccsid = "from: @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/ |
| 32 | /*static char *sccsid = "from: @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/ |
| 33 | static char *rcsid = "$Id: svc_tcp.c,v 1.3 1993/08/26 00:53:44 jtc Exp $"; |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * svc_tcp.c, Server side for TCP/IP based RPC. |
| 38 | * |
| 39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
| 40 | * |
| 41 | * Actually implements two flavors of transporter - |
| 42 | * a tcp rendezvouser (a listner and connection establisher) |
| 43 | * and a record/tcp stream. |
| 44 | */ |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include <rpc/rpc.h> |
| 48 | #include <sys/socket.h> |
| 49 | #include <errno.h> |
| 50 | extern bool_t abort(); |
| 51 | extern errno; |
| 52 | |
| 53 | /* |
| 54 | * Ops vector for TCP/IP based rpc service handle |
| 55 | */ |
| 56 | static bool_t svctcp_recv(); |
| 57 | static enum xprt_stat svctcp_stat(); |
| 58 | static bool_t svctcp_getargs(); |
| 59 | static bool_t svctcp_reply(); |
| 60 | static bool_t svctcp_freeargs(); |
| 61 | static void svctcp_destroy(); |
| 62 | |
| 63 | static struct xp_ops svctcp_op = { |
| 64 | svctcp_recv, |
| 65 | svctcp_stat, |
| 66 | svctcp_getargs, |
| 67 | svctcp_reply, |
| 68 | svctcp_freeargs, |
| 69 | svctcp_destroy |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * Ops vector for TCP/IP rendezvous handler |
| 74 | */ |
| 75 | static bool_t rendezvous_request(); |
| 76 | static enum xprt_stat rendezvous_stat(); |
| 77 | |
| 78 | static struct xp_ops svctcp_rendezvous_op = { |
| 79 | rendezvous_request, |
| 80 | rendezvous_stat, |
| 81 | abort, |
| 82 | abort, |
| 83 | abort, |
| 84 | svctcp_destroy |
| 85 | }; |
| 86 | |
| 87 | static int readtcp(), writetcp(); |
| 88 | static SVCXPRT *makefd_xprt(); |
| 89 | |
| 90 | struct tcp_rendezvous { /* kept in xprt->xp_p1 */ |
| 91 | u_int sendsize; |
| 92 | u_int recvsize; |
| 93 | }; |
| 94 | |
| 95 | struct tcp_conn { /* kept in xprt->xp_p1 */ |
| 96 | enum xprt_stat strm_stat; |
| 97 | u_long x_id; |
| 98 | XDR xdrs; |
| 99 | char verf_body[MAX_AUTH_BYTES]; |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * Usage: |
| 104 | * xprt = svctcp_create(sock, send_buf_size, recv_buf_size); |
| 105 | * |
| 106 | * Creates, registers, and returns a (rpc) tcp based transporter. |
| 107 | * Once *xprt is initialized, it is registered as a transporter |
| 108 | * see (svc.h, xprt_register). This routine returns |
| 109 | * a NULL if a problem occurred. |
| 110 | * |
| 111 | * If sock<0 then a socket is created, else sock is used. |
| 112 | * If the socket, sock is not bound to a port then svctcp_create |
| 113 | * binds it to an arbitrary port. The routine then starts a tcp |
| 114 | * listener on the socket's associated port. In any (successful) case, |
| 115 | * xprt->xp_sock is the registered socket number and xprt->xp_port is the |
| 116 | * associated port number. |
| 117 | * |
| 118 | * Since tcp streams do buffered io similar to stdio, the caller can specify |
| 119 | * how big the send and receive buffers are via the second and third parms; |
| 120 | * 0 => use the system default. |
| 121 | */ |
| 122 | SVCXPRT * |
| 123 | svctcp_create(sock, sendsize, recvsize) |
| 124 | register int sock; |
| 125 | u_int sendsize; |
| 126 | u_int recvsize; |
| 127 | { |
| 128 | bool_t madesock = FALSE; |
| 129 | register SVCXPRT *xprt; |
| 130 | register struct tcp_rendezvous *r; |
| 131 | struct sockaddr_in addr; |
| 132 | int len = sizeof(struct sockaddr_in); |
| 133 | |
| 134 | if (sock == RPC_ANYSOCK) { |
| 135 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
| 136 | perror("svctcp_.c - udp socket creation problem"); |
| 137 | return ((SVCXPRT *)NULL); |
| 138 | } |
| 139 | madesock = TRUE; |
| 140 | } |
| 141 | bzero((char *)&addr, sizeof (addr)); |
| 142 | addr.sin_family = AF_INET; |
| 143 | if (bindresvport(sock, &addr)) { |
| 144 | addr.sin_port = 0; |
| 145 | (void)bind(sock, (struct sockaddr *)&addr, len); |
| 146 | } |
| 147 | if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) || |
| 148 | (listen(sock, 2) != 0)) { |
| 149 | perror("svctcp_.c - cannot getsockname or listen"); |
| 150 | if (madesock) |
| 151 | (void)close(sock); |
| 152 | return ((SVCXPRT *)NULL); |
| 153 | } |
| 154 | r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r)); |
| 155 | if (r == NULL) { |
| 156 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); |
| 157 | return (NULL); |
| 158 | } |
| 159 | r->sendsize = sendsize; |
| 160 | r->recvsize = recvsize; |
| 161 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); |
| 162 | if (xprt == NULL) { |
| 163 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); |
| 164 | return (NULL); |
| 165 | } |
| 166 | xprt->xp_p2 = NULL; |
| 167 | xprt->xp_p1 = (caddr_t)r; |
| 168 | xprt->xp_verf = _null_auth; |
| 169 | xprt->xp_ops = &svctcp_rendezvous_op; |
| 170 | xprt->xp_port = ntohs(addr.sin_port); |
| 171 | xprt->xp_sock = sock; |
| 172 | xprt_register(xprt); |
| 173 | return (xprt); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Like svtcp_create(), except the routine takes any *open* UNIX file |
| 178 | * descriptor as its first input. |
| 179 | */ |
| 180 | SVCXPRT * |
| 181 | svcfd_create(fd, sendsize, recvsize) |
| 182 | int fd; |
| 183 | u_int sendsize; |
| 184 | u_int recvsize; |
| 185 | { |
| 186 | |
| 187 | return (makefd_xprt(fd, sendsize, recvsize)); |
| 188 | } |
| 189 | |
| 190 | static SVCXPRT * |
| 191 | makefd_xprt(fd, sendsize, recvsize) |
| 192 | int fd; |
| 193 | u_int sendsize; |
| 194 | u_int recvsize; |
| 195 | { |
| 196 | register SVCXPRT *xprt; |
| 197 | register struct tcp_conn *cd; |
| 198 | |
| 199 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); |
| 200 | if (xprt == (SVCXPRT *)NULL) { |
| 201 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); |
| 202 | goto done; |
| 203 | } |
| 204 | cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn)); |
| 205 | if (cd == (struct tcp_conn *)NULL) { |
| 206 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); |
| 207 | mem_free((char *) xprt, sizeof(SVCXPRT)); |
| 208 | xprt = (SVCXPRT *)NULL; |
| 209 | goto done; |
| 210 | } |
| 211 | cd->strm_stat = XPRT_IDLE; |
| 212 | xdrrec_create(&(cd->xdrs), sendsize, recvsize, |
| 213 | (caddr_t)xprt, readtcp, writetcp); |
| 214 | xprt->xp_p2 = NULL; |
| 215 | xprt->xp_p1 = (caddr_t)cd; |
| 216 | xprt->xp_verf.oa_base = cd->verf_body; |
| 217 | xprt->xp_addrlen = 0; |
| 218 | xprt->xp_ops = &svctcp_op; /* truely deals with calls */ |
| 219 | xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ |
| 220 | xprt->xp_sock = fd; |
| 221 | xprt_register(xprt); |
| 222 | done: |
| 223 | return (xprt); |
| 224 | } |
| 225 | |
| 226 | static bool_t |
| 227 | rendezvous_request(xprt) |
| 228 | register SVCXPRT *xprt; |
| 229 | { |
| 230 | int sock; |
| 231 | struct tcp_rendezvous *r; |
| 232 | struct sockaddr_in addr; |
| 233 | int len; |
| 234 | |
| 235 | r = (struct tcp_rendezvous *)xprt->xp_p1; |
| 236 | again: |
| 237 | len = sizeof(struct sockaddr_in); |
| 238 | if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr, |
| 239 | &len)) < 0) { |
| 240 | if (errno == EINTR) |
| 241 | goto again; |
| 242 | return (FALSE); |
| 243 | } |
| 244 | /* |
| 245 | * make a new transporter (re-uses xprt) |
| 246 | */ |
| 247 | xprt = makefd_xprt(sock, r->sendsize, r->recvsize); |
| 248 | xprt->xp_raddr = addr; |
| 249 | xprt->xp_addrlen = len; |
| 250 | return (FALSE); /* there is never an rpc msg to be processed */ |
| 251 | } |
| 252 | |
| 253 | static enum xprt_stat |
| 254 | rendezvous_stat() |
| 255 | { |
| 256 | |
| 257 | return (XPRT_IDLE); |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | svctcp_destroy(xprt) |
| 262 | register SVCXPRT *xprt; |
| 263 | { |
| 264 | register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1; |
| 265 | |
| 266 | xprt_unregister(xprt); |
| 267 | (void)close(xprt->xp_sock); |
| 268 | if (xprt->xp_port != 0) { |
| 269 | /* a rendezvouser socket */ |
| 270 | xprt->xp_port = 0; |
| 271 | } else { |
| 272 | /* an actual connection socket */ |
| 273 | XDR_DESTROY(&(cd->xdrs)); |
| 274 | } |
| 275 | mem_free((caddr_t)cd, sizeof(struct tcp_conn)); |
| 276 | mem_free((caddr_t)xprt, sizeof(SVCXPRT)); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * All read operations timeout after 35 seconds. |
| 281 | * A timeout is fatal for the connection. |
| 282 | */ |
| 283 | static struct timeval wait_per_try = { 35, 0 }; |
| 284 | |
| 285 | /* |
| 286 | * reads data from the tcp conection. |
| 287 | * any error is fatal and the connection is closed. |
| 288 | * (And a read of zero bytes is a half closed stream => error.) |
| 289 | */ |
| 290 | static int |
| 291 | readtcp(xprt, buf, len) |
| 292 | register SVCXPRT *xprt; |
| 293 | caddr_t buf; |
| 294 | register int len; |
| 295 | { |
| 296 | register int sock = xprt->xp_sock; |
| 297 | #ifdef FD_SETSIZE |
| 298 | fd_set mask; |
| 299 | fd_set readfds; |
| 300 | |
| 301 | FD_ZERO(&mask); |
| 302 | FD_SET(sock, &mask); |
| 303 | #else |
| 304 | register int mask = 1 << sock; |
| 305 | int readfds; |
| 306 | #endif /* def FD_SETSIZE */ |
| 307 | do { |
| 308 | readfds = mask; |
| 309 | if (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL, |
| 310 | &wait_per_try) <= 0) { |
| 311 | if (errno == EINTR) { |
| 312 | continue; |
| 313 | } |
| 314 | goto fatal_err; |
| 315 | } |
| 316 | #ifdef FD_SETSIZE |
| 317 | } while (!FD_ISSET(sock, &readfds)); |
| 318 | #else |
| 319 | } while (readfds != mask); |
| 320 | #endif /* def FD_SETSIZE */ |
| 321 | if ((len = read(sock, buf, len)) > 0) { |
| 322 | return (len); |
| 323 | } |
| 324 | fatal_err: |
| 325 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; |
| 326 | return (-1); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * writes data to the tcp connection. |
| 331 | * Any error is fatal and the connection is closed. |
| 332 | */ |
| 333 | static int |
| 334 | writetcp(xprt, buf, len) |
| 335 | register SVCXPRT *xprt; |
| 336 | caddr_t buf; |
| 337 | int len; |
| 338 | { |
| 339 | register int i, cnt; |
| 340 | |
| 341 | for (cnt = len; cnt > 0; cnt -= i, buf += i) { |
| 342 | if ((i = write(xprt->xp_sock, buf, cnt)) < 0) { |
| 343 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = |
| 344 | XPRT_DIED; |
| 345 | return (-1); |
| 346 | } |
| 347 | } |
| 348 | return (len); |
| 349 | } |
| 350 | |
| 351 | static enum xprt_stat |
| 352 | svctcp_stat(xprt) |
| 353 | SVCXPRT *xprt; |
| 354 | { |
| 355 | register struct tcp_conn *cd = |
| 356 | (struct tcp_conn *)(xprt->xp_p1); |
| 357 | |
| 358 | if (cd->strm_stat == XPRT_DIED) |
| 359 | return (XPRT_DIED); |
| 360 | if (! xdrrec_eof(&(cd->xdrs))) |
| 361 | return (XPRT_MOREREQS); |
| 362 | return (XPRT_IDLE); |
| 363 | } |
| 364 | |
| 365 | static bool_t |
| 366 | svctcp_recv(xprt, msg) |
| 367 | SVCXPRT *xprt; |
| 368 | register struct rpc_msg *msg; |
| 369 | { |
| 370 | register struct tcp_conn *cd = |
| 371 | (struct tcp_conn *)(xprt->xp_p1); |
| 372 | register XDR *xdrs = &(cd->xdrs); |
| 373 | |
| 374 | xdrs->x_op = XDR_DECODE; |
| 375 | (void)xdrrec_skiprecord(xdrs); |
| 376 | if (xdr_callmsg(xdrs, msg)) { |
| 377 | cd->x_id = msg->rm_xid; |
| 378 | return (TRUE); |
| 379 | } |
| 380 | return (FALSE); |
| 381 | } |
| 382 | |
| 383 | static bool_t |
| 384 | svctcp_getargs(xprt, xdr_args, args_ptr) |
| 385 | SVCXPRT *xprt; |
| 386 | xdrproc_t xdr_args; |
| 387 | caddr_t args_ptr; |
| 388 | { |
| 389 | |
| 390 | return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr)); |
| 391 | } |
| 392 | |
| 393 | static bool_t |
| 394 | svctcp_freeargs(xprt, xdr_args, args_ptr) |
| 395 | SVCXPRT *xprt; |
| 396 | xdrproc_t xdr_args; |
| 397 | caddr_t args_ptr; |
| 398 | { |
| 399 | register XDR *xdrs = |
| 400 | &(((struct tcp_conn *)(xprt->xp_p1))->xdrs); |
| 401 | |
| 402 | xdrs->x_op = XDR_FREE; |
| 403 | return ((*xdr_args)(xdrs, args_ptr)); |
| 404 | } |
| 405 | |
| 406 | static bool_t |
| 407 | svctcp_reply(xprt, msg) |
| 408 | SVCXPRT *xprt; |
| 409 | register struct rpc_msg *msg; |
| 410 | { |
| 411 | register struct tcp_conn *cd = |
| 412 | (struct tcp_conn *)(xprt->xp_p1); |
| 413 | register XDR *xdrs = &(cd->xdrs); |
| 414 | register bool_t stat; |
| 415 | |
| 416 | xdrs->x_op = XDR_ENCODE; |
| 417 | msg->rm_xid = cd->x_id; |
| 418 | stat = xdr_replymsg(xdrs, msg); |
| 419 | (void)xdrrec_endofrecord(xdrs, TRUE); |
| 420 | return (stat); |
| 421 | } |