-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinger.cpp
73 lines (58 loc) · 1.77 KB
/
pinger.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
#include "pinger.h"
void Pinger::discover_network(Config *config)
{
int pipe_arr[2];
char buf[BUFLEN];
//Create pipe - pipe_arr[0] is "reading end", pipe_arr[1] is "writing end"
pipe(pipe_arr);
if(fork() == 0) //child
{
dup2(pipe_arr[1], STDOUT_FILENO);
execl("/sbin/arp", "arp", "-a", (char*)NULL);
}
else //parent
{
wait(NULL);
read(pipe_arr[0], buf, BUFLEN);
printf("%s\n", buf);
}
close(pipe_arr[0]);
close(pipe_arr[1]);
std::ostringstream buf_str;
buf_str << buf;
parse_arp_response(buf_str.str(), config);
}
void Pinger::parse_arp_response(std::string buf, Config *config)
{
for (unsigned long i = 0; i < config->mys_config.macs.size(); i++)
{
unsigned long found_mac_at = buf.find(config->mys_config.macs.at(i), 0);
if (found_mac_at != std::string::npos)
{
config->mys_config.online.at(i) = true;
config->mys_config.ips.at(i) = buf.substr(buf.substr(0, found_mac_at).find_last_of("(") + 1,
buf.substr(0, found_mac_at).find_last_of(")") -
buf.substr(0, found_mac_at).find_last_of("(") - 1);
}
}
}
//void Pinger::send_ping(std::string *ip)
//{
// int pipe_arr[2];
// char buf[BUFLEN];
// //Create pipe - pipe_arr[0] is "reading end", pipe_arr[1] is "writing end"
// pipe(pipe_arr);
// if(fork() == 0) //child
// {
// dup2(pipe_arr[1], STDOUT_FILENO);
// execl("/sbin/ping", "ping", "-c 1", &ip, (char*)NULL);
// }
// else //parent
// {
// wait(NULL);
// read(pipe_arr[0], buf, BUFLEN);
// printf("%s\n", buf);
// }
// close(pipe_arr[0]);
// close(pipe_arr[1]);
//}