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