Skip to content

Expose remote port api #1854

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ struct AsyncSocket {
return addressAsText(getRemoteAddress());
}

/* Returns the remote port number or -1 on failure */
unsigned int getRemotePort() {
int port = us_socket_remote_port(SSL, (us_socket_t *) this);
return (unsigned int) port;
}

/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
* Returns pair of bytes written (anywhere) and whether or not this call resulted in the polling for
* writable (or we are in a state that implies polling for writable). */
Expand Down
5 changes: 5 additions & 0 deletions src/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ struct HttpResponse : public AsyncSocket<SSL> {
std::string_view getProxiedRemoteAddressAsText() {
return Super::addressAsText(getProxiedRemoteAddress());
}

unsigned int getProxiedRemotePort() {
return getHttpResponseData()->proxyParser.getSourcePort();
}
#endif

/* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler.
Expand Down Expand Up @@ -355,6 +359,7 @@ struct HttpResponse : public AsyncSocket<SSL> {
/* See AsyncSocket */
using Super::getRemoteAddress;
using Super::getRemoteAddressAsText;
using Super::getRemotePort;
using Super::getNativeHandle;

/* Throttle reads and writes */
Expand Down
16 changes: 16 additions & 0 deletions src/ProxyParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ struct ProxyParser {
}
}

unsigned int getSourcePort() {

// UNSPEC family and protocol
if (family == 0) {
return {};
}

if ((family & 0xf0) >> 4 == 1) {
/* Family 1 is INET4 */
return addr.ipv4_addr.src_port;
} else {
/* Family 2 is INET6 */
return addr.ipv6_addr.src_port;
}
}

/* Returns [done, consumed] where done = false on failure */
std::pair<bool, unsigned int> parse(std::string_view data) {

Expand Down
1 change: 1 addition & 0 deletions src/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct WebSocket : AsyncSocket<SSL> {
using Super::getBufferedAmount;
using Super::getRemoteAddress;
using Super::getRemoteAddressAsText;
using Super::getRemotePort;
using Super::getNativeHandle;

/* WebSocket close cannot be an alias to AsyncSocket::close since
Expand Down