1 /* $NetBSD: clnt_generic.c,v 1.22 2003/09/09 03:56:39 itojun 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.22 2003/09/09 03:56:39 itojun 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 <netinet/tcp.h> 52 #include <assert.h> 53 #include <stdio.h> 54 #include <errno.h> 55 #include <rpc/rpc.h> 56 #include <rpc/nettype.h> 57 #include <string.h> 58 #include <stdlib.h> 59 #include <unistd.h> 60 #include "rpc_internal.h" 61 62 #ifdef __weak_alias 63 __weak_alias(clnt_create_vers,_clnt_create_vers) 64 __weak_alias(clnt_create,_clnt_create) 65 __weak_alias(clnt_tp_create,_clnt_tp_create) 66 __weak_alias(clnt_tli_create,_clnt_tli_create) 67 #endif 68 69 /* 70 * Generic client creation with version checking the value of 71 * vers_out is set to the highest server supported value 72 * vers_low <= vers_out <= vers_high AND an error results 73 * if this can not be done. 74 */ 75 CLIENT * 76 clnt_create_vers(hostname, prog, vers_out, vers_low, vers_high, nettype) 77 const char *hostname; 78 rpcprog_t prog; 79 rpcvers_t *vers_out; 80 rpcvers_t vers_low; 81 rpcvers_t vers_high; 82 const char *nettype; 83 { 84 CLIENT *clnt; 85 struct timeval to; 86 enum clnt_stat rpc_stat; 87 struct rpc_err rpcerr; 88 89 _DIAGASSERT(hostname != NULL); 90 _DIAGASSERT(vers_out != NULL); 91 /* XXX: nettype appears to support being NULL */ 92 93 clnt = clnt_create(hostname, prog, vers_high, nettype); 94 if (clnt == NULL) { 95 return (NULL); 96 } 97 to.tv_sec = 10; 98 to.tv_usec = 0; 99 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, 100 (char *) NULL, (xdrproc_t) xdr_void, (char *) NULL, to); 101 if (rpc_stat == RPC_SUCCESS) { 102 *vers_out = vers_high; 103 return (clnt); 104 } 105 if (rpc_stat == RPC_PROGVERSMISMATCH) { 106 unsigned long minvers, maxvers; 107 108 clnt_geterr(clnt, &rpcerr); 109 minvers = rpcerr.re_vers.low; 110 maxvers = rpcerr.re_vers.high; 111 if (maxvers < vers_high) 112 vers_high = (rpcvers_t)maxvers; 113 if (minvers > vers_low) 114 vers_low = (rpcvers_t)minvers; 115 if (vers_low > vers_high) { 116 goto error; 117 } 118 CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high); 119 rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, 120 (char *) NULL, (xdrproc_t) xdr_void, 121 (char *) NULL, to); 122 if (rpc_stat == RPC_SUCCESS) { 123 *vers_out = vers_high; 124 return (clnt); 125 } 126 } 127 clnt_geterr(clnt, &rpcerr); 128 129 error: 130 rpc_createerr.cf_stat = rpc_stat; 131 rpc_createerr.cf_error = rpcerr; 132 clnt_destroy(clnt); 133 return (NULL); 134 } 135 136 /* 137 * Top level client creation routine. 138 * Generic client creation: takes (servers name, program-number, nettype) and 139 * returns client handle. Default options are set, which the user can 140 * change using the rpc equivalent of ioctl()'s. 141 * 142 * It tries for all the netids in that particular class of netid until 143 * it succeeds. 144 * XXX The error message in the case of failure will be the one 145 * pertaining to the last create error. 146 * 147 * It calls clnt_tp_create(); 148 */ 149 CLIENT * 150 clnt_create(hostname, prog, vers, nettype) 151 const char *hostname; /* server name */ 152 rpcprog_t prog; /* program number */ 153 rpcvers_t vers; /* version number */ 154 const char *nettype; /* net type */ 155 { 156 struct netconfig *nconf; 157 CLIENT *clnt = NULL; 158 void *handle; 159 enum clnt_stat save_cf_stat = RPC_SUCCESS; 160 struct rpc_err save_cf_error; 161 162 _DIAGASSERT(hostname != NULL); 163 /* XXX: nettype appears to support being NULL */ 164 165 if ((handle = __rpc_setconf(nettype)) == NULL) { 166 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 167 return (NULL); 168 } 169 rpc_createerr.cf_stat = RPC_SUCCESS; 170 while (clnt == NULL) { 171 if ((nconf = __rpc_getconf(handle)) == NULL) { 172 if (rpc_createerr.cf_stat == RPC_SUCCESS) 173 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 174 break; 175 } 176 #ifdef CLNT_DEBUG 177 printf("trying netid %s\n", nconf->nc_netid); 178 #endif 179 clnt = clnt_tp_create(hostname, prog, vers, nconf); 180 if (clnt) 181 break; 182 else 183 /* 184 * Since we didn't get a name-to-address 185 * translation failure here, we remember 186 * this particular error. The object of 187 * this is to enable us to return to the 188 * caller a more-specific error than the 189 * unhelpful ``Name to address translation 190 * failed'' which might well occur if we 191 * merely returned the last error (because 192 * the local loopbacks are typically the 193 * last ones in /etc/netconfig and the most 194 * likely to be unable to translate a host 195 * name). 196 */ 197 if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) { 198 save_cf_stat = rpc_createerr.cf_stat; 199 save_cf_error = rpc_createerr.cf_error; 200 } 201 } 202 203 /* 204 * Attempt to return an error more specific than ``Name to address 205 * translation failed'' 206 */ 207 if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) && 208 (save_cf_stat != RPC_SUCCESS)) { 209 rpc_createerr.cf_stat = save_cf_stat; 210 rpc_createerr.cf_error = save_cf_error; 211 } 212 __rpc_endconf(handle); 213 return (clnt); 214 } 215 216 /* 217 * Generic client creation: takes (servers name, program-number, netconf) and 218 * returns client handle. Default options are set, which the user can 219 * change using the rpc equivalent of ioctl()'s : clnt_control() 220 * It finds out the server address from rpcbind and calls clnt_tli_create() 221 */ 222 CLIENT * 223 clnt_tp_create(hostname, prog, vers, nconf) 224 const char *hostname; /* server name */ 225 rpcprog_t prog; /* program number */ 226 rpcvers_t vers; /* version number */ 227 const struct netconfig *nconf; /* net config struct */ 228 { 229 struct netbuf *svcaddr; /* servers address */ 230 CLIENT *cl = NULL; /* client handle */ 231 232 _DIAGASSERT(hostname != NULL); 233 /* nconf is handled below */ 234 235 if (nconf == NULL) { 236 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 237 return (NULL); 238 } 239 240 /* 241 * Get the address of the server 242 */ 243 if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname, 244 &cl)) == NULL) { 245 /* appropriate error number is set by rpcbind libraries */ 246 return (NULL); 247 } 248 if (cl == NULL) { 249 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, 250 prog, vers, 0, 0); 251 } else { 252 /* Reuse the CLIENT handle and change the appropriate fields */ 253 if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) { 254 if (cl->cl_netid == NULL) 255 cl->cl_netid = strdup(nconf->nc_netid); 256 if (cl->cl_tp == NULL) 257 cl->cl_tp = strdup(nconf->nc_device); 258 (void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog); 259 (void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers); 260 } else { 261 CLNT_DESTROY(cl); 262 cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, 263 prog, vers, 0, 0); 264 } 265 } 266 free(svcaddr->buf); 267 free(svcaddr); 268 return (cl); 269 } 270 271 /* 272 * Generic client creation: returns client handle. 273 * Default options are set, which the user can 274 * change using the rpc equivalent of ioctl()'s : clnt_control(). 275 * If fd is RPC_ANYFD, it will be opened using nconf. 276 * It will be bound if not so. 277 * If sizes are 0; appropriate defaults will be chosen. 278 */ 279 CLIENT * 280 clnt_tli_create(fd, nconf, svcaddr, prog, vers, sendsz, recvsz) 281 int fd; /* fd */ 282 const struct netconfig *nconf; /* netconfig structure */ 283 const struct netbuf *svcaddr; /* servers address */ 284 rpcprog_t prog; /* program number */ 285 rpcvers_t vers; /* version number */ 286 u_int sendsz; /* send size */ 287 u_int recvsz; /* recv size */ 288 { 289 CLIENT *cl; /* client handle */ 290 bool_t madefd = FALSE; /* whether fd opened here */ 291 long servtype; 292 int one = 1; 293 struct __rpc_sockinfo si; 294 295 /* nconf is handled below */ 296 _DIAGASSERT(svcaddr != NULL); 297 298 if (fd == RPC_ANYFD) { 299 if (nconf == NULL) { 300 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 301 return (NULL); 302 } 303 304 fd = __rpc_nconf2fd(nconf); 305 306 if (fd == -1) 307 goto err; 308 309 madefd = TRUE; 310 servtype = nconf->nc_semantics; 311 if (!__rpc_fd2sockinfo(fd, &si)) 312 goto err; 313 314 bindresvport(fd, NULL); 315 } else { 316 if (!__rpc_fd2sockinfo(fd, &si)) 317 goto err; 318 servtype = __rpc_socktype2seman(si.si_socktype); 319 if (servtype == -1) { 320 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 321 return NULL; 322 } 323 324 } 325 326 if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) { 327 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; /* XXX */ 328 goto err1; 329 } 330 331 switch (servtype) { 332 case NC_TPI_COTS_ORD: 333 cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz); 334 if (!nconf || !cl) 335 break; 336 /* XXX fvdl - is this useful? */ 337 if (strncmp(nconf->nc_protofmly, "inet", 4) == 0) 338 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, 339 sizeof (one)); 340 break; 341 case NC_TPI_CLTS: 342 cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz); 343 break; 344 default: 345 goto err; 346 } 347 348 if (cl == NULL) 349 goto err1; /* borrow errors from clnt_dg/vc creates */ 350 if (nconf) { 351 cl->cl_netid = strdup(nconf->nc_netid); 352 cl->cl_tp = strdup(nconf->nc_device); 353 } else { 354 cl->cl_netid = ""; 355 cl->cl_tp = ""; 356 } 357 if (madefd) { 358 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 359 /* (void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, (char *) NULL); */ 360 }; 361 362 return (cl); 363 364 err: 365 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 366 rpc_createerr.cf_error.re_errno = errno; 367 err1: if (madefd) 368 (void) close(fd); 369 return (NULL); 370 } 371