Skip to content

Commit b1147e0

Browse files
net: Removed IP4 dependency on Packet (only addr now)
1 parent baf917c commit b1147e0

5 files changed

Lines changed: 106 additions & 104 deletions

File tree

api/net/dhcp/dh4client.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <timers>
2323
#include "../packet.hpp"
24+
#include <net/ip4/ip4.hpp>
2425

2526
namespace net
2627
{

api/net/ip4/packet_ip4.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <net/util.hpp>
2424
#include <net/packet.hpp>
25+
#include "ip4.hpp"
2526

2627
namespace net {
2728

@@ -95,10 +96,10 @@ namespace net {
9596
{ ip_header().tot_len = htons(size() - sizeof(LinkLayer::header)); }
9697

9798
private:
98-
const IP4::ip_header& ip_header() const noexcept
99+
const ip4::Header& ip_header() const noexcept
99100
{ return (reinterpret_cast<IP4::full_header*>(buffer()))->ip_hdr; }
100101

101-
IP4::ip_header& ip_header() noexcept
102+
ip4::Header& ip_header() noexcept
102103
{ return (reinterpret_cast<IP4::full_header*>(buffer()))->ip_hdr; }
103104

104105
void set_ip4_checksum() noexcept {

api/net/ip6/ip6.hpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
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.
@@ -19,7 +19,7 @@
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

@@ -36,7 +36,7 @@
3636
namespace 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

api/net/packet.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define NET_PACKET_HPP
2020

2121
#include "buffer_store.hpp"
22-
#include "ip4/ip4.hpp"
22+
#include "ip4/addr.hpp"
2323
#include <cassert>
2424

2525
namespace net {
@@ -39,8 +39,8 @@ namespace net {
3939
* @WARNING: There are two adjacent parameters of the same type, violating CG I.24.
4040
*/
4141
Packet(
42-
uint16_t cap,
43-
uint16_t len,
42+
uint16_t cap,
43+
uint16_t len,
4444
deleter_t del = default_packet_deleter) noexcept
4545
: capacity_ (cap),
4646
size_ (len),
@@ -67,10 +67,10 @@ namespace net {
6767
}
6868

6969
/** next-hop ipv4 address for IP routing */
70-
void next_hop(IP4::addr ip) noexcept {
70+
void next_hop(ip4::Addr ip) noexcept {
7171
next_hop4_ = ip;
7272
}
73-
IP4::addr next_hop() const noexcept {
73+
auto next_hop() const noexcept {
7474
return next_hop4_;
7575
}
7676

@@ -158,7 +158,7 @@ namespace net {
158158

159159
uint16_t capacity_;
160160
uint16_t size_;
161-
IP4::addr next_hop4_;
161+
ip4::Addr next_hop4_;
162162
deleter_t deleter_;
163163
BufferStore::buffer_t payload_ {nullptr};
164164
BufferStore::buffer_t buf_[0];

0 commit comments

Comments
 (0)