1 /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 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 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */ 33 34 /* 35 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 36 * In addition, portions of such source code were derived from Berkeley 37 * 4.3 BSD under license from the Regents of the University of 38 * California. 39 */ 40 41 #if 0 42 #if !defined(lint) && defined(SCCSIDS) 43 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro"; 44 #endif 45 #endif 46 47 #ifdef PORTMAP 48 /* 49 * rpc_soc.c 50 * 51 * The backward compatibility routines for the earlier implementation 52 * of RPC, where the only transports supported were tcp/ip and udp/ip. 53 * Based on berkeley socket abstraction, now implemented on the top 54 * of TLI/Streams 55 */ 56 57 #include "namespace.h" 58 #include "reentrant.h" 59 #include <sys/types.h> 60 #include <sys/socket.h> 61 #include <stdio.h> 62 #include <rpc/rpc.h> 63 #include <rpc/pmap_clnt.h> 64 #include <rpc/pmap_prot.h> 65 #include <rpc/nettype.h> 66 #include <netinet/in.h> 67 #include <netdb.h> 68 #include <errno.h> 69 #include <syslog.h> 70 #include <stdlib.h> 71 #include <string.h> 72 #include <unistd.h> 73 74 #include "rpc_com.h" 75 76 #ifdef __weak_alias 77 __weak_alias(clntudp_bufcreate,_clntudp_bufcreate) 78 __weak_alias(clntudp_create,_clntudp_create) 79 __weak_alias(clnttcp_create,_clnttcp_create) 80 __weak_alias(clntraw_create,_clntraw_create) 81 __weak_alias(get_myaddress,_get_myaddress) 82 __weak_alias(svcfd_create,_svcfd_create) 83 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate) 84 __weak_alias(svcudp_create,_svcudp_create) 85 __weak_alias(svctcp_create,_svctcp_create) 86 __weak_alias(svcraw_create,_svcraw_create) 87 __weak_alias(callrpc,_callrpc) 88 __weak_alias(registerrpc,_registerrpc) 89 __weak_alias(clnt_broadcast,_clnt_broadcast) 90 #endif 91 92 #ifdef __REENT 93 extern mutex_t rpcsoc_lock; 94 #endif 95 96 static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t, 97 int *, u_int, u_int, char *)); 98 static SVCXPRT *svc_com_create __P((int, u_int, u_int, char *)); 99 static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *)); 100 101 /* 102 * A common clnt create routine 103 */ 104 static CLIENT * 105 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) 106 struct sockaddr_in *raddr; 107 rpcprog_t prog; 108 rpcvers_t vers; 109 int *sockp; 110 u_int sendsz; 111 u_int recvsz; 112 char *tp; 113 { 114 CLIENT *cl; 115 int madefd = FALSE; 116 int fd = *sockp; 117 struct netconfig *nconf; 118 struct netbuf bindaddr; 119 120 mutex_lock(&rpcsoc_lock); 121 if ((nconf = __rpc_getconfip(tp)) == NULL) { 122 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 123 mutex_unlock(&rpcsoc_lock); 124 return (NULL); 125 } 126 if (fd == RPC_ANYSOCK) { 127 fd = __rpc_nconf2fd(nconf); 128 if (fd == -1) 129 goto syserror; 130 madefd = TRUE; 131 } 132 133 if (raddr->sin_port == 0) { 134 u_int proto; 135 u_short sport; 136 137 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ 138 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; 139 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, 140 proto); 141 if (sport == 0) { 142 goto err; 143 } 144 raddr->sin_port = htons(sport); 145 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ 146 } 147 148 /* Transform sockaddr_in to netbuf */ 149 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); 150 bindaddr.buf = raddr; 151 152 bindresvport(fd, NULL); 153 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, 154 sendsz, recvsz); 155 if (cl) { 156 if (madefd == TRUE) { 157 /* 158 * The fd should be closed while destroying the handle. 159 */ 160 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 161 *sockp = fd; 162 } 163 (void) freenetconfigent(nconf); 164 mutex_unlock(&rpcsoc_lock); 165 return (cl); 166 } 167 goto err; 168 169 syserror: 170 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 171 rpc_createerr.cf_error.re_errno = errno; 172 173 err: if (madefd == TRUE) 174 (void) close(fd); 175 (void) freenetconfigent(nconf); 176 mutex_unlock(&rpcsoc_lock); 177 return (NULL); 178 } 179 180 CLIENT * 181 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) 182 struct sockaddr_in *raddr; 183 u_long prog; 184 u_long vers; 185 struct timeval wait; 186 int *sockp; 187 u_int sendsz; 188 u_int recvsz; 189 { 190 CLIENT *cl; 191 192 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 193 sendsz, recvsz, "udp"); 194 if (cl == NULL) { 195 return (NULL); 196 } 197 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait); 198 return (cl); 199 } 200 201 CLIENT * 202 clntudp_create(raddr, program, version, wait, sockp) 203 struct sockaddr_in *raddr; 204 u_long program; 205 u_long version; 206 struct timeval wait; 207 int *sockp; 208 { 209 return clntudp_bufcreate(raddr, program, version, wait, sockp, 210 UDPMSGSIZE, UDPMSGSIZE); 211 } 212 213 CLIENT * 214 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) 215 struct sockaddr_in *raddr; 216 u_long prog; 217 u_long vers; 218 int *sockp; 219 u_int sendsz; 220 u_int recvsz; 221 { 222 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 223 sendsz, recvsz, "tcp"); 224 } 225 226 CLIENT * 227 clntraw_create(prog, vers) 228 u_long prog; 229 u_long vers; 230 { 231 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); 232 } 233 234 /* 235 * A common server create routine 236 */ 237 static SVCXPRT * 238 svc_com_create(fd, sendsize, recvsize, netid) 239 int fd; 240 u_int sendsize; 241 u_int recvsize; 242 char *netid; 243 { 244 struct netconfig *nconf; 245 SVCXPRT *svc; 246 int madefd = FALSE; 247 int port; 248 struct sockaddr_in sin; 249 250 if ((nconf = __rpc_getconfip(netid)) == NULL) { 251 (void) syslog(LOG_ERR, "Could not get %s transport", netid); 252 return (NULL); 253 } 254 if (fd == RPC_ANYSOCK) { 255 fd = __rpc_nconf2fd(nconf); 256 if (fd == -1) { 257 (void) freenetconfigent(nconf); 258 (void) syslog(LOG_ERR, 259 "svc%s_create: could not open connection", netid); 260 return (NULL); 261 } 262 madefd = TRUE; 263 } 264 265 memset(&sin, 0, sizeof sin); 266 sin.sin_family = AF_INET; 267 bindresvport(fd, &sin); 268 listen(fd, SOMAXCONN); 269 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); 270 (void) freenetconfigent(nconf); 271 if (svc == NULL) { 272 if (madefd) 273 (void) close(fd); 274 return (NULL); 275 } 276 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); 277 svc->xp_port = ntohs(port); 278 return (svc); 279 } 280 281 SVCXPRT * 282 svctcp_create(fd, sendsize, recvsize) 283 int fd; 284 u_int sendsize; 285 u_int recvsize; 286 { 287 return svc_com_create(fd, sendsize, recvsize, "tcp"); 288 } 289 290 SVCXPRT * 291 svcudp_bufcreate(fd, sendsz, recvsz) 292 int fd; 293 u_int sendsz, recvsz; 294 { 295 return svc_com_create(fd, sendsz, recvsz, "udp"); 296 } 297 298 SVCXPRT * 299 svcfd_create(fd, sendsize, recvsize) 300 int fd; 301 u_int sendsize; 302 u_int recvsize; 303 { 304 return svc_fd_create(fd, sendsize, recvsize); 305 } 306 307 308 SVCXPRT * 309 svcudp_create(fd) 310 int fd; 311 { 312 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); 313 } 314 315 SVCXPRT * 316 svcraw_create() 317 { 318 return svc_raw_create(); 319 } 320 321 int 322 get_myaddress(addr) 323 struct sockaddr_in *addr; 324 { 325 memset((void *) addr, 0, sizeof(*addr)); 326 addr->sin_family = AF_INET; 327 addr->sin_port = htons(PMAPPORT); 328 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 329 return (0); 330 } 331 332 /* 333 * For connectionless "udp" transport. Obsoleted by rpc_call(). 334 */ 335 int 336 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 337 char *host; 338 int prognum, versnum, procnum; 339 xdrproc_t inproc, outproc; 340 char *in, *out; 341 { 342 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, 343 (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); 344 } 345 346 /* 347 * For connectionless kind of transport. Obsoleted by rpc_reg() 348 */ 349 int 350 registerrpc(prognum, versnum, procnum, progname, inproc, outproc) 351 int prognum, versnum, procnum; 352 char *(*progname) __P((char [UDPMSGSIZE])); 353 xdrproc_t inproc, outproc; 354 { 355 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum, 356 (rpcproc_t)procnum, progname, inproc, outproc, "udp"); 357 } 358 359 /* 360 * All the following clnt_broadcast stuff is convulated; it supports 361 * the earlier calling style of the callback function 362 */ 363 #ifdef __REENT 364 static thread_key_t clnt_broadcast_key; 365 #endif 366 static resultproc_t clnt_broadcast_result_main; 367 368 /* 369 * Need to translate the netbuf address into sockaddr_in address. 370 * Dont care about netid here. 371 */ 372 /* ARGSUSED */ 373 static bool_t 374 rpc_wrap_bcast(resultp, addr, nconf) 375 char *resultp; /* results of the call */ 376 struct netbuf *addr; /* address of the guy who responded */ 377 struct netconfig *nconf; /* Netconf of the transport */ 378 { 379 resultproc_t clnt_broadcast_result; 380 381 if (strcmp(nconf->nc_netid, "udp")) 382 return (FALSE); 383 #ifdef __REENT 384 if (_thr_main()) 385 clnt_broadcast_result = clnt_broadcast_result_main; 386 else 387 thr_getspecific(clnt_broadcast_key, 388 (void **) &clnt_broadcast_result); 389 #else 390 clnt_broadcast_result = clnt_broadcast_result_main; 391 #endif 392 return (*clnt_broadcast_result)(resultp, 393 (struct sockaddr_in *)addr->buf); 394 } 395 396 /* 397 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). 398 */ 399 enum clnt_stat 400 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 401 u_long prog; /* program number */ 402 u_long vers; /* version number */ 403 u_long proc; /* procedure number */ 404 xdrproc_t xargs; /* xdr routine for args */ 405 caddr_t argsp; /* pointer to args */ 406 xdrproc_t xresults; /* xdr routine for results */ 407 caddr_t resultsp; /* pointer to results */ 408 resultproc_t eachresult; /* call with each result obtained */ 409 { 410 #ifdef __REENT 411 extern mutex_t tsd_lock; 412 #endif 413 414 #ifdef __REENT 415 if (_thr_main()) 416 clnt_broadcast_result_main = eachresult; 417 else { 418 if (clnt_broadcast_key == 0) { 419 mutex_lock(&tsd_lock); 420 if (clnt_broadcast_key == 0) 421 thr_keycreate(&clnt_broadcast_key, free); 422 mutex_unlock(&tsd_lock); 423 } 424 thr_setspecific(clnt_broadcast_key, (void *) eachresult); 425 } 426 #else 427 clnt_broadcast_result_main = eachresult; 428 #endif 429 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers, 430 (rpcproc_t)proc, xargs, argsp, xresults, resultsp, 431 (resultproc_t) rpc_wrap_bcast, "udp"); 432 } 433 434 #endif /* PORTMAP */ 435