47 #ifndef __BASE_INET_HH__ 48 #define __BASE_INET_HH__ 61 #include "dnet/addr.h" 63 #include "dnet/icmp.h" 66 #include "dnet/intf.h" 67 #include "dnet/route.h" 69 #include "dnet/blob.h" 70 #include "dnet/rand.h" 86 EthAddr(
const std::string &addr);
90 int size()
const {
return sizeof(eth_addr); }
95 const uint8_t *
addr()
const {
return &
data[0]; }
100 bool isBroadcast =
true;
101 for (
int i = 0;
i < ETH_ADDR_LEN; ++
i) {
102 isBroadcast = isBroadcast &&
data[
i] == 0xff;
108 std::string
string()
const;
110 operator uint64_t()
const 113 reg |= ((uint64_t)
data[0]) << 40;
114 reg |= ((uint64_t)
data[1]) << 32;
115 reg |= ((uint64_t)
data[2]) << 24;
116 reg |= ((uint64_t)
data[3]) << 16;
117 reg |= ((uint64_t)
data[4]) << 8;
118 reg |= ((uint64_t)
data[5]) << 0;
129 bool isVlan()
const {
return (ntohs(eth_type) == ETH_TYPE_8021Q); }
132 return ntohs(eth_type);
136 return ntohs(*((uint16_t*)(((uint8_t *)
this) + 16)));
140 return ntohs(*((uint16_t*)(((uint8_t *)
this) + 14)));
150 return sizeof(eth_hdr);
152 return (
sizeof(eth_hdr)+4);
155 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
157 uint8_t *
bytes() {
return (uint8_t *)
this; }
185 operator bool()
const {
return (p !=
nullptr); }
186 int off()
const {
return 0; }
187 int pstart()
const {
return off() + ((
const EthHdr*)p->data)->size(); }
204 uint32_t
ip()
const {
return _ip; }
206 std::string
string()
const;
220 IpNetmask(
const uint32_t __ip,
const uint8_t __netmask) :
226 std::string
string()
const;
244 uint8_t
port()
const {
return _port; }
246 std::string
string()
const;
256 uint8_t
hlen()
const {
return ip_hl * 4; }
257 uint8_t
tos()
const {
return ip_tos; }
258 uint16_t
len()
const {
return ntohs(ip_len); }
259 uint16_t
id()
const {
return ntohs(ip_id); }
261 uint16_t
frag_off()
const {
return ntohs(ip_off) & 0x1fff; }
262 uint8_t
ttl()
const {
return ip_ttl; }
263 uint8_t
proto()
const {
return ip_p; }
264 uint16_t
sum()
const {
return ip_sum; }
265 uint32_t
src()
const {
return ntohl(ip_src); }
266 uint32_t
dst()
const {
return ntohl(ip_dst); }
269 void id(uint16_t _id) { ip_id = htons(_id); }
270 void len(uint16_t _len) { ip_len = htons(_len); }
274 int size()
const {
return hlen(); }
275 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
277 uint8_t *
bytes() {
return (uint8_t *)
this; }
292 eth_hdr_vlan =
false;
296 if (eth->
type() == ETH_TYPE_IP)
304 IpPtr() : p(0), eth_hdr_vlan(false) {}
307 IpPtr(
const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
310 ((eth_hdr_vlan) ? 4 : 0)); }
315 {
return (
const IpHdr *)(p->data +
sizeof(eth_hdr) +
316 ((eth_hdr_vlan) ? 4 : 0)); }
327 operator bool()
const {
return (p !=
nullptr); }
328 int off()
const {
return (
sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
334 struct IpOpt :
public ip_opt
336 uint8_t
type()
const {
return opt_type; }
337 uint8_t typeNumber()
const {
return IP_OPT_NUMBER(opt_type); }
338 uint8_t typeClass()
const {
return IP_OPT_CLASS(opt_type); }
339 uint8_t
typeCopied()
const {
return IP_OPT_COPIED(opt_type); }
340 uint8_t
len()
const {
return IP_OPT_TYPEONLY(
type()) ? 1 : opt_len; }
342 bool isNumber(
int num)
const {
return typeNumber() == IP_OPT_NUMBER(num); }
343 bool isClass(
int cls)
const {
return typeClass() == IP_OPT_CLASS(cls); }
344 bool isCopied(
int cpy)
const {
return typeCopied() == IP_OPT_COPIED(cpy); }
346 const uint8_t *
data()
const {
return opt_data.data8; }
347 void sec(ip_opt_data_sec &sec)
const;
348 void lsrr(ip_opt_data_rr &
rr)
const;
349 void ssrr(ip_opt_data_rr &rr)
const;
350 void ts(ip_opt_data_ts &
ts)
const;
351 uint16_t
satid()
const {
return ntohs(opt_data.satid); }
352 uint16_t
mtup()
const {
return ntohs(opt_data.mtu); }
353 uint16_t
mtur()
const {
return ntohs(opt_data.mtu); }
354 void tr(ip_opt_data_tr &tr)
const;
355 uint16_t
rtralt()
const {
return ntohs(opt_data.rtralt); }
366 uint32_t
flow()
const {
return ntohl(ip6_flow); }
367 uint16_t
plen()
const {
return ntohs(ip6_plen); }
368 uint16_t
hlen()
const {
return IP6_HDR_LEN; }
369 uint8_t
nxt()
const {
return ip6_nxt; }
370 uint8_t
hlim()
const {
return ip6_hlim; }
372 const uint8_t*
src()
const {
return ip6_src.data; }
373 const uint8_t*
dst()
const {
return ip6_dst.data; }
375 int extensionLength()
const;
380 uint8_t proto()
const;
382 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
384 int size()
const {
return IP6_HDR_LEN + extensionLength(); }
385 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
387 + extensionLength(); }
388 uint8_t *
bytes() {
return (uint8_t *)
this; }
390 + extensionLength(); }
404 eth_hdr_vlan =
false;
408 if (eth->
type() == ETH_TYPE_IPV6)
419 Ip6Ptr(
const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
422 + ((eth_hdr_vlan) ? 4 : 0)); }
427 {
return (
const Ip6Hdr *)(p->data +
sizeof(eth_hdr)
428 + ((eth_hdr_vlan) ? 4 : 0)); }
433 {
set(ptr);
return *
this; }
435 {
set(ptr.
p);
return *
this; }
437 { p = ptr.
p;
return *
this; }
442 operator bool()
const {
return (p !=
nullptr); }
443 int off()
const {
return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
462 #define HOME_ADDRESS_OPTION 0xC9 484 uint8_t
len()
const {
return extlen() + 8; }
511 uint16_t
sport()
const {
return ntohs(th_sport); }
512 uint16_t
dport()
const {
return ntohs(th_dport); }
513 uint32_t
seq()
const {
return ntohl(th_seq); }
514 uint32_t
ack()
const {
return ntohl(th_ack); }
515 uint8_t
off()
const {
return th_off*4; }
516 uint8_t
flags()
const {
return th_flags & 0x3f; }
517 uint16_t
win()
const {
return ntohs(th_win); }
518 uint16_t
sum()
const {
return th_sum; }
519 uint16_t
urp()
const {
return ntohs(th_urp); }
522 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
523 void flags(uint8_t _flags) { th_flags = _flags; }
527 int size()
const {
return off(); }
528 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
530 uint8_t *
bytes() {
return (uint8_t *)
this; }
543 if (ptr && ptr->proto() == IP_PROTO_TCP)
544 set(ptr.p, ptr.pstart());
550 if (ptr && ptr->proto() == IP_PROTO_TCP)
551 set(ptr.p, ptr.pstart());
571 {
set(
i);
return *
this; }
573 {
set(t.
p, t.
_off);
return *
this; }
578 operator bool()
const {
return (p !=
nullptr); }
579 int off()
const {
return _off; }
587 uint8_t
type()
const {
return opt_type; }
588 uint8_t
len()
const {
return TCP_OPT_TYPEONLY(
type()) ? 1 : opt_len; }
592 const uint8_t *
data()
const {
return opt_data.data8; }
594 uint16_t
mss()
const {
return ntohs(opt_data.mss); }
595 uint8_t
wscale()
const {
return opt_data.wscale; }
596 uint32_t
echo()
const {
return ntohl(opt_data.echo); }
597 uint32_t
tsval()
const {
return ntohl(opt_data.timestamp[0]); }
598 uint32_t
tsecr()
const {
return ntohl(opt_data.timestamp[1]); }
599 uint32_t
cc()
const {
return ntohl(opt_data.cc); }
600 uint8_t
cksum()
const{
return opt_data.cksum; }
601 const uint8_t *
md5()
const {
return opt_data.md5; }
604 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
606 uint8_t *
bytes() {
return (uint8_t *)
this; }
615 uint16_t
sport()
const {
return ntohs(uh_sport); }
616 uint16_t
dport()
const {
return ntohs(uh_dport); }
617 uint16_t
len()
const {
return ntohs(uh_ulen); }
618 uint16_t
sum()
const {
return uh_sum; }
621 void len(uint16_t _len) { uh_ulen = htons(_len); }
623 int size()
const {
return sizeof(udp_hdr); }
624 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
626 uint8_t *
bytes() {
return (uint8_t *)
this; }
639 if (ptr && ptr->proto() == IP_PROTO_UDP)
640 set(ptr.p, ptr.pstart());
646 if (ptr && ptr->proto() == IP_PROTO_UDP)
647 set(ptr.p, ptr.pstart());
668 {
set(t.
p, t.
_off);
return *
this; }
673 operator bool()
const {
return (p !=
nullptr); }
674 int off()
const {
return _off; }
686 #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)