-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
152 lines (138 loc) · 4.77 KB
/
parser.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Author: Matej Nesuta
* Login: xnesut00
**/
#include "parser.h"
#include "utils.h"
#include <stdbool.h>
#include <netinet/if_ether.h>
extern struct pools pools;
extern struct source source;
// If DHCP overload (option 52) is present, this function is called to search
// for option 53 in sname/bootfile/both.
u_char* checkSnameAndBootfile(u_char* payload,
int option_type,
int overload_type) {
u_char* type = NULL;
switch (overload_type) {
case 1:
type = findOptionInOptions(payload + 44 + 64, option_type,
payload + 236);
break;
case 2:
type =
findOptionInOptions(payload + 44, option_type, payload + 108);
break;
case 3:
type =
findOptionInOptions(payload + 44, option_type, payload + 108);
if (type == NULL) {
type = findOptionInOptions(payload + 44 + 64, option_type,
payload + 236);
}
break;
}
return type;
}
// This function is used to look for specific option either in the option field,
// or in sname/bootfile.
u_char* findOptionInOptions(u_char* options,
int option_type,
u_char* payload_end) {
while (*options != 255) {
if (*options == option_type) {
return options;
} else if (options + 1 >= payload_end) {
break;
} else if (*options != 0) {
options++;
if ((options + *options + 1) < payload_end) {
options += *options + 1;
} else {
break;
}
} else {
options++;
}
}
return NULL;
}
// This function compares 2 IPV4s in a network order.
void compareIPV4s(u_int32_t host, struct pool* pool, int set) {
int64_t difference = (int64_t)(host - ((*pool).addr.s_addr));
if (difference > 0 &&
difference < (int64_t)(1l << (32 - (*pool).prefix)) - 1) {
setBit((*pool).allocation.bits, difference, set);
}
return;
}
// This function was made using this tutorial:
// https://www.devdungeon.com/content/using-libpcap-c. It is called upon arrival
// of every packet.
void packet_handler(u_char* args,
const struct pcap_pkthdr* header,
const u_char* packet) {
args = NULL;
struct ether_header* eth_header;
eth_header = (struct ether_header*)packet;
int ethernet_header_length = 14; /* Doesn't change */
if (ntohs(eth_header->ether_type) != ETHERTYPE_IP) {
return;
}
const u_char* ip_header;
const u_char* payload;
int ip_header_length;
int udp_header_length = 8;
int payload_length;
ip_header = packet + ethernet_header_length;
ip_header_length = ((*ip_header) & 0x0F);
ip_header_length = ip_header_length * 4;
u_char protocol = *(ip_header + 9);
if (protocol != IPPROTO_UDP) {
return;
}
int total_headers_size =
ethernet_header_length + ip_header_length + udp_header_length;
payload_length = header->caplen - (ethernet_header_length +
ip_header_length + udp_header_length);
payload = packet + total_headers_size;
if (payload_length > 241) {
parseDHCP(payload, payload_length);
}
if (source.isInterface == true) {
printOnline(pools);
}
}
// This function is called when possible DHCP message was found.
void parseDHCP(const u_char* payload, int payload_size) {
// DHCP cookie check.
u_char* cookie = (u_char*)payload + 236;
if (cookie[0] != 99 || cookie[1] != 130 || cookie[2] != 83 ||
cookie[3] != 99) {
return;
}
// Firstly, we want to search for option 53 in options.
u_char* type =
findOptionInOptions(cookie + 4, 53, (u_char*)payload + payload_size);
u_char* overload = NULL;
// If option 53 is not found, we search for option 52.
if (type == NULL) {
overload = findOptionInOptions(cookie + 4, 52,
(u_char*)payload + payload_size);
}
// If option 52 was found, we continue the search in sname/bootfile.
// Otherwise, the message is discarded.
if (overload != NULL) {
type = checkSnameAndBootfile((u_char*)payload, 53, overload[2]);
}
if (type != NULL) {
// If ACK message is found, search through every network pool is done to
// check if yiaddr belongs to any of these pools.
if (type[2] == 5) {
uint32_t* yiaddr = (uint32_t*)(payload + 16);
for (size_t i = 0; i < pools.size; i++) {
compareIPV4s(htonl(*yiaddr), pools.data + i, 1);
}
}
}
}