PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libc/sys-minix/bind.c

http://www.minix3.org/
C | 217 lines | 180 code | 33 blank | 4 comment | 61 complexity | 9727605b30caa6ef31c8e9a0d791f8ff MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. #include <sys/cdefs.h>
  2. #include "namespace.h"
  3. #ifdef __weak_alias
  4. __weak_alias(bind, _bind)
  5. #endif
  6. #include <unistd.h>
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <netinet/in.h>
  18. #include <net/gen/in.h>
  19. #include <net/gen/tcp.h>
  20. #include <net/gen/tcp_io.h>
  21. #include <net/gen/udp.h>
  22. #include <net/gen/udp_io.h>
  23. #include <sys/un.h>
  24. #include <minix/config.h>
  25. #include <minix/const.h>
  26. #define DEBUG 0
  27. static int _tcp_bind(int sock, const struct sockaddr *address,
  28. socklen_t address_len, nwio_tcpconf_t *tcpconfp);
  29. static int _udp_bind(int sock, const struct sockaddr *address,
  30. socklen_t address_len, nwio_udpopt_t *udpoptp);
  31. static int _uds_bind(int sock, const struct sockaddr *address,
  32. socklen_t address_len, struct sockaddr_un *uds_addr);
  33. int bind(int sock, const struct sockaddr *address, socklen_t address_len)
  34. {
  35. int r;
  36. nwio_tcpconf_t tcpconf;
  37. nwio_udpopt_t udpopt;
  38. struct sockaddr_un uds_addr;
  39. r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
  40. if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
  41. {
  42. if (r == -1)
  43. return r;
  44. r= _tcp_bind(sock, address, address_len, &tcpconf);
  45. #if DEBUG
  46. if (r == -1)
  47. {
  48. int t_errno= errno;
  49. fprintf(stderr, "bind(tcp) failed: %s\n",
  50. strerror(errno));
  51. errno= t_errno;
  52. }
  53. #endif
  54. return r;
  55. }
  56. r= ioctl(sock, NWIOGUDPOPT, &udpopt);
  57. if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
  58. {
  59. if (r == -1)
  60. return r;
  61. return _udp_bind(sock, address, address_len, &udpopt);
  62. }
  63. r= ioctl(sock, NWIOGUDSADDR, &uds_addr);
  64. if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
  65. {
  66. if (r == -1)
  67. return r;
  68. return _uds_bind(sock, address, address_len, &uds_addr);
  69. }
  70. #if DEBUG
  71. fprintf(stderr, "bind: not implemented for fd %d\n", sock);
  72. #endif
  73. errno= ENOSYS;
  74. return -1;
  75. }
  76. static int _tcp_bind(int sock, const struct sockaddr *address,
  77. socklen_t address_len, nwio_tcpconf_t *tcpconfp)
  78. {
  79. int r;
  80. nwio_tcpconf_t tcpconf;
  81. struct sockaddr_in *sinp;
  82. sinp= (struct sockaddr_in *) __UNCONST(address);
  83. if (sinp->sin_family != AF_INET || address_len < sizeof(*sinp))
  84. {
  85. #if DEBUG
  86. fprintf(stderr, "bind(tcp): sin_family = %d, len = %d\n",
  87. sinp->sin_family, address_len);
  88. #endif
  89. errno= EAFNOSUPPORT;
  90. return -1;
  91. }
  92. if (sinp->sin_addr.s_addr != INADDR_ANY &&
  93. sinp->sin_addr.s_addr != tcpconfp->nwtc_locaddr)
  94. {
  95. errno= EADDRNOTAVAIL;
  96. return -1;
  97. }
  98. tcpconf.nwtc_flags= 0;
  99. if (sinp->sin_port == 0)
  100. tcpconf.nwtc_flags |= NWTC_LP_SEL;
  101. else
  102. {
  103. tcpconf.nwtc_flags |= NWTC_LP_SET;
  104. tcpconf.nwtc_locport= sinp->sin_port;
  105. }
  106. r= ioctl(sock, NWIOSTCPCONF, &tcpconf);
  107. return r;
  108. }
  109. static int _udp_bind(int sock, const struct sockaddr *address,
  110. socklen_t address_len, nwio_udpopt_t *udpoptp)
  111. {
  112. int r;
  113. unsigned long curr_flags;
  114. nwio_udpopt_t udpopt;
  115. struct sockaddr_in *sinp;
  116. sinp= (struct sockaddr_in *) __UNCONST(address);
  117. if (sinp->sin_family != AF_INET || address_len < sizeof(*sinp))
  118. {
  119. #if DEBUG
  120. fprintf(stderr, "bind(udp): sin_family = %d, len = %d\n",
  121. sinp->sin_family, address_len);
  122. #endif
  123. errno= EAFNOSUPPORT;
  124. return -1;
  125. }
  126. if (sinp->sin_addr.s_addr != INADDR_ANY &&
  127. sinp->sin_addr.s_addr != udpoptp->nwuo_locaddr)
  128. {
  129. errno= EADDRNOTAVAIL;
  130. return -1;
  131. }
  132. udpopt.nwuo_flags= 0;
  133. if (sinp->sin_port == 0)
  134. udpopt.nwuo_flags |= NWUO_LP_SEL;
  135. else
  136. {
  137. udpopt.nwuo_flags |= NWUO_LP_SET;
  138. udpopt.nwuo_locport= sinp->sin_port;
  139. }
  140. curr_flags= udpoptp->nwuo_flags;
  141. if (!(curr_flags & NWUO_ACC_MASK))
  142. udpopt.nwuo_flags |= NWUO_EXCL;
  143. if (!(curr_flags & (NWUO_EN_LOC|NWUO_DI_LOC)))
  144. udpopt.nwuo_flags |= NWUO_EN_LOC;
  145. if (!(curr_flags & (NWUO_EN_BROAD|NWUO_DI_BROAD)))
  146. udpopt.nwuo_flags |= NWUO_EN_BROAD;
  147. if (!(curr_flags & (NWUO_RP_SET|NWUO_RP_ANY)))
  148. udpopt.nwuo_flags |= NWUO_RP_ANY;
  149. if (!(curr_flags & (NWUO_RA_SET|NWUO_RA_ANY)))
  150. udpopt.nwuo_flags |= NWUO_RA_ANY;
  151. if (!(curr_flags & (NWUO_RWDATONLY|NWUO_RWDATALL)))
  152. udpopt.nwuo_flags |= NWUO_RWDATALL;
  153. if (!(curr_flags & (NWUO_EN_IPOPT|NWUO_DI_IPOPT)))
  154. udpopt.nwuo_flags |= NWUO_DI_IPOPT;
  155. r= ioctl(sock, NWIOSUDPOPT, &udpopt);
  156. return r;
  157. }
  158. static int _uds_bind(int sock, const struct sockaddr *address,
  159. socklen_t address_len, struct sockaddr_un *uds_addr)
  160. {
  161. int r;
  162. int did_mknod;
  163. if (address == NULL) {
  164. errno = EFAULT;
  165. return -1;
  166. }
  167. did_mknod = 0;
  168. r = mknod(((struct sockaddr_un *) __UNCONST(address))->sun_path,
  169. S_IFSOCK|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0);
  170. if (r == -1 && errno != EEXIST) {
  171. return -1;
  172. } else if (r == 0) {
  173. did_mknod = 1;
  174. }
  175. /* perform the bind */
  176. r= ioctl(sock, NWIOSUDSADDR, (void *) __UNCONST(address));
  177. if (r == -1 && did_mknod) {
  178. /* bind() failed in pfs, so we roll back the
  179. * file system change
  180. */
  181. unlink(((struct sockaddr_un *) __UNCONST(address))->sun_path);
  182. }
  183. return r;
  184. }