Mixe for Privacy and Anonymity in the Internet
CAIPAddrWithNetmask.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 #pragma once
30  {
31  public:
33  {
34  m_nIPAddr=0;
35  m_nNetmask=0xFFFFFFFF;
37  }
38 
39  CAIPAddrWithNetmask(const UINT8* strIPAddr,const UINT8* strNetmask)
40  {
41  m_nIPAddr=htonl(inet_addr((const char*)strIPAddr));
42  m_nNetmask=htonl(inet_addr((const char*)strNetmask));
44  }
45 
47 
48  SINT32 setAddr(const UINT8* strIPAddr)
49  {
50  m_nIPAddr=htonl(inet_addr((const char*)strIPAddr));
52  return E_SUCCESS;
53  }
54 
55  SINT32 setNetmask(const UINT8* strNetmask)
56  {
57  m_nNetmask=htonl(inet_addr((const char*)strNetmask));
59  return E_SUCCESS;
60  }
61 
62  //Note IP-Addr must be in network byte order!
63  bool equals(UINT32 nIPAddr) const
64  {
65  return m_nIPAddrAndNetmask==(nIPAddr&m_nNetmask);
66  }
67 
68  bool equals(const UINT8* pIPAddr) const
69  {
70  UINT32 nIPAddr=pIPAddr[0];
71  nIPAddr<<=8;
72  nIPAddr|=pIPAddr[1];
73  nIPAddr<<=8;
74  nIPAddr|=pIPAddr[2];
75  nIPAddr<<=8;
76  nIPAddr|=pIPAddr[3];
77  return m_nIPAddrAndNetmask==(nIPAddr&m_nNetmask);
78  }
79 
80  SINT32 toString(UINT8* buff,UINT32* buffLen)
81  {
82  UINT8 strIP[255];
83  UINT8 strNetmask[255];
84  struct in_addr addr;
85  addr.s_addr=ntohl(m_nIPAddr);
86  strcpy((char*)strIP,inet_ntoa(addr));
87  addr.s_addr=ntohl(m_nNetmask);
88  strcpy((char*)strNetmask,inet_ntoa(addr));
89  SINT32 ret=snprintf((char*)buff,*buffLen,"%s/%s",strIP,strNetmask);
90  if(ret<=0||((UINT32)ret)>=*buffLen)
91  {
92  buff[0]=0;
93  *buffLen=0;
94  return E_UNKNOWN;
95  }
96  *buffLen=ret;
97  return E_SUCCESS;
98  }
99 
100  private:
104  };
105 
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
CAIPAddrWithNetmask(const UINT8 *strIPAddr, const UINT8 *strNetmask)
SINT32 setNetmask(const UINT8 *strNetmask)
SINT32 toString(UINT8 *buff, UINT32 *buffLen)
bool equals(const UINT8 *pIPAddr) const
SINT32 setAddr(const UINT8 *strIPAddr)
bool equals(UINT32 nIPAddr) const
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3