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