Path: blob/main/crypto/krb5/src/lib/rpc/clnt_generic.c
39536 views
/* @(#)clnt_generic.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*/34#if !defined(lint) && defined(SCCSIDS)35static char sccsid[] = "@(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";36#endif3738#include "autoconf.h"39#include <string.h>40#include <gssrpc/rpc.h>41#include <sys/socket.h>42#include <sys/errno.h>43#include <netdb.h>4445/*46* Generic client creation: takes (hostname, program-number, protocol) and47* returns client handle. Default options are set, which the user can48* change using the rpc equivalent of ioctl()'s.49*/50CLIENT *51clnt_create(52char *hostname,53rpcprog_t prog,54rpcvers_t vers,55char *proto)56{57struct hostent *h;58struct protoent *p;59struct sockaddr_in sockin;60int sock;61struct timeval tv;62CLIENT *client;6364h = gethostbyname(hostname);65if (h == NULL) {66rpc_createerr.cf_stat = RPC_UNKNOWNHOST;67return (NULL);68}69if (h->h_addrtype != AF_INET) {70/*71* Only support INET for now72*/73rpc_createerr.cf_stat = RPC_SYSTEMERROR;74rpc_createerr.cf_error.re_errno = EAFNOSUPPORT;75return (NULL);76}77memset(&sockin, 0, sizeof(sockin));78sockin.sin_family = h->h_addrtype;79sockin.sin_port = 0;80memmove((char*)&sockin.sin_addr, h->h_addr, sizeof(sockin.sin_addr));81p = getprotobyname(proto);82if (p == NULL) {83rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;84rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;85return (NULL);86}87sock = RPC_ANYSOCK;88switch (p->p_proto) {89case IPPROTO_UDP:90tv.tv_sec = 5;91tv.tv_usec = 0;92client = clntudp_create(&sockin, prog, vers, tv, &sock);93if (client == NULL) {94return (NULL);95}96break;97case IPPROTO_TCP:98client = clnttcp_create(&sockin, prog, vers, &sock, 0, 0);99if (client == NULL) {100return (NULL);101}102break;103default:104rpc_createerr.cf_stat = RPC_SYSTEMERROR;105rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;106return (NULL);107}108return (client);109}110111112