Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Static Private Attributes
CASocketAddrINet Class Reference

This class represents a socket address for Internet (IP) connections. More...

#include <CASocketAddrINet.hpp>

Inheritance diagram for CASocketAddrINet:
[legend]
Collaboration diagram for CASocketAddrINet:
[legend]

List of all members.

Public Member Functions

SINT32 getType () const
 The type (family) of socket for which this address is useful.
 CASocketAddrINet ()
 Must be called once before using one of the CAsocketAddrINet functions.
 CASocketAddrINet (UINT16 port)
 Constructs an IP-Address for port and ANY local host ip.
 CASocketAddrINet (const CASocketAddrINet &addr)
 Constructs an IP-Address from an other IP Adress.
CASocketAddrclone () const
 Creates a copy of the Address.
const SOCKADDRLPSOCKADDR () const
 Makes a cast to struct SOCKADDR*.
SINT32 getSize () const
 Returns the Size of the SOCKADDR struct used.
SINT32 setAddr (const UINT8 *szIP, UINT16 port)
 Sets the address to szIP and port.
SINT32 setIP (UINT8 ip[4])
 Sets the IP-Numbers for this address.
SINT32 setPort (UINT16 port)
 Changes only(!) the port value of the address.
UINT16 getPort () const
 Returns the port value of the address.
SINT32 getHostName (UINT8 *buff, UINT32 len) const
 Returns the hostname for this address.
SINT32 getIP (UINT8 buff[4]) const
 Returns the IP-Numbers for this address.
SINT32 getIPAsStr (UINT8 *buff, UINT32 len) const
 Returns the IP-Number as an address string (doted-format).
bool isAnyIP ()
bool equalsIP (UINT8 ip[4]) const
virtual SINT32 toString (UINT8 *buff, UINT32 bufflen) const
 Returns a human readable representation of this address.

Static Public Member Functions

static SINT32 init ()
static SINT32 cleanup ()
static SINT32 getLocalHostName (UINT8 *buff, UINT32 len)
 Returns the name of the local host.
static SINT32 getLocalHostIP (UINT8 ip[4])
 Returns the local IP-Address as four bytes.

Static Private Attributes

static CAMutexm_pcsGet = NULL

Detailed Description

This class represents a socket address for Internet (IP) connections.

Definition at line 34 of file CASocketAddrINet.hpp.


Constructor & Destructor Documentation

Must be called once before using one of the CAsocketAddrINet functions.

Should be called if CASocketAddrINEt functions are not longer needed. If needed again init() must be called Constructs a IP-Address for port 0 and ANY local host ip

Definition at line 58 of file CASocketAddrINet.cpp.

References getSize(), and LPSOCKADDR().

Referenced by clone().

  {
    memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
    sin_family=AF_INET;
    sin_addr.s_addr=INADDR_ANY;
    sin_port=0;
  }

Here is the call graph for this function:

Constructs an IP-Address for port and ANY local host ip.

Definition at line 67 of file CASocketAddrINet.cpp.

References getSize(), and LPSOCKADDR().

  {
    memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
    sin_family=AF_INET;
    sin_port=htons(port);
    sin_addr.s_addr=INADDR_ANY;
  }

Here is the call graph for this function:

Constructs an IP-Address from an other IP Adress.

Definition at line 76 of file CASocketAddrINet.cpp.

References getSize(), and LPSOCKADDR().

  {
    memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
    sin_family=AF_INET;
    sin_port=addr.sin_port;
    sin_addr.s_addr=addr.sin_addr.s_addr;
  }

Here is the call graph for this function:


Member Function Documentation

static SINT32 CASocketAddrINet::cleanup ( ) [inline, static]

Definition at line 42 of file CASocketAddrINet.hpp.

References E_SUCCESS, and m_pcsGet.

      {
        delete m_pcsGet;
        m_pcsGet = NULL;
        return E_SUCCESS;
      }
CASocketAddr* CASocketAddrINet::clone ( ) const [inline, virtual]

Creates a copy of the Address.

Implements CASocketAddr.

Definition at line 59 of file CASocketAddrINet.hpp.

References CASocketAddrINet().

        {
          return new CASocketAddrINet(*this);
        }

Here is the call graph for this function:

bool CASocketAddrINet::equalsIP ( UINT8  ip[4]) const [inline]

Definition at line 94 of file CASocketAddrINet.hpp.

        {
          return (memcmp(ip,&sin_addr.s_addr,4)==0);
        }
SINT32 CASocketAddrINet::getHostName ( UINT8 buff,
UINT32  len 
) const

Returns the hostname for this address.

Parameters:
buffbuffer for the returned zero terminated hostname
lenthe size of the buffer
Return values:
E_SUCCESSif no error occured
E_UNKNOWN_HOSTif the name of the host could not be resolved
E_UNSPECIFIEDif buff was NULL
E_SPACEif size of the buffer is to small

Definition at line 145 of file CASocketAddrINet.cpp.

