Skip to content

Commit

Permalink
Merge pull request #703 from anhu/ipv6_part2
Browse files Browse the repository at this point in the history
improvements for ipv6
  • Loading branch information
dgarske authored Jun 3, 2024
2 parents c330222 + ec4909b commit b6ccb07
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
3 changes: 2 additions & 1 deletion apps/wolfssh/wolfssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
21 changes: 12 additions & 9 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
8 changes: 5 additions & 3 deletions examples/portfwd/portfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions examples/scpclient/scpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
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, ((struct sockaddr_in *)&clientAddr)->sin_family);
ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz);
if (ret != 0){
wolfSSH_free(*ssh);
Expand Down
11 changes: 6 additions & 5 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,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;
Expand All @@ -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
Expand Down Expand Up @@ -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) \
Expand Down

0 comments on commit b6ccb07

Please sign in to comment.