1
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
43
47
48
51
55
56
59
60
61
62
63
64
67
68
69
70
71
72
73
74
77
78
79
80
81
82
83
87
88
93
94
95
96
97
98
99
100
101
102
103
106
107
108
109
110
111
112
113
114
115
118
119
120
121
122
123
124
125
128
129
130
131
132
133
137
138
139
140
147
148
149
155
156
162
163
168
169
170
177
178
179
180
181
182
183
184
185
186
187
188
189
190
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
233
234
239
240
241
242
243
244
245
246
247
254
255
257
258
259
260
261
264
265
266
267
268
269
270
271
272
273
274
277
278
281
282
283
284
285
286
287
288
289
290
293
294
295
296
297
298
299
300
301
302
303
306
307
308
309
310
311
312
313
314
315
318
322
323
327
328
331
338
339
342
343
344
345
346
347
350
351
352
353
354
357
358
361
362
365
366
367
368
369
370
371
372
375
376
377
378
379
380
381
382
383
386
387
388
389
390
391
392
393
394
395
396
399
400
407
408
409
410
411
412
415
416
417
418
419
420
423
424
425
426
427
430
431
434
435
438
439
440
441
442
443
444
445
448
449
450
453
454
455
456
457
458
459
460
/* ... */
#include <mbedtls/build_info.h>
#if !defined(MBEDTLS_NET_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#define mbedtls_time time
#define mbedtls_time_t time_t/* ... */
#endif
#include "mbedtls/net_sockets.h"
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>12 includes
/* ... */
static int net_prepare( void )
{
return ( 0 );
}{ ... }
/* ... */
void mbedtls_net_init( mbedtls_net_context *ctx )
{
ctx->fd = -1;
}{ ... }
/* ... */
int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
{
int ret;
struct addrinfo hints, *addr_list, *cur;
if ( ( ret = net_prepare() ) != 0 ) {
return ( ret );
}{...}
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
if ( getaddrinfo( host, port, &hints, &addr_list ) != 0 ) {
return ( MBEDTLS_ERR_NET_UNKNOWN_HOST );
}{...}
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
int fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
if ( fd < 0 ) {
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}{...}
if ( connect( fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) {
ctx->fd = fd;
ret = 0;
break;
}{...}
close( fd );
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
}{...}
freeaddrinfo( addr_list );
return ( ret );
}{ ... }
/* ... */
int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
{
int ret;
struct addrinfo hints, *addr_list, *cur;
struct sockaddr_storage *serv_addr = NULL;
#if SO_REUSE
int n = 1;
#endif
if ( ( ret = net_prepare() ) != 0 ) {
return ( ret );
}{...}
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
if ( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) {
return ( MBEDTLS_ERR_NET_UNKNOWN_HOST );
}{...}
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
int fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
if ( fd < 0 ) {
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}{...}
#if SO_REUSE
if ( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &n, sizeof( n ) ) != 0 ) {
close( fd );
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}{...}
#endif/* ... */
serv_addr = (struct sockaddr_storage *) cur->ai_addr;
#if CONFIG_LWIP_IPV4
if (cur->ai_family == AF_INET) {
struct sockaddr_in *p = (struct sockaddr_in *)serv_addr;
p->sin_addr.s_addr = htonl(INADDR_ANY);
}{...}
#endif/* ... */
#if CONFIG_LWIP_IPV6
if (cur->ai_family == AF_INET6) {
struct sockaddr_in6 *p = (struct sockaddr_in6 *) serv_addr;
struct in6_addr inaddr_any = IN6ADDR_ANY_INIT;
p->sin6_addr = inaddr_any;
}{...}
#endif/* ... */
if ( bind( fd, (struct sockaddr *)serv_addr, cur->ai_addrlen ) != 0 ) {
close( fd );
ret = MBEDTLS_ERR_NET_BIND_FAILED;
continue;
}{...}
if ( proto == MBEDTLS_NET_PROTO_TCP ) {
if ( listen( fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) {
close( fd );
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
continue;
}{...}
}{...}
ctx->fd = fd;
ret = 0;
break;
}{...}
freeaddrinfo( addr_list );
return ( ret );
}{ ... }
/* ... */
static int net_would_block( const mbedtls_net_context *ctx )
{
int error = errno;
switch ( errno = error ) {
#if defined EAGAIN
case EAGAIN:
#endif
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
return ( 1 );
}{...}
return ( 0 );
}{ ... }
/* ... */
int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
mbedtls_net_context *client_ctx,
void *client_ip, size_t buf_size, size_t *ip_len )
{
int ret;
int type;
struct sockaddr_storage client_addr;
socklen_t n = (socklen_t) sizeof( client_addr );
socklen_t type_len = (socklen_t) sizeof( type );
if ( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
(void *) &type, (socklen_t *) &type_len ) != 0 ||
( type != SOCK_STREAM && type != SOCK_DGRAM ) ) {
return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}{...}
if ( type == SOCK_STREAM ) {
ret = client_ctx->fd = (int) accept( bind_ctx->fd,
(struct sockaddr *) &client_addr, &n );
}{...} else {
char buf[1] = { 0 };
ret = recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
(struct sockaddr *) &client_addr, &n );
}{...}
if ( ret < 0 ) {
if ( net_would_block( bind_ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_READ );
}{...}
return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}{...}
/* ... */
if ( type != SOCK_STREAM ) {
struct sockaddr_storage local_addr;
int one = 1;
if ( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 ) {
return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}{...}
client_ctx->fd = bind_ctx->fd;
bind_ctx->fd = -1;
n = sizeof( struct sockaddr_storage );
if ( getsockname( client_ctx->fd,
(struct sockaddr *) &local_addr, &n ) != 0 ||
( bind_ctx->fd = (int) socket( local_addr.ss_family,
SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &one, sizeof( one ) ) != 0 ) {
return ( MBEDTLS_ERR_NET_SOCKET_FAILED );
}{...}
if ( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 ) {
return ( MBEDTLS_ERR_NET_BIND_FAILED );
}{...}
}{...}
if ( client_ip != NULL ) {
#ifdef CONFIG_LWIP_IPV4
if( client_addr.ss_family == AF_INET )
{
struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
*ip_len = sizeof( addr4->sin_addr.s_addr );
if ( buf_size < *ip_len ) {
return ( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
}{...}
memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
}{...}
#endif/* ... */
#ifdef CONFIG_LWIP_IPV6
if( client_addr.ss_family == AF_INET6 )
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
*ip_len = sizeof( addr6->sin6_addr.s6_addr );
if( buf_size < *ip_len ) {
return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
}{...}
memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
}{...}
#endif/* ... */
}{...}
return ( 0 );
}{ ... }
/* ... */
int mbedtls_net_set_block( mbedtls_net_context *ctx )
{
return ( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) & ~O_NONBLOCK ) );
}{ ... }
int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
{
return ( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) | O_NONBLOCK ) );
}{ ... }
/* ... */
void mbedtls_net_usleep( unsigned long usec )
{
struct timeval tv;
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
select( 0, NULL, NULL, NULL, &tv );
}{ ... }
/* ... */
int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
{
int ret;
int fd = ((mbedtls_net_context *) ctx)->fd;
if ( fd < 0 ) {
return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}{...}
ret = (int) read( fd, buf, len );
if ( ret < 0 ) {
if ( net_would_block( ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_READ );
}{...}
if ( errno == EPIPE || errno == ECONNRESET ) {
return ( MBEDTLS_ERR_NET_CONN_RESET );
}{...}
if ( errno == EINTR ) {
return ( MBEDTLS_ERR_SSL_WANT_READ );
}{...}
return ( MBEDTLS_ERR_NET_RECV_FAILED );
}{...}
return ( ret );
}{ ... }
/* ... */
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
uint32_t timeout )
{
int ret;
struct timeval tv;
fd_set read_fds;
int fd = ((mbedtls_net_context *) ctx)->fd;
if ( fd < 0 ) {
return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}{...}
FD_ZERO( &read_fds );
FD_SET( fd, &read_fds );
tv.tv_sec = timeout / 1000;
tv.tv_usec = ( timeout % 1000 ) * 1000;
ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
if ( ret == 0 ) {
return ( MBEDTLS_ERR_SSL_TIMEOUT );
}{...}
if ( ret < 0 ) {
if ( errno == EINTR ) {
return ( MBEDTLS_ERR_SSL_WANT_READ );
}{...}
return ( MBEDTLS_ERR_NET_RECV_FAILED );
}{...}
return ( mbedtls_net_recv( ctx, buf, len ) );
}{ ... }
/* ... */
int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
{
int ret;
int fd = ((mbedtls_net_context *) ctx)->fd;
if ( fd < 0 ) {
return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}{...}
ret = (int) write( fd, buf, len );
if ( ret < 0 ) {
if ( net_would_block( ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_WRITE );
}{...}
if ( errno == EPIPE || errno == ECONNRESET ) {
return ( MBEDTLS_ERR_NET_CONN_RESET );
}{...}
if ( errno == EINTR ) {
return ( MBEDTLS_ERR_SSL_WANT_WRITE );
}{...}
return ( MBEDTLS_ERR_NET_SEND_FAILED );
}{...}
return ( ret );
}{ ... }
/* ... */
void mbedtls_net_free( mbedtls_net_context *ctx )
{
if ( ctx->fd == -1) {
return;
}{...}
shutdown( ctx->fd, 2);
close(ctx->fd);
ctx->fd = -1;
}{ ... }
/* ... */#endif