addressAsText, getRemoteProxiedAddress

This commit is contained in:
Alex Hultman
2020-06-06 19:52:28 +02:00
parent 2538cd18e9
commit d5782fdb9c
6 changed files with 46 additions and 15 deletions
+24 -1
View File
@@ -1,5 +1,5 @@
/*
* Authored by Alex Hultman, 2018-2019.
* Authored by Alex Hultman, 2018-2020.
* Intellectual property of third-party.
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -95,6 +95,24 @@ protected:
return (int) getAsyncSocketData()->buffer.size();
}
/* Returns the text representation of an IPv4 or IPv6 address */
std::string_view addressAsText(std::string_view binary) {
static thread_local char buf[64];
int ipLength = 0;
unsigned char *b = (unsigned char *) binary.data();
if (binary.length() == 4) {
ipLength = sprintf(buf, "%u.%u.%u.%u", b[0], b[1], b[2], b[3]);
} else {
ipLength = sprintf(buf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11],
b[12], b[13], b[14], b[15]);
}
return {buf, ipLength};
}
/* Returns the remote IP address or empty string on failure */
std::string_view getRemoteAddress() {
static thread_local char buf[16];
@@ -103,6 +121,11 @@ protected:
return std::string_view(buf, ipLength);
}
/* Returns the text representation of IP */
std::string_view getRemoteAddressAsText() {
return addressAsText(getRemoteAddress());
}
/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
* Returns pair of bytes written (anywhere) and wheter or not this call resulted in the polling for
* writable (or we are in a state that implies polling for writable). */