当前位置:网站首页>Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
2022-07-01 01:00:00 【liulilittle】
No nonsense :
bool TapTap2Socket::RST(My::Tun::ITap* tap, struct My::Net::Native::ip_hdr* iphdr, struct My::Net::Native::tcp_hdr* pkg, int len) { assert(tap && iphdr && pkg);
uint32_t dstAddr = iphdr->dest;
uint16_t dstPort = pkg->dest;
uint32_t srcAddr = iphdr->src;
uint16_t srcPort = pkg->src;
uint32_t seqNo = pkg->seqno;
uint32_t ackNo = pkg->ackno;
uint32_t hdrlen_bytes = tcp_hdr::TCPH_HDRLEN_BYTES(pkg);
uint32_t tcplen = len - hdrlen_bytes;
if (tcp_hdr::TCPH_FLAGS(pkg) & (TcpFlags::TCP_FIN | TcpFlags::TCP_SYN)) {
tcplen++;
}
len = tcp_hdr::TCP_HLEN; // 1 align sizeof(tcp_hdr)
iphdr->src = dstAddr;
pkg->src = dstPort;
iphdr->dest = srcAddr;
pkg->dest = srcPort;
pkg->ackno = seqNo + tcplen;
pkg->seqno = ackNo;
pkg->hdrlen_rsvd_flags = 0;
pkg->urgp = 0;
tcp_hdr::TCPH_HDRLEN_BYTES_SET(pkg, len);
tcp_hdr::TCPH_FLAGS_SET(pkg, TcpFlags::TCP_RST | TcpFlags::TCP_ACK);
return this->OPT(false, tap, iphdr, pkg, len);
} bool TapTap2Socket::OPT(bool lan2wan, My::Tun::ITap* tap, struct My::Net::Native::ip_hdr* iphdr, struct My::Net::Native::tcp_hdr* pkg, int len) { assert(tap && iphdr && pkg);
pkg->chksum = 0;
pkg->chksum = My::Net::Native::inet_chksum_pseudo((unsigned char*)pkg,
(unsigned int)ip_hdr::IP_PROTO_TCP,
(unsigned int)len,
iphdr->src,
iphdr->dest);
if (pkg->chksum == 0) {
pkg->chksum = 0xffff;
}
int iphdr_len = (char*)pkg - (char*)iphdr;
iphdr->chksum = 0;
iphdr->chksum = My::Net::Native::inet_chksum(iphdr, iphdr_len);
if (iphdr->chksum == 0) {
iphdr->chksum = 0xffff;
}
int ippkg_len = ((char*)pkg + len) - (char*)iphdr;
NetworkStatistics::Statistics& ip_stat = this->_statistics.IPv4;
if (lan2wan) {
ip_stat.OutgoingUnicastPacket++;
ip_stat.OutgoingTrafficSize += ippkg_len;
}
else {
ip_stat.IncomingUnicastPacket++;
ip_stat.IncomingTrafficSize += ippkg_len;
}
return tap->Output(iphdr, ippkg_len);
}tcp_hdr.h
#pragma once
#include <My/Net/Native/ip.h>
#include <My/Net/Native/checksum.h>
namespace My {
namespace Net {
namespace Native {
#pragma pack(push, 1)
/*
* typedef struct _tcp_hdr
* {
* unsigned short src_port; // Source port number
* unsigned short dst_port; // Destination port number
* unsigned int seq_no; // Serial number
* unsigned int ack_no; // Confirmation no.
* #if LITTLE_ENDIAN
* unsigned char reserved_1:4; // Retain 6 In the middle 4 Bit head length
* unsigned char thl:4; //tcp Head length
* unsigned char flag:6; //6 Bit mark
* unsigned char reseverd_2:2; // Retain 6 In the middle 2 position
* #else
* unsigned char thl:4; //tcp Head length
* unsigned char reserved_1:4; // Retain 6 In the middle 4 Bit head length
* unsigned char reseverd_2:2; // Retain 6 In the middle 2 position
* unsigned char flag:6; //6 Bit mark
* #endif
* unsigned short wnd_size; //16 Bit window size
* unsigned short chk_sum; //16 position TCP Inspection and
* unsigned short urgt_p; //16 Emergency pointer
* }tcp_hdr;
*/
struct tcp_hdr {
public:
enum TcpFlags {
TCP_FIN = 0x01,
TCP_SYN = 0x02,
TCP_RST = 0x04,
TCP_PSH = 0x08,
TCP_ACK = 0x10,
TCP_UGR = 0x20,
TCP_ECE = 0x40,
TCP_CWR = 0x80,
TCP_FLAGS = 0x3f
};
public:
unsigned short src;
unsigned short dest;
unsigned int seqno;
unsigned int ackno;
unsigned short hdrlen_rsvd_flags;
unsigned short wnd;
unsigned short chksum;
unsigned short urgp; // The application layer cannot appear “URGP/UGR or OPT” The agreement ; This kind of emergency protocol data message directly RST Link to .
public:
inline static unsigned short TCPH_HDRLEN(struct tcp_hdr* phdr) {
return ((unsigned short)(__ntohs((phdr)->hdrlen_rsvd_flags) >> 12));
}
inline static unsigned char TCPH_HDRLEN_BYTES(struct tcp_hdr* phdr) {
return ((unsigned char)(TCPH_HDRLEN(phdr) << 2));
}
inline static unsigned char TCPH_FLAGS(struct tcp_hdr* phdr) {
return ((unsigned char)((__ntohs((phdr)->hdrlen_rsvd_flags) & (unsigned char)TCP_FLAGS)));
}
inline static unsigned short TCPH_HDRLEN_SET(struct tcp_hdr* phdr, int len) {
int u = ((len) << 12) | TCPH_FLAGS(phdr);
return (phdr)->hdrlen_rsvd_flags = __htons((unsigned short)u);
}
inline static unsigned short TCPH_HDRLEN_BYTES_SET(struct tcp_hdr* phdr, int len) {
return TCPH_HDRLEN_SET(phdr, len >> 2);
}
inline static unsigned short PP_HTONS(int x) {
return ((unsigned short)((((x) & (unsigned short)0x00ffU) << 8) | (((x) & (unsigned short)0xff00U) >> 8)));
}
inline static unsigned short TCPH_FLAGS_SET(struct tcp_hdr* phdr, int flags) {
return (phdr)->hdrlen_rsvd_flags = (unsigned short)(((phdr)->hdrlen_rsvd_flags &
PP_HTONS(~(unsigned short)TCP_FLAGS)) | __htons((unsigned short)flags));
}
public:
static struct tcp_hdr* Parse(struct ip_hdr* iphdr, const void* packet, int size);
public:
static const int TCP_HLEN;
};
#pragma pack(pop)
}
}
}边栏推荐
- Deployment of mini version message queue based on redis6.0
- left join左连接匹配数据为NULL时显示指定值
- Oracle table creation and management
- Examples of topological sequences
- 女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
- Confirm() method of window
- 2022-2028 global rotary transmission system industry research and trend analysis report
- 20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
- 20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
- Vulnerability discovery - App application vulnerability probe type utilization and repair
猜你喜欢

Oracle table creation and management

What if the disk of datanode is full?

Redis - understand the master-slave replication mechanism

初识 Flutter 的绘图组件 — CustomPaint

Solving the weird problem that the query conditions affect the value of query fields in MySQL query

Docsify building a personal minimalist knowledge warehouse

Basic knowledge of Embedded Network - introduction of mqtt
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Sword finger offer 18 Delete the node of the linked list

Unit test concept and purpose
随机推荐
第53章 从业务逻辑实现角度整体性理解程序
The question of IBL precomputation is finally solved
Yboj mesh sequence [Lagrange interpolation]
Unit test concept and purpose
2022-2028 global herbal diet tea industry research and trend analysis report
Implementation of date class
2022就要过去一半了,挣钱好难
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
Oracle-表的创建与管理
Examples of topological sequences
The principle and related problems of acid in MySQL
Get to know the drawing component of flutter - custompaint
1009 product of polynomials (25 points) [PTA class A]
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
Get screen height
Redis - how to understand publishing and subscribing
PyTorch安装并使用gpu加速
2022-2028 global capsule shell industry research and trend analysis report