From a847da5339bb3cc5f3582a6ec61f7c1b8b7f4f6e Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Tue, 12 Mar 2024 10:02:23 -0500 Subject: [PATCH] Added support for printing out the peer's IPv6 address; replaced struct sockaddr with struct sockaddr_storage --- server.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server.cpp b/server.cpp index cd67f1b..a20f644 100644 --- a/server.cpp +++ b/server.cpp @@ -43,11 +43,14 @@ char* Server::recvAll() { if (this->ip_ver == 4) { /* FOR IPv4 */ struct sockaddr_in* temp_struct = (struct sockaddr_in*)this->dest; - /* Convert the s_addr field of the caseted struct to host network-byte, and convert it to a dotted decimal */ + /* Convert the s_addr field of the casted struct to host network-byte, and convert it to a dotted decimal */ peer_addr = connect_code::dec_to_dotted_dec(std::to_string(htonl(temp_struct->sin_addr.s_addr))); } else { - /* FOR IPv6 */ - peer_addr = "IPV6 NOT SUPPORTED YET"; + /* FOR IPv6 - Use the inet_ntop function, and convert the struct's address into a string */ + struct sockaddr_in6* temp_struct = (struct sockaddr_in6*)this->dest; + char* temp_buf = (char *)malloc(sizeof(char) * (INET6_ADDRSTRLEN + 1)); + peer_addr = std::string(inet_ntop(AF_INET6, temp_struct->sin6_addr.s6_addr, temp_buf, INET6_ADDRSTRLEN)); + free(temp_buf); } return to_return; @@ -83,7 +86,7 @@ is thrown as an exception. */ void Server::wait_for_peer() { if (this->protocol == ES_TCP) { - this->other_socket = accept(this->sock_fd, dest, &addrlen); + this->other_socket = accept(this->sock_fd, (struct sockaddr *)dest, &addrlen); if (this->other_socket < 0) { throw errno; }