Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,15 @@ struct AsyncSocket {

/* Returns the remote IP address or empty string on failure */
std::string_view getRemoteAddress() {
#ifdef UWS_REMOTE_ADDRESS_USERSPACE
AsyncSocketData<SSL> *data = getAsyncSocketData();
return std::string_view(data->remoteAddress, (unsigned int) data->remoteAddressLength);
#else
static thread_local char buf[16];
int ipLength = 16;
us_socket_remote_address(SSL, (us_socket_t *) this, buf, &ipLength);
return std::string_view(buf, (unsigned int) ipLength);
#endif
}

/* Returns the text representation of IP */
Expand Down
6 changes: 6 additions & 0 deletions src/AsyncSocketData.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ struct AsyncSocketData {
/* This will do for now */
BackPressure buffer;

#ifdef UWS_REMOTE_ADDRESS_USERSPACE
/* Cache for remote address, populated on socket open */
char remoteAddress[16];
int remoteAddressLength = 0;
#endif

/* Allow move constructing us */
AsyncSocketData(BackPressure &&backpressure) : buffer(std::move(backpressure)) {

Expand Down
16 changes: 15 additions & 1 deletion src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,27 @@ struct HttpContext {
/* Init the HttpContext by registering libusockets event handlers */
HttpContext<SSL> *init() {
/* Handle socket connections */
us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int /*is_client*/, char */*ip*/, int /*ip_length*/) {
us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int /*is_client*/, char *ip, int ip_length) {
/* Any connected socket should timeout until it has a request */
us_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S);

/* Init socket ext */
new (us_socket_ext(SSL, s)) HttpResponseData<SSL>;

#ifdef UWS_REMOTE_ADDRESS_USERSPACE
/* Copy remote address into per-socket cache for later retrieval */
AsyncSocketData<SSL> *asyncSocketData = (AsyncSocketData<SSL> *) us_socket_ext(SSL, s);
if (ip_length > 0 && ip_length <= 16) {
memcpy(asyncSocketData->remoteAddress, ip, (size_t) ip_length);
asyncSocketData->remoteAddressLength = ip_length;
} else {
asyncSocketData->remoteAddressLength = 0;
}
#else
(void) ip;
(void) ip_length;
#endif

/* Call filter */
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
for (auto &f : httpContextData->filterHandlers) {
Expand Down
Loading