-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.dev.cpp
48 lines (35 loc) · 1.16 KB
/
get.dev.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
// (c) 2018 Yuoa.
// get.dev.cpp
#include "common.hpp"
#include "dev.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
/*[Q] Why I used getifaddrs rather than pcap_findalldevs?
[A] I don't know why, but in my server, pcap_findalldevs doesn't operated
correctly. So I directly used getifaddrs.*/
char* getNetworkDevices() {
ifas *devsOriginalForm;
int getAddrResult;
getAddrResult = getifaddrs(&devsOriginalForm);
if (getAddrResult) {
cerr << "Failed to find default device: " << getAddrResult << endl;
terminate(ERR_FAIL_FINDDEVICES);
} else {
// Convert ifas** to vector
vector<ifas*> devs;
while (devsOriginalForm != NULL) {
devs.push_back(devsOriginalForm);
devsOriginalForm = devsOriginalForm->ifa_next;
}
if (devs.size() == 0) {
cerr << "There is no network device." << endl;
terminate(ERR_NO_NETDEVICES);
} else {
cout << devs.size() << " interfaces found." << endl;
return selectNetworkDevice(devs);
}
}
return NULL;
}