-
Notifications
You must be signed in to change notification settings - Fork 6
/
hat2.cpp
51 lines (43 loc) · 1.04 KB
/
hat2.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
#include "packet.hpp"
#include <winsock2.h>
int send_msg(SOCKET sock, int ver, u_char *msg, size_t len, int timeout) {
const u_char end[] = {0x64, 0x01, 0x00, 0x00, 0x00};
u_char *STp = 0;
int ret_code = 0, err_code;
u_int idx = 0;
u_char pack[0x96];
fd_set fd;
struct timeval tv;
int nBytesSent;
STp = (u_char *) malloc(len + 5);
memcpy(STp, msg, len);
memcpy(STp + len, end, 5);
memset(pack, 0, 8);
tv.tv_sec = timeout;
tv.tv_usec = 0;
len += 5;
while (idx < len) {
if (len - idx > 0x8E) {
pack[0] = 0x8E;
pack[7] = 0;
} else {
pack[0] = (u_char) (len - idx);
pack[7] = 1;
}
memcpy(pack + 8, STp + idx, pack[0]);
PACKET_XorByKey(pack + 8, pack[0], ver);
nBytesSent = 0;
while (nBytesSent < pack[0] + 8) {
err_code = send(sock, (const char *)pack, pack[0] + 8, 0);
if (err_code == SOCKET_ERROR || !err_code) {
ret_code = -1;
goto ex; /// send error
}
nBytesSent += err_code;
}
idx += pack[0];
}
ex:
if (STp) free(STp);
return ret_code;
}