47 48#ifdef PORTMAP 49/* 50 * rpc_soc.c 51 * 52 * The backward compatibility routines for the earlier implementation 53 * of RPC, where the only transports supported were tcp/ip and udp/ip. 54 * Based on berkeley socket abstraction, now implemented on the top 55 * of TLI/Streams 56 */ 57 58#include "namespace.h" 59#include "reentrant.h" 60#include <sys/types.h> 61#include <sys/socket.h> 62#include <stdio.h> 63#include <rpc/rpc.h> 64#include <rpc/pmap_clnt.h> 65#include <rpc/pmap_prot.h> 66#include <rpc/nettype.h> 67#include <syslog.h> 68#include <netinet/in.h> 69#include <netdb.h> 70#include <errno.h> 71#include <syslog.h> 72#include <stdlib.h> 73#include <string.h> 74#include <unistd.h> 75#include "un-namespace.h" 76 77#include "rpc_com.h" 78 79extern mutex_t rpcsoc_lock; 80 81static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t, 82 int *, u_int, u_int, char *); 83static SVCXPRT *svc_com_create(int, u_int, u_int, char *); 84static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *); 85 86/* XXX */ 87#define IN4_LOCALHOST_STRING "127.0.0.1" 88#define IN6_LOCALHOST_STRING "::1" 89 90/* 91 * A common clnt create routine 92 */ 93static CLIENT * 94clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) 95 struct sockaddr_in *raddr; 96 rpcprog_t prog; 97 rpcvers_t vers; 98 int *sockp; 99 u_int sendsz; 100 u_int recvsz; 101 char *tp; 102{ 103 CLIENT *cl; 104 int madefd = FALSE; 105 int fd = *sockp; 106 struct netconfig *nconf; 107 struct netbuf bindaddr; 108 109 mutex_lock(&rpcsoc_lock); 110 if ((nconf = __rpc_getconfip(tp)) == NULL) { 111 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 112 mutex_unlock(&rpcsoc_lock); 113 return (NULL); 114 } 115 if (fd == RPC_ANYSOCK) { 116 fd = __rpc_nconf2fd(nconf); 117 if (fd == -1) 118 goto syserror; 119 madefd = TRUE; 120 } 121 122 if (raddr->sin_port == 0) { 123 u_int proto; 124 u_short sport; 125 126 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ 127 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; 128 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, 129 proto); 130 if (sport == 0) { 131 goto err; 132 } 133 raddr->sin_port = htons(sport); 134 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ 135 } 136 137 /* Transform sockaddr_in to netbuf */ 138 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); 139 bindaddr.buf = raddr; 140 141 bindresvport(fd, NULL); 142 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, 143 sendsz, recvsz); 144 if (cl) { 145 if (madefd == TRUE) { 146 /* 147 * The fd should be closed while destroying the handle. 148 */ 149 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 150 *sockp = fd; 151 } 152 (void) freenetconfigent(nconf); 153 mutex_unlock(&rpcsoc_lock); 154 return (cl); 155 } 156 goto err; 157 158syserror: 159 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 160 rpc_createerr.cf_error.re_errno = errno; 161 162err: if (madefd == TRUE) 163 (void)_close(fd); 164 (void) freenetconfigent(nconf); 165 mutex_unlock(&rpcsoc_lock); 166 return (NULL); 167} 168 169CLIENT * 170clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) 171 struct sockaddr_in *raddr; 172 u_long prog; 173 u_long vers; 174 struct timeval wait; 175 int *sockp; 176 u_int sendsz; 177 u_int recvsz; 178{ 179 CLIENT *cl; 180 181 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 182 sendsz, recvsz, "udp"); 183 if (cl == NULL) { 184 return (NULL); 185 } 186 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait); 187 return (cl); 188} 189 190CLIENT * 191clntudp_create(raddr, program, version, wait, sockp) 192 struct sockaddr_in *raddr; 193 u_long program; 194 u_long version; 195 struct timeval wait; 196 int *sockp; 197{ 198 199 return clntudp_bufcreate(raddr, program, version, wait, sockp, 200 UDPMSGSIZE, UDPMSGSIZE); 201} 202 203CLIENT * 204clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) 205 struct sockaddr_in *raddr; 206 u_long prog; 207 u_long vers; 208 int *sockp; 209 u_int sendsz; 210 u_int recvsz; 211{ 212 213 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 214 sendsz, recvsz, "tcp"); 215} 216 217CLIENT * 218clntraw_create(prog, vers) 219 u_long prog; 220 u_long vers; 221{ 222 223 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); 224} 225 226/* 227 * A common server create routine 228 */ 229static SVCXPRT * 230svc_com_create(fd, sendsize, recvsize, netid) 231 int fd; 232 u_int sendsize; 233 u_int recvsize; 234 char *netid; 235{ 236 struct netconfig *nconf; 237 SVCXPRT *svc; 238 int madefd = FALSE; 239 int port; 240 struct sockaddr_in sin; 241 242 if ((nconf = __rpc_getconfip(netid)) == NULL) { 243 (void) syslog(LOG_ERR, "Could not get %s transport", netid); 244 return (NULL); 245 } 246 if (fd == RPC_ANYSOCK) { 247 fd = __rpc_nconf2fd(nconf); 248 if (fd == -1) { 249 (void) freenetconfigent(nconf); 250 (void) syslog(LOG_ERR, 251 "svc%s_create: could not open connection", netid); 252 return (NULL); 253 } 254 madefd = TRUE; 255 } 256 257 memset(&sin, 0, sizeof sin); 258 sin.sin_family = AF_INET; 259 bindresvport(fd, &sin); 260 _listen(fd, SOMAXCONN); 261 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); 262 (void) freenetconfigent(nconf); 263 if (svc == NULL) { 264 if (madefd) 265 (void)_close(fd); 266 return (NULL); 267 } 268 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); 269 svc->xp_port = ntohs(port); 270 return (svc); 271} 272 273SVCXPRT * 274svctcp_create(fd, sendsize, recvsize) 275 int fd; 276 u_int sendsize; 277 u_int recvsize; 278{ 279 280 return svc_com_create(fd, sendsize, recvsize, "tcp"); 281} 282 283SVCXPRT * 284svcudp_bufcreate(fd, sendsz, recvsz) 285 int fd; 286 u_int sendsz, recvsz; 287{ 288 289 return svc_com_create(fd, sendsz, recvsz, "udp"); 290} 291 292SVCXPRT * 293svcfd_create(fd, sendsize, recvsize) 294 int fd; 295 u_int sendsize; 296 u_int recvsize; 297{ 298 299 return svc_fd_create(fd, sendsize, recvsize); 300} 301 302 303SVCXPRT * 304svcudp_create(fd) 305 int fd; 306{ 307 308 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); 309} 310 311SVCXPRT * 312svcraw_create() 313{ 314 315 return svc_raw_create(); 316} 317 318int 319get_myaddress(addr) 320 struct sockaddr_in *addr; 321{ 322 323 memset((void *) addr, 0, sizeof(*addr)); 324 addr->sin_family = AF_INET; 325 addr->sin_port = htons(PMAPPORT); 326 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 327 return (0); 328} 329 330/* 331 * For connectionless "udp" transport. Obsoleted by rpc_call(). 332 */ 333int 334callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 335 const char *host; 336 int prognum, versnum, procnum; 337 xdrproc_t inproc, outproc; 338 void *in, *out; 339{ 340 341 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, 342 (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); 343} 344 345/* 346 * For connectionless kind of transport. Obsoleted by rpc_reg() 347 */ 348int 349registerrpc(prognum, versnum, procnum, progname, inproc, outproc) 350 int prognum, versnum, procnum; 351 char *(*progname)(char [UDPMSGSIZE]); 352 xdrproc_t inproc, outproc; 353{ 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 */ 363static thread_key_t clnt_broadcast_key; 364static resultproc_t clnt_broadcast_result_main; 365 366/* 367 * Need to translate the netbuf address into sockaddr_in address. 368 * Dont care about netid here. 369 */ 370/* ARGSUSED */ 371static bool_t 372rpc_wrap_bcast(resultp, addr, nconf) 373 char *resultp; /* results of the call */ 374 struct netbuf *addr; /* address of the guy who responded */ 375 struct netconfig *nconf; /* Netconf of the transport */ 376{ 377 resultproc_t clnt_broadcast_result; 378 379 if (strcmp(nconf->nc_netid, "udp")) 380 return (FALSE); 381 if (thr_main()) 382 clnt_broadcast_result = clnt_broadcast_result_main; 383 else 384 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key); 385 return (*clnt_broadcast_result)(resultp, 386 (struct sockaddr_in *)addr->buf); 387} 388 389/* 390 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). 391 */ 392enum clnt_stat 393clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 394 u_long prog; /* program number */ 395 u_long vers; /* version number */ 396 u_long proc; /* procedure number */ 397 xdrproc_t xargs; /* xdr routine for args */ 398 caddr_t argsp; /* pointer to args */ 399 xdrproc_t xresults; /* xdr routine for results */ 400 caddr_t resultsp; /* pointer to results */ 401 resultproc_t eachresult; /* call with each result obtained */ 402{ 403 extern mutex_t tsd_lock; 404 405 if (thr_main()) 406 clnt_broadcast_result_main = eachresult; 407 else { 408 if (clnt_broadcast_key == 0) { 409 mutex_lock(&tsd_lock); 410 if (clnt_broadcast_key == 0) 411 thr_keycreate(&clnt_broadcast_key, free); 412 mutex_unlock(&tsd_lock); 413 } 414 thr_setspecific(clnt_broadcast_key, (void *) eachresult); 415 } 416 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers, 417 (rpcproc_t)proc, xargs, argsp, xresults, resultsp, 418 (resultproc_t) rpc_wrap_bcast, "udp"); 419} 420 421/* 422 * Create the client des authentication object. Obsoleted by 423 * authdes_seccreate(). 424 */ 425AUTH * 426authdes_create(servername, window, syncaddr, ckey) 427 char *servername; /* network name of server */ 428 u_int window; /* time to live */ 429 struct sockaddr *syncaddr; /* optional hostaddr to sync with */ 430 des_block *ckey; /* optional conversation key to use */ 431{ 432 AUTH *dummy; 433 AUTH *nauth; 434 char hostname[NI_MAXHOST]; 435 436 if (syncaddr) { 437 /* 438 * Change addr to hostname, because that is the way 439 * new interface takes it. 440 */ 441 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname, 442 sizeof hostname, NULL, 0, 0) != 0) 443 goto fallback; 444 445 nauth = authdes_seccreate(servername, window, hostname, ckey); 446 return (nauth); 447 } 448fallback: 449 dummy = authdes_seccreate(servername, window, NULL, ckey); 450 return (dummy); 451} 452 453/* 454 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create() 455 */ 456CLIENT * 457clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz) 458 struct sockaddr_un *raddr; 459 u_long prog; 460 u_long vers; 461 int *sockp; 462 u_int sendsz; 463 u_int recvsz; 464{ 465 struct netbuf *svcaddr; 466 struct netconfig *nconf; 467 CLIENT *cl; 468 int len; 469 470 cl = NULL; 471 nconf = NULL; 472 svcaddr = NULL; 473 if ((raddr->sun_len == 0) || 474 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) || 475 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) { 476 if (svcaddr != NULL) 477 free(svcaddr); 478 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 479 rpc_createerr.cf_error.re_errno = errno; 480 return(cl); 481 } 482 if (*sockp < 0) { 483 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0); 484 len = raddr->sun_len = SUN_LEN(raddr); 485 if ((*sockp < 0) || (_connect(*sockp, 486 (struct sockaddr *)raddr, len) < 0)) { 487 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 488 rpc_createerr.cf_error.re_errno = errno; 489 if (*sockp != -1) 490 (void)_close(*sockp); 491 goto done; 492 } 493 } 494 svcaddr->buf = raddr; 495 svcaddr->len = raddr->sun_len; 496 svcaddr->maxlen = sizeof (struct sockaddr_un); 497 cl = clnt_vc_create(*sockp, svcaddr, prog, 498 vers, sendsz, recvsz); 499done: 500 free(svcaddr->buf); 501 free(svcaddr); 502 return(cl); 503} 504 505/* 506 * Creates, registers, and returns a (rpc) unix based transporter. 507 * Obsoleted by svc_vc_create(). 508 */ 509SVCXPRT * 510svcunix_create(sock, sendsize, recvsize, path) 511 int sock; 512 u_int sendsize; 513 u_int recvsize; 514 char *path; 515{ 516 struct netconfig *nconf; 517 void *localhandle; 518 struct sockaddr_un sun; 519 struct sockaddr *sa; 520 struct t_bind taddr; 521 SVCXPRT *xprt; 522 int addrlen; 523 524 xprt = (SVCXPRT *)NULL; 525 localhandle = setnetconfig(); 526 while ((nconf = getnetconfig(localhandle)) != NULL) { 527 if (nconf->nc_protofmly != NULL && 528 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 529 break; 530 } 531 if (nconf == NULL) 532 return(xprt); 533 534 if ((sock = __rpc_nconf2fd(nconf)) < 0) 535 goto done; 536 537 memset(&sun, 0, sizeof sun); 538 sun.sun_family = AF_LOCAL; 539 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= 540 sizeof(sun.sun_path)) 541 goto done; 542 sun.sun_len = SUN_LEN(&sun); 543 addrlen = sizeof (struct sockaddr_un); 544 sa = (struct sockaddr *)&sun; 545 546 if (_bind(sock, sa, addrlen) < 0) 547 goto done; 548 549 taddr.addr.len = taddr.addr.maxlen = addrlen; 550 taddr.addr.buf = malloc(addrlen); 551 if (taddr.addr.buf == NULL) 552 goto done; 553 memcpy(taddr.addr.buf, sa, addrlen); 554 555 if (nconf->nc_semantics != NC_TPI_CLTS) { 556 if (_listen(sock, SOMAXCONN) < 0) { 557 free(taddr.addr.buf); 558 goto done; 559 } 560 } 561 562 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize); 563 564done: 565 endnetconfig(localhandle); 566 return(xprt); 567} 568 569/* 570 * Like svunix_create(), except the routine takes any *open* UNIX file 571 * descriptor as its first input. Obsoleted by svc_fd_create(); 572 */ 573SVCXPRT * 574svcunixfd_create(fd, sendsize, recvsize) 575 int fd; 576 u_int sendsize; 577 u_int recvsize; 578{ 579 return (svc_fd_create(fd, sendsize, recvsize)); 580} 581 582#endif /* PORTMAP */
| 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 <syslog.h> 67#include <netinet/in.h> 68#include <netdb.h> 69#include <errno.h> 70#include <syslog.h> 71#include <stdlib.h> 72#include <string.h> 73#include <unistd.h> 74#include "un-namespace.h" 75 76#include "rpc_com.h" 77 78extern mutex_t rpcsoc_lock; 79 80static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t, 81 int *, u_int, u_int, char *); 82static SVCXPRT *svc_com_create(int, u_int, u_int, char *); 83static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *); 84 85/* XXX */ 86#define IN4_LOCALHOST_STRING "127.0.0.1" 87#define IN6_LOCALHOST_STRING "::1" 88 89/* 90 * A common clnt create routine 91 */ 92static CLIENT * 93clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) 94 struct sockaddr_in *raddr; 95 rpcprog_t prog; 96 rpcvers_t vers; 97 int *sockp; 98 u_int sendsz; 99 u_int recvsz; 100 char *tp; 101{ 102 CLIENT *cl; 103 int madefd = FALSE; 104 int fd = *sockp; 105 struct netconfig *nconf; 106 struct netbuf bindaddr; 107 108 mutex_lock(&rpcsoc_lock); 109 if ((nconf = __rpc_getconfip(tp)) == NULL) { 110 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 111 mutex_unlock(&rpcsoc_lock); 112 return (NULL); 113 } 114 if (fd == RPC_ANYSOCK) { 115 fd = __rpc_nconf2fd(nconf); 116 if (fd == -1) 117 goto syserror; 118 madefd = TRUE; 119 } 120 121 if (raddr->sin_port == 0) { 122 u_int proto; 123 u_short sport; 124 125 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ 126 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; 127 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, 128 proto); 129 if (sport == 0) { 130 goto err; 131 } 132 raddr->sin_port = htons(sport); 133 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ 134 } 135 136 /* Transform sockaddr_in to netbuf */ 137 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); 138 bindaddr.buf = raddr; 139 140 bindresvport(fd, NULL); 141 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, 142 sendsz, recvsz); 143 if (cl) { 144 if (madefd == TRUE) { 145 /* 146 * The fd should be closed while destroying the handle. 147 */ 148 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); 149 *sockp = fd; 150 } 151 (void) freenetconfigent(nconf); 152 mutex_unlock(&rpcsoc_lock); 153 return (cl); 154 } 155 goto err; 156 157syserror: 158 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 159 rpc_createerr.cf_error.re_errno = errno; 160 161err: if (madefd == TRUE) 162 (void)_close(fd); 163 (void) freenetconfigent(nconf); 164 mutex_unlock(&rpcsoc_lock); 165 return (NULL); 166} 167 168CLIENT * 169clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) 170 struct sockaddr_in *raddr; 171 u_long prog; 172 u_long vers; 173 struct timeval wait; 174 int *sockp; 175 u_int sendsz; 176 u_int recvsz; 177{ 178 CLIENT *cl; 179 180 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 181 sendsz, recvsz, "udp"); 182 if (cl == NULL) { 183 return (NULL); 184 } 185 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait); 186 return (cl); 187} 188 189CLIENT * 190clntudp_create(raddr, program, version, wait, sockp) 191 struct sockaddr_in *raddr; 192 u_long program; 193 u_long version; 194 struct timeval wait; 195 int *sockp; 196{ 197 198 return clntudp_bufcreate(raddr, program, version, wait, sockp, 199 UDPMSGSIZE, UDPMSGSIZE); 200} 201 202CLIENT * 203clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) 204 struct sockaddr_in *raddr; 205 u_long prog; 206 u_long vers; 207 int *sockp; 208 u_int sendsz; 209 u_int recvsz; 210{ 211 212 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, 213 sendsz, recvsz, "tcp"); 214} 215 216CLIENT * 217clntraw_create(prog, vers) 218 u_long prog; 219 u_long vers; 220{ 221 222 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); 223} 224 225/* 226 * A common server create routine 227 */ 228static SVCXPRT * 229svc_com_create(fd, sendsize, recvsize, netid) 230 int fd; 231 u_int sendsize; 232 u_int recvsize; 233 char *netid; 234{ 235 struct netconfig *nconf; 236 SVCXPRT *svc; 237 int madefd = FALSE; 238 int port; 239 struct sockaddr_in sin; 240 241 if ((nconf = __rpc_getconfip(netid)) == NULL) { 242 (void) syslog(LOG_ERR, "Could not get %s transport", netid); 243 return (NULL); 244 } 245 if (fd == RPC_ANYSOCK) { 246 fd = __rpc_nconf2fd(nconf); 247 if (fd == -1) { 248 (void) freenetconfigent(nconf); 249 (void) syslog(LOG_ERR, 250 "svc%s_create: could not open connection", netid); 251 return (NULL); 252 } 253 madefd = TRUE; 254 } 255 256 memset(&sin, 0, sizeof sin); 257 sin.sin_family = AF_INET; 258 bindresvport(fd, &sin); 259 _listen(fd, SOMAXCONN); 260 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); 261 (void) freenetconfigent(nconf); 262 if (svc == NULL) { 263 if (madefd) 264 (void)_close(fd); 265 return (NULL); 266 } 267 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); 268 svc->xp_port = ntohs(port); 269 return (svc); 270} 271 272SVCXPRT * 273svctcp_create(fd, sendsize, recvsize) 274 int fd; 275 u_int sendsize; 276 u_int recvsize; 277{ 278 279 return svc_com_create(fd, sendsize, recvsize, "tcp"); 280} 281 282SVCXPRT * 283svcudp_bufcreate(fd, sendsz, recvsz) 284 int fd; 285 u_int sendsz, recvsz; 286{ 287 288 return svc_com_create(fd, sendsz, recvsz, "udp"); 289} 290 291SVCXPRT * 292svcfd_create(fd, sendsize, recvsize) 293 int fd; 294 u_int sendsize; 295 u_int recvsize; 296{ 297 298 return svc_fd_create(fd, sendsize, recvsize); 299} 300 301 302SVCXPRT * 303svcudp_create(fd) 304 int fd; 305{ 306 307 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); 308} 309 310SVCXPRT * 311svcraw_create() 312{ 313 314 return svc_raw_create(); 315} 316 317int 318get_myaddress(addr) 319 struct sockaddr_in *addr; 320{ 321 322 memset((void *) addr, 0, sizeof(*addr)); 323 addr->sin_family = AF_INET; 324 addr->sin_port = htons(PMAPPORT); 325 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 326 return (0); 327} 328 329/* 330 * For connectionless "udp" transport. Obsoleted by rpc_call(). 331 */ 332int 333callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 334 const char *host; 335 int prognum, versnum, procnum; 336 xdrproc_t inproc, outproc; 337 void *in, *out; 338{ 339 340 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, 341 (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); 342} 343 344/* 345 * For connectionless kind of transport. Obsoleted by rpc_reg() 346 */ 347int 348registerrpc(prognum, versnum, procnum, progname, inproc, outproc) 349 int prognum, versnum, procnum; 350 char *(*progname)(char [UDPMSGSIZE]); 351 xdrproc_t inproc, outproc; 352{ 353 354 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum, 355 (rpcproc_t)procnum, progname, inproc, outproc, "udp"); 356} 357 358/* 359 * All the following clnt_broadcast stuff is convulated; it supports 360 * the earlier calling style of the callback function 361 */ 362static thread_key_t clnt_broadcast_key; 363static resultproc_t clnt_broadcast_result_main; 364 365/* 366 * Need to translate the netbuf address into sockaddr_in address. 367 * Dont care about netid here. 368 */ 369/* ARGSUSED */ 370static bool_t 371rpc_wrap_bcast(resultp, addr, nconf) 372 char *resultp; /* results of the call */ 373 struct netbuf *addr; /* address of the guy who responded */ 374 struct netconfig *nconf; /* Netconf of the transport */ 375{ 376 resultproc_t clnt_broadcast_result; 377 378 if (strcmp(nconf->nc_netid, "udp")) 379 return (FALSE); 380 if (thr_main()) 381 clnt_broadcast_result = clnt_broadcast_result_main; 382 else 383 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key); 384 return (*clnt_broadcast_result)(resultp, 385 (struct sockaddr_in *)addr->buf); 386} 387 388/* 389 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). 390 */ 391enum clnt_stat 392clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 393 u_long prog; /* program number */ 394 u_long vers; /* version number */ 395 u_long proc; /* procedure number */ 396 xdrproc_t xargs; /* xdr routine for args */ 397 caddr_t argsp; /* pointer to args */ 398 xdrproc_t xresults; /* xdr routine for results */ 399 caddr_t resultsp; /* pointer to results */ 400 resultproc_t eachresult; /* call with each result obtained */ 401{ 402 extern mutex_t tsd_lock; 403 404 if (thr_main()) 405 clnt_broadcast_result_main = eachresult; 406 else { 407 if (clnt_broadcast_key == 0) { 408 mutex_lock(&tsd_lock); 409 if (clnt_broadcast_key == 0) 410 thr_keycreate(&clnt_broadcast_key, free); 411 mutex_unlock(&tsd_lock); 412 } 413 thr_setspecific(clnt_broadcast_key, (void *) eachresult); 414 } 415 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers, 416 (rpcproc_t)proc, xargs, argsp, xresults, resultsp, 417 (resultproc_t) rpc_wrap_bcast, "udp"); 418} 419 420/* 421 * Create the client des authentication object. Obsoleted by 422 * authdes_seccreate(). 423 */ 424AUTH * 425authdes_create(servername, window, syncaddr, ckey) 426 char *servername; /* network name of server */ 427 u_int window; /* time to live */ 428 struct sockaddr *syncaddr; /* optional hostaddr to sync with */ 429 des_block *ckey; /* optional conversation key to use */ 430{ 431 AUTH *dummy; 432 AUTH *nauth; 433 char hostname[NI_MAXHOST]; 434 435 if (syncaddr) { 436 /* 437 * Change addr to hostname, because that is the way 438 * new interface takes it. 439 */ 440 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname, 441 sizeof hostname, NULL, 0, 0) != 0) 442 goto fallback; 443 444 nauth = authdes_seccreate(servername, window, hostname, ckey); 445 return (nauth); 446 } 447fallback: 448 dummy = authdes_seccreate(servername, window, NULL, ckey); 449 return (dummy); 450} 451 452/* 453 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create() 454 */ 455CLIENT * 456clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz) 457 struct sockaddr_un *raddr; 458 u_long prog; 459 u_long vers; 460 int *sockp; 461 u_int sendsz; 462 u_int recvsz; 463{ 464 struct netbuf *svcaddr; 465 struct netconfig *nconf; 466 CLIENT *cl; 467 int len; 468 469 cl = NULL; 470 nconf = NULL; 471 svcaddr = NULL; 472 if ((raddr->sun_len == 0) || 473 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) || 474 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) { 475 if (svcaddr != NULL) 476 free(svcaddr); 477 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 478 rpc_createerr.cf_error.re_errno = errno; 479 return(cl); 480 } 481 if (*sockp < 0) { 482 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0); 483 len = raddr->sun_len = SUN_LEN(raddr); 484 if ((*sockp < 0) || (_connect(*sockp, 485 (struct sockaddr *)raddr, len) < 0)) { 486 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 487 rpc_createerr.cf_error.re_errno = errno; 488 if (*sockp != -1) 489 (void)_close(*sockp); 490 goto done; 491 } 492 } 493 svcaddr->buf = raddr; 494 svcaddr->len = raddr->sun_len; 495 svcaddr->maxlen = sizeof (struct sockaddr_un); 496 cl = clnt_vc_create(*sockp, svcaddr, prog, 497 vers, sendsz, recvsz); 498done: 499 free(svcaddr->buf); 500 free(svcaddr); 501 return(cl); 502} 503 504/* 505 * Creates, registers, and returns a (rpc) unix based transporter. 506 * Obsoleted by svc_vc_create(). 507 */ 508SVCXPRT * 509svcunix_create(sock, sendsize, recvsize, path) 510 int sock; 511 u_int sendsize; 512 u_int recvsize; 513 char *path; 514{ 515 struct netconfig *nconf; 516 void *localhandle; 517 struct sockaddr_un sun; 518 struct sockaddr *sa; 519 struct t_bind taddr; 520 SVCXPRT *xprt; 521 int addrlen; 522 523 xprt = (SVCXPRT *)NULL; 524 localhandle = setnetconfig(); 525 while ((nconf = getnetconfig(localhandle)) != NULL) { 526 if (nconf->nc_protofmly != NULL && 527 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 528 break; 529 } 530 if (nconf == NULL) 531 return(xprt); 532 533 if ((sock = __rpc_nconf2fd(nconf)) < 0) 534 goto done; 535 536 memset(&sun, 0, sizeof sun); 537 sun.sun_family = AF_LOCAL; 538 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= 539 sizeof(sun.sun_path)) 540 goto done; 541 sun.sun_len = SUN_LEN(&sun); 542 addrlen = sizeof (struct sockaddr_un); 543 sa = (struct sockaddr *)&sun; 544 545 if (_bind(sock, sa, addrlen) < 0) 546 goto done; 547 548 taddr.addr.len = taddr.addr.maxlen = addrlen; 549 taddr.addr.buf = malloc(addrlen); 550 if (taddr.addr.buf == NULL) 551 goto done; 552 memcpy(taddr.addr.buf, sa, addrlen); 553 554 if (nconf->nc_semantics != NC_TPI_CLTS) { 555 if (_listen(sock, SOMAXCONN) < 0) { 556 free(taddr.addr.buf); 557 goto done; 558 } 559 } 560 561 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize); 562 563done: 564 endnetconfig(localhandle); 565 return(xprt); 566} 567 568/* 569 * Like svunix_create(), except the routine takes any *open* UNIX file 570 * descriptor as its first input. Obsoleted by svc_fd_create(); 571 */ 572SVCXPRT * 573svcunixfd_create(fd, sendsize, recvsize) 574 int fd; 575 u_int sendsize; 576 u_int recvsize; 577{ 578 return (svc_fd_create(fd, sendsize, recvsize)); 579} 580 581#endif /* PORTMAP */
|