Skip to content

Commit

Permalink
Fix the bug about the name resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
frodez committed Oct 25, 2021
1 parent 0f685b1 commit 9962a83
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@ if(NOT NO_EXAMPLES)

add_executable(http_server_simple ${PROJECT_SOURCE_DIR}/example/http/simple/server.cpp)
target_link_libraries(http_server_simple spinet_static Threads::Threads)

add_executable(address_resolve ${PROJECT_SOURCE_DIR}/example/address/resolve.cpp)
target_link_libraries(address_resolve spinet_static Threads::Threads)
endif()
#--------------------for the examples end--------------------
30 changes: 30 additions & 0 deletions example/address/resolve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>

#include "spinet.h"

int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "Usage: PROGRAM_NAME ${address} ${port}" << std::endl;
return EXIT_FAILURE;
}
char* ptr = nullptr;
long port = strtol(argv[2], &ptr, 10);
if (*ptr != 0) {
std::cout << "port is an invalid number." << std::endl;
return EXIT_FAILURE;
}
if (port < 0 || port > 65535) {
std::cout << "port should be between 0 and 65535." << std::endl;
return EXIT_FAILURE;
}
auto res = spinet::Address::resolve(argv[1], port);
if (res.index() == 1) {
std::cout << std::get<1>(res) << std::endl;
return EXIT_FAILURE;
}
auto addresses = std::get<0>(res);
for (auto& address : addresses) {
std::cout << address.to_string() << std::endl;
}
return EXIT_SUCCESS;
}
15 changes: 10 additions & 5 deletions src/core/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,26 @@ std::variant<std::vector<Address>, std::string> Address::resolve(const char* dom
hints.ai_family = PF_UNSPEC;
::addrinfo* res = nullptr;
{
int ec = ::getaddrinfo(domain, std::to_string(port).c_str(), &hints, &res);
int ec = ::getaddrinfo(domain, nullptr, &hints, &res);
if (ec != 0) {
return ::gai_strerror(ec);
}
}
std::vector<Address> addresses {};
for (::addrinfo* iter = res; iter != nullptr; iter = iter->ai_next) {
if (iter->ai_protocol != IPPROTO_IP) {
continue;
}
char buf[std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)] = { 0 };
if (iter->ai_family == AF_INET) {
::inet_ntop(AF_INET, iter->ai_addr->sa_data, buf, INET_ADDRSTRLEN);
Address address { IPV4, std::string { buf }, ntohs(((sockaddr_in*)iter->ai_addr)->sin_port) };
sockaddr_in* sockaddr = (sockaddr_in*)iter->ai_addr;
::inet_ntop(AF_INET, &sockaddr->sin_addr, buf, INET_ADDRSTRLEN);
Address address { IPV4, std::string { buf }, port };
addresses.push_back(address);
} else {
::inet_ntop(AF_INET6, iter->ai_addr->sa_data, buf, INET6_ADDRSTRLEN);
Address address { IPV6, std::string { buf }, ((sockaddr_in6*)iter->ai_addr)->sin6_port };
sockaddr_in6* sockaddr = (sockaddr_in6*)iter->ai_addr;
::inet_ntop(AF_INET6, &sockaddr->sin6_addr, buf, INET6_ADDRSTRLEN);
Address address { IPV6, std::string { buf }, port };
addresses.push_back(address);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ inline void set_reuse_port(int fd) {
}

inline std::string from_sockaddr_in(const ::sockaddr_in& address) {
std::size_t len = address.sin_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
char buf[std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)] = { 0 };
::inet_ntop(address.sin_family, &address.sin_addr, buf, len);
::inet_ntop(address.sin_family, &address.sin_addr, buf, address.sin_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN);
return { buf };
}

Expand Down

0 comments on commit 9962a83

Please sign in to comment.