-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.cpp
276 lines (223 loc) · 6.25 KB
/
ping.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
/***************************************************************************
* Copyright (C) 2006 by Dmitriy Gorbenko *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include "sys/wait.h"
#include "define.h"
#include "str.h"
#include "message.h"
#include "config.h"
#include "tools.h"
#include "ping.h"
using namespace std;
extern char * return_message;
extern struct config_data_def config_data;
unsigned int ping_manage(char *cmd)
{
unsigned int result = FALSE;
char * act = NULL;
act = get_word_by_number(cmd, 2);
if (!act)
return FALSE;
if (strcmp(act, "START") == 0) {
if (start_ping(cmd) == FALSE) {
result = FALSE;
goto ping_end;
}
result = TRUE;
}
if (strcmp(act, "STAT") == 0) {
if (get_stat_ping(cmd) == FALSE) {
result = FALSE;
goto ping_end;
}
result = TRUE;
}
ping_end:
if (act) safe_free(&act);
return result;
}
unsigned int start_ping(char * cmd)
{
unsigned int result = FALSE;
unsigned int i = 0;
int fork_res;
char * report_file = NULL;
char * ip = NULL;
char * count = NULL;
char * interval = NULL;
char * packet_size = NULL;
char * tmp = NULL;
ip = get_word_by_number(cmd, 3);
if (!ip) {
result = FALSE;
goto start_ping_end;
}
count = get_word_by_number(cmd, 4);
if (!count) {
result = FALSE;
goto start_ping_end;
}
if (atoi(count) < 1) {
return_message = strdup("NEGATIVE COUNT");
message("NEGATIVE COUNT: %s\n", count);
result = FALSE;
goto start_ping_end;
}
interval = get_word_by_number(cmd, 5);
if (!interval) {
result = FALSE;
goto start_ping_end;
}
for(i = 0; i < strlen(interval); i++) {
if (!(48 <= interval[i] && interval[i] <= 57)) {
return_message = strdup("WRONG INTERVAL");
message("WRONG INTERVAL: %s\n", interval);
result = FALSE;
goto start_ping_end;
}
}
if (atoi(interval) < 0) {
return_message = strdup("NEGATIVE INETRVAL");
message("NEGATIVE INTERVAL: %s\n", interval);
result = FALSE;
goto start_ping_end;
}
packet_size = get_word_by_number(cmd, 6);
if (!packet_size) {
result = FALSE;
goto start_ping_end;
}
for(i = 0; i < strlen(packet_size); i++) {
if (!(48 <= packet_size[i] && packet_size[i] <= 57)) {
return_message = strdup("WRONG PACKET_SIZE");
message("WRONG PACKET_SIZE: %s\n", packet_size);
result = FALSE;
goto start_ping_end;
}
}
if (atoi(packet_size) < 0) {
return_message = strdup("NEGATIVE PACKET_SIZE");
message("NEGATIVE PACKET_SIZE: %s\n", packet_size);
result = FALSE;
goto start_ping_end;
}
tmp = create_unique_id();
report_file = merge_strings(2, "/tmp/", tmp);
safe_free(&tmp);
return_message = merge_strings(2, "ID: ", report_file);
if (ROOT_COMPILE || 1) {
fork_res = vfork();
if (fork_res == 0) {
// After fork here child thread
char *args[7] = {PING_START, count, interval, packet_size, ip, report_file, NULL};
execve(PING_START, args, NULL);
_exit(EXIT_SUCCESS);
}
fork_res = vfork();
if (fork_res == 0) {
// After fork here child thread
char *args[3] = {PING_STOP, report_file, NULL};
execve(PING_STOP, args, NULL);
_exit(EXIT_SUCCESS);
}
}
result = TRUE;
start_ping_end:
if (ip) safe_free(&ip);
if (count) safe_free(&count);
if (interval) safe_free(&interval);
if (packet_size) safe_free(&packet_size);
if (report_file) safe_free(&report_file);
return result;
}
unsigned int get_stat_ping(char * cmd)
{
unsigned int result = FALSE;
char * buff = NULL;
FILE * f;
char * lines_to_skip = NULL;
char * file_name = NULL;
unsigned int i = 0, k;
lines_to_skip = get_word_by_number(cmd, 3);
if (!lines_to_skip) {
result = FALSE;
goto get_stat_ping_end;
}
for(i = 0; i < strlen(lines_to_skip); i++) {
if (!(48 <= lines_to_skip[i] && lines_to_skip[i] <= 57)) {
return_message = strdup("WRONG LINES_TO_SKIP");
message("WRONG LINES_TO_SKIP: %s\n", lines_to_skip);
result = FALSE;
goto get_stat_ping_end;
}
}
if (atoi(lines_to_skip) < 0) {
return_message = strdup("NEGATIVE LINES_TO_SKIP");
message("NEGATIVE LINES_TO_SKIP: %s\n", lines_to_skip);
result = FALSE;
goto get_stat_ping_end;
}
file_name = get_word_by_number(cmd, 4);
if (!file_name) {
result = FALSE;
goto get_stat_ping_end;
}
f = fopen(file_name, "r");
if (!f) {
result = FALSE;
goto get_stat_ping_end;
}
k = atoi(lines_to_skip);
i = 0;
while(!feof(f)) {
buff = read_string(f);
if (!buff)
break;
if (i <= k) {
safe_free(&buff);
i++;
continue;
}
if (return_message) {
return_message = sts(&return_message, "\n");
}
return_message = sts(&return_message, buff);
safe_free(&buff);
}
fclose(f);
if (return_message) {
if (buff) safe_free(&buff);
buff = return_message;
return_message = base64_encode(buff, strlen(buff));
if (buff) safe_free(&buff);
}
result = TRUE;
get_stat_ping_end:
if (lines_to_skip) safe_free(&lines_to_skip);
if (file_name) safe_free(&file_name);
if (buff) safe_free(&buff);
return result;
}