HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_IpAddress.h
Go to the documentation of this file.
1 /*
2  * POPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_IpAddress.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_IPADDRESS_H__
13 #define __UT_IPADDRESS_H__
14 
15 #include "UT_API.h"
16 
17 #include "UT_Assert.h"
18 #include "UT_FixedArray.h"
19 #include "UT_StringHolder.h"
20 
21 #include <SYS/SYS_Compiler.h>
22 
23 #include <cstdint>
24 #include <cstring>
25 
27 {
28  ANY,
29  IPv4,
30  IPv6
31 };
32 
33 /// This represents a Ipv4 address.
35 {
36 public:
38 
39  UT_IpAddressV4() : myAddress(0) { }
40 
41  explicit UT_IpAddressV4(uint32_t addr) { convert_(addr); }
42  explicit UT_IpAddressV4(const bytes_t& addr)
43  {
44  std::memcpy(&myAddress, addr.data(), 4);
45  }
47  unsigned char i0,
48  unsigned char i1,
49  unsigned char i2,
50  unsigned char i3)
51  {
52  convert_(i0, i1, i2, i3);
53  }
55  myAddress(addr.myAddress)
56  {}
58  {
59  myAddress = addr.myAddress;
60  return *this;
61  }
62 
63  bool operator==(const UT_IpAddressV4& other) const
64  {
65  return myAddress == other.myAddress;
66  }
67  bool operator!=(const UT_IpAddressV4& other) const
68  {
69  return !(*this == other);
70  }
71  bool operator<(const UT_IpAddressV4& other) const
72  {
73  return toUInt() < other.toUInt();
74  }
75  bool operator>(const UT_IpAddressV4& other) const
76  {
77  return other.toUInt() < toUInt();
78  }
79  bool operator<=(const UT_IpAddressV4& other) const
80  {
81  return !(other < *this);
82  }
83  bool operator>=(const UT_IpAddressV4& other) const
84  {
85  return !(*this < other);
86  }
87 
89  {
90  return toUInt() == 0;
91  }
93  {
94  return (toUInt() & 0xFF000000) == 0x7F000000;
95  }
97  {
98  return (toUInt() & 0xF0000000) == 0xE0000000;
99  }
101  {
102  bytes_t bytes;
103  std::memcpy(bytes.data(), &myAddress, 4);
104  return bytes;
105  }
106 
107  // Returns value in host byte order
108  SYS_NO_DISCARD_RESULT uint32_t toUInt() const;
109 
110  SYS_NO_DISCARD_RESULT static UT_IpAddressV4 fromString(
111  const UT_StringRef& str);
112  SYS_NO_DISCARD_RESULT UT_StringHolder toString() const;
114  {
115  return UT_IpAddressV4();
116  }
118  {
119  return UT_IpAddressV4(0x7F000001);
120  }
122  {
123  return UT_IpAddressV4(0xFFFFFFFF);
124  }
125 
126  SYS_NO_DISCARD_RESULT static uint32_t networkToHost(uint32_t v);
127  SYS_NO_DISCARD_RESULT static uint32_t hostToNetwork(uint32_t v);
128 private:
129  void convert_(uint32_t addr);
130  void convert_(uint8 i0, uint8 i1, uint8 i2, uint8 i3);
131 
132  // Stored in network byte order
133  uint32_t myAddress;
134 };
135 
136 /// This represents a Ipv6 address.
138 {
139 public:
141 
142  UT_IpAddressV6() : myAddress() { }
143  explicit UT_IpAddressV6(const bytes_t& addr) : myAddress(addr) {}
144 
145  bool operator==(const UT_IpAddressV6& other) const
146  {
147  return std::memcmp(myAddress.data(), other.myAddress.data(), 16) == 0;
148  }
149  bool operator!=(const UT_IpAddressV6& other) const
150  {
151  return !(*this == other);
152  }
153  bool operator<(const UT_IpAddressV6& other) const
154  {
155  return myAddress < other.myAddress;
156  }
157  bool operator>(const UT_IpAddressV6& other) const
158  {
159  return other < *this;
160  }
161  bool operator<=(const UT_IpAddressV6& other) const
162  {
163  return !(other < *this);
164  }
165  bool operator>=(const UT_IpAddressV6& other) const
166  {
167  return !(*this < other);
168  }
169 
171  {
172  return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
173  && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
174  && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
175  && myAddress[9] == 0 && myAddress[10] == 0 && myAddress[11] == 0
176  && myAddress[12] == 0 && myAddress[13] == 0 && myAddress[14] == 0
177  && myAddress[15] == 1;
178  }
180  {
181  return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
182  && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
183  && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
184  && myAddress[9] == 0 && myAddress[10] == 0 && myAddress[11] == 0
185  && myAddress[12] == 0 && myAddress[13] == 0 && myAddress[14] == 0
186  && myAddress[15] == 0;
187  }
189  {
190  return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
191  && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
192  && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
193  && myAddress[9] == 0 && myAddress[10] == 0xff
194  && myAddress[11] == 0xff;
195  }
197  {
198  return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0x80);
199  }
201  {
202  return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0xc0);
203  }
205  {
206  return myAddress[0] == 0xff;
207  }
209  {
210  return UT_IpAddressV6({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
211  }
212 
213  SYS_NO_DISCARD_RESULT const bytes_t& toBytes() const { return myAddress; }
214 
215  SYS_NO_DISCARD_RESULT static UT_IpAddressV6 fromString(
216  const UT_StringRef& str);
217  SYS_NO_DISCARD_RESULT UT_StringHolder toString() const;
219  {
220  UT_ASSERT(isV4Mapped());
221  return UT_IpAddressV4(
222  myAddress[12], myAddress[13], myAddress[14], myAddress[15]);
223  }
224 
225 private:
226  bytes_t myAddress;
227 };
228 
229 /// This represents either an Ipv4 address or an Ipv6 address.
231 {
232 public:
234  myType(Type::ipv4)
235  {
236  myIP.myIPv4 = UT_IpAddressV4();
237  }
239  : myType(Type::ipv4)
240  {
241  myIP.myIPv4 = UT_IpAddressV4(addr);
242  }
244  : myType(Type::ipv6)
245  {
246  myIP.myIPv6 = UT_IpAddressV6(addr);
247  }
249  unsigned char i0,
250  unsigned char i1,
251  unsigned char i2,
252  unsigned char i3)
253  : myType(Type::ipv4)
254  {
255  myIP.myIPv4 = UT_IpAddressV4(i0, i1, i2, i3);
256  }
257  UT_IpAddress(const UT_IpAddress& ip) : myType(ip.myType)
258  {
259  if (myType == Type::ipv4)
260  {
261  myIP.myIPv4 = ip.myIP.myIPv4;
262  }
263  else
264  {
265  myIP.myIPv6 = ip.myIP.myIPv6;
266  }
267  }
268  UT_IpAddress(const UT_IpAddressV4& ip) : myType(Type::ipv4)
269  {
270  myIP.myIPv4 = ip;
271  }
272  UT_IpAddress(const UT_IpAddressV6& ip) : myType(Type::ipv6)
273  {
274  myIP.myIPv6 = ip;
275  }
276 
277  bool operator==(const UT_IpAddress& other) const
278  {
279  if (isV4())
280  {
281  if (!other.isV4())
282  return false;
283  return myIP.myIPv4 == other.myIP.myIPv4;
284  }
285  return other.isV6() && myIP.myIPv6 == other.myIP.myIPv6;
286  }
287  bool operator!=(const UT_IpAddress& other) const
288  {
289  return !(*this == other);
290  }
291 
293  {
294  myType = ip.myType;
295  if (myType == Type::ipv4)
296  {
297  myIP.myIPv4 = ip.myIP.myIPv4;
298  }
299  else
300  {
301  myIP.myIPv6 = ip.myIP.myIPv6;
302  }
303  return *this;
304  }
306  {
307  myIP.myIPv4 = ip;
308  myType = Type::ipv4;
309  return *this;
310  }
312  {
313  myIP.myIPv6 = ip;
314  myType = Type::ipv6;
315  return *this;
316  }
317 
318  SYS_NO_DISCARD_RESULT bool isV4() const { return myType == Type::ipv4; }
319  SYS_NO_DISCARD_RESULT bool isV6() const { return myType == Type::ipv6; }
321  {
322  if (isV4())
323  return myIP.myIPv4.isUnspecified();
324  return myIP.myIPv6.isUnspecified();
325  }
327  {
328  if (isV4())
329  return myIP.myIPv4.isLoopback();
330  return myIP.myIPv6.isLoopback();
331  }
332 
334  const UT_StringRef& str)
335  {
336  // Check what type this address is in.
337  exint idx = str.findCharIndex(':');
338  if (idx >= 0)
339  {
341  }
343  }
344 
346  {
347  if (isV4())
348  return myIP.myIPv4.toString();
349  return myIP.myIPv6.toString();
350  }
351 
353  {
354  if (!isV4())
355  return UT_IpAddressV4();
356  return myIP.myIPv4;
357  }
359  {
360  if (!isV6())
361  return UT_IpAddressV6();
362  return myIP.myIPv6;
363  }
364 
365 private:
366  enum class Type
367  {
368  ipv4,
369  ipv6
370  };
371 
372  Type myType;
373  union IP
374  {
375  IP()
376  {
377  new(&myIPv6) UT_IpAddressV6;
378  }
379 
380  UT_IpAddressV4 myIPv4;
381  UT_IpAddressV6 myIPv6;
382  } myIP;
383 };
384 
386 {
387 public:
389  static constexpr uint32_t host_address_len = 32;
390 
392  myAddress(),
393  myPrefixLength(host_address_len)
394  {}
395  UT_IpNetworkV4(const address_t& address, uint16 prefix_length)
396  : myAddress(address), myPrefixLength(prefix_length)
397  {
398  UT_ASSERT(myPrefixLength <= host_address_len);
399  }
401  : myAddress(net.myAddress), myPrefixLength(net.myPrefixLength)
402  {
403  }
405  {
406  myAddress = other.myAddress;
407  myPrefixLength = other.myPrefixLength;
408  return *this;
409  }
410  bool operator==(const UT_IpNetworkV4& other) const
411  {
412  return myAddress == other.myAddress
413  && myPrefixLength == other.myPrefixLength;
414  }
415  bool operator!=(const UT_IpNetworkV4& other) const
416  {
417  return !(*this == other);
418  }
419  SYS_NO_DISCARD_RESULT static UT_IpNetworkV4 fromString(
420  const UT_StringRef& str);
422  {
423  return myAddress;
424  }
425  SYS_NO_DISCARD_RESULT address_t netmask() const;
427  {
428  return UT_IpAddressV4(myAddress.toUInt() & netmask().toUInt());
429  }
431  {
432  return UT_IpNetworkV4(network(), myPrefixLength);
433  }
434  SYS_NO_DISCARD_RESULT uint16_t prefixLength() const { return myPrefixLength; }
435  SYS_NO_DISCARD_RESULT bool isHost() const { return myPrefixLength == host_address_len; }
436  SYS_NO_DISCARD_RESULT bool isSubnetOf(const UT_IpNetworkV4& other) const;
438  {
439  UT_StringHolder str;
440  str.format("{}/{}", myAddress.toString(), myPrefixLength);
441  return str;
442  }
444  {
445  return UT_IpNetworkV4(UT_IpAddressV4::loopback(), host_address_len);
446  }
447 
448 private:
449  address_t myAddress;
450  uint16_t myPrefixLength;
451 };
452 
454 {
455 public:
457  static constexpr uint32_t host_address_len = 128;
458 
460  myAddress(),
461  myPrefixLength(host_address_len)
462  {}
463  UT_IpNetworkV6(const address_t& address, uint32_t prefix_length)
464  : myAddress(address), myPrefixLength(prefix_length)
465  {
466  UT_ASSERT(myPrefixLength <= host_address_len);
467  }
469  : myAddress(other.myAddress), myPrefixLength(other.myPrefixLength)
470  {
471  }
472 
474  {
475  myAddress = other.myAddress;
476  myPrefixLength = other.myPrefixLength;
477  return *this;
478  }
479  bool operator==(const UT_IpNetworkV6& net) const
480  {
481  return myAddress == net.myAddress
482  && myPrefixLength == net.myPrefixLength;
483  }
484  bool operator!=(const UT_IpNetworkV6& net) const { return !(*this == net); }
485 
486  SYS_NO_DISCARD_RESULT static UT_IpNetworkV6 fromString(
487  const UT_StringRef& str);
488  SYS_NO_DISCARD_RESULT address_t network() const;
490  {
491  return myAddress;
492  }
494  {
495  return UT_IpNetworkV6(network(), myPrefixLength);
496  }
497  SYS_NO_DISCARD_RESULT uint32_t prefixLength() const { return myPrefixLength; }
498  SYS_NO_DISCARD_RESULT bool isHost() const { return myPrefixLength == host_address_len; }
499  SYS_NO_DISCARD_RESULT bool isSubnetOf(const UT_IpNetworkV6& net) const;
501  {
502  UT_StringHolder str;
503  str.format("{}/{}", myAddress.toString(), myPrefixLength);
504  return str;
505  }
507  {
508  return UT_IpNetworkV6(UT_IpAddressV6::loopback(), host_address_len);
509  }
510 private:
511  address_t myAddress;
512  uint32_t myPrefixLength;
513 };
514 
515 #endif // __UT_IPADDRESS_H__
516 
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
Definition: UT_IpAddress.h:345
SYS_NO_DISCARD_RESULT bool isV4Mapped() const
Definition: UT_IpAddress.h:188
bool operator>(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:75
unsigned short uint16
Definition: SYS_Types.h:38
UT_IpAddressV4(const bytes_t &addr)
Definition: UT_IpAddress.h:42
UT_IpAddress(const UT_IpAddress &ip)
Definition: UT_IpAddress.h:257
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
Definition: UT_IpAddress.h:437
UT_IpAddress(const UT_IpAddressV6 &ip)
Definition: UT_IpAddress.h:272
UT_IpAddressV4(const UT_IpAddressV4 &addr)
Definition: UT_IpAddress.h:54
static SYS_NO_DISCARD_RESULT UT_IpNetworkV4 loopback()
Definition: UT_IpAddress.h:443
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
Definition: UT_IpAddress.h:500
UT_IpAddress(const UT_IpAddressV4::bytes_t &addr)
Definition: UT_IpAddress.h:238
This represents a Ipv4 address.
Definition: UT_IpAddress.h:34
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 loopback()
Definition: UT_IpAddress.h:117
bool operator>=(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:165
int64 exint
Definition: SYS_Types.h:125
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 broadcast()
Definition: UT_IpAddress.h:121
bool operator==(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:145
SYS_NO_DISCARD_RESULT bool isUnspecified() const
Definition: UT_IpAddress.h:179
SYS_NO_DISCARD_RESULT const address_t & address() const
Definition: UT_IpAddress.h:489
SYS_NO_DISCARD_RESULT uint32_t prefixLength() const
Definition: UT_IpAddress.h:497
static SYS_NO_DISCARD_RESULT UT_IpAddressV6 fromString(const UT_StringRef &str)
#define UT_API
Definition: UT_API.h:14
UT_IpAddress(unsigned char i0, unsigned char i1, unsigned char i2, unsigned char i3)
Definition: UT_IpAddress.h:248
bool operator==(const UT_IpAddress &other) const
Definition: UT_IpAddress.h:277
SYS_NO_DISCARD_RESULT bool isHost() const
Definition: UT_IpAddress.h:498
bool operator<=(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:79
bool operator==(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:63
UT_IpAddressV4(uint32_t addr)
Definition: UT_IpAddress.h:41
bool operator!=(const UT_IpAddress &other) const
Definition: UT_IpAddress.h:287
UT_IpNetworkV4 & operator=(const UT_IpNetworkV4 &other)
Definition: UT_IpAddress.h:404
unsigned char uint8
Definition: SYS_Types.h:36
SYS_NO_DISCARD_RESULT UT_IpNetworkV4 canonical() const
Definition: UT_IpAddress.h:430
SYS_NO_DISCARD_RESULT bool isMulticast() const
Definition: UT_IpAddress.h:96
UT_IpAddress & operator=(const UT_IpAddress &ip)
Definition: UT_IpAddress.h:292
bool operator>=(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:83
This represents either an Ipv4 address or an Ipv6 address.
Definition: UT_IpAddress.h:230
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 any()
Definition: UT_IpAddress.h:113
std::array< T, N > UT_FixedArray
Definition: UT_FixedArray.h:18
UT_IpAddressV4 & operator=(const UT_IpAddressV4 &addr)
Definition: UT_IpAddress.h:57
SYS_NO_DISCARD_RESULT bool isLoopback() const
Definition: UT_IpAddress.h:170
SYS_FORCE_INLINE exint findCharIndex(char c) const
Find the location of the character (or -1 if not found)
UT_IpNetworkV6(const address_t &address, uint32_t prefix_length)
Definition: UT_IpAddress.h:463
GLenum void * addr
Definition: glew.h:11547
SYS_NO_DISCARD_RESULT bool isV6() const
Definition: UT_IpAddress.h:319
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
Definition: UT_IpAddress.h:218
const GLdouble * v
Definition: glcorearb.h:836
UT_IpAddressV4(unsigned char i0, unsigned char i1, unsigned char i2, unsigned char i3)
Definition: UT_IpAddress.h:46
UT_IpAddress & operator=(const UT_IpAddressV6 &ip)
Definition: UT_IpAddress.h:311
bool operator!=(const UT_IpNetworkV6 &net) const
Definition: UT_IpAddress.h:484
bool operator<(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:71
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 fromString(const UT_StringRef &str)
bool operator==(const UT_IpNetworkV6 &net) const
Definition: UT_IpAddress.h:479
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
UT_IpAddress(const UT_IpAddressV4 &ip)
Definition: UT_IpAddress.h:268
UT_IpAddress(const UT_IpAddressV6::bytes_t &addr)
Definition: UT_IpAddress.h:243
SYS_NO_DISCARD_RESULT bool isSiteLocal() const
Definition: UT_IpAddress.h:200
bool operator>(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:157
UT_IpNetworkV4(const address_t &address, uint16 prefix_length)
Definition: UT_IpAddress.h:395
bool operator!=(const UT_IpAddressV4 &other) const
Definition: UT_IpAddress.h:67
GLuint GLuint64EXT address
Definition: glew.h:14913
SYS_NO_DISCARD_RESULT bool isLoopback() const
Definition: UT_IpAddress.h:326
size_t format(const char *fmt, const Args &...args)
Format a string using the same formatting codes as UTformat.
bool operator<(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:153
UT_IpNetworkV6(const UT_IpNetworkV6 &other)
Definition: UT_IpAddress.h:468
SYS_NO_DISCARD_RESULT address_t network() const
Definition: UT_IpAddress.h:426
SYS_NO_DISCARD_RESULT const address_t & address() const
Definition: UT_IpAddress.h:421
bool operator!=(const UT_IpNetworkV4 &other) const
Definition: UT_IpAddress.h:415
UT_FixedArray< uint8, 4 > bytes_t
Definition: UT_IpAddress.h:37
SYS_NO_DISCARD_RESULT UT_IpNetworkV6 canonical() const
Definition: UT_IpAddress.h:493
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
Definition: UT_IpAddress.h:352
UT_IpNetworkV4(const UT_IpNetworkV4 &net)
Definition: UT_IpAddress.h:400
SYS_NO_DISCARD_RESULT bool isV4() const
Definition: UT_IpAddress.h:318
This represents a Ipv6 address.
Definition: UT_IpAddress.h:137
static SYS_NO_DISCARD_RESULT UT_IpAddress fromString(const UT_StringRef &str)
Definition: UT_IpAddress.h:333
SYS_NO_DISCARD_RESULT UT_IpAddressV6 toV6() const
Definition: UT_IpAddress.h:358
SYS_NO_DISCARD_RESULT uint32_t toUInt() const
SYS_NO_DISCARD_RESULT bool isHost() const
Definition: UT_IpAddress.h:435
UT_FixedArray< uint8, 16 > bytes_t
Definition: UT_IpAddress.h:140
SYS_NO_DISCARD_RESULT bool isUnspecified() const
Definition: UT_IpAddress.h:88
SYS_NO_DISCARD_RESULT const bytes_t toBytes() const
Definition: UT_IpAddress.h:100
SYS_NO_DISCARD_RESULT uint16_t prefixLength() const
Definition: UT_IpAddress.h:434
bool operator==(const UT_IpNetworkV4 &other) const
Definition: UT_IpAddress.h:410
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:171
UT_IpNetworkV6 & operator=(const UT_IpNetworkV6 &other)
Definition: UT_IpAddress.h:473
SYS_NO_DISCARD_RESULT bool isMulticast() const
Definition: UT_IpAddress.h:204
bool operator!=(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:149
static SYS_NO_DISCARD_RESULT UT_IpAddressV6 loopback()
Definition: UT_IpAddress.h:208
SYS_NO_DISCARD_RESULT bool isLinkLocal() const
Definition: UT_IpAddress.h:196
UT_IpAddressV6(const bytes_t &addr)
Definition: UT_IpAddress.h:143
bool operator<=(const UT_IpAddressV6 &other) const
Definition: UT_IpAddress.h:161
UT_IpAddress & operator=(const UT_IpAddressV4 &ip)
Definition: UT_IpAddress.h:305
SYS_NO_DISCARD_RESULT bool isLoopback() const
Definition: UT_IpAddress.h:92
Definition: format.h:3611
SYS_NO_DISCARD_RESULT const bytes_t & toBytes() const
Definition: UT_IpAddress.h:213
UT_IpAddressFamily
Definition: UT_IpAddress.h:26
static SYS_NO_DISCARD_RESULT UT_IpNetworkV6 loopback()
Definition: UT_IpAddress.h:506
SYS_NO_DISCARD_RESULT bool isUnspecified() const
Definition: UT_IpAddress.h:320