1 /* @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC */ 2 /* 3 * Copyright (c) 2010, Oracle America, Inc. 4 * 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * * Neither the name of the "Oracle America, Inc." nor the names of 19 * its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 #if !defined(lint) && defined(SCCSIDS) 35 static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; 36 #endif 37 38 /* 39 * clnt_simple.c 40 * Simplified front end to rpc. 41 */ 42 43 #include "autoconf.h" 44 #include <stdio.h> 45 /* for close() */ 46 #include <unistd.h> 47 #include <gssrpc/rpc.h> 48 #include <sys/socket.h> 49 #include <netdb.h> 50 #include <string.h> 51 #include <port-sockets.h> 52 53 static struct callrpc_private { 54 CLIENT *client; 55 SOCKET socket; 56 rpcprog_t oldprognum; 57 rpcvers_t oldversnum; 58 int valid; 59 char *oldhost; 60 } *callrpc_private; 61 62 int 63 callrpc( 64 char *host, 65 rpcprog_t prognum, 66 rpcvers_t versnum, 67 rpcproc_t procnum, 68 xdrproc_t inproc, 69 char *in, 70 xdrproc_t outproc, 71 char *out) 72 { 73 struct callrpc_private *crp = callrpc_private; 74 struct sockaddr_in server_addr; 75 enum clnt_stat clnt_stat; 76 struct hostent *hp; 77 struct timeval timeout, tottimeout; 78 79 if (crp == 0) { 80 crp = (struct callrpc_private *)calloc(1, sizeof (*crp)); 81 if (crp == 0) 82 return (0); 83 callrpc_private = crp; 84 } 85 if (crp->oldhost == NULL) { 86 crp->oldhost = mem_alloc(256); 87 if (crp->oldhost == 0) 88 return 0; 89 crp->oldhost[0] = 0; 90 crp->socket = RPC_ANYSOCK; 91 } 92 if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum 93 && strcmp(crp->oldhost, host) == 0) { 94 /* reuse old client */ 95 } else { 96 crp->valid = 0; 97 (void)closesocket(crp->socket); 98 crp->socket = RPC_ANYSOCK; 99 if (crp->client) { 100 clnt_destroy(crp->client); 101 crp->client = NULL; 102 } 103 if ((hp = gethostbyname(host)) == NULL) 104 return ((int) RPC_UNKNOWNHOST); 105 timeout.tv_usec = 0; 106 timeout.tv_sec = 5; 107 memset(&server_addr, 0, sizeof(server_addr)); 108 memmove((char *)&server_addr.sin_addr, hp->h_addr, 109 sizeof(server_addr.sin_addr)); 110 server_addr.sin_family = AF_INET; 111 server_addr.sin_port = 0; 112 if ((crp->client = clntudp_create(&server_addr, prognum, 113 versnum, timeout, &crp->socket)) == NULL) 114 return ((int) rpc_createerr.cf_stat); 115 crp->valid = 1; 116 crp->oldprognum = prognum; 117 crp->oldversnum = versnum; 118 (void) strncpy(crp->oldhost, host, 255); 119 crp->oldhost[255] = '\0'; 120 } 121 tottimeout.tv_sec = 25; 122 tottimeout.tv_usec = 0; 123 clnt_stat = clnt_call(crp->client, procnum, inproc, in, 124 outproc, out, tottimeout); 125 /* 126 * if call failed, empty cache 127 */ 128 if (clnt_stat != RPC_SUCCESS) 129 crp->valid = 0; 130 return ((int) clnt_stat); 131 } 132