Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Sep 25, 2023
1 parent 44393df commit 44d8625
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
17 changes: 17 additions & 0 deletions net/test/test-ipv6.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <vector>
#include <gtest/gtest.h>

#include <photon/photon.h>
#include <photon/thread/thread11.h>
#include <photon/net/socket.h>
#include <photon/net/utils.h>
#include <photon/common/alog.h>

TEST(ipv6, addr) {
Expand Down Expand Up @@ -32,6 +34,21 @@ TEST(ipv6, addr) {
EXPECT_TRUE(b.is_link_local());
}

TEST(ipv6, get_host_by_peer) {
auto peer = photon::net::gethostbypeer(photon::net::IPAddr("2001:4860:4860::8888"));
ASSERT_TRUE(!peer.empty());
ASSERT_TRUE(!peer.is_ipv4());
LOG_INFO(peer);
}

TEST(ipv6, dns_lookup) {
std::vector<photon::net::IPAddr> ret;
int num = photon::net::gethostbyname("github.com", ret, true);
ASSERT_TRUE(num >= 1);
ASSERT_TRUE(!ret[0].is_ipv4());
LOG_INFO(ret[0]);
}

class DualStackTest : public ::testing::Test {
public:
void run() {
Expand Down
25 changes: 25 additions & 0 deletions net/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ int _gethostbyname(const char *name, Delegate<int, IPAddr> append_op) {
return idx;
}

int _gethostbyname_ipv6(const char* name, Delegate<int, IPAddr> append_op) {
int idx = 0;
addrinfo* result = nullptr;
addrinfo* cur = nullptr;
addrinfo hints = {};
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET6;

if (getaddrinfo(name, nullptr, &hints, &result) != 0) {
return -1;
}
for (cur = result; cur != nullptr; cur = cur->ai_next) {
if (cur->ai_family == AF_INET6) {
auto sock_addr = (sockaddr_in6*) cur->ai_addr;
if (append_op(IPAddr(sock_addr->sin6_addr)) < 0) {
break;
}
idx++;
}
}
freeaddrinfo(result);
return idx;
}

inline __attribute__((always_inline)) void base64_translate_3to4(const char *in, char *out) {
struct xlator {
unsigned char _;
Expand Down
20 changes: 12 additions & 8 deletions net/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ IPAddr gethostbypeer(const char* domain);
// Callback returns -1 means break

int _gethostbyname(const char* name, Callback<IPAddr> append_op);
int _gethostbyname_ipv6(const char* name, Callback<IPAddr> append_op);

// inline implemention for compatible

Expand All @@ -69,13 +70,16 @@ int _gethostbyname(const char* name, Callback<IPAddr> append_op);
* @param name Host name to resolve
* @return first resolved address.
*/
inline IPAddr gethostbyname(const char* name) {
inline IPAddr gethostbyname(const char* name, bool ip_v6 = false) {
IPAddr ret;
auto cb = [&](IPAddr addr) {
ret = addr;
return -1;
};
_gethostbyname(name, cb);
if (ip_v6)
_gethostbyname_ipv6(name, cb);
else
_gethostbyname(name, cb);
return ret;
}

Expand All @@ -88,15 +92,15 @@ int _gethostbyname(const char* name, Callback<IPAddr> append_op);
* @param name Host name to resolve
* @param buf IPAddr buffer pointer
* @param bufsize size of `buf`, takes `sizeof(IPAddr)` as unit
* @return sum of resolved address number. result will be filled into `buf`
* @return sum of resolved address number. -1 means error. result will be filled into `buf`
*/
inline int gethostbyname(const char* name, IPAddr* buf, int bufsize = 1) {
inline int gethostbyname(const char* name, IPAddr* buf, int bufsize = 1, bool ip_v6 = false) {
int i = 0;
auto cb = [&](IPAddr addr) {
if (i < bufsize) buf[i++] = addr;
return 0;
};
return _gethostbyname(name, cb);
return ip_v6 ? _gethostbyname_ipv6(name, cb) : _gethostbyname(name, cb);
}

/**
Expand All @@ -107,15 +111,15 @@ inline int gethostbyname(const char* name, IPAddr* buf, int bufsize = 1) {
*
* @param name Host name to resolve
* @param ret `std::vector<IPAddr>` reference to get results
* @return sum of resolved address number.
* @return sum of resolved address number. -1 means error.
*/
inline int gethostbyname(const char* name, std::vector<IPAddr>& ret) {
inline int gethostbyname(const char* name, std::vector<IPAddr>& ret, bool ip_v6 = false) {
ret.clear();
auto cb = [&](IPAddr addr) {
ret.push_back(addr);
return 0;
};
return _gethostbyname(name, cb);
return ip_v6 ? _gethostbyname_ipv6(name, cb) : _gethostbyname(name, cb);
}

/**
Expand Down

0 comments on commit 44d8625

Please sign in to comment.