NetBurner 3.1
nettypes.h
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
5 #ifndef NB_NET_TYPES_H
6 #define NB_NET_TYPES_H
7 
8 // NB Definitions
9 #include <predef.h>
10 
11 // NB Libs
12 #include <basictypes.h>
13 
14 /*
15  *******************************************************************************
16  *
17  * Definitions
18  *
19  *******************************************************************************
20  */
21 
22 #define PPP_TYPE (0x0021) /* These are Big Endian constants! */
23 #define EIP_TYPE (0x0800) /* These are Big Endian constants! */
24 #define EIP6_TYPE (0x86DD) /* These are Big Endian constants! */
25 #define HARD_ENET (0x0001) /* These are Big Endian constants! */
26 #define EARP_TYPE (0x0806)
27 #define ARP_REQUEST (0x01)
28 #define ARP_RESPONSE (0x02)
29 
30 /*
31  * Media Access Control (MAC) address size in 8 bit bytes and 16 bit words
32  */
33 #define MACADDRESS_OCTETS_48 (6)
34 #define MACADDRESS_WORDS_48 (3)
35 typedef struct _MACADDRESS_48
36 {
37  uint8_t octet[MACADDRESS_OCTETS_48];
38 } __attribute__((packed)) MACADDRESS_48;
39 
40 struct MACADR; // Formward
41 typedef struct MACADR
42 {
43  beuint16_t phywadr[MACADDRESS_WORDS_48];
44  inline bool IsNull() { return ((phywadr[2] == 0) && (phywadr[1] == 0) && (phywadr[0] == 0)); };
45  inline bool IsMultiCast() { return (phywadr[0] & 0x0100); };
46  inline bool IsBroadCast() { return ((phywadr[0] == 0xFFFF) && (phywadr[1] == 0xFFFF) && (phywadr[2] == 0xFFFF)); };
47  uint8_t GetByte(int n) const
48  {
49  //#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
50  // if (n&1) { return ((phywadr[n/2]>>8)&0xFF); }
51  // else { return (phywadr[n/2]&0xFF); }
52  //#else
53  if (n & 1) { return (phywadr[n / 2] & 0xFF); }
54  else
55  {
56  return ((phywadr[n / 2] >> 8) & 0xFF);
57  }
58  //#endif
59  };
60  inline void SetFromBytes(const uint8_t *pb)
61  {
62  uint8_t *pto = (uint8_t *)phywadr;
63  for (int i = 0; i < 6; i++)
64  {
65  pto[i] = pb[i];
66  }
67  };
68  void fdprint(int fd);
69  inline void print() { fdprint(1); };
70 } __attribute__((packed)) MACADR;
71 
72 // Helpers for === and != tests....
73 inline bool operator==(const MACADR &i, const MACADR &j)
74 {
75  if (i.phywadr[0] != j.phywadr[0]) return FALSE;
76  if (i.phywadr[1] != j.phywadr[1]) return FALSE;
77  if (i.phywadr[2] != j.phywadr[2]) return FALSE;
78  return TRUE;
79 }
80 
81 inline bool operator!=(const MACADR &i, const MACADR &j)
82 {
83  if (i.phywadr[0] != j.phywadr[0]) return TRUE;
84  if (i.phywadr[1] != j.phywadr[1]) return TRUE;
85  if (i.phywadr[2] != j.phywadr[2]) return TRUE;
86  return FALSE;
87 }
88 
89 inline bool operator>(const MACADR &i, const MACADR &j)
90 {
91  if (i.phywadr[0] > j.phywadr[0])
92  return true;
93  else if (i.phywadr[0] < j.phywadr[0])
94  return false;
95 
96  if (i.phywadr[1] > j.phywadr[1])
97  return true;
98  else if (i.phywadr[1] < j.phywadr[1])
99  return false;
100 
101  if (i.phywadr[2] > j.phywadr[2]) return true;
102 
103  return false;
104 }
105 
106 extern MACADR ENET_BCAST;
107 extern MACADR ENET_ZERO;
108 
109 // Forward declaration
110 class CUR_IPADDR4;
111 
112 struct IPADDR4
113 {
114 private :
115  beuint32_t ip_val;
116 
117  public:
118  IPADDR4() = default;
119  IPADDR4(const IPADDR4 &v) = default;
120 
121  IPADDR4 &operator=(const IPADDR4 &v)
122  {
123  ip_val = v.ip_val;
124  return *this;
125  };
126 
127 
128  IPADDR4 &operator=(const uint32_t v)
129  {
130  ip_val = v;
131  return *this;
132  };
133 
134  bool IsEmbeddedIPV4() { return true; };
135  IPADDR4 Extract4() const { return *this; };
136  operator uint32_t() const { return (uint32_t)ip_val; };
137 
138  inline bool IsNull() const { return ip_val == 0; };
139  inline bool NotNull() const { return !IsNull(); };
140  inline void SetNull() { ip_val = 0; };
141  inline bool IsLoopBack() const { return ((ip_val & 0xFF000000) == 0x7F000000); };
142  inline bool IsMultiCast() const { return ((ip_val & 0xF0000000) == 0xE0000000); }; // 224. to 239. E0....EF.
143  inline bool IsGlobalBroadCast() const { return ip_val == 0xffffffff; };
144  inline bool IsAutoIP() { return ((ip_val & 0xFFFF0000) == 0xA9FE0000); };
145 
146  // IPADDR4() {ip_val=0; };
147  IPADDR4(uint32_t v) { ip_val = v; };
148  IPADDR4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { ip_val = (a << 24) | (b << 16) | (c << 8) | d; }
149 
150  // Return the MAC address for multicasts on at this address, NULL if not multicast
151  inline MACADR McastMac() const
152  {
153  uint32_t ipDst = ip_val;
154  ipDst &= 0x007FFFFF;
155  MACADR ma;
156  ma.phywadr[0] = 0x0100;
157  ma.phywadr[1] = 0x5E00 | (uint16_t)((ipDst >> 16) & 0x7F);
158  ma.phywadr[2] = (uint16_t)(ipDst & (0xFFFF));
159  return ma;
160  };
161 
162  inline static IPADDR4 NullIP()
163  {
164  IPADDR4 i4;
165  i4.ip_val = 0;
166  return i4;
167  };
168  inline static IPADDR4 GlobalBroadCast()
169  {
170  IPADDR4 i4;
171  i4.ip_val = 0xFFFFFFFF;
172  return i4;
173  };
174 
175  void print() const;
176  void fdprint(int fd) const;
177  int sprintf(char *cp, int maxl) const;
178  void SetFromAscii(const char *cp);
179 
180  bool IsBcastNetMask(IPADDR4 intfIP, IPADDR4 mask) const
181  {
182  return ((ip_val & mask.ip_val) == (intfIP.ip_val & mask.ip_val)) && ((ip_val & ~mask.ip_val) == (~mask.ip_val));
183  }
184 
185  // Helpers for === and != tests....
186  friend bool operator==(const IPADDR4 i, const IPADDR4 j);
187  friend bool operator!=(const IPADDR4 i, const IPADDR4 j);
188  friend bool operator>(const IPADDR4 i, const IPADDR4 j);
189  friend bool operator<(const IPADDR4 i, const IPADDR4 j);
190 
191  friend bool operator==(const uint32_t i, const IPADDR4 j);
192  friend bool operator!=(const uint32_t i, const IPADDR4 j);
193  friend bool operator>(const uint32_t i, const IPADDR4 j);
194  friend bool operator<(const uint32_t i, const IPADDR4 j);
195 
196  friend bool operator==(const IPADDR4 i, const uint32_t j);
197  friend bool operator!=(const IPADDR4 i, const uint32_t j);
198  friend bool operator>(const IPADDR4 i, const uint32_t j);
199  friend bool operator<(const IPADDR4 i, const uint32_t j);
200 
201  friend IPADDR4 IPV4FromConst(uint32_t d);
202  friend IPADDR4 IPV4FromConst(beuint32_t d);
203 
204  friend IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask);
205 
206  friend class CUR_IPADDR4;
207 
208  // friend BE32<uint32_t>::BE32(IPADDR4 rhs);
209  // friend BE32<int32_t>::BE32(IPADDR4 rhs);
210 
211 } __attribute__((packed));
212 
213 // Helpers for === and != tests....
214 inline bool operator==(const IPADDR4 i, const IPADDR4 j)
215 {
216  return i.ip_val == j.ip_val;
217 }
218 inline bool operator!=(const IPADDR4 i, const IPADDR4 j)
219 {
220  return i.ip_val != j.ip_val;
221 }
222 inline bool operator>(const IPADDR4 i, const IPADDR4 j)
223 {
224  return i.ip_val > j.ip_val;
225 }
226 inline bool operator<(const IPADDR4 i, const IPADDR4 j)
227 {
228  return i.ip_val < j.ip_val;
229 }
230 
231 inline bool operator==(const uint32_t i, const IPADDR4 j)
232 {
233  return i == j.ip_val;
234 }
235 inline bool operator!=(const uint32_t i, const IPADDR4 j)
236 {
237  return i != j.ip_val;
238 }
239 inline bool operator>(const uint32_t i, const IPADDR4 j)
240 {
241  return i > j.ip_val;
242 }
243 inline bool operator<(const uint32_t i, const IPADDR4 j)
244 {
245  return i < j.ip_val;
246 }
247 
248 inline bool operator==(const IPADDR4 i, const uint32_t j)
249 {
250  return i.ip_val == j;
251 }
252 inline bool operator!=(const IPADDR4 i, const uint32_t j)
253 {
254  return i.ip_val != j;
255 }
256 inline bool operator>(const IPADDR4 i, const uint32_t j)
257 {
258  return i.ip_val > j;
259 }
260 inline bool operator<(const IPADDR4 i, const uint32_t j)
261 {
262  return i.ip_val < j;
263 }
264 
265 inline IPADDR4 IPV4FromConst(uint32_t d)
266 {
267  IPADDR4 i4;
268  i4.ip_val = d;
269  return i4;
270 };
271 
272 inline IPADDR4 LocalBroadCast(IPADDR4 ifip, IPADDR4 ipmask)
273 {
274  return IPV4FromConst(ifip.ip_val | ~(ipmask.ip_val));
275 };
276 
277 /*
278 
279 
280  *******************************************************************************
281  *
282  * Data Structures
283  *
284  *******************************************************************************
285  */
286 
287 /*
288  * MAC address
289  * octet - address bytes
290  */
291 
292 /*
293  * Definition for an Ethernet frame
294  */
295 typedef struct
296 {
297  MACADR dest_addr;
298  MACADR src_addr;
299  beuint16_t eType;
300  uint8_t pData[];
301 } EFRAME;
302 
303 typedef EFRAME *PEFRAME;
304 
305 
306 typedef struct
307 {
308  MACADR dest_addr;
309  MACADR src_addr;
310  beuint16_t eVlType;
311  beuint16_t eTag;
312  beuint16_t eType;
313  uint8_t pData[];
314 } VLEFRAME;
315 
316 
317 
318 typedef VLEFRAME *PVLEFRAME;
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 #ifdef IPV6
329 #include <ipv6/ipv6_addr.h>
330 typedef IPADDR6 IPADDR;
331 #define GetNullIP() IPADDR6::NullIP()
332 #else
333 #ifndef IPV4ONLY
334 #error Got to pick an IP version
335 #endif
336 typedef IPADDR4 IPADDR;
337 #define GetNullIP() IPADDR4::NullIP()
338 #endif
339 
340 #endif // #ifndef NB_NET_TYPES_H
NetBurner IPADDR6 Class.
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition: ipv6_addr.h:28