diff --git a/apps/wolfssh/wolfssh.c b/apps/wolfssh/wolfssh.c index 73a3cbbaa..1bc34b39e 100644 --- a/apps/wolfssh/wolfssh.c +++ b/apps/wolfssh/wolfssh.c @@ -991,7 +991,8 @@ 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, ((struct sockaddr_in *)&clientAddr)->sin_family); + 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..3fbcbf736 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2374,8 +2374,8 @@ static int StartSSHD(int argc, char** argv) #ifdef WOLFSSL_NUCLEUS struct addr_struct clientAddr; #else - SOCKADDR_IN_T clientAddr; - socklen_t clientAddrSz = sizeof(clientAddr); + struct sockaddr_in6 clientAddr; + socklen_t clientAddrSz = sizeof(clientAddr); #endif conn = (WOLFSSHD_CONNECTION*)WMALLOC(sizeof(WOLFSSHD_CONNECTION), NULL, DYNTYPE_SSHD); if (conn == NULL) { @@ -2396,13 +2396,16 @@ 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, -#else - &clientAddr.sin_addr, -#endif /* TEST_IPV6 */ - conn->ip, INET_ADDRSTRLEN); + if (clientAddr.sin6_family == AF_INET) { + struct sockaddr_in* addr4 = + (struct sockaddr_in*)&clientAddr; + inet_ntop(AF_INET, &addr4->sin_addr, conn->ip, + INET_ADDRSTRLEN); + } + else if (clientAddr.sin6_family == AF_INET6) { + inet_ntop(AF_INET6, &clientAddr.sin6_addr, conn->ip, + INET6_ADDRSTRLEN); + } } #endif diff --git a/examples/client/client.c b/examples/client/client.c index 26cfbfdf6..d375b21d9 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -904,7 +904,8 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) } build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); + tcp_socket(&sockFd, ((struct sockaddr_in *)&clientAddr)->sin_family); + 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..07bb95da9 100644 --- a/examples/portfwd/portfwd.c +++ b/examples/portfwd/portfwd.c @@ -380,12 +380,14 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) if (ret != WS_SUCCESS) err_sys("Couldn't set the username."); + /* Socket to SSH peer. */ build_addr(&hostAddr, host, port); + tcp_socket(&sshFd, ((struct sockaddr_in *)&hostAddr)->sin_family); + + /* Receive from client application or connect to server application. */ build_addr(&fwdFromHostAddr, fwdFromHost, fwdFromPort); + tcp_socket(&listenFd, ((struct sockaddr_in *)&fwdFromHostAddr)->sin_family); - tcp_socket(&sshFd); /* Socket to SSH peer. */ - tcp_socket(&listenFd); /* Either receive from client application or connect - to server application. */ tcp_listen(&listenFd, &fwdFromPort, 1); printf("Connecting to the SSH server...\n"); diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c index 1392b2006..2fadcf767 100644 --- a/examples/scpclient/scpclient.c +++ b/examples/scpclient/scpclient.c @@ -280,8 +280,9 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) { printf("IPV4 address\n"); build_addr(&clientAddr, host, port); - tcp_socket(&sockFd); - ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); + tcp_socket(&sockFd, ((struct sockaddr_in *)&clientAddr)->sin_family); + ret = connect(sockFd, (const struct sockaddr *)&clientAddr, + clientAddrSz); } if (ret != 0) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index edc036011..eec4db1a4 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -1366,7 +1366,8 @@ 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, ((struct sockaddr_in *)&clientAddr)->sin_family); + 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..e5600a389 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, ((struct sockaddr_in *)&clientAddr)->sin_family); 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..11e7b94eb 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,8 +546,10 @@ 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 targetProtocol) { + /* targetProtocol is only used if none of these platforms are defined. */ + WOLFSSH_UNUSED(targetProtocol); #ifdef MICROCHIP_MPLAB_HARMONY /* creates socket in listen or connect */ *sockFd = 0; @@ -558,7 +560,7 @@ 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); + *sockFd = socket(targetProtocol, SOCK_STREAM, 0); #endif #ifdef USE_WINDOWS_API @@ -637,8 +639,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, ((struct sockaddr_in *)&addr)->sin_family); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \ && !defined(WOLFSSL_KEIL_TCP_NET) \ && !defined(WOLFSSL_NUCLEUS) \