-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-command.c
284 lines (248 loc) · 8.13 KB
/
ping-command.c
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
278
279
280
281
282
283
284
#include "ping-command.h"
int REQUEST_COUNT = 3; // 单个ping命令执行时,指定发送的请求数
int SINGLE_REQUEST_TIMEOUT = 1000; // 单个ping命令执行时,等待每次回复的超时时间,单位毫秒(如发送三个请求,每个最多等待1s,一共等待3s)注意linux上存在差异
int MAX_TIMEOUT = 10000; // 等待系统命令执行返回的最大超时时间, 单位毫秒
int main()
{
// 定义要ping的IP地址数组
const char *ip_addresses[] = {
"192.168.1.1",
"192.168.255.255",
"8.8.8.8",
"8.8.4.4"};
// IP地址的数量
int num_ips = sizeof(ip_addresses) / sizeof(ip_addresses[0]);
// 用于存储ping结果的数组
int results[num_ips];
// 调用 ping_multiple_ips 函数
ping_multiple_ips(ip_addresses, results, num_ips);
// 打印结果
for (int i = 0; i < num_ips; i++)
{
printf("IP Address: %s, Ping Result: %d\n", ip_addresses[i], results[i]);
}
return 0;
}
void ping_multiple_ips(const char **ip_addresses, int *results, int num_ips)
{
if (ip_addresses == NULL)
{
printf("in ping_multiple_ips, param error, ip_addresses is NULL");
return;
}
if (results == NULL)
{
printf("in ping_multiple_ips, param error, results is NULL");
return;
}
if (num_ips <= 0)
{
printf("in ping_multiple_ips, param error, num_ips:%d is less than or equal to 0", num_ips);
return;
}
#ifdef _WIN32
ping_multiple_ips_on_win(ip_addresses, results, num_ips);
#else
ping_multiple_ips_on_unix(ip_addresses, results, num_ips);
#endif
}
#ifdef _WIN32
DWORD WINAPI PingThreadOnWin(LPVOID lpParam)
{
PingData *pingData = (PingData *)lpParam;
const char *ip_address = pingData->ip_address;
/* windows ping 命令参数
-w timeout:指定等待每个回复的超时时间,单位毫秒
-n count:指定发送的请求数
*/
char command[100];
snprintf(command, sizeof(command), "ping -w %d -n %d %s >NUL 2>&1", SINGLE_REQUEST_TIMEOUT, REQUEST_COUNT, ip_address);
printf("command:%s\n", command);
// CreateProcess variables
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; // Hide the window
ZeroMemory(&pi, sizeof(pi));
// Add the command to cmd.exe
char full_command[200];
snprintf(full_command, sizeof(full_command), "cmd.exe /C %s", command);
if (CreateProcess(NULL, full_command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Get the exit code.
DWORD exit_code;
GetExitCodeProcess(pi.hProcess, &exit_code);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
switch (exit_code)
{
case 0:
// Alive
*(pingData->result) = 1;
break;
// Unreachable or unknown host
case 1:
case 68:
default:
// Unreachable, unknown host, or other errors
*(pingData->result) = -1;
break;
}
}
else
{
printf("CreateProcess failed...");
*(pingData->result) = -1;
}
return 0;
}
void ping_multiple_ips_on_win(const char **ip_addresses, int *results, int num_ips)
{
HANDLE *threads = (HANDLE *)malloc(num_ips * sizeof(HANDLE));
PingData *pingData = (PingData *)malloc(num_ips * sizeof(PingData));
if (threads == NULL || pingData == NULL)
{
printf("Memory allocation failed");
if (threads)
free(threads);
if (pingData)
free(pingData);
return;
}
for (int i = 0; i < num_ips; i++)
{
pingData[i].ip_address = ip_addresses[i];
pingData[i].result = &results[i];
threads[i] = CreateThread(NULL, 0, PingThreadOnWin, &pingData[i], 0, NULL);
if (threads[i] == NULL)
{
printf("CreateThread failed for IP %s", ip_addresses[i]);
results[i] = -1; // Mark as failed
}
}
// Wait for all threads to complete, with a timeout
DWORD wait_result = WaitForMultipleObjects(num_ips, threads, TRUE, MAX_TIMEOUT);
if (wait_result == WAIT_TIMEOUT)
{
printf("Timeout waiting for threads to complete.");
}
// Check each thread result
for (int i = 0; i < num_ips; i++)
{
DWORD exit_code;
if (threads[i] == NULL)
{
continue;
}
if (GetExitCodeThread(threads[i], &exit_code))
{
if (exit_code == STILL_ACTIVE)
{
printf("Thread for IP %s did not complete in time.", ip_addresses[i]);
results[i] = -1; // Mark as failed
}
}
else
{
printf("Failed to get exit code for thread %d.", i);
results[i] = -1; // Mark as failed
}
CloseHandle(threads[i]);
}
free(threads);
free(pingData);
}
#else
void *PingThreadOnLinux(void *arg)
{
PingData *pingData = (PingData *)arg;
const char *ip_address = pingData->ip_address;
/* linux ping 命令参数
-w timeout:指定总的超时时间,单位是秒,即多少秒后退出,与windows上存在差异
-c count:指定发送的请求数
*/
int REQUEST_TOTAL_TIMEOUT_LINUX = (SINGLE_REQUEST_TIMEOUT / 1000) * REQUEST_COUNT; // (ms --> s) * (单个请求等待时间)
char command[100];
snprintf(command, sizeof(command), "ping -w %d -c %d %s >/dev/null 2>&1", REQUEST_TOTAL_TIMEOUT_LINUX, REQUEST_COUNT, ip_address);
// printf("command:%s", command);
int response = WEXITSTATUS(system(command));
switch (response)
{
case 0:
// Alive
*(pingData->result) = 1;
break;
case 1: // For Unix, 1 indicates unreachable
case 2: // For Unix, 2 indicates unknown host
case 256: // For some Unix systems, 256 indicates unreachable
default:
// Unreachable, unknown host, or other errors
*(pingData->result) = -1;
break;
}
pthread_mutex_lock(&pingData->mutex);
pingData->finished = 1;
pthread_cond_signal(&pingData->cond);
pthread_mutex_unlock(&pingData->mutex);
return NULL;
}
void ping_multiple_ips_on_unix(const char **ip_addresses, int *results, int num_ips)
{
PingData *pingData = (PingData *)malloc(num_ips * sizeof(PingData));
if (pingData == NULL)
{
printf("Memory allocation failed");
return;
}
for (int i = 0; i < num_ips; i++)
{
pingData[i].ip_address = ip_addresses[i];
pingData[i].result = &results[i];
pingData[i].finished = 0;
pthread_mutex_init(&pingData[i].mutex, NULL);
pthread_cond_init(&pingData[i].cond, NULL);
if (pthread_create(&pingData[i].thread_id, NULL, PingThreadOnLinux, &pingData[i]) == ETIMEDOUT)
{
printf("pthread_create");
results[i] = -1; // Mark as failed if thread creation fails
}
}
struct timespec end_tm;
clock_gettime(CLOCK_REALTIME, &end_tm);
end_tm.tv_sec += (MAX_TIMEOUT / 1000);
for (int i = 0; i < num_ips; i++)
{
pthread_mutex_lock(&pingData[i].mutex);
while (!pingData[i].finished)
{
int res = pthread_cond_timedwait(&pingData[i].cond, &pingData[i].mutex, &end_tm);
if (res != 0)
{
if (res == ETIMEDOUT)
{
printf("Timeout waiting for thread %d", i);
}
else
{
printf("Error waiting for thread %d: %s", i, strerror(res));
}
results[i] = -1;
break;
}
}
pthread_mutex_unlock(&pingData[i].mutex);
pthread_mutex_destroy(&pingData[i].mutex);
pthread_cond_destroy(&pingData[i].cond);
}
for (int i = 0; i < num_ips; i++)
{
pthread_join(pingData[i].thread_id, NULL);
}
free(pingData);
}
#endif