PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/rpc/rpc_generic.c

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