net_sockets.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * \file net_sockets.h
  3. *
  4. * \brief Network communication functions
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_NET_SOCKETS_H
  26. #define MBEDTLS_NET_SOCKETS_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include "ssl.h"
  33. #include <stddef.h>
  34. #include <stdint.h>
  35. #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
  36. #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
  37. #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
  38. #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
  39. #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
  40. #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
  41. #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
  42. #define MBEDTLS_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
  43. #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052 /**< Failed to get an IP address for the given hostname. */
  44. #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043 /**< Buffer is too small to hold the data. */
  45. #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 /**< The context is invalid, eg because it was free()ed. */
  46. #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
  47. #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
  48. #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * Wrapper type for sockets.
  54. *
  55. * Currently backed by just a file descriptor, but might be more in the future
  56. * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
  57. * structures for hand-made UDP demultiplexing).
  58. */
  59. typedef struct
  60. {
  61. int fd; /**< The underlying file descriptor */
  62. }
  63. mbedtls_net_context;
  64. /**
  65. * \brief Initialize a context
  66. * Just makes the context ready to be used or freed safely.
  67. *
  68. * \param ctx Context to initialize
  69. */
  70. void mbedtls_net_init( mbedtls_net_context *ctx );
  71. /**
  72. * \brief Initiate a connection with host:port in the given protocol
  73. *
  74. * \param ctx Socket to use
  75. * \param host Host to connect to
  76. * \param port Port to connect to
  77. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  78. *
  79. * \return 0 if successful, or one of:
  80. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  81. * MBEDTLS_ERR_NET_UNKNOWN_HOST,
  82. * MBEDTLS_ERR_NET_CONNECT_FAILED
  83. *
  84. * \note Sets the socket in connected mode even with UDP.
  85. */
  86. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto );
  87. /**
  88. * \brief Create a receiving socket on bind_ip:port in the chosen
  89. * protocol. If bind_ip == NULL, all interfaces are bound.
  90. *
  91. * \param ctx Socket to use
  92. * \param bind_ip IP to bind to, can be NULL
  93. * \param port Port number to use
  94. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  95. *
  96. * \return 0 if successful, or one of:
  97. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  98. * MBEDTLS_ERR_NET_BIND_FAILED,
  99. * MBEDTLS_ERR_NET_LISTEN_FAILED
  100. *
  101. * \note Regardless of the protocol, opens the sockets and binds it.
  102. * In addition, make the socket listening if protocol is TCP.
  103. */
  104. int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto );
  105. /**
  106. * \brief Accept a connection from a remote client
  107. *
  108. * \param bind_ctx Relevant socket
  109. * \param client_ctx Will contain the connected client socket
  110. * \param client_ip Will contain the client IP address
  111. * \param buf_size Size of the client_ip buffer
  112. * \param ip_len Will receive the size of the client IP written
  113. *
  114. * \return 0 if successful, or
  115. * MBEDTLS_ERR_NET_ACCEPT_FAILED, or
  116. * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
  117. * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
  118. * non-blocking and accept() would block.
  119. */
  120. int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  121. mbedtls_net_context *client_ctx,
  122. void *client_ip, size_t buf_size, size_t *ip_len );
  123. /**
  124. * \brief Set the socket blocking
  125. *
  126. * \param ctx Socket to set
  127. *
  128. * \return 0 if successful, or a non-zero error code
  129. */
  130. int mbedtls_net_set_block( mbedtls_net_context *ctx );
  131. /**
  132. * \brief Set the socket non-blocking
  133. *
  134. * \param ctx Socket to set
  135. *
  136. * \return 0 if successful, or a non-zero error code
  137. */
  138. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
  139. /**
  140. * \brief Portable usleep helper
  141. *
  142. * \param usec Amount of microseconds to sleep
  143. *
  144. * \note Real amount of time slept will not be less than
  145. * select()'s timeout granularity (typically, 10ms).
  146. */
  147. void mbedtls_net_usleep( unsigned long usec );
  148. /**
  149. * \brief Read at most 'len' characters. If no error occurs,
  150. * the actual amount read is returned.
  151. *
  152. * \param ctx Socket
  153. * \param buf The buffer to write to
  154. * \param len Maximum length of the buffer
  155. *
  156. * \return the number of bytes received,
  157. * or a non-zero error code; with a non-blocking socket,
  158. * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
  159. */
  160. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
  161. /**
  162. * \brief Write at most 'len' characters. If no error occurs,
  163. * the actual amount read is returned.
  164. *
  165. * \param ctx Socket
  166. * \param buf The buffer to read from
  167. * \param len The length of the buffer
  168. *
  169. * \return the number of bytes sent,
  170. * or a non-zero error code; with a non-blocking socket,
  171. * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
  172. */
  173. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
  174. /**
  175. * \brief Read at most 'len' characters, blocking for at most
  176. * 'timeout' seconds. If no error occurs, the actual amount
  177. * read is returned.
  178. *
  179. * \param ctx Socket
  180. * \param buf The buffer to write to
  181. * \param len Maximum length of the buffer
  182. * \param timeout Maximum number of milliseconds to wait for data
  183. * 0 means no timeout (wait forever)
  184. *
  185. * \return the number of bytes received,
  186. * or a non-zero error code:
  187. * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
  188. * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  189. *
  190. * \note This function will block (until data becomes available or
  191. * timeout is reached) even if the socket is set to
  192. * non-blocking. Handling timeouts with non-blocking reads
  193. * requires a different strategy.
  194. */
  195. int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  196. uint32_t timeout );
  197. /**
  198. * \brief Gracefully shutdown the connection and free associated data
  199. *
  200. * \param ctx The context to free
  201. */
  202. void mbedtls_net_free( mbedtls_net_context *ctx );
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif /* net_sockets.h */