Mixe for Privacy and Anonymity in the Internet
CASocketAddrINet.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #ifndef __CASOCKETADDRINET__
29 #define __CASOCKETADDRINET__
30 #include "CASocketAddr.hpp"
31 #include "CAMutex.hpp"
34 class CASocketAddrINet:private sockaddr_in,public CASocketAddr
35  {
36  public:
37  static SINT32 init()
38  {
39  m_pcsGet=new CAMutex();
40  return E_SUCCESS;
41  }
42  static SINT32 cleanup()
43  {
44  delete m_pcsGet;
45  m_pcsGet = NULL;
46  return E_SUCCESS;
47  }
48 
49  //static SINT32 destroy();
50  SINT32 getType()const
51  {
52  return AF_INET;
53  }
56 
58 
60  {
61  return new CASocketAddrINet(*this);
62  }
63 
65  const SOCKADDR* LPSOCKADDR()const
66  {
67  #if defined(_WIN32) &&!defined(MSC_VER) //for Borland C++ under Windows
68  return (const ::LPSOCKADDR)(sockaddr_in*)this;
69  #else
70  return (const ::LPSOCKADDR)(static_cast<const sockaddr_in* const>(this));
71  #endif
72  }
73 
77  SINT32 getSize() const
78  {
79  return sizeof(sockaddr_in);
80  }
81 
82  SINT32 setAddr(const UINT8* szIP,UINT16 port);
83  SINT32 setIP(UINT8 ip[4]);
84  SINT32 setPort(UINT16 port);
85  UINT16 getPort() const;
86  SINT32 getHostName(UINT8* buff,UINT32 len)const;
87  SINT32 getIP(UINT8 buff[4]) const;
88  SINT32 getIPAsStr(UINT8* buff,UINT32 len) const;
89  bool isAnyIP()
90  {
91  return sin_addr.s_addr==INADDR_ANY;
92  }
93 
94  bool equalsIP(UINT8 ip[4]) const
95  {
96  return (memcmp(ip,&sin_addr.s_addr,4)==0);
97  }
98 
99  static SINT32 getLocalHostName(UINT8* buff,UINT32 len);
100  static SINT32 getLocalHostIP(UINT8 ip[4]);
101  /*** Gets the IP for a doted IP string (a.b.c.d)
102  */
103  static SINT32 getIPForString(UINT8* strIP, UINT8 ip[4])
104  {
105  in_addr addr;
106  SINT32 ret = inet_pton(AF_INET,(const char*)strIP,&addr); //is it a doted string (a.b.c.d) ?
107  if (ret!=1) //no..
108  {
109  return E_UNKNOWN;
110  }
111  UINT32 uAddr = ntohl(addr.s_addr);
112  ip[0] = (uAddr >> 24) & 0xFF;
113  ip[1] = (uAddr >> 16) & 0xFF;
114  ip[2] = (uAddr >> 8) & 0xFF;
115  ip[3] = (uAddr) & 0xFF;
116  return E_SUCCESS;
117  }
118 // operator LPSOCKADDR(){return (::LPSOCKADDR)m_pAddr;}
119 
128  virtual SINT32 toString(UINT8* buff,UINT32 bufflen) const
129  {
130  UINT8 tmpbuff[255];
131  if(getIPAsStr(tmpbuff,255)!=E_SUCCESS)
132  return E_UNKNOWN;
133  if(snprintf((char*)buff,bufflen,"INet address: %s:%u",tmpbuff,getPort())<0)
134  return E_SPACE;
135  return E_SUCCESS;
136  }
137  private:
138  static CAMutex* m_pcsGet;
139  };
140 
141 #endif
SOCKADDR * LPSOCKADDR
Definition: StdAfx.h:459
struct sockaddr SOCKADDR
Definition: StdAfx.h:458
unsigned short UINT16
Definition: basetypedefs.h:133
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
This is an abstract class for representing a socket address used in CASocket, CADatagramSocket and CA...
This class represents a socket address for Internet (IP) connections.
SINT32 setPort(UINT16 port)
Changes only(!) the port value of the address.
CASocketAddr * clone() const
Creates a copy of the Address.
static SINT32 cleanup()
bool equalsIP(UINT8 ip[4]) const
SINT32 getType() const
The type (family) of socket for which this address is useful.
static CAMutex * m_pcsGet
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 getHostName(UINT8 *buff, UINT32 len) const
Returns the hostname for this address.
CASocketAddrINet()
Must be called once before using one of the CAsocketAddrINet functions.
static SINT32 init()
const SOCKADDR * LPSOCKADDR() const
Makes a cast to struct SOCKADDR*.
SINT32 getSize() const
Returns the Size of the SOCKADDR struct used.
static SINT32 getIPForString(UINT8 *strIP, UINT8 ip[4])
SINT32 getIPAsStr(UINT8 *buff, UINT32 len) const
Returns the IP-Number as an address string (doted-format).
virtual SINT32 toString(UINT8 *buff, UINT32 bufflen) const
Returns a human readable representation of this address.
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.
UINT16 getPort() const
Returns the port value of the address.
SINT32 getIP(UINT8 buff[4]) const
Returns the IP-Numbers for this address.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_SPACE
Definition: errorcodes.hpp:7
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0