Skip to content

Commit c7b37fc

Browse files
Expose remote port api (#1854)
1 parent d81295c commit c7b37fc

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/AsyncSocket.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ struct AsyncSocket {
230230
return addressAsText(getRemoteAddress());
231231
}
232232

233+
/* Returns the remote port number or -1 on failure */
234+
unsigned int getRemotePort() {
235+
int port = us_socket_remote_port(SSL, (us_socket_t *) this);
236+
return (unsigned int) port;
237+
}
238+
233239
/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
234240
* Returns pair of bytes written (anywhere) and whether or not this call resulted in the polling for
235241
* writable (or we are in a state that implies polling for writable). */

src/HttpResponse.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ struct HttpResponse : public AsyncSocket<SSL> {
232232
std::string_view getProxiedRemoteAddressAsText() {
233233
return Super::addressAsText(getProxiedRemoteAddress());
234234
}
235+
236+
unsigned int getProxiedRemotePort() {
237+
return getHttpResponseData()->proxyParser.getSourcePort();
238+
}
235239
#endif
236240

237241
/* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler.
@@ -358,6 +362,7 @@ struct HttpResponse : public AsyncSocket<SSL> {
358362
/* See AsyncSocket */
359363
using Super::getRemoteAddress;
360364
using Super::getRemoteAddressAsText;
365+
using Super::getRemotePort;
361366
using Super::getNativeHandle;
362367

363368
/* Throttle reads and writes */

src/ProxyParser.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ struct ProxyParser {
9191
}
9292
}
9393

94+
unsigned int getSourcePort() {
95+
96+
// UNSPEC family and protocol
97+
if (family == 0) {
98+
return {};
99+
}
100+
101+
if ((family & 0xf0) >> 4 == 1) {
102+
/* Family 1 is INET4 */
103+
return addr.ipv4_addr.src_port;
104+
} else {
105+
/* Family 2 is INET6 */
106+
return addr.ipv6_addr.src_port;
107+
}
108+
}
109+
94110
/* Returns [done, consumed] where done = false on failure */
95111
std::pair<bool, unsigned int> parse(std::string_view data) {
96112

src/WebSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct WebSocket : AsyncSocket<SSL> {
5858
using Super::getBufferedAmount;
5959
using Super::getRemoteAddress;
6060
using Super::getRemoteAddressAsText;
61+
using Super::getRemotePort;
6162
using Super::getNativeHandle;
6263

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

0 commit comments

Comments
 (0)