1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
95
99
100
101
102
103
104
105
106
109
110
115
122
123
124
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
153
154
159
160
161
162
163
164
165
166
167
168
169
170
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
200
201
202
209
210
216
217
218
227
228
229
230
231
232
233
234
235
236
237
238
241
242
243
244
245
246
247
248
249
250
251
255
256
258
259
260
261
266
267
272
278
279
286
287
289
290
291
292
293
294
298
299
300
301
302
303
309
310
314
315
316
346
347
348
349
350
353
358
359
364
365
368
372
373
376
377
378
379
380
381
385
386
387
388
389
390
394
395
399
400
404
405
406
407
408
409
410
411
414
415
420
421
422
423
424
427
428
429
430
431
432
433
434
444
445
446
447
448
451
452
453
454
455
456
460
461
462
463
464
465
469
470
474
475
479
480
481
482
483
484
485
486
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* ... */
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <string.h>
#include <stdint.h>
#if defined(MBEDTLS_NET_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#endif
#include "mbedtls/net_sockets.h"
#include "lwip/dhcp.h"
#include "lwip/tcpip.h"
#include "lwip/ip_addr.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "netif/ethernet.h"
#include "ethernetif.h"
#include "stm32f4xx_hal.h"
#include "main.h"
10 includes
static struct netif netif;
static int initialized = 0;
struct sockaddr_storage client_addr;
static int net_would_block( const mbedtls_net_context *ctx );
/* ... */
void mbedtls_net_init( mbedtls_net_context *ctx )
{
ip4_addr_t addr;
ip4_addr_t netmask;
ip4_addr_t gw;
uint32_t start;
ctx->fd = -1;
if (initialized != 0)
return;
tcpip_init(NULL, NULL);
IP4_ADDR(&addr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
IP4_ADDR(&netmask, MASK_ADDR0, MASK_ADDR1, MASK_ADDR2, MASK_ADDR3);
netif_add(&netif, &addr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
netif_set_default(&netif);
if (netif_is_link_up(&netif))
{
netif_set_up(&netif);
}if (netif_is_link_up(&netif)) { ... }
else
{
netif_set_down(&netif);
}else { ... }
#ifdef USE_DHCP
dhcp_start(&netif);
#endif
osDelay(500);
start = HAL_GetTick();
while((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
{
}while ((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000)) { ... }
if (netif.ip_addr.addr == 0)
{
printf(" Failed to get ip address! Please check your network configuration.\n");
Error_Handler();
}if (netif.ip_addr.addr == 0) { ... }
else
{
printf("\nIpAdress = %lu.%lu.%lu.%lu\n", (netif.ip_addr.addr & 0xff), ((netif.ip_addr.addr >> 8) & 0xff)
, ((netif.ip_addr.addr >> 16) & 0xff), ((netif.ip_addr.addr >> 24)& 0xff));
initialized = 1;
}else { ... }
}{ ... }
/* ... */
int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
{
int ret;
struct addrinfo hints;
struct addrinfo *list;
struct addrinfo *current;
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, &list) != 0)
return MBEDTLS_ERR_NET_UNKNOWN_HOST;
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for( current = list; current != NULL; current = current->ai_next)
{
ctx->fd = (int) socket(current->ai_family, current->ai_socktype, current->ai_protocol);
if(ctx->fd < 0)
{
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}if (ctx->fd < 0) { ... }
if(connect(ctx->fd, current->ai_addr, (uint32_t)current->ai_addrlen) == 0)
{
ret = 0;
break;
}if (connect(ctx->fd, current->ai_addr, (uint32_t)current->ai_addrlen) == 0) { ... }
close( ctx->fd );
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
}for (current = list; current != NULL; current = current->ai_next) { ... }
freeaddrinfo(list);
return ret;
}{ ... }
/* ... */
int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
{
int n, ret;
struct addrinfo hints, *addr_list, *cur;
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( bind_ip == NULL )
hints.ai_flags = AI_PASSIVE;
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 )
{
ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
cur->ai_protocol );
if( ctx->fd < 0 )
{
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}if (ctx->fd < 0) { ... }
n = 1;
if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &n, sizeof( n ) ) != 0 )
{
close( ctx->fd );
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}if (setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof( n ) ) != 0) { ... }
if( bind( ctx->fd, cur->ai_addr, (socklen_t)cur->ai_addrlen ) != 0 )
{
close( ctx->fd );
ret = MBEDTLS_ERR_NET_BIND_FAILED;
continue;
}if (bind( ctx->fd, cur->ai_addr, (socklen_t)cur->ai_addrlen ) != 0) { ... }
if( proto == MBEDTLS_NET_PROTO_TCP )
{
if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
{
close( ctx->fd );
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
continue;
}if (listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0) { ... }
}if (proto == MBEDTLS_NET_PROTO_TCP) { ... }
ret = 0;
break;
}for (cur = addr_list; cur != NULL; cur = cur->ai_next) { ... }
freeaddrinfo( addr_list );
return ret;
}{ ... }
/* ... */
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;
#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
#error _SOCKLEN_T
socklen_t n = (socklen_t) sizeof( client_addr );
socklen_t type_len = (socklen_t) sizeof( type );/* ... */
#else
int n = (int) sizeof( client_addr );
int type_len = (int) sizeof( type );/* ... */
#endif
if(getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE, (void *) &type, (u32_t *)(&type_len) ) != 0 ||
(type != SOCK_STREAM && type != SOCK_DGRAM))
{
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE, (void *) &type, (u32_t *)(&type_len) ) != 0 || (type != SOCK_STREAM && type != SOCK_DGRAM)) { ... }
if( type == SOCK_STREAM )
{
ret = client_ctx->fd = (int) accept( bind_ctx->fd, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
}if (type == SOCK_STREAM) { ... }
else
{
char buf[1] = { 0 };
ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
}else { ... }
if( ret < 0 )
{
if( net_would_block( bind_ctx ) != 0 )
return( MBEDTLS_ERR_SSL_WANT_READ );
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}if (ret < 0) { ... }
/* ... */
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 );
}if (connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0) { ... }
client_ctx->fd = bind_ctx->fd;
bind_ctx->fd = -1;
n = sizeof( struct sockaddr_storage );
if( getsockname(client_ctx->fd, (struct sockaddr *) &local_addr, (u32_t*)(&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 (getsockname(client_ctx->fd, (struct sockaddr *) &local_addr, (u32_t*)(&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) { ... }
if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
{
return( MBEDTLS_ERR_NET_BIND_FAILED );
}if (bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0) { ... }
}if (type != SOCK_STREAM) { ... }
if( client_ip != NULL )
{
if( client_addr.ss_family == AF_INET )
{
#if LWIP_IPV4
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 );
}if (buf_size < *ip_len) { ... }
memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );/* ... */
#endif
}if (client_addr.ss_family == AF_INET) { ... }
else
{
#if LWIP_IPV6
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 );
}if (buf_size < *ip_len) { ... }
memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);/* ... */
#endif
}else { ... }
}if (client_ip != NULL) { ... }
return( 0 );
}{ ... }
/* ... */
int mbedtls_net_set_block( mbedtls_net_context *ctx )
{
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
return 0;
}{ ... }
int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
{
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
return 0;
}{ ... }
/* ... */
void mbedtls_net_usleep( unsigned long usec )
{
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
}{ ... }
/* ... */
int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
{
int32_t ret;
int32_t fd = ((mbedtls_net_context *) ctx)->fd;
if( fd < 0 )
{
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}if (fd < 0) { ... }
ret = (int32_t) read( fd, buf, len );
if( ret < 0 )
{
if(net_would_block(ctx) != 0)
{
return MBEDTLS_ERR_SSL_WANT_READ;
}if (net_would_block(ctx) != 0) { ... }
if(errno == EPIPE || errno == ECONNRESET)
{
return MBEDTLS_ERR_NET_CONN_RESET;
}if (errno == EPIPE || errno == ECONNRESET) { ... }
if(errno == EINTR)
{
return MBEDTLS_ERR_SSL_WANT_READ;
}if (errno == EINTR) { ... }
return MBEDTLS_ERR_NET_RECV_FAILED;
}if (ret < 0) { ... }
return ret;
}{ ... }
/* ... */
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
uint32_t timeout )
{
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
return mbedtls_net_recv( ctx, buf, len );
}{ ... }
static int net_would_block( const mbedtls_net_context *ctx )
{
/* ... */
int val = 0;
UNUSED(val);
if( ( fcntl( ctx->fd, F_GETFL, val) & O_NONBLOCK ) != O_NONBLOCK )
return( 0 );
switch( errno )
{
#if defined EAGAIN
case EAGAIN:
#endif
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAINcase EAGAIN:
case EWOULDBLOCK:
#endif
return( 1 );case EWOULDBLOCK:
}switch (errno) { ... }
return( 0 );
}{ ... }
/* ... */
int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
{
int32_t ret;
int fd = ((mbedtls_net_context *) ctx)->fd;
if( fd < 0 )
{
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}if (fd < 0) { ... }
ret = (int32_t) write(fd, buf, len);
if( ret < 0 )
{
if(net_would_block(ctx) != 0)
{
return MBEDTLS_ERR_SSL_WANT_WRITE;
}if (net_would_block(ctx) != 0) { ... }
if(errno == EPIPE || errno == ECONNRESET)
{
return MBEDTLS_ERR_NET_CONN_RESET;
}if (errno == EPIPE || errno == ECONNRESET) { ... }
if(errno == EINTR)
{
return MBEDTLS_ERR_SSL_WANT_WRITE;
}if (errno == EINTR) { ... }
return MBEDTLS_ERR_NET_SEND_FAILED;
}if (ret < 0) { ... }
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;
initialized = 0;
}{ ... }
/* ... */#endif