Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
net_sockets.h
00001 /** 00002 * \file net_sockets.h 00003 * 00004 * \brief Network sockets abstraction layer to integrate Mbed TLS into a 00005 * BSD-style sockets API. 00006 * 00007 * The network sockets module provides an example integration of the 00008 * Mbed TLS library into a BSD sockets implementation. The module is 00009 * intended to be an example of how Mbed TLS can be integrated into a 00010 * networking stack, as well as to be Mbed TLS's network integration 00011 * for its supported platforms. 00012 * 00013 * The module is intended only to be used with the Mbed TLS library and 00014 * is not intended to be used by third party application software 00015 * directly. 00016 * 00017 * The supported platforms are as follows: 00018 * * Microsoft Windows and Windows CE 00019 * * POSIX/Unix platforms including Linux, OS X 00020 * 00021 */ 00022 /* 00023 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00024 * SPDX-License-Identifier: Apache-2.0 00025 * 00026 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00027 * not use this file except in compliance with the License. 00028 * You may obtain a copy of the License at 00029 * 00030 * http://www.apache.org/licenses/LICENSE-2.0 00031 * 00032 * Unless required by applicable law or agreed to in writing, software 00033 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00034 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00035 * See the License for the specific language governing permissions and 00036 * limitations under the License. 00037 * 00038 * This file is part of mbed TLS (https://tls.mbed.org) 00039 */ 00040 #ifndef MBEDTLS_NET_SOCKETS_H 00041 #define MBEDTLS_NET_SOCKETS_H 00042 00043 #if !defined(MBEDTLS_CONFIG_FILE) 00044 #include "mbedtls/config.h" 00045 #else 00046 #include MBEDTLS_CONFIG_FILE 00047 #endif 00048 00049 #include "mbedtls/ssl.h" 00050 00051 #include <stddef.h> 00052 #include <stdint.h> 00053 00054 #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */ 00055 #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */ 00056 #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */ 00057 #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */ 00058 #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */ 00059 #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */ 00060 #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */ 00061 #define MBEDTLS_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */ 00062 #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052 /**< Failed to get an IP address for the given hostname. */ 00063 #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043 /**< Buffer is too small to hold the data. */ 00064 #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 /**< The context is invalid, eg because it was free()ed. */ 00065 #define MBEDTLS_ERR_NET_POLL_FAILED -0x0047 /**< Polling the net context failed. */ 00066 #define MBEDTLS_ERR_NET_BAD_INPUT_DATA -0x0049 /**< Input invalid. */ 00067 00068 #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */ 00069 00070 #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 00071 #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 00072 00073 #define MBEDTLS_NET_POLL_READ 1 /**< Used in \c mbedtls_net_poll to check for pending data */ 00074 #define MBEDTLS_NET_POLL_WRITE 2 /**< Used in \c mbedtls_net_poll to check if write possible */ 00075 00076 #ifdef __cplusplus 00077 extern "C" { 00078 #endif 00079 00080 /** 00081 * Wrapper type for sockets. 00082 * 00083 * Currently backed by just a file descriptor, but might be more in the future 00084 * (eg two file descriptors for combined IPv4 + IPv6 support, or additional 00085 * structures for hand-made UDP demultiplexing). 00086 */ 00087 typedef struct mbedtls_net_context 00088 { 00089 int fd; /**< The underlying file descriptor */ 00090 } 00091 mbedtls_net_context; 00092 00093 /** 00094 * \brief Initialize a context 00095 * Just makes the context ready to be used or freed safely. 00096 * 00097 * \param ctx Context to initialize 00098 */ 00099 void mbedtls_net_init( mbedtls_net_context *ctx ); 00100 00101 /** 00102 * \brief Initiate a connection with host:port in the given protocol 00103 * 00104 * \param ctx Socket to use 00105 * \param host Host to connect to 00106 * \param port Port to connect to 00107 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP 00108 * 00109 * \return 0 if successful, or one of: 00110 * MBEDTLS_ERR_NET_SOCKET_FAILED, 00111 * MBEDTLS_ERR_NET_UNKNOWN_HOST, 00112 * MBEDTLS_ERR_NET_CONNECT_FAILED 00113 * 00114 * \note Sets the socket in connected mode even with UDP. 00115 */ 00116 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto ); 00117 00118 /** 00119 * \brief Create a receiving socket on bind_ip:port in the chosen 00120 * protocol. If bind_ip == NULL, all interfaces are bound. 00121 * 00122 * \param ctx Socket to use 00123 * \param bind_ip IP to bind to, can be NULL 00124 * \param port Port number to use 00125 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP 00126 * 00127 * \return 0 if successful, or one of: 00128 * MBEDTLS_ERR_NET_SOCKET_FAILED, 00129 * MBEDTLS_ERR_NET_BIND_FAILED, 00130 * MBEDTLS_ERR_NET_LISTEN_FAILED 00131 * 00132 * \note Regardless of the protocol, opens the sockets and binds it. 00133 * In addition, make the socket listening if protocol is TCP. 00134 */ 00135 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto ); 00136 00137 /** 00138 * \brief Accept a connection from a remote client 00139 * 00140 * \param bind_ctx Relevant socket 00141 * \param client_ctx Will contain the connected client socket 00142 * \param client_ip Will contain the client IP address, can be NULL 00143 * \param buf_size Size of the client_ip buffer 00144 * \param ip_len Will receive the size of the client IP written, 00145 * can be NULL if client_ip is null 00146 * 00147 * \return 0 if successful, or 00148 * MBEDTLS_ERR_NET_ACCEPT_FAILED, or 00149 * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small, 00150 * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to 00151 * non-blocking and accept() would block. 00152 */ 00153 int mbedtls_net_accept( mbedtls_net_context *bind_ctx, 00154 mbedtls_net_context *client_ctx, 00155 void *client_ip, size_t buf_size, size_t *ip_len ); 00156 00157 /** 00158 * \brief Check and wait for the context to be ready for read/write 00159 * 00160 * \param ctx Socket to check 00161 * \param rw Bitflag composed of MBEDTLS_NET_POLL_READ and 00162 * MBEDTLS_NET_POLL_WRITE specifying the events 00163 * to wait for: 00164 * - If MBEDTLS_NET_POLL_READ is set, the function 00165 * will return as soon as the net context is available 00166 * for reading. 00167 * - If MBEDTLS_NET_POLL_WRITE is set, the function 00168 * will return as soon as the net context is available 00169 * for writing. 00170 * \param timeout Maximal amount of time to wait before returning, 00171 * in milliseconds. If \c timeout is zero, the 00172 * function returns immediately. If \c timeout is 00173 * -1u, the function blocks potentially indefinitely. 00174 * 00175 * \return Bitmask composed of MBEDTLS_NET_POLL_READ/WRITE 00176 * on success or timeout, or a negative return code otherwise. 00177 */ 00178 int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout ); 00179 00180 /** 00181 * \brief Set the socket blocking 00182 * 00183 * \param ctx Socket to set 00184 * 00185 * \return 0 if successful, or a non-zero error code 00186 */ 00187 int mbedtls_net_set_block( mbedtls_net_context *ctx ); 00188 00189 /** 00190 * \brief Set the socket non-blocking 00191 * 00192 * \param ctx Socket to set 00193 * 00194 * \return 0 if successful, or a non-zero error code 00195 */ 00196 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx ); 00197 00198 /** 00199 * \brief Portable usleep helper 00200 * 00201 * \param usec Amount of microseconds to sleep 00202 * 00203 * \note Real amount of time slept will not be less than 00204 * select()'s timeout granularity (typically, 10ms). 00205 */ 00206 void mbedtls_net_usleep( unsigned long usec ); 00207 00208 /** 00209 * \brief Read at most 'len' characters. If no error occurs, 00210 * the actual amount read is returned. 00211 * 00212 * \param ctx Socket 00213 * \param buf The buffer to write to 00214 * \param len Maximum length of the buffer 00215 * 00216 * \return the number of bytes received, 00217 * or a non-zero error code; with a non-blocking socket, 00218 * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block. 00219 */ 00220 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ); 00221 00222 /** 00223 * \brief Write at most 'len' characters. If no error occurs, 00224 * the actual amount read is returned. 00225 * 00226 * \param ctx Socket 00227 * \param buf The buffer to read from 00228 * \param len The length of the buffer 00229 * 00230 * \return the number of bytes sent, 00231 * or a non-zero error code; with a non-blocking socket, 00232 * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block. 00233 */ 00234 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ); 00235 00236 /** 00237 * \brief Read at most 'len' characters, blocking for at most 00238 * 'timeout' seconds. If no error occurs, the actual amount 00239 * read is returned. 00240 * 00241 * \param ctx Socket 00242 * \param buf The buffer to write to 00243 * \param len Maximum length of the buffer 00244 * \param timeout Maximum number of milliseconds to wait for data 00245 * 0 means no timeout (wait forever) 00246 * 00247 * \return the number of bytes received, 00248 * or a non-zero error code: 00249 * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out, 00250 * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal. 00251 * 00252 * \note This function will block (until data becomes available or 00253 * timeout is reached) even if the socket is set to 00254 * non-blocking. Handling timeouts with non-blocking reads 00255 * requires a different strategy. 00256 */ 00257 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, 00258 uint32_t timeout ); 00259 00260 /** 00261 * \brief Closes down the connection and free associated data 00262 * 00263 * \param ctx The context to close 00264 */ 00265 void mbedtls_net_close( mbedtls_net_context *ctx ); 00266 00267 /** 00268 * \brief Gracefully shutdown the connection and free associated data 00269 * 00270 * \param ctx The context to free 00271 */ 00272 void mbedtls_net_free( mbedtls_net_context *ctx ); 00273 00274 #ifdef __cplusplus 00275 } 00276 #endif 00277 00278 #endif /* net_sockets.h */
Generated on Tue Jul 12 2022 13:54:37 by
