Skip to content

Commit

Permalink
HPCC-17017 Add port info to socket tracename
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Kelly <mark.kelly@lexisnexisrisk.com>
  • Loading branch information
mckellyln committed Jul 7, 2017
1 parent d725a5e commit da27589
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
56 changes: 22 additions & 34 deletions system/jlib/jsocket.cpp
Expand Up @@ -406,10 +406,11 @@ class CSocket: public ISocket, public CInterface
int wait_read(unsigned timeout);
void logPollError(unsigned revents, const char *rwstr);
int wait_write(unsigned timeout);
int name(char *name, size32_t namemax, bool oktothrow=true);
int name(char *name,size32_t namemax);
int peer_name(char *name,size32_t namemax);
SocketEndpoint &getPeerEndpoint(SocketEndpoint &ep);
IpAddress & getPeerAddress(IpAddress &addr);
SocketEndpoint &getEndpoint(SocketEndpoint &ep);
void set_return_addr(int port,const char *name); // sets returnep
void cancel_accept();
size32_t get_max_send_size();
Expand Down Expand Up @@ -894,8 +895,8 @@ int CSocket::post_connect ()
nagling = true;
set_nagle(false);
state = ss_open;
char lname[256];
localPort = name(lname, sizeof(lname), false);
SocketEndpoint ep;
localPort = getEndpoint(ep).port;
}
else if ((err!=JSE_TIMEDOUT)&&(err!=JSE_CONNREFUSED)) // handled by caller
LOGERR2(err,1,"post_connect");
Expand Down Expand Up @@ -1065,35 +1066,11 @@ void CSocket::set_keep_alive(bool set)
}


int CSocket::name(char *retname, size32_t namemax, bool oktothrow)
int CSocket::name(char *retname,size32_t namemax)
{
if (!retname)
namemax = 0;
if (namemax)
retname[0] = 0;
if (state != ss_open)
{
if (oktothrow)
{
THROWJSOCKEXCEPTION(JSOCKERR_not_opened);
}
else
return 0;
}
DEFINE_SOCKADDR(u);
socklen_t ul = sizeof(u);
if (::getsockname(sock,&u.sa, &ul)<0)
{
if (oktothrow)
{
THROWJSOCKEXCEPTION(ERRNO());
}
else
return 0;
}
SocketEndpoint ep;
getSockAddrEndpoint(u,ul,ep);
if (namemax>=1)
getEndpoint(ep);
if (retname && namemax)
{
StringBuffer s;
ep.getIpText(s);
Expand Down Expand Up @@ -1178,6 +1155,19 @@ void CSocket::set_return_addr(int port,const char *retname)
}


SocketEndpoint &CSocket::getEndpoint(SocketEndpoint &ep)
{
if (state != ss_open) {
THROWJSOCKEXCEPTION(JSOCKERR_not_opened);
}
DEFINE_SOCKADDR(u);
socklen_t ul = sizeof(u);
if (::getsockname(sock,&u.sa, &ul)<0) {
THROWJSOCKEXCEPTION(ERRNO());
}
getSockAddrEndpoint(u,ul,ep);
return ep;
}

void CSocket::cancel_accept()
{
Expand Down Expand Up @@ -2690,9 +2680,7 @@ CSocket::CSocket(T_SOCKET new_sock,SOCKETMODE smode,bool _owned)
sock = new_sock;
if (new_sock!=INVALID_SOCKET)
STATS.activesockets++;
hostname = NULL;
mcastreq = NULL;
hostport = 0;
#ifdef _TRACE
tracename = NULL;
#endif
Expand All @@ -2707,8 +2695,8 @@ CSocket::CSocket(T_SOCKET new_sock,SOCKETMODE smode,bool _owned)
char peer[256];
hostport = peer_name(peer,sizeof(peer));
hostname = strdup(peer);
char lname[256];
localPort = name(lname, sizeof(lname), false);
SocketEndpoint ep;
localPort = getEndpoint(ep).port;
#ifdef _TRACE
setTraceName("A!", peer);
#endif
Expand Down
7 changes: 5 additions & 2 deletions system/jlib/jsocket.hpp
Expand Up @@ -319,8 +319,8 @@ class jlib_decl ISocket : extends IInterface
//
virtual void shutdown(unsigned mode=SHUTDOWN_READWRITE) = 0; // not needed for UDP

// Get name of accepted or connected socket and returns port
virtual int name(char *name, size32_t namemax, bool oktothrow=true)=0;
// Get local name of accepted (or connected) socket and returns port
virtual int name(char *name,size32_t namemax)=0;

// Get peer name of socket and returns port - in UDP returns return addr
virtual int peer_name(char *name,size32_t namemax)=0;
Expand All @@ -331,6 +331,9 @@ class jlib_decl ISocket : extends IInterface
// Get peer ip of socket - in UDP returns return addr
virtual IpAddress &getPeerAddress(IpAddress &addr)=0;

// Get local endpoint of socket
virtual SocketEndpoint &getEndpoint(SocketEndpoint &ep)=0;

//
// Close socket
//
Expand Down
12 changes: 9 additions & 3 deletions system/security/securesocket/securesocket.cpp
Expand Up @@ -233,10 +233,10 @@ class CSecureSocket : implements ISecureSocket, public CInterface
m_socket->shutdown(mode);
}

// Get name of accepted socket and returns port
virtual int name(char *name, size32_t namemax, bool oktothrow=true)
// Get local name of accepted (or connected) socket and returns port
virtual int name(char *name,size32_t namemax)
{
return m_socket->name(name, namemax, oktothrow);
return m_socket->name(name, namemax);
}

// Get peer name of socket and returns port - in UDP returns return addr
Expand All @@ -257,6 +257,12 @@ class CSecureSocket : implements ISecureSocket, public CInterface
return m_socket->getPeerAddress(addr);
}

// Get local endpoint of socket
virtual SocketEndpoint &getEndpoint(SocketEndpoint &ep)
{
return m_socket->getEndpoint(ep);
}

//
// Close socket
//
Expand Down

0 comments on commit da27589

Please sign in to comment.