NetBurner 3.1
ipv6_addr.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
14 #ifndef IPV6_ADDR_H
15 #define IPV6_ADDR_H
16 
17 struct IPADDR6; // Forward
18 
19 #include <basictypes.h>
20 #include <nbprintfinternal.h>
21 #include <nettypes.h>
22 
28 struct IPADDR6
29 {
30  protected:
31  void Compactformat(char *cp) const;
32  beuint32_t val[4];
33 
34  public:
35  // IPv6 operator = overload
36  IPADDR6 &operator=(const IPADDR6 &v)
37  {
38  val[0] = v.val[0];
39  val[1] = v.val[1];
40  val[2] = v.val[2];
41  val[3] = v.val[3];
42  return *this;
43  }
44 
55  bool IsEmbeddedIPV4() const { return ((val[2] == 0xFFFF) && (val[0] == 0) && (val[1] == 0)); }
56 
63  IPADDR4 Extract4() const
64  {
65  IPADDR4 i4;
66  i4 = val[3];
67  return i4;
68  };
69 
70  // Operator = overload
71  IPADDR6 &operator=(const IPADDR4 v4)
72  {
73  val[0] = 0;
74  val[1] = 0;
75  val[2] = 0xFFFF;
76  val[3] = v4;
77  return *this;
78  }
79 
80  //---------------------------------------------------------------------------------------
81  // Member functions to query IP address status
82  //---------------------------------------------------------------------------------------
83 
91  bool IsNull() const { return ((val[0] | val[1] | val[3] | ((val[2] != 0xFFFF) && (val[2] != 0))) == 0); }
92 
100  bool NotNull() const { return !IsNull(); }
101 
109  inline bool IsLoopBack() const { return ((val[3] == 1) && (val[2] == 0) && (val[1] == 0) && (val[0] == 0)); };
110 
118  inline bool IsMultiCast() const { return ((val[0] & 0xff000000) == 0xff000000); };
119 
120  // Link local is FE80::/10, need to mask the top 10 bits and check
128  inline bool IsLinkLocal() const { return ((val[0] & 0xffc00000) == 0xfe800000); };
129 
137  MACADR McastMac() const;
138 
139  //---------------------------------------------------------------------------------------
140  // Member functions to display/print an IP address
141  //---------------------------------------------------------------------------------------
142 
152  void print(bool bCompact = true, bool bShowV4Raw = false) const;
153 
165  void fdprint(int fd, bool bCompact = true, bool bShowV4Raw = false) const;
166 
180  int sprintf(char *cp, int maxl, bool bCompact = true, bool bShowV4Raw = false) const;
181 
182  //---------------------------------------------------------------------------------------
183  // Static functions that return an IPADDR6 object
184  //---------------------------------------------------------------------------------------
185 
196  static IPADDR6 AsciiToIp6(const char *cp, bool bembed_v4addreses = true); // false only parses a v6
197 
198  // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
199  static IPADDR6 FromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
200 
201  // Internal use only
202  static IPADDR6 SolicitedNodeIP6(const IPADDR6 &ip6);
203 
211  static IPADDR6 NullIP();
212 
213  //---------------------------------------------------------------------------------------
214  // Member functions that correspond to the static functions
215  //---------------------------------------------------------------------------------------
216 
225  void SetFromAscii(const char *cp, bool bembed_v4addreses = true);
226 
227  void SetGlobal(MACADR &ma, const IPADDR6 &g_root); // Internal use only
228  void SetSolicitedNodeIP6(const IPADDR6 &ip6); // Internal use only
229 
230  // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
231  void SetFromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
232 
240  void SetFromIP4(IPADDR4 ip);
241 
252  void SetFromUint32Shortcut(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
253  {
254  val[0] = w0;
255  val[1] = w1;
256  val[2] = w2;
257  val[3] = w3;
258  }
259 
265  void SetNull()
266  {
267  val[0] = 0;
268  val[1] = 0;
269  val[2] = 0;
270  val[3] = 0;
271  }
272 
273  uint32_t csum(); // Calculate the inet pre csum for this
274 
275  //---------------------------------------------------------------------------------------
276  // Constructors
277  //---------------------------------------------------------------------------------------
278 
279  IPADDR6() { SetNull(); };
280  IPADDR6(IPADDR4 ip4) { SetFromIP4(ip4); };
281  IPADDR6(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) { SetFromUint32Shortcut(w0, w1, w2, w3); };
282 
283  //---------------------------------------------------------------------------------------
284  // Internal use only, used by system printf functions
285  //---------------------------------------------------------------------------------------
286 
287  int GetPrintLen(bool compact);
288  int PrintHelper(PutCharsFunction *pf, void *data, bool compact);
289 
290  // Access the uint32_t values
291  beuint32_t inline GetInternalValue(int i) const { return val[i]; };
292 
293  //---------------------------------------------------------------------------------------
294  // Friend functions for operator overloading
295  //---------------------------------------------------------------------------------------
296 
297  friend bool operator==(const IPADDR6 i, const IPADDR6 j);
298  friend bool operator!=(const IPADDR6 i, const IPADDR6 j);
299  friend bool operator>(const IPADDR6 i, const IPADDR6 j);
300  friend bool operator<(const IPADDR6 i, const IPADDR6 j);
301 
302 } __attribute__((packed));
303 
304 //---------------------------------------------------------------------------------------
305 // friend function implementations for operator overload
306 //---------------------------------------------------------------------------------------
307 
308 inline bool operator==(const IPADDR6 i, const IPADDR6 j)
309 {
310  return ((i.val[3] == j.val[3]) && (i.val[2] == j.val[2]) && (i.val[1] == j.val[1]) && (i.val[0] == j.val[0]));
311 }
312 
313 inline bool operator!=(const IPADDR6 i, const IPADDR6 j)
314 {
315  return !((i.val[3] == j.val[3]) && (i.val[2] == j.val[2]) && (i.val[1] == j.val[1]) && (i.val[0] == j.val[0]));
316 }
317 
318 inline bool operator>(const IPADDR6 i, const IPADDR6 j)
319 {
320  if (i.val[0] > j.val[0]) return true;
321 
322  if (i.val[0] == j.val[0])
323  {
324  if (i.val[1] > j.val[1]) return true;
325 
326  if (i.val[1] == j.val[1])
327  {
328  if (i.val[2] > j.val[2]) return true;
329 
330  if (i.val[2] == j.val[2])
331  {
332  if (i.val[3] > j.val[3]) return true;
333  }
334  }
335  }
336  return false;
337 };
338 
339 inline bool operator<(const IPADDR6 i, const IPADDR6 j)
340 {
341  if (i.val[0] < j.val[0]) return true;
342 
343  if (i.val[0] == j.val[0])
344  {
345  if (i.val[1] < j.val[1]) return true;
346  if (i.val[1] == j.val[1])
347  {
348  if (i.val[2] < j.val[2]) return true;
349  if (i.val[2] == j.val[2])
350  {
351  if (i.val[3] < j.val[3]) return true;
352  }
353  }
354  }
355  return false;
356 };
357 
358 #endif
static IPADDR6 AsciiToIp6(const char *cp, bool bembed_v4addreses=true)
Return an IPADDR6 object created from an ASCII value IPv4 or IPv6 address.
Definition: ipv6_addr.cpp:322
static IPADDR6 NullIP()
Return a null IPADDR6 object.
Definition: ipv6_addr.cpp:334
void SetFromIP4(IPADDR4 ip)
Set the IP address value of an IPADDR6 object from an IPADD4 object.
Definition: ipv6_addr.cpp:441
bool IsEmbeddedIPV4() const
An IPADDR6 object can store a IPv4 or IPv6 address. This function returns true if the instance contai...
Definition: ipv6_addr.h:55
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition: ipv6_addr.h:28
void fdprint(int fd, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified file descriptor.
Definition: ipv6_addr.cpp:120
void SetFromUint32Shortcut(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
Set the IP address value of an IPADDR6 object from 4 discrete uint32_t values.
Definition: ipv6_addr.h:252
bool IsMultiCast() const
Check if the IPADDR6 object contains a Multicast IP address the interface.
Definition: ipv6_addr.h:118
void print(bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address value to stdout.
Definition: ipv6_addr.cpp:101
void SetNull()
Set the IP address value of an IPADDR6 object to null.
Definition: ipv6_addr.h:265
int sprintf(char *cp, int maxl, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified buffer.
Definition: ipv6_addr.cpp:138
IPADDR4 Extract4() const
Extracts an IPv4 address from the object.
Definition: ipv6_addr.h:63
MACADR McastMac() const
Return the MAC address used for Multicasts for the interface.
Definition: ipv6_addr.cpp:19
bool IsLinkLocal() const
Check if the IP address is the link-local address for the interface.
Definition: ipv6_addr.h:128
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition: ipv6_addr.h:109
bool IsNull() const
Check if the IP address is null.
Definition: ipv6_addr.h:91
bool NotNull() const
Check if the IP address is not null.
Definition: ipv6_addr.h:100
void SetFromAscii(const char *cp, bool bembed_v4addreses=true)
Set the IP address value of an IPADDR6 object.
Definition: ipv6_addr.cpp:186