xref: /freebsd/lib/libc/rpc/clnt_generic.c (revision 4c3af266f6ed1d7255aa89c13405b5db0a37fc3e)
199064799SGarrett Wollman /*
299064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
399064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
499064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
599064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
699064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
799064799SGarrett Wollman  * program developed by the user.
899064799SGarrett Wollman  *
999064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1099064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1199064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1299064799SGarrett Wollman  *
1399064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1499064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1599064799SGarrett Wollman  * modification or enhancement.
1699064799SGarrett Wollman  *
1799064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1899064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1999064799SGarrett Wollman  * OR ANY PART THEREOF.
2099064799SGarrett Wollman  *
2199064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2299064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2399064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2499064799SGarrett Wollman  *
2599064799SGarrett Wollman  * Sun Microsystems, Inc.
2699064799SGarrett Wollman  * 2550 Garcia Avenue
2799064799SGarrett Wollman  * Mountain View, California  94043
2899064799SGarrett Wollman  */
2999064799SGarrett Wollman 
3099064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
3199064799SGarrett Wollman /*static char *sccsid = "from: @(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";*/
3299064799SGarrett Wollman /*static char *sccsid = "from: @(#)clnt_generic.c	2.2 88/08/01 4.0 RPCSRC";*/
334c3af266SPoul-Henning Kamp static char *rcsid = "$Id: clnt_generic.c,v 1.2 1995/05/30 05:41:14 rgrimes Exp $";
3499064799SGarrett Wollman #endif
3599064799SGarrett Wollman 
3699064799SGarrett Wollman /*
3799064799SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
3899064799SGarrett Wollman  */
3999064799SGarrett Wollman #include <rpc/rpc.h>
4099064799SGarrett Wollman #include <sys/socket.h>
4199064799SGarrett Wollman #include <sys/errno.h>
4299064799SGarrett Wollman #include <netdb.h>
434c3af266SPoul-Henning Kamp #include <string.h>
4499064799SGarrett Wollman 
4599064799SGarrett Wollman /*
4699064799SGarrett Wollman  * Generic client creation: takes (hostname, program-number, protocol) and
4799064799SGarrett Wollman  * returns client handle. Default options are set, which the user can
4899064799SGarrett Wollman  * change using the rpc equivalent of ioctl()'s.
4999064799SGarrett Wollman  */
5099064799SGarrett Wollman CLIENT *
5199064799SGarrett Wollman clnt_create(hostname, prog, vers, proto)
5299064799SGarrett Wollman 	char *hostname;
5399064799SGarrett Wollman 	u_long prog;
5499064799SGarrett Wollman 	u_long vers;
5599064799SGarrett Wollman 	char *proto;
5699064799SGarrett Wollman {
5799064799SGarrett Wollman 	struct hostent *h;
5899064799SGarrett Wollman 	struct protoent *p;
5999064799SGarrett Wollman 	struct sockaddr_in sin;
6099064799SGarrett Wollman 	int sock;
6199064799SGarrett Wollman 	struct timeval tv;
6299064799SGarrett Wollman 	CLIENT *client;
6399064799SGarrett Wollman 
6499064799SGarrett Wollman 	h = gethostbyname(hostname);
6599064799SGarrett Wollman 	if (h == NULL) {
6699064799SGarrett Wollman 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
6799064799SGarrett Wollman 		return (NULL);
6899064799SGarrett Wollman 	}
6999064799SGarrett Wollman 	if (h->h_addrtype != AF_INET) {
7099064799SGarrett Wollman 		/*
7199064799SGarrett Wollman 		 * Only support INET for now
7299064799SGarrett Wollman 		 */
7399064799SGarrett Wollman 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7499064799SGarrett Wollman 		rpc_createerr.cf_error.re_errno = EAFNOSUPPORT;
7599064799SGarrett Wollman 		return (NULL);
7699064799SGarrett Wollman 	}
7799064799SGarrett Wollman 	sin.sin_family = h->h_addrtype;
7899064799SGarrett Wollman 	sin.sin_port = 0;
7999064799SGarrett Wollman 	bzero(sin.sin_zero, sizeof(sin.sin_zero));
8099064799SGarrett Wollman 	bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
8199064799SGarrett Wollman 	p = getprotobyname(proto);
8299064799SGarrett Wollman 	if (p == NULL) {
8399064799SGarrett Wollman 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
8499064799SGarrett Wollman 		rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
8599064799SGarrett Wollman 		return (NULL);
8699064799SGarrett Wollman 	}
8799064799SGarrett Wollman 	sock = RPC_ANYSOCK;
8899064799SGarrett Wollman 	switch (p->p_proto) {
8999064799SGarrett Wollman 	case IPPROTO_UDP:
9099064799SGarrett Wollman 		tv.tv_sec = 5;
9199064799SGarrett Wollman 		tv.tv_usec = 0;
9299064799SGarrett Wollman 		client = clntudp_create(&sin, prog, vers, tv, &sock);
9399064799SGarrett Wollman 		if (client == NULL) {
9499064799SGarrett Wollman 			return (NULL);
9599064799SGarrett Wollman 		}
9699064799SGarrett Wollman 		tv.tv_sec = 25;
9799064799SGarrett Wollman 		clnt_control(client, CLSET_TIMEOUT, &tv);
9899064799SGarrett Wollman 		break;
9999064799SGarrett Wollman 	case IPPROTO_TCP:
10099064799SGarrett Wollman 		client = clnttcp_create(&sin, prog, vers, &sock, 0, 0);
10199064799SGarrett Wollman 		if (client == NULL) {
10299064799SGarrett Wollman 			return (NULL);
10399064799SGarrett Wollman 		}
10499064799SGarrett Wollman 		tv.tv_sec = 25;
10599064799SGarrett Wollman 		tv.tv_usec = 0;
10699064799SGarrett Wollman 		clnt_control(client, CLSET_TIMEOUT, &tv);
10799064799SGarrett Wollman 		break;
10899064799SGarrett Wollman 	default:
10999064799SGarrett Wollman 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
11099064799SGarrett Wollman 		rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
11199064799SGarrett Wollman 		return (NULL);
11299064799SGarrett Wollman 	}
11399064799SGarrett Wollman 	return (client);
11499064799SGarrett Wollman }
115