blob: c8bcde06986816f5708d8f5e412b8b729610c054 [file] [log] [blame]
Andres AG788aa4a2016-09-14 14:32:09 +01001/**
2 * \file net_sockets.h
3 *
Simon Butcherca33caf2018-07-18 17:52:14 +01004 * \brief Network sockets abstraction layer to integrate Mbed TLS into a
5 * BSD-style sockets API.
6 *
7 * The network sockets module provides an example integration of the
8 * Mbed TLS library into a BSD sockets implementation. The module is
Simon Butcher5cf4d062018-07-23 14:36:40 +01009 * intended to be an example of how Mbed TLS can be integrated into a
10 * networking stack, as well as to be Mbed TLS's network integration
11 * for its supported platforms.
Simon Butcherca33caf2018-07-18 17:52:14 +010012 *
Simon Butcher5cf4d062018-07-23 14:36:40 +010013 * The module is intended only to be used with the Mbed TLS library and
14 * is not intended to be used by third party application software
15 * directly.
Simon Butcherca33caf2018-07-18 17:52:14 +010016 *
17 * The supported platforms are as follows:
18 * * Microsoft Windows and Windows CE
19 * * POSIX/Unix platforms including Linux, OS X
20 *
Darryl Greena40a1012018-01-05 15:33:17 +000021 */
22/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020023 * Copyright The Mbed TLS Contributors
Andres AG788aa4a2016-09-14 14:32:09 +010024 * SPDX-License-Identifier: Apache-2.0
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License"); you may
27 * not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
Andres AG788aa4a2016-09-14 14:32:09 +010037 */
38#ifndef MBEDTLS_NET_SOCKETS_H
39#define MBEDTLS_NET_SOCKETS_H
40
41#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010042#include "mbedtls/config.h"
Andres AG788aa4a2016-09-14 14:32:09 +010043#else
44#include MBEDTLS_CONFIG_FILE
45#endif
46
Jaeden Amero6609aef2019-07-04 20:01:14 +010047#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010048
49#include <stddef.h>
50#include <stdint.h>
51
Gilles Peskinea3974432021-07-26 18:48:10 +020052/** Failed to open a socket. */
53#define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042
54/** The connection to the given server / port failed. */
55#define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044
56/** Binding of the socket failed. */
57#define MBEDTLS_ERR_NET_BIND_FAILED -0x0046
58/** Could not listen on the socket. */
59#define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048
60/** Could not accept the incoming connection. */
61#define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A
62/** Reading information from the socket failed. */
63#define MBEDTLS_ERR_NET_RECV_FAILED -0x004C
64/** Sending information through the socket failed. */
65#define MBEDTLS_ERR_NET_SEND_FAILED -0x004E
66/** Connection was reset by peer. */
67#define MBEDTLS_ERR_NET_CONN_RESET -0x0050
68/** Failed to get an IP address for the given hostname. */
69#define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052
70/** Buffer is too small to hold the data. */
71#define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043
72/** The context is invalid, eg because it was free()ed. */
73#define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045
74/** Polling the net context failed. */
75#define MBEDTLS_ERR_NET_POLL_FAILED -0x0047
76/** Input invalid. */
77#define MBEDTLS_ERR_NET_BAD_INPUT_DATA -0x0049
Andres AG788aa4a2016-09-14 14:32:09 +010078
79#define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
80
81#define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
82#define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
83
Hanno Beckere09ca3d2017-05-22 15:06:06 +010084#define MBEDTLS_NET_POLL_READ 1 /**< Used in \c mbedtls_net_poll to check for pending data */
85#define MBEDTLS_NET_POLL_WRITE 2 /**< Used in \c mbedtls_net_poll to check if write possible */
86
Andres AG788aa4a2016-09-14 14:32:09 +010087#ifdef __cplusplus
88extern "C" {
89#endif
90
91/**
92 * Wrapper type for sockets.
93 *
94 * Currently backed by just a file descriptor, but might be more in the future
95 * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
96 * structures for hand-made UDP demultiplexing).
97 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098typedef struct mbedtls_net_context {
Andres AG788aa4a2016-09-14 14:32:09 +010099 int fd; /**< The underlying file descriptor */
100}
101mbedtls_net_context;
102
103/**
104 * \brief Initialize a context
105 * Just makes the context ready to be used or freed safely.
106 *
107 * \param ctx Context to initialize
108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109void mbedtls_net_init(mbedtls_net_context *ctx);
Andres AG788aa4a2016-09-14 14:32:09 +0100110
111/**
112 * \brief Initiate a connection with host:port in the given protocol
113 *
114 * \param ctx Socket to use
115 * \param host Host to connect to
116 * \param port Port to connect to
117 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
118 *
119 * \return 0 if successful, or one of:
120 * MBEDTLS_ERR_NET_SOCKET_FAILED,
121 * MBEDTLS_ERR_NET_UNKNOWN_HOST,
122 * MBEDTLS_ERR_NET_CONNECT_FAILED
123 *
124 * \note Sets the socket in connected mode even with UDP.
125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto);
Andres AG788aa4a2016-09-14 14:32:09 +0100127
128/**
129 * \brief Create a receiving socket on bind_ip:port in the chosen
130 * protocol. If bind_ip == NULL, all interfaces are bound.
131 *
132 * \param ctx Socket to use
133 * \param bind_ip IP to bind to, can be NULL
134 * \param port Port number to use
135 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
136 *
137 * \return 0 if successful, or one of:
138 * MBEDTLS_ERR_NET_SOCKET_FAILED,
Gilles Peskine9264e012021-03-03 12:25:06 +0100139 * MBEDTLS_ERR_NET_UNKNOWN_HOST,
Andres AG788aa4a2016-09-14 14:32:09 +0100140 * MBEDTLS_ERR_NET_BIND_FAILED,
141 * MBEDTLS_ERR_NET_LISTEN_FAILED
142 *
143 * \note Regardless of the protocol, opens the sockets and binds it.
144 * In addition, make the socket listening if protocol is TCP.
145 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto);
Andres AG788aa4a2016-09-14 14:32:09 +0100147
148/**
149 * \brief Accept a connection from a remote client
150 *
151 * \param bind_ctx Relevant socket
152 * \param client_ctx Will contain the connected client socket
aitap4dab5512017-01-13 13:22:31 +0400153 * \param client_ip Will contain the client IP address, can be NULL
Andres AG788aa4a2016-09-14 14:32:09 +0100154 * \param buf_size Size of the client_ip buffer
aitap4dab5512017-01-13 13:22:31 +0400155 * \param ip_len Will receive the size of the client IP written,
Ivan Krylov5cb1f092018-03-24 18:48:04 +0300156 * can be NULL if client_ip is null
Andres AG788aa4a2016-09-14 14:32:09 +0100157 *
158 * \return 0 if successful, or
Gilles Peskine9264e012021-03-03 12:25:06 +0100159 * MBEDTLS_ERR_NET_SOCKET_FAILED,
160 * MBEDTLS_ERR_NET_BIND_FAILED,
Andres AG788aa4a2016-09-14 14:32:09 +0100161 * MBEDTLS_ERR_NET_ACCEPT_FAILED, or
162 * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
163 * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
164 * non-blocking and accept() would block.
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
167 mbedtls_net_context *client_ctx,
168 void *client_ip, size_t buf_size, size_t *ip_len);
Andres AG788aa4a2016-09-14 14:32:09 +0100169
170/**
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100171 * \brief Check and wait for the context to be ready for read/write
172 *
Gilles Peskinee28f2362021-02-24 19:51:23 +0100173 * \note The current implementation of this function uses
174 * select() and returns an error if the file descriptor
Gilles Peskinec8dab5b2021-03-01 11:39:21 +0100175 * is \c FD_SETSIZE or greater.
Gilles Peskinee28f2362021-02-24 19:51:23 +0100176 *
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100177 * \param ctx Socket to check
178 * \param rw Bitflag composed of MBEDTLS_NET_POLL_READ and
179 * MBEDTLS_NET_POLL_WRITE specifying the events
180 * to wait for:
181 * - If MBEDTLS_NET_POLL_READ is set, the function
182 * will return as soon as the net context is available
183 * for reading.
184 * - If MBEDTLS_NET_POLL_WRITE is set, the function
185 * will return as soon as the net context is available
186 * for writing.
187 * \param timeout Maximal amount of time to wait before returning,
188 * in milliseconds. If \c timeout is zero, the
189 * function returns immediately. If \c timeout is
190 * -1u, the function blocks potentially indefinitely.
191 *
192 * \return Bitmask composed of MBEDTLS_NET_POLL_READ/WRITE
193 * on success or timeout, or a negative return code otherwise.
194 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100196
197/**
Andres AG788aa4a2016-09-14 14:32:09 +0100198 * \brief Set the socket blocking
199 *
200 * \param ctx Socket to set
201 *
202 * \return 0 if successful, or a non-zero error code
203 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204int mbedtls_net_set_block(mbedtls_net_context *ctx);
Andres AG788aa4a2016-09-14 14:32:09 +0100205
206/**
207 * \brief Set the socket non-blocking
208 *
209 * \param ctx Socket to set
210 *
211 * \return 0 if successful, or a non-zero error code
212 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213int mbedtls_net_set_nonblock(mbedtls_net_context *ctx);
Andres AG788aa4a2016-09-14 14:32:09 +0100214
215/**
216 * \brief Portable usleep helper
217 *
218 * \param usec Amount of microseconds to sleep
219 *
220 * \note Real amount of time slept will not be less than
221 * select()'s timeout granularity (typically, 10ms).
222 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223void mbedtls_net_usleep(unsigned long usec);
Andres AG788aa4a2016-09-14 14:32:09 +0100224
225/**
226 * \brief Read at most 'len' characters. If no error occurs,
227 * the actual amount read is returned.
228 *
229 * \param ctx Socket
230 * \param buf The buffer to write to
231 * \param len Maximum length of the buffer
232 *
233 * \return the number of bytes received,
234 * or a non-zero error code; with a non-blocking socket,
235 * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
236 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
Andres AG788aa4a2016-09-14 14:32:09 +0100238
239/**
240 * \brief Write at most 'len' characters. If no error occurs,
241 * the actual amount read is returned.
242 *
243 * \param ctx Socket
244 * \param buf The buffer to read from
245 * \param len The length of the buffer
246 *
247 * \return the number of bytes sent,
248 * or a non-zero error code; with a non-blocking socket,
249 * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
250 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len);
Andres AG788aa4a2016-09-14 14:32:09 +0100252
253/**
254 * \brief Read at most 'len' characters, blocking for at most
255 * 'timeout' seconds. If no error occurs, the actual amount
256 * read is returned.
257 *
Gilles Peskinee28f2362021-02-24 19:51:23 +0100258 * \note The current implementation of this function uses
259 * select() and returns an error if the file descriptor
Gilles Peskinec8dab5b2021-03-01 11:39:21 +0100260 * is \c FD_SETSIZE or greater.
Gilles Peskinee28f2362021-02-24 19:51:23 +0100261 *
Andres AG788aa4a2016-09-14 14:32:09 +0100262 * \param ctx Socket
263 * \param buf The buffer to write to
264 * \param len Maximum length of the buffer
265 * \param timeout Maximum number of milliseconds to wait for data
266 * 0 means no timeout (wait forever)
267 *
Gilles Peskine9264e012021-03-03 12:25:06 +0100268 * \return The number of bytes received if successful.
269 * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out.
Andres AG788aa4a2016-09-14 14:32:09 +0100270 * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
Gilles Peskine9264e012021-03-03 12:25:06 +0100271 * Another negative error code (MBEDTLS_ERR_NET_xxx)
272 * for other failures.
Andres AG788aa4a2016-09-14 14:32:09 +0100273 *
274 * \note This function will block (until data becomes available or
275 * timeout is reached) even if the socket is set to
276 * non-blocking. Handling timeouts with non-blocking reads
277 * requires a different strategy.
278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len,
280 uint32_t timeout);
Andres AG788aa4a2016-09-14 14:32:09 +0100281
282/**
Robert Larsendf8e5112019-08-23 10:55:47 +0200283 * \brief Closes down the connection and free associated data
284 *
285 * \param ctx The context to close
286 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287void mbedtls_net_close(mbedtls_net_context *ctx);
Robert Larsendf8e5112019-08-23 10:55:47 +0200288
289/**
Andres AG788aa4a2016-09-14 14:32:09 +0100290 * \brief Gracefully shutdown the connection and free associated data
291 *
292 * \param ctx The context to free
293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294void mbedtls_net_free(mbedtls_net_context *ctx);
Andres AG788aa4a2016-09-14 14:32:09 +0100295
296#ifdef __cplusplus
297}
298#endif
299
300#endif /* net_sockets.h */