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