net_sockets.c 18 KB

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