misc: Merge branch v20.1.0.3 hotfix into develop
[gem5.git] / src / base / inet.hh
1 /*
2 * Copyright (c) 2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 #ifndef __BASE_INET_HH__
43 #define __BASE_INET_HH__
44
45 #include <iosfwd>
46 #include <string>
47 #include <utility>
48 #include <vector>
49
50 #include "base/types.hh"
51 #include "dev/net/etherpkt.hh"
52 #include "dnet/os.h"
53 #include "dnet/eth.h"
54 #include "dnet/ip.h"
55 #include "dnet/ip6.h"
56 #include "dnet/addr.h"
57 #include "dnet/arp.h"
58 #include "dnet/icmp.h"
59 #include "dnet/tcp.h"
60 #include "dnet/udp.h"
61 #include "dnet/intf.h"
62 #include "dnet/route.h"
63 #include "dnet/fw.h"
64 #include "dnet/blob.h"
65 #include "dnet/rand.h"
66
67 namespace Net {
68
69 /*
70 * Ethernet Stuff
71 */
72 struct EthAddr : protected eth_addr
73 {
74 protected:
75 void parse(const std::string &addr);
76
77 public:
78 /**
79 * @ingroup api_inet
80 * @{
81 */
82 EthAddr();
83 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
84 EthAddr(const eth_addr &ea);
85 EthAddr(const std::string &addr);
86 const EthAddr &operator=(const eth_addr &ea);
87 const EthAddr &operator=(const std::string &addr);
88 /** @} */ // end of api_inet
89
90 /**
91 * @ingroup api_inet
92 */
93 int size() const { return sizeof(eth_addr); }
94
95
96 /**
97 * @ingroup api_inet
98 * @{
99 */
100 const uint8_t *bytes() const { return &data[0]; }
101 uint8_t *bytes() { return &data[0]; }
102 /** @} */ // end of api_inet
103
104 /**
105 * @ingroup api_inet
106 * @{
107 */
108 const uint8_t *addr() const { return &data[0]; }
109 bool unicast() const { return !(data[0] & 0x01); }
110 bool multicast() const { return !unicast() && !broadcast(); }
111 bool broadcast() const
112 {
113 bool isBroadcast = true;
114 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
115 isBroadcast = isBroadcast && data[i] == 0xff;
116 }
117
118 return isBroadcast;
119 }
120 /** @} */ // end of api_inet
121
122 /**
123 * @ingroup api_inet
124 */
125 std::string string() const;
126
127 /**
128 * @ingroup api_inet
129 */
130 operator uint64_t() const
131 {
132 uint64_t reg = 0;
133 reg |= ((uint64_t)data[0]) << 40;
134 reg |= ((uint64_t)data[1]) << 32;
135 reg |= ((uint64_t)data[2]) << 24;
136 reg |= ((uint64_t)data[3]) << 16;
137 reg |= ((uint64_t)data[4]) << 8;
138 reg |= ((uint64_t)data[5]) << 0;
139 return reg;
140 }
141
142 };
143
144 /**
145 * @ingroup api_inet
146 * @{
147 */
148 std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
149 bool operator==(const EthAddr &left, const EthAddr &right);
150 /** @} */ // end of api_inet
151
152 struct EthHdr : public eth_hdr
153 {
154 bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); }
155 uint16_t type() const {
156 if (!isVlan())
157 return ntohs(eth_type);
158 else
159 // L3 type is now 16 bytes into the hdr with 802.1Q
160 // instead of 12. dnet/eth.h only supports 802.1
161 return ntohs(*((uint16_t*)(((uint8_t *)this) + 16)));
162 }
163 uint16_t vlanId() const {
164 if (isVlan())
165 return ntohs(*((uint16_t*)(((uint8_t *)this) + 14)));
166 else
167 return 0x0000;
168 }
169
170 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
171 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
172
173 int size() const {
174 if (!isVlan())
175 return sizeof(eth_hdr);
176 else
177 return (sizeof(eth_hdr)+4);
178 }
179
180 const uint8_t *bytes() const { return (const uint8_t *)this; }
181 const uint8_t *payload() const { return bytes() + size(); }
182 uint8_t *bytes() { return (uint8_t *)this; }
183 uint8_t *payload() { return bytes() + size(); }
184 };
185
186 class EthPtr
187 {
188 protected:
189 friend class IpPtr;
190 friend class Ip6Ptr;
191 EthPacketPtr p;
192
193 public:
194 /**
195 * @ingroup api_inet
196 * @{
197 */
198 EthPtr() {}
199 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
200 /** @} */ // end of api_inet
201
202 EthHdr *operator->() { return (EthHdr *)p->data; }
203 EthHdr &operator*() { return *(EthHdr *)p->data; }
204 operator EthHdr *() { return (EthHdr *)p->data; }
205
206 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
207 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
208 operator const EthHdr *() const { return (const EthHdr *)p->data; }
209
210 /**
211 * @ingroup api_inet
212 */
213 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
214
215 /**
216 * @ingroup api_inet
217 * @{
218 */
219 const EthPacketPtr packet() const { return p; }
220 EthPacketPtr packet() { return p; }
221 bool operator!() const { return !p; }
222 operator bool() const { return (p != nullptr); }
223 int off() const { return 0; }
224 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
225 /** @} */ // end of api_inet
226 };
227
228 /*
229 * IP Stuff
230 */
231 struct IpAddress
232 {
233 protected:
234 uint32_t _ip;
235
236 public:
237 /**
238 * @ingroup api_inet
239 * @{
240 */
241 IpAddress() : _ip(0)
242 {}
243 IpAddress(const uint32_t __ip) : _ip(__ip)
244 {}
245 /** @} */ // end of api_net
246
247 /**
248 * @ingroup api_inet
249 */
250 uint32_t ip() const { return _ip; }
251
252 /**
253 * @ingroup api_inet
254 */
255 std::string string() const;
256 };
257
258 /**
259 * @ingroup api_inet
260 * @{
261 */
262 std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
263 bool operator==(const IpAddress &left, const IpAddress &right);
264 /** @} */ // end of api_inet
265
266 struct IpNetmask : public IpAddress
267 {
268 protected:
269 uint8_t _netmask;
270
271 public:
272 IpNetmask() : IpAddress(), _netmask(0)
273 {}
274 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
275 IpAddress(__ip), _netmask(__netmask)
276 {}
277
278 /**
279 * @ingroup api_inet
280 */
281 uint8_t netmask() const { return _netmask; }
282
283 std::string string() const;
284 };
285
286 /**
287 * @ingroup api_inet
288 * @{
289 */
290 std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
291 bool operator==(const IpNetmask &left, const IpNetmask &right);
292 /** @} */ // end of api_inet
293
294 struct IpWithPort : public IpAddress
295 {
296 protected:
297 uint16_t _port;
298
299 public:
300 IpWithPort() : IpAddress(), _port(0)
301 {}
302 IpWithPort(const uint32_t __ip, const uint16_t __port) :
303 IpAddress(__ip), _port(__port)
304 {}
305
306 /**
307 * @ingroup api_inet
308 */
309 uint8_t port() const { return _port; }
310
311 std::string string() const;
312 };
313
314 /**
315 * @ingroup api_inet
316 * @{
317 */
318 std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
319 bool operator==(const IpWithPort &left, const IpWithPort &right);
320 /** @} */ // end of api_inet
321
322 struct IpOpt;
323 struct IpHdr : public ip_hdr
324 {
325 uint8_t version() const { return ip_v; }
326 uint8_t hlen() const { return ip_hl * 4; }
327 uint8_t tos() const { return ip_tos; }
328 uint16_t len() const { return ntohs(ip_len); }
329 uint16_t id() const { return ntohs(ip_id); }
330 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
331 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
332 uint8_t ttl() const { return ip_ttl; }
333 uint8_t proto() const { return ip_p; }
334 uint16_t sum() const { return ip_sum; }
335 uint32_t src() const { return ntohl(ip_src); }
336 uint32_t dst() const { return ntohl(ip_dst); }
337
338 void sum(uint16_t sum) { ip_sum = sum; }
339 void id(uint16_t _id) { ip_id = htons(_id); }
340 void len(uint16_t _len) { ip_len = htons(_len); }
341
342 bool options(std::vector<const IpOpt *> &vec) const;
343
344 int size() const { return hlen(); }
345 const uint8_t *bytes() const { return (const uint8_t *)this; }
346 const uint8_t *payload() const { return bytes() + size(); }
347 uint8_t *bytes() { return (uint8_t *)this; }
348 uint8_t *payload() { return bytes() + size(); }
349 };
350
351 class IpPtr
352 {
353 protected:
354 friend class TcpPtr;
355 friend class UdpPtr;
356 EthPacketPtr p;
357 bool eth_hdr_vlan;
358
359 void set(const EthPacketPtr &ptr)
360 {
361 p = 0;
362 eth_hdr_vlan = false;
363
364 if (ptr) {
365 EthHdr *eth = (EthHdr *)ptr->data;
366 if (eth->type() == ETH_TYPE_IP)
367 p = ptr;
368 if (eth->isVlan())
369 eth_hdr_vlan = true;
370 }
371 }
372
373 public:
374 /**
375 * @ingroup api_inet
376 * @{
377 */
378 IpPtr() : p(0), eth_hdr_vlan(false) {}
379 IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
380 IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
381 IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
382 /** @} */ // end of api_inet
383
384 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) +
385 ((eth_hdr_vlan) ? 4 : 0)); }
386 IpHdr *operator->() { return get(); }
387 IpHdr &operator*() { return *get(); }
388
389 /**
390 * @ingroup api_inet
391 * @{
392 */
393 const IpHdr *get() const
394 { return (const IpHdr *)(p->data + sizeof(eth_hdr) +
395 ((eth_hdr_vlan) ? 4 : 0)); }
396 const IpHdr *operator->() const { return get(); }
397 const IpHdr &operator*() const { return *get(); }
398 /** @} */ // end of api_inet
399
400 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
401 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
402 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
403
404 /**
405 * @ingroup api_inet
406 * @{
407 */
408 const EthPacketPtr packet() const { return p; }
409 EthPacketPtr packet() { return p; }
410 bool operator!() const { return !p; }
411 operator bool() const { return (p != nullptr); }
412 int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
413 int pstart() const { return (off() + get()->size()); }
414 /** @} */ // end of api_inet
415 };
416
417 /**
418 * @ingroup api_inet
419 */
420 uint16_t cksum(const IpPtr &ptr);
421
422 struct IpOpt : public ip_opt
423 {
424 uint8_t type() const { return opt_type; }
425 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
426 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
427 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
428 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
429
430 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
431 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
432 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
433
434 const uint8_t *data() const { return opt_data.data8; }
435 void sec(ip_opt_data_sec &sec) const;
436 void lsrr(ip_opt_data_rr &rr) const;
437 void ssrr(ip_opt_data_rr &rr) const;
438 void ts(ip_opt_data_ts &ts) const;
439 uint16_t satid() const { return ntohs(opt_data.satid); }
440 uint16_t mtup() const { return ntohs(opt_data.mtu); }
441 uint16_t mtur() const { return ntohs(opt_data.mtu); }
442 void tr(ip_opt_data_tr &tr) const;
443 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
444 void sdb(std::vector<uint32_t> &vec) const;
445 };
446
447 /*
448 * Ip6 Classes
449 */
450 struct Ip6Opt;
451 struct Ip6Hdr : public ip6_hdr
452 {
453 uint8_t version() const { return ip6_vfc; }
454 uint32_t flow() const { return ntohl(ip6_flow); }
455 uint16_t plen() const { return ntohs(ip6_plen); }
456 uint16_t hlen() const { return IP6_HDR_LEN; }
457 uint8_t nxt() const { return ip6_nxt; }
458 uint8_t hlim() const { return ip6_hlim; }
459
460 const uint8_t* src() const { return ip6_src.data; }
461 const uint8_t* dst() const { return ip6_dst.data; }
462
463 int extensionLength() const;
464 const Ip6Opt* getExt(uint8_t ext) const;
465 const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); }
466 const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); }
467 const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); }
468 uint8_t proto() const;
469
470 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
471
472 int size() const { return IP6_HDR_LEN + extensionLength(); }
473 const uint8_t *bytes() const { return (const uint8_t *)this; }
474 const uint8_t *payload() const { return bytes() + IP6_HDR_LEN
475 + extensionLength(); }
476 uint8_t *bytes() { return (uint8_t *)this; }
477 uint8_t *payload() { return bytes() + IP6_HDR_LEN
478 + extensionLength(); }
479 };
480
481 class Ip6Ptr
482 {
483 protected:
484 friend class TcpPtr;
485 friend class UdpPtr;
486 EthPacketPtr p;
487 bool eth_hdr_vlan;
488
489 void set(const EthPacketPtr &ptr)
490 {
491 p = 0;
492 eth_hdr_vlan = false;
493
494 if (ptr) {
495 EthHdr *eth = (EthHdr *)ptr->data;
496 if (eth->type() == ETH_TYPE_IPV6)
497 p = ptr;
498 if (eth->isVlan())
499 eth_hdr_vlan = true;
500 }
501 }
502
503 public:
504 /**
505 * @ingroup api_inet
506 * @{
507 */
508 Ip6Ptr() : p(0), eth_hdr_vlan(false) {}
509 Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
510 Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
511 Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
512 /** @} */ // end of api_inet
513
514 Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr)
515 + ((eth_hdr_vlan) ? 4 : 0)); }
516 Ip6Hdr *operator->() { return get(); }
517 Ip6Hdr &operator*() { return *get(); }
518
519 const Ip6Hdr *get() const
520 { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr)
521 + ((eth_hdr_vlan) ? 4 : 0)); }
522 const Ip6Hdr *operator->() const { return get(); }
523 const Ip6Hdr &operator*() const { return *get(); }
524
525 /**
526 * @ingroup api_inet
527 * @{
528 */
529 const Ip6Ptr &operator=(const EthPacketPtr &ptr)
530 { set(ptr); return *this; }
531 const Ip6Ptr &operator=(const EthPtr &ptr)
532 { set(ptr.p); return *this; }
533 const Ip6Ptr &operator=(const Ip6Ptr &ptr)
534 { p = ptr.p; return *this; }
535 /** @} */ // end of api_inet
536
537 /**
538 * @ingroup api_inet
539 * @{
540 */
541 const EthPacketPtr packet() const { return p; }
542 EthPacketPtr packet() { return p; }
543 bool operator!() const { return !p; }
544 operator bool() const { return (p != nullptr); }
545 int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
546 int pstart() const { return off() + get()->size(); }
547 /** @} */ // end of api_inet
548 };
549
550 // Dnet supplied ipv6 opt header is incomplete and
551 // newer NIC card filters expect a more robust
552 // ipv6 header option declaration.
553 struct ip6_opt_fragment {
554 uint16_t offlg;
555 uint32_t ident;
556 };
557
558 struct ip6_opt_routing_type2 {
559 uint8_t type;
560 uint8_t segleft;
561 uint32_t reserved;
562 ip6_addr_t addr;
563 };
564
565 #define HOME_ADDRESS_OPTION 0xC9
566 struct M5_ATTR_PACKED ip6_opt_dstopts {
567 uint8_t type;
568 uint8_t length;
569 ip6_addr_t addr;
570 };
571
572 struct M5_ATTR_PACKED ip6_opt_hdr
573 {
574 uint8_t ext_nxt;
575 uint8_t ext_len;
576 union {
577 struct ip6_opt_fragment fragment;
578 struct ip6_opt_routing_type2 rtType2;
579 struct ip6_opt_dstopts dstOpts;
580 } ext_data;
581 };
582
583 struct Ip6Opt : public ip6_opt_hdr
584 {
585 uint8_t nxt() const { return ext_nxt; }
586 uint8_t extlen() const { return ext_len; }
587 uint8_t len() const { return extlen() + 8; }
588
589 // Supporting the types of header extensions likely to be encountered:
590 // fragment, routing type 2 and dstopts.
591
592 // Routing type 2
593 uint8_t rtType2Type() const { return ext_data.rtType2.type; }
594 uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; }
595 const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; }
596
597 // Fragment
598 uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); }
599 uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); }
600
601 // Dst Options/Home Address Option
602 uint8_t dstOptType() const { return ext_data.dstOpts.type; }
603 uint8_t dstOptLength() const { return ext_data.dstOpts.length; }
604 const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; }
605 };
606
607
608 /*
609 * TCP Stuff
610 */
611 struct TcpOpt;
612 struct TcpHdr : public tcp_hdr
613 {
614 uint16_t sport() const { return ntohs(th_sport); }
615 uint16_t dport() const { return ntohs(th_dport); }
616 uint32_t seq() const { return ntohl(th_seq); }
617 uint32_t ack() const { return ntohl(th_ack); }
618 uint8_t off() const { return th_off*4; }
619 uint8_t flags() const { return th_flags & 0x3f; }
620 uint16_t win() const { return ntohs(th_win); }
621 uint16_t sum() const { return th_sum; }
622 uint16_t urp() const { return ntohs(th_urp); }
623
624 void sum(uint16_t sum) { th_sum = sum; }
625 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
626 void flags(uint8_t _flags) { th_flags = _flags; }
627
628 bool options(std::vector<const TcpOpt *> &vec) const;
629
630 int size() const { return off(); }
631 const uint8_t *bytes() const { return (const uint8_t *)this; }
632 const uint8_t *payload() const { return bytes() + size(); }
633 uint8_t *bytes() { return (uint8_t *)this; }
634 uint8_t *payload() { return bytes() + size(); }
635 };
636
637 class TcpPtr
638 {
639 protected:
640 EthPacketPtr p;
641 int _off;
642
643 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
644 void set(const IpPtr &ptr)
645 {
646 if (ptr && ptr->proto() == IP_PROTO_TCP)
647 set(ptr.p, ptr.pstart());
648 else
649 set(0, 0);
650 }
651 void set(const Ip6Ptr &ptr)
652 {
653 if (ptr && ptr->proto() == IP_PROTO_TCP)
654 set(ptr.p, ptr.pstart());
655 else
656 set(0, 0);
657 }
658
659 public:
660 /**
661 * @ingroup api_inet
662 * @{
663 */
664 TcpPtr() : p(0), _off(0) {}
665 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
666 TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
667 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
668 /** @} */ // end of api_inet
669
670 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
671 TcpHdr *operator->() { return get(); }
672 TcpHdr &operator*() { return *get(); }
673
674 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
675 const TcpHdr *operator->() const { return get(); }
676 const TcpHdr &operator*() const { return *get(); }
677
678 /**
679 * @ingroup api_inet
680 * @{
681 */
682 const TcpPtr &operator=(const IpPtr &i)
683 { set(i); return *this; }
684 const TcpPtr &operator=(const TcpPtr &t)
685 { set(t.p, t._off); return *this; }
686 /** @} */ // end of api_inet
687
688 /**
689 * @ingroup api_inet
690 * @{
691 */
692 const EthPacketPtr packet() const { return p; }
693 EthPacketPtr packet() { return p; }
694 bool operator!() const { return !p; }
695 operator bool() const { return (p != nullptr); }
696 int off() const { return _off; }
697 int pstart() const { return off() + get()->size(); }
698 /** @} */ // end of api_inet
699 };
700
701 /**
702 * @ingroup api_inet
703 */
704 uint16_t cksum(const TcpPtr &ptr);
705
706 struct TcpOpt : public tcp_opt
707 {
708 uint8_t type() const { return opt_type; }
709 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
710
711 bool isopt(int opt) const { return type() == opt; }
712
713 const uint8_t *data() const { return opt_data.data8; }
714
715 uint16_t mss() const { return ntohs(opt_data.mss); }
716 uint8_t wscale() const { return opt_data.wscale; }
717 uint32_t echo() const { return ntohl(opt_data.echo); }
718 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
719 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
720 uint32_t cc() const { return ntohl(opt_data.cc); }
721 uint8_t cksum() const{ return opt_data.cksum; }
722 const uint8_t *md5() const { return opt_data.md5; }
723
724 int size() const { return len(); }
725 const uint8_t *bytes() const { return (const uint8_t *)this; }
726 const uint8_t *payload() const { return bytes() + size(); }
727 uint8_t *bytes() { return (uint8_t *)this; }
728 uint8_t *payload() { return bytes() + size(); }
729 };
730
731 /*
732 * UDP Stuff
733 */
734 struct UdpHdr : public udp_hdr
735 {
736 uint16_t sport() const { return ntohs(uh_sport); }
737 uint16_t dport() const { return ntohs(uh_dport); }
738 uint16_t len() const { return ntohs(uh_ulen); }
739 uint16_t sum() const { return uh_sum; }
740
741 void sum(uint16_t sum) { uh_sum = sum; }
742 void len(uint16_t _len) { uh_ulen = htons(_len); }
743
744 int size() const { return sizeof(udp_hdr); }
745 const uint8_t *bytes() const { return (const uint8_t *)this; }
746 const uint8_t *payload() const { return bytes() + size(); }
747 uint8_t *bytes() { return (uint8_t *)this; }
748 uint8_t *payload() { return bytes() + size(); }
749 };
750
751 class UdpPtr
752 {
753 protected:
754 EthPacketPtr p;
755 int _off;
756
757 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
758 void set(const IpPtr &ptr)
759 {
760 if (ptr && ptr->proto() == IP_PROTO_UDP)
761 set(ptr.p, ptr.pstart());
762 else
763 set(0, 0);
764 }
765 void set(const Ip6Ptr &ptr)
766 {
767 if (ptr && ptr->proto() == IP_PROTO_UDP)
768 set(ptr.p, ptr.pstart());
769 else
770 set(0, 0);
771 }
772
773 public:
774 /**
775 * @ingroup api_inet
776 */
777 UdpPtr() : p(0), _off(0) {}
778 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
779 UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
780 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
781 /** @} */ // end of api_inet
782
783 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
784 UdpHdr *operator->() { return get(); }
785 UdpHdr &operator*() { return *get(); }
786
787 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
788 const UdpHdr *operator->() const { return get(); }
789 const UdpHdr &operator*() const { return *get(); }
790
791 /**
792 * @ingroup api_inet
793 * @{
794 */
795 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
796 const UdpPtr &operator=(const UdpPtr &t)
797 { set(t.p, t._off); return *this; }
798 /** @} */ // end of api_inet
799
800 /**
801 * @ingroup api_inet
802 * @{
803 */
804 const EthPacketPtr packet() const { return p; }
805 EthPacketPtr packet() { return p; }
806 bool operator!() const { return !p; }
807 operator bool() const { return (p != nullptr); }
808 int off() const { return _off; }
809 int pstart() const { return off() + get()->size(); }
810 /** @} */ // end of api_inet
811 };
812
813 /**
814 * @ingroup api_inet
815 * @{
816 */
817 uint16_t __tu_cksum6(const Ip6Ptr &ip6);
818 uint16_t __tu_cksum(const IpPtr &ip);
819 uint16_t cksum(const UdpPtr &ptr);
820 /** @} */ // end of api_inet
821
822 /**
823 * @ingroup api_inet
824 */
825 int hsplit(const EthPacketPtr &ptr);
826
827 } // namespace Net
828
829 #endif // __BASE_INET_HH__