From b9dd9e8e6c40a058b16410e3a4f516dff07d0fb6 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Tue, 8 Dec 2015 22:07:26 -0500 Subject: [PATCH] getRemoteName() Cache the TCP peer address as a string and pass it to createChannel(). --- src/remote/blockingUDP.h | 5 +++++ src/remote/blockingUDPTransport.cpp | 5 +++++ src/remote/codec.cpp | 35 +++++++++++------------------ src/remote/codec.h | 4 ++++ src/remote/remote.h | 2 ++ src/server/responseHandlers.cpp | 2 +- testApp/remote/testCodec.cpp | 2 ++ 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/remote/blockingUDP.h b/src/remote/blockingUDP.h index d8ab0ea..554204b 100644 --- a/src/remote/blockingUDP.h +++ b/src/remote/blockingUDP.h @@ -73,6 +73,10 @@ namespace epics { return &_remoteAddress; } + virtual const std::string& getRemoteName() const { + return _remoteName; + } + virtual std::string getType() const { return std::string("udp"); } @@ -329,6 +333,7 @@ namespace epics { * Remote address. */ osiSockAddr _remoteAddress; + std::string _remoteName; /** * Send addresses. diff --git a/src/remote/blockingUDPTransport.cpp b/src/remote/blockingUDPTransport.cpp index e735f3e..36dabdc 100644 --- a/src/remote/blockingUDPTransport.cpp +++ b/src/remote/blockingUDPTransport.cpp @@ -71,6 +71,11 @@ inline int sendto(int s, const char *buf, size_t len, int flags, const struct so char strBuffer[64]; epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); LOG(logLevelDebug, "getsockname error: %s.", strBuffer); + _remoteName = ":0"; + } else { + char strBuffer[64]; + sockAddrToA(&_remoteAddress.sa, strBuffer, sizeof(strBuffer)); + _remoteName = strBuffer; } } diff --git a/src/remote/codec.cpp b/src/remote/codec.cpp index 7036d1a..f886c0a 100644 --- a/src/remote/codec.cpp +++ b/src/remote/codec.cpp @@ -1201,7 +1201,13 @@ namespace epics { LOG(logLevelError, "Error fetching socket remote address: %s.", errStr); + _socketName = ":0"; + } else { + char ipAddrStr[64]; + ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); + _socketName = ipAddrStr; } + } // must be called only once, when there will be no operation on socket (e.g. just before tx/rx thread exists) @@ -1443,7 +1449,6 @@ namespace epics { sendBufferSize, receiveBufferSize, PVA_DEFAULT_PRIORITY), _lastChannelSID(0), _verifyOrVerified(false), _securityRequired(false) { - // NOTE: priority not yet known, default priority is used to //register/unregister // TODO implement priorities in Reactor... not that user will @@ -1589,12 +1594,10 @@ namespace epics { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[64]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); LOG( logLevelDebug, "Transport to %s still has %zd channel(s) active and closing...", - ipAddrStr, _channels.size()); + _socketName.c_str(), _channels.size()); } std::map::iterator it = _channels.begin(); @@ -1614,9 +1617,7 @@ namespace epics { { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); - LOG(logLevelDebug, "Authentication completed with status '%s' for PVA client: %s.", Status::StatusTypeName[status.getType()], ipAddrStr); + LOG(logLevelDebug, "Authentication completed with status '%s' for PVA client: %s.", Status::StatusTypeName[status.getType()], _socketName.c_str()); } if (!isVerified()) // TODO sync @@ -1663,9 +1664,7 @@ namespace epics { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); - LOG(logLevelDebug, "No security plug-in installed, selecting default plug-in '%s' for PVA client: %s.", securityPlugin->getId().c_str(), ipAddrStr); + LOG(logLevelDebug, "No security plug-in installed, selecting default plug-in '%s' for PVA client: %s.", securityPlugin->getId().c_str(), _socketName.c_str()); } } } @@ -1689,9 +1688,7 @@ namespace epics { } catch (SecurityException &se) { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); - LOG(logLevelDebug, "Security plug-in '%s' failed to create a session for PVA client: %s.", initData->securityPluginName.c_str(), ipAddrStr); + LOG(logLevelDebug, "Security plug-in '%s' failed to create a session for PVA client: %s.", initData->securityPluginName.c_str(), _socketName.c_str()); } Status status(Status::STATUSTYPE_ERROR, se.what()); verified(status); @@ -1792,9 +1789,7 @@ namespace epics { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); - LOG(logLevelDebug, "Acquiring transport to %s.", ipAddrStr); + LOG(logLevelDebug, "Acquiring transport to %s.", _socketName.c_str()); } _owners[client->getID()] = TransportClient::weak_pointer(client); @@ -1829,12 +1824,10 @@ namespace epics { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); LOG( logLevelDebug, "Transport to %s still has %d client(s) active and closing...", - ipAddrStr, refs); + _socketName.c_str(), refs); } TransportClientMap_t::iterator it = _owners.begin(); @@ -1858,9 +1851,7 @@ namespace epics { if (IS_LOGGABLE(logLevelDebug)) { - char ipAddrStr[48]; - ipAddrToDottedIP(&_socketAddress.ia, ipAddrStr, sizeof(ipAddrStr)); - LOG(logLevelDebug, "Releasing TCP transport to %s.", ipAddrStr); + LOG(logLevelDebug, "Releasing TCP transport to %s.", _socketName.c_str()); } _owners.erase(clientID); diff --git a/src/remote/codec.h b/src/remote/codec.h index e01d863..07a7410 100644 --- a/src/remote/codec.h +++ b/src/remote/codec.h @@ -348,6 +348,7 @@ namespace epics { SOCKET _channel; osiSockAddr _socketAddress; + std::string _socketName; }; @@ -393,6 +394,9 @@ namespace epics { return &_socketAddress; } + const std::string& getRemoteName() const { + return _socketName; + } epics::pvData::int8 getRevision() const { return PVA_PROTOCOL_REVISION; diff --git a/src/remote/remote.h b/src/remote/remote.h index 28f7133..c05306e 100644 --- a/src/remote/remote.h +++ b/src/remote/remote.h @@ -199,6 +199,8 @@ namespace epics { */ virtual const osiSockAddr* getRemoteAddress() const = 0; + virtual const std::string& getRemoteName() const = 0; + // TODO getContext? /** diff --git a/src/server/responseHandlers.cpp b/src/server/responseHandlers.cpp index b6fe912..d1db8c0 100644 --- a/src/server/responseHandlers.cpp +++ b/src/server/responseHandlers.cpp @@ -760,7 +760,7 @@ ChannelRequester::shared_pointer ServerChannelRequesterImpl::create( std::tr1::shared_ptr tp(new ServerChannelRequesterImpl(transport, channelName, cid, css)); ChannelRequester::shared_pointer cr = tp; // TODO exception guard and report error back - provider->createChannel(channelName, cr, transport->getPriority()); + provider->createChannel(channelName, cr, transport->getPriority(), transport->getRemoteName()); return cr; } diff --git a/testApp/remote/testCodec.cpp b/testApp/remote/testCodec.cpp index 9afa90d..77115de 100644 --- a/testApp/remote/testCodec.cpp +++ b/testApp/remote/testCodec.cpp @@ -350,6 +350,8 @@ namespace epics { } const osiSockAddr* getRemoteAddress() const { return 0; } + std::string dummyRemoteName; + const std::string& getRemoteName() const {return dummyRemoteName;} epics::pvData::int8 getRevision() const {