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