net_sockets.h 8.3 KB

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