home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / inetray / sendrpc.c < prev    next >
C/C++ Source or Header  |  1992-06-19  |  5KB  |  144 lines

  1. /*======================================================================
  2.                     S E N D R P C . C 
  3.                     doc: Thu May  7 16:55:29 1992
  4.                     dlm: Fri Jun 19 17:50:32 1992
  5.                     (c) 1992 ant@ips.id.ethz.ch
  6.                     uE-Info: 97 39 T 0 0 72 2 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. /*
  10.    This file is a slightly modified version fo clnt_simple.c of the
  11.    original SUNRPC package. It's routine, sendrpc(), works much like
  12.    callrpc() except:
  13.      - return is immediate (independently of out and outproc which
  14.        are dummies)
  15.      - the ascii-form of inet addresses are accepted (not only the
  16.            names)
  17. */
  18.  
  19. /* @(#)clnt_simple.c    2.2 88/08/01 4.0 RPCSRC */
  20. /*
  21.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  22.  * unrestricted use provided that this legend is included on all tape
  23.  * media and as a part of the software program in whole or part.  Users
  24.  * may copy or modify Sun RPC without charge, but are not authorized
  25.  * to license or distribute it to anyone else except as part of a product or
  26.  * program developed by the user.
  27.  * 
  28.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  29.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  30.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  31.  * 
  32.  * Sun RPC is provided with no support and without any obligation on the
  33.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  34.  * modification or enhancement.
  35.  * 
  36.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  37.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  38.  * OR ANY PART THEREOF.
  39.  * 
  40.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  41.  * or profits or other special, indirect and consequential damages, even if
  42.  * Sun has been advised of the possibility of such damages.
  43.  * 
  44.  * Sun Microsystems, Inc.
  45.  * 2550 Garcia Avenue
  46.  * Mountain View, California  94043
  47.  */
  48. #if !defined(lint) && defined(SCCSIDS)
  49. static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
  50. #endif
  51.  
  52. /* 
  53.  * clnt_simple.c
  54.  * Simplified front end to rpc.
  55.  *
  56.  * Copyright (C) 1984, Sun Microsystems, Inc.
  57.  */
  58.  
  59. #include <stdio.h>
  60. #include <rpc/rpc.h>
  61. #include <sys/socket.h>
  62. #include </usr/include/netdb.h>        /* Prefer system over rpc/netdb.h */
  63. #include <strings.h>
  64. #ifndef ITIMER_REAL
  65. #include     <sys/time.h>
  66. #endif
  67. #ifdef AUX_QUIRK
  68. #include    "aux_quirk.h"
  69. #endif
  70.  
  71. static struct callrpc_private {
  72.     CLIENT    *client;
  73.     int    socket;
  74.     int    oldprognum, oldversnum, valid;
  75.     char    *oldhost;
  76. } *callrpc_private;
  77.  
  78. sendrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
  79.     char *host;
  80.     xdrproc_t inproc, outproc;
  81.     char *in, *out;
  82. {
  83.     register struct callrpc_private *crp = callrpc_private;
  84.     struct sockaddr_in server_addr;
  85.     enum clnt_stat clnt_stat;
  86.     struct hostent *hp;
  87.     struct timeval timeout, tottimeout, noDefault;
  88.  
  89.     noDefault.tv_sec = 0; noDefault.tv_usec = -1;
  90.     if (crp == 0) {
  91.         crp = (struct callrpc_private *)calloc(1, sizeof (*crp));
  92.         if (crp == 0)
  93.             return (0);
  94.         callrpc_private = crp;
  95.     }
  96.     if (crp->oldhost == NULL) {
  97.         crp->oldhost = (char *)malloc(256);
  98.         crp->oldhost[0] = 0;
  99.         crp->socket = RPC_ANYSOCK;
  100.     }
  101.     if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
  102.         && strcmp(crp->oldhost, host) == 0) {
  103.         /* reuse old client */        
  104.     } else {
  105.         crp->valid = 0;
  106.         (void)close(crp->socket);
  107.         crp->socket = RPC_ANYSOCK;
  108.         if (crp->client) {
  109.             clnt_destroy(crp->client);
  110.             crp->client = NULL;
  111.         }
  112.         if ((hp = gethostbyname(host)) == NULL) {   /* is address? */
  113.             return ((int) RPC_UNKNOWNHOST);
  114.         } else {
  115.              bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length);
  116.          }
  117.         timeout.tv_usec = 0;
  118.         timeout.tv_sec = 5;
  119.         server_addr.sin_family = AF_INET;
  120.         server_addr.sin_port =  0;
  121.         if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
  122.             (u_long)versnum, timeout, &crp->socket)) == NULL)
  123.             return ((int) rpc_createerr.cf_stat);
  124.         if (!clnt_control(crp->client,CLSET_TIMEOUT,&noDefault)) 
  125.             return ((int) RPC_FAILED);
  126.         crp->valid = 1;
  127.         crp->oldprognum = prognum;
  128.         crp->oldversnum = versnum;
  129.         (void) strcpy(crp->oldhost, host);
  130.     }
  131.     tottimeout.tv_sec = 
  132.     tottimeout.tv_usec = 0;
  133.     clnt_stat = clnt_call(crp->client, procnum, inproc, in,
  134.         outproc, out, tottimeout);
  135.     /* 
  136.      * if call failed, empty cache
  137.      */
  138.     if (clnt_stat == RPC_TIMEDOUT)
  139.         clnt_stat = RPC_SUCCESS;
  140.     if (clnt_stat != RPC_SUCCESS)
  141.         crp->valid = 0;
  142.     return ((int) clnt_stat);
  143. }
  144.