inet.hh (7777:369f90d32e2e) inet.hh (7778:6a7207241112)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Steve Reinhardt
31 * Gabe Black
30 */
31
32#ifndef __BASE_INET_HH__
33#define __BASE_INET_HH__
34
35#include <iosfwd>
36#include <string>
37#include <utility>
38#include <vector>
39
40#include "base/range.hh"
41#include "base/types.hh"
42#include "dev/etherpkt.hh"
43
44#include "dnet/os.h"
45#include "dnet/eth.h"
46#include "dnet/ip.h"
47#include "dnet/ip6.h"
48#include "dnet/addr.h"
49#include "dnet/arp.h"
50#include "dnet/icmp.h"
51#include "dnet/tcp.h"
52#include "dnet/udp.h"
53#include "dnet/intf.h"
54#include "dnet/route.h"
55#include "dnet/fw.h"
56#include "dnet/blob.h"
57#include "dnet/rand.h"
58
59namespace Net {
60
61/*
62 * Ethernet Stuff
63 */
64struct EthAddr : protected eth_addr
65{
66 protected:
67 void parse(const std::string &addr);
68
69 public:
70 EthAddr();
71 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
72 EthAddr(const eth_addr &ea);
73 EthAddr(const std::string &addr);
74 const EthAddr &operator=(const eth_addr &ea);
75 const EthAddr &operator=(const std::string &addr);
76
77 int size() const { return sizeof(eth_addr); }
78
79 const uint8_t *bytes() const { return &data[0]; }
80 uint8_t *bytes() { return &data[0]; }
81
82 const uint8_t *addr() const { return &data[0]; }
83 bool unicast() const { return data[0] == 0x00; }
84 bool multicast() const { return data[0] == 0x01; }
85 bool broadcast() const { return data[0] == 0xff; }
86 std::string string() const;
87
88 operator uint64_t() const
89 {
90 uint64_t reg = 0;
91 reg |= ((uint64_t)data[0]) << 40;
92 reg |= ((uint64_t)data[1]) << 32;
93 reg |= ((uint64_t)data[2]) << 24;
94 reg |= ((uint64_t)data[3]) << 16;
95 reg |= ((uint64_t)data[4]) << 8;
96 reg |= ((uint64_t)data[5]) << 0;
97 return reg;
98 }
99
100};
101
102std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
103bool operator==(const EthAddr &left, const EthAddr &right);
104
105struct EthHdr : public eth_hdr
106{
107 uint16_t type() const { return ntohs(eth_type); }
108 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
109 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
110
111 int size() const { return sizeof(eth_hdr); }
112
113 const uint8_t *bytes() const { return (const uint8_t *)this; }
114 const uint8_t *payload() const { return bytes() + size(); }
115 uint8_t *bytes() { return (uint8_t *)this; }
116 uint8_t *payload() { return bytes() + size(); }
117};
118
119class EthPtr
120{
121 protected:
122 friend class IpPtr;
123 EthPacketPtr p;
124
125 public:
126 EthPtr() {}
127 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
128
129 EthHdr *operator->() { return (EthHdr *)p->data; }
130 EthHdr &operator*() { return *(EthHdr *)p->data; }
131 operator EthHdr *() { return (EthHdr *)p->data; }
132
133 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
134 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
135 operator const EthHdr *() const { return (const EthHdr *)p->data; }
136
137 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
138
139 const EthPacketPtr packet() const { return p; }
140 EthPacketPtr packet() { return p; }
141 bool operator!() const { return !p; }
142 operator bool() const { return p; }
143 int off() const { return 0; }
144 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
145};
146
147/*
148 * IP Stuff
149 */
150struct IpAddress
151{
152 protected:
153 uint32_t _ip;
154
155 public:
156 IpAddress() : _ip(0)
157 {}
158 IpAddress(const uint32_t __ip) : _ip(__ip)
159 {}
160
161 uint32_t ip() const { return _ip; }
162
163 std::string string() const;
164};
165
166std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
167bool operator==(const IpAddress &left, const IpAddress &right);
168
169struct IpNetmask : public IpAddress
170{
171 protected:
172 uint8_t _netmask;
173
174 public:
175 IpNetmask() : IpAddress(), _netmask(0)
176 {}
177 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
178 IpAddress(__ip), _netmask(__netmask)
179 {}
180
181 uint8_t netmask() const { return _netmask; }
182
183 std::string string() const;
184};
185
186std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
187bool operator==(const IpNetmask &left, const IpNetmask &right);
188
189struct IpWithPort : public IpAddress
190{
191 protected:
192 uint16_t _port;
193
194 public:
195 IpWithPort() : IpAddress(), _port(0)
196 {}
197 IpWithPort(const uint32_t __ip, const uint16_t __port) :
198 IpAddress(__ip), _port(__port)
199 {}
200
201 uint8_t port() const { return _port; }
202
203 std::string string() const;
204};
205
206std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
207bool operator==(const IpWithPort &left, const IpWithPort &right);
208
209struct IpOpt;
210struct IpHdr : public ip_hdr
211{
212 uint8_t version() const { return ip_v; }
213 uint8_t hlen() const { return ip_hl * 4; }
214 uint8_t tos() const { return ip_tos; }
215 uint16_t len() const { return ntohs(ip_len); }
216 uint16_t id() const { return ntohs(ip_id); }
217 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
218 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
219 uint8_t ttl() const { return ip_ttl; }
220 uint8_t proto() const { return ip_p; }
221 uint16_t sum() const { return ip_sum; }
222 uint32_t src() const { return ntohl(ip_src); }
223 uint32_t dst() const { return ntohl(ip_dst); }
224
225 void sum(uint16_t sum) { ip_sum = sum; }
226 void id(uint16_t _id) { ip_id = htons(_id); }
227 void len(uint16_t _len) { ip_len = htons(_len); }
228
229
230 bool options(std::vector<const IpOpt *> &vec) const;
231
232 int size() const { return hlen(); }
233 const uint8_t *bytes() const { return (const uint8_t *)this; }
234 const uint8_t *payload() const { return bytes() + size(); }
235 uint8_t *bytes() { return (uint8_t *)this; }
236 uint8_t *payload() { return bytes() + size(); }
237};
238
239class IpPtr
240{
241 protected:
242 friend class TcpPtr;
243 friend class UdpPtr;
244 EthPacketPtr p;
245
246 void set(const EthPacketPtr &ptr)
247 {
248 p = 0;
249
250 if (ptr) {
251 EthHdr *eth = (EthHdr *)ptr->data;
252 if (eth->type() == ETH_TYPE_IP)
253 p = ptr;
254 }
255 }
256
257 public:
258 IpPtr() : p(0) {}
259 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); }
260 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); }
261 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
262
263 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
264 IpHdr *operator->() { return get(); }
265 IpHdr &operator*() { return *get(); }
266
267 const IpHdr *get() const
268 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
269 const IpHdr *operator->() const { return get(); }
270 const IpHdr &operator*() const { return *get(); }
271
272 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
273 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
274 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
275
276 const EthPacketPtr packet() const { return p; }
277 EthPacketPtr packet() { return p; }
278 bool operator!() const { return !p; }
279 operator bool() const { return p; }
280 int off() const { return sizeof(eth_hdr); }
281 int pstart() const { return off() + get()->size(); }
282};
283
284uint16_t cksum(const IpPtr &ptr);
285
286struct IpOpt : public ip_opt
287{
288 uint8_t type() const { return opt_type; }
289 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
290 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
291 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
292 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
293
294 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
295 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
296 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
297
298 const uint8_t *data() const { return opt_data.data8; }
299 void sec(ip_opt_data_sec &sec) const;
300 void lsrr(ip_opt_data_rr &rr) const;
301 void ssrr(ip_opt_data_rr &rr) const;
302 void ts(ip_opt_data_ts &ts) const;
303 uint16_t satid() const { return ntohs(opt_data.satid); }
304 uint16_t mtup() const { return ntohs(opt_data.mtu); }
305 uint16_t mtur() const { return ntohs(opt_data.mtu); }
306 void tr(ip_opt_data_tr &tr) const;
307 const uint32_t *addext() const { return &opt_data.addext[0]; }
308 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
309 void sdb(std::vector<uint32_t> &vec) const;
310};
311
312/*
313 * TCP Stuff
314 */
315struct TcpOpt;
316struct TcpHdr : public tcp_hdr
317{
318 uint16_t sport() const { return ntohs(th_sport); }
319 uint16_t dport() const { return ntohs(th_dport); }
320 uint32_t seq() const { return ntohl(th_seq); }
321 uint32_t ack() const { return ntohl(th_ack); }
322 uint8_t off() const { return th_off; }
323 uint8_t flags() const { return th_flags & 0x3f; }
324 uint16_t win() const { return ntohs(th_win); }
325 uint16_t sum() const { return th_sum; }
326 uint16_t urp() const { return ntohs(th_urp); }
327
328 void sum(uint16_t sum) { th_sum = sum; }
329 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
330 void flags(uint8_t _flags) { th_flags = _flags; }
331
332 bool options(std::vector<const TcpOpt *> &vec) const;
333
334 int size() const { return off(); }
335 const uint8_t *bytes() const { return (const uint8_t *)this; }
336 const uint8_t *payload() const { return bytes() + size(); }
337 uint8_t *bytes() { return (uint8_t *)this; }
338 uint8_t *payload() { return bytes() + size(); }
339};
340
341class TcpPtr
342{
343 protected:
344 EthPacketPtr p;
345 int _off;
346
347 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
348 void set(const IpPtr &ptr)
349 {
350 if (ptr && ptr->proto() == IP_PROTO_TCP)
351 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
352 else
353 set(0, 0);
354 }
355
356 public:
357 TcpPtr() : p(0), _off(0) {}
358 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
359 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
360
361 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
362 TcpHdr *operator->() { return get(); }
363 TcpHdr &operator*() { return *get(); }
364
365 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
366 const TcpHdr *operator->() const { return get(); }
367 const TcpHdr &operator*() const { return *get(); }
368
369 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
370 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; }
371
372 const EthPacketPtr packet() const { return p; }
373 EthPacketPtr packet() { return p; }
374 bool operator!() const { return !p; }
375 operator bool() const { return p; }
376 int off() const { return _off; }
377 int pstart() const { return off() + get()->size(); }
378};
379
380uint16_t cksum(const TcpPtr &ptr);
381
382typedef Range<uint16_t> SackRange;
383
384struct TcpOpt : public tcp_opt
385{
386 uint8_t type() const { return opt_type; }
387 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
388
389 bool isopt(int opt) const { return type() == opt; }
390
391 const uint8_t *data() const { return opt_data.data8; }
392
393 uint16_t mss() const { return ntohs(opt_data.mss); }
394 uint8_t wscale() const { return opt_data.wscale; }
395 bool sack(std::vector<SackRange> &vec) const;
396 uint32_t echo() const { return ntohl(opt_data.echo); }
397 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
398 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
399 uint32_t cc() const { return ntohl(opt_data.cc); }
400 uint8_t cksum() const{ return opt_data.cksum; }
401 const uint8_t *md5() const { return opt_data.md5; }
402
403 int size() const { return len(); }
404 const uint8_t *bytes() const { return (const uint8_t *)this; }
405 const uint8_t *payload() const { return bytes() + size(); }
406 uint8_t *bytes() { return (uint8_t *)this; }
407 uint8_t *payload() { return bytes() + size(); }
408};
409
410/*
411 * UDP Stuff
412 */
413struct UdpHdr : public udp_hdr
414{
415 uint16_t sport() const { return ntohs(uh_sport); }
416 uint16_t dport() const { return ntohs(uh_dport); }
417 uint16_t len() const { return ntohs(uh_ulen); }
418 uint16_t sum() const { return uh_sum; }
419
420 void sum(uint16_t sum) { uh_sum = sum; }
421 void len(uint16_t _len) { uh_ulen = htons(_len); }
422
423 int size() const { return sizeof(udp_hdr); }
424 const uint8_t *bytes() const { return (const uint8_t *)this; }
425 const uint8_t *payload() const { return bytes() + size(); }
426 uint8_t *bytes() { return (uint8_t *)this; }
427 uint8_t *payload() { return bytes() + size(); }
428};
429
430class UdpPtr
431{
432 protected:
433 EthPacketPtr p;
434 int _off;
435
436 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
437 void set(const IpPtr &ptr)
438 {
439 if (ptr && ptr->proto() == IP_PROTO_UDP)
440 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
441 else
442 set(0, 0);
443 }
444
445 public:
446 UdpPtr() : p(0), _off(0) {}
447 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
448 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
449
450 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
451 UdpHdr *operator->() { return get(); }
452 UdpHdr &operator*() { return *get(); }
453
454 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
455 const UdpHdr *operator->() const { return get(); }
456 const UdpHdr &operator*() const { return *get(); }
457
458 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
459 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; }
460
461 const EthPacketPtr packet() const { return p; }
462 EthPacketPtr packet() { return p; }
463 bool operator!() const { return !p; }
464 operator bool() const { return p; }
465 int off() const { return _off; }
466 int pstart() const { return off() + get()->size(); }
467};
468
469uint16_t cksum(const UdpPtr &ptr);
470
471int hsplit(const EthPacketPtr &ptr);
472
473/* namespace Net */ }
474
475#endif // __BASE_INET_HH__
32 */
33
34#ifndef __BASE_INET_HH__
35#define __BASE_INET_HH__
36
37#include <iosfwd>
38#include <string>
39#include <utility>
40#include <vector>
41
42#include "base/range.hh"
43#include "base/types.hh"
44#include "dev/etherpkt.hh"
45
46#include "dnet/os.h"
47#include "dnet/eth.h"
48#include "dnet/ip.h"
49#include "dnet/ip6.h"
50#include "dnet/addr.h"
51#include "dnet/arp.h"
52#include "dnet/icmp.h"
53#include "dnet/tcp.h"
54#include "dnet/udp.h"
55#include "dnet/intf.h"
56#include "dnet/route.h"
57#include "dnet/fw.h"
58#include "dnet/blob.h"
59#include "dnet/rand.h"
60
61namespace Net {
62
63/*
64 * Ethernet Stuff
65 */
66struct EthAddr : protected eth_addr
67{
68 protected:
69 void parse(const std::string &addr);
70
71 public:
72 EthAddr();
73 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
74 EthAddr(const eth_addr &ea);
75 EthAddr(const std::string &addr);
76 const EthAddr &operator=(const eth_addr &ea);
77 const EthAddr &operator=(const std::string &addr);
78
79 int size() const { return sizeof(eth_addr); }
80
81 const uint8_t *bytes() const { return &data[0]; }
82 uint8_t *bytes() { return &data[0]; }
83
84 const uint8_t *addr() const { return &data[0]; }
85 bool unicast() const { return data[0] == 0x00; }
86 bool multicast() const { return data[0] == 0x01; }
87 bool broadcast() const { return data[0] == 0xff; }
88 std::string string() const;
89
90 operator uint64_t() const
91 {
92 uint64_t reg = 0;
93 reg |= ((uint64_t)data[0]) << 40;
94 reg |= ((uint64_t)data[1]) << 32;
95 reg |= ((uint64_t)data[2]) << 24;
96 reg |= ((uint64_t)data[3]) << 16;
97 reg |= ((uint64_t)data[4]) << 8;
98 reg |= ((uint64_t)data[5]) << 0;
99 return reg;
100 }
101
102};
103
104std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
105bool operator==(const EthAddr &left, const EthAddr &right);
106
107struct EthHdr : public eth_hdr
108{
109 uint16_t type() const { return ntohs(eth_type); }
110 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
111 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
112
113 int size() const { return sizeof(eth_hdr); }
114
115 const uint8_t *bytes() const { return (const uint8_t *)this; }
116 const uint8_t *payload() const { return bytes() + size(); }
117 uint8_t *bytes() { return (uint8_t *)this; }
118 uint8_t *payload() { return bytes() + size(); }
119};
120
121class EthPtr
122{
123 protected:
124 friend class IpPtr;
125 EthPacketPtr p;
126
127 public:
128 EthPtr() {}
129 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
130
131 EthHdr *operator->() { return (EthHdr *)p->data; }
132 EthHdr &operator*() { return *(EthHdr *)p->data; }
133 operator EthHdr *() { return (EthHdr *)p->data; }
134
135 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
136 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
137 operator const EthHdr *() const { return (const EthHdr *)p->data; }
138
139 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
140
141 const EthPacketPtr packet() const { return p; }
142 EthPacketPtr packet() { return p; }
143 bool operator!() const { return !p; }
144 operator bool() const { return p; }
145 int off() const { return 0; }
146 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
147};
148
149/*
150 * IP Stuff
151 */
152struct IpAddress
153{
154 protected:
155 uint32_t _ip;
156
157 public:
158 IpAddress() : _ip(0)
159 {}
160 IpAddress(const uint32_t __ip) : _ip(__ip)
161 {}
162
163 uint32_t ip() const { return _ip; }
164
165 std::string string() const;
166};
167
168std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
169bool operator==(const IpAddress &left, const IpAddress &right);
170
171struct IpNetmask : public IpAddress
172{
173 protected:
174 uint8_t _netmask;
175
176 public:
177 IpNetmask() : IpAddress(), _netmask(0)
178 {}
179 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
180 IpAddress(__ip), _netmask(__netmask)
181 {}
182
183 uint8_t netmask() const { return _netmask; }
184
185 std::string string() const;
186};
187
188std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
189bool operator==(const IpNetmask &left, const IpNetmask &right);
190
191struct IpWithPort : public IpAddress
192{
193 protected:
194 uint16_t _port;
195
196 public:
197 IpWithPort() : IpAddress(), _port(0)
198 {}
199 IpWithPort(const uint32_t __ip, const uint16_t __port) :
200 IpAddress(__ip), _port(__port)
201 {}
202
203 uint8_t port() const { return _port; }
204
205 std::string string() const;
206};
207
208std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
209bool operator==(const IpWithPort &left, const IpWithPort &right);
210
211struct IpOpt;
212struct IpHdr : public ip_hdr
213{
214 uint8_t version() const { return ip_v; }
215 uint8_t hlen() const { return ip_hl * 4; }
216 uint8_t tos() const { return ip_tos; }
217 uint16_t len() const { return ntohs(ip_len); }
218 uint16_t id() const { return ntohs(ip_id); }
219 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
220 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
221 uint8_t ttl() const { return ip_ttl; }
222 uint8_t proto() const { return ip_p; }
223 uint16_t sum() const { return ip_sum; }
224 uint32_t src() const { return ntohl(ip_src); }
225 uint32_t dst() const { return ntohl(ip_dst); }
226
227 void sum(uint16_t sum) { ip_sum = sum; }
228 void id(uint16_t _id) { ip_id = htons(_id); }
229 void len(uint16_t _len) { ip_len = htons(_len); }
230
231
232 bool options(std::vector<const IpOpt *> &vec) const;
233
234 int size() const { return hlen(); }
235 const uint8_t *bytes() const { return (const uint8_t *)this; }
236 const uint8_t *payload() const { return bytes() + size(); }
237 uint8_t *bytes() { return (uint8_t *)this; }
238 uint8_t *payload() { return bytes() + size(); }
239};
240
241class IpPtr
242{
243 protected:
244 friend class TcpPtr;
245 friend class UdpPtr;
246 EthPacketPtr p;
247
248 void set(const EthPacketPtr &ptr)
249 {
250 p = 0;
251
252 if (ptr) {
253 EthHdr *eth = (EthHdr *)ptr->data;
254 if (eth->type() == ETH_TYPE_IP)
255 p = ptr;
256 }
257 }
258
259 public:
260 IpPtr() : p(0) {}
261 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); }
262 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); }
263 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
264
265 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
266 IpHdr *operator->() { return get(); }
267 IpHdr &operator*() { return *get(); }
268
269 const IpHdr *get() const
270 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
271 const IpHdr *operator->() const { return get(); }
272 const IpHdr &operator*() const { return *get(); }
273
274 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
275 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
276 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
277
278 const EthPacketPtr packet() const { return p; }
279 EthPacketPtr packet() { return p; }
280 bool operator!() const { return !p; }
281 operator bool() const { return p; }
282 int off() const { return sizeof(eth_hdr); }
283 int pstart() const { return off() + get()->size(); }
284};
285
286uint16_t cksum(const IpPtr &ptr);
287
288struct IpOpt : public ip_opt
289{
290 uint8_t type() const { return opt_type; }
291 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
292 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
293 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
294 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
295
296 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
297 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
298 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
299
300 const uint8_t *data() const { return opt_data.data8; }
301 void sec(ip_opt_data_sec &sec) const;
302 void lsrr(ip_opt_data_rr &rr) const;
303 void ssrr(ip_opt_data_rr &rr) const;
304 void ts(ip_opt_data_ts &ts) const;
305 uint16_t satid() const { return ntohs(opt_data.satid); }
306 uint16_t mtup() const { return ntohs(opt_data.mtu); }
307 uint16_t mtur() const { return ntohs(opt_data.mtu); }
308 void tr(ip_opt_data_tr &tr) const;
309 const uint32_t *addext() const { return &opt_data.addext[0]; }
310 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
311 void sdb(std::vector<uint32_t> &vec) const;
312};
313
314/*
315 * TCP Stuff
316 */
317struct TcpOpt;
318struct TcpHdr : public tcp_hdr
319{
320 uint16_t sport() const { return ntohs(th_sport); }
321 uint16_t dport() const { return ntohs(th_dport); }
322 uint32_t seq() const { return ntohl(th_seq); }
323 uint32_t ack() const { return ntohl(th_ack); }
324 uint8_t off() const { return th_off; }
325 uint8_t flags() const { return th_flags & 0x3f; }
326 uint16_t win() const { return ntohs(th_win); }
327 uint16_t sum() const { return th_sum; }
328 uint16_t urp() const { return ntohs(th_urp); }
329
330 void sum(uint16_t sum) { th_sum = sum; }
331 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
332 void flags(uint8_t _flags) { th_flags = _flags; }
333
334 bool options(std::vector<const TcpOpt *> &vec) const;
335
336 int size() const { return off(); }
337 const uint8_t *bytes() const { return (const uint8_t *)this; }
338 const uint8_t *payload() const { return bytes() + size(); }
339 uint8_t *bytes() { return (uint8_t *)this; }
340 uint8_t *payload() { return bytes() + size(); }
341};
342
343class TcpPtr
344{
345 protected:
346 EthPacketPtr p;
347 int _off;
348
349 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
350 void set(const IpPtr &ptr)
351 {
352 if (ptr && ptr->proto() == IP_PROTO_TCP)
353 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
354 else
355 set(0, 0);
356 }
357
358 public:
359 TcpPtr() : p(0), _off(0) {}
360 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
361 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
362
363 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
364 TcpHdr *operator->() { return get(); }
365 TcpHdr &operator*() { return *get(); }
366
367 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
368 const TcpHdr *operator->() const { return get(); }
369 const TcpHdr &operator*() const { return *get(); }
370
371 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
372 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; }
373
374 const EthPacketPtr packet() const { return p; }
375 EthPacketPtr packet() { return p; }
376 bool operator!() const { return !p; }
377 operator bool() const { return p; }
378 int off() const { return _off; }
379 int pstart() const { return off() + get()->size(); }
380};
381
382uint16_t cksum(const TcpPtr &ptr);
383
384typedef Range<uint16_t> SackRange;
385
386struct TcpOpt : public tcp_opt
387{
388 uint8_t type() const { return opt_type; }
389 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
390
391 bool isopt(int opt) const { return type() == opt; }
392
393 const uint8_t *data() const { return opt_data.data8; }
394
395 uint16_t mss() const { return ntohs(opt_data.mss); }
396 uint8_t wscale() const { return opt_data.wscale; }
397 bool sack(std::vector<SackRange> &vec) const;
398 uint32_t echo() const { return ntohl(opt_data.echo); }
399 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
400 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
401 uint32_t cc() const { return ntohl(opt_data.cc); }
402 uint8_t cksum() const{ return opt_data.cksum; }
403 const uint8_t *md5() const { return opt_data.md5; }
404
405 int size() const { return len(); }
406 const uint8_t *bytes() const { return (const uint8_t *)this; }
407 const uint8_t *payload() const { return bytes() + size(); }
408 uint8_t *bytes() { return (uint8_t *)this; }
409 uint8_t *payload() { return bytes() + size(); }
410};
411
412/*
413 * UDP Stuff
414 */
415struct UdpHdr : public udp_hdr
416{
417 uint16_t sport() const { return ntohs(uh_sport); }
418 uint16_t dport() const { return ntohs(uh_dport); }
419 uint16_t len() const { return ntohs(uh_ulen); }
420 uint16_t sum() const { return uh_sum; }
421
422 void sum(uint16_t sum) { uh_sum = sum; }
423 void len(uint16_t _len) { uh_ulen = htons(_len); }
424
425 int size() const { return sizeof(udp_hdr); }
426 const uint8_t *bytes() const { return (const uint8_t *)this; }
427 const uint8_t *payload() const { return bytes() + size(); }
428 uint8_t *bytes() { return (uint8_t *)this; }
429 uint8_t *payload() { return bytes() + size(); }
430};
431
432class UdpPtr
433{
434 protected:
435 EthPacketPtr p;
436 int _off;
437
438 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
439 void set(const IpPtr &ptr)
440 {
441 if (ptr && ptr->proto() == IP_PROTO_UDP)
442 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
443 else
444 set(0, 0);
445 }
446
447 public:
448 UdpPtr() : p(0), _off(0) {}
449 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
450 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
451
452 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
453 UdpHdr *operator->() { return get(); }
454 UdpHdr &operator*() { return *get(); }
455
456 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
457 const UdpHdr *operator->() const { return get(); }
458 const UdpHdr &operator*() const { return *get(); }
459
460 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
461 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; }
462
463 const EthPacketPtr packet() const { return p; }
464 EthPacketPtr packet() { return p; }
465 bool operator!() const { return !p; }
466 operator bool() const { return p; }
467 int off() const { return _off; }
468 int pstart() const { return off() + get()->size(); }
469};
470
471uint16_t cksum(const UdpPtr &ptr);
472
473int hsplit(const EthPacketPtr &ptr);
474
475/* namespace Net */ }
476
477#endif // __BASE_INET_HH__