home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI-RPC 4.0 / clnt_tcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-11  |  11.8 KB  |  477 lines  |  [TEXT/MPS ]

  1. /* @(#)clnt_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 !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  *
  39.  * TCP based RPC supports 'batched calls'.
  40.  * A sequence of calls may be batched-up in a send buffer.  The rpc call
  41.  * return immediately to the client even though the call was not necessarily
  42.  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
  43.  * the rpc timeout value is zero (see clnt.h, rpc).
  44.  *
  45.  * Clients should NOT casually batch calls that in fact return results; that is,
  46.  * the server side should be aware that a call is batched and not produce any
  47.  * return message.  Batched calls that produce many result messages can
  48.  * deadlock (netlock) the client and the server....
  49.  *
  50.  * Now go hang yourself.
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include <rpc/rpc.h>
  55. #include <sys/socket.h>
  56. #include <netdb.h>
  57. #include <sys/errno.h>
  58. #include <rpc/pmap_clnt.h>
  59.  
  60. #define MCALL_MSG_SIZE 24
  61.  
  62. extern int errno;
  63.  
  64. static int    readtcp();
  65. static int    writetcp();
  66.  
  67. static enum clnt_stat    clnttcp_call();
  68. static void        clnttcp_abort();
  69. static void        clnttcp_geterr();
  70. static bool_t        clnttcp_freeres();
  71. static bool_t           clnttcp_control();
  72. static void        clnttcp_destroy();
  73.  
  74. static struct clnt_ops tcp_ops = {
  75.     clnttcp_call,
  76.     clnttcp_abort,
  77.     clnttcp_geterr,
  78.     clnttcp_freeres,
  79.     clnttcp_destroy,
  80.     clnttcp_control
  81. };
  82.  
  83. struct ct_data {
  84.     int        ct_sock;
  85.     bool_t        ct_closeit;
  86.     struct timeval    ct_wait;
  87.     bool_t          ct_waitset;       /* wait set by clnt_control? */
  88.     struct sockaddr_in ct_addr; 
  89.     struct rpc_err    ct_error;
  90.     char        ct_mcall[MCALL_MSG_SIZE];    /* marshalled callmsg */
  91.     u_int        ct_mpos;            /* pos after marshal */
  92.     XDR        ct_xdrs;
  93. };
  94.  
  95. /*
  96.  * Create a client handle for a tcp/ip connection.
  97.  * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  98.  * connected to raddr.  If *sockp non-negative then
  99.  * raddr is ignored.  The rpc/tcp package does buffering
  100.  * similar to stdio, so the client must pick send and receive buffer sizes,];
  101.  * 0 => use the default.
  102.  * If raddr->sin_port is 0, then a binder on the remote machine is
  103.  * consulted for the right port number.
  104.  * NB: *sockp is copied into a private area.
  105.  * NB: It is the clients responsibility to close *sockp.
  106.  * NB: The rpch->cl_auth is set null authentication.  Caller may wish to set this
  107.  * something more useful.
  108.  */
  109. CLIENT *
  110. clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
  111.     struct sockaddr_in *raddr;
  112.     u_long prog;
  113.     u_long vers;
  114.     register int *sockp;
  115.     u_int sendsz;
  116.     u_int recvsz;
  117. {
  118.     CLIENT *h;
  119.     register struct ct_data *ct;
  120. #if 1
  121.     time_t now;
  122. #else
  123.     struct timeval now;
  124. #endif
  125.     struct rpc_msg call_msg;
  126.  
  127.     h  = (CLIENT *)mem_alloc(sizeof(*h));
  128.     if (h == NULL) {
  129.         (void)fprintf(stderr, "clnttcp_create: out of memory\n");
  130.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  131.         rpc_createerr.cf_error.re_errno = errno;
  132.         goto fooy;
  133.     }
  134.     ct = (struct ct_data *)mem_alloc(sizeof(*ct));
  135.     if (ct == NULL) {
  136.         (void)fprintf(stderr, "clnttcp_create: out of memory\n");
  137.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  138.         rpc_createerr.cf_error.re_errno = errno;
  139.         goto fooy;
  140.     }
  141.  
  142.     /*
  143.      * If no port number given ask the pmap for one
  144.      */
  145.     if (raddr->sin_port == 0) {
  146.         u_short port;
  147.         if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
  148.             mem_free((caddr_t)h, sizeof(CLIENT));
  149.             return ((CLIENT *)NULL);
  150.         }
  151.         raddr->sin_port = htons(port);
  152.     }
  153.  
  154.     /*
  155.      * If no socket given, open one
  156.      */
  157.     if (*sockp < 0) {
  158.         *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  159.         (void)bindresvport(*sockp, (struct sockaddr_in *)0);
  160.         if ((*sockp < 0)
  161.             || (connect(*sockp, (struct sockaddr *)raddr,
  162.             sizeof(*raddr)) < 0)) {
  163.             rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  164.             rpc_createerr.cf_error.re_errno = errno;
  165.             (void)close(*sockp);
  166.             goto fooy;
  167.         }
  168.         ct->ct_closeit = TRUE;
  169.     } else {
  170.         ct->ct_closeit = FALSE;
  171.     }
  172.  
  173.     /*
  174.      * Set up private data struct
  175.      */
  176.     ct->ct_sock = *sockp;
  177.     ct->ct_wait.tv_usec = 0;
  178.     ct->ct_waitset = FALSE;
  179.     ct->ct_addr = *raddr;
  180.     /*
  181.      * Initialize call message
  182.      */
  183. #if 0
  184.     (void)gettimeofday(&now, (struct timezone *)0);
  185.     call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
  186. #else
  187.     time(&now);
  188.     call_msg.rm_xid = getpid() ^ now;
  189. #endif
  190.     call_msg.rm_direction = CALL;
  191.     call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  192.     call_msg.rm_call.cb_prog = prog;
  193.     call_msg.rm_call.cb_vers = vers;
  194.  
  195.     /*
  196.      * pre-serialize the staic part of the call msg and stash it away
  197.      */
  198.     xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
  199.         XDR_ENCODE);
  200.     if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
  201.         if (ct->ct_closeit) {
  202.             (void)close(*sockp);
  203.         }
  204.         goto fooy;
  205.     }
  206.     ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
  207.     XDR_DESTROY(&(ct->ct_xdrs));
  208.  
  209.     /*
  210.      * Create a client handle which uses xdrrec for serialization
  211.      * and authnone for authentication.
  212.      */
  213.     xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
  214.         (caddr_t)ct, readtcp, writetcp);
  215.     h->cl_ops = &tcp_ops;
  216.     h->cl_private = (caddr_t) ct;
  217.     h->cl_auth = authnone_create();
  218.     return (h);
  219.  
  220. fooy:
  221.     /*
  222.      * Something goofed, free stuff and barf
  223.      */
  224.     mem_free((caddr_t)ct, sizeof(struct ct_data));
  225.     mem_free((caddr_t)h, sizeof(CLIENT));
  226.     return ((CLIENT *)NULL);
  227. }
  228.  
  229. static enum clnt_stat
  230. clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
  231.     register CLIENT *h;
  232.     u_long proc;
  233.     xdrproc_t xdr_args;
  234.     caddr_t args_ptr;
  235.     xdrproc_t xdr_results;
  236.     caddr_t results_ptr;
  237.     struct timeval timeout;
  238. {
  239.     register struct ct_data *ct = (struct ct_data *) h->cl_private;
  240.     register XDR *xdrs = &(ct->ct_xdrs);
  241.     struct rpc_msg reply_msg;
  242.     u_long x_id;
  243.     u_long *msg_x_id = (u_long *)(ct->ct_mcall);    /* yuk */
  244.     register bool_t shipnow;
  245.     int refreshes = 2;
  246.  
  247.     if (!ct->ct_waitset) {
  248.         ct->ct_wait = timeout;
  249.     }
  250.  
  251.     shipnow =
  252.         (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
  253.         && timeout.tv_usec == 0) ? FALSE : TRUE;
  254.  
  255. call_again:
  256.     xdrs->x_op = XDR_ENCODE;
  257.     ct->ct_error.re_status = RPC_SUCCESS;
  258.     x_id = ntohl(--(*msg_x_id));
  259.     if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  260.         (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
  261.         (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
  262.         (! (*xdr_args)(xdrs, args_ptr))) {
  263.         if (ct->ct_error.re_status == RPC_SUCCESS)
  264.             ct->ct_error.re_status = RPC_CANTENCODEARGS;
  265.         (void)xdrrec_endofrecord(xdrs, TRUE);
  266.         return (ct->ct_error.re_status);
  267.     }
  268.  
  269.     if (! xdrrec_endofrecord(xdrs, shipnow))
  270.         return (ct->ct_error.re_status = RPC_CANTSEND);
  271.  
  272.     if (! shipnow)
  273.         return (RPC_SUCCESS);
  274.  
  275.     /*
  276.      * Hack to provide rpc-based message passing
  277.      */
  278.     if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
  279.         return(ct->ct_error.re_status = RPC_TIMEDOUT);
  280.     }
  281.  
  282.     /*
  283.      * Keep receiving until we get a valid transaction id
  284.      */
  285.     xdrs->x_op = XDR_DECODE;
  286.     while (TRUE) {
  287.         reply_msg.acpted_rply.ar_verf = _null_auth;
  288.         reply_msg.acpted_rply.ar_results.where = NULL;
  289.         reply_msg.acpted_rply.ar_results.proc = xdr_void;
  290.         if (! xdrrec_skiprecord(xdrs))
  291.             return (ct->ct_error.re_status);
  292.         /* now decode and validate the response header */
  293.         if (! xdr_replymsg(xdrs, &reply_msg)) {
  294.             if (ct->ct_error.re_status == RPC_SUCCESS)
  295.                 continue;
  296.             return (ct->ct_error.re_status);
  297.         }
  298.         if (reply_msg.rm_xid == x_id)
  299.             break;
  300.     }
  301.  
  302.     /*
  303.      * process header
  304.      */
  305.     _seterr_reply(&reply_msg, &(ct->ct_error));
  306.     if (ct->ct_error.re_status == RPC_SUCCESS) {
  307.         if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
  308.             ct->ct_error.re_status = RPC_AUTHERROR;
  309.             ct->ct_error.re_why = AUTH_INVALIDRESP;
  310.         } else if (! (*xdr_results)(xdrs, results_ptr)) {
  311.             if (ct->ct_error.re_status == RPC_SUCCESS)
  312.                 ct->ct_error.re_status = RPC_CANTDECODERES;
  313.         }
  314.         /* free verifier ... */
  315.         if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
  316.             xdrs->x_op = XDR_FREE;
  317.             (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  318.         }
  319.     }  /* end successful completion */
  320.     else {
  321.         /* maybe our credentials need to be refreshed ... */
  322.         if (refreshes-- && AUTH_REFRESH(h->cl_auth))
  323.             goto call_again;
  324.     }  /* end of unsuccessful completion */
  325.  
  326.     return (ct->ct_error.re_status);
  327. }
  328.  
  329. static void
  330. clnttcp_geterr(h, errp)
  331.     CLIENT *h;
  332.     struct rpc_err *errp;
  333. {
  334.     register struct ct_data *ct =
  335.         (struct ct_data *) h->cl_private;
  336.  
  337.     *errp = ct->ct_error;
  338. }
  339.  
  340. static bool_t
  341. clnttcp_freeres(cl, xdr_res, res_ptr)
  342.     CLIENT *cl;
  343.     xdrproc_t xdr_res;
  344.     caddr_t res_ptr;
  345. {
  346.     register struct ct_data *ct = (struct ct_data *)cl->cl_private;
  347.     register XDR *xdrs = &(ct->ct_xdrs);
  348.  
  349.     xdrs->x_op = XDR_FREE;
  350.     return ((*xdr_res)(xdrs, res_ptr));
  351. }
  352.  
  353. static void
  354. clnttcp_abort()
  355. {
  356. }
  357.  
  358. static bool_t
  359. clnttcp_control(cl, request, info)
  360.     CLIENT *cl;
  361.     int request;
  362.     char *info;
  363. {
  364.     register struct ct_data *ct = (struct ct_data *)cl->cl_private;
  365.  
  366.     switch (request) {
  367.     case CLSET_TIMEOUT:
  368.         ct->ct_wait = *(struct timeval *)info;
  369.         ct->ct_waitset = TRUE;
  370.         break;
  371.     case CLGET_TIMEOUT:
  372.         *(struct timeval *)info = ct->ct_wait;
  373.         break;
  374.     case CLGET_SERVER_ADDR:
  375.         *(struct sockaddr_in *)info = ct->ct_addr;
  376.         break;
  377.     default:
  378.         return (FALSE);
  379.     }
  380.     return (TRUE);
  381. }
  382.  
  383.  
  384. static void
  385. clnttcp_destroy(h)
  386.     CLIENT *h;
  387. {
  388.     register struct ct_data *ct =
  389.         (struct ct_data *) h->cl_private;
  390.  
  391.     if (ct->ct_closeit) {
  392.         (void)close(ct->ct_sock);
  393.     }
  394.     XDR_DESTROY(&(ct->ct_xdrs));
  395.     mem_free((caddr_t)ct, sizeof(struct ct_data));
  396.     mem_free((caddr_t)h, sizeof(CLIENT));
  397. }
  398.  
  399. /*
  400.  * Interface between xdr serializer and tcp connection.
  401.  * Behaves like the system calls, read & write, but keeps some error state
  402.  * around for the rpc level.
  403.  */
  404. static int
  405. readtcp(ct, buf, len)
  406.     register struct ct_data *ct;
  407.     caddr_t buf;
  408.     register int len;
  409. {
  410. #ifdef FD_SETSIZE
  411.     fd_set mask;
  412.     fd_set readfds;
  413.     
  414.     if (len == 0)
  415.         return (0);
  416.     FD_ZERO(&mask);
  417.     FD_SET(ct->ct_sock, &mask);
  418. #else
  419.     register int mask = 1 << (ct->ct_sock);
  420.     int readfds;
  421.  
  422.     if (len == 0)
  423.         return (0);
  424.  
  425. #endif /* def FD_SETSIZE */
  426.  
  427.     while (TRUE) {
  428.         readfds = mask;
  429.         switch (select(_rpc_dtablesize(), &readfds, (fd_set *)NULL, (fd_set *)NULL,
  430.                    &(ct->ct_wait))) {
  431.         case 0:
  432.             ct->ct_error.re_status = RPC_TIMEDOUT;
  433.             return (-1);
  434.         case -1:
  435.             if (errno == EINTR)
  436.                 continue;
  437.             ct->ct_error.re_status = RPC_CANTRECV;
  438.             ct->ct_error.re_errno = errno;
  439.             return (-1);
  440.         }
  441.         break;
  442.     }
  443.  
  444.     switch (len = read(ct->ct_sock, buf, len)) {
  445.     case 0:
  446.         /* premature eof */
  447.         ct->ct_error.re_errno = ECONNRESET;
  448.         ct->ct_error.re_status = RPC_CANTRECV;
  449.         len = -1;  /* it's really an error */
  450.         break;
  451.  
  452.     case -1:
  453.         ct->ct_error.re_errno = errno;
  454.         ct->ct_error.re_status = RPC_CANTRECV;
  455.         break;
  456.     }
  457.     return (len);
  458. }
  459.  
  460. static int
  461. writetcp(ct, buf, len)
  462.     struct ct_data *ct;
  463.     caddr_t buf;
  464.     int len;
  465. {
  466.     register int i, cnt;
  467.  
  468.     for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  469.         if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
  470.             ct->ct_error.re_errno = errno;
  471.             ct->ct_error.re_status = RPC_CANTSEND;
  472.             return (-1);
  473.         }
  474.     }
  475.     return (len);
  476. }
  477.