Debian Codegraph @

protolib/include/protoPktETH.h

raw
1 #ifndef _PROTO_PKT_ETH
2 #define _PROTO_PKT_ETH
3
4 #include "protoPkt.h"
5 #include "protoAddress.h"
6
7 /**
8 * @class ProtoPktETH
9 *
10 * @brief This is a class that provides access to and control of ETHernet
11 * header fields for the associated buffer space. The ProtoPkt "buffer"
12 * is presumed to contain a valid ETHernet frame.
13 *
14 * NOTE: Since ETHernet headers are 14 bytes and often encapsulate
15 * IP packets (for most Protolib purposes), this creates a
16 * problem with respect to 32-bit alignment of the buffer
17 * space if a ProtoPktIP is subsequently instantiated from the same
18 * buffer space. My recommendation here is to actually cheat and
19 * offset into the buffer space by 2 bytes for the ProtoPktETH
20 * instance so that the ProtoPktIP ends up being properly aligned.
21 * (You can get away with this since accesses to the buffer
22 * for ProtoPktETH purposes are 16-bit aligned worst case
23 * anyway. On some systems this will produce a compiler warning
24 * when you the following, but it should work OK:
25 *
26 * UINT32 buffer[MAX_LEN/4];
27 * ProtoPktETHer etherFrame((UINT32*)(((UINT16*)buffer)+1), MAX_LEN-2);
28 *
29 * UINT16 numBytes = ReadETHernetFrame(((char*)buffer)+2, MAX_LEN-2);
30 * etherFrame.SetLength(numBytes); // "numBytes" include ETHernet header
31 *
32 * ASSERT(ProtoPktETHer::IP == etherFrame.GetPayloadType());
33 * ProtoPktIp ipPkt(buffer+4, MAX_LEN-16); // Notice IP content is 32-bit aligned
34 * ipPkt.SetLength(etherFrame.GetPayloadLength());
35 *
36 * (The resultant "ipPkt" instance can be safely manipulated with 32-bit alignment)
37 */
38
39 // Ethernet Packet Format:
40 //
41 // 0 1 2 3
42 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
43 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 // | destination MAC address |
45 // + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 // | | |
47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
48 // | source MAC address |
49 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 // | *Ethertype = 0x8100 | *Vlan-id |
51 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
52 // | Ethertype | |
53 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
54 // | data ... |
55 // + +
56 // | |
57 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 // | CRC checksum |
59 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60
61
62 class ProtoPktETH : public ProtoPkt
63 {
64 public:
65 ProtoPktETH(void* bufferPtr = NULL,
66 unsigned int numBytes = 0,
67 bool freeOnDestruct = false);
68 ~ProtoPktETH();
69
70 enum Type
71 {
72 IP = 0x0800,
73 ARP = 0x0806,
74 VLAN = 0x8100, // 802.1Q vlan tagged frame
75 IPv6 = 0x86dd
76 };
77
78 enum
79 {
80 ADDR_LEN = 6
81 };
82
83 bool InitFromBuffer(unsigned int frameLength,
84 void* bufferPtr = NULL,
85 unsigned int numBytes = 0,
86 bool freeOnDestruct = false)
87 {return ProtoPkt::InitFromBuffer(frameLength, bufferPtr, numBytes, freeOnDestruct);}
88
89 void GetSrcAddr(ProtoAddress& addr)
90 {addr.SetRawHostAddress(ProtoAddress::ETH, (char*)AccessBuffer(OFFSET_SRC), ADDR_LEN);}
91 void GetDstAddr(ProtoAddress& addr) const
92 {addr.SetRawHostAddress(ProtoAddress::ETH, (char*)GetBuffer(OFFSET_DST), ADDR_LEN);}
93 Type GetType() const
94 {
95 Type type = GetType1();
96 return (VLAN == type) ? GetType2() : type;
97 }
98 unsigned int GetHeaderLength() const
99 {
100 unsigned int len = 2*ADDR_LEN + 2;
101 return (VLAN == GetType1()) ? (len + 4) : len;
102 }
103 static unsigned int GetHeaderLength(void* bufferPtr, unsigned int bufferLen)
104 {
105 unsigned int len = 2*ADDR_LEN + 2;
106 const char* ptr =(const char*)bufferPtr;
107 return (bufferLen > OFFSET_TYPE1) ?
108 ((VLAN == (Type)ptr[OFFSET_TYPE1]) ? (len + 4) : len) : 0;
109 }
110
111 unsigned int GetPayloadLength() const
112 {return (GetLength() - GetHeaderLength());}
113 const void* GetPayload() const
114 {return GetBuffer(OffsetPayload());}
115 void* AccessPayload()
116 {return AccessBuffer(OffsetPayload());}
117 unsigned int GetPayloadMax() const
118 {return (GetBufferLength() - OffsetPayload());}
119
120 bool InitIntoBuffer(void* bufferPtr = NULL,
121 unsigned int bufferBytes = 0,
122 bool freeOnDestruct = false);
123 void SetSrcAddr(ProtoAddress srcAddr)
124 {memcpy(AccessBuffer(OFFSET_SRC), srcAddr.GetRawHostAddress(), ADDR_LEN);}
125
126 void SetDstAddr(ProtoAddress dstAddr)
127 {memcpy(AccessBuffer(OFFSET_DST), dstAddr.GetRawHostAddress(), ADDR_LEN);}
128
129 void SetType(Type type)
130 {SetWord16(OFFSET_TYPE1, (UINT16)type);}
131 // TBD - add SetVlanTag() method for 802.1Q
132 void SetVlanType(Type type)
133 {SetWord16(OFFSET_TYPE2, (UINT16)type);}
134 void SetPayload(const char* payload, unsigned int numBytes)
135 {
136 memcpy(AccessBuffer(OffsetPayload()), payload, numBytes);
137 SetPayloadLength(numBytes);
138 }
139 void SetPayloadLength(unsigned int numBytes)
140 {SetLength(numBytes + GetHeaderLength());}
141
142 private:
143 Type GetType1() const
144 {return (Type)GetWord16(OFFSET_TYPE1);}
145 Type GetType2() const
146 {return (Type)GetWord16(OFFSET_TYPE2);}
147
148 enum
149 {
150 OFFSET_DST = 0, // 6 bytes, zero offset
151 OFFSET_SRC = OFFSET_DST + ADDR_LEN, // 6 bytes, UINT8 offset
152 OFFSET_TYPE1 = (OFFSET_SRC + ADDR_LEN)/2, // 2 bytes, UINT16 offset
153 OFFSET_TAG = OFFSET_TYPE1 + 1, // 2 bytes, UINT16 offset
154 OFFSET_TYPE2 = OFFSET_TAG + 1 // 2 bytes, UINT16 offset
155 };
156
157 unsigned OffsetPayload() const // UINT8 offset
158 {
159 unsigned int offset = OFFSET_TAG*2; // UINT8 offset
160 return (VLAN == GetType1()) ? (offset + 4) : offset;
161 }
162
163 }; // end class ProtoPktETH
164
165
166 #endif // _PROTO_PKT_ETH