66// Licensed under the Apache License, Version 2.0 (the "License");
77// you may not use this file except in compliance with the License.
88// You may obtain a copy of the License at
9- //
9+ //
1010// http://www.apache.org/licenses/LICENSE-2.0
11- //
11+ //
1212// Unless required by applicable law or agreed to in writing, software
1313// distributed under the License is distributed on an "AS IS" BASIS,
1414// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1919#define NET_IP6_IP6_HPP
2020
2121#include < delegate>
22- #include " .. /ethernet.hpp"
22+ #include < net /ethernet/ethernet .hpp>
2323#include " ../packet.hpp"
2424#include " ../util.hpp"
2525
3636namespace net
3737{
3838 class PacketIP6 ;
39-
39+
4040 /* * IP6 layer skeleton */
4141 class IP6
4242 {
@@ -45,32 +45,32 @@ namespace net
4545 enum proto
4646 {
4747 PROTO_HOPOPT = 0 , // IPv6 hop-by-hop
48-
48+
4949 PROTO_ICMPv4 = 1 ,
5050 PROTO_TCP = 6 ,
5151 PROTO_UDP = 17 ,
52-
52+
5353 PROTO_ICMPv6 = 58 , // IPv6 ICMP
5454 PROTO_NoNext = 59 , // no next-header
5555 PROTO_OPTSv6 = 60 , // dest options
5656 };
57-
57+
5858 struct addr
5959 {
6060 // constructors
6161 addr ()
6262 : i32 {0 , 0 , 0 , 0 } {}
63- addr (uint16_t a1, uint16_t a2, uint16_t b1, uint16_t b2,
63+ addr (uint16_t a1, uint16_t a2, uint16_t b1, uint16_t b2,
6464 uint16_t c1, uint16_t c2, uint16_t d1, uint16_t d2)
6565 {
6666 i16 [0 ] = a1; i16 [1 ] = a2;
6767 i16 [2 ] = b1; i16 [3 ] = b2;
6868 i16 [4 ] = c1; i16 [5 ] = c2;
6969 i16 [6 ] = d1; i16 [7 ] = d2;
7070 /* i128 = _mm_set_epi16(
71- htons(d2), htons(d1),
72- htons(c2), htons(c1),
73- htons(b2), htons(b1),
71+ htons(d2), htons(d1),
72+ htons(c2), htons(c1),
73+ htons(b2), htons(b1),
7474 htons(a2), htons(a1));*/
7575 }
7676 addr (uint32_t a, uint32_t b, uint32_t c, uint32_t d)
@@ -91,7 +91,7 @@ namespace net
9191 // i128 = a.i128;
9292 return *this ;
9393 }
94-
94+
9595 // comparison functions
9696 bool operator == (const addr& a) const
9797 {
@@ -106,35 +106,35 @@ namespace net
106106 {
107107 return !this ->operator ==(a);
108108 }
109-
109+
110110 // returns this IPv6 address as a string
111111 std::string str () const ;
112-
112+
113113 // multicast IPv6 addresses
114114 static const addr node_all_nodes; // RFC 4921
115115 static const addr node_all_routers; // RFC 4921
116116 static const addr node_mDNSv6; // RFC 6762 (multicast DNSv6)
117-
117+
118118 // unspecified link-local address
119119 static const addr link_unspecified;
120-
120+
121121 // RFC 4291 2.4.6:
122122 // Link-Local addresses are designed to be used for addressing on a
123123 // single link for purposes such as automatic address configuration,
124124 // neighbor discovery, or when no routers are present.
125125 static const addr link_all_nodes; // RFC 4921
126126 static const addr link_all_routers; // RFC 4921
127127 static const addr link_mDNSv6; // RFC 6762
128-
128+
129129 static const addr link_dhcp_servers; // RFC 3315
130130 static const addr site_dhcp_servers; // RFC 3315
131-
131+
132132 // returns true if this addr is a IPv6 multicast address
133133 bool is_multicast () const
134134 {
135135 /* *
136136 RFC 4291 2.7 Multicast Addresses
137-
137+
138138 An IPv6 multicast address is an identifier for a group of interfaces
139139 (typically on different nodes). An interface may belong to any
140140 number of multicast groups. Multicast addresses have the following format:
@@ -145,7 +145,7 @@ namespace net
145145 **/
146146 return i8 [0 ] == 0xFF ;
147147 }
148-
148+
149149 union
150150 {
151151 // __m128i i128;
@@ -154,7 +154,7 @@ namespace net
154154 uint8_t i8 [16 ];
155155 };
156156 };
157-
157+
158158#pragma pack(push, 1)
159159 class header
160160 {
@@ -165,15 +165,15 @@ namespace net
165165 }
166166 uint8_t tclass () const
167167 {
168- return ((scanline[0 ] & 0xF000 ) >> 12 ) +
168+ return ((scanline[0 ] & 0xF000 ) >> 12 ) +
169169 (scanline[0 ] & 0xF );
170170 }
171171 // initializes the first scanline with the IPv6 version
172172 void init_scan0 ()
173173 {
174174 scanline[0 ] = 6u >> 4 ;
175175 }
176-
176+
177177 uint16_t size () const
178178 {
179179 return ((scanline[1 ] & 0x00FF ) << 8 ) +
@@ -184,7 +184,7 @@ namespace net
184184 scanline[1 ] &= 0xFFFF0000 ;
185185 scanline[1 ] |= htons (newsize);
186186 }
187-
187+
188188 uint8_t next () const
189189 {
190190 return (scanline[1 ] >> 16 ) & 0xFF ;
@@ -203,21 +203,21 @@ namespace net
203203 scanline[1 ] &= 0x00FFFFFF ;
204204 scanline[1 ] |= limit << 24 ;
205205 }
206-
206+
207207 private:
208208 uint32_t scanline[2 ];
209209 public:
210210 addr src;
211211 addr dst;
212212 };
213-
213+
214214 struct options_header
215215 {
216216 uint8_t next_header;
217217 uint8_t hdr_ext_len;
218218 uint16_t opt_1;
219219 uint32_t opt_2;
220-
220+
221221 uint8_t next () const
222222 {
223223 return next_header;
@@ -232,87 +232,87 @@ namespace net
232232 }
233233 };
234234#pragma pack(pop)
235-
235+
236236 struct full_header
237237 {
238238 Ethernet::header eth_hdr;
239239 IP6::header ip6_hdr;
240240 };
241-
241+
242242 // downstream delegate for transmit()
243243 typedef delegate<int (std::shared_ptr<PacketIP6>&)> downstream6;
244244 typedef downstream6 upstream6;
245-
245+
246246 /* * Constructor. Requires ethernet to latch on to. */
247247 IP6 (const addr& local);
248-
248+
249249 const IP6::addr& local_ip () const
250250 {
251251 return local;
252252 }
253-
253+
254254 uint8_t parse6 (uint8_t *& reader, uint8_t next);
255-
255+
256256 static std::string protocol_name (uint8_t protocol)
257257 {
258258 switch (protocol)
259259 {
260260 case PROTO_HOPOPT:
261261 return " IPv6 Hop-By-Hop (0)" ;
262-
262+
263263 case PROTO_TCP:
264264 return " TCPv6 (6)" ;
265265 case PROTO_UDP:
266266 return " UDPv6 (17)" ;
267-
267+
268268 case PROTO_ICMPv6:
269269 return " ICMPv6 (58)" ;
270270 case PROTO_NoNext:
271271 return " No next header (59)" ;
272272 case PROTO_OPTSv6:
273273 return " IPv6 destination options (60)" ;
274-
274+
275275 default :
276276 return " Unknown: " + std::to_string (protocol);
277277 }
278278 }
279-
279+
280280 // handler for upstream IPv6 packets
281281 void bottom (Packet_ptr pckt);
282-
282+
283283 // transmit packets to the ether
284284 void transmit (std::shared_ptr<PacketIP6>& pckt);
285-
285+
286286 // modify upstream handlers
287287 inline void set_handler (uint8_t proto, upstream& handler)
288288 {
289289 proto_handlers[proto] = handler;
290290 }
291-
291+
292292 inline void set_linklayer_out (downstream func)
293293 {
294294 _linklayer_out = func;
295295 }
296-
296+
297297 // creates a new IPv6 packet to be sent over the ether
298298 static std::shared_ptr<PacketIP6> create (uint8_t proto,
299299 Ethernet::addr ether_dest, const IP6::addr& dest);
300-
300+
301301 private:
302302 addr local;
303-
303+
304304 /* * Downstream: Linklayer output delegate */
305305 downstream _linklayer_out;
306-
306+
307307 /* * Upstream delegates */
308308 std::map<uint8_t , upstream> proto_handlers;
309309 };
310-
310+
311311 inline std::ostream& operator << (std::ostream& out, const IP6::addr& ip)
312312 {
313313 return out << ip.str ();
314314 }
315-
315+
316316} // namespace net
317317
318318#endif
0 commit comments