-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicmp.cpp
205 lines (177 loc) · 5.17 KB
/
icmp.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include "icmp.h"
#include "colors.h"
#include "shared.h"
#include "tags.h"
/*
The timestamp for the icmp echo (ping) request/reply is an integer
representing the number of milliseconds since epoch time
*/
void handle_icmp(QList<QStandardItem *> *row, const uint8_t *data, uint16_t length){
uint8_t type = data[0];
uint8_t code = data[1];
uint16_t checksum = ntohs(((uint16_t *)(data+2))[0]);
uint16_t offset = 4;
printf(CYAN " ICMP:\n" RESET);
row->append(new QStandardItem("ICMP"));
QString infoStr;
printf(" Type --------- [%u] ", type);
switch(type){
case ICMP_TYPE_ECHO_REPLY: {
printf("Echo reply\n");
infoStr += "Echo Reply, ";
time_t seconds = *((time_t *)(data) + 2);
time_t ms = *((time_t *)(data) + 3);
printTimestamp(&infoStr, seconds, ms);
break;
}
case ICMP_TYPE_ECHO_REQUEST: {
printf("Echo request\n");
infoStr += "Echo Request, ";
time_t seconds = *((time_t *)(data) + 2);
time_t ms = *((time_t *)(data) + 3);
printTimestamp(&infoStr, seconds, ms);
break;
}
case ICMP_TYPE_DEST_UNREACH: {
printf("Destination unreachable\n");
infoStr += "Destination unreachable";
break;
}
/*
case ICMP_TYPE_TRACEROUTE: {
printf("Traceroute\n");
break;
}
*/
default: {
printf( YELLOW "Unknown type\n" RESET);
break;
}
}
row->append(new QStandardItem(infoStr));
printf(" Code --------- [%u]\n", code);
printf(" Checksum ----- 0x%04X\n", checksum);
printf(" Data:\n\t\t\t");
data += offset;
uint16_t n = 0; //Used for newlines
while(offset < length){
if(n == 4){
printf("\n\t\t\t");
n = 0;
}
printBinaryuint8_t(*data);
putchar(' ');
data++;
n++;
offset++;
}
putchar('\n');
}
void handle_icmp_fill(QString *infoStr, const uint8_t *data, uint16_t length){
uint8_t type = data[0];
uint8_t code = data[1];
uint16_t checksum = ntohs(((uint16_t *)(data+2))[0]);
uint16_t offset = 4;
//printf(CYAN " ICMP:\n" RESET);
infoStr->append(QString(HEADER_TAG_START "ICMP:" HEADER_TAG_END NEWLINE));
//printf(" Type --------- [%u] ", type);
infoStr->append(QString(TAB BOLD_TAG_START "Type" BOLD_TAG_END " --------- [%1] ").arg(type));
switch(type){
case ICMP_TYPE_ECHO_REPLY: {
//printf("Echo reply\n");
infoStr->append("Echo reply" NEWLINE);
time_t seconds = *((time_t *)(data) + 2);
time_t ms = *((time_t *)(data) + 3);
//printTimestamp(&infoStr, seconds, ms);
infoStr->append(TAB BOLD_TAG_START "Timestamp ---- " BOLD_TAG_END);
infoStr->append(getTimeStamp(seconds, ms));
infoStr->append(NEWLINE);
break;
}
case ICMP_TYPE_ECHO_REQUEST: {
//printf("Echo request\n");
infoStr->append("Echo request" NEWLINE);
time_t seconds = *((time_t *)(data) + 2);
time_t ms = *((time_t *)(data) + 3);
break;
}
case ICMP_TYPE_DEST_UNREACH: {
//printf("Destination unreachable\n");
infoStr->append("Destination unreachable" NEWLINE);
break;
}
/*
case ICMP_TYPE_TRACEROUTE: {
printf("Traceroute\n");
break;
}
*/
default: {
//printf( YELLOW "Unknown type\n" RESET);
infoStr->append(YELLOW_FONT_START "Unknown type" YELLOW_FONT_END NEWLINE);
break;
}
}
//printf(" Code --------- [%u]\n", code);
infoStr->append(QString(TAB BOLD_TAG_START "Code" BOLD_TAG_END " --------- [%1]" NEWLINE).arg(code));
//printf(" Checksum ----- 0x%04X\n", checksum);
char checksumBuffer[5];
snprintf(checksumBuffer, sizeof(checksumBuffer), "%04X", checksum);
infoStr->append(QString(TAB BOLD_TAG_START "Checksum" BOLD_TAG_END " ----- 0x%1" NEWLINE).arg(checksumBuffer));
//printf(" Data:\n\t\t\t");
infoStr->append(TAB BOLD_TAG_START "Data:" BOLD_TAG_END NEWLINE TAB TAB);
data += offset;
uint16_t n = 0; //Used for newlines
while(offset < length){
if(n == 4){
//printf("\n\t\t\t");
infoStr->append(NEWLINE TAB TAB);
n = 0;
}
//printBinaryuint8_t(*data);
infoStr->append(strBinaryuint8_t(*data));
//putchar(' ');
infoStr->append(' ');
data++;
n++;
offset++;
}
//putchar('\n');
}
void printTimestamp(QString *infoStr, time_t seconds, time_t ms){
char *datetime = ctime(&seconds);
datetime[strlen(datetime)-1] = '\0';
int i = strlen(datetime);
while(datetime[i] != ' '){ //Find the index of the last space (the space right before the year)
i--;
}
printf(" Timestamp ---- ");
int j = 0;
while(j < i){
putchar(datetime[j]);
infoStr->append(datetime[j]);
j++;
}
printf(".%u%s\n", (uint32_t)ms, datetime+i);
infoStr->append(QString(".%1%2").arg(ms).arg(QString(datetime+i)));
}
QString getTimeStamp(time_t seconds, time_t ms){
char *datetime = ctime(&seconds);
datetime[strlen(datetime)-1] = '\0';
QString timeString;
int i = strlen(datetime);
while(datetime[i] != ' '){ //Find the index of the last space (the space right before the year)
i--;
}
int j = 0;
while(j < i){
timeString.append(datetime[j]);
j++;
}
timeString.append(QString(".%1%2").arg(ms).arg(QString(datetime+i)));
return timeString;
}