References E_SPACE, E_SUCCESS, E_UNKNOWN_HOST, E_UNSPECIFIED, CAMutex::lock(), m_pcsGet, and CAMutex::unlock().

Referenced by CAAccountingBIInterface::initBIConnection().

  {
    if(buff==NULL)
      return E_UNSPECIFIED;
    SINT32 ret;
    m_pcsGet->lock();
    HOSTENT* hosten=gethostbyaddr((const char*)&sin_addr,4,AF_INET);
    if(hosten==NULL||hosten->h_name==NULL)
      ret=E_UNKNOWN_HOST;
    else if(strlen(hosten->h_name)>=len)
      ret=E_SPACE;
    else
      {
        strcpy((char*)buff,hosten->h_name);
        ret=E_SUCCESS;
      }
    m_pcsGet->unlock();
    return ret;
  }

Here is the call graph for this function:

SINT32 CASocketAddrINet::getIP ( UINT8  buff[4]) const

Returns the IP-Numbers for this address.

Parameters:
buffbuffer for the returned IP-Address (4 Bytes)
Return values:
E_SUCCESSif no error occured

Definition at line 169 of file CASocketAddrINet.cpp.

References E_SUCCESS.

Referenced by getIPAsStr(), isAllowedToPassRestrictions(), and CALastMix::setTargets().

  {
    memcpy(buff,&sin_addr.s_addr,4);
    return E_SUCCESS;
  }
SINT32 CASocketAddrINet::getIPAsStr ( UINT8 buff,
UINT32  len 
) const

Returns the IP-Number as an address string (doted-format).

Parameters:
buffbuffer for the returned IP-Address
lenbuffer-space
Return values:
E_SUCCESSif no error occured

Definition at line 190 of file CASocketAddrINet.cpp.

References E_SUCCESS, E_UNKNOWN, and getIP().

Referenced by CAInfoService::getPaymentInstance(), CAInfoService::sendCascadeHelo(), CAInfoService::sendMixHelo(), CAInfoService::sendStatus(), and toString().

  {
    if(buff==NULL)
      return E_UNKNOWN;
    UINT8 ip[4];
    if(getIP(ip)!=E_SUCCESS)
      return E_UNKNOWN;
    char* strAddr=inet_ntoa(sin_addr);
    if(strAddr==NULL)
      return E_UNKNOWN;
    if(strlen(strAddr)>=len)
      return E_UNKNOWN;
    strcpy((char*)buff,strAddr);
    return E_SUCCESS;
  }

Here is the call graph for this function:

Returns the local IP-Address as four bytes.

Parameters:
ipbuffer for the returned IP-Address
Return values:
E_SUCCESSif no error occurs
E_UNKNOWNin case of an error

Definition at line 244 of file CASocketAddrINet.cpp.

References E_SUCCESS, E_UNKNOWN, CAMutex::lock(), m_pcsGet, and CAMutex::unlock().

  {
    SINT32 ret;
    char buff[256];
    m_pcsGet->lock();
    if(gethostname(buff,256)==-1)
      ret=E_UNKNOWN;
    else
      {
        HOSTENT* hosten=gethostbyname((char*)buff);
        if(hosten==NULL)
          ret=E_UNKNOWN;
        else
          {
            memcpy((char*)ip,hosten->h_addr_list[0],4);
            ret=E_SUCCESS;
          }
      }
    m_pcsGet->unlock();
    return ret;
  }

Here is the call graph for this function:

SINT32 CASocketAddrINet::getLocalHostName ( UINT8 buff,
UINT32  len 
) [static]

Returns the name of the local host.

Parameters:
buffbuffer for the returned zero terminated hostname
lenthe size of the buffer
Return values:
E_SUCCESSif no error occured
E_UNKNOWN_HOSTif the name of the host could not be resolved
E_UNSPECIFIEDif buff was NULL
E_SPACEif size of the buffer is to small

Definition at line 214 of file CASocketAddrINet.cpp.

References E_SPACE, E_SUCCESS, E_UNKNOWN_HOST, E_UNSPECIFIED, CAMutex::lock(), m_pcsGet, and CAMutex::unlock().

  {
    if(buff==NULL)
      return E_UNSPECIFIED;
    SINT32 ret;
    m_pcsGet->lock();
    if(gethostname((char*)buff,len)==-1)
      ret=E_SPACE;
    else
      {
        HOSTENT* hosten=gethostbyname((char*)buff);
        if(hosten==NULL||hosten->h_name==NULL)
          ret=E_UNKNOWN_HOST;
        else if(strlen(hosten->h_name)>=len)
          ret=E_SPACE;
        else
          {
            strcpy((char*)buff,hosten->h_name);
            ret=E_SUCCESS;
          }
      }
    m_pcsGet->unlock();
    return ret;
  }

Here is the call graph for this function:

SINT32 CASocketAddrINet::getSize ( ) const [inline, virtual]

Returns the Size of the SOCKADDR struct used.

Returns:
sizeof(sockaddr_in)

Implements CASocketAddr.

