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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
87
91
92
93
94
95
96
97
100
101
106
111
112
113
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
142
143
148
149
150
151
152
153
154
155
156
157
158
159
162
169
170
173
174
175
180
181
184
189
190
195
196
199
203
204
207
208
209
210
211
212
216
217
218
219
220
221
225
226
230
231
235
236
237
238
239
240
241
242
245
246
252
253
254
255
256
259
260
261
262
263
264
265
275
276
277
278
279
282
283
284
285
286
287
291
292
293
294
295
296
300
301
305
306
310
311
312
313
314
315
316
317
320
321
322
323
324
325
326
327
328
329
330
331
/* ... */
#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/netdb.h"
#include "lwip/sockets.h"
#include "netif/ethernet.h"
#include "ethernetif.h"
#include "stm32f4xx_hal.h"
#include "main.h"
9 includes
static struct netif netif;
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;
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
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));
}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 ret = 0;
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
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 )
{
mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
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;
}{ ... }
/* ... */#endif