|
21 | 21 |
|
22 | 22 | #include <string> |
23 | 23 |
|
| 24 | +#include "header.hpp" |
| 25 | +#include <hw/nic.hpp> |
| 26 | +#include <hw/mac_addr.hpp> |
24 | 27 | #include <net/inet_common.hpp> |
25 | 28 |
|
26 | | -namespace hw { |
27 | | - class Nic; |
28 | | -} |
29 | | - |
30 | 29 | namespace net { |
31 | 30 |
|
32 | 31 | /** Ethernet packet handling. */ |
33 | 32 | class Ethernet { |
34 | 33 | public: |
35 | | - static constexpr size_t ETHER_ADDR_LEN = 6; |
36 | 34 | static constexpr size_t MINIMUM_PAYLOAD = 46; |
37 | 35 |
|
38 | 36 | /** |
@@ -61,64 +59,19 @@ namespace net { |
61 | 59 | }; |
62 | 60 |
|
63 | 61 | // MAC address |
64 | | - union addr { |
65 | | - uint8_t part[ETHER_ADDR_LEN]; |
66 | | - |
67 | | - struct { |
68 | | - uint16_t minor; |
69 | | - uint32_t major; |
70 | | - } __attribute__((packed)); |
71 | | - |
72 | | - addr() noexcept : part{} {} |
73 | | - |
74 | | - addr(const uint8_t a, const uint8_t b, const uint8_t c, |
75 | | - const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
76 | | - : part{a,b,c,d,e,f} |
77 | | - {} |
78 | | - |
79 | | - addr& operator=(const addr cpy) noexcept { |
80 | | - minor = cpy.minor; |
81 | | - major = cpy.major; |
82 | | - return *this; |
83 | | - } |
84 | | - |
85 | | - // hex string representation |
86 | | - std::string str() const { |
87 | | - char eth_addr[18]; |
88 | | - snprintf(eth_addr, sizeof(eth_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
89 | | - part[0], part[1], part[2], |
90 | | - part[3], part[4], part[5]); |
91 | | - return eth_addr; |
92 | | - } |
93 | | - |
94 | | - /** Check for equality */ |
95 | | - bool operator==(const addr mac) const noexcept |
96 | | - { |
97 | | - return strncmp( |
98 | | - reinterpret_cast<const char*>(part), |
99 | | - reinterpret_cast<const char*>(mac.part), |
100 | | - ETHER_ADDR_LEN) == 0; |
101 | | - } |
102 | | - |
103 | | - static const addr MULTICAST_FRAME; |
104 | | - static const addr BROADCAST_FRAME; |
105 | | - |
106 | | - static const addr IPv6mcast_01; |
107 | | - static const addr IPv6mcast_02; |
108 | | - |
109 | | - } __attribute__((packed)); //< union addr |
| 62 | + using addr = hw::MAC_addr; |
110 | 63 |
|
111 | | - /** Constructor */ |
112 | | - explicit Ethernet(hw::Nic& nic) noexcept; |
| 64 | + static const addr MULTICAST_FRAME; |
| 65 | + static const addr BROADCAST_FRAME; |
113 | 66 |
|
114 | | - struct header { |
115 | | - addr dest; |
116 | | - addr src; |
117 | | - unsigned short type; |
| 67 | + static const addr IPv6mcast_01; |
| 68 | + static const addr IPv6mcast_02; |
118 | 69 |
|
119 | | - } __attribute__((packed)) ; |
| 70 | + /** Constructor */ |
| 71 | + explicit Ethernet(hw::Nic& nic) noexcept; |
120 | 72 |
|
121 | | - using trailer = uint32_t; |
| 73 | + using header = ethernet::Header; |
| 74 | + using trailer = ethernet::trailer_t; |
122 | 75 |
|
123 | 76 | /** Bottom upstream input, "Bottom up". Handle raw ethernet buffer. */ |
124 | 77 | void bottom(Packet_ptr); |
@@ -147,7 +100,8 @@ namespace net { |
147 | 100 | { physical_out_ = del; } |
148 | 101 |
|
149 | 102 | /** @return Mac address of the underlying device */ |
150 | | - const addr mac() const noexcept; |
| 103 | + const auto& mac() const noexcept |
| 104 | + { return nic_.mac(); } |
151 | 105 |
|
152 | 106 | /** Transmit data, with preallocated space for eth.header */ |
153 | 107 | void transmit(Packet_ptr); |
|
0 commit comments