Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetHost.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cNetHost_H
9 #define _INC_cNetHost_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cNetSystem.h"
17 
18 #ifndef __linux__
19 typedef short sa_family_t;
20 #endif
21 
22 namespace GrayLib
23 {
24  UNITTEST2_PREDEF(cNetHost);
25 
26 #if defined(_WINDLL) || defined(USE_64BIT) || defined(__linux__) || (defined(_MSC_VER) && ( _MSC_VER >= 1600 ))
27 #define USE_WINSOCK2_ADDR // addrinfo,ADDRINFO doesn't work for Win2000 ? used for multiple resolutions on a single hostname.
28 #endif
29 
31  {
35 
36  public:
37  static const size_t k_SIZE_M48 = 6;
38  static const size_t k_SIZE_E48 = 6;
39  static const size_t k_SIZE_E64 = 8;
40  static const size_t k_SIZE_MAX = 8;
41 
42  BYTE m_bAddr[k_SIZE_MAX];
43 
44  public:
45  cNetMacAddr() noexcept
46  {
47  cMem::Zero(m_bAddr, sizeof(m_bAddr));
48  }
49  cNetMacAddr(const cNetMacAddr& mac) noexcept
50  {
51  ::memcpy(m_bAddr, mac.m_bAddr, sizeof(m_bAddr));
52  }
53  cNetMacAddr(const char* pszMac)
54  {
55  put_MacStr(pszMac);
56  }
57 
58  cStringA get_MacStr() const;
59  bool put_MacStr(const char* pszMac);
60  HRESULT SetMacAddrFromAdapter(sa_family_t nAddressFamily = AF_INET, int nAdapterIndex = 0);
61  };
62 
64  {
75 
76  public:
77 #ifdef USE_LITTLE_ENDIAN
78  static const UINT32 k_LOOPBACK_N = 0x0100007f;
79 #else
80  static const UINT32 k_LOOPBACK_N = 0x7F000001;
81 #endif
82  static const UINT32 k_BROADCAST = 0xFFFFFFFF;
83 
84  protected:
85  in_addr m_ipa4;
86 
87  public:
88  cNetHost4() noexcept
89  {
90  m_ipa4.s_addr = INADDR_ANY;
91  }
92  explicit cNetHost4(UINT uIP4) noexcept
93  {
95  m_ipa4.s_addr = cMemT::HtoN(uIP4);
96  }
97  cNetHost4(const in_addr& ip) noexcept
98  : m_ipa4(ip)
99  {
100  }
101  cNetHost4(const char* pszIP)
102  {
105  put_HostStr(pszIP);
106  }
107 
108  //********************************
109 
110  bool isHostValid() const noexcept
111  {
113  return m_ipa4.s_addr != INADDR_ANY;
114  }
115  bool isHostLoopback() const noexcept
116  {
118  return m_ipa4.s_addr == k_LOOPBACK_N; // cMemT::HtoN(INADDR_LOOPBACK)
119  }
120  bool isHostBroadcast() const noexcept
121  {
123  return m_ipa4.s_addr == k_BROADCAST;
124  }
125  bool isHostPrivate() const
126  {
128  UINT32 ip4h = get_HostIp4_H(); // host order.
129  BYTE bClassB;
130  switch ((ip4h & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT)
131  {
132  case 0: // 0.* - INADDR_ANY
133  if (ip4h == 0)
134  return true;
135  break;
136  case 10: // 10.0.0.0 - 10.255.255.255 class A
137  return true;
138  case 127: // INADDR_LOOPBACK
139  if (ip4h == INADDR_LOOPBACK)
140  return true;
141  break;
142  case 172: // 172.16.0.0 - 172.31.255.255 class B
143  bClassB = (BYTE)((ip4h >> IN_CLASSB_NSHIFT) & 0xFF);
144  if (bClassB >= 16 && bClassB <= 31)
145  return true;
146  break;
147  case 192: // 192.168.0.0 - 192.168.255.255 class C
148  bClassB = (BYTE)((ip4h >> IN_CLASSB_NSHIFT) & 0xFF);
149  if (bClassB == 168)
150  return true;
151  break;
152  case 255: // 255.255.255.255 = INADDR_BROADCAST
153  if (ip4h == INADDR_BROADCAST)
154  return true;
155  break;
156  }
157  return false;
158  }
159  bool isHostLocalTo(const cNetHost4& ip, UINT32 dwMask = 0xFFFFFF00) const noexcept
160  {
164  UINT32 dwMaskN = cMemT::HtoN(dwMask); // to network order
165  return (m_ipa4.s_addr & dwMaskN) == (ip.m_ipa4.s_addr & dwMaskN);
166  }
167  bool operator == (cNetHost4 ip) const noexcept
168  {
169  return m_ipa4.s_addr == ip.m_ipa4.s_addr;
170  }
171  void SetEmptyHostAddr() noexcept
172  {
174  m_ipa4.s_addr = INADDR_NONE;
175  }
176 
177  //********************************
178 
179  operator in_addr() const noexcept
180  {
182  return m_ipa4;
183  }
184  BYTE GetByte(int i = 0) const
185  {
187  ASSERT(IS_INDEX_GOOD(i, 4));
188  return ((const BYTE*)&m_ipa4.s_addr)[i];
189  }
190  in_addr get_HostIp4() const noexcept
191  {
193  return m_ipa4;
194  }
195  UINT32 get_HostIp4_H() const noexcept
196  {
199  return cMemT::NtoH(m_ipa4.s_addr);
200  }
201  void put_HostIp4(in_addr addr4) noexcept
202  {
204  m_ipa4 = addr4;
205  }
206  void put_HostIp4_H(UINT32 uIP4) noexcept
207  {
210  m_ipa4.s_addr = cMemT::HtoN(uIP4);
211  }
212  void put_HostLoopback() noexcept
213  {
215  m_ipa4.s_addr = k_LOOPBACK_N; // cMemT::HtoN(INADDR_LOOPBACK);
216  }
217  HASHCODE32_t get_HashCode() const noexcept
218  {
221  return (HASHCODE32_t)get_HostIp4_H();
222  }
223 
224  //********************************
225 
226  cStringA get_HostStr() const;
227  HRESULT put_HostStr(const char* pszIP);
228  static StrLen_t GRAYCALL GetIP4Format(const char* pszIP, bool bTerm = true);
229 
230  static const in_addr k_AddrNull;
231  static const in_addr k_AddrBroadcast;
232 
233  UNITTEST_FRIEND(cNetHost);
234  };
235 
237  {
243 
244  public:
245  static const in6_addr k_AddrNull;
246  protected:
247  in6_addr m_ipa6;
248 
249  public:
250  cNetHost6() noexcept
251  {
252  cMem::Zero(&m_ipa6, sizeof(m_ipa6)); // IN6ADDR_ANY_INIT
253  }
254  cNetHost6(const in6_addr& ip) noexcept
255  : m_ipa6(ip)
256  {
257  }
258  cNetHost6(const in_addr& ip)
259  {
261  cUnion32* pU = (cUnion32*)&m_ipa6;
262  pU[0].u_dw = 0;
263  pU[1].u_dw = 0;
264  pU[2].u_dw = 0xFFFF0000; // assume USE_LITTLE_ENDIAN
265  pU[3].u_dw = ip.s_addr;
266  ASSERT(isHostIP4());
267  }
268  cNetHost6(const char* pszIP)
269  {
272  put_HostStr(pszIP);
273  }
274  void SetEmptyHostAddr() noexcept
275  {
276  cMem::Zero(&m_ipa6, sizeof(m_ipa6));
277  }
278 
279  bool isHostValid() const;
280  bool isHostLoopback() const;
281  bool isHostPrivate() const;
282  bool isHostIP4() const;
283 
284  static StrLen_t GRAYCALL GetIP6Format(const char* pszIP, bool bTerm = true);
285 
286  HRESULT put_HostStr(const char* pszIP);
287  cStringA get_HostStr() const;
288 
289  operator in6_addr() const noexcept
290  {
291  return m_ipa6;
292  }
293  const in6_addr& get_HostIp6() const noexcept
294  {
295  return m_ipa6 ;
296  }
297  HASHCODE_t get_HashCode() const noexcept;
298 
299  bool operator == (const in6_addr& ip) const noexcept
300  {
301  return !::memcmp(&m_ipa6, &ip, sizeof(m_ipa6));
302  }
303  };
304 }
305 #endif // _INC_cNetHost_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define IS_INDEX_GOOD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
short sa_family_t
enum of network family types. e.g. AF_UNSPEC=0, AF_INET = 2, AF_INET6 = 23, AF_NETBIOS = 17,...
Definition: cNetHost.h:19
Definition: cNetHost.h:64
static const in_addr k_AddrBroadcast
Broadcast. INADDR_BROADCAST.
Definition: cNetHost.h:231
cNetHost4(const char *pszIP)
Definition: cNetHost.h:101
UINT32 get_HostIp4_H() const noexcept
Definition: cNetHost.h:195
bool isHostValid() const noexcept
Definition: cNetHost.h:110
void SetEmptyHostAddr() noexcept
Definition: cNetHost.h:171
cNetHost4(const in_addr &ip) noexcept
Definition: cNetHost.h:97
HASHCODE32_t get_HashCode() const noexcept
Definition: cNetHost.h:217
void put_HostIp4(in_addr addr4) noexcept
Definition: cNetHost.h:201
in_addr get_HostIp4() const noexcept
Definition: cNetHost.h:190
void put_HostLoopback() noexcept
Definition: cNetHost.h:212
cNetHost4() noexcept
Definition: cNetHost.h:88
BYTE GetByte(int i=0) const
Definition: cNetHost.h:184
bool isHostLocalTo(const cNetHost4 &ip, UINT32 dwMask=0xFFFFFF00) const noexcept
Definition: cNetHost.h:159
in_addr m_ipa4
(network order bytes) (4 bytes)
Definition: cNetHost.h:85
bool isHostPrivate() const
Definition: cNetHost.h:125
static const in_addr k_AddrNull
invalid address. INADDR_ANY (NOT INADDR_NONE)
Definition: cNetHost.h:230
void put_HostIp4_H(UINT32 uIP4) noexcept
Definition: cNetHost.h:206
bool isHostLoopback() const noexcept
Definition: cNetHost.h:115
cNetHost4(UINT uIP4) noexcept
Definition: cNetHost.h:92
UNITTEST_FRIEND(cNetHost)
bool isHostBroadcast() const noexcept
Definition: cNetHost.h:120
Definition: cNetHost.h:237
void SetEmptyHostAddr() noexcept
Definition: cNetHost.h:274
const in6_addr & get_HostIp6() const noexcept
Definition: cNetHost.h:293
in6_addr m_ipa6
(network order bytes) (16 bytes)
Definition: cNetHost.h:247
cNetHost6() noexcept
Definition: cNetHost.h:250
cNetHost6(const in_addr &ip)
Definition: cNetHost.h:258
static const in6_addr k_AddrNull
invalid address for initialize.
Definition: cNetHost.h:245
cNetHost6(const char *pszIP)
Definition: cNetHost.h:268
cNetHost6(const in6_addr &ip) noexcept
Definition: cNetHost.h:254
Definition: cNetHost.h:31
cNetMacAddr() noexcept
Definition: cNetHost.h:45
cNetMacAddr(const cNetMacAddr &mac) noexcept
Definition: cNetHost.h:49
cNetMacAddr(const char *pszMac)
Definition: cNetHost.h:53
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
UINT_PTR HASHCODE_t
could hold a pointer converted to a number? maybe 64 or 32 bit ? same as size_t.
Definition: GrayCore.h:116
UINT32 HASHCODE32_t
always 32 bits.
Definition: GrayCore.h:117
bool operator==(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:250
static TYPE NtoH(TYPE nVal) noexcept
Definition: cMem.h:502
static TYPE HtoN(TYPE nVal) noexcept
Definition: cMem.h:491
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
Definition: cTypes.h:66
UINT32 u_dw
32 bit unsigned
Definition: cTypes.h:78