-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcap_reader.cpp
135 lines (102 loc) · 3.49 KB
/
pcap_reader.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
#include "pcap_reader.h"
//LOGIC FOR PCAP_READER
//WRITTEN BY NIK BENDER
//UPDATE THE FOLLOWING FIVE INCLUDES FOR YOUR OPERATING SYSTEM (Currently configured for: Ubuntu 18.04)
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/can/error.h>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
using namespace boost::filesystem;
using namespace std;
//MEMORY STRUCT FOR HOLDING PACKET VALUES
typedef struct __attribute__((packed))
{
uint16_t length;
uint16_t type;
uint8_t tag[8];
uint32_t time_low;
uint32_t time_high;
uint8_t channel;
uint8_t dlc;
uint16_t flags;
uint32_t canid;
uint8_t data[8];
}
peakpacket_t;
/*
FUNCTION TO IMPORT PCAP FILES
@param FILEPATH OF .PCAP CONTAINING FOLDER, SET TO resources/radar
RETURNS VECTOR OF .PCAP FILES
*/
vector <string> get_pcaps(string folder_path)
{
path p{folder_path};
vector <string> files;
if(is_directory(p))
{
for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))
{
if(entry.path().extension().string() == ".pcap")
{
files.push_back(entry.path().string());
}
}
}
return files;
}
/*
FUNCTION TO PROCESS PCAP FILES
*see code for more in-depth breakdown
*/
void process()
{
int s;
struct sockaddr_can addr;
struct ifreq ifr;
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
strcpy(ifr.ifr_name, "vcan0");
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(s, (struct sockaddr *)&addr, sizeof(addr));
//importing files
vector <string> file_names = get_pcaps("../resources/radar/");
//prints number of packet files loaded sucessfully (check this with your actual number of .pcap files)
printf("Number of files loaded sucessfully: %d \n \n \n", file_names.size());
//for loop runs until files are fully searched through
for(auto file_path : file_names)
{
int nbytes;
struct can_frame frame;
char errbuff[PCAP_ERRBUF_SIZE];
pcap_t *pcap = pcap_open_offline(file_path.c_str(), errbuff);
u_int packetCount = 0;
struct pcap_pkthdr *header;
peakpacket_t *data;
//prints size of packet... useful for debugging (make sure these numbers are consistant with what you're processing)
printf("Sizeof peakpackt: %d \n", sizeof(peakpacket_t));
while (int returnValue = pcap_next_ex(pcap, &header, (const uint8_t**)&data) >= 0)
{
//display warning if the length captured is different
if (header->len != header->caplen)
printf("Warning! Capture size different than packet size: %ld bytes\n", header->len);
frame.can_dlc = data->dlc;
frame.can_id = ntohl(data->canid);
memcpy(frame.data, data->data, data->dlc);
//takes dummy can struct and writes to socket (this is what you'll be capturing)
nbytes = write(s, &frame, sizeof(struct can_frame));
}
}
//print to confirm program finished
printf("FINISHED PROCESSING \n \n");
}
int main()
{
cout << endl << endl << "############################################" << endl;
cout << "PCAP_READER BY NIK BENDER AND COLE RADETICH" << endl;
cout << "FOR READING RAW PCAP PACKET DATA FROM RADAR" << endl;
cout << "############################################" << endl << endl;
//PCAP PROCESSING FUNCTION
process();
}