1 /*
2 * TCP/IP or UDP/IP networking functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 /* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
21 * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
22 * Harmless on other platforms. */
23 #ifndef _POSIX_C_SOURCE
24 #define _POSIX_C_SOURCE 200112L
25 #endif
26 #ifndef _XOPEN_SOURCE
27 #define _XOPEN_SOURCE 600 /* sockaddr_storage */
28 #endif
29
30 #include "common.h"
31
32 #if defined(MBEDTLS_NET_C)
33
34 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
35 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
36 !defined(__HAIKU__) && !defined(__midipix__)
37 #error "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h"
38 #endif
39
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
42 #else
43 #include <stdlib.h>
44 #endif
45
46 #include "mbedtls/net_sockets.h"
47 #include "mbedtls/error.h"
48
49 #include <string.h>
50
51 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
52 !defined(EFI32)
53
54 #define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
55
56 #if !defined(_WIN32_WINNT)
57 /* Enables getaddrinfo() & Co */
58 #define _WIN32_WINNT 0x0501
59 #endif
60
61 #include <ws2tcpip.h>
62
63 #include <winsock2.h>
64 #include <windows.h>
65 #if (_WIN32_WINNT < 0x0501)
66 #include <wspiapi.h>
67 #endif
68
69 #if defined(_MSC_VER)
70 #if defined(_WIN32_WCE)
71 #pragma comment( lib, "ws2.lib" )
72 #else
73 #pragma comment( lib, "ws2_32.lib" )
74 #endif
75 #endif /* _MSC_VER */
76
77 #define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
78 #define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
79 #define close(fd) closesocket(fd)
80
81 static int wsa_init_done = 0;
82
83 #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
84
85 #ifdef ESCP_MBDTLS_CUSTOMIZATION
86 #include "lwip/sockets.h"
87 #include "lwip/err.h"
88 #include "lwip/ip.h"
89 #include "lwip/tcpip.h"
90 #include "lwip/netif.h"
91 #include "lwip/dhcp.h"
92 #include "lwip/netdb.h"
93 #include <signal.h>
94 #define socklen_t u32_t
95
96 #else
97 #if defined(VENDOR_IOT_LWIP_C)
98 #include <lwip/sockets.h>
99 #include <lwip/netdb.h>
100 #else
101 #include <sys/types.h>
102 #include <sys/socket.h>
103 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #include <sys/time.h>
106 #include <unistd.h>
107 #include <fcntl.h>
108 #include <netdb.h>
109 #include <signal.h>
110 #include <errno.h>
111 #endif
112 #include <signal.h>
113 #include <fcntl.h>
114 #include <netdb.h>
115 #include <errno.h>
116 #endif
117
118 #if defined(WS_IOT_LWIP_C)
119 #include "lwip/sockets.h"
120 #endif
121
122 #define IS_EINTR( ret ) ( ( ret ) == EINTR )
123
124 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
125
126 /* Some MS functions want int and MSVC warns if we pass size_t,
127 * but the standard functions use socklen_t, so cast only for MSVC */
128 #if defined(_MSC_VER)
129 #define MSVC_INT_CAST (int)
130 #else
131 #define MSVC_INT_CAST
132 #endif
133
134 #include <stdio.h>
135
136 #include <time.h>
137
138 #include <stdint.h>
139
140 /*
141 * Prepare for using the sockets interface
142 */
net_prepare(void)143 static int net_prepare( void )
144 {
145 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
146 !defined(EFI32)
147 WSADATA wsaData;
148
149 if( wsa_init_done == 0 )
150 {
151 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
152 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
153
154 wsa_init_done = 1;
155 }
156 #else
157 #if !defined(EFIX64) && !defined(EFI32)
158 signal( SIGPIPE, SIG_IGN );
159 #endif
160 #endif
161 return( 0 );
162 }
163
164 /*
165 * Return 0 if the file descriptor is valid, an error otherwise.
166 * If for_select != 0, check whether the file descriptor is within the range
167 * allowed for fd_set used for the FD_xxx macros and the select() function.
168 */
check_fd(int fd,int for_select)169 static int check_fd( int fd, int for_select )
170 {
171 if( fd < 0 )
172 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
173
174 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
175 !defined(EFI32)
176 (void) for_select;
177 #else
178 /* A limitation of select() is that it only works with file descriptors
179 * that are strictly less than FD_SETSIZE. This is a limitation of the
180 * fd_set type. Error out early, because attempting to call FD_SET on a
181 * large file descriptor is a buffer overflow on typical platforms. */
182 if( for_select && fd >= FD_SETSIZE )
183 return( MBEDTLS_ERR_NET_POLL_FAILED );
184 #endif
185
186 return( 0 );
187 }
188
189 /*
190 * Initialize a context
191 */
mbedtls_net_init(mbedtls_net_context * ctx)192 void mbedtls_net_init( mbedtls_net_context *ctx )
193 {
194 ctx->fd = -1;
195 }
196
197 /*
198 * Initiate a TCP connection with host:port and the given protocol
199 */
200 #ifdef ESCP_MBDTLS_CUSTOMIZATION
mbedtls_net_connect_internel(mbedtls_net_context * ctx,const char * host,const char * port,int proto,int blockConnect)201 int mbedtls_net_connect_internel( mbedtls_net_context *ctx, const char *host,
202 const char *port, int proto, int blockConnect )
203 {
204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
205 struct addrinfo hints, *addr_list, *cur;
206 int flags;
207
208 if( ( ret = net_prepare() ) != 0 )
209 return( ret );
210
211 /* Do name resolution with both IPv6 and IPv4 */
212 memset( &hints, 0, sizeof( hints ) );
213 hints.ai_family = AF_UNSPEC;
214 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
215 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
216
217 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
218 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
219
220 /* Try the sockaddrs until a connection succeeds */
221 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
222 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
223 {
224 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
225 cur->ai_protocol );
226 if( ctx->fd < 0 )
227 {
228 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
229 continue;
230 }
231
232 if(FALSE == blockConnect) { //NONBLOCK
233 flags = fcntl(ctx->fd , F_GETFL, 0);
234 (VOS_VOID)fcntl(ctx->fd , F_SETFL, flags | O_NONBLOCK);
235 ret = connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen );
236 break;
237 } else {
238 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
239 {
240 ret = 0;
241 break;
242 }
243
244 close( ctx->fd );
245 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
246 }
247 }
248
249 freeaddrinfo( addr_list );
250
251 return( ret );
252 }
253
mbedtls_net_connect(mbedtls_net_context * ctx,const char * host,const char * port,int proto)254 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
255 const char *port, int proto )
256 {
257 return mbedtls_net_connect_internel(ctx, host, port, proto, 1);
258 }
259
260 #else
mbedtls_net_connect(mbedtls_net_context * ctx,const char * host,const char * port,int proto)261 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
262 const char *port, int proto )
263 {
264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
265 struct addrinfo hints, *addr_list, *cur;
266
267 if( ( ret = net_prepare() ) != 0 )
268 return( ret );
269
270 /* Do name resolution with both IPv6 and IPv4 */
271 memset( &hints, 0, sizeof( hints ) );
272 hints.ai_family = AF_UNSPEC;
273 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
274 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
275
276 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
277 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
278
279 /* Try the sockaddrs until a connection succeeds */
280 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
281 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
282 {
283 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
284 cur->ai_protocol );
285 if( ctx->fd < 0 )
286 {
287 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
288 continue;
289 }
290
291 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
292 {
293 ret = 0;
294 break;
295 }
296
297 #if defined(WS_IOT_LWIP_C)
298 lwip_close( ctx->fd );
299 #else
300 close( ctx->fd );
301 #endif
302 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
303 }
304
305 freeaddrinfo( addr_list );
306
307 return( ret );
308 }
309 #endif
310 /*
311 * Create a listening socket on bind_ip:port
312 */
mbedtls_net_bind(mbedtls_net_context * ctx,const char * bind_ip,const char * port,int proto)313 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
314 {
315 int n, ret;
316 struct addrinfo hints, *addr_list, *cur;
317
318 if( ( ret = net_prepare() ) != 0 )
319 return( ret );
320
321 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
322 memset( &hints, 0, sizeof( hints ) );
323 hints.ai_family = AF_UNSPEC;
324 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
325 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
326 if( bind_ip == NULL )
327 hints.ai_flags = AI_PASSIVE;
328
329 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
330 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
331
332 /* Try the sockaddrs until a binding succeeds */
333 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
334 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
335 {
336 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
337 cur->ai_protocol );
338 if( ctx->fd < 0 )
339 {
340 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
341 continue;
342 }
343
344 n = 1;
345 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
346 (const char *) &n, sizeof( n ) ) != 0 )
347 {
348 close( ctx->fd );
349 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
350 continue;
351 }
352
353 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
354 {
355 close( ctx->fd );
356 ret = MBEDTLS_ERR_NET_BIND_FAILED;
357 continue;
358 }
359
360 /* Listen only makes sense for TCP */
361 if( proto == MBEDTLS_NET_PROTO_TCP )
362 {
363 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
364 {
365 close( ctx->fd );
366 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
367 continue;
368 }
369 }
370
371 /* Bind was successful */
372 ret = 0;
373 break;
374 }
375
376 freeaddrinfo( addr_list );
377
378 return( ret );
379
380 }
381
382 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
383 !defined(EFI32)
384 /*
385 * Check if the requested operation would be blocking on a non-blocking socket
386 * and thus 'failed' with a negative return value.
387 */
net_would_block(const mbedtls_net_context * ctx)388 static int net_would_block( const mbedtls_net_context *ctx )
389 {
390 ((void) ctx);
391 return( WSAGetLastError() == WSAEWOULDBLOCK );
392 }
393 #else
394 /*
395 * Check if the requested operation would be blocking on a non-blocking socket
396 * and thus 'failed' with a negative return value.
397 *
398 * Note: on a blocking socket this function always returns 0!
399 */
net_would_block(const mbedtls_net_context * ctx)400 static int net_would_block( const mbedtls_net_context *ctx )
401 {
402 int err = errno;
403
404 /*
405 * Never return 'WOULD BLOCK' on a blocking socket
406 */
407 #if defined(VENDOR_IOT_LWIP_C)
408 if( ( fcntl( ctx->fd, F_GETFL, 0 ) & O_NONBLOCK ) != O_NONBLOCK )
409 #else
410 #ifdef ESCP_MBDTLS_CUSTOMIZATION
411 if( ( fcntl( ctx->fd, F_GETFL, 0 ) & O_NONBLOCK ) != O_NONBLOCK )
412 #else
413 #if defined(WS_IOT_LWIP_C)
414 if( (lwip_fcntl( ctx->fd, F_GETFL, 0 ) & O_NONBLOCK ) != O_NONBLOCK )
415 #else
416 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
417 #endif
418 #endif
419 #endif
420 {
421 errno = err;
422 return( 0 );
423 }
424
425 switch( errno = err )
426 {
427 #if defined EAGAIN
428 case EAGAIN:
429 #endif
430 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
431 case EWOULDBLOCK:
432 #endif
433 return( 1 );
434 }
435 return( 0 );
436 }
437 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
438
439 /*
440 * Accept a connection from a remote client
441 */
mbedtls_net_accept(mbedtls_net_context * bind_ctx,mbedtls_net_context * client_ctx,void * client_ip,size_t buf_size,size_t * ip_len)442 int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
443 mbedtls_net_context *client_ctx,
444 void *client_ip, size_t buf_size, size_t *ip_len )
445 {
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447 int type;
448
449 struct sockaddr_storage client_addr;
450
451 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
452 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
453 defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
454 socklen_t n = (socklen_t) sizeof( client_addr );
455 socklen_t type_len = (socklen_t) sizeof( type );
456 #else
457 int n = (int) sizeof( client_addr );
458 int type_len = (int) sizeof( type );
459 #endif
460
461 /* Is this a TCP or UDP socket? */
462 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
463 (void *) &type, &type_len ) != 0 ||
464 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
465 {
466 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
467 }
468
469 if( type == SOCK_STREAM )
470 {
471 /* TCP: actual accept() */
472 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
473 (struct sockaddr *) &client_addr, &n );
474 }
475 else
476 {
477 /* UDP: wait for a message, but keep it in the queue */
478 char buf[1] = { 0 };
479
480 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
481 (struct sockaddr *) &client_addr, &n );
482
483 #if defined(_WIN32)
484 if( ret == SOCKET_ERROR &&
485 WSAGetLastError() == WSAEMSGSIZE )
486 {
487 /* We know buf is too small, thanks, just peeking here */
488 ret = 0;
489 }
490 #endif
491 }
492
493 if( ret < 0 )
494 {
495 if( net_would_block( bind_ctx ) != 0 )
496 return( MBEDTLS_ERR_SSL_WANT_READ );
497
498 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
499 }
500
501 /* UDP: hijack the listening socket to communicate with the client,
502 * then bind a new socket to accept new connections */
503 if( type != SOCK_STREAM )
504 {
505 struct sockaddr_storage local_addr;
506 int one = 1;
507
508 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
509 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
510
511 client_ctx->fd = bind_ctx->fd;
512 bind_ctx->fd = -1; /* In case we exit early */
513
514 n = sizeof( struct sockaddr_storage );
515 if( getsockname( client_ctx->fd,
516 (struct sockaddr *) &local_addr, &n ) != 0 ||
517 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
518 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
519 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
520 (const char *) &one, sizeof( one ) ) != 0 )
521 {
522 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
523 }
524
525 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
526 {
527 return( MBEDTLS_ERR_NET_BIND_FAILED );
528 }
529 }
530
531 if( client_ip != NULL )
532 {
533 if( client_addr.ss_family == AF_INET )
534 {
535 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
536 *ip_len = sizeof( addr4->sin_addr.s_addr );
537
538 if( buf_size < *ip_len )
539 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
540
541 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
542 }
543 #ifndef ESCP_MBDTLS_CUSTOMIZATION
544 else
545 {
546 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
547 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
548
549 if( buf_size < *ip_len )
550 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
551
552 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
553 }
554 #endif
555 }
556
557 return( 0 );
558 }
559
560 /*
561 * Set the socket blocking or non-blocking
562 */
mbedtls_net_set_block(mbedtls_net_context * ctx)563 int mbedtls_net_set_block( mbedtls_net_context *ctx )
564 {
565 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
566 !defined(EFI32)
567 u_long n = 0;
568 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
569 #else
570 #if defined(VENDOR_IOT_LWIP_C)
571 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) & ~O_NONBLOCK ) );
572 #else
573 #ifdef ESCP_MBDTLS_CUSTOMIZATION
574 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) & ~O_NONBLOCK ) );
575 #else
576 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
577 #endif
578 #endif
579 #endif
580 }
581
mbedtls_net_set_nonblock(mbedtls_net_context * ctx)582 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
583 {
584 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
585 !defined(EFI32)
586 u_long n = 1;
587 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
588 #else
589 #if defined(VENDOR_IOT_LWIP_C)
590 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) | O_NONBLOCK ) );
591 #else
592 #ifdef ESCP_MBDTLS_CUSTOMIZATION
593 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) | O_NONBLOCK ) );
594 #else
595 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
596 #endif
597 #endif
598 #endif
599 }
600
601 /*
602 * Check if data is available on the socket
603 */
604
mbedtls_net_poll(mbedtls_net_context * ctx,uint32_t rw,uint32_t timeout)605 int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
606 {
607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
608 struct timeval tv;
609
610 fd_set read_fds;
611 fd_set write_fds;
612
613 int fd = ctx->fd;
614
615 ret = check_fd( fd, 1 );
616 if( ret != 0 )
617 return( ret );
618
619 #if defined(__has_feature)
620 #if __has_feature(memory_sanitizer)
621 /* Ensure that memory sanitizers consider read_fds and write_fds as
622 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
623 * is implemented in assembly. */
624 memset( &read_fds, 0, sizeof( read_fds ) );
625 memset( &write_fds, 0, sizeof( write_fds ) );
626 #endif
627 #endif
628
629 FD_ZERO( &read_fds );
630 if( rw & MBEDTLS_NET_POLL_READ )
631 {
632 rw &= ~MBEDTLS_NET_POLL_READ;
633 FD_SET( fd, &read_fds );
634 }
635
636 FD_ZERO( &write_fds );
637 if( rw & MBEDTLS_NET_POLL_WRITE )
638 {
639 rw &= ~MBEDTLS_NET_POLL_WRITE;
640 FD_SET( fd, &write_fds );
641 }
642
643 if( rw != 0 )
644 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
645
646 tv.tv_sec = timeout / 1000;
647 tv.tv_usec = ( timeout % 1000 ) * 1000;
648
649 do
650 {
651 ret = select( fd + 1, &read_fds, &write_fds, NULL,
652 timeout == (uint32_t) -1 ? NULL : &tv );
653 }
654 while( IS_EINTR( ret ) );
655
656 if( ret < 0 )
657 return( MBEDTLS_ERR_NET_POLL_FAILED );
658
659 ret = 0;
660 if( FD_ISSET( fd, &read_fds ) )
661 ret |= MBEDTLS_NET_POLL_READ;
662 if( FD_ISSET( fd, &write_fds ) )
663 ret |= MBEDTLS_NET_POLL_WRITE;
664
665 return( ret );
666 }
667
668 /*
669 * Portable usleep helper
670 */
mbedtls_net_usleep(unsigned long usec)671 void mbedtls_net_usleep( unsigned long usec )
672 {
673 #if defined(_WIN32)
674 Sleep( ( usec + 999 ) / 1000 );
675 #else
676 struct timeval tv;
677 tv.tv_sec = usec / 1000000;
678 #if defined(__unix__) || defined(__unix) || \
679 ( defined(__APPLE__) && defined(__MACH__) )
680 tv.tv_usec = (suseconds_t) usec % 1000000;
681 #else
682 tv.tv_usec = usec % 1000000;
683 #endif
684 select( 0, NULL, NULL, NULL, &tv );
685 #endif
686 }
687
688 /*
689 * Read at most 'len' characters
690 */
mbedtls_net_recv(void * ctx,unsigned char * buf,size_t len)691 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
692 {
693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
694 int fd = ((mbedtls_net_context *) ctx)->fd;
695
696 ret = check_fd( fd, 0 );
697 if( ret != 0 )
698 return( ret );
699
700 #if defined(WS_IOT_LWIP_C)
701 ret = (int) lwip_read( fd, buf, len );
702 #else
703 ret = (int) read( fd, buf, len );
704 #endif
705
706 if( ret < 0 )
707 {
708 if( net_would_block( ctx ) != 0 )
709 return( MBEDTLS_ERR_SSL_WANT_READ );
710
711 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
712 !defined(EFI32)
713 if( WSAGetLastError() == WSAECONNRESET )
714 return( MBEDTLS_ERR_NET_CONN_RESET );
715 #else
716 if( errno == EPIPE || errno == ECONNRESET )
717 return( MBEDTLS_ERR_NET_CONN_RESET );
718
719 if( errno == EINTR )
720 return( MBEDTLS_ERR_SSL_WANT_READ );
721 #endif
722
723 return( MBEDTLS_ERR_NET_RECV_FAILED );
724 }
725
726 return( ret );
727 }
728
729 /*
730 * Read at most 'len' characters, blocking for at most 'timeout' ms
731 */
mbedtls_net_recv_timeout(void * ctx,unsigned char * buf,size_t len,uint32_t timeout)732 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
733 size_t len, uint32_t timeout )
734 {
735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
736 struct timeval tv;
737 fd_set read_fds;
738 int fd = ((mbedtls_net_context *) ctx)->fd;
739
740 ret = check_fd( fd, 1 );
741 if( ret != 0 )
742 return( ret );
743
744 FD_ZERO( &read_fds );
745 FD_SET( fd, &read_fds );
746
747 tv.tv_sec = timeout / 1000;
748 tv.tv_usec = ( timeout % 1000 ) * 1000;
749 #ifndef WS_IOT_LWIP_C
750 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
751 #else
752 ret = lwip_select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
753 #endif
754 /* Zero fds ready means we timed out */
755 if( ret == 0 )
756 return( MBEDTLS_ERR_SSL_TIMEOUT );
757
758 if( ret < 0 )
759 {
760 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
761 !defined(EFI32)
762 if( WSAGetLastError() == WSAEINTR )
763 return( MBEDTLS_ERR_SSL_WANT_READ );
764 #else
765 if( errno == EINTR )
766 return( MBEDTLS_ERR_SSL_WANT_READ );
767 #endif
768
769 return( MBEDTLS_ERR_NET_RECV_FAILED );
770 }
771
772 /* This call will not block */
773 return( mbedtls_net_recv( ctx, buf, len ) );
774 }
775
776 /*
777 * Write at most 'len' characters
778 */
mbedtls_net_send(void * ctx,const unsigned char * buf,size_t len)779 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
780 {
781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
782 int fd = ((mbedtls_net_context *) ctx)->fd;
783
784 ret = check_fd( fd, 0 );
785 if( ret != 0 )
786 return( ret );
787
788 #if defined(WS_IOT_LWIP_C)
789 ret = (int) lwip_write( fd, buf, len );
790 #else
791 ret = (int) write( fd, buf, len );
792 #endif
793
794 if( ret < 0 )
795 {
796 if( net_would_block( ctx ) != 0 )
797 return( MBEDTLS_ERR_SSL_WANT_WRITE );
798
799 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
800 !defined(EFI32)
801 if( WSAGetLastError() == WSAECONNRESET )
802 return( MBEDTLS_ERR_NET_CONN_RESET );
803 #else
804 if( errno == EPIPE || errno == ECONNRESET )
805 return( MBEDTLS_ERR_NET_CONN_RESET );
806
807 if( errno == EINTR )
808 return( MBEDTLS_ERR_SSL_WANT_WRITE );
809 #endif
810
811 return( MBEDTLS_ERR_NET_SEND_FAILED );
812 }
813
814 return( ret );
815 }
816
817 /*
818 * Close the connection
819 */
mbedtls_net_close(mbedtls_net_context * ctx)820 void mbedtls_net_close( mbedtls_net_context *ctx )
821 {
822 if( ctx->fd == -1 )
823 return;
824
825 #if defined(WS_IOT_LWIP_C)
826 lwip_close( ctx->fd );
827 #else
828 close( ctx->fd );
829 #endif
830
831 ctx->fd = -1;
832 }
833
834 /*
835 * Gracefully close the connection
836 */
mbedtls_net_free(mbedtls_net_context * ctx)837 void mbedtls_net_free( mbedtls_net_context *ctx )
838 {
839 if( ctx->fd == -1 )
840 return;
841
842 shutdown( ctx->fd, 2 );
843 #if defined(WS_IOT_LWIP_C)
844 lwip_close( ctx->fd );
845 #else
846 close( ctx->fd );
847 #endif
848
849 ctx->fd = -1;
850 }
851
852 #endif /* MBEDTLS_NET_C */
853