From 03e8668bc3793831ed3194492598d57ac7404928 Mon Sep 17 00:00:00 2001 From: Anthony Hu Date: Thu, 30 May 2024 14:13:03 -0400 Subject: [PATCH] improvements for ipv6 --- apps/wolfssh/wolfssh.c | 8 +++++++- apps/wolfsshd/wolfsshd.c | 8 ++++---- examples/client/client.c | 8 +++++++- examples/portfwd/portfwd.c | 18 +++++++++++++++--- examples/scpclient/scpclient.c | 8 +++++++- examples/sftpclient/sftpclient.c | 9 ++++++++- tests/api.c | 2 +- wolfssh/test.h | 11 +++++++---- 8 files changed, 56 insertions(+), 16 deletions(-) diff --git a/apps/wolfssh/wolfssh.c b/apps/wolfssh/wolfssh.c index 73a3cbbaa..47e95584a 100644 --- a/apps/wolfssh/wolfssh.c +++ b/apps/wolfssh/wolfssh.c @@ -991,7 +991,13 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args) err_sys("Couldn't set the username."); build_addr(&clientAddr, config.hostname, config.port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, 1, +#ifdef TEST_IPV6 + clientAddr.sin6_family +#else + clientAddr.sin_family +#endif + ); ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); if (ret != 0) err_sys("Couldn't connect to server."); diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index e994df50a..1f75e5b3f 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2396,13 +2396,13 @@ static int StartSSHD(int argc, char** argv) conn->fd = (int)accept(listenFd, (struct sockaddr*)&clientAddr, &clientAddrSz); if (conn->fd >= 0) { - inet_ntop(AF_INET, #ifdef TEST_IPV6 - &clientAddr.sin6_addr, + inet_ntop(AF_INET, &clientAddr.sin6_addr, conn->ip, + INET6_ADDRSTRLEN); #else - &clientAddr.sin_addr, + inet_ntop(AF_INET, &clientAddr.sin_addr, conn->ip, + INET_ADDRSTRLEN); #endif /* TEST_IPV6 */ - conn->ip, INET_ADDRSTRLEN); } #endif diff --git a/examples/client/client.c b/examples/client/client.c index 26cfbfdf6..11644dca3 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -904,7 +904,13 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) } build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, 1, +#ifdef TEST_IPV6 + clientAddr.sin6_family +#else + clientAddr.sin_family +#endif + ); ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); if (ret != 0) err_sys("Couldn't connect to server."); diff --git a/examples/portfwd/portfwd.c b/examples/portfwd/portfwd.c index ecc38f7e8..3a157de21 100644 --- a/examples/portfwd/portfwd.c +++ b/examples/portfwd/portfwd.c @@ -381,10 +381,22 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) err_sys("Couldn't set the username."); build_addr(&hostAddr, host, port); - build_addr(&fwdFromHostAddr, fwdFromHost, fwdFromPort); + tcp_socket(&sshFd, 1, +#ifdef TEST_IPV6 + hostAddr.sin6_family +#else + hostAddr.sin_family +#endif + ); /* Socket to SSH peer. */ - tcp_socket(&sshFd); /* Socket to SSH peer. */ - tcp_socket(&listenFd); /* Either receive from client application or connect + build_addr(&fwdFromHostAddr, fwdFromHost, fwdFromPort); + tcp_socket(&listenFd, 1, +#ifdef TEST_IPV6 + fwdFromHostAddr.sin6_family +#else + fwdFromHostAddr.sin_family +#endif + ); /* Either receive from client application or connect to server application. */ tcp_listen(&listenFd, &fwdFromPort, 1); diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c index c0cc7602b..4bcb33d17 100644 --- a/examples/scpclient/scpclient.c +++ b/examples/scpclient/scpclient.c @@ -280,7 +280,13 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) { printf("IPV4 address\n"); build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, 1, +#ifdef TEST_IPV6 + clientAddr.sin6_family +#else + clientAddr.sin_family +#endif + ); ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); } diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index edc036011..e590cfd40 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -1366,7 +1366,14 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args) err_sys("Couldn't set the username."); build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, 1, +#ifdef TEST_IPV6 + clientAddr.sin6_family +#else + clientAddr.sin_family +#endif + ); + ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); if (ret != 0) err_sys("Couldn't connect to server."); diff --git a/tests/api.c b/tests/api.c index 3c738bc81..5ca9caa87 100644 --- a/tests/api.c +++ b/tests/api.c @@ -921,7 +921,7 @@ static void sftp_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port) } build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, 0, 0); ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); if (ret != 0){ wolfSSH_free(*ssh); diff --git a/wolfssh/test.h b/wolfssh/test.h index e8dc484bc..e73e1d24d 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -519,7 +519,7 @@ static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET_V; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; @@ -546,7 +546,7 @@ static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, #endif -static INLINE void tcp_socket(WS_SOCKET_T* sockFd) +static INLINE void tcp_socket(WS_SOCKET_T* sockFd, int specifyProtocol, int targetProtocol) { #ifdef MICROCHIP_MPLAB_HARMONY /* creates socket in listen or connect */ @@ -558,7 +558,10 @@ static INLINE void tcp_socket(WS_SOCKET_T* sockFd) #elif defined(WOLFSSL_NUCLEUS) *sockFd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0); #else - *sockFd = socket(AF_INET_V, SOCK_STREAM, 0); + if (!specifyProtocol) + *sockFd = socket(AF_INET_V, SOCK_STREAM, 0); + else + *sockFd = socket(targetProtocol, SOCK_STREAM, 0); #endif #ifdef USE_WINDOWS_API @@ -637,7 +640,7 @@ static INLINE void tcp_listen(WS_SOCKET_T* sockfd, word16* port, int useAnyAddr) /* don't use INADDR_ANY by default, firewall may block, make user switch on */ build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSshIp), *port); - tcp_socket(sockfd); + tcp_socket(sockfd, 0, 0); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \ && !defined(WOLFSSL_KEIL_TCP_NET) \