net_sockets.c 18 KB

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