net_sockets.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * \file net_sockets.h
  3. *
  4. * \brief Network sockets abstraction layer to integrate Mbed TLS into a
  5. * BSD-style sockets API.
  6. *
  7. * The network sockets module provides an example integration of the
  8. * Mbed TLS library into a BSD sockets implementation. The module is
  9. * intended to be an example of how Mbed TLS can be integrated into a
  10. * networking stack, as well as to be Mbed TLS's network integration
  11. * for its supported platforms.
  12. *
  13. * The module is intended only to be used with the Mbed TLS library and
  14. * is not intended to be used by third party application software
  15. * directly.
  16. *
  17. * The supported platforms are as follows:
  18. * * Microsoft Windows and Windows CE
  19. * * POSIX/Unix platforms including Linux, OS X
  20. *
  21. */
  22. /*
  23. * Copyright The Mbed TLS Contributors
  24. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  25. */
  26. #ifndef MBEDTLS_NET_SOCKETS_H
  27. #define MBEDTLS_NET_SOCKETS_H
  28. #include "mbedtls/private_access.h"
  29. #include "mbedtls/build_info.h"
  30. #include "mbedtls/ssl.h"
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. /** Failed to open a socket. */
  34. #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042
  35. /** The connection to the given server / port failed. */
  36. #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044
  37. /** Binding of the socket failed. */
  38. #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046
  39. /** Could not listen on the socket. */
  40. #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048
  41. /** Could not accept the incoming connection. */
  42. #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A
  43. /** Reading information from the socket failed. */
  44. #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C
  45. /** Sending information through the socket failed. */
  46. #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E
  47. /** Connection was reset by peer. */
  48. #define MBEDTLS_ERR_NET_CONN_RESET -0x0050
  49. /** Failed to get an IP address for the given hostname. */
  50. #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052
  51. /** Buffer is too small to hold the data. */
  52. #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043
  53. /** The context is invalid, eg because it was free()ed. */
  54. #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045
  55. /** Polling the net context failed. */
  56. #define MBEDTLS_ERR_NET_POLL_FAILED -0x0047
  57. /** Input invalid. */
  58. #define MBEDTLS_ERR_NET_BAD_INPUT_DATA -0x0049
  59. #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
  60. #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
  61. #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
  62. #define MBEDTLS_NET_POLL_READ 1 /**< Used in \c mbedtls_net_poll to check for pending data */
  63. #define MBEDTLS_NET_POLL_WRITE 2 /**< Used in \c mbedtls_net_poll to check if write possible */
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. /**
  68. * Wrapper type for sockets.
  69. *
  70. * Currently backed by just a file descriptor, but might be more in the future
  71. * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
  72. * structures for hand-made UDP demultiplexing).
  73. */
  74. typedef struct mbedtls_net_context {
  75. /** The underlying file descriptor.
  76. *
  77. * This field is only guaranteed to be present on POSIX/Unix-like platforms.
  78. * On other platforms, it may have a different type, have a different
  79. * meaning, or be absent altogether.
  80. */
  81. int fd;
  82. }
  83. mbedtls_net_context;
  84. /**
  85. * \brief Initialize a context
  86. * Just makes the context ready to be used or freed safely.
  87. *
  88. * \param ctx Context to initialize
  89. */
  90. void mbedtls_net_init(mbedtls_net_context *ctx);
  91. /**
  92. * \brief Initiate a connection with host:port in the given protocol
  93. *
  94. * \param ctx Socket to use
  95. * \param host Host to connect to
  96. * \param port Port to connect to
  97. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  98. *
  99. * \return 0 if successful, or one of:
  100. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  101. * MBEDTLS_ERR_NET_UNKNOWN_HOST,
  102. * MBEDTLS_ERR_NET_CONNECT_FAILED
  103. *
  104. * \note Sets the socket in connected mode even with UDP.
  105. */
  106. int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto);
  107. /**
  108. * \brief Create a receiving socket on bind_ip:port in the chosen
  109. * protocol. If bind_ip == NULL, all interfaces are bound.
  110. *
  111. * \param ctx Socket to use
  112. * \param bind_ip IP to bind to, can be NULL
  113. * \param port Port number to use
  114. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  115. *
  116. * \return 0 if successful, or one of:
  117. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  118. * MBEDTLS_ERR_NET_UNKNOWN_HOST,
  119. * MBEDTLS_ERR_NET_BIND_FAILED,
  120. * MBEDTLS_ERR_NET_LISTEN_FAILED
  121. *
  122. * \note Regardless of the protocol, opens the sockets and binds it.
  123. * In addition, make the socket listening if protocol is TCP.
  124. */
  125. int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto);
  126. /**
  127. * \brief Accept a connection from a remote client
  128. *
  129. * \param bind_ctx Relevant socket
  130. * \param client_ctx Will contain the connected client socket
  131. * \param client_ip Will contain the client IP address, can be NULL
  132. * \param buf_size Size of the client_ip buffer
  133. * \param cip_len Will receive the size of the client IP written,
  134. * can be NULL if client_ip is null
  135. *
  136. * \return 0 if successful, or
  137. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  138. * MBEDTLS_ERR_NET_BIND_FAILED,
  139. * MBEDTLS_ERR_NET_ACCEPT_FAILED, or
  140. * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
  141. * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
  142. * non-blocking and accept() would block.
  143. */
  144. int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
  145. mbedtls_net_context *client_ctx,
  146. void *client_ip, size_t buf_size, size_t *cip_len);
  147. /**
  148. * \brief Check and wait for the context to be ready for read/write
  149. *
  150. * \note The current implementation of this function uses
  151. * select() and returns an error if the file descriptor
  152. * is \c FD_SETSIZE or greater.
  153. *
  154. * \param ctx Socket to check
  155. * \param rw Bitflag composed of MBEDTLS_NET_POLL_READ and
  156. * MBEDTLS_NET_POLL_WRITE specifying the events
  157. * to wait for:
  158. * - If MBEDTLS_NET_POLL_READ is set, the function
  159. * will return as soon as the net context is available
  160. * for reading.
  161. * - If MBEDTLS_NET_POLL_WRITE is set, the function
  162. * will return as soon as the net context is available
  163. * for writing.
  164. * \param timeout Maximal amount of time to wait before returning,
  165. * in milliseconds. If \c timeout is zero, the
  166. * function returns immediately. If \c timeout is
  167. * -1u, the function blocks potentially indefinitely.
  168. *
  169. * \return Bitmask composed of MBEDTLS_NET_POLL_READ/WRITE
  170. * on success or timeout, or a negative return code otherwise.
  171. */
  172. int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout);
  173. /**
  174. * \brief Set the socket blocking
  175. *
  176. * \param ctx Socket to set
  177. *
  178. * \return 0 if successful, or a non-zero error code
  179. */
  180. int mbedtls_net_set_block(mbedtls_net_context *ctx);
  181. /**
  182. * \brief Set the socket non-blocking
  183. *
  184. * \param ctx Socket to set
  185. *
  186. * \return 0 if successful, or a non-zero error code
  187. */
  188. int mbedtls_net_set_nonblock(mbedtls_net_context *ctx);
  189. /**
  190. * \brief Portable usleep helper
  191. *
  192. * \param usec Amount of microseconds to sleep
  193. *
  194. * \note Real amount of time slept will not be less than
  195. * select()'s timeout granularity (typically, 10ms).
  196. */
  197. void mbedtls_net_usleep(unsigned long usec);
  198. /**
  199. * \brief Read at most 'len' characters. If no error occurs,
  200. * the actual amount read is returned.
  201. *
  202. * \param ctx Socket
  203. * \param buf The buffer to write to
  204. * \param len Maximum length of the buffer
  205. *
  206. * \return the number of bytes received,
  207. * or a non-zero error code; with a non-blocking socket,
  208. * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
  209. */
  210. int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
  211. /**
  212. * \brief Write at most 'len' characters. If no error occurs,
  213. * the actual amount written is returned.
  214. *
  215. * \param ctx Socket
  216. * \param buf The buffer to read from
  217. * \param len The length of the buffer
  218. *
  219. * \return the number of bytes sent,
  220. * or a non-zero error code; with a non-blocking socket,
  221. * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
  222. */
  223. int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len);
  224. /**
  225. * \brief Read at most 'len' characters, blocking for at most
  226. * 'timeout' seconds. If no error occurs, the actual amount
  227. * read is returned.
  228. *
  229. * \note The current implementation of this function uses
  230. * select() and returns an error if the file descriptor
  231. * is \c FD_SETSIZE or greater.
  232. *
  233. * \param ctx Socket
  234. * \param buf The buffer to write to
  235. * \param len Maximum length of the buffer
  236. * \param timeout Maximum number of milliseconds to wait for data
  237. * 0 means no timeout (wait forever)
  238. *
  239. * \return The number of bytes received if successful.
  240. * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out.
  241. * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  242. * Another negative error code (MBEDTLS_ERR_NET_xxx)
  243. * for other failures.
  244. *
  245. * \note This function will block (until data becomes available or
  246. * timeout is reached) even if the socket is set to
  247. * non-blocking. Handling timeouts with non-blocking reads
  248. * requires a different strategy.
  249. */
  250. int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len,
  251. uint32_t timeout);
  252. /**
  253. * \brief Closes down the connection and free associated data
  254. *
  255. * \param ctx The context to close
  256. *
  257. * \note This function frees and clears data associated with the
  258. * context but does not free the memory pointed to by \p ctx.
  259. * This memory is the responsibility of the caller.
  260. */
  261. void mbedtls_net_close(mbedtls_net_context *ctx);
  262. /**
  263. * \brief Gracefully shutdown the connection and free associated data
  264. *
  265. * \param ctx The context to free
  266. *
  267. * \note This function frees and clears data associated with the
  268. * context but does not free the memory pointed to by \p ctx.
  269. * This memory is the responsibility of the caller.
  270. */
  271. void mbedtls_net_free(mbedtls_net_context *ctx);
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif /* net_sockets.h */