lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2010, Oracle America, Inc. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials |
| 13 | * provided with the distribution. |
| 14 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <alloca.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | #include <rpc/rpc.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netdb.h> |
| 38 | |
| 39 | /* |
| 40 | * Generic client creation: takes (hostname, program-number, protocol) and |
| 41 | * returns client handle. Default options are set, which the user can |
| 42 | * change using the rpc equivalent of ioctl()'s. |
| 43 | */ |
| 44 | CLIENT * |
| 45 | clnt_create (const char *hostname, u_long prog, u_long vers, |
| 46 | const char *proto) |
| 47 | { |
| 48 | struct hostent hostbuf, *h; |
| 49 | size_t hstbuflen; |
| 50 | char *hsttmpbuf; |
| 51 | struct protoent protobuf, *p; |
| 52 | size_t prtbuflen; |
| 53 | char *prttmpbuf; |
| 54 | struct sockaddr_in sin; |
| 55 | struct sockaddr_un sun; |
| 56 | int sock; |
| 57 | struct timeval tv; |
| 58 | CLIENT *client; |
| 59 | int herr; |
| 60 | |
| 61 | if (strcmp (proto, "unix") == 0) |
| 62 | { |
| 63 | __bzero ((char *)&sun, sizeof (sun)); |
| 64 | sun.sun_family = AF_UNIX; |
| 65 | strcpy (sun.sun_path, hostname); |
| 66 | sock = RPC_ANYSOCK; |
| 67 | client = clntunix_create (&sun, prog, vers, &sock, 0, 0); |
| 68 | if (client == NULL) |
| 69 | return NULL; |
| 70 | #if 0 |
| 71 | /* This is not wanted. This would disable the user from having |
| 72 | a timeout in the clnt_call() call. Only a call to cnlt_control() |
| 73 | by the user should set the timeout value. */ |
| 74 | tv.tv_sec = 25; |
| 75 | tv.tv_usec = 0; |
| 76 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); |
| 77 | #endif |
| 78 | return client; |
| 79 | } |
| 80 | |
| 81 | hstbuflen = 1024; |
| 82 | hsttmpbuf = __alloca (hstbuflen); |
| 83 | while (__gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen, |
| 84 | &h, &herr) != 0 |
| 85 | || h == NULL) |
| 86 | if (herr != NETDB_INTERNAL || errno != ERANGE) |
| 87 | { |
| 88 | get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST; |
| 89 | return NULL; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | /* Enlarge the buffer. */ |
| 94 | hstbuflen *= 2; |
| 95 | hsttmpbuf = __alloca (hstbuflen); |
| 96 | } |
| 97 | |
| 98 | if (h->h_addrtype != AF_INET) |
| 99 | { |
| 100 | /* |
| 101 | * Only support INET for now |
| 102 | */ |
| 103 | struct rpc_createerr *ce = &get_rpc_createerr (); |
| 104 | ce->cf_stat = RPC_SYSTEMERROR; |
| 105 | ce->cf_error.re_errno = EAFNOSUPPORT; |
| 106 | return NULL; |
| 107 | } |
| 108 | sin.sin_family = h->h_addrtype; |
| 109 | sin.sin_port = 0; |
| 110 | __bzero (sin.sin_zero, sizeof (sin.sin_zero)); |
| 111 | memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length); |
| 112 | |
| 113 | prtbuflen = 1024; |
| 114 | prttmpbuf = __alloca (prtbuflen); |
| 115 | while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0 |
| 116 | || p == NULL) |
| 117 | if (errno != ERANGE) |
| 118 | { |
| 119 | struct rpc_createerr *ce = &get_rpc_createerr (); |
| 120 | ce->cf_stat = RPC_UNKNOWNPROTO; |
| 121 | ce->cf_error.re_errno = EPFNOSUPPORT; |
| 122 | return NULL; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | /* Enlarge the buffer. */ |
| 127 | prtbuflen *= 2; |
| 128 | prttmpbuf = __alloca (prtbuflen); |
| 129 | } |
| 130 | |
| 131 | sock = RPC_ANYSOCK; |
| 132 | switch (p->p_proto) |
| 133 | { |
| 134 | case IPPROTO_UDP: |
| 135 | tv.tv_sec = 5; |
| 136 | tv.tv_usec = 0; |
| 137 | client = clntudp_create (&sin, prog, vers, tv, &sock); |
| 138 | if (client == NULL) |
| 139 | { |
| 140 | return NULL; |
| 141 | } |
| 142 | #if 0 |
| 143 | /* This is not wanted. This would disable the user from having |
| 144 | a timeout in the clnt_call() call. Only a call to cnlt_control() |
| 145 | by the user should set the timeout value. */ |
| 146 | tv.tv_sec = 25; |
| 147 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); |
| 148 | #endif |
| 149 | break; |
| 150 | case IPPROTO_TCP: |
| 151 | client = clnttcp_create (&sin, prog, vers, &sock, 0, 0); |
| 152 | if (client == NULL) |
| 153 | { |
| 154 | return NULL; |
| 155 | } |
| 156 | #if 0 |
| 157 | /* This is not wanted. This would disable the user from having |
| 158 | a timeout in the clnt_call() call. Only a call to cnlt_control() |
| 159 | by the user should set the timeout value. */ |
| 160 | tv.tv_sec = 25; |
| 161 | tv.tv_usec = 0; |
| 162 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); |
| 163 | #endif |
| 164 | break; |
| 165 | default: |
| 166 | { |
| 167 | struct rpc_createerr *ce = &get_rpc_createerr (); |
| 168 | ce->cf_stat = RPC_SYSTEMERROR; |
| 169 | ce->cf_error.re_errno = EPFNOSUPPORT; |
| 170 | } |
| 171 | return (NULL); |
| 172 | } |
| 173 | return client; |
| 174 | } |
| 175 | #ifdef EXPORT_RPC_SYMBOLS |
| 176 | libc_hidden_def (clnt_create) |
| 177 | #else |
| 178 | libc_hidden_nolink_sunrpc (clnt_create, GLIBC_2_0) |
| 179 | #endif |