Definition at line 77 of file CASocketAddrINet.hpp.

Referenced by CASocketAddrINet().

        {
          return sizeof(sockaddr_in);
        }
SINT32 CASocketAddrINet::getType ( ) const [inline, virtual]

The type (family) of socket for which this address is useful.

Must be overwritten in subclasses.

Implements CASocketAddr.

Definition at line 50 of file CASocketAddrINet.hpp.

        {
          return AF_INET;
        }
static SINT32 CASocketAddrINet::init ( ) [inline, static]

Definition at line 37 of file CASocketAddrINet.hpp.

References E_SUCCESS, and m_pcsGet.

      {
        m_pcsGet=new CAMutex();
        return E_SUCCESS;
      }
bool CASocketAddrINet::isAnyIP ( ) [inline]

Definition at line 89 of file CASocketAddrINet.hpp.

Referenced by CALocalProxy::init().

        {
          return sin_addr.s_addr==INADDR_ANY;
        }
const SOCKADDR* CASocketAddrINet::LPSOCKADDR ( ) const [inline, virtual]

Makes a cast to struct SOCKADDR*.

Implements CASocketAddr.

Definition at line 65 of file CASocketAddrINet.hpp.

Referenced by CASocketAddrINet().

        {
          #if defined(_WIN32) &&!defined(MSC_VER) //for Borland C++ under Windows
            return (const ::LPSOCKADDR)(sockaddr_in*)this;
          #else
            return (const ::LPSOCKADDR)(static_cast<const sockaddr_in* const>(this));
          #endif
        }
SINT32 CASocketAddrINet::setAddr ( const UINT8 szIP,
UINT16  port 
)

Sets the address to szIP and port.

szIP could be either a hostname or an IP-Address of the form a.b.c.d . If szIP==NULL, the the IP-Adredress ist set to ANY local IP Address

Parameters:
szIPnew value for IP-Address or hostname (zero terminated string)
portnew value for port
Return values:
E_SUCCESSif no error occurs
E_UNKNOWN_HOSTif the hostname couldt not be resolved (or the ip is wrong). In this case the old values are NOT changed.

Definition at line 93 of file CASocketAddrINet.cpp.

References E_SUCCESS, E_UNKNOWN_HOST, CAMutex::lock(), m_pcsGet, and CAMutex::unlock().

Referenced by CAInfoService::getPaymentInstance(), CALocalProxy::init(), and CAAccountingBIInterface::setPIServerConfiguration().

  {
    UINT32 newAddr=INADDR_ANY;
    if(szIP!=NULL)
      {
        newAddr=inet_addr((const char*)szIP); //is it a doted string (a.b.c.d) ?
        if(newAddr==INADDR_NONE) //if not try to find the hostname
          {
            m_pcsGet->lock();
            HOSTENT* hostent=gethostbyname((const char*)szIP); //lookup
            if(hostent!=NULL) //get it!
              memcpy(&newAddr,hostent->h_addr_list[0],hostent->h_length);
            else
              {
                m_pcsGet->unlock();
                return E_UNKNOWN_HOST; //not found!
              }
            m_pcsGet->unlock();
          }
      }
    sin_addr.s_addr=newAddr;
    sin_port=htons(port);
    return E_SUCCESS;
  }

Here is the call graph for this function:

Sets the IP-Numbers for this address.

Parameters:
ipbuffer with the IP-Address (4 Bytes)
Return values:
E_SUCCESSif no error occured

Definition at line 179 of file CASocketAddrINet.cpp.

References E_SUCCESS.

  {
    memcpy(&sin_addr.s_addr,ip,4);
    return E_SUCCESS;
  }

Changes only(!) the port value of the address.

Parameters:
portnew value for port
Returns:
always E_SUCCESS

Definition at line 123 of file CASocketAddrINet.cpp.

References E_SUCCESS.

  {
    sin_port=htons(port);
    return E_SUCCESS;
  }
virtual SINT32 CASocketAddrINet::toString ( UINT8 buff,
UINT32  bufflen 
) const [inline, virtual]

Returns a human readable representation of this address.

Parameters:
buffbuffer which stores the address string
bufflensize of the buffer
Return values:
E_SPACEif the buffer is to small
E_UNKNOWNif an error occured
E_SUCCESSif successfull

Implements CASocketAddr.

Definition at line 111 of file CASocketAddrINet.hpp.

References E_SPACE, E_SUCCESS, E_UNKNOWN, getIPAsStr(), and getPort().

        {
          UINT8 tmpbuff[255];
          if(getIPAsStr(tmpbuff,255)!=E_SUCCESS)
            return E_UNKNOWN;
          if(snprintf((char*)buff,bufflen,"INet address: %s:%u",tmpbuff,getPort())<0)
            return E_SPACE;
          return E_SUCCESS;
        }

Here is the call graph for this function:


Member Data Documentation

CAMutex * CASocketAddrINet::m_pcsGet = NULL [static, private]

The documentation for this class was generated from the following files: