forked from KTZ-Goel/PacketSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.cpp
executable file
·108 lines (91 loc) · 3.37 KB
/
udp.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
#include <arpa/inet.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "dns.h"
#include "ports.h"
#include "udp.h"
#include "colors.h"
#include "shared.h"
#include "tags.h"
void handle_udp(QList<QStandardItem*>* row, const struct sniff_udp* udp)
{
uint16_t sourcePort = ntohs(udp->uh_sport);
uint16_t destinationPort = ntohs(udp->uh_dport);
uint16_t length = ntohs(udp->uh_len);
uint16_t checksum = ntohs(udp->uh_sum);
printf(CYAN " UDP Header:\n" RESET);
if (length < 8) {
printf(RED " Invalid UDP header length: %u bytes\n" NORMAL, length);
return;
}
printf(" Src. port ---- %u\n", sourcePort);
printf(" Dest. Port --- %u\n", destinationPort);
printf(" Length ------- %u bytes\n", length);
printf(" Checksum ----- 0x%X\n", checksum);
bool portFound = false;
switch (destinationPort) {
case PORT_DNS: {
handle_dns(row, (struct sniff_dns*)(((char*)udp) + 8));
portFound = true;
break;
}
default: {
row->append(new QStandardItem("UDP"));
QString infoString;
infoString = "Port " + QString::number(sourcePort, 10) + " port " + QString::number(destinationPort, 10) + " communication";
row->append(new QStandardItem(infoString));
portFound = true;
}
}
if (portFound == false) {
switch (sourcePort) {
case PORT_DNS: {
handle_dns(row, (struct sniff_dns*)(((char*)udp) + 8));
break;
}
default: {
printf(YELLOW " Application layer protocol [%d] not implemented yet." RESET "\n", destinationPort);
row->append(new QStandardItem("UDP"));
QString infoString;
infoString = "Port " + QString::number(sourcePort, 10) + " port " + QString::number(destinationPort, 10) + " communication";
row->append(new QStandardItem(infoString));
}
}
}
}
void handle_udp_fill(QString* infoStr, const struct sniff_udp* udp)
{
uint16_t sourcePort = ntohs(udp->uh_sport);
uint16_t destinationPort = ntohs(udp->uh_dport);
uint16_t length = ntohs(udp->uh_len);
uint16_t checksum = ntohs(udp->uh_sum);
infoStr->append(HEADER_TAG_START "UDP Header:" HEADER_TAG_END NEWLINE);
if (length < 8) {
infoStr->append(TAB ERROR_TAG_START + QString("Invalid UDP header length: %1 bytes").arg(length) + ERROR_TAG_END);
return;
}
infoStr->append(TAB + QString(BOLD_TAG_START "Src. port" BOLD_TAG_END " ---- %1").arg(sourcePort) + NEWLINE);
infoStr->append(TAB + QString(BOLD_TAG_START "Dest. Port" BOLD_TAG_END " --- %1").arg(destinationPort) + NEWLINE);
infoStr->append(TAB + QString(BOLD_TAG_START "Length" BOLD_TAG_END " ------- %1 bytes").arg(length) + NEWLINE);
infoStr->append(TAB + QString(BOLD_TAG_START "Checksum" BOLD_TAG_END " ----- 0x%1").arg(checksum) + NEWLINE);
bool portFound = false;
switch (destinationPort) {
case PORT_DNS: {
handle_dns_fill(infoStr, (struct sniff_dns*)(((char*)udp) + 8));
portFound = true;
break;
}
}
if (portFound == false) {
switch (sourcePort) {
case PORT_DNS: {
handle_dns_fill(infoStr, (struct sniff_dns*)(((char*)udp) + 8));
break;
}
default: {
infoStr->append(QString(TAB "Neither port is implemented yet." NEWLINE));
}
}
}
}