socket.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /****************************************************************************
  2. * net/socket/socket.c
  3. *
  4. * Copyright (C) 2007-2009, 2012, 2014-2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/socket.h>
  40. #include <errno.h>
  41. #include <assert.h>
  42. #include <debug.h>
  43. #include "usrsock/usrsock.h"
  44. #include "socket/socket.h"
  45. #ifdef CONFIG_NET
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: psock_socket
  51. *
  52. * Description:
  53. * socket() creates an endpoint for communication and returns a socket
  54. * structure.
  55. *
  56. * NOTE: This function does not set the reference count on the socket
  57. * structure. This down by the socket() front end when socket structure
  58. * was allocated. Internal OS users of psock_socket() must set the s_crefs
  59. * field to one if psock_socket() returns success.
  60. *
  61. * Input Parameters:
  62. * domain (see sys/socket.h)
  63. * type (see sys/socket.h)
  64. * protocol (see sys/socket.h)
  65. * psock A pointer to a user allocated socket structure to be initialized.
  66. *
  67. * Returned Value:
  68. * Returns zero (OK) on success. On failure, it returns a negated errno
  69. * value to indicate the nature of the error:
  70. *
  71. * EACCES
  72. * Permission to create a socket of the specified type and/or protocol
  73. * is denied.
  74. * EAFNOSUPPORT
  75. * The implementation does not support the specified address family.
  76. * EINVAL
  77. * Unknown protocol, or protocol family not available.
  78. * EMFILE
  79. * Process file table overflow.
  80. * ENFILE
  81. * The system limit on the total number of open files has been reached.
  82. * ENOBUFS or ENOMEM
  83. * Insufficient memory is available. The socket cannot be created until
  84. * sufficient resources are freed.
  85. * EPROTONOSUPPORT
  86. * The protocol type or the specified protocol is not supported within
  87. * this domain.
  88. *
  89. ****************************************************************************/
  90. int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
  91. {
  92. FAR const struct sock_intf_s *sockif = NULL;
  93. int ret;
  94. /* Initialize the socket structure */
  95. psock->s_domain = domain;
  96. psock->s_type = type;
  97. psock->s_conn = NULL;
  98. #if defined(CONFIG_NET_TCP_WRITE_BUFFERS) || defined(CONFIG_NET_UDP_WRITE_BUFFERS)
  99. psock->s_sndcb = NULL;
  100. #endif
  101. #ifdef CONFIG_NET_USRSOCK
  102. if (domain != PF_LOCAL && domain != PF_UNSPEC)
  103. {
  104. /* Handle special setup for USRSOCK sockets (user-space networking
  105. * stack).
  106. */
  107. ret = g_usrsock_sockif.si_setup(psock, protocol);
  108. if (ret == -ENETDOWN)
  109. {
  110. /* -ENETDOWN means that USRSOCK daemon is not running. Attempt to
  111. * open socket with kernel networking stack.
  112. */
  113. }
  114. else
  115. {
  116. psock->s_sockif = &g_usrsock_sockif;
  117. if (ret < 0)
  118. {
  119. return ret;
  120. }
  121. return ret;
  122. }
  123. }
  124. #endif /* CONFIG_NET_USRSOCK */
  125. /* Get the socket interface */
  126. sockif = net_sockif(domain, type, protocol);
  127. if (sockif == NULL)
  128. {
  129. nerr("ERROR: socket address family unsupported: %d\n", domain);
  130. return -EAFNOSUPPORT;
  131. }
  132. /* The remaining of the socket initialization depends on the address
  133. * family.
  134. */
  135. DEBUGASSERT(sockif->si_setup != NULL);
  136. psock->s_sockif = sockif;
  137. ret = sockif->si_setup(psock, protocol);
  138. if (ret < 0)
  139. {
  140. nerr("ERROR: socket si_setup() failed: %d\n", ret);
  141. return ret;
  142. }
  143. return OK;
  144. }
  145. /****************************************************************************
  146. * Name: socket
  147. *
  148. * Description:
  149. * socket() creates an endpoint for communication and returns a descriptor.
  150. *
  151. * Input Parameters:
  152. * domain (see sys/socket.h)
  153. * type (see sys/socket.h)
  154. * protocol (see sys/socket.h)
  155. *
  156. * Returned Value:
  157. * A non-negative socket descriptor on success; -1 on error with errno set
  158. * appropriately.
  159. *
  160. * EACCES
  161. * Permission to create a socket of the specified type and/or protocol
  162. * is denied.
  163. * EAFNOSUPPORT
  164. * The implementation does not support the specified address family.
  165. * EINVAL
  166. * Unknown protocol, or protocol family not available.
  167. * EMFILE
  168. * Process file table overflow.
  169. * ENFILE
  170. * The system limit on the total number of open files has been reached.
  171. * ENOBUFS or ENOMEM
  172. * Insufficient memory is available. The socket cannot be created until
  173. * sufficient resources are freed.
  174. * EPROTONOSUPPORT
  175. * The protocol type or the specified protocol is not supported within
  176. * this domain.
  177. *
  178. * Assumptions:
  179. *
  180. ****************************************************************************/
  181. int socket(int domain, int type, int protocol)
  182. {
  183. FAR struct socket *psock;
  184. int errcode;
  185. int sockfd;
  186. int ret;
  187. /* Allocate a socket descriptor */
  188. sockfd = sockfd_allocate(0);
  189. if (sockfd < 0)
  190. {
  191. nerr("ERROR: Failed to allocate a socket descriptor\n");
  192. errcode = ENFILE;
  193. goto errout;
  194. }
  195. /* Get the underlying socket structure */
  196. psock = sockfd_socket(sockfd);
  197. if (!psock)
  198. {
  199. errcode = ENOSYS; /* should not happen */
  200. goto errout_with_sockfd;
  201. }
  202. /* Initialize the socket structure */
  203. ret = psock_socket(domain, type, protocol, psock);
  204. if (ret < 0)
  205. {
  206. nerr("ERROR: psock_socket() failed: %d\n", ret);
  207. errcode = -ret;
  208. goto errout_with_sockfd;
  209. }
  210. return sockfd;
  211. errout_with_sockfd:
  212. sockfd_release(sockfd);
  213. errout:
  214. set_errno(errcode);
  215. return ERROR;
  216. }
  217. #endif /* CONFIG_NET */