diff options
Diffstat (limited to 'src/deps/libuwsockets.cpp')
-rw-r--r-- | src/deps/libuwsockets.cpp | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/deps/libuwsockets.cpp b/src/deps/libuwsockets.cpp index da38bcebb..79dd68b42 100644 --- a/src/deps/libuwsockets.cpp +++ b/src/deps/libuwsockets.cpp @@ -1,8 +1,7 @@ +// clang-format off #include "_libusockets.h" - #include <bun-uws/src/App.h> #include <bun-uws/src/AsyncSocket.h> - #include <bun-usockets/src/internal/internal.h> #include <string_view> @@ -1577,4 +1576,29 @@ extern "C" s->context->loop->data.last_write_failed = 1; us_poll_change(&s->p, s->context->loop, LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE); } + + uint64_t uws_res_get_remote_address_info(uws_res_t *res, const char **dest, int *port, bool *is_ipv6) + { + // This function is manual inlining + modification of + // us_socket_remote_address + // AsyncSocket::getRemoteAddress + // To get { ip, port, is_ipv6 } for Bun.serve().requestIP() + // AsyncSocket::addressAsText + static thread_local char b[64]; + auto length = us_get_remote_address_info(b, (us_socket_t *)res, dest, port, (int*)is_ipv6); + + if (length == 4) { + length = sprintf(b, "%u.%u.%u.%u", b[0], b[1], b[2], b[3]); + *is_ipv6 = false; + } else { + length = sprintf(b, "%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]); + *is_ipv6 = true; + } + + *dest = b; + + return (unsigned int) length; + } } |