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
98
101
102
107
112
113
114
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
143
144
149
150
151
152
153
154
155
156
157
158
159
160
163
170
171
174
175
176
181
182
185
190
191
196
197
200
204
205
208
209
210
211
212
213
217
218
219
220
221
222
226
227
231
232
236
237
238
239
240
241
242
243
246
247
253
254
255
256
257
260
261
262
263
264
265
266
267
277
278
279
280
281
284
285
286
287
288
289
293
294
295
296
297
298
302
303
307
308
312
313
314
315
316
317
318
319
322
323
324
325
326
327
328
329
330
331
332
333
/* ... */
#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
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));
}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