1 /* $NetBSD: clnt_generic.c,v 1.26 2006/06/22 19:35:34 christos Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 /* 32 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33 */ 34 35 /* #ident "@(#)clnt_generic.c 1.20 94/05/03 SMI" */ 36 37 #include <sys/cdefs.h> 38 #if defined(LIBC_SCCS) && !defined(lint) 39 #if 0 40 static char sccsid[] = "@(#)clnt_generic.c 1.32 89/03/16 Copyr 1988 Sun Micro"; 41 #else 42 __RCSID("$NetBSD: clnt_generic.c,v 1.26 2006/06/22 19:35:34 christos Exp $"); 43 #endif 44 #endif 45 46 #include "namespace.h" 47 #include "reentrant.h" 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 #include <netinet/in.h> 51 #include <assert.h> 52 #include <stdio.h> 53 #include <errno.h> 54 #include <rpc/rpc.h> 55 #include <rpc/nettype.h> 56 #include <string.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 #include "rpc_internal.h" 60 61 #ifdef __weak_alias 62 __weak_alias(clnt_create_vers,_clnt_create_vers) 63 __weak_alias(clnt_create,_clnt_create) 64 __weak_alias(clnt_tp_create,_clnt_tp_create) 65 __weak_alias(clnt_tli_create,_clnt_tli_create) 66 #endif 67 68 /* 69 * Generic client creation with version checking the value of 70 * vers_out is set to the highest server supported value 71 * vers_low <= vers_out <= vers_high AND an error results 72 * if this can not be done. 73 */ 74 CLIENT * 75 clnt_create_vers(hostname, prog, vers_out, vers_low, vers_high, nettype) 76 const char *hostname; 77 rpcprog_t prog; 78 rpcvers_t *vers_out; 79 rpcvers_t vers_low; 80 rpcvers_t vers_high; 81 const char *nettype; 82 { 83 CLIENT *clnt; 84 struct timeval to; 85 enum clnt_stat rpc_stat; 86 struct rpc_err rpcerr; 87 88 _DIAGASSERT(hostname != NULL); 89 _DIAGASSERT(vers_out != NULL); 90 /* XXX: nettype appears to support being NULL */ 91 92 clnt = clnt_create(hostname, prog, vers_high, nettype); 93 if (clnt == NULL) { 94 return (NULL); 95 } 96 to.tv_sec = 10; 97 to.tv_usec = 0; 98 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, 99 (char *) NULL, (xdrproc_t) xdr_void, (char *) NULL, to); 100 if (rpc_stat == RPC_SUCCESS) { 101 *vers_out = vers_high; 102 return (clnt); 103 } 104 if (rpc_stat == RPC_PROGVERSMISMATCH) { 105 unsigned long minvers, maxvers; 106 107 clnt_geterr(clnt, &rpcerr); 108 minvers = rpcerr.re_vers.low; 109 maxvers = rpcerr.re_vers.high; 110 if (maxvers < vers_high) 111 vers_high = (rpcvers_t)maxvers; 112 if (minvers > vers_low) 113 vers_low = (rpcvers_t)minvers; 114 if (vers_low > vers_high) { 115 goto error; 116 } 117 CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high); 118 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, 119 (char *) NULL, (xdrproc_t) xdr_void, 120 (char *) NULL, to); 121 if (rpc_stat == RPC_SUCCESS) { 122 *vers_out = vers_high; 123 return (clnt); 124 } 125 } 126 clnt_geterr(clnt, &rpcerr); 127 128 error: 129 rpc_createerr.cf_stat = rpc_stat; 130 rpc_createerr.cf_error = rpcerr; 131 clnt_destroy(clnt); 132 return (NULL); 133 } 134 135 /* 136 * Top level client creation routine. 137 * Generic client creation: takes (servers name, program-number, nettype) and 138 * returns client handle. Default options are set, which the user can 139 * change using the rpc equivalent of ioctl()'s. 140 * 141 * It tries for all the netids in that particular class of netid until 142 * it succeeds. 143 * XXX The error message in the case of failure will be the one 144 * pertaining to the last create error. 145 * 146 * It calls clnt_tp_create(); 147 */ 148 CLIENT * 149 clnt_create(hostname, prog, vers, nettype) 150 const char *hostname; /* server name */ 151 rpcprog_t prog; /* program number */ 152 rpcvers_t vers; /* version number */ 153 const char *nettype; /* net type */ 154 { 155 struct netconfig *nconf; 156 CLIENT *clnt = NULL; 157 void *handle; 158 enum clnt_stat save_cf_stat = RPC_SUCCESS; 159 struct rpc_err save_cf_error; 160 161 _DIAGASSERT(hostname != NULL); 162 /* XXX: nettype appears to support being NULL */ 163 164 if ((handle = __rpc_setconf(nettype)) == NULL) { 165 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 166 return (NULL); 167 } 168 rpc_createerr.cf_stat = RPC_SUCCESS; 169 while (clnt == NULL) { 170 if ((nconf = __rpc_getconf(handle)) == NULL) { 171 if (rpc_createerr.cf_stat == RPC_SUCCESS) 172 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 173 break; 174 } 175 #ifdef CLNT_DEBUG 176 printf("trying netid %s\n", nconf->nc_netid); 177 #endif 178 clnt = clnt_tp_create(hostname, prog, vers, nconf); 179 if (clnt) 180 break; 181 else 182 /* 183 * Since we didn't get a name-to-address 184 * translation failure here, we remember 185 * this particular error. The object of 186 * this is to enable us to return to the 187 * caller a more-specific error than the 188 * unhelpful ``Name to address translation 189 * failed'' which might well occur if we 190 * merely returned the last error (because 191 * the local loopbacks are typically the 192 * last ones in /etc/netconfig and the most 193 * likely to be unable to translate a host 194 * name). 195 */ 196 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) { 197 save_cf_stat = rpc_createerr.cf_stat; 198 save_cf_error = rpc_createerr.cf_error; 199 } 200 } 201 202 /* 203 * Attempt to return an error more specific than ``Name to address 204 * translation failed'' 205 */ 206 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) && 207 (save_cf_stat != RPC_SUCCESS)) { 208 rpc_createerr.cf_stat = save_cf_stat; 209 rpc_createerr.cf_error = save_cf_error; 210 } 211 __rpc_endconf(handle); 212 return (clnt); 213 } 214 215 /* 216 * Generic client creation: takes (servers name, program-number, netconf) and 217 * returns client handle. Default options are set, which the user can 218 * change using the rpc equivalent of ioctl()'s : clnt_control() 219 * It finds out the server address from rpcbind and calls clnt_tli_create() 220 */ 221 CLIENT * 222 clnt_tp_create(hostname, prog, vers, nconf) 223 const char *hostname; /* server name */ 224 rpcprog_t prog; /* program number */ 225 rpcvers_t vers; /* version number */ 226 const struct netconfig *nconf; /* net config struct */ 227 { 228 struct netbuf *svcaddr; /* servers address */ 229 CLIENT *cl = NULL; /* client handle */ 230 231 _DIAGASSERT(hostname != NULL); 232 /* nconf is handled below */ 233 234 if (nconf == NULL) { 235 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 236 return (NULL); 237 } 238 239 /* 240 * Get the address of the server 241 */ 242 if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname, 243 &cl)) == NULL) { 244 /* appropriate error number is set by rpcbind libraries */ 245 return (NULL); 246 } 247 if (cl == NULL) { 248 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, 249 prog, vers, 0, 0); 250 } else { 251 /* Reuse the CLIENT handle and change the appropriate fields */ 252 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) { 253 if (cl->cl_netid == NULL) 254 cl->cl_netid = strdup(nconf->nc_netid); 255 if (cl->cl_tp == NULL) 256 cl->cl_tp = strdup(nconf->nc_device); 257 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog); 258 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers); 259 } else { 260 CLNT_DESTROY(cl); 261 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, 262 prog, vers, 0, 0); 263 } 264 } 265 free(svcaddr->buf); 266 free(svcaddr); 267 return (cl); 268 } 269 270 /* 271 * Generic client creation: returns client handle. 272 * Default options are set, which the user can 273 * change using the rpc equivalent of ioctl()'s : clnt_control(). 274 * If fd is RPC_ANYFD, it will be opened using nconf. 275 * It will be bound if not so. 276 * If sizes are 0; appropriate defaults will be chosen. 277 */ 278 CLIENT * 279 clnt_tli_create(fd, nconf, svcaddr, prog, vers, sendsz, recvsz) 280 int fd; /* fd */ 281 const struct netconfig *nconf; /* netconfig structure */ 282 const struct netbuf *svcaddr; /* servers address */ 283 rpcprog_t prog; /* program number */ 284 rpcvers_t vers; /* version number */ 285 u_int sendsz; /* send size */ 286 u_int recvsz; /* recv size */ 287 { 288 CLIENT *cl; /* client handle */ 289 bool_t madefd = FALSE; /* whether fd opened here */ 290 long servtype; 291 struct __rpc_sockinfo si; 292 293 /* nconf is handled below */ 294 _DIAGASSERT(svcaddr != NULL); 295 296 if (fd == RPC_ANYFD) { 297 if (nconf == NULL) { 298 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 299 return (NULL); 300 } 301 302 fd = __rpc_nconf2fd(nconf); 303 304 if (fd == -1) 305 goto err; 306 307 madefd = TRUE; 308 servtype = nconf->nc_semantics; 309 if (!__rpc_fd2sockinfo(fd, &si)) 310 goto err; 311 312 bindresvport(fd, NULL); 313 } else { 314 if (!__rpc_fd2sockinfo(fd, &si)) 315 goto err; 316 servtype = __rpc_socktype2seman(si.si_socktype); 317 if (servtype == -1) { 318 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 319 return NULL; 320 } 321 322 } 323 324 if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) { 325 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; /* XXX */ 326 goto err1; 327 } 328 329 switch (servtype) { 330 case NC_TPI_COTS_ORD: 331 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz); 332 if (!nconf || !cl) 333 break; 334 __rpc_setnodelay(fd, &si); 335 break; 336 case NC_TPI_CLTS: 337 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz); 338 break; 339 default: 340 goto err; 341 } 342 343 if (cl == NULL) 344 goto err1; /* borrow errors from clnt_dg/vc creates */ 345 if (nconf) { 346 cl->cl_netid = strdup(nconf->nc_netid); 347 cl->cl_tp = strdup(nconf->nc_device); 348 } else { 349 cl->cl_netid = __UNCONST(""); 350 cl->cl_tp = __UNCONST(""); 351 } 352 if (madefd) { 353 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 354 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, (char *) NULL); */ 355 }; 356 357 return (cl); 358 359 err: 360 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 361 rpc_createerr.cf_error.re_errno = errno; 362 err1: if (madefd) 363 (void) close(fd); 364 return (NULL); 365 } 366