-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathethernet.cpp
More file actions
129 lines (111 loc) · 5.17 KB
/
ethernet.cpp
File metadata and controls
129 lines (111 loc) · 5.17 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <arpa/inet.h>
#include <QList>
#include <QVariant>
#include <QStandardItem>
#include "ethernet.h"
#include "modelcolumnindexes.h"
/* Ethertypes */
#include "arp.h"
#include "ipv4.h"
#include "ipv6.h"
#include "colors.h"
#include "shared.h"
#include "tags.h"
void handle_ethernet(QList<QStandardItem *> *row, const uint8_t *packet){
//time_t currentTime = time(NULL);
const struct sniff_ethernet *ethernet; //The ethernet header
ethernet = (struct sniff_ethernet*)packet;
// Time printed in this format: YYYY/MM/DD HH:MM:SS
/*
struct tm *tmptr = localtime(¤tTime);
printf(CYAN " Time:\n" RESET);
printf(" Y/M/D h:m:s -- %d/%02d/%02d %02d:%02d:%02d\n", tmptr->tm_year+1900, tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec);
printf(" epoch time --- %ld seconds\n", (long)currentTime);
//Append the time recieved column
char date[20];
snprintf(date, sizeof(date), "%d/%02d/%02d %02d:%02d:%02d", tmptr->tm_year+1900,
tmptr->tm_mon+1,
tmptr->tm_mday,
tmptr->tm_hour,
tmptr->tm_min,
tmptr->tm_sec);
date[sizeof(date)-1] = '\0';
row->append(new QStandardItem(QString(date)));
*/
//Ethernet header information
printf(CYAN " Ethernet Header:\n" RESET);
printf(" Destination -- %02X:%02X:%02X:%02X:%02X:%02X\n", ethernet->ether_dhost[0], ethernet->ether_dhost[1], ethernet->ether_dhost[2], ethernet->ether_dhost[3], ethernet->ether_dhost[4], ethernet->ether_dhost[5]);
printf(" Source ------- %02X:%02X:%02X:%02X:%02X:%02X\n", ethernet->ether_dhost[0], ethernet->ether_shost[1], ethernet->ether_shost[2], ethernet->ether_shost[3], ethernet->ether_shost[4], ethernet->ether_shost[5]);
printf(" Ethertype ---- 0x%04X ", ntohs(ethernet->ether_type));
//Determine the Network/Internet layer protocol
switch(ntohs(ethernet->ether_type)){
case ETHERTYPE_IPV4:{
printf("(IPv4)\n");
handle_ipv4(row, (struct sniff_ipv4*)(packet + SIZE_ETHERNET));
break;
}
case ETHERTYPE_IPV6: {
printf("(IPv6)\n");
handle_ipv6(row, (struct sniff_ipv6*)(packet + SIZE_ETHERNET));
break;
}
case ETHERTYPE_ARP: {
printf("(ARP)\n");
handle_arp(row, (struct sniff_arp*)(packet + SIZE_ETHERNET));
break;
}
default: {
printf(YELLOW "(Not implemented yet )" RESET "\n");
break;
}
}
}
void handle_ethernet_fill(QString *infoStr, const char *data){
const struct sniff_ethernet *ethernet = (struct sniff_ethernet*)data; //The ethernet header
//Ethernet header
infoStr->append(HEADER_TAG_START "Ethernet Header" HEADER_TAG_END NEWLINE);
char buffer[18];
//Append the destination mac address
snprintf(buffer, sizeof(buffer), "%02X:%02X:%02X:%02X:%02X:%02X", ethernet->ether_dhost[0],
ethernet->ether_dhost[1],
ethernet->ether_dhost[2],
ethernet->ether_dhost[3],
ethernet->ether_dhost[4],
ethernet->ether_dhost[5]);
infoStr->append(TAB + QString(BOLD_TAG_START "Destination" BOLD_TAG_END " --- %1").arg(buffer) + NEWLINE);
//Append the source mac address
snprintf(buffer, sizeof(buffer), "%02X:%02X:%02X:%02X:%02X:%02X", ethernet->ether_shost[0],
ethernet->ether_shost[1],
ethernet->ether_shost[2],
ethernet->ether_shost[3],
ethernet->ether_shost[4],
ethernet->ether_shost[5]);
infoStr->append(TAB + QString(BOLD_TAG_START "Source" BOLD_TAG_END " -------- %1").arg(buffer) + NEWLINE);
infoStr->append(TAB + QString(BOLD_TAG_START "Ethertype" BOLD_TAG_END " ----- %1").arg(ntohs(ethernet->ether_type)));
//Determine the Network/Internet layer protocol
switch(ntohs(ethernet->ether_type)){
case ETHERTYPE_IPV4:{
infoStr->append("(IPv4)" NEWLINE);
handle_ipv4_fill(infoStr, (struct sniff_ipv4*)(data + SIZE_ETHERNET));
break;
}
case ETHERTYPE_IPV6: {
infoStr->append("(IPv6)" NEWLINE);
//handle_ipv6(row, (struct sniff_ipv6*)(data + SIZE_ETHERNET));
break;
}
case ETHERTYPE_ARP: {
infoStr->append("(ARP)" NEWLINE);
handle_arp_fill(infoStr, (struct sniff_arp*)(data + SIZE_ETHERNET));
break;
}
default: {
infoStr->append(YELLOW_FONT_START "(Not implemented yet)" YELLOW_FONT_END NEWLINE);
break;
}
}
}