-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
report: add fallback for uv_getnameinfo() failures #26140
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,42 @@ using node::MallocedBuffer; | |
|
||
static constexpr auto null = JSONWriter::Null{}; | ||
|
||
// Utility function to format socket information. | ||
static bool ReportEndpoint(uv_handle_t* h, | ||
struct sockaddr* addr, | ||
const char* name, | ||
JSONWriter* writer) { | ||
uv_getnameinfo_t endpoint; | ||
if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) { | ||
writer->json_objectstart(name); | ||
writer->json_keyvalue("host", endpoint.host); | ||
writer->json_keyvalue("port", endpoint.service); | ||
writer->json_objectend(); | ||
return true; | ||
} else { | ||
char host[INET6_ADDRSTRLEN]; | ||
int family = addr->sa_family; | ||
if (uv_inet_ntop(family, addr, host, sizeof(host)) == 0) { | ||
std::string port = std::to_string(ntohs(family == AF_INET ? | ||
reinterpret_cast<sockaddr_in*>(addr)->sin_port : | ||
reinterpret_cast<sockaddr_in6*>(addr)->sin6_port)); | ||
writer->json_objectstart(name); | ||
writer->json_keyvalue("host", host); | ||
writer->json_keyvalue("port", port); | ||
writer->json_objectend(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// Utility function to format libuv socket information. | ||
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) { | ||
struct sockaddr_storage addr_storage; | ||
struct sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); | ||
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h); | ||
int addr_size = sizeof(addr_storage); | ||
int rc = -1; | ||
bool wrote_local_endpoint = false; | ||
bool wrote_remote_endpoint = false; | ||
|
||
switch (h->type) { | ||
case UV_UDP: | ||
|
@@ -27,38 +54,17 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) { | |
default: | ||
break; | ||
} | ||
if (rc == 0) { | ||
// uv_getnameinfo will format host and port and handle IPv4/IPv6. | ||
uv_getnameinfo_t local; | ||
rc = uv_getnameinfo(h->loop, &local, nullptr, addr, NI_NUMERICSERV); | ||
|
||
if (rc == 0) { | ||
writer->json_objectstart("localEndpoint"); | ||
writer->json_keyvalue("host", local.host); | ||
writer->json_keyvalue("port", local.service); | ||
writer->json_objectend(); | ||
wrote_local_endpoint = true; | ||
} | ||
if (rc != 0 || !ReportEndpoint(h, addr, "localEndpoint", writer)) { | ||
writer->json_keyvalue("localEndpoint", null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move this into if (rc != 0)
ReportEndpoint(h, addr, "localEndpoint", writer); Same for the remote endpoint code below. |
||
} | ||
if (!wrote_local_endpoint) writer->json_keyvalue("localEndpoint", null); | ||
|
||
if (h->type == UV_TCP) { | ||
// Get the remote end of the connection. | ||
rc = uv_tcp_getpeername(&handle->tcp, addr, &addr_size); | ||
if (rc == 0) { | ||
uv_getnameinfo_t remote; | ||
rc = uv_getnameinfo(h->loop, &remote, nullptr, addr, NI_NUMERICSERV); | ||
|
||
if (rc == 0) { | ||
writer->json_objectstart("remoteEndpoint"); | ||
writer->json_keyvalue("host", remote.host); | ||
writer->json_keyvalue("port", remote.service); | ||
writer->json_objectend(); | ||
wrote_local_endpoint = true; | ||
} | ||
if (rc != 0 || !ReportEndpoint(h, addr, "remoteEndpoint", writer)) { | ||
writer->json_keyvalue("remoteEndpoint", null); | ||
} | ||
} | ||
if (!wrote_remote_endpoint) writer->json_keyvalue("remoteEndpoint", null); | ||
} | ||
|
||
// Utility function to format libuv path information. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to represent the port as an integer? That would allow skipping the stringifying step, but I imagine we’d also need to do that in the
uv_getnameinfo()
case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it probably does make sense to be an integer. I've stringified it here for consistency with the existing
uv_getnameinfo()
case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you just assign the host and port in the conditionals, then you can DRY the writer code a bit.