home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / clnt_simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  2.5 KB  |  100 lines

  1. /*
  2.  *      $Id: clnt_simple.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Simplified client front end to rpc.
  5.  *
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  */
  10.  
  11. /* @(#)clnt_simple.c    2.2 88/08/01 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /* 
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  */
  19.  
  20. #include <sys/param.h>
  21. #include <stdio.h>
  22. #include <rpc/rpc.h>
  23. #include <sys/socket.h>
  24. #include <netdb.h>
  25.  
  26. /* Single element cache
  27.  */
  28. static struct callrpc_private {
  29.     CLIENT    *client;
  30.     int    socket;
  31.     int    oldprognum, oldversnum, valid;
  32.     char    *oldhost;
  33. } *callrpc_private;
  34.  
  35. int
  36. callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
  37.     const char * host;
  38.     u_long prognum, versnum, procnum;
  39.     xdrproc_t inproc, outproc;
  40.     void * in, * out;
  41. {
  42.     register struct callrpc_private *crp = callrpc_private;
  43.     struct sockaddr_in server_addr;
  44.     enum clnt_stat clnt_stat;
  45.     struct hostent *hp;
  46.     struct timeval timeout, tottimeout;
  47.  
  48.     if (crp == 0) {
  49.         crp = (struct callrpc_private *)mem_calloc(1, sizeof (*crp));
  50.         if (crp == 0)
  51.             return (0);
  52.         callrpc_private = crp;
  53.     }
  54.     if (crp->oldhost == NULL) {
  55.         crp->oldhost = mem_alloc(256);
  56.         crp->oldhost[0] = 0;
  57.         crp->socket = RPC_ANYSOCK;
  58.     }
  59.     if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
  60.         && strcmp(crp->oldhost, host) == 0) {
  61.         /* reuse old client */        
  62.     } else {
  63.         crp->valid = 0;
  64. #ifdef AMITCP
  65.         (void)CloseSocket(crp->socket);
  66. #else
  67.         (void)close(crp->socket);
  68. #endif
  69.         crp->socket = RPC_ANYSOCK;
  70.         if (crp->client) {
  71.             clnt_destroy(crp->client);
  72.             crp->client = NULL;
  73.         }
  74.         if ((hp = gethostbyname(host)) == NULL)
  75.             return ((int) RPC_UNKNOWNHOST);
  76.         timeout.tv_usec = 0;
  77.         timeout.tv_sec = 5;
  78.         bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length);
  79.         server_addr.sin_family = AF_INET;
  80.         server_addr.sin_port =  0;
  81.         if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
  82.             (u_long)versnum, timeout, &crp->socket)) == NULL)
  83.             return ((int) rpc_createerr.cf_stat);
  84.         crp->valid = 1;
  85.         crp->oldprognum = prognum;
  86.         crp->oldversnum = versnum;
  87.         (void) strcpy(crp->oldhost, host);
  88.     }
  89.     tottimeout.tv_sec = 25;
  90.     tottimeout.tv_usec = 0;
  91.     clnt_stat = clnt_call(crp->client, procnum, inproc, in,
  92.         outproc, out, tottimeout);
  93.     /* 
  94.      * if call failed, empty cache
  95.      */
  96.     if (clnt_stat != RPC_SUCCESS)
  97.         crp->valid = 0;
  98.     return ((int) clnt_stat);
  99. }
  100.