home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part06 / rpc / rpclib / clnt_simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.9 KB  |  105 lines

  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. #ifndef lint
  30. static char sccsid[] = "@(#)clnt_simple.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  31. #endif
  32.  
  33. /* 
  34.  * clnt_simple.c
  35.  * Simplified front end to rpc.
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <rpc/rpc.h>
  42. #include <sys/socket.h>
  43. #include <sys/time.h>
  44. #include <netdb.h>
  45.  
  46. char *malloc();
  47. static CLIENT *client;
  48. static int socket;
  49. static int oldprognum, oldversnum, valid;
  50. static char *oldhost;
  51.  
  52. callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
  53.     char *host;
  54.     xdrproc_t inproc, outproc;
  55.     char *in, *out;
  56. {
  57.     struct sockaddr_in server_addr;
  58.     enum clnt_stat clnt_stat;
  59.     struct hostent *hp;
  60.     struct timeval timeout, tottimeout;
  61.  
  62.     if (oldhost == NULL) {
  63.         oldhost = malloc(256);
  64.         oldhost[0] = 0;
  65.         socket = RPC_ANYSOCK;
  66.     }
  67.     if (valid && oldprognum == prognum && oldversnum == versnum
  68.         && strcmp(oldhost, host) == 0) {
  69.         /* reuse old client */        
  70.     }
  71.     else {
  72.         valid = 0;
  73.         close(socket);
  74.         socket = RPC_ANYSOCK;
  75.         if (client) {
  76.             clnt_destroy(client);
  77.             client = NULL;
  78.         }
  79.         if ((hp = gethostbyname(host)) == NULL)
  80.             return ((int) RPC_UNKNOWNHOST);
  81.         timeout.tv_usec = 0;
  82.         timeout.tv_sec = 5;
  83.         bcopy(hp->h_addr, &server_addr.sin_addr, hp->h_length);
  84.         server_addr.sin_family = AF_INET;
  85.         server_addr.sin_port =  0;
  86.         if ((client = clntudp_create(&server_addr, prognum,
  87.             versnum, timeout, &socket)) == NULL)
  88.             return ((int) rpc_createerr.cf_stat);
  89.         valid = 1;
  90.         oldprognum = prognum;
  91.         oldversnum = versnum;
  92.         strcpy(oldhost, host);
  93.     }
  94.     tottimeout.tv_sec = 25;
  95.     tottimeout.tv_usec = 0;
  96.     clnt_stat = clnt_call(client, procnum, inproc, in,
  97.         outproc, out, tottimeout);
  98.     /* 
  99.      * if call failed, empty cache
  100.      */
  101.     if (clnt_stat != RPC_SUCCESS)
  102.         valid = 0;
  103.     return ((int) clnt_stat);
  104. }
  105.