NetBurner 3.5.6
PDF Version
ipv6_addr.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
25#ifndef IPV6_ADDR_H
26#define IPV6_ADDR_H
27
28struct IPADDR6; // Forward
29
30#include <basictypes.h>
31#include <nbprintfinternal.h>
32#include <nettypes.h>
33
41{
42 protected:
43 void Compactformat(char *cp) const;
44 beuint32_t val[4];
45
46 public:
47 // IPv6 operator = overload
48 IPADDR6 &operator=(const IPADDR6 &v)
49 {
50 val[0] = v.val[0];
51 val[1] = v.val[1];
52 val[2] = v.val[2];
53 val[3] = v.val[3];
54 return *this;
55 }
56
66 bool IsEmbeddedIPV4() const { return ((val[2] == 0xFFFF) && (val[0] == 0) && (val[1] == 0)); }
67
81 {
82 IPADDR4 i4;
83 if(IsEmbeddedIPV4())
84 i4 = val[3];
85 else
86 i4=0;
87 return i4;
88 };
89
104 uint32_t GetEmbededIP4Uint()const
105 {
106 if(!IsEmbeddedIPV4()) return 0;
107 return (uint32_t) val[3];
108
109 }
110
111
112 // Operator = overload
113 IPADDR6 &operator=(const IPADDR4 v4)
114 {
115 val[0] = 0;
116 val[1] = 0;
117 val[2] = 0xFFFF;
118 val[3] = v4;
119 return *this;
120 }
121
122 //---------------------------------------------------------------------------------------
123 // Member functions to query IP address status
124 //---------------------------------------------------------------------------------------
125
133 bool IsNull() const { return ((val[0] | val[1] | val[3] | ((val[2] != 0xFFFF) && (val[2] != 0))) == 0); }
134
142 bool NotNull() const { return !IsNull(); }
143
151 inline bool IsLoopBack() const { if(IsEmbeddedIPV4())
152 { return Extract4().IsLoopBack(); }
153 else
154 { return ((val[3] == 1) && (val[2] == 0) && (val[1] == 0) && (val[0] == 0)); }
155 };
156
164 inline bool IsMultiCast() const { return ((val[0] & 0xff000000) == 0xff000000); };
165
175 inline bool IsLinkLocal() const { return ((val[0] & 0xffc00000) == 0xfe800000); };
176
184
185 //---------------------------------------------------------------------------------------
186 // Member functions to display/print an IP address
187 //---------------------------------------------------------------------------------------
188
198 void print(bool bCompact = true, bool bShowV4Raw = false) const;
199
210 void fdprint(int fd, bool bCompact = true, bool bShowV4Raw = false) const;
211
225 int sprintf(char *cp, int maxl, bool bCompact = true, bool bShowV4Raw = false) const;
226
227 //---------------------------------------------------------------------------------------
228 // Static functions that return an IPADDR6 object
229 //---------------------------------------------------------------------------------------
230
241 static IPADDR6 AsciiToIp6(const char *cp, bool bembed_v4addresses = true); // false only parses a v6
242
243 // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
244 static IPADDR6 FromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
245
246 // Internal use only
247 static IPADDR6 SolicitedNodeIP6(const IPADDR6 &ip6);
248
266 static IPADDR6 NullIP();
267
268 //---------------------------------------------------------------------------------------
269 // Member functions that correspond to the static functions
270 //---------------------------------------------------------------------------------------
271
280 void SetFromAscii(const char *cp, bool bembed_v4addresses = true);
281
282 void SetGlobal(MACADR &ma, const IPADDR6 &g_root); // Internal use only
283 void SetSolicitedNodeIP6(const IPADDR6 &ip6); // Internal use only
284
285 // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
286 void SetFromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
287
296
307 void SetFromUint32Shortcut(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
308 {
309 val[0] = w0;
310 val[1] = w1;
311 val[2] = w2;
312 val[3] = w3;
313 }
314
320 void SetNull()
321 {
322 val[0] = 0;
323 val[1] = 0;
324 val[2] = 0;
325 val[3] = 0;
326 }
327
328 uint32_t csum(); // Calculate the inet pre csum for this
329
330 //---------------------------------------------------------------------------------------
331 // Constructors
332 //---------------------------------------------------------------------------------------
333
334 IPADDR6() { SetNull(); };
335 IPADDR6(IPADDR4 ip4) { SetFromIP4(ip4); };
336 IPADDR6(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) { SetFromUint32Shortcut(w0, w1, w2, w3); };
337
338 //---------------------------------------------------------------------------------------
339 // Internal use only, used by system printf functions
340 //---------------------------------------------------------------------------------------
341
342 int GetPrintLen(bool compact);
343 int PrintHelper(PutCharsFunction *pf, void *data, bool compact);
344
345 // Access the uint32_t values
346 beuint32_t inline GetInternalValue(int i) const { return val[i]; };
347
348 //---------------------------------------------------------------------------------------
349 // Friend functions for operator overloading
350 //---------------------------------------------------------------------------------------
351
352 friend bool operator==(const IPADDR6 i, const IPADDR6 j);
353 friend bool operator!=(const IPADDR6 i, const IPADDR6 j);
354 friend bool operator>(const IPADDR6 i, const IPADDR6 j);
355 friend bool operator<(const IPADDR6 i, const IPADDR6 j);
356
357} __attribute__((packed));
358
359//---------------------------------------------------------------------------------------
360// friend function implementations for operator overload
361//---------------------------------------------------------------------------------------
362
363inline bool operator==(const IPADDR6 i, const IPADDR6 j)
364{
365 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]));
366}
367
368inline bool operator!=(const IPADDR6 i, const IPADDR6 j)
369{
370 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]));
371}
372
373inline bool operator>(const IPADDR6 i, const IPADDR6 j)
374{
375 if (i.val[0] > j.val[0]) return true;
376
377 if (i.val[0] == j.val[0])
378 {
379 if (i.val[1] > j.val[1]) return true;
380
381 if (i.val[1] == j.val[1])
382 {
383 if (i.val[2] > j.val[2]) return true;
384
385 if (i.val[2] == j.val[2])
386 {
387 if (i.val[3] > j.val[3]) return true;
388 }
389 }
390 }
391 return false;
392};
393
394inline bool operator<(const IPADDR6 i, const IPADDR6 j)
395{
396 if (i.val[0] < j.val[0]) return true;
397
398 if (i.val[0] == j.val[0])
399 {
400 if (i.val[1] < j.val[1]) return true;
401 if (i.val[1] == j.val[1])
402 {
403 if (i.val[2] < j.val[2]) return true;
404 if (i.val[2] == j.val[2])
405 {
406 if (i.val[3] < j.val[3]) return true;
407 }
408 }
409 }
410 return false;
411};
412
413#endif
414
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition nettypes.h:304
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
void print(bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address value to stdout.
int sprintf(char *cp, int maxl, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified buffer.
void SetFromAscii(const char *cp, bool bembed_v4addresses=true)
Set the IP address value of an IPADDR6 object.
IPADDR4 Extract4() const
Extracts an IPv4 address from the object.
Definition ipv6_addr.h:80
bool IsLinkLocal() const
Check if the IP address is the link-local address for the interface.
Definition ipv6_addr.h:175
void SetFromIP4(IPADDR4 ip)
Set the IP address value of an IPADDR6 object from an IPADD4 object.
uint32_t GetEmbededIP4Uint() const
Get the embedded IPv4 address as a 32-bit unsigned integer.
Definition ipv6_addr.h:104
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:66
MACADR McastMac() const
Return the MAC address used for Multicasts for the interface.
void SetNull()
Set the IP address value of an IPADDR6 object to null.
Definition ipv6_addr.h:320
bool IsMultiCast() const
Check if the IPADDR6 object contains a Multicast IP address the interface.
Definition ipv6_addr.h:164
bool IsNull() const
Check if the IP address is null.
Definition ipv6_addr.h:133
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:307
static IPADDR6 NullIP()
Static function to return a null IPADDR6 object.
void fdprint(int fd, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified file descriptor.
static IPADDR6 AsciiToIp6(const char *cp, bool bembed_v4addresses=true)
Static function to return an IPADDR6 object created from an ASCII value IPv4 or IPv6 address.
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition ipv6_addr.h:151
bool NotNull() const
Check if the IP address is not null.
Definition ipv6_addr.h:142
Used to store and manipulate MAC addresses.
Definition nettypes.h:69