net.h 8.3 KB

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