-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
277 lines (238 loc) · 6.88 KB
/
main.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//#include <winsock2.h>
//#include <ws2tcpip.h>
//#include <Mswsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include "fonctions.h"
//#include <windows.h>
//#include <tchar.h>
//#include <strsafe.h>
#include <unistd.h>
// Linux Threads
#include <pthread.h>
// Linux Sockets
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
// Errno
#include <errno.h>
#include <err.h>
// Signals
#include <signal.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Kernel32.lib")
unsigned int MAX_THREADS = 8;
unsigned short DEFAULT_PORT = 80;
int count = 0;
int round = 0;
using namespace std;
// CRITICAL_SECTION* zoneCritique = new CRITICAL_SECTION();
pthread_mutex_t criticalZ;
std::ifstream *fRu = new std::ifstream();
std::ofstream *fResultats = new std::ofstream();
// HANDLE writeEvent;
pthread_t *event;
bool process_ip_user(char *ip) {
int coSockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bool res = false;
if (-1 == coSockfd) // Socket creation test
warnx("Error socket creation! %s ", strerror(errno));
int iResult = 0;
char adresseIp[16];
// FIXME dirty
for (int i = 0; i < 16; i++) {
adresseIp[i] = ip[i];
}
struct sockaddr_in recpHostInfo;
recpHostInfo.sin_family = AF_INET;
recpHostInfo.sin_addr.s_addr = inet_addr(adresseIp);
recpHostInfo.sin_port = htons(DEFAULT_PORT);
/*
iResult = connect(coSockfd, (sockaddr * ) & recpHostInfo,
sizeof(recpHostInfo));
*/
/* Establish connection */
if (connect(coSockfd,
(struct sockaddr *) &recpHostInfo,
sizeof(recpHostInfo)) < 0) {
pthread_mutex_lock(&criticalZ);
warnx("Error no connection (%s) ! %s", adresseIp, strerror(errno));
// WSACleanup(); -> auto in Linux
pthread_mutex_unlock(&criticalZ);
} else {
pthread_mutex_lock(&criticalZ);
printf("Connection établie avec %s !\n", adresseIp);
fResultats->write(adresseIp, 16);
pthread_mutex_unlock(&criticalZ);
res = true;
}
close(coSockfd); // close fd socket
return res;
}
//DWORD WINAPI Thread1( LPVOID lpParameter )
void *process_ip(void *data) {
while (count < round) // std::char_traits<wchar_t>::eof()
{
int coSockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == coSockfd) // Socket creation test
warnx("Error socket creation! %s ", strerror(errno));
int iResult = 0;
char adresseIp[16];
pthread_mutex_lock(&criticalZ);
lireFichierIp(adresseIp, 16, fRu);
pthread_mutex_unlock(&criticalZ);
struct sockaddr_in recpHostInfo;
recpHostInfo.sin_family = AF_INET;
recpHostInfo.sin_addr.s_addr = inet_addr(adresseIp);
recpHostInfo.sin_port = htons(DEFAULT_PORT);
/*
iResult = connect(coSockfd, (sockaddr * ) & recpHostInfo,
sizeof(recpHostInfo));
*/
/* Establish connection */
if (connect(coSockfd,
(struct sockaddr *) &recpHostInfo,
sizeof(recpHostInfo)) < 0) {
pthread_mutex_lock(&criticalZ);
warnx("Error no connection (%s) ! %s", adresseIp, strerror(errno));
// WSACleanup(); -> auto in Linux
pthread_mutex_unlock(&criticalZ);
} else {
pthread_mutex_lock(&criticalZ);
printf("Connection établie avec %s !\n", adresseIp);
fResultats->write(adresseIp, 16);
pthread_mutex_unlock(&criticalZ);
}
close(coSockfd); // close fd socket
count += 1;
}
// WSACleanup(); -> auto on Linux
// SetEvent(writeEvent); FIXME Idk what is this !!! FIXME
return NULL;
}
int main(int argc, char *argv[]) {
// Windows : system("TITLE IP_Port_Tester");
// Windows : system("COLOR 0A");
char *user = (char *) malloc(sizeof(char) * 81);
// Menu
cout << "File mode or Intractive mode ? (file or inter)\n";
cin >> user;
int mode_f = 0;
try {
if ("file" == user) {
mode_f = 1;
}
}
catch (const exception e) {
cerr << "Error user input " << user << "\n";
}
// Interactive Mode
if (mode_f == 0) {
cout << "==== INTERACTIVE MODE ====\n";
cout << " Enter PORT ?\n";
cin >> user;
int port_test = 80;
try {
port_test = atoi(user);
}
catch (const exception e) {
cout << " Error input\n";
}
DEFAULT_PORT = port_test;
cout << " Port test : " << DEFAULT_PORT << "\n";
bool res;
while (true) {
cout << " Enter IP to test ?\n >";
cin >> user;
if (user == "quit" || user == "exit") {
break;
}
res = process_ip_user(user);
}
return 0;
}
// File Mode
cout << "==== FILE MODE ====\n";
cout << "Testing all IP addresses from fIPs file.\n";
cout << " Enter PORT ?\n";
cin >> user;
int port_test = 80;
try {
port_test = atoi(user);
}
catch (const exception e) {
cout << " Error input\n";
}
DEFAULT_PORT = port_test;
cout << " Port test : " << DEFAULT_PORT << "\n";
// writeEvent = CreateEvent(NULL, TRUE, FALSE, NULL); FIXME convert to LINUX
/*
if (writeEvent == NULL)
{
printf("Echec de l'event erreur : (%d)\n", GetLastError());
return 1;
}
*/
cout << " How many threads do you want ?\n";
free(user);
user = (char *) malloc(sizeof(char) * 81);
cin >> user;
int max_thr = 80;
try {
max_thr = atoi(user);
}
catch (const exception e) {
cout << " Error input\n";
}
MAX_THREADS = max_thr;
cout << " " << MAX_THREADS << " Thread(s) activated\n";
// cout << " Begin Process (y/n)\n";
free(user);
cout << " How many IPs on test ? (10000 x) if max (30000) " << endl << endl;
cin >> round;
// InitializeCriticalSection(zoneCritique); // pointeur objet
// create mutex attr var
pthread_mutexattr_t mA;
// setup recursive mutex for mutex attr
pthread_mutexattr_settype(&mA, PTHREAD_MUTEX_RECURSIVE_NP);
// use the mutex attr to create the mutex
pthread_mutex_init(&criticalZ, &mA);
// mutex attr can be estroyed
pthread_mutexattr_destroy(&mA);
std::string fileRes = "fResultats"; // + port_test;
fResultats->open(fileRes);
fRu->open("fIPs");
if (fRu->fail())
cout << "Erreur ouverture incorrecte de fIPs !\n";
pthread_t *threadsTable = (pthread_t *) malloc(
MAX_THREADS * sizeof(pthread_t));
for (int i = 0; i < MAX_THREADS; i++)
pthread_create(&threadsTable[i], NULL, process_ip, NULL);
process_ip(NULL); // HERE ALL the MAGIC !!!
int attendreResultat;
// attendreResultat = WaitForSingleObject(writeEvent, INFINITE);
/* FIXME !!!
switch (attendreResultat)
{
case WAIT_OBJECT_0:
break;
default:
printf("Erreur prob : (%d)\n", GetLastError());
return 0;
}
*/
sleep(100000); // FIXME dirty
fRu->close();
fResultats->close();
// CloseHandle(writeEvent); FIXME convert to Linux
// attendreResultat = WaitForSingleObject(writeEvent, INFINITE);
cout << "Process ended well !\n";
pthread_mutex_destroy(&criticalZ);
return 0;
}