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 * @(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI 30 * @(#)clnt_generic.c 2.2 88/08/01 4.0 RPCSRC 31 * $FreeBSD: src/lib/libc/rpc/clnt_generic.c,v 1.9.2.1 2001/03/05 10:48:28 obrien Exp $ 32 * $DragonFly: src/lib/libc/rpc/clnt_generic.c,v 1.2 2003/06/17 04:26:44 dillon Exp $ 33 */ 34 35 /* 36 * Copyright (C) 1987, Sun Microsystems, Inc. 37 */ 38 #include <rpc/rpc.h> 39 #include <sys/socket.h> 40 #include <errno.h> 41 #include <netdb.h> 42 #include <string.h> 43 44 /* 45 * Generic client creation: takes (hostname, program-number, protocol) and 46 * returns client handle. Default options are set, which the user can 47 * change using the rpc equivalent of ioctl()'s. 48 */ 49 CLIENT * 50 clnt_create(hostname, prog, vers, proto) 51 char *hostname; 52 u_long prog; 53 u_long vers; 54 char *proto; 55 { 56 struct hostent *h; 57 struct protoent *p; 58 struct sockaddr_in sin; 59 struct sockaddr_un sun; 60 int sock; 61 static struct timeval tv; 62 CLIENT *client; 63 64 if (!strcmp(proto, "unix")) { 65 bzero((char *)&sun, sizeof(sun)); 66 sun.sun_family = AF_UNIX; 67 strcpy(sun.sun_path, hostname); 68 sun.sun_len = sizeof(sun.sun_len) + sizeof(sun.sun_family) + 69 strlen(sun.sun_path) + 1; 70 sock = RPC_ANYSOCK; 71 client = clntunix_create(&sun, prog, vers, &sock, 0, 0); 72 if (client == NULL) 73 return(NULL); 74 tv.tv_sec = 25; 75 tv.tv_usec = 0; 76 clnt_control(client, CLSET_TIMEOUT, &tv); 77 return(client); 78 } 79 80 h = gethostbyname(hostname); 81 if (h == NULL) { 82 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; 83 return (NULL); 84 } 85 if (h->h_addrtype != AF_INET) { 86 /* 87 * Only support INET for now 88 */ 89 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 90 rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; 91 return (NULL); 92 } 93 memset(&sin, 0, sizeof(sin)); 94 sin.sin_len = sizeof(struct sockaddr_in); 95 sin.sin_family = h->h_addrtype; 96 sin.sin_port = 0; 97 memcpy((char*)&sin.sin_addr, h->h_addr, h->h_length); 98 p = getprotobyname(proto); 99 if (p == NULL) { 100 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 101 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 102 return (NULL); 103 } 104 sock = RPC_ANYSOCK; 105 switch (p->p_proto) { 106 case IPPROTO_UDP: 107 tv.tv_sec = 5; 108 tv.tv_usec = 0; 109 client = clntudp_create(&sin, prog, vers, tv, &sock); 110 if (client == NULL) { 111 return (NULL); 112 } 113 #if 0 /* XXX do we need this? */ 114 tv.tv_sec = 25; 115 tv.tv_usec = 0; 116 clnt_control(client, CLSET_TIMEOUT, &tv); 117 #endif 118 break; 119 case IPPROTO_TCP: 120 client = clnttcp_create(&sin, prog, vers, &sock, 0, 0); 121 if (client == NULL) { 122 return (NULL); 123 } 124 #if 0 /* XXX do we need this? */ 125 tv.tv_sec = 25; 126 tv.tv_usec = 0; 127 clnt_control(client, CLSET_TIMEOUT, &tv); 128 #endif 129 break; 130 default: 131 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 132 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 133 return (NULL); 134 } 135 return (client); 136 } 137