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