当前位置:网站首页>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)
}
}
}边栏推荐
- Simple application example of rhai script engine
- MySQL variables, stored procedures and functions
- What if the disk of datanode is full?
- Pytorch auto derivation
- Longest valid bracket
- 2022-2028 global capsule shell industry research and trend analysis report
- Docsify building a personal minimalist knowledge warehouse
- leetcode 474. Ones and zeroes (medium)
- File reading and writing for rust file system processing - rust Practice Guide
- 2022-2028 global single travel industry research and trend analysis report
猜你喜欢

The principle of journal node

HDU 2488 A Knight's Journey(DFS)

Implementation of OSD on Hisilicon platform (1)

The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design

2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report

PyTorch安装并使用gpu加速

2022-2028 global carbon fiber room scraper system industry research and trend analysis report

魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years

2022-2028 global herbal diet tea industry research and trend analysis report
随机推荐
Tide - rust web framework based on async STD
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
P4学习——Basic Tunneling
2022-2028 global PTFE lined valve industry research and trend analysis report
剑指 Offer 18. 删除链表的节点
2022-2028 global capsule shell industry research and trend analysis report
[untitled]
2022-2028 global public address fire alarm system industry research and trend analysis report
History of deep learning
给按钮的边框和文字设置不同的背景色
Introduction to ES6 promise, new features of ES7 and es8 async and await
Oracle-数据完整性
IBL预计算的疑问终于解开了
C # generates PPK files in putty format (supports passphrase)
C#生成putty格式的ppk文件(支持passphrase)
Cmu15445 (fall 2019) project 1 - buffer pool details
对libco的一点看法
The principle of journal node
Vulnerability discovery - App application vulnerability probe type utilization and repair
Experiment 8 T-SQL, stored procedure