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