Select one of the symbols to view example projects that use it.
 
Outline
#include "mbedtls/config.h"
#include
#include <string.h>
#include <stdint.h>
#include "mbedtls/platform.h"
#include <stdlib.h>
#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"
netif
initialized
client_addr
mbedtls_net_init(mbedtls_net_context *)
mbedtls_net_connect(mbedtls_net_context *, const char *, const char *, int)
mbedtls_net_bind(mbedtls_net_context *, const char *, const char *, int)
mbedtls_net_accept(mbedtls_net_context *, mbedtls_net_context *, void *, size_t, size_t *)
mbedtls_net_set_block(mbedtls_net_context *)
mbedtls_net_set_nonblock(mbedtls_net_context *)
mbedtls_net_usleep(unsigned long)
mbedtls_net_recv(void *, unsigned char *, size_t)
mbedtls_net_recv_timeout(void *, unsigned char *, size_t, uint32_t)
net_would_block(const mbedtls_net_context *)
mbedtls_net_send(void *, const unsigned char *, size_t)
mbedtls_net_free(mbedtls_net_context *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesSSL_ServerSrc/net_sockets.c
 
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
62
63
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** * Portions COPYRIGHT 2016 STMicroelectronics * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * ****************************************************************************** * @file net_sockets.c * @author MCD Application Team * @brief TCP/IP or UDP/IP networking functions implementation based on LwIP API see the file "mbedTLS/library/net_socket_template.c" for the standard implementation ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ #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 ); /* * Initialize LwIP stack and get a dynamic IP address. *//* ... */ 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); /* IP default settings, to be overridden by DHCP */ 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); /* add the network interface */ netif_add(&netif, &addr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input); /* register the default network interface. */ 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 { ... } }{ ... } /* * Initiate a TCP connection with host:port and the given protocol *//* ... */ 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; /* Do name resolution with both IPv6 and IPv4 */ 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; /* Try the sockaddrs until a connection succeeds */ 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; }{ ... } /* * Create a listening socket on bind_ip:port *//* ... */ 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; /* Bind to IPv6 and/or IPv4, but only in the desired protocol */ 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 ); /* Try the sockaddrs until a binding succeeds */ 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) { ... } /* Listen only makes sense for TCP */ 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) { ... } /* I we ever get there, it's a success */ ret = 0; break; }for (cur = addr_list; cur != NULL; cur = cur->ai_next) { ... } freeaddrinfo( addr_list ); return ret; }{ ... } /* * Accept a connection from a remote client *//* ... */ 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 /* Is this a TCP or UDP socket? */ 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 ) { /* TCP: actual accept() */ ret = client_ctx->fd = (int) accept( bind_ctx->fd, (struct sockaddr *) &client_addr, (u32_t *)(&n) ); }if (type == SOCK_STREAM) { ... } else { /* UDP: wait for a message, but keep it in the queue */ 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) { ... } /* UDP: hijack the listening socket to communicate with the client, * then bind a new socket to accept new connections *//* ... */ 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; /* In case we exit early */ 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 ); }{ ... } /* * Set the socket blocking or non-blocking *//* ... */ 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; }{ ... } /* * Portable usleep helper *//* ... */ void mbedtls_net_usleep( unsigned long usec ) { mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__); }{ ... } /* * Read at most 'len' characters *//* ... */ 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; }{ ... } /* * Read at most 'len' characters, blocking for at most 'timeout' ms *//* ... */ 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 ) { /* * Do not return 'WOULD BLOCK' on a non-blocking socket *//* ... */ 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 ); }{ ... } /* * Write at most 'len' characters *//* ... */ 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; }{ ... } /* * Gracefully close the connection *//* ... */ 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 /* MBEDTLS_NET_C */