当前位置:网站首页>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)
}
}
}边栏推荐
- Vulnerability discovery - App application vulnerability probe type utilization and repair
- 2022-2028 global herbal diet tea industry research and trend analysis report
- 20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
- C#生成putty格式的ppk文件(支持passphrase)
- CTF tool (1) -- archpr -- including installation / use process
- 【2023联发科提前批笔试题】~ 题目及参考答案
- PyTorch安装并使用gpu加速
- Rhai - rust's embedded scripting engine
- Pytorch auto derivation
- 魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
猜你喜欢

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Implementation of date class

2022-2028 global weight loss ginger tea industry research and trend analysis report

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

Two-stage RO: part 1

CentOS installation starts redis

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

Docsify building a personal minimalist knowledge warehouse

2022-2028 global mobile scanning radiology room industry survey and trend analysis report
随机推荐
Tibetan poem PTA
Confirm() method of window
Get to know the drawing component of flutter - custompaint
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
P4 learning - Basic tunneling
CSDN common complex formula template record
2022就要过去一半了,挣钱好难
Wechat official account development (1) introduction to wechat official account
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Redis - cache penetration, cache breakdown, cache avalanche
A proper job is a good job
2022-2028 global weight loss ginger tea industry research and trend analysis report
Join table query select generation
深度学习的历史
Examples of topological sequences
IBL预计算的疑问终于解开了
Member management applet actual development 07 page Jump
ArrayList分析1-循环、扩容、版本
Redis - sentinel mode
Implementation of OSD on Hisilicon platform (1)