• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/types.h>
86 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <sys/time.h>
90 #include <unistd.h>
91 #include <signal.h>
92 #include <fcntl.h>
93 #include <netdb.h>
94 #include <errno.h>
95 
96 #define IS_EINTR( ret ) ( ( ret ) == EINTR )
97 
98 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
99 
100 /* Some MS functions want int and MSVC warns if we pass size_t,
101  * but the standard functions use socklen_t, so cast only for MSVC */
102 #if defined(_MSC_VER)
103 #define MSVC_INT_CAST   (int)
104 #else
105 #define MSVC_INT_CAST
106 #endif
107 
108 #include <stdio.h>
109 
110 #include <time.h>
111 
112 #include <stdint.h>
113 
114 #ifdef LITEOS_VERSION
115 #include "lwip/sockets.h"
116 #define close(fd) lwip_close(fd)
117 #define shutdown(fd, how) lwip_shutdown(fd, how)
118 #endif
119 
120 /*
121  * Prepare for using the sockets interface
122  */
net_prepare(void)123 static int net_prepare( void )
124 {
125 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
126     !defined(EFI32)
127     WSADATA wsaData;
128 
129     if( wsa_init_done == 0 )
130     {
131         if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
132             return( MBEDTLS_ERR_NET_SOCKET_FAILED );
133 
134         wsa_init_done = 1;
135     }
136 #else
137 #if !defined(EFIX64) && !defined(EFI32)
138     signal( SIGPIPE, SIG_IGN );
139 #endif
140 #endif
141     return( 0 );
142 }
143 
144 /*
145  * Return 0 if the file descriptor is valid, an error otherwise.
146  * If for_select != 0, check whether the file descriptor is within the range
147  * allowed for fd_set used for the FD_xxx macros and the select() function.
148  */
check_fd(int fd,int for_select)149 static int check_fd( int fd, int for_select )
150 {
151     if( fd < 0 )
152         return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
153 
154 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
155     !defined(EFI32)
156     (void) for_select;
157 #else
158     /* A limitation of select() is that it only works with file descriptors
159      * that are strictly less than FD_SETSIZE. This is a limitation of the
160      * fd_set type. Error out early, because attempting to call FD_SET on a
161      * large file descriptor is a buffer overflow on typical platforms. */
162     if( for_select && fd >= FD_SETSIZE )
163         return( MBEDTLS_ERR_NET_POLL_FAILED );
164 #endif
165 
166     return( 0 );
167 }
168 
169 /*
170  * Initialize a context
171  */
mbedtls_net_init(mbedtls_net_context * ctx)172 void mbedtls_net_init( mbedtls_net_context *ctx )
173 {
174     ctx->fd = -1;
175 }
176 
177 /*
178  * Initiate a TCP connection with host:port and the given protocol
179  */
mbedtls_net_connect(mbedtls_net_context * ctx,const char * host,const char * port,int proto)180 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
181                          const char *port, int proto )
182 {
183     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
184     struct addrinfo hints, *addr_list, *cur;
185 
186     if( ( ret = net_prepare() ) != 0 )
187         return( ret );
188 
189     /* Do name resolution with both IPv6 and IPv4 */
190     memset( &hints, 0, sizeof( hints ) );
191     hints.ai_family = AF_UNSPEC;
192     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
193     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
194 
195     if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
196         return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
197 
198     /* Try the sockaddrs until a connection succeeds */
199     ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
200     for( cur = addr_list; cur != NULL; cur = cur->ai_next )
201     {
202 #ifdef LITEOS_VERSION
203         if (cur->ai_family != AF_INET || cur->ai_socktype != SOCK_STREAM) {
204             continue;
205         }
206         ctx->fd = (int) socket(AF_INET, SOCK_STREAM, 0);
207 #else
208         ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
209                             cur->ai_protocol );
210 #endif
211         if( ctx->fd < 0 )
212         {
213             ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
214             continue;
215         }
216 
217         if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
218         {
219             ret = 0;
220             break;
221         }
222 
223         close( ctx->fd );
224         ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
225     }
226 
227     freeaddrinfo( addr_list );
228 
229     return( ret );
230 }
231 
232 /*
233  * Create a listening socket on bind_ip:port
234  */
mbedtls_net_bind(mbedtls_net_context * ctx,const char * bind_ip,const char * port,int proto)235 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
236 {
237     int n, ret;
238     struct addrinfo hints, *addr_list, *cur;
239 
240     if( ( ret = net_prepare() ) != 0 )
241         return( ret );
242 
243     /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
244     memset( &hints, 0, sizeof( hints ) );
245     hints.ai_family = AF_UNSPEC;
246     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
247     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
248     if( bind_ip == NULL )
249         hints.ai_flags = AI_PASSIVE;
250 
251     if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
252         return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
253 
254     /* Try the sockaddrs until a binding succeeds */
255     ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
256     for( cur = addr_list; cur != NULL; cur = cur->ai_next )
257     {
258         ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
259                             cur->ai_protocol );
260         if( ctx->fd < 0 )
261         {
262             ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
263             continue;
264         }
265 
266         n = 1;
267         if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
268                         (const char *) &n, sizeof( n ) ) != 0 )
269         {
270             close( ctx->fd );
271             ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
272             continue;
273         }
274 
275         if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
276         {
277             close( ctx->fd );
278             ret = MBEDTLS_ERR_NET_BIND_FAILED;
279             continue;
280         }
281 
282         /* Listen only makes sense for TCP */
283         if( proto == MBEDTLS_NET_PROTO_TCP )
284         {
285             if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
286             {
287                 close( ctx->fd );
288                 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
289                 continue;
290             }
291         }
292 
293         /* Bind was successful */
294         ret = 0;
295         break;
296     }
297 
298     freeaddrinfo( addr_list );
299 
300     return( ret );
301 
302 }
303 
304 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
305     !defined(EFI32)
306 /*
307  * Check if the requested operation would be blocking on a non-blocking socket
308  * and thus 'failed' with a negative return value.
309  */
net_would_block(const mbedtls_net_context * ctx)310 static int net_would_block( const mbedtls_net_context *ctx )
311 {
312     ((void) ctx);
313     return( WSAGetLastError() == WSAEWOULDBLOCK );
314 }
315 #else
316 /*
317  * Check if the requested operation would be blocking on a non-blocking socket
318  * and thus 'failed' with a negative return value.
319  *
320  * Note: on a blocking socket this function always returns 0!
321  */
net_would_block(const mbedtls_net_context * ctx)322 static int net_would_block( const mbedtls_net_context *ctx )
323 {
324     int err = errno;
325 
326     /*
327      * Never return 'WOULD BLOCK' on a blocking socket
328      */
329     if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
330     {
331         errno = err;
332         return( 0 );
333     }
334 
335     switch( errno = err )
336     {
337 #if defined EAGAIN
338         case EAGAIN:
339 #endif
340 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
341         case EWOULDBLOCK:
342 #endif
343             return( 1 );
344     }
345     return( 0 );
346 }
347 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
348 
349 /*
350  * Accept a connection from a remote client
351  */
mbedtls_net_accept(mbedtls_net_context * bind_ctx,mbedtls_net_context * client_ctx,void * client_ip,size_t buf_size,size_t * ip_len)352 int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
353                         mbedtls_net_context *client_ctx,
354                         void *client_ip, size_t buf_size, size_t *ip_len )
355 {
356     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
357     int type;
358 
359     struct sockaddr_storage client_addr;
360 
361 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) ||  \
362     defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
363     defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
364     socklen_t n = (socklen_t) sizeof( client_addr );
365     socklen_t type_len = (socklen_t) sizeof( type );
366 #else
367     int n = (int) sizeof( client_addr );
368     int type_len = (int) sizeof( type );
369 #endif
370 
371     /* Is this a TCP or UDP socket? */
372     if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
373                     (void *) &type, &type_len ) != 0 ||
374         ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
375     {
376         return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
377     }
378 
379     if( type == SOCK_STREAM )
380     {
381         /* TCP: actual accept() */
382         ret = client_ctx->fd = (int) accept( bind_ctx->fd,
383                                              (struct sockaddr *) &client_addr, &n );
384     }
385     else
386     {
387         /* UDP: wait for a message, but keep it in the queue */
388         char buf[1] = { 0 };
389 
390         ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
391                         (struct sockaddr *) &client_addr, &n );
392 
393 #if defined(_WIN32)
394         if( ret == SOCKET_ERROR &&
395             WSAGetLastError() == WSAEMSGSIZE )
396         {
397             /* We know buf is too small, thanks, just peeking here */
398             ret = 0;
399         }
400 #endif
401     }
402 
403     if( ret < 0 )
404     {
405         if( net_would_block( bind_ctx ) != 0 )
406             return( MBEDTLS_ERR_SSL_WANT_READ );
407 
408         return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
409     }
410 
411     /* UDP: hijack the listening socket to communicate with the client,
412      * then bind a new socket to accept new connections */
413     if( type != SOCK_STREAM )
414     {
415         struct sockaddr_storage local_addr;
416         int one = 1;
417 
418         if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
419             return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
420 
421         client_ctx->fd = bind_ctx->fd;
422         bind_ctx->fd   = -1; /* In case we exit early */
423 
424         n = sizeof( struct sockaddr_storage );
425         if( getsockname( client_ctx->fd,
426                          (struct sockaddr *) &local_addr, &n ) != 0 ||
427             ( bind_ctx->fd = (int) socket( local_addr.ss_family,
428                                            SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
429             setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
430                         (const char *) &one, sizeof( one ) ) != 0 )
431         {
432             return( MBEDTLS_ERR_NET_SOCKET_FAILED );
433         }
434 
435         if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
436         {
437             return( MBEDTLS_ERR_NET_BIND_FAILED );
438         }
439     }
440 
441     if( client_ip != NULL )
442     {
443         if( client_addr.ss_family == AF_INET )
444         {
445             struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
446             *ip_len = sizeof( addr4->sin_addr.s_addr );
447 
448             if( buf_size < *ip_len )
449                 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
450 
451             memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
452         }
453         else
454         {
455             struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
456             *ip_len = sizeof( addr6->sin6_addr.s6_addr );
457 
458             if( buf_size < *ip_len )
459                 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
460 
461             memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
462         }
463     }
464 
465     return( 0 );
466 }
467 
468 /*
469  * Set the socket blocking or non-blocking
470  */
mbedtls_net_set_block(mbedtls_net_context * ctx)471 int mbedtls_net_set_block( mbedtls_net_context *ctx )
472 {
473 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
474     !defined(EFI32)
475     u_long n = 0;
476     return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
477 #else
478     return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
479 #endif
480 }
481 
mbedtls_net_set_nonblock(mbedtls_net_context * ctx)482 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
483 {
484 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
485     !defined(EFI32)
486     u_long n = 1;
487     return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
488 #else
489     return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
490 #endif
491 }
492 
493 /*
494  * Check if data is available on the socket
495  */
496 
mbedtls_net_poll(mbedtls_net_context * ctx,uint32_t rw,uint32_t timeout)497 int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
498 {
499     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
500     struct timeval tv;
501 
502     fd_set read_fds;
503     fd_set write_fds;
504 
505     int fd = ctx->fd;
506 
507     ret = check_fd( fd, 1 );
508     if( ret != 0 )
509         return( ret );
510 
511 #if defined(__has_feature)
512 #if __has_feature(memory_sanitizer)
513     /* Ensure that memory sanitizers consider read_fds and write_fds as
514      * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
515      * is implemented in assembly. */
516     memset( &read_fds, 0, sizeof( read_fds ) );
517     memset( &write_fds, 0, sizeof( write_fds ) );
518 #endif
519 #endif
520 
521     FD_ZERO( &read_fds );
522     if( rw & MBEDTLS_NET_POLL_READ )
523     {
524         rw &= ~MBEDTLS_NET_POLL_READ;
525         FD_SET( fd, &read_fds );
526     }
527 
528     FD_ZERO( &write_fds );
529     if( rw & MBEDTLS_NET_POLL_WRITE )
530     {
531         rw &= ~MBEDTLS_NET_POLL_WRITE;
532         FD_SET( fd, &write_fds );
533     }
534 
535     if( rw != 0 )
536         return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
537 
538     tv.tv_sec  = timeout / 1000;
539     tv.tv_usec = ( timeout % 1000 ) * 1000;
540 
541     do
542     {
543         ret = select( fd + 1, &read_fds, &write_fds, NULL,
544                       timeout == (uint32_t) -1 ? NULL : &tv );
545     }
546     while( IS_EINTR( ret ) );
547 
548     if( ret < 0 )
549         return( MBEDTLS_ERR_NET_POLL_FAILED );
550 
551     ret = 0;
552     if( FD_ISSET( fd, &read_fds ) )
553         ret |= MBEDTLS_NET_POLL_READ;
554     if( FD_ISSET( fd, &write_fds ) )
555         ret |= MBEDTLS_NET_POLL_WRITE;
556 
557     return( ret );
558 }
559 
560 /*
561  * Portable usleep helper
562  */
mbedtls_net_usleep(unsigned long usec)563 void mbedtls_net_usleep( unsigned long usec )
564 {
565 #if defined(_WIN32)
566     Sleep( ( usec + 999 ) / 1000 );
567 #else
568     struct timeval tv;
569     tv.tv_sec  = usec / 1000000;
570 #if defined(__unix__) || defined(__unix) || \
571     ( defined(__APPLE__) && defined(__MACH__) )
572     tv.tv_usec = (suseconds_t) usec % 1000000;
573 #else
574     tv.tv_usec = usec % 1000000;
575 #endif
576     select( 0, NULL, NULL, NULL, &tv );
577 #endif
578 }
579 
580 /*
581  * Read at most 'len' characters
582  */
mbedtls_net_recv(void * ctx,unsigned char * buf,size_t len)583 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
584 {
585     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586     int fd = ((mbedtls_net_context *) ctx)->fd;
587 
588     ret = check_fd( fd, 0 );
589     if( ret != 0 )
590         return( ret );
591 #ifdef LITEOS_VERSION
592     ret = (int) recv( fd, buf, len, 0);
593 #else
594 
595     ret = (int) read( fd, buf, len );
596 #endif
597 
598     if( ret < 0 )
599     {
600         if( net_would_block( ctx ) != 0 )
601             return( MBEDTLS_ERR_SSL_WANT_READ );
602 
603 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
604     !defined(EFI32)
605         if( WSAGetLastError() == WSAECONNRESET )
606             return( MBEDTLS_ERR_NET_CONN_RESET );
607 #else
608         if( errno == EPIPE || errno == ECONNRESET )
609             return( MBEDTLS_ERR_NET_CONN_RESET );
610 
611         if( errno == EINTR )
612             return( MBEDTLS_ERR_SSL_WANT_READ );
613 #endif
614 
615         return( MBEDTLS_ERR_NET_RECV_FAILED );
616     }
617 
618     return( ret );
619 }
620 
621 /*
622  * Read at most 'len' characters, blocking for at most 'timeout' ms
623  */
mbedtls_net_recv_timeout(void * ctx,unsigned char * buf,size_t len,uint32_t timeout)624 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
625                               size_t len, uint32_t timeout )
626 {
627     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
628     struct timeval tv;
629     fd_set read_fds;
630     int fd = ((mbedtls_net_context *) ctx)->fd;
631 
632     ret = check_fd( fd, 1 );
633     if( ret != 0 )
634         return( ret );
635 
636     FD_ZERO( &read_fds );
637     FD_SET( fd, &read_fds );
638 
639     tv.tv_sec  = timeout / 1000;
640     tv.tv_usec = ( timeout % 1000 ) * 1000;
641 
642     ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
643 
644     /* Zero fds ready means we timed out */
645     if( ret == 0 )
646         return( MBEDTLS_ERR_SSL_TIMEOUT );
647 
648     if( ret < 0 )
649     {
650 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
651     !defined(EFI32)
652         if( WSAGetLastError() == WSAEINTR )
653             return( MBEDTLS_ERR_SSL_WANT_READ );
654 #else
655         if( errno == EINTR )
656             return( MBEDTLS_ERR_SSL_WANT_READ );
657 #endif
658 
659         return( MBEDTLS_ERR_NET_RECV_FAILED );
660     }
661 
662     /* This call will not block */
663     return( mbedtls_net_recv( ctx, buf, len ) );
664 }
665 
666 /*
667  * Write at most 'len' characters
668  */
mbedtls_net_send(void * ctx,const unsigned char * buf,size_t len)669 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
670 {
671     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
672     int fd = ((mbedtls_net_context *) ctx)->fd;
673 
674     ret = check_fd( fd, 0 );
675     if( ret != 0 )
676         return( ret );
677 
678 #ifdef LITEOS_VERSION
679     ret = (int) send( fd, buf, len, 0);
680 #else
681 
682     ret = (int) write( fd, buf, len );
683 #endif
684 
685     if( ret < 0 )
686     {
687         if( net_would_block( ctx ) != 0 )
688             return( MBEDTLS_ERR_SSL_WANT_WRITE );
689 
690 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
691     !defined(EFI32)
692         if( WSAGetLastError() == WSAECONNRESET )
693             return( MBEDTLS_ERR_NET_CONN_RESET );
694 #else
695         if( errno == EPIPE || errno == ECONNRESET )
696             return( MBEDTLS_ERR_NET_CONN_RESET );
697 
698         if( errno == EINTR )
699             return( MBEDTLS_ERR_SSL_WANT_WRITE );
700 #endif
701 
702         return( MBEDTLS_ERR_NET_SEND_FAILED );
703     }
704 
705     return( ret );
706 }
707 
708 /*
709  * Close the connection
710  */
mbedtls_net_close(mbedtls_net_context * ctx)711 void mbedtls_net_close( mbedtls_net_context *ctx )
712 {
713     if( ctx->fd == -1 )
714         return;
715 
716     close( ctx->fd );
717 
718     ctx->fd = -1;
719 }
720 
721 /*
722  * Gracefully close the connection
723  */
mbedtls_net_free(mbedtls_net_context * ctx)724 void mbedtls_net_free( mbedtls_net_context *ctx )
725 {
726     if( ctx->fd == -1 )
727         return;
728 
729     shutdown( ctx->fd, 2 );
730     close( ctx->fd );
731 
732     ctx->fd = -1;
733 }
734 
735 #endif /* MBEDTLS_NET_C */
736