-
Notifications
You must be signed in to change notification settings - Fork 16
/
tcpipprotocol.h
207 lines (185 loc) · 5.69 KB
/
tcpipprotocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ---------------------------------------------
// TCP/IP数据包操作类
// ---------------------------------------------
#ifndef TCPIPPROTOCOL_H
#define TCPIPPROTOCOL_H
#include "tcpipcommon.h"
#include <QString>
namespace TcpIpProtocol {
class YEthernetPacket;
class YArpPacket;
class YIPPacket;
class YTcpPacket;
class YUdpPacket;
class YHttPPacket;
}
//***********************************************************************************
//YEthernetPacket Class
//***********************************************************************************
class YEthernetPacket
{
public:
YEthernetPacket();
public:
/** 单独设置 **/
// setSrcMac(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF);
void setSrcMac(u_char m0,u_char m1,u_char m2,u_char m3,u_char m4,u_char m5);
// setDestMac(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF);
void setDestMac(u_char m0,u_char m1,u_char m2,u_char m3,u_char m4,u_char m5);
// setType(0x0806);
void setType(u_short type);
// void getData(&dataStruct);
u_char* getData();
QString getEtherSrcMacAdd(void);
QString getEtherDestMacAdd(void);
u_short getEtherNetType();
void setData(const u_char *data);
/** 一次设置 **/
void fillEthernetHeader(u_char* srcMac, u_char* detMac, u_short type);
private:
u_char data[ETHERNET_HEAD_LENGTH];
};
//***********************************************************************************
//YIPHeaderPacket Class
//***********************************************************************************
class YIPHeaderPacket
{
public:
YIPHeaderPacket();
void setData(const u_char *pktData);
// TCP/UDP/ICMP
QString getProtocolType(void);
QString getSourceIpAddStr(void);
QString getDestIpAddStr(void);
private:
u_char data[IP_HEAD_LENGTH];
};
//***********************************************************************************
//YICMPHeaderPacket Class Ping实现,无需data,只需要ICMP头即可,关键是校验和生成
//***********************************************************************************
class YICMPHeaderPacket
{
public:
YICMPHeaderPacket();
void setType(u_char type);
void setCode(u_char code);
void setIdent(u_short ident);
void setSeq(u_short seq);
// 不包括校验和
void fillICMPHeader(u_char type,u_char code,u_short ident,u_short seq);
// 计算校验和
u_short calcCheckSum(u_short *icmpHeader,int headerLen);
//
private:
u_char data[ICMP_HEAD_LENGTH];
};
//***********************************************************************************
//YArpPacket Class
//***********************************************************************************
class YArpPacket
{
public:
YArpPacket();
public:
// setHardwareType(ARP_HARDWARE);
void setHardwareType(u_short type = 0x0001);
//
void setProtocolType(u_short type = 0x0800);
//
void setHardwareAddLen(u_char len = 0x06);
//
void setProtocolAddLen(u_char len = 0x04);
//
void setOperationField(u_short type);
//
void setSourceMacAdd(u_char m0,u_char m1,u_char m2,u_char m3,u_char m4,u_char m5);
//
void setSourIpAdd(u_int);
//
void setSourIpAdd(const char *ipStr);
//
void setDestMacAdd(u_char m0,u_char m1,u_char m2,u_char m3,u_char m4,u_char m5);\
//
void setDestIpAdd(u_int);
//
void setDestIpAdd(const char *ipStr);
/** fillArpPacket 硬件len=6,协议len=4已默认 **/
void fillArpPacket(u_short hdType,u_short proType,u_short opFilt,
u_char *srcMac,char *srcIp,u_char *destMac,char *destIp);
// 将EthernetHeader 和 ArpHeader 整合成ArpPacket data
void setData();
void setData(const u_char *data);
/** 获取数据 **/
void clearData();
//
u_char * getData();
//
u_short getEtherNetType();
//
u_short getHardwareType();
//
u_short getProtocolType();
//
u_short getOperationField();
//
QString getSourceMacAdd();
//
QString getSourceIpAddStr();
//
u_long getSourceIpAdd();
//
QString getDestIpAddStr();
//
u_long getDestIpAdd();
//
QString getDestMacAdd();
//
YEthernetPacket &getEthernetPacket();
private:
YEthernetPacket etherPacket;
ArpPacket arpPacket;
ArpHeader arpBody;
u_char data[ARP_PACKET_LENGTH];
};
//***********************************************************************************
//YIPPacket Class
//***********************************************************************************
class YIPPacket
{
};
//***********************************************************************************
//YTcpPacket Class
//***********************************************************************************
#define HOST_PORT 0xc522 // 本机端口(随便搞个):
class YTcpPacket
{
public:
YTcpPacket();
YTcpPacket(u_char *srcMac,u_char *destMac,char *srcIp,char *destIp,u_short port);
private:
YEthernetPacket *ethernetHeader;
YIPHeaderPacket *ipHeader;
u_char data[TCP_PACKET_LENGTH];
private:
u_short calcCheckNum(u_short* buffer, int size);
public:
void setData(const u_char *pktData);
u_char* getData(void);
u_char getFlag();
u_short getSrcPort();
QString getDestIpAddStr(void);
void setScanPort(u_short port);
};
//***********************************************************************************
//YUdpPacket Class
//***********************************************************************************
class YUdpPacket
{
};
//***********************************************************************************
//YHttPPacket Class
//***********************************************************************************
class YHttPPacket
{
};
#endif // TCPIPPROTOCOL_H