Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetAddr.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cNetAddr_H
9 #define _INC_cNetAddr_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cNetHost.h"
15 #include "cNetPort.h"
18 #include "GrayCore/include/cMem.h"
21 
22 #ifdef _WIN32
23 typedef int socklen_t;
24 #endif
25 
26 namespace GrayLib
27 {
29 
31  {
37 
38  private:
39  socklen_t m_nSockAddrLen;
40  union // sizeof(sockaddr)==16, sizeof(sockaddr_in6) == 28 bytes
41  {
42  sockaddr m_SockAddr;
43  sockaddr_in m_SockAddr4;
44  sockaddr_in6 m_SockAddr6;
45  } m_u;
46 
47  protected:
48  const in_addr& ref_HostIp4() const
49  {
50  ASSERT(get_Family() == AF_INET);
51  return m_u.m_SockAddr4.sin_addr;
52  }
53  in_addr& ref_HostIp4()
54  {
55  ASSERT(get_Family() == AF_INET);
56  return m_u.m_SockAddr4.sin_addr;
57  }
58  const in6_addr& ref_HostIp6() const
59  {
60  ASSERT(get_Family() == AF_INET6);
61  return m_u.m_SockAddr6.sin6_addr;
62  }
63  in6_addr& ref_HostIp6()
64  {
65  ASSERT(get_Family() == AF_INET6);
66  return m_u.m_SockAddr6.sin6_addr;
67  }
68 
69  public:
70  cNetAddress() noexcept
71  : m_nSockAddrLen(sizeof(m_u))
72  {
76  cMem::Zero(&m_u, sizeof(m_u));
77  DEBUG_ASSERT(!isAddrValid(), "cNetAddress");
78  }
79  cNetAddress(sa_family_t nFamily, NET_PORT_t wPort = k_NET_PORT_INVALID) noexcept
80  : m_nSockAddrLen(sizeof(m_u))
81  {
85  cMem::Zero(&m_u, sizeof(m_u));
86  m_u.m_SockAddr.sa_family = nFamily;
87  m_u.m_SockAddr4.sin_port = cMemT::HtoN(wPort);
88  }
89  cNetAddress(in_addr ip, NET_PORT_t wPort = k_NET_PORT_INVALID) noexcept
90  : m_nSockAddrLen(sizeof(m_u))
91  {
94  cMem::Zero(&m_u, sizeof(m_u));
95  m_u.m_SockAddr.sa_family = AF_INET;
96  m_u.m_SockAddr4.sin_port = cMemT::HtoN(wPort);
97  m_u.m_SockAddr4.sin_addr = ip;
98  }
99  cNetAddress(const char* pszAddr, bool bWait = false)
100  : m_nSockAddrLen(sizeof(m_u))
101  {
104  cMem::Zero(&m_u, sizeof(m_u));
105  // nFamily = AF_UNSPEC; // AF_INET, AF_INET6, etc
106  // wPort = k_NET_PORT_INVALID;
107  put_AddrStr(pszAddr, bWait);
108  }
109  cNetAddress(const char* pszAddr, NET_PORT_t wPortDefault, bool bWait = false)
110  : m_nSockAddrLen(sizeof(m_u))
111  {
114  cMem::Zero(&m_u, sizeof(m_u));
115  m_u.m_SockAddr4.sin_port = cMemT::HtoN(wPortDefault); // Set the default port first. (if not also set in the string)
116  put_AddrStr(pszAddr, bWait);
117  }
118 
119  sa_family_t get_Family() const noexcept
120  {
122  return m_u.m_SockAddr.sa_family;
123  }
124  socklen_t get_AddrLen() const noexcept
125  {
128  return m_nSockAddrLen;
129  }
130  socklen_t& ref_AddrLen() noexcept
131  {
133  return m_nSockAddrLen;
134  }
135  const sockaddr& get_Addr() const noexcept
136  {
138  return (sockaddr&)m_u.m_SockAddr;
139  }
140  const sockaddr& ref_Addr() const noexcept
141  {
143  return (sockaddr&)m_u.m_SockAddr;
144  }
145  sockaddr& ref_Addr() noexcept
146  {
148  return (sockaddr&)m_u.m_SockAddr;
149  }
150 
151  bool isAddrValid() const;
152  void ResetAddr() noexcept
153  {
154  m_nSockAddrLen = sizeof(m_u);
155  // sa_family = AF_UNSPEC;
156  // sin_port = k_NET_PORT_INVALID;
157  // sin_addr.s_addr = INADDR_NONE; // invalid address
158  cMem::Zero(&m_u, sizeof(m_u));
159  }
160  COMPARE_t CompareAddr(const cNetAddress& SocketAddr) const
161  {
163  if (m_nSockAddrLen != SocketAddr.m_nSockAddrLen)
164  {
165  return m_nSockAddrLen - SocketAddr.m_nSockAddrLen;
166  }
167  ASSERT((size_t)m_nSockAddrLen <= sizeof(m_u));
168  COMPARE_t iRet = ::memcmp(&m_u, &SocketAddr.m_u, m_nSockAddrLen);
169  if (iRet != COMPARE_Equal)
170  {
171  return iRet;
172  }
173  return COMPARE_Equal;
174  }
175  bool operator == (const cNetAddress& SocketAddr) const
176  {
177  return(CompareAddr(SocketAddr) == COMPARE_Equal);
178  }
179  bool operator != (const cNetAddress& SocketAddr) const
180  {
181  return(CompareAddr(SocketAddr) != COMPARE_Equal);
182  }
183 
184  HASHCODE_t get_HashCodeHost() const noexcept;
185  HASHCODE_t get_HashCode() const noexcept
186  {
187  return get_HashCodeHost();
188  }
189 
190  //********************************
191  // Isolate the port part of the address
192 
193  NET_PORT_t get_PortNumber() const noexcept
194  {
199  return cMemT::NtoH(m_u.m_SockAddr4.sin_port);
200  }
202  {
205  m_u.m_SockAddr4.sin_port = cMemT::HtoN(wPort);
206  }
207  bool isPortValid() const
208  {
210  return(m_u.m_SockAddr4.sin_port != k_NET_PORT_INVALID);
211  }
212  static char* GRAYCALL FindPortExtPtr(const char* pszAddr);
213  bool put_PortServiceName(const char* pszPortServiceName);
214  cStringA get_PortServiceName() const;
215 
216  //********************************
217  // Isolate the host part of the address. depends on family type.
218 
219  const BYTE* get_HostPtr() const
220  {
222  return (const BYTE*)&m_u.m_SockAddr4.sin_addr;
223  }
224  BYTE* ref_HostPtr() const
225  {
227  return (BYTE*)&m_u.m_SockAddr4.sin_addr;
228  }
229  size_t get_HostLen() const
230  {
232  return m_nSockAddrLen - (sizeof(sa_family_t) + sizeof(NET_PORT_t));
233  }
234 
235  bool isHostLoopback() const;
236  bool isHostPrivate() const;
237  bool put_HostLoopback(sa_family_t nFamily = AF_UNSPEC);
238 
239  COMPARE_t CompareHost(const cNetAddress& SocketAddr) const;
240 
241  HRESULT SetHostStr(const char* pszHost, bool bWait = false);
242  HRESULT SetHostStrFix(const char* pszAddr, bool bWait = false);
243  cStringA get_HostStr() const;
244 
245  cNetHost4 get_HostIp4() const;
246  void put_HostIp4(in_addr nIP)
247  {
249  m_nSockAddrLen = sizeof(m_u.m_SockAddr4);
250  m_u.m_SockAddr4.sin_family = AF_INET;
251  ref_HostIp4() = nIP;
252  }
253  cNetHost6 get_HostIp6() const;
254  void put_HostIp6(const in6_addr& addr)
255  {
257  m_nSockAddrLen = sizeof(m_u.m_SockAddr6);
258  m_u.m_SockAddr6.sin6_family = AF_INET6;
259  ref_HostIp6() = addr;
260  }
261  bool SetHostData(sa_family_t nFamily, const void* pHostData, size_t nDataSize = sizeof(in_addr));
262  void ResetHost()
263  {
266  NET_PORT_t wPort = get_PortNumber();
267  ResetAddr();
268  put_PortNumber(wPort);
269  }
270 
271  //********************************
272 
273  void put_Addr(const cNetAddress& rAddr)
274  {
275  *this = rAddr;
276  }
277  bool SetAddrData(const sockaddr* pSockAddr, socklen_t nSockAddrLen);
278 
279  cStringA get_AddrStr() const;
280  HRESULT put_AddrStr(const char* pszAddr, bool bWait = false);
281 
282  //*****************************
283 
284  HRESULT SerializeOutput(cStreamOutput& rout);
285  HRESULT SerializeInput(cStreamInput& file);
286 
288  };
289 
291 
292 };
293 #endif // _INC_cNetAddr_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
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_ASSERT(exp, sDesc)
Definition: cDebugAssert.h:93
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: cNetAddr.h:31
void ResetHost()
Definition: cNetAddr.h:262
const sockaddr & ref_Addr() const noexcept
Definition: cNetAddr.h:140
void ResetAddr() noexcept
Definition: cNetAddr.h:152
sockaddr_in6 m_SockAddr6
Address is ipv6 = 28 bytes.
Definition: cNetAddr.h:44
cNetAddress(in_addr ip, NET_PORT_t wPort=k_NET_PORT_INVALID) noexcept
Definition: cNetAddr.h:89
void put_HostIp4(in_addr nIP)
Definition: cNetAddr.h:246
size_t get_HostLen() const
Definition: cNetAddr.h:229
const BYTE * get_HostPtr() const
Definition: cNetAddr.h:219
void put_HostIp6(const in6_addr &addr)
Definition: cNetAddr.h:254
bool isPortValid() const
Definition: cNetAddr.h:207
const in_addr & ref_HostIp4() const
Definition: cNetAddr.h:48
in_addr & ref_HostIp4()
Definition: cNetAddr.h:53
sockaddr_in m_SockAddr4
Address is ipv4 = 16 bytes.
Definition: cNetAddr.h:43
cNetAddress(const char *pszAddr, bool bWait=false)
Definition: cNetAddr.h:99
COMPARE_t CompareAddr(const cNetAddress &SocketAddr) const
Definition: cNetAddr.h:160
cNetAddress() noexcept
Definition: cNetAddr.h:70
in6_addr & ref_HostIp6()
Definition: cNetAddr.h:63
sa_family_t get_Family() const noexcept
Definition: cNetAddr.h:119
socklen_t get_AddrLen() const noexcept
Definition: cNetAddr.h:124
void put_Addr(const cNetAddress &rAddr)
Definition: cNetAddr.h:273
socklen_t & ref_AddrLen() noexcept
Definition: cNetAddr.h:130
NET_PORT_t get_PortNumber() const noexcept
Definition: cNetAddr.h:193
sockaddr & ref_Addr() noexcept
Definition: cNetAddr.h:145
sockaddr m_SockAddr
Address that is family independent. (interchangeable with sockaddr/SOCKADDR/SOCKADDR_IN....
Definition: cNetAddr.h:42
void put_PortNumber(NET_PORT_t wPort)
Definition: cNetAddr.h:201
UNITTEST_FRIEND(cNetAddress)
const in6_addr & ref_HostIp6() const
Definition: cNetAddr.h:58
cNetAddress(const char *pszAddr, NET_PORT_t wPortDefault, bool bWait=false)
Definition: cNetAddr.h:109
BYTE * ref_HostPtr() const
Definition: cNetAddr.h:224
cNetAddress(sa_family_t nFamily, NET_PORT_t wPort=k_NET_PORT_INVALID) noexcept
Definition: cNetAddr.h:79
const sockaddr & get_Addr() const noexcept
Definition: cNetAddr.h:135
Definition: cNetHost.h:64
Definition: cNetHost.h:237
Definition: cStream.h:306
Definition: cStream.h:126
Definition: cMesh.h:22
WORD NET_PORT_t
WinINet calls ports NET_PORT_t INTERNET_PORT = a service.
Definition: cNetPort.h:21
UNITTEST2_PREDEF(cQuadtree)
cArrayStruct< cNetAddress > cNetAddressArray
Definition: cNetAddr.h:290
int COMPARE_t
result of compare. 0=same, 1=a>b, -1=a<b
Definition: cValT.h:17
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
bool operator!=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:254
bool operator==(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:250
@ COMPARE_Equal
VARCMP_EQ.
Definition: cValT.h:23
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