Mixe for Privacy and Anonymity in the Internet
CASocketAddrINet.cpp
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 #include "StdAfx.h"
29 #include "CASocketAddrINet.hpp"
30 #include "CAMsg.hpp"
31 
33 
35 /*SINT32 CASocketAddrINet::init()
36  {
37  if(!m_bIsCsInitialized)
38  {
39  m_bIsCsInitialized=true;
40  }
41  return E_SUCCESS;
42  }
43 */
46 /*SINT32 CASocketAddrINet::destroy()
47  {
48  if(m_bIsCsInitialized)
49  {
50  DeleteCriticalSection(&m_csGet);
51  }
52  m_bIsCsInitialized=false;
53  return E_SUCCESS;
54  }
55 */
56 
59  {
60  memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
61  sin_family=AF_INET;
62  sin_addr.s_addr=INADDR_ANY;
63  sin_port=0;
64  }
65 
68  {
69  memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
70  sin_family=AF_INET;
71  sin_port=htons(port);
72  sin_addr.s_addr=INADDR_ANY;
73  }
74 
77  {
78  memset((SOCKADDR*)LPSOCKADDR(),0,getSize());
79  sin_family=AF_INET;
80  sin_port=addr.sin_port;
81  sin_addr.s_addr=addr.sin_addr.s_addr;
82  }
83 
94  {
95  UINT32 newAddr=INADDR_ANY;
96  if(szIP!=NULL)
97  {
98  newAddr=inet_addr((const char*)szIP); //is it a doted string (a.b.c.d) ?
99  if(newAddr==INADDR_NONE) //if not try to find the hostname
100  {
101  m_pcsGet->lock();
102  HOSTENT* hostent=gethostbyname((const char*)szIP); //lookup
103  if(hostent!=NULL) //get it!
104  memcpy(&newAddr,hostent->h_addr_list[0],hostent->h_length);
105  else
106  {
107  m_pcsGet->unlock();
108  return E_UNKNOWN_HOST; //not found!
109  }
110  m_pcsGet->unlock();
111  }
112  }
113  sin_addr.s_addr=newAddr;
114  sin_port=htons(port);
115  return E_SUCCESS;
116  }
117 
124  {
125  sin_port=htons(port);
126  return E_SUCCESS;
127  }
128 
133  {
134  return ntohs(sin_port);
135  }
136 
146  {
147  if(buff==NULL)
148  return E_UNSPECIFIED;
149  SINT32 ret;
150  m_pcsGet->lock();
151  HOSTENT* hosten=gethostbyaddr((const char*)&sin_addr,4,AF_INET);
152  if(hosten==NULL||hosten->h_name==NULL)
153  ret=E_UNKNOWN_HOST;
154  else if(strlen(hosten->h_name)>=len)
155  ret=E_SPACE;
156  else
157  {
158  strcpy((char*)buff,hosten->h_name);
159  ret=E_SUCCESS;
160  }
161  m_pcsGet->unlock();
162  return ret;
163  }
164 
170  {
171  memcpy(buff,&sin_addr.s_addr,4);
172  return E_SUCCESS;
173  }
174 
180  {
181  memcpy(&sin_addr.s_addr,ip,4);
182  return E_SUCCESS;
183  }
184 
191  {
192  if(buff==NULL)
193  return E_UNKNOWN;
194  UINT8 ip[4];
195  if(getIP(ip)!=E_SUCCESS)
196  return E_UNKNOWN;
197  char* strAddr=inet_ntoa(sin_addr);
198  if(strAddr==NULL)
199  return E_UNKNOWN;
200  if(strlen(strAddr)>=len)
201  return E_UNKNOWN;
202  strcpy((char*)buff,strAddr);
203  return E_SUCCESS;
204  }
205 
215  {
216  if(buff==NULL)
217  return E_UNSPECIFIED;
218  SINT32 ret;
219  m_pcsGet->lock();
220  if(gethostname((char*)buff,len)==-1)
221  ret=E_SPACE;
222  else
223  {
224  HOSTENT* hosten=gethostbyname((char*)buff);
225  if(hosten==NULL||hosten->h_name==NULL)
226  ret=E_UNKNOWN_HOST;
227  else if(strlen(hosten->h_name)>=len)
228  ret=E_SPACE;
229  else
230  {
231  strcpy((char*)buff,hosten->h_name);
232  ret=E_SUCCESS;
233  }
234  }
235  m_pcsGet->unlock();
236  return ret;
237  }
238 
245  {
246  SINT32 ret;
247  char buff[256];
248  m_pcsGet->lock();
249  if(gethostname(buff,256)==-1)
250  ret=E_UNKNOWN;
251  else
252  {
253  HOSTENT* hosten=gethostbyname((char*)buff);
254  if(hosten==NULL)
255  ret=E_UNKNOWN;
256  else
257  {
258  memcpy((char*)ip,hosten->h_addr_list[0],4);
259  ret=E_SUCCESS;
260  }
261  }
262  m_pcsGet->unlock();
263  return ret;
264  }
265 
struct hostent HOSTENT
Definition: StdAfx.h:461
#define INADDR_NONE
Definition: StdAfx.h:479
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
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
This class represents a socket address for Internet (IP) connections.
SINT32 setPort(UINT16 port)
Changes only(!) the port value of the address.
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.
const SOCKADDR * LPSOCKADDR() const
Makes a cast to struct SOCKADDR*.
SINT32 getSize() const
Returns the Size of the SOCKADDR struct used.
SINT32 getIPAsStr(UINT8 *buff, UINT32 len) const
Returns the IP-Number as an address string (doted-format).
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_HOST
Definition: errorcodes.hpp:18
#define E_UNKNOWN
Definition: errorcodes.hpp:3
#define E_UNSPECIFIED
Definition: errorcodes.hpp:6
UINT16 len
Definition: typedefs.hpp:0