1 /* $NetBSD: rpc_generic.c,v 1.26 2012/03/20 17:14:50 matt 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 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */ 36 37 /* 38 * rpc_generic.c, Miscl routines for RPC. 39 * 40 */ 41 42 #include <sys/cdefs.h> 43 #if defined(LIBC_SCCS) && !defined(lint) 44 __RCSID("$NetBSD: rpc_generic.c,v 1.26 2012/03/20 17:14:50 matt Exp $"); 45 #endif 46 47 #include "namespace.h" 48 #include "reentrant.h" 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/socket.h> 52 #include <sys/un.h> 53 #include <sys/resource.h> 54 #include <netinet/in.h> 55 #include <netinet/tcp.h> 56 #include <arpa/inet.h> 57 #include <rpc/rpc.h> 58 #include <assert.h> 59 #include <ctype.h> 60 #include <stdio.h> 61 #include <netdb.h> 62 #include <netconfig.h> 63 #include <malloc.h> 64 #include <string.h> 65 #include <syslog.h> 66 #include <rpc/nettype.h> 67 #include "rpc_internal.h" 68 69 #ifdef __weak_alias 70 __weak_alias(taddr2uaddr,_taddr2uaddr) 71 __weak_alias(uaddr2taddr,_uaddr2taddr) 72 #endif 73 74 struct handle { 75 NCONF_HANDLE *nhandle; 76 int nflag; /* Whether NETPATH or NETCONFIG */ 77 int nettype; 78 }; 79 80 static const struct _rpcnettype { 81 const char *name; 82 const int type; 83 } _rpctypelist[] = { 84 { "netpath", _RPC_NETPATH }, 85 { "visible", _RPC_VISIBLE }, 86 { "circuit_v", _RPC_CIRCUIT_V }, 87 { "datagram_v", _RPC_DATAGRAM_V }, 88 { "circuit_n", _RPC_CIRCUIT_N }, 89 { "datagram_n", _RPC_DATAGRAM_N }, 90 { "tcp", _RPC_TCP }, 91 { "udp", _RPC_UDP }, 92 { 0, _RPC_NONE } 93 }; 94 95 struct netid_af { 96 const char *netid; 97 int af; 98 int protocol; 99 }; 100 101 static const struct netid_af na_cvt[] = { 102 { "udp", AF_INET, IPPROTO_UDP }, 103 { "tcp", AF_INET, IPPROTO_TCP }, 104 #ifdef INET6 105 { "udp6", AF_INET6, IPPROTO_UDP }, 106 { "tcp6", AF_INET6, IPPROTO_TCP }, 107 #endif 108 { "local", AF_LOCAL, 0 } 109 }; 110 111 #if 0 112 static char *strlocase(char *); 113 #endif 114 static int getnettype(const char *); 115 116 /* 117 * Cache the result of getrlimit(), so we don't have to do an 118 * expensive call every time. 119 */ 120 int 121 __rpc_dtbsize(void) 122 { 123 static int tbsize; 124 struct rlimit rl; 125 126 if (tbsize) { 127 return (tbsize); 128 } 129 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 130 return (tbsize = (int)rl.rlim_max); 131 } 132 /* 133 * Something wrong. I'll try to save face by returning a 134 * pessimistic number. 135 */ 136 return (32); 137 } 138 139 140 /* 141 * Find the appropriate buffer size 142 */ 143 u_int 144 /*ARGSUSED*/ 145 __rpc_get_t_size( 146 int af, 147 int proto, 148 int size) /* Size requested */ 149 { 150 int maxsize, defsize; 151 152 maxsize = 256 * 1024; /* XXX */ 153 switch (proto) { 154 case IPPROTO_TCP: 155 defsize = 64 * 1024; /* XXX */ 156 break; 157 case IPPROTO_UDP: 158 defsize = UDPMSGSIZE; 159 break; 160 default: 161 defsize = RPC_MAXDATASIZE; 162 break; 163 } 164 if (size == 0) 165 return defsize; 166 167 /* Check whether the value is within the upper max limit */ 168 return (size > maxsize ? (u_int)maxsize : (u_int)size); 169 } 170 171 /* 172 * Find the appropriate address buffer size 173 */ 174 u_int 175 __rpc_get_a_size(int af) 176 { 177 switch (af) { 178 case AF_INET: 179 return sizeof (struct sockaddr_in); 180 #ifdef INET6 181 case AF_INET6: 182 return sizeof (struct sockaddr_in6); 183 #endif 184 case AF_LOCAL: 185 return sizeof (struct sockaddr_un); 186 default: 187 break; 188 } 189 return ((u_int)RPC_MAXADDRSIZE); 190 } 191 192 #if 0 193 static char * 194 strlocase(char *p) 195 { 196 char *t = p; 197 198 _DIAGASSERT(p != NULL); 199 200 for (; *p; p++) 201 if (isupper(*p)) 202 *p = tolower(*p); 203 return (t); 204 } 205 #endif 206 207 /* 208 * Returns the type of the network as defined in <rpc/nettype.h> 209 * If nettype is NULL, it defaults to NETPATH. 210 */ 211 static int 212 getnettype(const char *nettype) 213 { 214 int i; 215 216 if ((nettype == NULL) || (nettype[0] == 0)) { 217 return (_RPC_NETPATH); /* Default */ 218 } 219 220 #if 0 221 nettype = strlocase(nettype); 222 #endif 223 for (i = 0; _rpctypelist[i].name; i++) 224 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) { 225 return (_rpctypelist[i].type); 226 } 227 return (_rpctypelist[i].type); 228 } 229 230 /* 231 * For the given nettype (tcp or udp only), return the first structure found. 232 * This should be freed by calling freenetconfigent() 233 */ 234 235 #ifdef _REENTRANT 236 static thread_key_t tcp_key, udp_key; 237 static once_t __rpc_getconfigp_once = ONCE_INITIALIZER; 238 239 static void 240 __rpc_getconfigp_setup(void) 241 { 242 243 thr_keycreate(&tcp_key, free); 244 thr_keycreate(&udp_key, free); 245 } 246 #endif 247 248 struct netconfig * 249 __rpc_getconfip(const char *nettype) 250 { 251 char *netid; 252 char *netid_tcp = NULL; 253 char *netid_udp = NULL; 254 static char *netid_tcp_main; 255 static char *netid_udp_main; 256 struct netconfig *dummy; 257 #ifdef _REENTRANT 258 if (__isthreaded == 0) { 259 netid_udp = netid_udp_main; 260 netid_tcp = netid_tcp_main; 261 } else { 262 thr_once(&__rpc_getconfigp_once, __rpc_getconfigp_setup); 263 netid_tcp = thr_getspecific(tcp_key); 264 netid_udp = thr_getspecific(udp_key); 265 } 266 #else 267 netid_udp = netid_udp_main; 268 netid_tcp = netid_tcp_main; 269 #endif 270 271 _DIAGASSERT(nettype != NULL); 272 273 if (!netid_udp && !netid_tcp) { 274 struct netconfig *nconf; 275 void *confighandle; 276 277 if (!(confighandle = setnetconfig())) { 278 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 279 return (NULL); 280 } 281 while ((nconf = getnetconfig(confighandle)) != NULL) { 282 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 283 if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 284 netid_tcp = strdup(nconf->nc_netid); 285 if (netid_tcp == NULL) 286 return NULL; 287 #ifdef _REENTRANT 288 if (__isthreaded == 0) 289 netid_tcp_main = netid_tcp; 290 else 291 thr_setspecific(tcp_key, 292 (void *) netid_tcp); 293 #else 294 netid_tcp_main = netid_tcp; 295 #endif 296 } else 297 if (strcmp(nconf->nc_proto, NC_UDP) == 0) { 298 netid_udp = strdup(nconf->nc_netid); 299 if (netid_udp == NULL) 300 return NULL; 301 #ifdef _REENTRANT 302 if (__isthreaded == 0) 303 netid_udp_main = netid_udp; 304 else 305 thr_setspecific(udp_key, 306 (void *) netid_udp); 307 #else 308 netid_udp_main = netid_udp; 309 #endif 310 } 311 } 312 } 313 endnetconfig(confighandle); 314 } 315 if (strcmp(nettype, "udp") == 0) 316 netid = netid_udp; 317 else if (strcmp(nettype, "tcp") == 0) 318 netid = netid_tcp; 319 else { 320 return (NULL); 321 } 322 if ((netid == NULL) || (netid[0] == 0)) { 323 return (NULL); 324 } 325 dummy = getnetconfigent(netid); 326 return (dummy); 327 } 328 329 /* 330 * Returns the type of the nettype, which should then be used with 331 * __rpc_getconf(). 332 */ 333 void * 334 __rpc_setconf(const char *nettype) 335 { 336 struct handle *handle; 337 338 /* nettype may be NULL; getnettype() supports that */ 339 340 handle = malloc(sizeof(*handle)); 341 if (handle == NULL) { 342 return (NULL); 343 } 344 switch (handle->nettype = getnettype(nettype)) { 345 case _RPC_NETPATH: 346 case _RPC_CIRCUIT_N: 347 case _RPC_DATAGRAM_N: 348 if (!(handle->nhandle = setnetpath())) { 349 free(handle); 350 return (NULL); 351 } 352 handle->nflag = TRUE; 353 break; 354 case _RPC_VISIBLE: 355 case _RPC_CIRCUIT_V: 356 case _RPC_DATAGRAM_V: 357 case _RPC_TCP: 358 case _RPC_UDP: 359 if (!(handle->nhandle = setnetconfig())) { 360 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 361 free(handle); 362 return (NULL); 363 } 364 handle->nflag = FALSE; 365 break; 366 default: 367 free(handle); 368 return (NULL); 369 } 370 371 return (handle); 372 } 373 374 /* 375 * Returns the next netconfig struct for the given "net" type. 376 * __rpc_setconf() should have been called previously. 377 */ 378 struct netconfig * 379 __rpc_getconf(void *vhandle) 380 { 381 struct handle *handle; 382 struct netconfig *nconf; 383 384 handle = (struct handle *)vhandle; 385 if (handle == NULL) { 386 return (NULL); 387 } 388 for (;;) { 389 if (handle->nflag) 390 nconf = getnetpath(handle->nhandle); 391 else 392 nconf = getnetconfig(handle->nhandle); 393 if (nconf == NULL) 394 break; 395 if ((nconf->nc_semantics != NC_TPI_CLTS) && 396 (nconf->nc_semantics != NC_TPI_COTS) && 397 (nconf->nc_semantics != NC_TPI_COTS_ORD)) 398 continue; 399 switch (handle->nettype) { 400 case _RPC_VISIBLE: 401 if (!(nconf->nc_flag & NC_VISIBLE)) 402 continue; 403 /* FALLTHROUGH */ 404 case _RPC_NETPATH: /* Be happy */ 405 break; 406 case _RPC_CIRCUIT_V: 407 if (!(nconf->nc_flag & NC_VISIBLE)) 408 continue; 409 /* FALLTHROUGH */ 410 case _RPC_CIRCUIT_N: 411 if ((nconf->nc_semantics != NC_TPI_COTS) && 412 (nconf->nc_semantics != NC_TPI_COTS_ORD)) 413 continue; 414 break; 415 case _RPC_DATAGRAM_V: 416 if (!(nconf->nc_flag & NC_VISIBLE)) 417 continue; 418 /* FALLTHROUGH */ 419 case _RPC_DATAGRAM_N: 420 if (nconf->nc_semantics != NC_TPI_CLTS) 421 continue; 422 break; 423 case _RPC_TCP: 424 if (((nconf->nc_semantics != NC_TPI_COTS) && 425 (nconf->nc_semantics != NC_TPI_COTS_ORD)) || 426 (strcmp(nconf->nc_protofmly, NC_INET) 427 #ifdef INET6 428 && strcmp(nconf->nc_protofmly, NC_INET6)) 429 #else 430 ) 431 #endif 432 || 433 strcmp(nconf->nc_proto, NC_TCP)) 434 continue; 435 break; 436 case _RPC_UDP: 437 if ((nconf->nc_semantics != NC_TPI_CLTS) || 438 (strcmp(nconf->nc_protofmly, NC_INET) 439 #ifdef INET6 440 && strcmp(nconf->nc_protofmly, NC_INET6)) 441 #else 442 ) 443 #endif 444 || 445 strcmp(nconf->nc_proto, NC_UDP)) 446 continue; 447 break; 448 } 449 break; 450 } 451 return (nconf); 452 } 453 454 void 455 __rpc_endconf(void *vhandle) 456 { 457 struct handle *handle; 458 459 handle = (struct handle *) vhandle; 460 if (handle == NULL) { 461 return; 462 } 463 if (handle->nflag) { 464 endnetpath(handle->nhandle); 465 } else { 466 endnetconfig(handle->nhandle); 467 } 468 free(handle); 469 } 470 471 /* 472 * Used to ping the NULL procedure for clnt handle. 473 * Returns NULL if fails, else a non-NULL pointer. 474 */ 475 void * 476 rpc_nullproc(CLIENT *clnt) 477 { 478 struct timeval TIMEOUT = {25, 0}; 479 480 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL, 481 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) { 482 return (NULL); 483 } 484 return ((void *) clnt); 485 } 486 487 /* 488 * Try all possible transports until 489 * one succeeds in finding the netconf for the given fd. 490 */ 491 struct netconfig * 492 __rpcgettp(int fd) 493 { 494 const char *netid; 495 struct __rpc_sockinfo si; 496 497 if (!__rpc_fd2sockinfo(fd, &si)) 498 return NULL; 499 500 if (!__rpc_sockinfo2netid(&si, &netid)) 501 return NULL; 502 503 return getnetconfigent(__UNCONST(netid)); 504 } 505 506 int 507 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip) 508 { 509 socklen_t len; 510 int type, proto; 511 struct sockaddr_storage ss; 512 513 _DIAGASSERT(sip != NULL); 514 515 len = sizeof ss; 516 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0) 517 return 0; 518 sip->si_alen = len; 519 520 len = sizeof type; 521 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0) 522 return 0; 523 524 /* XXX */ 525 if (ss.ss_family != AF_LOCAL) { 526 if (type == SOCK_STREAM) 527 proto = IPPROTO_TCP; 528 else if (type == SOCK_DGRAM) 529 proto = IPPROTO_UDP; 530 else 531 return 0; 532 } else 533 proto = 0; 534 535 sip->si_af = ss.ss_family; 536 sip->si_proto = proto; 537 sip->si_socktype = type; 538 539 return 1; 540 } 541 542 /* 543 * Linear search, but the number of entries is small. 544 */ 545 int 546 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip) 547 { 548 size_t i; 549 550 _DIAGASSERT(nconf != NULL); 551 _DIAGASSERT(sip != NULL); 552 553 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) 554 if (!strcmp(na_cvt[i].netid, nconf->nc_netid)) { 555 sip->si_af = na_cvt[i].af; 556 sip->si_proto = na_cvt[i].protocol; 557 sip->si_socktype = 558 __rpc_seman2socktype((int)nconf->nc_semantics); 559 if (sip->si_socktype == -1) 560 return 0; 561 sip->si_alen = __rpc_get_a_size(sip->si_af); 562 return 1; 563 } 564 565 return 0; 566 } 567 568 int 569 __rpc_nconf2fd(const struct netconfig *nconf) 570 { 571 struct __rpc_sockinfo si; 572 573 _DIAGASSERT(nconf != NULL); 574 575 if (!__rpc_nconf2sockinfo(nconf, &si)) 576 return 0; 577 578 return socket(si.si_af, si.si_socktype, si.si_proto); 579 } 580 581 int 582 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid) 583 { 584 size_t i; 585 586 _DIAGASSERT(sip != NULL); 587 /* netid may be NULL */ 588 589 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) 590 if (na_cvt[i].af == sip->si_af && 591 na_cvt[i].protocol == sip->si_proto) { 592 if (netid) 593 *netid = na_cvt[i].netid; 594 return 1; 595 } 596 597 return 0; 598 } 599 600 char * 601 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf) 602 { 603 struct __rpc_sockinfo si; 604 605 _DIAGASSERT(nconf != NULL); 606 _DIAGASSERT(nbuf != NULL); 607 608 if (!__rpc_nconf2sockinfo(nconf, &si)) 609 return NULL; 610 return __rpc_taddr2uaddr_af(si.si_af, nbuf); 611 } 612 613 struct netbuf * 614 uaddr2taddr(const struct netconfig *nconf, const char *uaddr) 615 { 616 struct __rpc_sockinfo si; 617 618 _DIAGASSERT(nconf != NULL); 619 _DIAGASSERT(uaddr != NULL); 620 621 if (!__rpc_nconf2sockinfo(nconf, &si)) 622 return NULL; 623 return __rpc_uaddr2taddr_af(si.si_af, uaddr); 624 } 625 626 char * 627 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf) 628 { 629 char *ret; 630 struct sockaddr_in *sinp; 631 struct sockaddr_un *sun; 632 char namebuf[INET_ADDRSTRLEN]; 633 #ifdef INET6 634 struct sockaddr_in6 *sin6; 635 char namebuf6[INET6_ADDRSTRLEN]; 636 #endif 637 u_int16_t port; 638 639 _DIAGASSERT(nbuf != NULL); 640 641 switch (af) { 642 case AF_INET: 643 sinp = nbuf->buf; 644 if (inet_ntop(af, &sinp->sin_addr, namebuf, 645 (socklen_t)sizeof namebuf) == NULL) 646 return NULL; 647 port = ntohs(sinp->sin_port); 648 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8, 649 port & 0xff) < 0) 650 return NULL; 651 break; 652 #ifdef INET6 653 case AF_INET6: 654 sin6 = nbuf->buf; 655 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, 656 (socklen_t)sizeof namebuf6) == NULL) 657 return NULL; 658 port = ntohs(sin6->sin6_port); 659 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8, 660 port & 0xff) < 0) 661 return NULL; 662 break; 663 #endif 664 case AF_LOCAL: 665 sun = nbuf->buf; 666 sun->sun_path[sizeof(sun->sun_path) - 1] = '\0'; /* safety */ 667 ret = strdup(sun->sun_path); 668 break; 669 default: 670 return NULL; 671 } 672 673 return ret; 674 } 675 676 struct netbuf * 677 __rpc_uaddr2taddr_af(int af, const char *uaddr) 678 { 679 struct netbuf *ret = NULL; 680 char *addrstr, *p; 681 unsigned port, portlo, porthi; 682 size_t len; 683 struct sockaddr_in *sinp; 684 #ifdef INET6 685 struct sockaddr_in6 *sin6; 686 #endif 687 struct sockaddr_un *sun; 688 689 _DIAGASSERT(uaddr != NULL); 690 691 addrstr = strdup(uaddr); 692 if (addrstr == NULL) 693 return NULL; 694 695 /* 696 * AF_LOCAL addresses are expected to be absolute 697 * pathnames, anything else will be AF_INET or AF_INET6. 698 */ 699 port = 0; 700 if (*addrstr != '/') { 701 p = strrchr(addrstr, '.'); 702 if (p == NULL) 703 goto out; 704 portlo = (unsigned)atoi(p + 1); 705 *p = '\0'; 706 707 p = strrchr(addrstr, '.'); 708 if (p == NULL) 709 goto out; 710 porthi = (unsigned)atoi(p + 1); 711 *p = '\0'; 712 port = (porthi << 8) | portlo; 713 } 714 715 ret = malloc(sizeof(*ret)); 716 if (ret == NULL) 717 goto out; 718 719 switch (af) { 720 case AF_INET: 721 sinp = malloc(sizeof(*sinp)); 722 if (sinp == NULL) 723 goto out; 724 memset(sinp, 0, sizeof *sinp); 725 sinp->sin_family = AF_INET; 726 sinp->sin_port = htons(port); 727 if (inet_pton(AF_INET, addrstr, &sinp->sin_addr) <= 0) { 728 free(sinp); 729 free(ret); 730 ret = NULL; 731 goto out; 732 } 733 sinp->sin_len = ret->maxlen = ret->len = sizeof *sinp; 734 ret->buf = sinp; 735 break; 736 #ifdef INET6 737 case AF_INET6: 738 sin6 = malloc(sizeof(*sin6)); 739 if (sin6 == NULL) 740 goto out; 741 memset(sin6, 0, sizeof *sin6); 742 sin6->sin6_family = AF_INET6; 743 sin6->sin6_port = htons(port); 744 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) { 745 free(sin6); 746 free(ret); 747 ret = NULL; 748 goto out; 749 } 750 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6; 751 ret->buf = sin6; 752 break; 753 #endif 754 case AF_LOCAL: 755 sun = malloc(sizeof(*sun)); 756 if (sun == NULL) 757 goto out; 758 memset(sun, 0, sizeof *sun); 759 sun->sun_family = AF_LOCAL; 760 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1); 761 len = SUN_LEN(sun); 762 _DIAGASSERT(__type_fit(uint8_t, len)); 763 ret->len = ret->maxlen = sun->sun_len = (uint8_t)len; 764 ret->buf = sun; 765 break; 766 default: 767 break; 768 } 769 out: 770 free(addrstr); 771 return ret; 772 } 773 774 int 775 __rpc_seman2socktype(int semantics) 776 { 777 switch (semantics) { 778 case NC_TPI_CLTS: 779 return SOCK_DGRAM; 780 case NC_TPI_COTS_ORD: 781 return SOCK_STREAM; 782 case NC_TPI_RAW: 783 return SOCK_RAW; 784 default: 785 break; 786 } 787 788 return -1; 789 } 790 791 int 792 __rpc_socktype2seman(int socktype) 793 { 794 switch (socktype) { 795 case SOCK_DGRAM: 796 return NC_TPI_CLTS; 797 case SOCK_STREAM: 798 return NC_TPI_COTS_ORD; 799 case SOCK_RAW: 800 return NC_TPI_RAW; 801 default: 802 break; 803 } 804 805 return -1; 806 } 807 808 /* 809 * XXXX - IPv6 scope IDs can't be handled in universal addresses. 810 * Here, we compare the original server address to that of the RPC 811 * service we just received back from a call to rpcbind on the remote 812 * machine. If they are both "link local" or "site local", copy 813 * the scope id of the server address over to the service address. 814 */ 815 /* ARGSUSED */ 816 int 817 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc) 818 { 819 #ifdef INET6 820 struct sockaddr *sa_new, *sa_svc; 821 struct sockaddr_in6 *sin6_new, *sin6_svc; 822 823 _DIAGASSERT(new != NULL); 824 _DIAGASSERT(svc != NULL); 825 826 sa_svc = (struct sockaddr *)svc->buf; 827 sa_new = (struct sockaddr *)new->buf; 828 829 if (sa_new->sa_family == sa_svc->sa_family && 830 sa_new->sa_family == AF_INET6) { 831 sin6_new = (struct sockaddr_in6 *)new->buf; 832 sin6_svc = (struct sockaddr_in6 *)svc->buf; 833 834 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) && 835 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) || 836 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) && 837 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) { 838 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id; 839 } 840 } 841 #endif 842 return 1; 843 } 844 845 int 846 __rpc_sockisbound(int fd) 847 { 848 struct sockaddr_storage ss; 849 socklen_t slen; 850 851 slen = sizeof (struct sockaddr_storage); 852 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) 853 return 0; 854 855 switch (ss.ss_family) { 856 case AF_INET: 857 return (((struct sockaddr_in *) 858 (void *)&ss)->sin_port != 0); 859 #ifdef INET6 860 case AF_INET6: 861 return (((struct sockaddr_in6 *) 862 (void *)&ss)->sin6_port != 0); 863 #endif 864 case AF_LOCAL: 865 /* XXX check this */ 866 return (((struct sockaddr_un *) 867 (void *)&ss)->sun_path[0] != '\0'); 868 default: 869 break; 870 } 871 872 return 0; 873 } 874 875 /* 876 * For TCP transport, Host Requirements RFCs mandate 877 * Nagle (RFC-896) processing. But for RPC, Nagle 878 * processing adds adds unwanted latency to the last, 879 * partial TCP segment of each RPC message. See: 880 * R. W. Scheifler and J. Gettys, The X Window System, 881 * ACM Transactions on Graphics 16:8 (Aug. 1983), pp. 57-69. 882 * So for TCP transport, disable Nagle via TCP_NODELAY. 883 * XXX: moral equivalent for non-TCP protocols? 884 */ 885 int 886 __rpc_setnodelay(int fd, const struct __rpc_sockinfo *si) 887 { 888 int one = 1; 889 if (si->si_proto != IPPROTO_TCP) 890 return 0; 891 return setsockopt(fd, si->si_proto, TCP_NODELAY, &one, 892 (socklen_t)sizeof(one)); 893 } 894