1 /* $OpenBSD: clnt_generic.c,v 1.7 2010/09/01 14:43:34 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 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