net_sockets.c 19 KB

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