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