Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / clnt_simple.c
1 #ifndef lint
2 static char sfs_clnt_simple_id[] = "@(#)clnt_simple.c     2.1     97/10/23";
3 #endif
4 /* @(#)clnt_simple.c    2.2 88/08/01 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /* 
56  * clnt_simple.c
57  * Simplified front end to rpc.
58  *
59  * Copyright (C) 1984, Sun Microsystems, Inc.
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include "rpc/rpc.h"
66 #include "rpc/osdep.h"
67 #include <netdb.h>
68 #include <string.h>
69
70 static struct callrpc_private {
71         CLIENT  *client;
72         int     socket;
73         int     oldprognum, oldversnum, valid;
74         char    *oldhost;
75 } *callrpc_private;
76
77 callrpc(
78         char *host,
79         int prognum,
80         int versnum,
81         int procnum,
82         xdrproc_t inproc,
83         char *in,
84         xdrproc_t outproc,
85         char *out)
86 {
87         register struct callrpc_private *crp = callrpc_private;
88         struct sockaddr_in server_addr;
89         enum clnt_stat clnt_stat;
90         struct hostent *hp;
91         struct timeval timeout, tottimeout;
92
93         if (crp == 0) {
94                 crp = (struct callrpc_private *)calloc(1,
95                                         sizeof (struct callrpc_private));
96                 if (crp == 0)
97                         return (0);
98                 callrpc_private = crp;
99         }
100         if (crp->oldhost == NULL) {
101                 crp->oldhost = malloc(256);
102                 crp->oldhost[0] = 0;
103                 crp->socket = RPC_ANYSOCK;
104         }
105         if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
106                 && strcmp(crp->oldhost, host) == 0) {
107                 /* reuse old client */          
108         } else {
109                 crp->valid = 0;
110                 (void)close(crp->socket);
111                 crp->socket = RPC_ANYSOCK;
112                 if (crp->client) {
113                         clnt_destroy(crp->client);
114                         crp->client = NULL;
115                 }
116                 if ((hp = gethostbyname(host)) == NULL)
117                         return ((int) RPC_UNKNOWNHOST);
118                 timeout.tv_usec = 0;
119                 timeout.tv_sec = 5;
120                 memmove((char *)&server_addr.sin_addr, hp->h_addr, hp->h_length);
121                 server_addr.sin_family = AF_INET;
122                 server_addr.sin_port =  0;
123                 if ((crp->client = clntudp_create(&server_addr, (uint32_t)prognum,
124                     (uint32_t)versnum, timeout, &crp->socket)) == NULL)
125                         return ((int) rpc_createerr.cf_stat);
126                 crp->valid = 1;
127                 crp->oldprognum = prognum;
128                 crp->oldversnum = versnum;
129                 (void) strcpy(crp->oldhost, host);
130         }
131         tottimeout.tv_sec = 25;
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_SUCCESS)
139                 crp->valid = 0;
140         return ((int) clnt_stat);
141 }