xref: /openbsd/lib/libc/rpc/clnt_generic.c (revision 78b63d65)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: clnt_generic.c,v 1.3 1996/08/19 08:31:25 tholo Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 /*
35  * Copyright (C) 1987, Sun Microsystems, Inc.
36  */
37 #include <string.h>
38 #include <rpc/rpc.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 #include <netdb.h>
42 
43 /*
44  * Generic client creation: takes (hostname, program-number, protocol) and
45  * returns client handle. Default options are set, which the user can
46  * change using the rpc equivalent of ioctl()'s.
47  */
48 CLIENT *
49 clnt_create(hostname, prog, vers, proto)
50 	char *hostname;
51 	u_long prog;
52 	u_long vers;
53 	char *proto;
54 {
55 	struct hostent *h;
56 	struct protoent *p;
57 	struct sockaddr_in sin;
58 	int sock;
59 	struct timeval tv;
60 	CLIENT *client;
61 
62 	h = gethostbyname(hostname);
63 	if (h == NULL) {
64 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
65 		return (NULL);
66 	}
67 	if (h->h_addrtype != AF_INET) {
68 		/*
69 		 * Only support INET for now
70 		 */
71 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
72 		rpc_createerr.cf_error.re_errno = EAFNOSUPPORT;
73 		return (NULL);
74 	}
75 	memset(&sin, 0, sizeof(sin));
76 	sin.sin_len = sizeof(struct sockaddr_in);
77 	sin.sin_family = h->h_addrtype;
78 	sin.sin_port = 0;
79 	memcpy((char*)&sin.sin_addr, h->h_addr, h->h_length);
80 	p = getprotobyname(proto);
81 	if (p == NULL) {
82 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
83 		rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
84 		return (NULL);
85 	}
86 	sock = RPC_ANYSOCK;
87 	switch (p->p_proto) {
88 	case IPPROTO_UDP:
89 		tv.tv_sec = 5;
90 		tv.tv_usec = 0;
91 		client = clntudp_create(&sin, prog, vers, tv, &sock);
92 		if (client == NULL) {
93 			return (NULL);
94 		}
95 		break;
96 	case IPPROTO_TCP:
97 		client = clnttcp_create(&sin, prog, vers, &sock, 0, 0);
98 		if (client == NULL) {
99 			return (NULL);
100 		}
101 		break;
102 	default:
103 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
104 		rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
105 		return (NULL);
106 	}
107 	return (client);
108 }
109