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/netdb.h"
#include "lwip/sockets.h"
#include "netif/ethernet.h"
#include "ethernetif.h"
#include "stm32f4xx_hal.h"
#include "main.h"
netif
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_ClientSrc/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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** * 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/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 ); /* * 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; 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 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 { ... } }{ ... } /* * 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 ret = 0; mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__); 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 ) { mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__); 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 ) { /* * Never 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; }{ ... } /* ... */#endif /* MBEDTLS_NET_C */