This is Unofficial EPICS BASE Doxygen Site
hostNameCache.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /*
11  *
12  *
13  * L O S A L A M O S
14  * Los Alamos National Laboratory
15  * Los Alamos, New Mexico 87545
16  *
17  * Copyright, 1986, The Regents of the University of California.
18  *
19  *
20  * Author Jeffrey O. Hill
21  * johill@lanl.gov
22  */
23 
24 #include <algorithm>
25 #include <cstring>
26 
27 #define epicsAssertAuthor "Jeff Hill johill@lanl.gov"
28 #include "hostNameCache.h"
29 #include "epicsGuard.h"
30 
32  const osiSockAddr & addr, ipAddrToAsciiEngine & engine ) :
33  dnsTransaction ( engine.createTransaction() ), nameLength ( 0 )
34 {
35  sockAddrToDottedIP ( &addr.sa, hostNameBuf, sizeof ( hostNameBuf ) );
36  hostNameBuf[ sizeof ( hostNameBuf ) - 1 ] = '\0';
37  nameLength = strlen ( hostNameBuf );
38  this->dnsTransaction.ipAddrToAscii ( addr, *this );
39 }
40 
42 {
43  delete this;
44 }
45 
47 {
48  this->dnsTransaction.release ();
49 }
50 
51 void hostNameCache::transactionComplete ( const char * pHostNameIn )
52 {
53  epicsGuard < epicsMutex > guard ( this->mutex );
54  // a few legacy clients have a direct pointer to this buffer so we
55  // set the entrire string to nill terminators before we start copying
56  // in the name (this reduces the chance that another thread will see
57  // garbage characters).
58  size_t newNameLen = strlen ( pHostNameIn );
59  if ( newNameLen > sizeof ( this->hostNameBuf ) - 1u ) {
60  newNameLen = sizeof ( this->hostNameBuf ) - 1u;
61  }
62  strncpy ( this->hostNameBuf, "", sizeof ( this->hostNameBuf ) );
63  strncpy ( this->hostNameBuf, pHostNameIn, sizeof ( this->hostNameBuf ) - 1 );
64  this->nameLength = newNameLen;
65 }
66 
68  char * pBuf, unsigned bufSize ) const
69 {
70  if ( bufSize == 0u ) {
71  return 0u;
72  }
73  epicsGuard < epicsMutex > guard ( this->mutex );
74  if ( this->nameLength > 0u ) {
75  if ( this->nameLength < bufSize ) {
76  strcpy ( pBuf, this->hostNameBuf );
77  return this->nameLength;
78  }
79  else {
80  unsigned reducedSize = bufSize - 1u;
81  strncpy ( pBuf, this->hostNameBuf, reducedSize );
82  pBuf [ reducedSize ] = '\0';
83  return reducedSize;
84  }
85  }
86  else {
87  osiSockAddr tmpAddr = this->dnsTransaction.address ();
88  return sockAddrToDottedIP ( &tmpAddr.sa, pBuf, bufSize );
89  }
90 }
91 
unsigned epicsStdCall sockAddrToDottedIP(const struct sockaddr *paddr, char *pBuf, unsigned bufSize)
Definition: osiSock.c:118
virtual void ipAddrToAscii(const osiSockAddr &, ipAddrToAsciiCallBack &)=0
struct sockaddr sa
Definition: osiSock.h:158
virtual osiSockAddr address() const =0
hostNameCache(const osiSockAddr &addr, ipAddrToAsciiEngine &engine)
void transactionComplete(const char *pHostName)
unsigned getName(char *pBuf, unsigned bufLength) const
virtual void release()=0