From 41ba3c9b8c0013aa3af18d523b5d788f5321cb8a Mon Sep 17 00:00:00 2001 From: Jake Smith Date: Fri, 18 Oct 2024 14:43:41 +0100 Subject: [PATCH] HPCC-32829 Avoid getPeerEndpoint refetching peer name When a socket is attached the peer is captured in existing member, reuse them in getPeerEndpoint/getPeerAddress. This also means that these calls will continue to work after the socket is closed. Therefore tracing of the peer on a closed socket will now be valid. Signed-off-by: Jake Smith --- system/jlib/jsocket.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/system/jlib/jsocket.cpp b/system/jlib/jsocket.cpp index b75500c9cb9..5dfd4acde52 100644 --- a/system/jlib/jsocket.cpp +++ b/system/jlib/jsocket.cpp @@ -609,10 +609,10 @@ class CSocket: public ISocket, public CInterface enum { ss_open, ss_shutdown, ss_close, ss_pre_open } state; T_SOCKET sock; // char* hostname; // host address - unsigned short hostport; // host port + unsigned short hostport; // host port (NB: this is the peer port if an attached socket) unsigned short localPort; SOCKETMODE sockmode; - IpAddress targetip; + IpAddress targetip; // NB: this is peer if an attached socket SocketEndpoint returnep; // set by set_return_addr MCASTREQ * mcastreq; @@ -1472,16 +1472,8 @@ SocketEndpoint &CSocket::getPeerEndpoint(SocketEndpoint &ep) if (sockmode==sm_udp_server) { // udp server ep.set(returnep); } - else { - DEFINE_SOCKADDR(u); - socklen_t ul = sizeof(u); - if (::getpeername(sock,&u.sa, &ul)<0) { - DBGLOG("getpeername failed %d",SOCKETERRNO()); - ep.set(NULL, 0); - } - else - getSockAddrEndpoint(u,ul,ep); - } + else + ep.set(hostport, targetip); // NB: if this is an attached socket, targetip/hostpost are the peer return ep; }