Skip to content

Commit

Permalink
fix: Skip Link-Local IPv6 Address (#400)
Browse files Browse the repository at this point in the history
* feat: Skip getaddrinfo if IPv6 not enabled

* feat: Elevate logging level for craned connection

* fix: Skip link local only ipv6 addr

* fix: Remove useless headers
  • Loading branch information
Nativu5 authored Dec 15, 2024
1 parent 872cbff commit 815c797
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/CraneCtld/CraneCtld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ void InitializeCtldGlobalVariables() {
g_craned_keeper = std::make_unique<CranedKeeper>(g_config.Nodes.size());

g_craned_keeper->SetCranedIsUpCb([](const CranedId& craned_id) {
CRANE_TRACE(
CRANE_DEBUG(
"A new node #{} is up now. Add its resource to the global resource "
"pool.",
craned_id);
Expand All @@ -706,7 +706,7 @@ void InitializeCtldGlobalVariables() {
});

g_craned_keeper->SetCranedIsDownCb([](const CranedId& craned_id) {
CRANE_TRACE(
CRANE_DEBUG(
"CranedNode #{} is down now. "
"Remove its resource from the global resource pool.",
craned_id);
Expand Down
23 changes: 16 additions & 7 deletions src/Utilities/PublicHeader/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool ResolveHostnameFromIpv4(ipv4_t addr, std::string* hostname) {
if (internal::g_hosts_map->FindFirstHostnameOfIpv4(addr, hostname))
return true;

struct sockaddr_in sa {};
struct sockaddr_in sa{};
char hbuf[NI_MAXHOST];

std::string ipv4_str = Ipv4ToStr(addr);
Expand All @@ -169,7 +169,7 @@ bool ResolveHostnameFromIpv6(const ipv6_t& addr, std::string* hostname) {
if (internal::g_hosts_map->FindFirstHostnameOfIpv6(addr, hostname))
return true;

struct sockaddr_in6 sa6 {};
struct sockaddr_in6 sa6{};
char hbuf[NI_MAXHOST];

/* For IPv6*/
Expand All @@ -196,7 +196,7 @@ bool ResolveHostnameFromIpv6(const ipv6_t& addr, std::string* hostname) {
bool ResolveIpv4FromHostname(const std::string& hostname, ipv4_t* addr) {
if (internal::g_hosts_map->FindIpv4OfHostname(hostname, addr)) return true;

struct addrinfo hints {};
struct addrinfo hints{};
struct addrinfo* res;
char host[NI_MAXHOST];

Expand Down Expand Up @@ -227,7 +227,7 @@ bool ResolveIpv4FromHostname(const std::string& hostname, ipv4_t* addr) {
bool ResolveIpv6FromHostname(const std::string& hostname, ipv6_t* addr) {
if (internal::g_hosts_map->FindIpv6OfHostname(hostname, addr)) return true;

struct addrinfo hints {};
struct addrinfo hints{};
struct addrinfo *res, *tmp;
char host[NI_MAXHOST];

Expand All @@ -245,13 +245,22 @@ bool ResolveIpv6FromHostname(const std::string& hostname, ipv6_t* addr) {
nullptr, 0, NI_NUMERICHOST);
auto ipv6_sockaddr = (struct sockaddr_in6*)tmp->ai_addr;
if (ret == 0) {
if (IN6_IS_ADDR_LINKLOCAL(&ipv6_sockaddr->sin6_addr)) {
// Check if it is a link-local address, which must have a scope ID (e.g.
// %eth0). We don't support link-local address as it's too complex to
// handle.
CRANE_TRACE("Skipping link-local address of {}", hostname);
continue;
}

*addr = 0;
for (int i = 0; i < 4; ++i) {
*addr = 0;
uint32_t part = ntohl(ipv6_sockaddr->sin6_addr.s6_addr32[i]);
*addr = (*addr << 32) | part;
}
} else
CRANE_ERROR("error getnameinfo {}", gai_strerror(ret));
} else {
CRANE_ERROR("Error in getnameinfo {}", gai_strerror(ret));
}
}

freeaddrinfo(res);
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/PublicHeader/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void SetCloseOnExecFromFd(int fd_begin) {
}

bool SetMaxFileDescriptorNumber(unsigned long num) {
struct rlimit rlim {};
struct rlimit rlim{};
rlim.rlim_cur = num;
rlim.rlim_max = num;

Expand Down

0 comments on commit 815c797

Please sign in to comment.