NetBurner 3.5.6
PDF Version
nettypes.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
27#ifndef NB_NET_TYPES_H
28#define NB_NET_TYPES_H
29
30#include <predef.h>
31#include <basictypes.h>
32
33typedef int(PutCharsFunction)(void *data, const char *chars, int len);
34
35/*
36 *******************************************************************************
37 *
38 * Definitions
39 *
40 *******************************************************************************
41 */
42
43#define PPP_TYPE (0x0021) /* These are Big Endian constants! */
44#define EIP_TYPE (0x0800) /* These are Big Endian constants! */
45#define EIP6_TYPE (0x86DD) /* These are Big Endian constants! */
46#define HARD_ENET (0x0001) /* These are Big Endian constants! */
47#define VLAN_TYPE (0x8100)
48#define EARP_TYPE (0x0806)
49#define ARP_REQUEST (0x01)
50#define ARP_RESPONSE (0x02)
51
52/*
53 * Media Access Control (MAC) address size in 8 bit bytes and 16 bit words
54 */
55#define MACADDRESS_OCTETS_48 (6)
56#define MACADDRESS_WORDS_48 (3)
57typedef struct _MACADDRESS_48
58{
59 uint8_t octet[MACADDRESS_OCTETS_48];
60} __attribute__((packed)) MACADDRESS_48;
61
62class MACADR; // Forward
63
68typedef class MACADR
69{
70 public:
71 beuint16_t phywadr[MACADDRESS_WORDS_48] = {0};
76 inline bool IsNull() { return ((phywadr[2] == 0) && (phywadr[1] == 0) && (phywadr[0] == 0)); };
81 inline bool IsMultiCast() { return (phywadr[0] & 0x0100); };
86 inline bool IsBroadCast() { return ((phywadr[0] == 0xFFFF) && (phywadr[1] == 0xFFFF) && (phywadr[2] == 0xFFFF)); };
91 uint8_t GetByte(int n) const
92 {
93 //#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
94 // if (n&1) { return ((phywadr[n/2]>>8)&0xFF); }
95 // else { return (phywadr[n/2]&0xFF); }
96 //#else
97 if (n & 1) { return (phywadr[n / 2] & 0xFF); }
98 else
99 {
100 return ((phywadr[n / 2] >> 8) & 0xFF);
101 }
102 //#endif
103 };
108 inline void SetFromBytes(const uint8_t *pb)
109 {
110 uint8_t *pto = (uint8_t *)phywadr;
111 for (int i = 0; i < 6; i++)
112 {
113 pto[i] = pb[i];
114 }
115 };
120 void fdprint(int fd);
125 inline void print() { fdprint(1); };
130 MACADR operator+(uint32_t rhs)
131 {
132 MACADR ret = *this;
133 uint32_t tmp;
134 tmp = ret.phywadr[2] + rhs;
135 do {
136 ret.phywadr[2] = tmp & 0xffff;
137 tmp >>= 16;
138 tmp = phywadr[1] + tmp;
139 ret.phywadr[1] = tmp & 0xffff;
140 tmp >>= 16;
141 tmp = phywadr[0] + tmp;
142 ret.phywadr[0] = tmp & 0xffff;
143 tmp >>= 16;
144 } while (tmp && ((tmp = ret.phywadr[2] + rhs)));
145 return ret;
146 }
147 MACADR operator-(uint32_t rhs)
148 {
149 MACADR ret = *this;
150 uint32_t tmp;
151 tmp = ret.phywadr[2] - rhs;
152 do {
153 ret.phywadr[2] = tmp & 0xffff;
154 tmp >>= 16;
155 tmp = phywadr[1] - tmp;
156 ret.phywadr[1] = tmp & 0xffff;
157 tmp >>= 16;
158 tmp = phywadr[0] - tmp;
159 ret.phywadr[0] = tmp & 0xffff;
160 tmp >>= 16;
161 } while (tmp && ((tmp = ret.phywadr[2] - rhs)));
162 return ret;
163 }
164} __attribute__((packed)) MACADR;
165
170inline bool operator==(const MACADR &i, const MACADR &j)
171{
172 if (i.phywadr[0] != j.phywadr[0]) return FALSE;
173 if (i.phywadr[1] != j.phywadr[1]) return FALSE;
174 if (i.phywadr[2] != j.phywadr[2]) return FALSE;
175 return TRUE;
176}
177
182inline bool operator!=(const MACADR &i, const MACADR &j)
183{
184 if (i.phywadr[0] != j.phywadr[0]) return TRUE;
185 if (i.phywadr[1] != j.phywadr[1]) return TRUE;
186 if (i.phywadr[2] != j.phywadr[2]) return TRUE;
187 return FALSE;
188}
189
194inline bool operator>(const MACADR &i, const MACADR &j)
195{
196 if (i.phywadr[0] > j.phywadr[0])
197 return true;
198 else if (i.phywadr[0] < j.phywadr[0])
199 return false;
200
201 if (i.phywadr[1] > j.phywadr[1])
202 return true;
203 else if (i.phywadr[1] < j.phywadr[1])
204 return false;
205
206 if (i.phywadr[2] > j.phywadr[2]) return true;
207
208 return false;
209}
210
211extern MACADR ENET_BCAST;
212extern MACADR ENET_ZERO;
213
214// Forward declaration
215class CUR_IPADDR4;
216
225{
226 private:
227 beuint32_t ip_val;
228
229 public:
234 IPADDR4() = default;
235
242 IPADDR4(const IPADDR4 &v) = default;
243
244 IPADDR4 &operator=(const IPADDR4 &v)
245 {
246 ip_val = v.ip_val;
247 return *this;
248 };
249
250 volatile IPADDR4 &operator=(const IPADDR4 &v) volatile
251 {
252 ip_val = v.ip_val;
253 return *this;
254 };
255
256 IPADDR4 &operator=(const uint32_t v)
257 {
258 ip_val = v;
259 return *this;
260 };
261
262 volatile IPADDR4 &operator=(const uint32_t v) volatile
263 {
264 ip_val = v;
265 return *this;
266 };
267
268 bool IsEmbeddedIPV4() const { return true; };
269 IPADDR4 Extract4() const { return *this; };
270 operator uint32_t() const { return (uint32_t)ip_val; };
271
279 inline bool IsNull() const { return ip_val == 0; };
280
288 inline bool NotNull() const { return !IsNull(); };
289
295 inline void SetNull() { ip_val = 0; };
296
304 inline bool IsLoopBack() const { return ((ip_val & 0xFF000000) == 0x7F000000); };
305
313 inline bool IsMultiCast() const { return ((ip_val & 0xF0000000) == 0xE0000000); }; // 224. to 239. E0....EF.
314
322 inline bool IsmDns() { return (ip_val==0xE00000FB); };
323
324
332 inline bool IsGlobalBroadCast() const { return ip_val == 0xffffffff; };
333
341 inline bool IsAutoIP() { return ((ip_val & 0xFFFF0000) == 0xA9FE0000); };
342
343 // IPADDR4() {ip_val=0; };
344 IPADDR4(uint32_t v) { ip_val = v; };
345 IPADDR4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { ip_val = (a << 24) | (b << 16) | (c << 8) | d; }
346
347 // Return the MAC address for multicasts on at this address, NULL if not multicast
348 inline MACADR McastMac() const
349 {
350 uint32_t ipDst = ip_val;
351 ipDst &= 0x007FFFFF;
352 MACADR ma;
353 ma.phywadr[0] = 0x0100;
354 ma.phywadr[1] = 0x5E00 | (uint16_t)((ipDst >> 16) & 0x7F);
355 ma.phywadr[2] = (uint16_t)(ipDst & (0xFFFF));
356 return ma;
357 };
358
363 inline static IPADDR4 NullIP()
364 {
365 IPADDR4 i4;
366 i4.ip_val = 0;
367 return i4;
368 };
369
374 inline static IPADDR4 GlobalBroadCast()
375 {
376 IPADDR4 i4;
377 i4.ip_val = 0xFFFFFFFF;
378 return i4;
379 };
380
385 void print() const;
386
393 void fdprint(int fd) const;
394
404 int sprintf(char *cp, int maxl) const;
405
411 void SetFromAscii(const char *cp);
412
413 int GetPrintLen(bool compact);
414 int PrintHelper(PutCharsFunction *pf, void *data, bool compact);
415
416 bool IsBcastNetMask(IPADDR4 intfIP, IPADDR4 mask) const
417 {
418 return ((ip_val & mask.ip_val) == (intfIP.ip_val & mask.ip_val)) && ((ip_val & ~mask.ip_val) == (~mask.ip_val));
419 }
420
421 // Helpers for === and != tests....
422 friend bool operator==(const IPADDR4 i, const IPADDR4 j);
423 friend bool operator!=(const IPADDR4 i, const IPADDR4 j);
424 friend bool operator>(const IPADDR4 i, const IPADDR4 j);
425 friend bool operator<(const IPADDR4 i, const IPADDR4 j);
426
427 friend bool operator==(const uint32_t i, const IPADDR4 j);
428 friend bool operator!=(const uint32_t i, const IPADDR4 j);
429 friend bool operator>(const uint32_t i, const IPADDR4 j);
430 friend bool operator<(const uint32_t i, const IPADDR4 j);
431
432 friend bool operator==(const IPADDR4 i, const uint32_t j);
433 friend bool operator!=(const IPADDR4 i, const uint32_t j);
434 friend bool operator>(const IPADDR4 i, const uint32_t j);
435 friend bool operator<(const IPADDR4 i, const uint32_t j);
436
437 friend IPADDR4 IPV4FromConst(uint32_t d);
438 friend IPADDR4 IPV4FromConst(beuint32_t d);
439
440 friend IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask);
441
442 friend class CUR_IPADDR4;
443
444 // friend BE32<uint32_t>::BE32(IPADDR4 rhs);
445 // friend BE32<int32_t>::BE32(IPADDR4 rhs);
446
447} __attribute__((packed));
448
449// Helpers for === and != tests....
450inline bool operator==(const IPADDR4 i, const IPADDR4 j)
451{
452 return i.ip_val == j.ip_val;
453}
454inline bool operator!=(const IPADDR4 i, const IPADDR4 j)
455{
456 return i.ip_val != j.ip_val;
457}
458inline bool operator>(const IPADDR4 i, const IPADDR4 j)
459{
460 return i.ip_val > j.ip_val;
461}
462inline bool operator<(const IPADDR4 i, const IPADDR4 j)
463{
464 return i.ip_val < j.ip_val;
465}
466
467inline bool operator==(const uint32_t i, const IPADDR4 j)
468{
469 return i == j.ip_val;
470}
471inline bool operator!=(const uint32_t i, const IPADDR4 j)
472{
473 return i != j.ip_val;
474}
475inline bool operator>(const uint32_t i, const IPADDR4 j)
476{
477 return i > j.ip_val;
478}
479inline bool operator<(const uint32_t i, const IPADDR4 j)
480{
481 return i < j.ip_val;
482}
483
484inline bool operator==(const IPADDR4 i, const uint32_t j)
485{
486 return i.ip_val == j;
487}
488inline bool operator!=(const IPADDR4 i, const uint32_t j)
489{
490 return i.ip_val != j;
491}
492inline bool operator>(const IPADDR4 i, const uint32_t j)
493{
494 return i.ip_val > j;
495}
496inline bool operator<(const IPADDR4 i, const uint32_t j)
497{
498 return i.ip_val < j;
499}
500
501inline IPADDR4 IPV4FromConst(uint32_t d)
502{
503 IPADDR4 i4;
504 i4.ip_val = d;
505 return i4;
506};
507
508inline IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask)
509{
510 return IPV4FromConst(ifip.ip_val | ~(ipmask.ip_val));
511};
512
513/*
514
515
516 *******************************************************************************
517 *
518 * Data Structures
519 *
520 *******************************************************************************
521 */
522
523/*
524 * MAC address
525 * octet - address bytes
526 */
527
528/*
529 * Definition for an Ethernet frame
530 */
531typedef struct
532{
533 MACADR dest_addr;
534 MACADR src_addr;
535 beuint16_t eType;
536 uint8_t pData[];
537} EFRAME;
538
539typedef EFRAME *PEFRAME;
540
541typedef struct
542{
543 MACADR dest_addr;
544 MACADR src_addr;
545 beuint16_t eVlType;
546 beuint16_t eTag;
547 beuint16_t eType;
548 uint8_t pData[];
549} VLEFRAME;
550
551typedef VLEFRAME *PVLEFRAME;
552
553#ifdef IPV6
554#ifdef IPV4ONLY
555#error Must select IPV4ONLY or IPV6 but not both
556#endif
557#endif
558
559
560
561#ifdef IPV6
562#include <ipv6/ipv6_addr.h>
563
569
570#define GetNullIP() IPADDR::NullIP()
571
572#else
573
574#ifndef IPV4ONLY
575#error Got to pick an IP version
576#endif
577typedef IPADDR4 IPADDR;
578#define GetNullIP() IPADDR4::NullIP()
579#endif
580
581
582/* Any address */
583#define INADDR_ANY4 IPADDR4::NullIP()
584
585#ifdef IPV6
586#define INADDR_ANY IPADDR::NullIP()
587#else
588#define INADDR_ANY INADDR_ANY4
589#endif
590
591#endif // #ifndef NB_NET_TYPES_H
592
Definition config_netobj.h:147
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
void print() const
Print an IPv4 address to the stdout serial port.
static IPADDR4 GlobalBroadCast()
C++ static function for a global broadcast IP address.
Definition nettypes.h:374
bool IsNull() const
Check if the IP address is null.
Definition nettypes.h:279
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition nettypes.h:304
bool IsAutoIP()
Check if the IPADDR4 object contains an AutoIP address.
Definition nettypes.h:341
bool IsMultiCast() const
Check if the IPADDR4 object contains a Multicast IP address the interface.
Definition nettypes.h:313
int sprintf(char *cp, int maxl) const
sprintf an IPv4 address to the specified buffer
IPADDR4(const IPADDR4 &v)=default
Constructor to create an IPv4 object initialized to to the specified IPADDR4 IP address.
bool NotNull() const
Check if the IP address is not null.
Definition nettypes.h:288
void fdprint(int fd) const
Print an IPv4 address to the specified file descriptor.
static IPADDR4 NullIP()
C++ static function for a null IP address.
Definition nettypes.h:363
void SetFromAscii(const char *cp)
Set the IPv4 address from a character string.
void SetNull()
Set the IP address to null.
Definition nettypes.h:295
bool IsGlobalBroadCast() const
Check if the IPADDR4 object contains a global broadcast address: 255.255.255.255.
Definition nettypes.h:332
IPADDR4()=default
Constructor to create an IPv4 object initialized to null.
bool IsmDns()
Check if the IPADDR4 object contains a mDNS IP address the interface.
Definition nettypes.h:322
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
void print()
print the MAC to file descriptor 1
Definition nettypes.h:125
void SetFromBytes(const uint8_t *pb)
Set the MAC Address from a pointer to an array of 6 bytes.
Definition nettypes.h:108
bool IsNull()
Checks if MAC is null.
Definition nettypes.h:76
uint8_t GetByte(int n) const
Get the nth byte of the MAC.
Definition nettypes.h:91
bool IsBroadCast()
Checks if MAC is a Broadcast MAC.
Definition nettypes.h:86
void fdprint(int fd)
print the MAC to a file descriptor
bool IsMultiCast()
Checks if MAC is a Multicast MAC.
Definition nettypes.h:81
MACADR operator+(uint32_t rhs)
Definition nettypes.h:130
IPADDR6 IPADDR
IPADDR Object Type (either v4 or v6)
Definition nettypes.h:568
bool operator==(const MACADR &i, const MACADR &j)
Check MAC equality.
Definition nettypes.h:170
bool operator>(const MACADR &i, const MACADR &j)
Check MAC greater than.
Definition nettypes.h:194
class MACADR MACADR
Used to store and manipulate MAC addresses.
bool operator!=(const MACADR &i, const MACADR &j)
Check MAC inequality.
Definition nettypes.h:182
NetBurner IPADDR6 Class.