inet.hh (2632:1bb2f91485ea) inet.hh (2665:a124942bacb8)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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
27 */
28
29#ifndef __BASE_INET_HH__
30#define __BASE_INET_HH__
31
32#include <iosfwd>
33#include <string>
34#include <utility>
35#include <vector>
36
37#include "base/range.hh"
38#include "dev/etherpkt.hh"
39#include "sim/host.hh"
40
41#include "dnet/os.h"
42#include "dnet/eth.h"
43#include "dnet/ip.h"
44#include "dnet/ip6.h"
45#include "dnet/addr.h"
46#include "dnet/arp.h"
47#include "dnet/icmp.h"
48#include "dnet/tcp.h"
49#include "dnet/udp.h"
50#include "dnet/intf.h"
51#include "dnet/route.h"
52#include "dnet/fw.h"
53#include "dnet/blob.h"
54#include "dnet/rand.h"
55
56namespace Net {
57
58/*
59 * Ethernet Stuff
60 */
61struct EthAddr : protected eth_addr
62{
63 protected:
64 void parse(const std::string &addr);
65
66 public:
67 EthAddr();
68 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
69 EthAddr(const eth_addr &ea);
70 EthAddr(const std::string &addr);
71 const EthAddr &operator=(const eth_addr &ea);
72 const EthAddr &operator=(const std::string &addr);
73
74 int size() const { return sizeof(eth_addr); }
75
76 const uint8_t *bytes() const { return &data[0]; }
77 uint8_t *bytes() { return &data[0]; }
78
79 const uint8_t *addr() const { return &data[0]; }
80 bool unicast() const { return data[0] == 0x00; }
81 bool multicast() const { return data[0] == 0x01; }
82 bool broadcast() const { return data[0] == 0xff; }
83 std::string string() const;
84
85 operator uint64_t() const
86 {
87 uint64_t reg = 0;
88 reg |= ((uint64_t)data[0]) << 40;
89 reg |= ((uint64_t)data[1]) << 32;
90 reg |= ((uint64_t)data[2]) << 24;
91 reg |= ((uint64_t)data[3]) << 16;
92 reg |= ((uint64_t)data[4]) << 8;
93 reg |= ((uint64_t)data[5]) << 0;
94 return reg;
95 }
96
97};
98
99std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
100bool operator==(const EthAddr &left, const EthAddr &right);
101
102struct EthHdr : public eth_hdr
103{
104 uint16_t type() const { return ntohs(eth_type); }
105 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
106 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
107
108 int size() const { return sizeof(eth_hdr); }
109
110 const uint8_t *bytes() const { return (const uint8_t *)this; }
111 const uint8_t *payload() const { return bytes() + size(); }
112 uint8_t *bytes() { return (uint8_t *)this; }
113 uint8_t *payload() { return bytes() + size(); }
114};
115
116class EthPtr
117{
118 protected:
119 friend class IpPtr;
120 EthPacketPtr p;
121
122 public:
123 EthPtr() {}
124 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
125
126 EthHdr *operator->() { return (EthHdr *)p->data; }
127 EthHdr &operator*() { return *(EthHdr *)p->data; }
128 operator EthHdr *() { return (EthHdr *)p->data; }
129
130 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
131 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
132 operator const EthHdr *() const { return (const EthHdr *)p->data; }
133
134 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
135
136 const EthPacketPtr packet() const { return p; }
137 EthPacketPtr packet() { return p; }
138 bool operator!() const { return !p; }
139 operator bool() const { return p; }
140};
141
142/*
143 * IP Stuff
144 */
145struct IpOpt;
146struct IpHdr : public ip_hdr
147{
148 uint8_t version() const { return ip_v; }
149 uint8_t hlen() const { return ip_hl * 4; }
150 uint8_t tos() const { return ip_tos; }
151 uint16_t len() const { return ntohs(ip_len); }
152 uint16_t id() const { return ntohs(ip_id); }
153 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
154 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
155 uint8_t ttl() const { return ip_ttl; }
156 uint8_t proto() const { return ip_p; }
157 uint16_t sum() const { return ip_sum; }
158 uint32_t src() const { return ntohl(ip_src); }
159 uint32_t dst() const { return ntohl(ip_dst); }
160
161 void sum(uint16_t sum) { ip_sum = sum; }
162
163 bool options(std::vector<const IpOpt *> &vec) const;
164
165 int size() const { return hlen(); }
166 const uint8_t *bytes() const { return (const uint8_t *)this; }
167 const uint8_t *payload() const { return bytes() + size(); }
168 uint8_t *bytes() { return (uint8_t *)this; }
169 uint8_t *payload() { return bytes() + size(); }
170};
171
172class IpPtr
173{
174 protected:
175 friend class TcpPtr;
176 friend class UdpPtr;
177 EthPacketPtr p;
178
179 const IpHdr *h() const
180 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
181 IpHdr *h() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
182
183 void set(const EthPacketPtr &ptr)
184 {
185 EthHdr *eth = (EthHdr *)ptr->data;
186 if (eth->type() == ETH_TYPE_IP)
187 p = ptr;
188 else
189 p = 0;
190 }
191
192 public:
193 IpPtr() {}
194 IpPtr(const EthPacketPtr &ptr) { set(ptr); }
195 IpPtr(const EthPtr &ptr) { set(ptr.p); }
196 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
197
198 IpHdr *operator->() { return h(); }
199 IpHdr &operator*() { return *h(); }
200 operator IpHdr *() { return h(); }
201
202 const IpHdr *operator->() const { return h(); }
203 const IpHdr &operator*() const { return *h(); }
204 operator const IpHdr *() const { return h(); }
205
206 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
207 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
208 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
209
210 const EthPacketPtr packet() const { return p; }
211 EthPacketPtr packet() { return p; }
212 bool operator!() const { return !p; }
213 operator bool() const { return p; }
214 operator bool() { return p; }
215};
216
217uint16_t cksum(const IpPtr &ptr);
218
219struct IpOpt : public ip_opt
220{
221 uint8_t type() const { return opt_type; }
222 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
223 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
224 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
225 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
226
227 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
228 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
229 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
230
231 const uint8_t *data() const { return opt_data.data8; }
232 void sec(ip_opt_data_sec &sec) const;
233 void lsrr(ip_opt_data_rr &rr) const;
234 void ssrr(ip_opt_data_rr &rr) const;
235 void ts(ip_opt_data_ts &ts) const;
236 uint16_t satid() const { return ntohs(opt_data.satid); }
237 uint16_t mtup() const { return ntohs(opt_data.mtu); }
238 uint16_t mtur() const { return ntohs(opt_data.mtu); }
239 void tr(ip_opt_data_tr &tr) const;
240 const uint32_t *addext() const { return &opt_data.addext[0]; }
241 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
242 void sdb(std::vector<uint32_t> &vec) const;
243};
244
245/*
246 * TCP Stuff
247 */
248struct TcpOpt;
249struct TcpHdr : public tcp_hdr
250{
251 uint16_t sport() const { return ntohs(th_sport); }
252 uint16_t dport() const { return ntohs(th_dport); }
253 uint32_t seq() const { return ntohl(th_seq); }
254 uint32_t ack() const { return ntohl(th_ack); }
255 uint8_t off() const { return th_off; }
256 uint8_t flags() const { return th_flags & 0x3f; }
257 uint16_t win() const { return ntohs(th_win); }
258 uint16_t sum() const { return th_sum; }
259 uint16_t urp() const { return ntohs(th_urp); }
260
261 void sum(uint16_t sum) { th_sum = sum; }
262
263 bool options(std::vector<const TcpOpt *> &vec) const;
264
265 int size() const { return off(); }
266 const uint8_t *bytes() const { return (const uint8_t *)this; }
267 const uint8_t *payload() const { return bytes() + size(); }
268 uint8_t *bytes() { return (uint8_t *)this; }
269 uint8_t *payload() { return bytes() + size(); }
270};
271
272class TcpPtr
273{
274 protected:
275 EthPacketPtr p;
276 int off;
277
278 const TcpHdr *h() const { return (const TcpHdr *)(p->data + off); }
279 TcpHdr *h() { return (TcpHdr *)(p->data + off); }
280
281 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; }
282 void set(const IpPtr &ptr)
283 {
284 if (ptr->proto() == IP_PROTO_TCP)
285 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
286 else
287 set(0, 0);
288 }
289
290 public:
291 TcpPtr() {}
292 TcpPtr(const IpPtr &ptr) { set(ptr); }
293 TcpPtr(const TcpPtr &ptr) : p(ptr.p), off(ptr.off) {}
294
295 TcpHdr *operator->() { return h(); }
296 TcpHdr &operator*() { return *h(); }
297 operator TcpHdr *() { return h(); }
298
299 const TcpHdr *operator->() const { return h(); }
300 const TcpHdr &operator*() const { return *h(); }
301 operator const TcpHdr *() const { return h(); }
302
303 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
304 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t.off); return *this; }
305
306 const EthPacketPtr packet() const { return p; }
307 EthPacketPtr packet() { return p; }
308 bool operator!() const { return !p; }
309 operator bool() const { return p; }
310 operator bool() { return p; }
311};
312
313uint16_t cksum(const TcpPtr &ptr);
314
315typedef Range<uint16_t> SackRange;
316
317struct TcpOpt : public tcp_opt
318{
319 uint8_t type() const { return opt_type; }
320 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
321
322 bool isopt(int opt) const { return type() == opt; }
323
324 const uint8_t *data() const { return opt_data.data8; }
325
326 uint16_t mss() const { return ntohs(opt_data.mss); }
327 uint8_t wscale() const { return opt_data.wscale; }
328 bool sack(std::vector<SackRange> &vec) const;
329 uint32_t echo() const { return ntohl(opt_data.echo); }
330 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
331 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
332 uint32_t cc() const { return ntohl(opt_data.cc); }
333 uint8_t cksum() const{ return opt_data.cksum; }
334 const uint8_t *md5() const { return opt_data.md5; }
335
336 int size() const { return len(); }
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
343/*
344 * UDP Stuff
345 */
346struct UdpHdr : public udp_hdr
347{
348 uint16_t sport() const { return ntohs(uh_sport); }
349 uint16_t dport() const { return ntohs(uh_dport); }
350 uint16_t len() const { return ntohs(uh_ulen); }
351 uint16_t sum() const { return uh_sum; }
352
353 void sum(uint16_t sum) { uh_sum = sum; }
354
355 int size() const { return sizeof(udp_hdr); }
356 const uint8_t *bytes() const { return (const uint8_t *)this; }
357 const uint8_t *payload() const { return bytes() + size(); }
358 uint8_t *bytes() { return (uint8_t *)this; }
359 uint8_t *payload() { return bytes() + size(); }
360};
361
362class UdpPtr
363{
364 protected:
365 EthPacketPtr p;
366 int off;
367
368 const UdpHdr *h() const { return (const UdpHdr *)(p->data + off); }
369 UdpHdr *h() { return (UdpHdr *)(p->data + off); }
370
371 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; }
372 void set(const IpPtr &ptr)
373 {
374 if (ptr->proto() == IP_PROTO_UDP)
375 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
376 else
377 set(0, 0);
378 }
379
380 public:
381 UdpPtr() {}
382 UdpPtr(const IpPtr &ptr) { set(ptr); }
383 UdpPtr(const UdpPtr &ptr) : p(ptr.p), off(ptr.off) {}
384
385 UdpHdr *operator->() { return h(); }
386 UdpHdr &operator*() { return *h(); }
387 operator UdpHdr *() { return h(); }
388
389 const UdpHdr *operator->() const { return h(); }
390 const UdpHdr &operator*() const { return *h(); }
391 operator const UdpHdr *() const { return h(); }
392
393 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
394 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t.off); return *this; }
395
396 const EthPacketPtr packet() const { return p; }
397 EthPacketPtr packet() { return p; }
398 bool operator!() const { return !p; }
399 operator bool() const { return p; }
400 operator bool() { return p; }
401};
402
403uint16_t cksum(const UdpPtr &ptr);
404
405/* namespace Net */ }
406
407#endif // __BASE_INET_HH__
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 "dev/etherpkt.hh"
42#include "sim/host.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};
144
145/*
146 * IP Stuff
147 */
148struct IpOpt;
149struct IpHdr : public ip_hdr
150{
151 uint8_t version() const { return ip_v; }
152 uint8_t hlen() const { return ip_hl * 4; }
153 uint8_t tos() const { return ip_tos; }
154 uint16_t len() const { return ntohs(ip_len); }
155 uint16_t id() const { return ntohs(ip_id); }
156 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
157 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
158 uint8_t ttl() const { return ip_ttl; }
159 uint8_t proto() const { return ip_p; }
160 uint16_t sum() const { return ip_sum; }
161 uint32_t src() const { return ntohl(ip_src); }
162 uint32_t dst() const { return ntohl(ip_dst); }
163
164 void sum(uint16_t sum) { ip_sum = sum; }
165
166 bool options(std::vector<const IpOpt *> &vec) const;
167
168 int size() const { return hlen(); }
169 const uint8_t *bytes() const { return (const uint8_t *)this; }
170 const uint8_t *payload() const { return bytes() + size(); }
171 uint8_t *bytes() { return (uint8_t *)this; }
172 uint8_t *payload() { return bytes() + size(); }
173};
174
175class IpPtr
176{
177 protected:
178 friend class TcpPtr;
179 friend class UdpPtr;
180 EthPacketPtr p;
181
182 const IpHdr *h() const
183 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
184 IpHdr *h() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
185
186 void set(const EthPacketPtr &ptr)
187 {
188 EthHdr *eth = (EthHdr *)ptr->data;
189 if (eth->type() == ETH_TYPE_IP)
190 p = ptr;
191 else
192 p = 0;
193 }
194
195 public:
196 IpPtr() {}
197 IpPtr(const EthPacketPtr &ptr) { set(ptr); }
198 IpPtr(const EthPtr &ptr) { set(ptr.p); }
199 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
200
201 IpHdr *operator->() { return h(); }
202 IpHdr &operator*() { return *h(); }
203 operator IpHdr *() { return h(); }
204
205 const IpHdr *operator->() const { return h(); }
206 const IpHdr &operator*() const { return *h(); }
207 operator const IpHdr *() const { return h(); }
208
209 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
210 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
211 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
212
213 const EthPacketPtr packet() const { return p; }
214 EthPacketPtr packet() { return p; }
215 bool operator!() const { return !p; }
216 operator bool() const { return p; }
217 operator bool() { return p; }
218};
219
220uint16_t cksum(const IpPtr &ptr);
221
222struct IpOpt : public ip_opt
223{
224 uint8_t type() const { return opt_type; }
225 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
226 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
227 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
228 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
229
230 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
231 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
232 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
233
234 const uint8_t *data() const { return opt_data.data8; }
235 void sec(ip_opt_data_sec &sec) const;
236 void lsrr(ip_opt_data_rr &rr) const;
237 void ssrr(ip_opt_data_rr &rr) const;
238 void ts(ip_opt_data_ts &ts) const;
239 uint16_t satid() const { return ntohs(opt_data.satid); }
240 uint16_t mtup() const { return ntohs(opt_data.mtu); }
241 uint16_t mtur() const { return ntohs(opt_data.mtu); }
242 void tr(ip_opt_data_tr &tr) const;
243 const uint32_t *addext() const { return &opt_data.addext[0]; }
244 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
245 void sdb(std::vector<uint32_t> &vec) const;
246};
247
248/*
249 * TCP Stuff
250 */
251struct TcpOpt;
252struct TcpHdr : public tcp_hdr
253{
254 uint16_t sport() const { return ntohs(th_sport); }
255 uint16_t dport() const { return ntohs(th_dport); }
256 uint32_t seq() const { return ntohl(th_seq); }
257 uint32_t ack() const { return ntohl(th_ack); }
258 uint8_t off() const { return th_off; }
259 uint8_t flags() const { return th_flags & 0x3f; }
260 uint16_t win() const { return ntohs(th_win); }
261 uint16_t sum() const { return th_sum; }
262 uint16_t urp() const { return ntohs(th_urp); }
263
264 void sum(uint16_t sum) { th_sum = sum; }
265
266 bool options(std::vector<const TcpOpt *> &vec) const;
267
268 int size() const { return off(); }
269 const uint8_t *bytes() const { return (const uint8_t *)this; }
270 const uint8_t *payload() const { return bytes() + size(); }
271 uint8_t *bytes() { return (uint8_t *)this; }
272 uint8_t *payload() { return bytes() + size(); }
273};
274
275class TcpPtr
276{
277 protected:
278 EthPacketPtr p;
279 int off;
280
281 const TcpHdr *h() const { return (const TcpHdr *)(p->data + off); }
282 TcpHdr *h() { return (TcpHdr *)(p->data + off); }
283
284 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; }
285 void set(const IpPtr &ptr)
286 {
287 if (ptr->proto() == IP_PROTO_TCP)
288 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
289 else
290 set(0, 0);
291 }
292
293 public:
294 TcpPtr() {}
295 TcpPtr(const IpPtr &ptr) { set(ptr); }
296 TcpPtr(const TcpPtr &ptr) : p(ptr.p), off(ptr.off) {}
297
298 TcpHdr *operator->() { return h(); }
299 TcpHdr &operator*() { return *h(); }
300 operator TcpHdr *() { return h(); }
301
302 const TcpHdr *operator->() const { return h(); }
303 const TcpHdr &operator*() const { return *h(); }
304 operator const TcpHdr *() const { return h(); }
305
306 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
307 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t.off); return *this; }
308
309 const EthPacketPtr packet() const { return p; }
310 EthPacketPtr packet() { return p; }
311 bool operator!() const { return !p; }
312 operator bool() const { return p; }
313 operator bool() { return p; }
314};
315
316uint16_t cksum(const TcpPtr &ptr);
317
318typedef Range<uint16_t> SackRange;
319
320struct TcpOpt : public tcp_opt
321{
322 uint8_t type() const { return opt_type; }
323 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
324
325 bool isopt(int opt) const { return type() == opt; }
326
327 const uint8_t *data() const { return opt_data.data8; }
328
329 uint16_t mss() const { return ntohs(opt_data.mss); }
330 uint8_t wscale() const { return opt_data.wscale; }
331 bool sack(std::vector<SackRange> &vec) const;
332 uint32_t echo() const { return ntohl(opt_data.echo); }
333 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
334 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
335 uint32_t cc() const { return ntohl(opt_data.cc); }
336 uint8_t cksum() const{ return opt_data.cksum; }
337 const uint8_t *md5() const { return opt_data.md5; }
338
339 int size() const { return len(); }
340 const uint8_t *bytes() const { return (const uint8_t *)this; }
341 const uint8_t *payload() const { return bytes() + size(); }
342 uint8_t *bytes() { return (uint8_t *)this; }
343 uint8_t *payload() { return bytes() + size(); }
344};
345
346/*
347 * UDP Stuff
348 */
349struct UdpHdr : public udp_hdr
350{
351 uint16_t sport() const { return ntohs(uh_sport); }
352 uint16_t dport() const { return ntohs(uh_dport); }
353 uint16_t len() const { return ntohs(uh_ulen); }
354 uint16_t sum() const { return uh_sum; }
355
356 void sum(uint16_t sum) { uh_sum = sum; }
357
358 int size() const { return sizeof(udp_hdr); }
359 const uint8_t *bytes() const { return (const uint8_t *)this; }
360 const uint8_t *payload() const { return bytes() + size(); }
361 uint8_t *bytes() { return (uint8_t *)this; }
362 uint8_t *payload() { return bytes() + size(); }
363};
364
365class UdpPtr
366{
367 protected:
368 EthPacketPtr p;
369 int off;
370
371 const UdpHdr *h() const { return (const UdpHdr *)(p->data + off); }
372 UdpHdr *h() { return (UdpHdr *)(p->data + off); }
373
374 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; }
375 void set(const IpPtr &ptr)
376 {
377 if (ptr->proto() == IP_PROTO_UDP)
378 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
379 else
380 set(0, 0);
381 }
382
383 public:
384 UdpPtr() {}
385 UdpPtr(const IpPtr &ptr) { set(ptr); }
386 UdpPtr(const UdpPtr &ptr) : p(ptr.p), off(ptr.off) {}
387
388 UdpHdr *operator->() { return h(); }
389 UdpHdr &operator*() { return *h(); }
390 operator UdpHdr *() { return h(); }
391
392 const UdpHdr *operator->() const { return h(); }
393 const UdpHdr &operator*() const { return *h(); }
394 operator const UdpHdr *() const { return h(); }
395
396 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
397 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t.off); return *this; }
398
399 const EthPacketPtr packet() const { return p; }
400 EthPacketPtr packet() { return p; }
401 bool operator!() const { return !p; }
402 operator bool() const { return p; }
403 operator bool() { return p; }
404};
405
406uint16_t cksum(const UdpPtr &ptr);
407
408/* namespace Net */ }
409
410#endif // __BASE_INET_HH__