NetBurner 3.5.0
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} __attribute__((packed)) MACADR;
148
153inline bool operator==(const MACADR &i, const MACADR &j)
154{
155 if (i.phywadr[0] != j.phywadr[0]) return FALSE;
156 if (i.phywadr[1] != j.phywadr[1]) return FALSE;
157 if (i.phywadr[2] != j.phywadr[2]) return FALSE;
158 return TRUE;
159}
160
165inline bool operator!=(const MACADR &i, const MACADR &j)
166{
167 if (i.phywadr[0] != j.phywadr[0]) return TRUE;
168 if (i.phywadr[1] != j.phywadr[1]) return TRUE;
169 if (i.phywadr[2] != j.phywadr[2]) return TRUE;
170 return FALSE;
171}
172
177inline bool operator>(const MACADR &i, const MACADR &j)
178{
179 if (i.phywadr[0] > j.phywadr[0])
180 return true;
181 else if (i.phywadr[0] < j.phywadr[0])
182 return false;
183
184 if (i.phywadr[1] > j.phywadr[1])
185 return true;
186 else if (i.phywadr[1] < j.phywadr[1])
187 return false;
188
189 if (i.phywadr[2] > j.phywadr[2]) return true;
190
191 return false;
192}
193
194extern MACADR ENET_BCAST;
195extern MACADR ENET_ZERO;
196
197// Forward declaration
198class CUR_IPADDR4;
199
208{
209 private:
210 beuint32_t ip_val;
211
212 public:
217 IPADDR4() = default;
218
225 IPADDR4(const IPADDR4 &v) = default;
226
227 IPADDR4 &operator=(const IPADDR4 &v)
228 {
229 ip_val = v.ip_val;
230 return *this;
231 };
232
233 volatile IPADDR4 &operator=(const IPADDR4 &v) volatile
234 {
235 ip_val = v.ip_val;
236 return *this;
237 };
238
239 IPADDR4 &operator=(const uint32_t v)
240 {
241 ip_val = v;
242 return *this;
243 };
244
245 volatile IPADDR4 &operator=(const uint32_t v) volatile
246 {
247 ip_val = v;
248 return *this;
249 };
250
251 bool IsEmbeddedIPV4() const { return true; };
252 IPADDR4 Extract4() const { return *this; };
253 operator uint32_t() const { return (uint32_t)ip_val; };
254
262 inline bool IsNull() const { return ip_val == 0; };
263
271 inline bool NotNull() const { return !IsNull(); };
272
278 inline void SetNull() { ip_val = 0; };
279
287 inline bool IsLoopBack() const { return ((ip_val & 0xFF000000) == 0x7F000000); };
288
296 inline bool IsMultiCast() const { return ((ip_val & 0xF0000000) == 0xE0000000); }; // 224. to 239. E0....EF.
297
305 inline bool IsmDns() { return (ip_val==0xE00000FB); };
306
307
315 inline bool IsGlobalBroadCast() const { return ip_val == 0xffffffff; };
316
324 inline bool IsAutoIP() { return ((ip_val & 0xFFFF0000) == 0xA9FE0000); };
325
326 // IPADDR4() {ip_val=0; };
327 IPADDR4(uint32_t v) { ip_val = v; };
328 IPADDR4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { ip_val = (a << 24) | (b << 16) | (c << 8) | d; }
329
330 // Return the MAC address for multicasts on at this address, NULL if not multicast
331 inline MACADR McastMac() const
332 {
333 uint32_t ipDst = ip_val;
334 ipDst &= 0x007FFFFF;
335 MACADR ma;
336 ma.phywadr[0] = 0x0100;
337 ma.phywadr[1] = 0x5E00 | (uint16_t)((ipDst >> 16) & 0x7F);
338 ma.phywadr[2] = (uint16_t)(ipDst & (0xFFFF));
339 return ma;
340 };
341
346 inline static IPADDR4 NullIP()
347 {
348 IPADDR4 i4;
349 i4.ip_val = 0;
350 return i4;
351 };
352
357 inline static IPADDR4 GlobalBroadCast()
358 {
359 IPADDR4 i4;
360 i4.ip_val = 0xFFFFFFFF;
361 return i4;
362 };
363
368 void print() const;
369
376 void fdprint(int fd) const;
377
387 int sprintf(char *cp, int maxl) const;
388
394 void SetFromAscii(const char *cp);
395
396 int GetPrintLen(bool compact);
397 int PrintHelper(PutCharsFunction *pf, void *data, bool compact);
398
399 bool IsBcastNetMask(IPADDR4 intfIP, IPADDR4 mask) const
400 {
401 return ((ip_val & mask.ip_val) == (intfIP.ip_val & mask.ip_val)) && ((ip_val & ~mask.ip_val) == (~mask.ip_val));
402 }
403
404 // Helpers for === and != tests....
405 friend bool operator==(const IPADDR4 i, const IPADDR4 j);
406 friend bool operator!=(const IPADDR4 i, const IPADDR4 j);
407 friend bool operator>(const IPADDR4 i, const IPADDR4 j);
408 friend bool operator<(const IPADDR4 i, const IPADDR4 j);
409
410 friend bool operator==(const uint32_t i, const IPADDR4 j);
411 friend bool operator!=(const uint32_t i, const IPADDR4 j);
412 friend bool operator>(const uint32_t i, const IPADDR4 j);
413 friend bool operator<(const uint32_t i, const IPADDR4 j);
414
415 friend bool operator==(const IPADDR4 i, const uint32_t j);
416 friend bool operator!=(const IPADDR4 i, const uint32_t j);
417 friend bool operator>(const IPADDR4 i, const uint32_t j);
418 friend bool operator<(const IPADDR4 i, const uint32_t j);
419
420 friend IPADDR4 IPV4FromConst(uint32_t d);
421 friend IPADDR4 IPV4FromConst(beuint32_t d);
422
423 friend IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask);
424
425 friend class CUR_IPADDR4;
426
427 // friend BE32<uint32_t>::BE32(IPADDR4 rhs);
428 // friend BE32<int32_t>::BE32(IPADDR4 rhs);
429
430} __attribute__((packed));
431
432// Helpers for === and != tests....
433inline bool operator==(const IPADDR4 i, const IPADDR4 j)
434{
435 return i.ip_val == j.ip_val;
436}
437inline bool operator!=(const IPADDR4 i, const IPADDR4 j)
438{
439 return i.ip_val != j.ip_val;
440}
441inline bool operator>(const IPADDR4 i, const IPADDR4 j)
442{
443 return i.ip_val > j.ip_val;
444}
445inline bool operator<(const IPADDR4 i, const IPADDR4 j)
446{
447 return i.ip_val < j.ip_val;
448}
449
450inline bool operator==(const uint32_t i, const IPADDR4 j)
451{
452 return i == j.ip_val;
453}
454inline bool operator!=(const uint32_t i, const IPADDR4 j)
455{
456 return i != j.ip_val;
457}
458inline bool operator>(const uint32_t i, const IPADDR4 j)
459{
460 return i > j.ip_val;
461}
462inline bool operator<(const uint32_t i, const IPADDR4 j)
463{
464 return i < j.ip_val;
465}
466
467inline bool operator==(const IPADDR4 i, const uint32_t j)
468{
469 return i.ip_val == j;
470}
471inline bool operator!=(const IPADDR4 i, const uint32_t j)
472{
473 return i.ip_val != j;
474}
475inline bool operator>(const IPADDR4 i, const uint32_t j)
476{
477 return i.ip_val > j;
478}
479inline bool operator<(const IPADDR4 i, const uint32_t j)
480{
481 return i.ip_val < j;
482}
483
484inline IPADDR4 IPV4FromConst(uint32_t d)
485{
486 IPADDR4 i4;
487 i4.ip_val = d;
488 return i4;
489};
490
491inline IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask)
492{
493 return IPV4FromConst(ifip.ip_val | ~(ipmask.ip_val));
494};
495
496/*
497
498
499 *******************************************************************************
500 *
501 * Data Structures
502 *
503 *******************************************************************************
504 */
505
506/*
507 * MAC address
508 * octet - address bytes
509 */
510
511/*
512 * Definition for an Ethernet frame
513 */
514typedef struct
515{
516 MACADR dest_addr;
517 MACADR src_addr;
518 beuint16_t eType;
519 uint8_t pData[];
520} EFRAME;
521
522typedef EFRAME *PEFRAME;
523
524typedef struct
525{
526 MACADR dest_addr;
527 MACADR src_addr;
528 beuint16_t eVlType;
529 beuint16_t eTag;
530 beuint16_t eType;
531 uint8_t pData[];
532} VLEFRAME;
533
534typedef VLEFRAME *PVLEFRAME;
535
536#ifdef IPV6
537#include <ipv6/ipv6_addr.h>
538
544
545#define GetNullIP() IPADDR::NullIP()
546
547#else
548
549#ifndef IPV4ONLY
550#error Got to pick an IP version
551#endif
552typedef IPADDR4 IPADDR;
553#define GetNullIP() IPADDR4::NullIP()
554#endif
555
556
557/* Any address */
558#define INADDR_ANY4 IPADDR4::NullIP()
559
560#ifdef IPV6
561#define INADDR_ANY IPADDR::NullIP()
562#else
563#define INADDR_ANY INADDR_ANY4
564#endif
565
566#endif // #ifndef NB_NET_TYPES_H
567
Definition config_netobj.h:139
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:208
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:357
bool IsNull() const
Check if the IP address is null.
Definition nettypes.h:262
bool IsLoopBack() const
Check if the IP address is the loopback address for the interface.
Definition nettypes.h:287
bool IsAutoIP()
Check if the IPADDR4 object contains an AutoIP address.
Definition nettypes.h:324
bool IsMultiCast() const
Check if the IPADDR4 object contains a Multicast IP address the interface.
Definition nettypes.h:296
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:271
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:346
void SetFromAscii(const char *cp)
Set the IPv4 address from a character string.
void SetNull()
Set the IP address to null.
Definition nettypes.h:278
bool IsGlobalBroadCast() const
Check if the IPADDR4 object contains a global broadcast address: 255.255.255.255.
Definition nettypes.h:315
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:305
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:543
bool operator==(const MACADR &i, const MACADR &j)
Check MAC equality.
Definition nettypes.h:153
bool operator>(const MACADR &i, const MACADR &j)
Check MAC greater than.
Definition nettypes.h:177
class MACADR MACADR
Used to store and manipulate MAC addresses.
bool operator!=(const MACADR &i, const MACADR &j)
Check MAC inequality.
Definition nettypes.h:165
NetBurner IPADDR6 Class.