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.
Fork of OmniWheels by
net_sockets.h
00001 /** 00002 * \file net_sockets.h 00003 * 00004 * \brief Network communication functions 00005 */ 00006 /* 00007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 * This file is part of mbed TLS (https://tls.mbed.org) 00023 */ 00024 #ifndef MBEDTLS_NET_SOCKETS_H 00025 #define MBEDTLS_NET_SOCKETS_H 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include "ssl.h" 00034 00035 #include <stddef.h> 00036 #include <stdint.h> 00037 00038 #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */ 00039 #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */ 00040 #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */ 00041 #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */ 00042 #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */ 00043 #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */ 00044 #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */ 00045 #define MBEDTLS_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */ 00046 #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052 /**< Failed to get an IP address for the given hostname. */ 00047 #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043 /**< Buffer is too small to hold the data. */ 00048 #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 /**< The context is invalid, eg because it was free()ed. */ 00049 00050 #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */ 00051 00052 #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 00053 #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 00054 00055 #ifdef __cplusplus 00056 extern "C" { 00057 #endif 00058 00059 /** 00060 * Wrapper type for sockets. 00061 * 00062 * Currently backed by just a file descriptor, but might be more in the future 00063 * (eg two file descriptors for combined IPv4 + IPv6 support, or additional 00064 * structures for hand-made UDP demultiplexing). 00065 */ 00066 typedef struct 00067 { 00068 int fd; /**< The underlying file descriptor */ 00069 } 00070 mbedtls_net_context; 00071 00072 /** 00073 * \brief Initialize a context 00074 * Just makes the context ready to be used or freed safely. 00075 * 00076 * \param ctx Context to initialize 00077 */ 00078 void mbedtls_net_init( mbedtls_net_context *ctx ); 00079 00080 /** 00081 * \brief Initiate a connection with host:port in the given protocol 00082 * 00083 * \param ctx Socket to use 00084 * \param host Host to connect to 00085 * \param port Port to connect to 00086 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP 00087 * 00088 * \return 0 if successful, or one of: 00089 * MBEDTLS_ERR_NET_SOCKET_FAILED, 00090 * MBEDTLS_ERR_NET_UNKNOWN_HOST, 00091 * MBEDTLS_ERR_NET_CONNECT_FAILED 00092 * 00093 * \note Sets the socket in connected mode even with UDP. 00094 */ 00095 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto ); 00096 00097 /** 00098 * \brief Create a receiving socket on bind_ip:port in the chosen 00099 * protocol. If bind_ip == NULL, all interfaces are bound. 00100 * 00101 * \param ctx Socket to use 00102 * \param bind_ip IP to bind to, can be NULL 00103 * \param port Port number to use 00104 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP 00105 * 00106 * \return 0 if successful, or one of: 00107 * MBEDTLS_ERR_NET_SOCKET_FAILED, 00108 * MBEDTLS_ERR_NET_BIND_FAILED, 00109 * MBEDTLS_ERR_NET_LISTEN_FAILED 00110 * 00111 * \note Regardless of the protocol, opens the sockets and binds it. 00112 * In addition, make the socket listening if protocol is TCP. 00113 */ 00114 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto ); 00115 00116 /** 00117 * \brief Accept a connection from a remote client 00118 * 00119 * \param bind_ctx Relevant socket 00120 * \param client_ctx Will contain the connected client socket 00121 * \param client_ip Will contain the client IP address 00122 * \param buf_size Size of the client_ip buffer 00123 * \param ip_len Will receive the size of the client IP written 00124 * 00125 * \return 0 if successful, or 00126 * MBEDTLS_ERR_NET_ACCEPT_FAILED, or 00127 * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small, 00128 * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to 00129 * non-blocking and accept() would block. 00130 */ 00131 int mbedtls_net_accept( mbedtls_net_context *bind_ctx, 00132 mbedtls_net_context *client_ctx, 00133 void *client_ip, size_t buf_size, size_t *ip_len ); 00134 00135 /** 00136 * \brief Set the socket blocking 00137 * 00138 * \param ctx Socket to set 00139 * 00140 * \return 0 if successful, or a non-zero error code 00141 */ 00142 int mbedtls_net_set_block( mbedtls_net_context *ctx ); 00143 00144 /** 00145 * \brief Set the socket non-blocking 00146 * 00147 * \param ctx Socket to set 00148 * 00149 * \return 0 if successful, or a non-zero error code 00150 */ 00151 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx ); 00152 00153 /** 00154 * \brief Portable usleep helper 00155 * 00156 * \param usec Amount of microseconds to sleep 00157 * 00158 * \note Real amount of time slept will not be less than 00159 * select()'s timeout granularity (typically, 10ms). 00160 */ 00161 void mbedtls_net_usleep( unsigned long usec ); 00162 00163 /** 00164 * \brief Read at most 'len' characters. If no error occurs, 00165 * the actual amount read is returned. 00166 * 00167 * \param ctx Socket 00168 * \param buf The buffer to write to 00169 * \param len Maximum length of the buffer 00170 * 00171 * \return the number of bytes received, 00172 * or a non-zero error code; with a non-blocking socket, 00173 * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block. 00174 */ 00175 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ); 00176 00177 /** 00178 * \brief Write at most 'len' characters. If no error occurs, 00179 * the actual amount read is returned. 00180 * 00181 * \param ctx Socket 00182 * \param buf The buffer to read from 00183 * \param len The length of the buffer 00184 * 00185 * \return the number of bytes sent, 00186 * or a non-zero error code; with a non-blocking socket, 00187 * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block. 00188 */ 00189 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ); 00190 00191 /** 00192 * \brief Read at most 'len' characters, blocking for at most 00193 * 'timeout' seconds. If no error occurs, the actual amount 00194 * read is returned. 00195 * 00196 * \param ctx Socket 00197 * \param buf The buffer to write to 00198 * \param len Maximum length of the buffer 00199 * \param timeout Maximum number of milliseconds to wait for data 00200 * 0 means no timeout (wait forever) 00201 * 00202 * \return the number of bytes received, 00203 * or a non-zero error code: 00204 * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out, 00205 * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal. 00206 * 00207 * \note This function will block (until data becomes available or 00208 * timeout is reached) even if the socket is set to 00209 * non-blocking. Handling timeouts with non-blocking reads 00210 * requires a different strategy. 00211 */ 00212 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, 00213 uint32_t timeout ); 00214 00215 /** 00216 * \brief Gracefully shutdown the connection and free associated data 00217 * 00218 * \param ctx The context to free 00219 */ 00220 void mbedtls_net_free( mbedtls_net_context *ctx ); 00221 00222 #ifdef __cplusplus 00223 } 00224 #endif 00225 00226 #endif /* net_sockets.h */
Generated on Fri Jul 22 2022 04:53:57 by
 1.7.2
 1.7.2 
    