Back to index

glibc  2.9
svc_tcp.c
Go to the documentation of this file.
00001 /* @(#)svc_tcp.c     2.2 88/08/01 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
00032 #endif
00033 
00034 /*
00035  * svc_tcp.c, Server side for TCP/IP based RPC.
00036  *
00037  * Copyright (C) 1984, Sun Microsystems, Inc.
00038  *
00039  * Actually implements two flavors of transporter -
00040  * a tcp rendezvouser (a listener and connection establisher)
00041  * and a record/tcp stream.
00042  */
00043 
00044 #include <stdio.h>
00045 #include <unistd.h>
00046 #include <string.h>
00047 #include <libintl.h>
00048 #include <rpc/rpc.h>
00049 #include <sys/socket.h>
00050 #include <sys/poll.h>
00051 #include <errno.h>
00052 #include <stdlib.h>
00053 
00054 #ifdef USE_IN_LIBIO
00055 # include <wchar.h>
00056 # include <libio/iolibio.h>
00057 #endif
00058 
00059 /*
00060  * Ops vector for TCP/IP based rpc service handle
00061  */
00062 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
00063 static enum xprt_stat svctcp_stat (SVCXPRT *);
00064 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
00065 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
00066 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
00067 static void svctcp_destroy (SVCXPRT *);
00068 
00069 static const struct xp_ops svctcp_op =
00070 {
00071   svctcp_recv,
00072   svctcp_stat,
00073   svctcp_getargs,
00074   svctcp_reply,
00075   svctcp_freeargs,
00076   svctcp_destroy
00077 };
00078 
00079 /*
00080  * Ops vector for TCP/IP rendezvous handler
00081  */
00082 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
00083 static enum xprt_stat rendezvous_stat (SVCXPRT *);
00084 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
00085 
00086 /* This function makes sure abort() relocation goes through PLT
00087    and thus can be lazy bound.  */
00088 static void
00089 svctcp_rendezvous_abort (void)
00090 {
00091   abort ();
00092 };
00093 
00094 static const struct xp_ops svctcp_rendezvous_op =
00095 {
00096   rendezvous_request,
00097   rendezvous_stat,
00098   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
00099   (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
00100   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
00101   svctcp_destroy
00102 };
00103 
00104 static int readtcp (char*, char *, int);
00105 static int writetcp (char *, char *, int);
00106 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
00107 
00108 struct tcp_rendezvous
00109   {                         /* kept in xprt->xp_p1 */
00110     u_int sendsize;
00111     u_int recvsize;
00112   };
00113 
00114 struct tcp_conn
00115   {                         /* kept in xprt->xp_p1 */
00116     enum xprt_stat strm_stat;
00117     u_long x_id;
00118     XDR xdrs;
00119     char verf_body[MAX_AUTH_BYTES];
00120   };
00121 
00122 /*
00123  * Usage:
00124  *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
00125  *
00126  * Creates, registers, and returns a (rpc) tcp based transporter.
00127  * Once *xprt is initialized, it is registered as a transporter
00128  * see (svc.h, xprt_register).  This routine returns
00129  * a NULL if a problem occurred.
00130  *
00131  * If sock<0 then a socket is created, else sock is used.
00132  * If the socket, sock is not bound to a port then svctcp_create
00133  * binds it to an arbitrary port.  The routine then starts a tcp
00134  * listener on the socket's associated port.  In any (successful) case,
00135  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
00136  * associated port number.
00137  *
00138  * Since tcp streams do buffered io similar to stdio, the caller can specify
00139  * how big the send and receive buffers are via the second and third parms;
00140  * 0 => use the system default.
00141  */
00142 SVCXPRT *
00143 svctcp_create (int sock, u_int sendsize, u_int recvsize)
00144 {
00145   bool_t madesock = FALSE;
00146   SVCXPRT *xprt;
00147   struct tcp_rendezvous *r;
00148   struct sockaddr_in addr;
00149   socklen_t len = sizeof (struct sockaddr_in);
00150 
00151   if (sock == RPC_ANYSOCK)
00152     {
00153       if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
00154        {
00155          perror (_("svc_tcp.c - tcp socket creation problem"));
00156          return (SVCXPRT *) NULL;
00157        }
00158       madesock = TRUE;
00159     }
00160   __bzero ((char *) &addr, sizeof (addr));
00161   addr.sin_family = AF_INET;
00162   if (bindresvport (sock, &addr))
00163     {
00164       addr.sin_port = 0;
00165       (void) __bind (sock, (struct sockaddr *) &addr, len);
00166     }
00167   if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
00168       (__listen (sock, SOMAXCONN) != 0))
00169     {
00170       perror (_("svc_tcp.c - cannot getsockname or listen"));
00171       if (madesock)
00172        (void) __close (sock);
00173       return (SVCXPRT *) NULL;
00174     }
00175   r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
00176   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
00177   if (r == NULL || xprt == NULL)
00178     {
00179       (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
00180       mem_free (r, sizeof (*r));
00181       mem_free (xprt, sizeof (SVCXPRT));
00182       return NULL;
00183     }
00184   r->sendsize = sendsize;
00185   r->recvsize = recvsize;
00186   xprt->xp_p2 = NULL;
00187   xprt->xp_p1 = (caddr_t) r;
00188   xprt->xp_verf = _null_auth;
00189   xprt->xp_ops = &svctcp_rendezvous_op;
00190   xprt->xp_port = ntohs (addr.sin_port);
00191   xprt->xp_sock = sock;
00192   xprt_register (xprt);
00193   return xprt;
00194 }
00195 
00196 /*
00197  * Like svtcp_create(), except the routine takes any *open* UNIX file
00198  * descriptor as its first input.
00199  */
00200 SVCXPRT *
00201 svcfd_create (int fd, u_int sendsize, u_int recvsize)
00202 {
00203   return makefd_xprt (fd, sendsize, recvsize);
00204 }
00205 
00206 static SVCXPRT *
00207 internal_function
00208 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
00209 {
00210   SVCXPRT *xprt;
00211   struct tcp_conn *cd;
00212 
00213   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
00214   cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
00215   if (xprt == (SVCXPRT *) NULL || cd == NULL)
00216     {
00217       (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
00218                       _("out of memory\n"));
00219       mem_free (xprt, sizeof (SVCXPRT));
00220       mem_free (cd, sizeof (struct tcp_conn));
00221       return NULL;
00222     }
00223   cd->strm_stat = XPRT_IDLE;
00224   INTUSE(xdrrec_create) (&(cd->xdrs), sendsize, recvsize,
00225                       (caddr_t) xprt, readtcp, writetcp);
00226   xprt->xp_p2 = NULL;
00227   xprt->xp_p1 = (caddr_t) cd;
00228   xprt->xp_verf.oa_base = cd->verf_body;
00229   xprt->xp_addrlen = 0;
00230   xprt->xp_ops = &svctcp_op;       /* truly deals with calls */
00231   xprt->xp_port = 0;        /* this is a connection, not a rendezvouser */
00232   xprt->xp_sock = fd;
00233   xprt_register (xprt);
00234   return xprt;
00235 }
00236 
00237 static bool_t
00238 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
00239 {
00240   int sock;
00241   struct tcp_rendezvous *r;
00242   struct sockaddr_in addr;
00243   socklen_t len;
00244 
00245   r = (struct tcp_rendezvous *) xprt->xp_p1;
00246 again:
00247   len = sizeof (struct sockaddr_in);
00248   if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
00249     {
00250       if (errno == EINTR)
00251        goto again;
00252       return FALSE;
00253     }
00254   /*
00255    * make a new transporter (re-uses xprt)
00256    */
00257   xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
00258   memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
00259   xprt->xp_addrlen = len;
00260   return FALSE;             /* there is never an rpc msg to be processed */
00261 }
00262 
00263 static enum xprt_stat
00264 rendezvous_stat (SVCXPRT *xprt)
00265 {
00266   return XPRT_IDLE;
00267 }
00268 
00269 static void
00270 svctcp_destroy (SVCXPRT *xprt)
00271 {
00272   struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
00273 
00274   xprt_unregister (xprt);
00275   (void) __close (xprt->xp_sock);
00276   if (xprt->xp_port != 0)
00277     {
00278       /* a rendezvouser socket */
00279       xprt->xp_port = 0;
00280     }
00281   else
00282     {
00283       /* an actual connection socket */
00284       XDR_DESTROY (&(cd->xdrs));
00285     }
00286   mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
00287   mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
00288 }
00289 
00290 
00291 /*
00292  * reads data from the tcp connection.
00293  * any error is fatal and the connection is closed.
00294  * (And a read of zero bytes is a half closed stream => error.)
00295  */
00296 static int
00297 readtcp (char *xprtptr, char *buf, int len)
00298 {
00299   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
00300   int sock = xprt->xp_sock;
00301   int milliseconds = 35 * 1000;
00302   struct pollfd pollfd;
00303 
00304   do
00305     {
00306       pollfd.fd = sock;
00307       pollfd.events = POLLIN;
00308       switch (__poll (&pollfd, 1, milliseconds))
00309        {
00310        case -1:
00311          if (errno == EINTR)
00312            continue;
00313          /*FALLTHROUGH*/
00314        case 0:
00315          goto fatal_err;
00316        default:
00317           if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
00318               || (pollfd.revents & POLLNVAL))
00319             goto fatal_err;
00320          break;
00321        }
00322     }
00323   while ((pollfd.revents & POLLIN) == 0);
00324 
00325   if ((len = __read (sock, buf, len)) > 0)
00326     return len;
00327 
00328  fatal_err:
00329   ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
00330   return -1;
00331 }
00332 
00333 /*
00334  * writes data to the tcp connection.
00335  * Any error is fatal and the connection is closed.
00336  */
00337 static int
00338 writetcp (char *xprtptr, char * buf, int len)
00339 {
00340   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
00341   int i, cnt;
00342 
00343   for (cnt = len; cnt > 0; cnt -= i, buf += i)
00344     {
00345       if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
00346        {
00347          ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
00348          return -1;
00349        }
00350     }
00351   return len;
00352 }
00353 
00354 static enum xprt_stat
00355 svctcp_stat (SVCXPRT *xprt)
00356 {
00357   struct tcp_conn *cd =
00358   (struct tcp_conn *) (xprt->xp_p1);
00359 
00360   if (cd->strm_stat == XPRT_DIED)
00361     return XPRT_DIED;
00362   if (!INTUSE(xdrrec_eof) (&(cd->xdrs)))
00363     return XPRT_MOREREQS;
00364   return XPRT_IDLE;
00365 }
00366 
00367 static bool_t
00368 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
00369 {
00370   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
00371   XDR *xdrs = &(cd->xdrs);
00372 
00373   xdrs->x_op = XDR_DECODE;
00374   (void) INTUSE(xdrrec_skiprecord) (xdrs);
00375   if (INTUSE(xdr_callmsg) (xdrs, msg))
00376     {
00377       cd->x_id = msg->rm_xid;
00378       return TRUE;
00379     }
00380   cd->strm_stat = XPRT_DIED;       /* XXXX */
00381   return FALSE;
00382 }
00383 
00384 static bool_t
00385 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
00386 {
00387   return ((*xdr_args) (&(((struct tcp_conn *)
00388                        (xprt->xp_p1))->xdrs), args_ptr));
00389 }
00390 
00391 static bool_t
00392 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
00393 {
00394   XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
00395 
00396   xdrs->x_op = XDR_FREE;
00397   return ((*xdr_args) (xdrs, args_ptr));
00398 }
00399 
00400 static bool_t
00401 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
00402 {
00403   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
00404   XDR *xdrs = &(cd->xdrs);
00405   bool_t stat;
00406 
00407   xdrs->x_op = XDR_ENCODE;
00408   msg->rm_xid = cd->x_id;
00409   stat = INTUSE(xdr_replymsg) (xdrs, msg);
00410   (void) INTUSE(xdrrec_endofrecord) (xdrs, TRUE);
00411   return stat;
00412 }