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