misc: Merged release-staging-v19.0.0.0 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 EthAddr();
79 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
80 EthAddr(const eth_addr &ea);
81 EthAddr(const std::string &addr);
82 const EthAddr &operator=(const eth_addr &ea);
83 const EthAddr &operator=(const std::string &addr);
84
85 int size() const { return sizeof(eth_addr); }
86
87 const uint8_t *bytes() const { return &data[0]; }
88 uint8_t *bytes() { return &data[0]; }
89
90 const uint8_t *addr() const { return &data[0]; }
91 bool unicast() const { return !(data[0] & 0x01); }
92 bool multicast() const { return !unicast() && !broadcast(); }
93 bool broadcast() const
94 {
95 bool isBroadcast = true;
96 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
97 isBroadcast = isBroadcast && data[i] == 0xff;
98 }
99
100 return isBroadcast;
101 }
102
103 std::string string() const;
104
105 operator uint64_t() const
106 {
107 uint64_t reg = 0;
108 reg |= ((uint64_t)data[0]) << 40;
109 reg |= ((uint64_t)data[1]) << 32;
110 reg |= ((uint64_t)data[2]) << 24;
111 reg |= ((uint64_t)data[3]) << 16;
112 reg |= ((uint64_t)data[4]) << 8;
113 reg |= ((uint64_t)data[5]) << 0;
114 return reg;
115 }
116
117 };
118
119 std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
120 bool operator==(const EthAddr &left, const EthAddr &right);
121
122 struct EthHdr : public eth_hdr
123 {
124 bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); }
125 uint16_t type() const {
126 if (!isVlan())
127 return ntohs(eth_type);
128 else
129 // L3 type is now 16 bytes into the hdr with 802.1Q
130 // instead of 12. dnet/eth.h only supports 802.1
131 return ntohs(*((uint16_t*)(((uint8_t *)this) + 16)));
132 }
133 uint16_t vlanId() const {
134 if (isVlan())
135 return ntohs(*((uint16_t*)(((uint8_t *)this) + 14)));
136 else
137 return 0x0000;
138 }
139
140 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
141 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
142
143 int size() const {
144 if (!isVlan())
145 return sizeof(eth_hdr);
146 else
147 return (sizeof(eth_hdr)+4);
148 }
149
150 const uint8_t *bytes() const { return (const uint8_t *)this; }
151 const uint8_t *payload() const { return bytes() + size(); }
152 uint8_t *bytes() { return (uint8_t *)this; }
153 uint8_t *payload() { return bytes() + size(); }
154 };
155
156 class EthPtr
157 {
158 protected:
159 friend class IpPtr;
160 friend class Ip6Ptr;
161 EthPacketPtr p;
162
163 public:
164 EthPtr() {}
165 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
166
167 EthHdr *operator->() { return (EthHdr *)p->data; }
168 EthHdr &operator*() { return *(EthHdr *)p->data; }
169 operator EthHdr *() { return (EthHdr *)p->data; }
170
171 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
172 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
173 operator const EthHdr *() const { return (const EthHdr *)p->data; }
174
175 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
176
177 const EthPacketPtr packet() const { return p; }
178 EthPacketPtr packet() { return p; }
179 bool operator!() const { return !p; }
180 operator bool() const { return (p != nullptr); }
181 int off() const { return 0; }
182 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
183 };
184
185 /*
186 * IP Stuff
187 */
188 struct IpAddress
189 {
190 protected:
191 uint32_t _ip;
192
193 public:
194 IpAddress() : _ip(0)
195 {}
196 IpAddress(const uint32_t __ip) : _ip(__ip)
197 {}
198
199 uint32_t ip() const { return _ip; }
200
201 std::string string() const;
202 };
203
204 std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
205 bool operator==(const IpAddress &left, const IpAddress &right);
206
207 struct IpNetmask : public IpAddress
208 {
209 protected:
210 uint8_t _netmask;
211
212 public:
213 IpNetmask() : IpAddress(), _netmask(0)
214 {}
215 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
216 IpAddress(__ip), _netmask(__netmask)
217 {}
218
219 uint8_t netmask() const { return _netmask; }
220
221 std::string string() const;
222 };
223
224 std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
225 bool operator==(const IpNetmask &left, const IpNetmask &right);
226
227 struct IpWithPort : public IpAddress
228 {
229 protected:
230 uint16_t _port;
231
232 public:
233 IpWithPort() : IpAddress(), _port(0)
234 {}
235 IpWithPort(const uint32_t __ip, const uint16_t __port) :
236 IpAddress(__ip), _port(__port)
237 {}
238
239 uint8_t port() const { return _port; }
240
241 std::string string() const;
242 };
243
244 std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
245 bool operator==(const IpWithPort &left, const IpWithPort &right);
246
247 struct IpOpt;
248 struct IpHdr : public ip_hdr
249 {
250 uint8_t version() const { return ip_v; }
251 uint8_t hlen() const { return ip_hl * 4; }
252 uint8_t tos() const { return ip_tos; }
253 uint16_t len() const { return ntohs(ip_len); }
254 uint16_t id() const { return ntohs(ip_id); }
255 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
256 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
257 uint8_t ttl() const { return ip_ttl; }
258 uint8_t proto() const { return ip_p; }
259 uint16_t sum() const { return ip_sum; }
260 uint32_t src() const { return ntohl(ip_src); }
261 uint32_t dst() const { return ntohl(ip_dst); }
262
263 void sum(uint16_t sum) { ip_sum = sum; }
264 void id(uint16_t _id) { ip_id = htons(_id); }
265 void len(uint16_t _len) { ip_len = htons(_len); }
266
267 bool options(std::vector<const IpOpt *> &vec) const;
268
269 int size() const { return hlen(); }
270 const uint8_t *bytes() const { return (const uint8_t *)this; }
271 const uint8_t *payload() const { return bytes() + size(); }
272 uint8_t *bytes() { return (uint8_t *)this; }
273 uint8_t *payload() { return bytes() + size(); }
274 };
275
276 class IpPtr
277 {
278 protected:
279 friend class TcpPtr;
280 friend class UdpPtr;
281 EthPacketPtr p;
282 bool eth_hdr_vlan;
283
284 void set(const EthPacketPtr &ptr)
285 {
286 p = 0;
287 eth_hdr_vlan = false;
288
289 if (ptr) {
290 EthHdr *eth = (EthHdr *)ptr->data;
291 if (eth->type() == ETH_TYPE_IP)
292 p = ptr;
293 if (eth->isVlan())
294 eth_hdr_vlan = true;
295 }
296 }
297
298 public:
299 IpPtr() : p(0), eth_hdr_vlan(false) {}
300 IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
301 IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
302 IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
303
304 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) +
305 ((eth_hdr_vlan) ? 4 : 0)); }
306 IpHdr *operator->() { return get(); }
307 IpHdr &operator*() { return *get(); }
308
309 const IpHdr *get() const
310 { return (const IpHdr *)(p->data + sizeof(eth_hdr) +
311 ((eth_hdr_vlan) ? 4 : 0)); }
312 const IpHdr *operator->() const { return get(); }
313 const IpHdr &operator*() const { return *get(); }
314
315 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
316 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
317 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
318
319 const EthPacketPtr packet() const { return p; }
320 EthPacketPtr packet() { return p; }
321 bool operator!() const { return !p; }
322 operator bool() const { return (p != nullptr); }
323 int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
324 int pstart() const { return (off() + get()->size()); }
325 };
326
327 uint16_t cksum(const IpPtr &ptr);
328
329 struct IpOpt : public ip_opt
330 {
331 uint8_t type() const { return opt_type; }
332 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
333 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
334 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
335 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
336
337 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
338 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
339 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
340
341 const uint8_t *data() const { return opt_data.data8; }
342 void sec(ip_opt_data_sec &sec) const;
343 void lsrr(ip_opt_data_rr &rr) const;
344 void ssrr(ip_opt_data_rr &rr) const;
345 void ts(ip_opt_data_ts &ts) const;
346 uint16_t satid() const { return ntohs(opt_data.satid); }
347 uint16_t mtup() const { return ntohs(opt_data.mtu); }
348 uint16_t mtur() const { return ntohs(opt_data.mtu); }
349 void tr(ip_opt_data_tr &tr) const;
350 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
351 void sdb(std::vector<uint32_t> &vec) const;
352 };
353
354 /*
355 * Ip6 Classes
356 */
357 struct Ip6Opt;
358 struct Ip6Hdr : public ip6_hdr
359 {
360 uint8_t version() const { return ip6_vfc; }
361 uint32_t flow() const { return ntohl(ip6_flow); }
362 uint16_t plen() const { return ntohs(ip6_plen); }
363 uint16_t hlen() const { return IP6_HDR_LEN; }
364 uint8_t nxt() const { return ip6_nxt; }
365 uint8_t hlim() const { return ip6_hlim; }
366
367 const uint8_t* src() const { return ip6_src.data; }
368 const uint8_t* dst() const { return ip6_dst.data; }
369
370 int extensionLength() const;
371 const Ip6Opt* getExt(uint8_t ext) const;
372 const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); }
373 const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); }
374 const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); }
375 uint8_t proto() const;
376
377 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
378
379 int size() const { return IP6_HDR_LEN + extensionLength(); }
380 const uint8_t *bytes() const { return (const uint8_t *)this; }
381 const uint8_t *payload() const { return bytes() + IP6_HDR_LEN
382 + extensionLength(); }
383 uint8_t *bytes() { return (uint8_t *)this; }
384 uint8_t *payload() { return bytes() + IP6_HDR_LEN
385 + extensionLength(); }
386 };
387
388 class Ip6Ptr
389 {
390 protected:
391 friend class TcpPtr;
392 friend class UdpPtr;
393 EthPacketPtr p;
394 bool eth_hdr_vlan;
395
396 void set(const EthPacketPtr &ptr)
397 {
398 p = 0;
399 eth_hdr_vlan = false;
400
401 if (ptr) {
402 EthHdr *eth = (EthHdr *)ptr->data;
403 if (eth->type() == ETH_TYPE_IPV6)
404 p = ptr;
405 if (eth->isVlan())
406 eth_hdr_vlan = true;
407 }
408 }
409
410 public:
411 Ip6Ptr() : p(0), eth_hdr_vlan(false) {}
412 Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
413 Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
414 Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
415
416 Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr)
417 + ((eth_hdr_vlan) ? 4 : 0)); }
418 Ip6Hdr *operator->() { return get(); }
419 Ip6Hdr &operator*() { return *get(); }
420
421 const Ip6Hdr *get() const
422 { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr)
423 + ((eth_hdr_vlan) ? 4 : 0)); }
424 const Ip6Hdr *operator->() const { return get(); }
425 const Ip6Hdr &operator*() const { return *get(); }
426
427 const Ip6Ptr &operator=(const EthPacketPtr &ptr)
428 { set(ptr); return *this; }
429 const Ip6Ptr &operator=(const EthPtr &ptr)
430 { set(ptr.p); return *this; }
431 const Ip6Ptr &operator=(const Ip6Ptr &ptr)
432 { p = ptr.p; return *this; }
433
434 const EthPacketPtr packet() const { return p; }
435 EthPacketPtr packet() { return p; }
436 bool operator!() const { return !p; }
437 operator bool() const { return (p != nullptr); }
438 int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
439 int pstart() const { return off() + get()->size(); }
440 };
441
442 // Dnet supplied ipv6 opt header is incomplete and
443 // newer NIC card filters expect a more robust
444 // ipv6 header option declaration.
445 struct ip6_opt_fragment {
446 uint16_t offlg;
447 uint32_t ident;
448 };
449
450 struct ip6_opt_routing_type2 {
451 uint8_t type;
452 uint8_t segleft;
453 uint32_t reserved;
454 ip6_addr_t addr;
455 };
456
457 #define HOME_ADDRESS_OPTION 0xC9
458 struct ip6_opt_dstopts {
459 uint8_t type;
460 uint8_t length;
461 ip6_addr_t addr;
462 } __attribute__((packed));
463
464 struct ip6_opt_hdr
465 {
466 uint8_t ext_nxt;
467 uint8_t ext_len;
468 union {
469 struct ip6_opt_fragment fragment;
470 struct ip6_opt_routing_type2 rtType2;
471 struct ip6_opt_dstopts dstOpts;
472 } ext_data;
473 } __attribute__((packed));
474
475 struct Ip6Opt : public ip6_opt_hdr
476 {
477 uint8_t nxt() const { return ext_nxt; }
478 uint8_t extlen() const { return ext_len; }
479 uint8_t len() const { return extlen() + 8; }
480
481 // Supporting the types of header extensions likely to be encountered:
482 // fragment, routing type 2 and dstopts.
483
484 // Routing type 2
485 uint8_t rtType2Type() const { return ext_data.rtType2.type; }
486 uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; }
487 const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; }
488
489 // Fragment
490 uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); }
491 uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); }
492
493 // Dst Options/Home Address Option
494 uint8_t dstOptType() const { return ext_data.dstOpts.type; }
495 uint8_t dstOptLength() const { return ext_data.dstOpts.length; }
496 const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; }
497 };
498
499
500 /*
501 * TCP Stuff
502 */
503 struct TcpOpt;
504 struct TcpHdr : public tcp_hdr
505 {
506 uint16_t sport() const { return ntohs(th_sport); }
507 uint16_t dport() const { return ntohs(th_dport); }
508 uint32_t seq() const { return ntohl(th_seq); }
509 uint32_t ack() const { return ntohl(th_ack); }
510 uint8_t off() const { return th_off*4; }
511 uint8_t flags() const { return th_flags & 0x3f; }
512 uint16_t win() const { return ntohs(th_win); }
513 uint16_t sum() const { return th_sum; }
514 uint16_t urp() const { return ntohs(th_urp); }
515
516 void sum(uint16_t sum) { th_sum = sum; }
517 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
518 void flags(uint8_t _flags) { th_flags = _flags; }
519
520 bool options(std::vector<const TcpOpt *> &vec) const;
521
522 int size() const { return off(); }
523 const uint8_t *bytes() const { return (const uint8_t *)this; }
524 const uint8_t *payload() const { return bytes() + size(); }
525 uint8_t *bytes() { return (uint8_t *)this; }
526 uint8_t *payload() { return bytes() + size(); }
527 };
528
529 class TcpPtr
530 {
531 protected:
532 EthPacketPtr p;
533 int _off;
534
535 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
536 void set(const IpPtr &ptr)
537 {
538 if (ptr && ptr->proto() == IP_PROTO_TCP)
539 set(ptr.p, ptr.pstart());
540 else
541 set(0, 0);
542 }
543 void set(const Ip6Ptr &ptr)
544 {
545 if (ptr && ptr->proto() == IP_PROTO_TCP)
546 set(ptr.p, ptr.pstart());
547 else
548 set(0, 0);
549 }
550
551 public:
552 TcpPtr() : p(0), _off(0) {}
553 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
554 TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
555 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
556
557 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
558 TcpHdr *operator->() { return get(); }
559 TcpHdr &operator*() { return *get(); }
560
561 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
562 const TcpHdr *operator->() const { return get(); }
563 const TcpHdr &operator*() const { return *get(); }
564
565 const TcpPtr &operator=(const IpPtr &i)
566 { set(i); return *this; }
567 const TcpPtr &operator=(const TcpPtr &t)
568 { set(t.p, t._off); return *this; }
569
570 const EthPacketPtr packet() const { return p; }
571 EthPacketPtr packet() { return p; }
572 bool operator!() const { return !p; }
573 operator bool() const { return (p != nullptr); }
574 int off() const { return _off; }
575 int pstart() const { return off() + get()->size(); }
576 };
577
578 uint16_t cksum(const TcpPtr &ptr);
579
580 struct TcpOpt : public tcp_opt
581 {
582 uint8_t type() const { return opt_type; }
583 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
584
585 bool isopt(int opt) const { return type() == opt; }
586
587 const uint8_t *data() const { return opt_data.data8; }
588
589 uint16_t mss() const { return ntohs(opt_data.mss); }
590 uint8_t wscale() const { return opt_data.wscale; }
591 uint32_t echo() const { return ntohl(opt_data.echo); }
592 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
593 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
594 uint32_t cc() const { return ntohl(opt_data.cc); }
595 uint8_t cksum() const{ return opt_data.cksum; }
596 const uint8_t *md5() const { return opt_data.md5; }
597
598 int size() const { return len(); }
599 const uint8_t *bytes() const { return (const uint8_t *)this; }
600 const uint8_t *payload() const { return bytes() + size(); }
601 uint8_t *bytes() { return (uint8_t *)this; }
602 uint8_t *payload() { return bytes() + size(); }
603 };
604
605 /*
606 * UDP Stuff
607 */
608 struct UdpHdr : public udp_hdr
609 {
610 uint16_t sport() const { return ntohs(uh_sport); }
611 uint16_t dport() const { return ntohs(uh_dport); }
612 uint16_t len() const { return ntohs(uh_ulen); }
613 uint16_t sum() const { return uh_sum; }
614
615 void sum(uint16_t sum) { uh_sum = sum; }
616 void len(uint16_t _len) { uh_ulen = htons(_len); }
617
618 int size() const { return sizeof(udp_hdr); }
619 const uint8_t *bytes() const { return (const uint8_t *)this; }
620 const uint8_t *payload() const { return bytes() + size(); }
621 uint8_t *bytes() { return (uint8_t *)this; }
622 uint8_t *payload() { return bytes() + size(); }
623 };
624
625 class UdpPtr
626 {
627 protected:
628 EthPacketPtr p;
629 int _off;
630
631 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
632 void set(const IpPtr &ptr)
633 {
634 if (ptr && ptr->proto() == IP_PROTO_UDP)
635 set(ptr.p, ptr.pstart());
636 else
637 set(0, 0);
638 }
639 void set(const Ip6Ptr &ptr)
640 {
641 if (ptr && ptr->proto() == IP_PROTO_UDP)
642 set(ptr.p, ptr.pstart());
643 else
644 set(0, 0);
645 }
646
647 public:
648 UdpPtr() : p(0), _off(0) {}
649 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
650 UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
651 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
652
653 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
654 UdpHdr *operator->() { return get(); }
655 UdpHdr &operator*() { return *get(); }
656
657 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
658 const UdpHdr *operator->() const { return get(); }
659 const UdpHdr &operator*() const { return *get(); }
660
661 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
662 const UdpPtr &operator=(const UdpPtr &t)
663 { set(t.p, t._off); return *this; }
664
665 const EthPacketPtr packet() const { return p; }
666 EthPacketPtr packet() { return p; }
667 bool operator!() const { return !p; }
668 operator bool() const { return (p != nullptr); }
669 int off() const { return _off; }
670 int pstart() const { return off() + get()->size(); }
671 };
672
673 uint16_t __tu_cksum6(const Ip6Ptr &ip6);
674 uint16_t __tu_cksum(const IpPtr &ip);
675 uint16_t cksum(const UdpPtr &ptr);
676
677 int hsplit(const EthPacketPtr &ptr);
678
679 } // namespace Net
680
681 #endif // __BASE_INET_HH__