diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index add34b8..2ae7c10 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -87,6 +87,14 @@ protected: return getAsyncSocketData()->buffer.size(); } + /* Returns the remote IP address or empty string on failure */ + std::string_view getRemoteAddress() { + static thread_local char buf[16]; + int ipLength = 16; + us_new_socket_remote_address(SSL, (us_new_socket_t *) this, buf, &ipLength); + return std::string_view(buf, ipLength); + } + /* 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). */ diff --git a/src/HttpContext.h b/src/HttpContext.h index bda67dd..5964d58 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -59,7 +59,7 @@ private: /* Init the HttpContext by registering libusockets event handlers */ HttpContext *init() { /* Handle socket connections */ - us_new_socket_context_on_open(SSL, getSocketContext(), [](auto *s, int is_client) { + us_new_socket_context_on_open(SSL, getSocketContext(), [](auto *s, int is_client, char *ip, int ip_length) { /* Any connected socket should timeout until it has a request */ us_new_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S); diff --git a/src/HttpResponse.h b/src/HttpResponse.h index a07d19d..44902b9 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -167,6 +167,8 @@ public: /* Immediately terminate this Http response */ using Super::close; + using Super::getRemoteAddress; + /* Note: Headers are not checked in regards to timeout. * We only check when you actively push data or end the request */ diff --git a/src/WebSocket.h b/src/WebSocket.h index 1b47580..3a830f8 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -48,6 +48,7 @@ public: /* See AsyncSocket */ using Super::getBufferedAmount; + using Super::getRemoteAddress; /* Simple, immediate close of the socket. Emits close event */ using Super::close; diff --git a/uSockets b/uSockets index a83f842..824ff97 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit a83f8423af5b4ae088ddb2996a115bab895dd4c3 +Subproject commit 824ff9708bac06093278ae0c23f3c0837fce22c8