-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocols.cpp
160 lines (132 loc) · 2.8 KB
/
protocols.cpp
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
#include <stdio.h>
#include <string.h>
#include "protocols.h"
#include <winsock2.h>
// 检查域名中是否含有端口号,有则去掉
void checkAddr(char* addr, int addr_len)
{
char* pCurrent;
int count;
pCurrent = addr;
count = 0;
while(*pCurrent != ':')
{
count++;
*pCurrent++;
}
count = addr_len - count;
memset(pCurrent, '\0', addr_len - count + 1);
}
void fprintHTTPMsg(FILE* file, char* msg,int msg_len)
{
int count = 0;
char* p = msg;
while(true)
{
if(count >= msg_len)
break;
if( (*p >= 32 && *p <=126) || *p == 10 || *p == 13)
fprintf(file, "%c", *p);
else
fprintf(file, ".");
count++;
p++;
}
fprintf(file, "\n");
}
void printBuffer(char* buf,int buflen)
{
int count = 0;
char* p = buf;
printf("\n--------------%d Bytes----------------\n", buflen);
while(true)
{
if(count >= buflen)
break;
if( (*p >= 32 && *p <=126) || *p == 10 || *p == 13)
printf("%c", *p);
else
printf(".");
count++;
p++;
}
printf("\n------------------------------------------------\n");
}
// 从IP:Port中获取IP地址
void getIPAddr(char* addr, int addr_len)
{
char* pCurrent;
int count;
pCurrent = addr;
count = 0;
while(*pCurrent != ':')
{
count++;
*pCurrent++;
}
count = addr_len - count;
memset(pCurrent, '\0', addr_len - count + 1);
}
int getHTTPHost(char* msg, char* host)
{
char* pCurrent;
char* pHostStart;
char* pHostEnd;
int host_len;
bool findFlag;
findFlag = false;
pCurrent = msg;
while(true)
{
// 跳过一行
while(*pCurrent != '\n')
{
// 查找字段名为Host对应的值
if(strncmp("Host: ", pCurrent, 6) == 0)
{
pCurrent += 6;
pHostStart = pCurrent;
while(*pCurrent != '\r')
pCurrent++;
pHostEnd = pCurrent;
findFlag = true;
break;
}
pCurrent++;
}
pCurrent++;
if(findFlag == true)
break;
}
host_len = pHostEnd - pHostStart;
memcpy(host, pHostStart, host_len);
host[host_len] = '\0';
return host_len;
}
// 返回payload长度
int getTCPPayload(char* segment, char* payload, int segment_len)
{
PTCPHDR pTCPHDR;
int tcp_header_len;
int tcp_total_len;
int payload_len;
pTCPHDR = (PTCPHDR)segment;
tcp_header_len = (pTCPHDR->h_len >> 4);
tcp_total_len = segment_len;
payload_len = tcp_total_len - tcp_header_len;
memcpy(payload, segment + tcp_header_len, payload_len);
return payload_len;
}
int getIPPayload(char* packet, char* payload)
{
PIPHDR pIPHDR;
int ip_header_len;
int ip_total_len;
int payload_len;
pIPHDR = (PIPHDR)packet;
ip_header_len = pIPHDR->h_lenver & 0x0f;
ip_total_len = ntohs(pIPHDR->total_len);
payload_len = ip_total_len - ip_header_len;
memcpy(payload, packet + ip_header_len, payload_len);
return payload_len;
}