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;
124 std::ostream &
operator<<(std::ostream &stream,
const EthAddr &
ea);
125 bool operator==(
const EthAddr &left,
const EthAddr &right);
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; }
206 std::string
string()
const;
209 std::ostream &
operator<<(std::ostream &stream,
const IpAddress &
ia);
210 bool operator==(
const IpAddress &left,
const IpAddress &right);
220 IpNetmask(
const uint32_t __ip,
const uint8_t __netmask) :
226 std::string
string()
const;
229 std::ostream &
operator<<(std::ostream &stream,
const IpNetmask &in);
230 bool operator==(
const IpNetmask &left,
const IpNetmask &right);
246 std::string
string()
const;
249 std::ostream &
operator<<(std::ostream &stream,
const IpWithPort &iwp);
250 bool operator==(
const IpWithPort &left,
const IpWithPort &right);
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); }
275 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
277 uint8_t *
bytes() {
return (uint8_t *)
this; }
296 if (eth->
type() == ETH_TYPE_IP)
315 {
return (
const IpHdr *)(
p->data +
sizeof(eth_hdr) +
327 operator bool()
const {
return (
p !=
nullptr); }
332 uint16_t
cksum(
const IpPtr &ptr);
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; }
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 const uint32_t *
addext()
const {
return &opt_data.addext[0]; }
356 uint16_t
rtralt()
const {
return ntohs(opt_data.rtralt); }
367 uint32_t
flow()
const {
return ntohl(ip6_flow); }
368 uint16_t
plen()
const {
return ntohs(ip6_plen); }
369 uint16_t
hlen()
const {
return IP6_HDR_LEN; }
370 uint8_t
nxt()
const {
return ip6_nxt; }
371 uint8_t
hlim()
const {
return ip6_hlim; }
373 const uint8_t*
src()
const {
return ip6_src.data; }
374 const uint8_t*
dst()
const {
return ip6_dst.data; }
381 uint8_t
proto()
const;
383 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
386 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
389 uint8_t *
bytes() {
return (uint8_t *)
this; }
409 if (eth->
type() == ETH_TYPE_IPV6)
428 {
return (
const Ip6Hdr *)(
p->data +
sizeof(eth_hdr)
434 {
set(ptr);
return *
this; }
436 {
set(ptr.
p);
return *
this; }
438 {
p = ptr.
p;
return *
this; }
443 operator bool()
const {
return (
p !=
nullptr); }
463 #define HOME_ADDRESS_OPTION 0xC9
512 uint16_t
sport()
const {
return ntohs(th_sport); }
513 uint16_t
dport()
const {
return ntohs(th_dport); }
514 uint32_t
seq()
const {
return ntohl(th_seq); }
515 uint32_t
ack()
const {
return ntohl(th_ack); }
516 uint8_t
off()
const {
return th_off*4; }
517 uint8_t
flags()
const {
return th_flags & 0x3f; }
518 uint16_t
win()
const {
return ntohs(th_win); }
519 uint16_t
sum()
const {
return th_sum; }
520 uint16_t
urp()
const {
return ntohs(th_urp); }
523 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
524 void flags(uint8_t _flags) { th_flags = _flags; }
529 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
531 uint8_t *
bytes() {
return (uint8_t *)
this; }
544 if (ptr && ptr->
proto() == IP_PROTO_TCP)
551 if (ptr && ptr->
proto() == IP_PROTO_TCP)
572 {
set(i);
return *
this; }
579 operator bool()
const {
return (
p !=
nullptr); }
584 uint16_t
cksum(
const TcpPtr &ptr);
588 uint8_t
type()
const {
return opt_type; }
589 uint8_t
len()
const {
return TCP_OPT_TYPEONLY(
type()) ? 1 : opt_len; }
593 const uint8_t *
data()
const {
return opt_data.data8; }
595 uint16_t
mss()
const {
return ntohs(opt_data.mss); }
596 uint8_t
wscale()
const {
return opt_data.wscale; }
597 uint32_t
echo()
const {
return ntohl(opt_data.echo); }
598 uint32_t
tsval()
const {
return ntohl(opt_data.timestamp[0]); }
599 uint32_t
tsecr()
const {
return ntohl(opt_data.timestamp[1]); }
600 uint32_t
cc()
const {
return ntohl(opt_data.cc); }
601 uint8_t
cksum()
const{
return opt_data.cksum; }
602 const uint8_t *
md5()
const {
return opt_data.md5; }
605 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
607 uint8_t *
bytes() {
return (uint8_t *)
this; }
616 uint16_t
sport()
const {
return ntohs(uh_sport); }
617 uint16_t
dport()
const {
return ntohs(uh_dport); }
618 uint16_t
len()
const {
return ntohs(uh_ulen); }
619 uint16_t
sum()
const {
return uh_sum; }
622 void len(uint16_t _len) { uh_ulen = htons(_len); }
624 int size()
const {
return sizeof(udp_hdr); }
625 const uint8_t *
bytes()
const {
return (
const uint8_t *)
this; }
627 uint8_t *
bytes() {
return (uint8_t *)
this; }
640 if (ptr && ptr->
proto() == IP_PROTO_UDP)
647 if (ptr && ptr->
proto() == IP_PROTO_UDP)
674 operator bool()
const {
return (
p !=
nullptr); }
681 uint16_t
cksum(
const UdpPtr &ptr);
687 #endif // __BASE_INET_HH__
std::string string() const
UdpPtr(const UdpPtr &ptr)
std::string string() const
TcpPtr(const Ip6Ptr &ptr)
const IpPtr & operator=(const EthPtr &ptr)
std::string string() const
void set(const IpPtr &ptr)
const Ip6Ptr & operator=(const Ip6Ptr &ptr)
const EthPacketPtr packet() const
bool isopt(int opt) const
uint8_t rtType2Type() const
IpPtr(const EthPacketPtr &ptr)
uint8_t rtType2SegLft() const
void lsrr(ip_opt_data_rr &rr) const
uint8_t dstOptType() const
void ts(ip_opt_data_ts &ts) const
uint32_t fragmentIdent() const
struct ip6_opt_dstopts dstOpts
const IpPtr & operator=(const EthPacketPtr &ptr)
const Ip6Ptr & operator=(const EthPacketPtr &ptr)
const uint8_t * payload() const
const uint8_t * rtType2Addr() const
const UdpPtr & operator=(const IpPtr &i)
uint16_t cksum(const IpPtr &ptr)
bool isNumber(int num) const
Ip6Ptr(const Ip6Ptr &ptr)
int hsplit(const EthPacketPtr &ptr)
struct ip6_opt_routing_type2 rtType2
Ip6Ptr(const EthPtr &ptr)
void flags(uint8_t _flags)
const EthPacketPtr packet() const
uint8_t typeCopied() const
const IpHdr * operator->() const
union Net::ip6_opt_hdr::@29 ext_data
const TcpPtr & operator=(const IpPtr &i)
IpAddress(const uint32_t __ip)
const IpHdr & operator*() const
const uint8_t * dstOptAddr() const
const EthPacketPtr packet() const
void ssrr(ip_opt_data_rr &rr) const
const uint8_t * bytes() const
void set(const Ip6Ptr &ptr)
bool operator==(const EthAddr &left, const EthAddr &right)
void set(const EthPacketPtr &ptr, int offset)
const uint8_t * bytes() const
const uint8_t * payload() const
uint8_t typeClass() const
const Ip6Hdr & operator*() const
const Ip6Opt * fragmentExt() const
const uint8_t * bytes() const
uint16_t frag_off() const
const uint8_t * bytes() const
const uint8_t * bytes() const
IpNetmask(const uint32_t __ip, const uint8_t __netmask)
const EthPtr & operator=(const EthPacketPtr &ptr)
void set(const EthPacketPtr &ptr)
const EthHdr & operator*() const
const EthPacketPtr packet() const
void sec(ip_opt_data_sec &sec) const
const Ip6Opt * dstOptExt() const
const uint8_t * addr() const
const uint8_t * md5() const
Ip6Ptr(const EthPacketPtr &ptr)
void sdb(std::vector< uint32_t > &vec) const
IpWithPort(const uint32_t __ip, const uint16_t __port)
std::string string() const
uint16_t fragmentOfflg() const
bool options(std::vector< const TcpOpt * > &vec) const
EthPtr(const EthPacketPtr &ptr)
const UdpHdr & operator*() const
const uint8_t * data() const
const uint8_t * dst() const
std::shared_ptr< EthPacketData > EthPacketPtr
uint8_t dstOptLength() const
void set(const IpPtr &ptr)
const UdpPtr & operator=(const UdpPtr &t)
uint16_t frag_flags() const
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
const TcpHdr * operator->() const
const uint32_t * addext() const
const UdpHdr * operator->() const
const uint8_t * src() const
void plen(uint16_t _plen)
const IpPtr & operator=(const IpPtr &ptr)
const Ip6Opt * getExt(uint8_t ext) const
Net::Ip6Opt __attribute__
ostream & operator<<(ostream &stream, const EthAddr &ea)
int extensionLength() const
void set(const Ip6Ptr &ptr)
uint8_t typeNumber() const
void set(const EthPacketPtr &ptr)
const uint8_t * data() const
TcpPtr(const TcpPtr &ptr)
const EthAddr & operator=(const eth_addr &ea)
void parse(const std::string &addr)
bool isClass(int cls) const
const EthAddr & src() const
const EthAddr & dst() const
const Ip6Opt * rtTypeExt() const
void set(const EthPacketPtr &ptr, int offset)
uint16_t __tu_cksum6(const Ip6Ptr &ip6)
const uint8_t * payload() const
const Ip6Ptr & operator=(const EthPtr &ptr)
const TcpHdr & operator*() const
const uint8_t * bytes() const
const EthHdr * operator->() const
const uint8_t * payload() const
struct ip6_opt_fragment fragment
const EthPacketPtr packet() const
const uint8_t * payload() const
const TcpPtr & operator=(const TcpPtr &t)
if(it_gpu==gpuTypeMap.end())
bool isCopied(int cpy) const
const Ip6Hdr * operator->() const
uint16_t __tu_cksum(const IpPtr &ip)
const uint8_t * bytes() const
bool options(std::vector< const IpOpt * > &vec) const
void tr(ip_opt_data_tr &tr) const
const uint8_t * payload() const
UdpPtr(const Ip6Ptr &ptr)