net_sockets.c 17 KB

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