lib/libc/rpc/bindresvport.c

http://www.minix3.org/ · C · 164 lines · 104 code · 21 blank · 39 comment · 17 complexity · 50699da9093af73a7b1dbb1f140e2ecc MD5 · raw file

  1. /* $NetBSD: bindresvport.c,v 1.21 2003/01/18 11:29:03 thorpej 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. #include <sys/cdefs.h>
  31. #if defined(LIBC_SCCS) && !defined(lint)
  32. #if 0
  33. static char *sccsid = "@(#)bindresvport.c 1.8 88/02/08 SMI";
  34. static char *sccsid = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";
  35. #else
  36. __RCSID("$NetBSD: bindresvport.c,v 1.21 2003/01/18 11:29:03 thorpej Exp $");
  37. #endif
  38. #endif
  39. /*
  40. * Copyright (c) 1987 by Sun Microsystems, Inc.
  41. */
  42. #include "namespace.h"
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netinet/in.h>
  46. #include <errno.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include <rpc/rpc.h>
  50. #ifdef __weak_alias
  51. __weak_alias(bindresvport,_bindresvport)
  52. __weak_alias(bindresvport_sa,_bindresvport_sa)
  53. #endif
  54. /*
  55. * Bind a socket to a privileged IP port
  56. */
  57. int
  58. bindresvport(sd, brsin)
  59. int sd;
  60. struct sockaddr_in *brsin;
  61. {
  62. return bindresvport_sa(sd, (struct sockaddr *)(void *)brsin);
  63. }
  64. /*
  65. * Bind a socket to a privileged IP port
  66. */
  67. int
  68. bindresvport_sa(sd, sa)
  69. int sd;
  70. struct sockaddr *sa;
  71. {
  72. int error, old;
  73. struct sockaddr_storage myaddr;
  74. struct sockaddr_in *brsin;
  75. #ifdef INET6
  76. struct sockaddr_in6 *brsin6;
  77. #endif
  78. int proto, portrange, portlow;
  79. u_int16_t *portp;
  80. socklen_t salen;
  81. int af;
  82. if (sa == NULL) {
  83. salen = sizeof(myaddr);
  84. sa = (struct sockaddr *)(void *)&myaddr;
  85. if (getsockname(sd, sa, &salen) == -1)
  86. return -1; /* errno is correctly set */
  87. af = sa->sa_family;
  88. memset(sa, 0, salen);
  89. } else
  90. af = sa->sa_family;
  91. switch (af) {
  92. case AF_INET:
  93. proto = IPPROTO_IP;
  94. portrange = IP_PORTRANGE;
  95. portlow = IP_PORTRANGE_LOW;
  96. brsin = (struct sockaddr_in *)(void *)sa;
  97. salen = sizeof(struct sockaddr_in);
  98. portp = &brsin->sin_port;
  99. break;
  100. #ifdef INET6
  101. case AF_INET6:
  102. proto = IPPROTO_IPV6;
  103. portrange = IPV6_PORTRANGE;
  104. portlow = IPV6_PORTRANGE_LOW;
  105. brsin6 = (struct sockaddr_in6 *)(void *)sa;
  106. salen = sizeof(struct sockaddr_in6);
  107. portp = &brsin6->sin6_port;
  108. break;
  109. #endif
  110. default:
  111. errno = EPFNOSUPPORT;
  112. return (-1);
  113. }
  114. sa->sa_family = af;
  115. sa->sa_len = salen;
  116. if (*portp == 0) {
  117. socklen_t oldlen = sizeof(old);
  118. error = getsockopt(sd, proto, portrange, &old, &oldlen);
  119. if (error < 0)
  120. return (error);
  121. error = setsockopt(sd, proto, portrange, &portlow,
  122. sizeof(portlow));
  123. if (error < 0)
  124. return (error);
  125. }
  126. error = bind(sd, sa, salen);
  127. if (*portp == 0) {
  128. int saved_errno = errno;
  129. if (error < 0) {
  130. if (setsockopt(sd, proto, portrange, &old,
  131. sizeof(old)) < 0)
  132. errno = saved_errno;
  133. return (error);
  134. }
  135. if (sa != (struct sockaddr *)(void *)&myaddr) {
  136. /* What did the kernel assign? */
  137. if (getsockname(sd, sa, &salen) < 0)
  138. errno = saved_errno;
  139. return (error);
  140. }
  141. }
  142. return (error);
  143. }