Skip to content

Commit

Permalink
improvements for ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
anhu committed May 31, 2024
1 parent d09e00a commit 03e8668
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 16 deletions.
8 changes: 7 additions & 1 deletion apps/wolfssh/wolfssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
8 changes: 4 additions & 4 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
18 changes: 15 additions & 3 deletions examples/portfwd/portfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion examples/scpclient/scpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 8 additions & 1 deletion examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions wolfssh/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -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) \
Expand Down

0 comments on commit 03e8668

Please sign in to comment.