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