NetBurner 3.5.0
PDF Version
 
ipv6_addr.h
Go to the documentation of this file.
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
74 {
75 IPADDR4 i4;
76 i4 = val[3];
77 return i4;
78 };
79
80 // Operator = overload
81 IPADDR6 &operator=(const IPADDR4 v4)
82 {
83 val[0] = 0;
84 val[1] = 0;
85 val[2] = 0xFFFF;
86 val[3] = v4;
87 return *this;
88 }
89
90 //---------------------------------------------------------------------------------------
91 // Member functions to query IP address status
92 //---------------------------------------------------------------------------------------
93
101 bool IsNull() const { return ((val[0] | val[1] | val[3] | ((val[2] != 0xFFFF) && (val[2] != 0))) == 0); }
102
110 bool NotNull() const { return !IsNull(); }
111
119 inline bool IsLoopBack() const { if(IsEmbeddedIPV4())
120 { return Extract4().IsLoopBack(); }
121 else
122 { return ((val[3] == 1) && (val[2] == 0) && (val[1] == 0) && (val[0] == 0)); }
123 };
124
132 inline bool IsMultiCast() const { return ((val[0] & 0xff000000) == 0xff000000); };
133
143 inline bool IsLinkLocal() const { return ((val[0] & 0xffc00000) == 0xfe800000); };
144
152
153 //---------------------------------------------------------------------------------------
154 // Member functions to display/print an IP address
155 //---------------------------------------------------------------------------------------
156
166 void print(bool bCompact = true, bool bShowV4Raw = false) const;
167
178 void fdprint(int fd, bool bCompact = true, bool bShowV4Raw = false) const;
179
193 int sprintf(char *cp, int maxl, bool bCompact = true, bool bShowV4Raw = false) const;
194
195 //---------------------------------------------------------------------------------------
196 // Static functions that return an IPADDR6 object
197 //---------------------------------------------------------------------------------------
198
209 static IPADDR6 AsciiToIp6(const char *cp, bool bembed_v4addresses = true); // false only parses a v6
210
211 // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
212 static IPADDR6 FromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
213
214 // Internal use only
215 static IPADDR6 SolicitedNodeIP6(const IPADDR6 &ip6);
216
234 static IPADDR6 NullIP();
235
236 //---------------------------------------------------------------------------------------
237 // Member functions that correspond to the static functions
238 //---------------------------------------------------------------------------------------
239
248 void SetFromAscii(const char *cp, bool bembed_v4addresses = true);
249
250 void SetGlobal(MACADR &ma, const IPADDR6 &g_root); // Internal use only
251 void SetSolicitedNodeIP6(const IPADDR6 &ip6); // Internal use only
252
253 // Used internally to generate auto-configuration addresses, such as a prefix received from a router.
254 void SetFromIPMask(MACADR &ma, const IPADDR6 &g_root, int mask_len);
255
264
275 void SetFromUint32Shortcut(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
276 {
277 val[0] = w0;
278 val[1] = w1;
279 val[2] = w2;
280 val[3] = w3;
281 }
282
288 void SetNull()
289 {
290 val[0] = 0;
291 val[1] = 0;
292 val[2] = 0;
293 val[3] = 0;
294 }
295
296 uint32_t csum(); // Calculate the inet pre csum for this
297
298 //---------------------------------------------------------------------------------------
299 // Constructors
300 //---------------------------------------------------------------------------------------
301
302 IPADDR6() { SetNull(); };
303 IPADDR6(IPADDR4 ip4) { SetFromIP4(ip4); };
304 IPADDR6(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) { SetFromUint32Shortcut(w0, w1, w2, w3); };
305
306 //---------------------------------------------------------------------------------------
307 // Internal use only, used by system printf functions
308 //---------------------------------------------------------------------------------------
309
310 int GetPrintLen(bool compact);
311 int PrintHelper(PutCharsFunction *pf, void *data, bool compact);
312
313 // Access the uint32_t values
314 beuint32_t inline GetInternalValue(int i) const { return val[i]; };
315
316 //---------------------------------------------------------------------------------------
317 // Friend functions for operator overloading
318 //---------------------------------------------------------------------------------------
319
320 friend bool operator==(const IPADDR6 i, const IPADDR6 j);
321 friend bool operator!=(const IPADDR6 i, const IPADDR6 j);
322 friend bool operator>(const IPADDR6 i, const IPADDR6 j);
323 friend bool operator<(const IPADDR6 i, const IPADDR6 j);
324
325} __attribute__((packed));
326
327//---------------------------------------------------------------------------------------
328// friend function implementations for operator overload
329//---------------------------------------------------------------------------------------
330
331inline bool operator==(const IPADDR6 i, const IPADDR6 j)
332{
333 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]));
334}
335
336inline bool operator!=(const IPADDR6 i, const IPADDR6 j)
337{
338 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]));
339}
340
341inline bool operator>(const IPADDR6 i, const IPADDR6 j)
342{
343 if (i.val[0] > j.val[0]) return true;
344
345 if (i.val[0] == j.val[0])
346 {
347 if (i.val[1] > j.val[1]) return true;
348
349 if (i.val[1] == j.val[1])
350 {
351 if (i.val[2] > j.val[2]) return true;
352
353 if (i.val[2] == j.val[2])
354 {
355 if (i.val[3] > j.val[3]) return true;
356 }
357 }
358 }
359 return false;
360};
361
362inline bool operator<(const IPADDR6 i, const IPADDR6 j)
363{
364 if (i.val[0] < j.val[0]) return true;
365
366 if (i.val[0] == j.val[0])
367 {
368 if (i.val[1] < j.val[1]) return true;
369 if (i.val[1] == j.val[1])
370 {
371 if (i.val[2] < j.val[2]) return true;
372 if (i.val[2] == j.val[2])
373 {
374 if (i.val[3] < j.val[3]) return true;
375 }
376 }
377 }
378 return false;
379};
380
381#endif
382
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:208
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition nettypes.h:287
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:73
bool IsLinkLocal() const
Check if the IP address is the link-local address for the interface.
Definition ipv6_addr.h:143
void SetFromIP4(IPADDR4 ip)
Set the IP address value of an IPADDR6 object from an IPADD4 object.
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:288
bool IsMultiCast() const
Check if the IPADDR6 object contains a Multicast IP address the interface.
Definition ipv6_addr.h:132
bool IsNull() const
Check if the IP address is null.
Definition ipv6_addr.h:101
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:275
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:119
bool NotNull() const
Check if the IP address is not null.
Definition ipv6_addr.h:110
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.