-
Notifications
You must be signed in to change notification settings - Fork 1
/
EasySock.hpp
134 lines (109 loc) · 3.15 KB
/
EasySock.hpp
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
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <thread>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
class EasySock {
public:
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
struct hostent* host;
struct sockaddr_in server, client;
locale local;
char buffer[1024];
string response = "";
string sip;
unsigned int port;
int response_length;
EasySock(string in_ip, int in_port) {
int lineCount = 0;
int rowCount = 0;
sip = in_ip;
char ip[50]{};
unsigned int i;
for (i = 0; i < sip.length() + 1; i++) {
ip[i] = sip[i];
}
port = in_port;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed." << endl;
system("pause");
}
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SockAddr.sin_port = htons(port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = inet_addr(ip);
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ip);
server.sin_port = htons(4444);
}
int connect_to_target() {
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0) {
return(1);
}
return(0);
}
void send_to_target(string raw) {
send(Socket, raw.c_str(), strlen(raw.c_str()), 0);
}
int bind_to() {
if (bind(Socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
return(1);
}
return(0);
}
int listen_for_clients(int i) {
listen(Socket, i);
return(0);
}
int accept_clients() {
int c = sizeof(struct sockaddr_in);
Socket = accept(Socket, (struct sockaddr*)&client, &c);
if (Socket == INVALID_SOCKET)
{
return(1);
}
return(0);
}
void free_buffer() {
memset(buffer, 0, sizeof(buffer));
}
string recv_response() {
int timeout = 1000;
setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(int));
while ((response_length = recv(Socket, buffer, 1024, 0)) > 0) {
response += string(buffer);
}
string rresponse = response;
response = "";
response_length = 0;
free_buffer();
return(rresponse);
}
string recv_long_response(int timeout) {
setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(int));
while ((response_length = recv(Socket, buffer, 1024, 0)) > 0) {
response += string(buffer);
}
string rresponse = response;
response = "";
response_length = 0;
free_buffer();
return(rresponse);
}
void clean_up() {
closesocket(Socket);
WSACleanup();
}
};