-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
74 lines (68 loc) · 2.65 KB
/
Copy pathutils.cpp
File metadata and controls
74 lines (68 loc) · 2.65 KB
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
#include "utils.h"
#include <string.h>
namespace utils {
void split(const char *str, const char *delim, std::vector<std::string>& out)
{
const char* begin = str;
const char* it = strstr(str, delim);
if (it != NULL){
std::string data{begin, it};
out.push_back(data);
it++;
split(it, delim, out);
} else {
std::string data{str};
out.push_back(data);
}
}
struct EthL4 GetEthL4(const u_char* data)
{
#ifdef __unix__
#define DATAOFFSET eth.tcpHeader->doff
#else
#define DATAOFFSET eth.tcpHeader->data_offset
#endif
struct EthL4 eth;
memset(ð, 0, sizeof(struct EthL4));
eth.type = EthL4::UNKNOWN; // default
if (!data)
return eth;
eth.ethernetHeader = (struct ether_header*)data;
if (ntohs(eth.ethernetHeader->ether_type) == ETHERTYPE_IP) {
eth.ipHeader = (struct ip*)(data + sizeof(struct ether_header));
inet_ntop(AF_INET, &(eth.ipHeader->ip_src), eth.sourceIP, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(eth.ipHeader->ip_dst), eth.destIP, INET_ADDRSTRLEN);
if (eth.ipHeader->ip_p == IPPROTO_UDP) {
eth.udpHeader = (struct udphdr*)(data + sizeof(struct ether_header) + sizeof(struct ip));
eth.type = EthL4::UDP;
} else if (eth.ipHeader->ip_p == IPPROTO_TCP) {
size_t total = sizeof(struct ether_header) + sizeof(struct ip);
eth.tcpHeader = (struct tcphdr*)(data + total);
if (DATAOFFSET > 5) {
total += sizeof(struct tcphdr);
eth.options = (unsigned char*) (data + total);
auto test = total + DATAOFFSET*4 -sizeof(struct tcphdr);
unsigned char* endPtr = (unsigned char*)&data[test];
if (eth.options)
{
for(unsigned char* o = eth.options; o != endPtr; o++){
if (*o > EthL4::NOP && *o <= EthL4::TIMESTAMP) {
o++;
eth.options_len += *o;
o += *o-2;
} else {
eth.options_len++;
}
}
}
}
eth.payload_len = eth.options_len + total;
eth.type = EthL4::TCP;
} else {
eth.type = EthL4::UNKNOWN;
}
}
#undef DATAOFFSET
return eth;
}
}