-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
56 lines (55 loc) · 1.81 KB
/
main.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
#include "src/util.h"
#include "src/device_manager.h"
#include <pcap.h>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <netinet/if_ether.h>
#include <unistd.h>
#include <iostream>
/*
Description of this file:
1) find all devices and try to add them to device_list;
2) each device will send a packet to "ff:ff:ff:ff:ff:ff" every 5 seconds;
3) During Device initianlizing, it will create a thread using "pcap_loop" to receive packets;
*/
int main() {
char errBuf[PCAP_ERRBUF_SIZE] = {0};
pcap_if_t *alldevs;
if (pcap_findalldevs(&alldevs, errBuf) < 0) {
perror("[Error] [findalldevs]");
return 0;
}
pcap_if_t *head = alldevs;
DeviceManager manager;
setFrameReceiveCallback(myOnReceived, manager);
while (head->next != nullptr) {
printf("[Info] [Name: %s] [Description : %s]\n", head->name, head->description);
if (addDevice(head->name, manager) < 0) {
std::cerr << "[Info] [Name:] " << head->name << " add failed!\n";
} else {
std::cout << "[Info] [Name:] " << head->name << " add succeeded!\n";
}
std::cout << "\n";
head = head->next;
}
while (1) {
for (auto &device : manager.devices) {
fprintf(stdout, "[name: %s] [id: %d]\n", device.name.c_str(), device.id);
u_char *content = new u_char[100];
memset(content, 15, 100);
u_char *dest_mac = new u_char[6];
memset(dest_mac, 255, 6);
sendFrame(content, 100, 0x0800, dest_mac, device.id, manager);
fprintf(stdin, "CRC of this frame is %0X\n", calculateCRC32(content, 100));
delete[] content;
delete[] dest_mac;
}
sleep(5);
}
return 0;
}