net_sockets.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. /* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
  20. * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
  21. * Harmless on other platforms. */
  22. #ifndef _POSIX_C_SOURCE
  23. #define _POSIX_C_SOURCE 200112L
  24. #endif
  25. #ifndef _XOPEN_SOURCE
  26. #define _XOPEN_SOURCE 600 /* sockaddr_storage */
  27. #endif
  28. #include "common.h"
  29. #if defined(MBEDTLS_NET_C)
  30. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  31. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  32. !defined(__HAIKU__) && !defined(__midipix__)
  33. #error "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h"
  34. #endif
  35. #include "mbedtls/platform.h"
  36. #include "mbedtls/net_sockets.h"
  37. #include "mbedtls/error.h"
  38. #include <string.h>
  39. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  40. !defined(EFI32)
  41. #define IS_EINTR(ret) ((ret) == WSAEINTR)
  42. #if !defined(_WIN32_WINNT)
  43. /* Enables getaddrinfo() & Co */
  44. #define _WIN32_WINNT 0x0501
  45. #endif
  46. #include <ws2tcpip.h>
  47. #include <winsock2.h>
  48. #include <windows.h>
  49. #if (_WIN32_WINNT < 0x0501)
  50. #include <wspiapi.h>
  51. #endif
  52. #if defined(_MSC_VER)
  53. #if defined(_WIN32_WCE)
  54. #pragma comment( lib, "ws2.lib" )
  55. #else
  56. #pragma comment( lib, "ws2_32.lib" )
  57. #endif
  58. #endif /* _MSC_VER */
  59. #define read(fd, buf, len) recv(fd, (char *) (buf), (int) (len), 0)
  60. #define write(fd, buf, len) send(fd, (char *) (buf), (int) (len), 0)
  61. #define close(fd) closesocket(fd)
  62. static int wsa_init_done = 0;
  63. #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  64. #include <sys/types.h>
  65. #include <sys/socket.h>
  66. #include <netinet/in.h>
  67. #include <arpa/inet.h>
  68. #include <sys/time.h>
  69. #include <unistd.h>
  70. #include <signal.h>
  71. #include <fcntl.h>
  72. #include <netdb.h>
  73. #include <errno.h>
  74. #define IS_EINTR(ret) ((ret) == EINTR)
  75. #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  76. /* Some MS functions want int and MSVC warns if we pass size_t,
  77. * but the standard functions use socklen_t, so cast only for MSVC */
  78. #if defined(_MSC_VER)
  79. #define MSVC_INT_CAST (int)
  80. #else
  81. #define MSVC_INT_CAST
  82. #endif
  83. #include <stdio.h>
  84. #if defined(MBEDTLS_HAVE_TIME)
  85. #include <time.h>
  86. #endif
  87. #include <stdint.h>
  88. /*
  89. * Prepare for using the sockets interface
  90. */
  91. static int net_prepare(void)
  92. {
  93. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  94. !defined(EFI32)
  95. WSADATA wsaData;
  96. if (wsa_init_done == 0) {
  97. if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
  98. return MBEDTLS_ERR_NET_SOCKET_FAILED;
  99. }
  100. wsa_init_done = 1;
  101. }
  102. #else
  103. #if !defined(EFIX64) && !defined(EFI32)
  104. signal(SIGPIPE, SIG_IGN);
  105. #endif
  106. #endif
  107. return 0;
  108. }
  109. /*
  110. * Return 0 if the file descriptor is valid, an error otherwise.
  111. * If for_select != 0, check whether the file descriptor is within the range
  112. * allowed for fd_set used for the FD_xxx macros and the select() function.
  113. */
  114. static int check_fd(int fd, int for_select)
  115. {
  116. if (fd < 0) {
  117. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  118. }
  119. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  120. !defined(EFI32)
  121. (void) for_select;
  122. #else
  123. /* A limitation of select() is that it only works with file descriptors
  124. * that are strictly less than FD_SETSIZE. This is a limitation of the
  125. * fd_set type. Error out early, because attempting to call FD_SET on a
  126. * large file descriptor is a buffer overflow on typical platforms. */
  127. if (for_select && fd >= FD_SETSIZE) {
  128. return MBEDTLS_ERR_NET_POLL_FAILED;
  129. }
  130. #endif
  131. return 0;
  132. }
  133. /*
  134. * Initialize a context
  135. */
  136. void mbedtls_net_init(mbedtls_net_context *ctx)
  137. {
  138. ctx->fd = -1;
  139. }
  140. /*
  141. * Initiate a TCP connection with host:port and the given protocol
  142. */
  143. int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
  144. const char *port, int proto)
  145. {
  146. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  147. struct addrinfo hints, *addr_list, *cur;
  148. if ((ret = net_prepare()) != 0) {
  149. return ret;
  150. }
  151. /* Do name resolution with both IPv6 and IPv4 */
  152. memset(&hints, 0, sizeof(hints));
  153. hints.ai_family = AF_UNSPEC;
  154. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  155. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  156. if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
  157. return MBEDTLS_ERR_NET_UNKNOWN_HOST;
  158. }
  159. /* Try the sockaddrs until a connection succeeds */
  160. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  161. for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
  162. ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
  163. cur->ai_protocol);
  164. if (ctx->fd < 0) {
  165. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  166. continue;
  167. }
  168. if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) == 0) {
  169. ret = 0;
  170. break;
  171. }
  172. close(ctx->fd);
  173. ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  174. }
  175. freeaddrinfo(addr_list);
  176. return ret;
  177. }
  178. /*
  179. * Create a listening socket on bind_ip:port
  180. */
  181. int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
  182. {
  183. int n, ret;
  184. struct addrinfo hints, *addr_list, *cur;
  185. if ((ret = net_prepare()) != 0) {
  186. return ret;
  187. }
  188. /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
  189. memset(&hints, 0, sizeof(hints));
  190. hints.ai_family = AF_UNSPEC;
  191. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  192. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  193. if (bind_ip == NULL) {
  194. hints.ai_flags = AI_PASSIVE;
  195. }
  196. if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0) {
  197. return MBEDTLS_ERR_NET_UNKNOWN_HOST;
  198. }
  199. /* Try the sockaddrs until a binding succeeds */
  200. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  201. for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
  202. ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
  203. cur->ai_protocol);
  204. if (ctx->fd < 0) {
  205. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  206. continue;
  207. }
  208. n = 1;
  209. if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  210. (const char *) &n, sizeof(n)) != 0) {
  211. close(ctx->fd);
  212. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  213. continue;
  214. }
  215. if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
  216. close(ctx->fd);
  217. ret = MBEDTLS_ERR_NET_BIND_FAILED;
  218. continue;
  219. }
  220. /* Listen only makes sense for TCP */
  221. if (proto == MBEDTLS_NET_PROTO_TCP) {
  222. if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
  223. close(ctx->fd);
  224. ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
  225. continue;
  226. }
  227. }
  228. /* Bind was successful */
  229. ret = 0;
  230. break;
  231. }
  232. freeaddrinfo(addr_list);
  233. return ret;
  234. }
  235. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  236. !defined(EFI32)
  237. /*
  238. * Check if the requested operation would be blocking on a non-blocking socket
  239. * and thus 'failed' with a negative return value.
  240. */
  241. static int net_would_block(const mbedtls_net_context *ctx)
  242. {
  243. ((void) ctx);
  244. return WSAGetLastError() == WSAEWOULDBLOCK;
  245. }
  246. #else
  247. /*
  248. * Check if the requested operation would be blocking on a non-blocking socket
  249. * and thus 'failed' with a negative return value.
  250. *
  251. * Note: on a blocking socket this function always returns 0!
  252. */
  253. static int net_would_block(const mbedtls_net_context *ctx)
  254. {
  255. int err = errno;
  256. /*
  257. * Never return 'WOULD BLOCK' on a blocking socket
  258. */
  259. if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
  260. errno = err;
  261. return 0;
  262. }
  263. switch (errno = err) {
  264. #if defined EAGAIN
  265. case EAGAIN:
  266. #endif
  267. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  268. case EWOULDBLOCK:
  269. #endif
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  275. /*
  276. * Accept a connection from a remote client
  277. */
  278. int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
  279. mbedtls_net_context *client_ctx,
  280. void *client_ip, size_t buf_size, size_t *ip_len)
  281. {
  282. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  283. int type;
  284. struct sockaddr_storage client_addr;
  285. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
  286. defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
  287. defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
  288. socklen_t n = (socklen_t) sizeof(client_addr);
  289. socklen_t type_len = (socklen_t) sizeof(type);
  290. #else
  291. int n = (int) sizeof(client_addr);
  292. int type_len = (int) sizeof(type);
  293. #endif
  294. /* Is this a TCP or UDP socket? */
  295. if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE,
  296. (void *) &type, &type_len) != 0 ||
  297. (type != SOCK_STREAM && type != SOCK_DGRAM)) {
  298. return MBEDTLS_ERR_NET_ACCEPT_FAILED;
  299. }
  300. if (type == SOCK_STREAM) {
  301. /* TCP: actual accept() */
  302. ret = client_ctx->fd = (int) accept(bind_ctx->fd,
  303. (struct sockaddr *) &client_addr, &n);
  304. } else {
  305. /* UDP: wait for a message, but keep it in the queue */
  306. char buf[1] = { 0 };
  307. ret = (int) recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
  308. (struct sockaddr *) &client_addr, &n);
  309. #if defined(_WIN32)
  310. if (ret == SOCKET_ERROR &&
  311. WSAGetLastError() == WSAEMSGSIZE) {
  312. /* We know buf is too small, thanks, just peeking here */
  313. ret = 0;
  314. }
  315. #endif
  316. }
  317. if (ret < 0) {
  318. if (net_would_block(bind_ctx) != 0) {
  319. return MBEDTLS_ERR_SSL_WANT_READ;
  320. }
  321. return MBEDTLS_ERR_NET_ACCEPT_FAILED;
  322. }
  323. /* UDP: hijack the listening socket to communicate with the client,
  324. * then bind a new socket to accept new connections */
  325. if (type != SOCK_STREAM) {
  326. struct sockaddr_storage local_addr;
  327. int one = 1;
  328. if (connect(bind_ctx->fd, (struct sockaddr *) &client_addr, n) != 0) {
  329. return MBEDTLS_ERR_NET_ACCEPT_FAILED;
  330. }
  331. client_ctx->fd = bind_ctx->fd;
  332. bind_ctx->fd = -1; /* In case we exit early */
  333. n = sizeof(struct sockaddr_storage);
  334. if (getsockname(client_ctx->fd,
  335. (struct sockaddr *) &local_addr, &n) != 0 ||
  336. (bind_ctx->fd = (int) socket(local_addr.ss_family,
  337. SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
  338. setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  339. (const char *) &one, sizeof(one)) != 0) {
  340. return MBEDTLS_ERR_NET_SOCKET_FAILED;
  341. }
  342. if (bind(bind_ctx->fd, (struct sockaddr *) &local_addr, n) != 0) {
  343. return MBEDTLS_ERR_NET_BIND_FAILED;
  344. }
  345. }
  346. if (client_ip != NULL) {
  347. if (client_addr.ss_family == AF_INET) {
  348. struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
  349. *ip_len = sizeof(addr4->sin_addr.s_addr);
  350. if (buf_size < *ip_len) {
  351. return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
  352. }
  353. memcpy(client_ip, &addr4->sin_addr.s_addr, *ip_len);
  354. } else {
  355. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
  356. *ip_len = sizeof(addr6->sin6_addr.s6_addr);
  357. if (buf_size < *ip_len) {
  358. return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
  359. }
  360. memcpy(client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
  361. }
  362. }
  363. return 0;
  364. }
  365. /*
  366. * Set the socket blocking or non-blocking
  367. */
  368. int mbedtls_net_set_block(mbedtls_net_context *ctx)
  369. {
  370. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  371. !defined(EFI32)
  372. u_long n = 0;
  373. return ioctlsocket(ctx->fd, FIONBIO, &n);
  374. #else
  375. return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
  376. #endif
  377. }
  378. int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
  379. {
  380. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  381. !defined(EFI32)
  382. u_long n = 1;
  383. return ioctlsocket(ctx->fd, FIONBIO, &n);
  384. #else
  385. return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
  386. #endif
  387. }
  388. /*
  389. * Check if data is available on the socket
  390. */
  391. int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
  392. {
  393. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  394. struct timeval tv;
  395. fd_set read_fds;
  396. fd_set write_fds;
  397. int fd = ctx->fd;
  398. ret = check_fd(fd, 1);
  399. if (ret != 0) {
  400. return ret;
  401. }
  402. #if defined(__has_feature)
  403. #if __has_feature(memory_sanitizer)
  404. /* Ensure that memory sanitizers consider read_fds and write_fds as
  405. * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
  406. * is implemented in assembly. */
  407. memset(&read_fds, 0, sizeof(read_fds));
  408. memset(&write_fds, 0, sizeof(write_fds));
  409. #endif
  410. #endif
  411. FD_ZERO(&read_fds);
  412. if (rw & MBEDTLS_NET_POLL_READ) {
  413. rw &= ~MBEDTLS_NET_POLL_READ;
  414. FD_SET(fd, &read_fds);
  415. }
  416. FD_ZERO(&write_fds);
  417. if (rw & MBEDTLS_NET_POLL_WRITE) {
  418. rw &= ~MBEDTLS_NET_POLL_WRITE;
  419. FD_SET(fd, &write_fds);
  420. }
  421. if (rw != 0) {
  422. return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
  423. }
  424. tv.tv_sec = timeout / 1000;
  425. tv.tv_usec = (timeout % 1000) * 1000;
  426. do {
  427. ret = select(fd + 1, &read_fds, &write_fds, NULL,
  428. timeout == (uint32_t) -1 ? NULL : &tv);
  429. } while (IS_EINTR(ret));
  430. if (ret < 0) {
  431. return MBEDTLS_ERR_NET_POLL_FAILED;
  432. }
  433. ret = 0;
  434. if (FD_ISSET(fd, &read_fds)) {
  435. ret |= MBEDTLS_NET_POLL_READ;
  436. }
  437. if (FD_ISSET(fd, &write_fds)) {
  438. ret |= MBEDTLS_NET_POLL_WRITE;
  439. }
  440. return ret;
  441. }
  442. /*
  443. * Portable usleep helper
  444. */
  445. void mbedtls_net_usleep(unsigned long usec)
  446. {
  447. #if defined(_WIN32)
  448. Sleep((usec + 999) / 1000);
  449. #else
  450. struct timeval tv;
  451. tv.tv_sec = usec / 1000000;
  452. #if defined(__unix__) || defined(__unix) || \
  453. (defined(__APPLE__) && defined(__MACH__))
  454. tv.tv_usec = (suseconds_t) usec % 1000000;
  455. #else
  456. tv.tv_usec = usec % 1000000;
  457. #endif
  458. select(0, NULL, NULL, NULL, &tv);
  459. #endif
  460. }
  461. /*
  462. * Read at most 'len' characters
  463. */
  464. int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
  465. {
  466. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  467. int fd = ((mbedtls_net_context *) ctx)->fd;
  468. ret = check_fd(fd, 0);
  469. if (ret != 0) {
  470. return ret;
  471. }
  472. ret = (int) read(fd, buf, len);
  473. if (ret < 0) {
  474. if (net_would_block(ctx) != 0) {
  475. return MBEDTLS_ERR_SSL_WANT_READ;
  476. }
  477. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  478. !defined(EFI32)
  479. if (WSAGetLastError() == WSAECONNRESET) {
  480. return MBEDTLS_ERR_NET_CONN_RESET;
  481. }
  482. #else
  483. if (errno == EPIPE || errno == ECONNRESET) {
  484. return MBEDTLS_ERR_NET_CONN_RESET;
  485. }
  486. if (errno == EINTR) {
  487. return MBEDTLS_ERR_SSL_WANT_READ;
  488. }
  489. #endif
  490. return MBEDTLS_ERR_NET_RECV_FAILED;
  491. }
  492. return ret;
  493. }
  494. /*
  495. * Read at most 'len' characters, blocking for at most 'timeout' ms
  496. */
  497. int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf,
  498. size_t len, uint32_t timeout)
  499. {
  500. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  501. struct timeval tv;
  502. fd_set read_fds;
  503. int fd = ((mbedtls_net_context *) ctx)->fd;
  504. ret = check_fd(fd, 1);
  505. if (ret != 0) {
  506. return ret;
  507. }
  508. FD_ZERO(&read_fds);
  509. FD_SET(fd, &read_fds);
  510. tv.tv_sec = timeout / 1000;
  511. tv.tv_usec = (timeout % 1000) * 1000;
  512. ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
  513. /* Zero fds ready means we timed out */
  514. if (ret == 0) {
  515. return MBEDTLS_ERR_SSL_TIMEOUT;
  516. }
  517. if (ret < 0) {
  518. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  519. !defined(EFI32)
  520. if (WSAGetLastError() == WSAEINTR) {
  521. return MBEDTLS_ERR_SSL_WANT_READ;
  522. }
  523. #else
  524. if (errno == EINTR) {
  525. return MBEDTLS_ERR_SSL_WANT_READ;
  526. }
  527. #endif
  528. return MBEDTLS_ERR_NET_RECV_FAILED;
  529. }
  530. /* This call will not block */
  531. return mbedtls_net_recv(ctx, buf, len);
  532. }
  533. /*
  534. * Write at most 'len' characters
  535. */
  536. int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
  537. {
  538. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  539. int fd = ((mbedtls_net_context *) ctx)->fd;
  540. ret = check_fd(fd, 0);
  541. if (ret != 0) {
  542. return ret;
  543. }
  544. ret = (int) write(fd, buf, len);
  545. if (ret < 0) {
  546. if (net_would_block(ctx) != 0) {
  547. return MBEDTLS_ERR_SSL_WANT_WRITE;
  548. }
  549. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  550. !defined(EFI32)
  551. if (WSAGetLastError() == WSAECONNRESET) {
  552. return MBEDTLS_ERR_NET_CONN_RESET;
  553. }
  554. #else
  555. if (errno == EPIPE || errno == ECONNRESET) {
  556. return MBEDTLS_ERR_NET_CONN_RESET;
  557. }
  558. if (errno == EINTR) {
  559. return MBEDTLS_ERR_SSL_WANT_WRITE;
  560. }
  561. #endif
  562. return MBEDTLS_ERR_NET_SEND_FAILED;
  563. }
  564. return ret;
  565. }
  566. /*
  567. * Close the connection
  568. */
  569. void mbedtls_net_close(mbedtls_net_context *ctx)
  570. {
  571. if (ctx->fd == -1) {
  572. return;
  573. }
  574. close(ctx->fd);
  575. ctx->fd = -1;
  576. }
  577. /*
  578. * Gracefully close the connection
  579. */
  580. void mbedtls_net_free(mbedtls_net_context *ctx)
  581. {
  582. if (ctx->fd == -1) {
  583. return;
  584. }
  585. shutdown(ctx->fd, 2);
  586. close(ctx->fd);
  587. ctx->fd = -1;
  588. }
  589. #endif /* MBEDTLS_NET_C */