From 085b228c830f3826b8587d3668086d39fc9687b9 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Tue, 19 Oct 2004 20:24:57 +0000 Subject: [PATCH] getName() returns string size --- src/ca/hostNameCache.cpp | 33 +++++++++++++++++++++++---------- src/ca/hostNameCache.h | 12 +++++++++--- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/ca/hostNameCache.cpp b/src/ca/hostNameCache.cpp index 9d3eea46a..b4fb5d2d6 100644 --- a/src/ca/hostNameCache.cpp +++ b/src/ca/hostNameCache.cpp @@ -29,8 +29,9 @@ #include "hostNameCache.h" #include "epicsGuard.h" -hostNameCache::hostNameCache ( const osiSockAddr & addr, ipAddrToAsciiEngine & engine ) : - dnsTransaction ( engine.createTransaction() ), ioComplete ( false ) +hostNameCache::hostNameCache ( + const osiSockAddr & addr, ipAddrToAsciiEngine & engine ) : + dnsTransaction ( engine.createTransaction() ), nameLength ( 0 ) { this->dnsTransaction.ipAddrToAscii ( addr, *this ); } @@ -45,26 +46,38 @@ hostNameCache::~hostNameCache () this->dnsTransaction.release (); } -void hostNameCache::transactionComplete ( const char *pHostNameIn ) +void hostNameCache::transactionComplete ( const char * pHostNameIn ) { epicsGuard < epicsMutex > guard ( this->mutex ); - if ( ! this->ioComplete ) { - this->ioComplete = true; + if ( this->nameLength == 0u ) { strncpy ( this->hostNameBuf, pHostNameIn, sizeof ( this->hostNameBuf ) ); this->hostNameBuf[ sizeof ( this->hostNameBuf ) - 1 ] = '\0'; + this->nameLength = strlen ( this->hostNameBuf ); } } -void hostNameCache::hostName ( char *pBuf, unsigned bufSize ) const +unsigned hostNameCache::getName ( + char * pBuf, unsigned bufSize ) const { + if ( bufSize == 0u ) { + return 0u; + } epicsGuard < epicsMutex > guard ( this->mutex ); - if ( this->ioComplete ) { - strncpy ( pBuf, this->hostNameBuf, bufSize); - pBuf [ bufSize - 1u ] = '\0'; + if ( this->nameLength > 0u ) { + if ( this->nameLength < bufSize ) { + strcpy ( pBuf, this->hostNameBuf ); + return this->nameLength; + } + else { + unsigned reducedSize = bufSize - 1u; + strncpy ( pBuf, this->hostNameBuf, reducedSize ); + pBuf [ reducedSize ] = '\0'; + return reducedSize; + } } else { osiSockAddr tmpAddr = this->dnsTransaction.address (); - sockAddrToDottedIP ( &tmpAddr.sa, pBuf, bufSize ); + return sockAddrToDottedIP ( &tmpAddr.sa, pBuf, bufSize ); } } diff --git a/src/ca/hostNameCache.h b/src/ca/hostNameCache.h index 5acfd99b8..a4eacfbb3 100644 --- a/src/ca/hostNameCache.h +++ b/src/ca/hostNameCache.h @@ -40,16 +40,22 @@ class hostNameCache : public ipAddrToAsciiCallBack { public: - hostNameCache ( const osiSockAddr &addr, ipAddrToAsciiEngine &engine ); + hostNameCache ( const osiSockAddr & addr, ipAddrToAsciiEngine & engine ); ~hostNameCache (); void destroy (); void transactionComplete ( const char * pHostName ); - void hostName ( char *pBuf, unsigned bufLength ) const; + unsigned getName ( char *pBuf, unsigned bufLength ) const; + const char * pointer () const; private: char hostNameBuf [128]; mutable epicsMutex mutex; ipAddrToAsciiTransaction & dnsTransaction; - bool ioComplete; + unsigned nameLength; }; +inline const char * hostNameCache::pointer () const +{ + return this->hostNameBuf; +} + #endif // #ifndef hostNameCacheh