From b11d61e095dd656afec02716f350db1e2063a8b9 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 4 Aug 2021 20:38:59 +0200 Subject: [PATCH] mbedtls_net_context: make fd public on Unix/POSIX platforms On platforms with BSD-like sockets, it is useful for applications to have access to the underlying file descriptor so that they can use functions like select() and poll(). Do not promise that the field will exist on other platforms such as Windows (where the type and name of the field are technically wrong because Windows socket handles are actually not file descriptors). Signed-off-by: Gilles Peskine --- include/mbedtls/net_sockets.h | 8 +++++++- programs/ssl/mini_client.c | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/mbedtls/net_sockets.h b/include/mbedtls/net_sockets.h index c8214a2335..0c754b122c 100644 --- a/include/mbedtls/net_sockets.h +++ b/include/mbedtls/net_sockets.h @@ -94,7 +94,13 @@ extern "C" { */ typedef struct mbedtls_net_context { - int MBEDTLS_PRIVATE(fd); /**< The underlying file descriptor */ + /** The underlying file descriptor. + * + * This field is only guaranteed to be present on POSIX/Unix-like platforms. + * On other platforms, it may have a different type, have a different + * meaning, or be absent altogether. + */ + int fd; } mbedtls_net_context; diff --git a/programs/ssl/mini_client.c b/programs/ssl/mini_client.c index 1e0bef6b1c..97bfe68061 100644 --- a/programs/ssl/mini_client.c +++ b/programs/ssl/mini_client.c @@ -246,13 +246,13 @@ int main( void ) addr.sin_addr.s_addr = *((char *) &ret) == ret ? ADDR_LE : ADDR_BE; ret = 0; - if( ( server_fd.MBEDTLS_PRIVATE(fd) = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) + if( ( server_fd.fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) { ret = socket_failed; goto exit; } - if( connect( server_fd.MBEDTLS_PRIVATE(fd), + if( connect( server_fd.fd, (const struct sockaddr *) &addr, sizeof( addr ) ) < 0 ) { ret = connect_failed;