42 #ifndef __BASE_INET_HH__ 43 #define __BASE_INET_HH__ 56 #include "dnet/addr.h" 58 #include "dnet/icmp.h" 61 #include "dnet/intf.h" 62 #include "dnet/route.h" 64 #include "dnet/blob.h" 65 #include "dnet/rand.h" 81 EthAddr(
const std::string &addr);
85 int size()
const {
return sizeof(eth_addr); }
90 const uint8_t *
addr()
const {
return &
data[0]; }
95 bool isBroadcast =
true;
96 for (
int i = 0;
i < ETH_ADDR_LEN; ++
i) {
97 isBroadcast = isBroadcast &&
data[
i] == 0xff;
103 std::string
string()
const;
105 operator uint64_t()
const 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;
124 bool isVlan()
const {
return (ntohs(eth_type) == ETH_TYPE_8021Q); }
127 return ntohs(eth_type);
131 return ntohs(*((uint16_t*)(((uint8_t *)
this) + 16)));
135 return ntohs(*((uint16_t*)(((uint8_t *)
this) + 14)));
145 return sizeof(eth_hdr);
147 return (
sizeof(eth_hdr)+4);
150 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
152 uint8_t *
bytes() {
return (uint8_t *)
this; }
180 operator bool()
const {
return (p !=
nullptr); }
181 int off()
const {
return 0; }
182 int pstart()
const {
return off() + ((
const EthHdr*)p->data)->size(); }
199 uint32_t
ip()
const {
return _ip; }
201 std::string
string()
const;
215 IpNetmask(
const uint32_t __ip,
const uint8_t __netmask) :
221 std::string
string()
const;
239 uint8_t
port()
const {
return _port; }
241 std::string
string()
const;
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); }
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); }
264 void id(uint16_t _id) { ip_id = htons(_id); }
265 void len(uint16_t _len) { ip_len = htons(_len); }
269 int size()
const {
return hlen(); }
270 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
272 uint8_t *
bytes() {
return (uint8_t *)
this; }
287 eth_hdr_vlan =
false;
291 if (eth->
type() == ETH_TYPE_IP)
299 IpPtr() : p(0), eth_hdr_vlan(false) {}
302 IpPtr(
const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
305 ((eth_hdr_vlan) ? 4 : 0)); }
310 {
return (
const IpHdr *)(p->data +
sizeof(eth_hdr) +
311 ((eth_hdr_vlan) ? 4 : 0)); }
322 operator bool()
const {
return (p !=
nullptr); }
323 int off()
const {
return (
sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
329 struct IpOpt :
public ip_opt
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; }
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); }
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); }
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; }
367 const uint8_t*
src()
const {
return ip6_src.data; }
368 const uint8_t*
dst()
const {
return ip6_dst.data; }
370 int extensionLength()
const;
375 uint8_t proto()
const;
377 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
379 int size()
const {
return IP6_HDR_LEN + extensionLength(); }
380 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
382 + extensionLength(); }
383 uint8_t *
bytes() {
return (uint8_t *)
this; }
385 + extensionLength(); }
399 eth_hdr_vlan =
false;
403 if (eth->
type() == ETH_TYPE_IPV6)
414 Ip6Ptr(
const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
417 + ((eth_hdr_vlan) ? 4 : 0)); }
422 {
return (
const Ip6Hdr *)(p->data +
sizeof(eth_hdr)
423 + ((eth_hdr_vlan) ? 4 : 0)); }
428 {
set(ptr);
return *
this; }
430 {
set(ptr.
p);
return *
this; }
432 { p = ptr.
p;
return *
this; }
437 operator bool()
const {
return (p !=
nullptr); }
438 int off()
const {
return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
457 #define HOME_ADDRESS_OPTION 0xC9 479 uint8_t
len()
const {
return extlen() + 8; }
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); }
517 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
518 void flags(uint8_t _flags) { th_flags = _flags; }
522 int size()
const {
return off(); }
523 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
525 uint8_t *
bytes() {
return (uint8_t *)
this; }
538 if (ptr && ptr->proto() == IP_PROTO_TCP)
539 set(ptr.p, ptr.pstart());
545 if (ptr && ptr->proto() == IP_PROTO_TCP)
546 set(ptr.p, ptr.pstart());
566 {
set(
i);
return *
this; }
568 {
set(t.
p, t.
_off);
return *
this; }
573 operator bool()
const {
return (p !=
nullptr); }
574 int off()
const {
return _off; }
582 uint8_t
type()
const {
return opt_type; }
583 uint8_t
len()
const {
return TCP_OPT_TYPEONLY(
type()) ? 1 : opt_len; }
587 const uint8_t *
data()
const {
return opt_data.data8; }
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; }
599 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
601 uint8_t *
bytes() {
return (uint8_t *)
this; }
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; }
616 void len(uint16_t _len) { uh_ulen = htons(_len); }
618 int size()
const {
return sizeof(udp_hdr); }
619 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
621 uint8_t *
bytes() {
return (uint8_t *)
this; }
634 if (ptr && ptr->proto() == IP_PROTO_UDP)
635 set(ptr.p, ptr.pstart());
641 if (ptr && ptr->proto() == IP_PROTO_UDP)
642 set(ptr.p, ptr.pstart());
663 {
set(t.
p, t.
_off);
return *
this; }
668 operator bool()
const {
return (p !=
nullptr); }
669 int off()
const {
return _off; }
681 #endif // __BASE_INET_HH__ UdpPtr(const UdpPtr &ptr)
const UdpHdr & operator*() const
const uint8_t * bytes() const
bool isCopied(int cpy) const
const uint8_t * bytes() const
TcpPtr(const Ip6Ptr &ptr)
const EthPacketPtr packet() const
const IpPtr & operator=(const EthPtr &ptr)
const Ip6Ptr & operator=(const Ip6Ptr &ptr)
const EthHdr * operator->() const
IpPtr(const EthPacketPtr &ptr)
const uint8_t * rtType2Addr() const
uint8_t rtType2Type() const
const uint8_t * bytes() const
const IpPtr & operator=(const EthPacketPtr &ptr)
const Ip6Ptr & operator=(const EthPacketPtr &ptr)
const uint8_t * addr() const
const UdpPtr & operator=(const IpPtr &i)
uint16_t cksum(const IpPtr &ptr)
Ip6Ptr(const Ip6Ptr &ptr)
int hsplit(const EthPacketPtr &ptr)
Ip6Ptr(const EthPtr &ptr)
void flags(uint8_t _flags)
const uint8_t * data() const
const TcpPtr & operator=(const IpPtr &i)
const EthPacketPtr packet() const
IpAddress(const uint32_t __ip)
const uint8_t * payload() const
std::string string() const
const EthAddr & dst() const
const uint8_t * payload() const
bool operator==(const EthAddr &left, const EthAddr &right)
uint16_t fragmentOfflg() const
const uint8_t * src() const
struct ip6_opt_fragment fragment
const Ip6Opt * dstOptExt() const
IpNetmask(const uint32_t __ip, const uint8_t __netmask)
uint16_t frag_off() const
const EthAddr & src() const
const EthPtr & operator=(const EthPacketPtr &ptr)
const Ip6Opt * fragmentExt() const
const EthPacketPtr packet() const
const uint8_t * bytes() const
Ip6Ptr(const EthPacketPtr &ptr)
const uint8_t * payload() const
const IpHdr & operator*() const
IpWithPort(const uint32_t __ip, const uint16_t __port)
const uint8_t * bytes() const
EthPtr(const EthPacketPtr &ptr)
struct ip6_opt_routing_type2 rtType2
const uint8_t * bytes() const
bool isNumber(int num) const
std::shared_ptr< EthPacketData > EthPacketPtr
const UdpPtr & operator=(const UdpPtr &t)
const uint8_t * bytes() const
uint8_t rtType2SegLft() const
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
uint32_t fragmentIdent() const
struct ip6_opt_dstopts dstOpts
void plen(uint16_t _plen)
uint8_t dstOptType() const
const IpPtr & operator=(const IpPtr &ptr)
const IpHdr * operator->() const
Net::Ip6Opt __attribute__
const Ip6Hdr * operator->() const
ostream & operator<<(ostream &stream, const EthAddr &ea)
const Ip6Opt * rtTypeExt() const
const EthHdr & operator*() const
const uint8_t * dst() const
bool isopt(int opt) const
const uint8_t * payload() const
const EthPacketPtr packet() const
TcpPtr(const TcpPtr &ptr)
const EthAddr & operator=(const eth_addr &ea)
void parse(const std::string &addr)
uint16_t frag_flags() const
uint16_t __tu_cksum6(const Ip6Ptr &ip6)
const Ip6Ptr & operator=(const EthPtr &ptr)
bool isClass(int cls) const
const TcpHdr & operator*() const
const TcpHdr * operator->() const
const uint8_t * md5() const
const uint8_t * dstOptAddr() const
const uint8_t * data() const
uint8_t typeCopied() const
uint8_t dstOptLength() const
const uint8_t * payload() const
const EthPacketPtr packet() const
const TcpPtr & operator=(const TcpPtr &t)
const uint8_t * payload() const
uint16_t __tu_cksum(const IpPtr &ip)
const Ip6Hdr & operator*() const
const UdpHdr * operator->() const
UdpPtr(const Ip6Ptr &ptr)