-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.cc
186 lines (153 loc) · 3.51 KB
/
gui.cc
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
#include <qapplication.h>
#include <qlabel.h>
#include <qfont.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include "secvis.h"
#include "capture.h"
QApplication *a;
QLabel *textBox;
QString to_hex(unsigned char c)
{
QString theString;
unsigned char firstHex;
unsigned char secHex;
firstHex = (c>>4)%16;
if(firstHex < 10)
{
firstHex += '0';
}
else
{
firstHex += 'A' - 10;
}
secHex = c%16;
if(secHex < 10)
{
secHex += '0';
}
else
{
secHex += 'A' - 10;
}
theString.append(firstHex);
theString.append(secHex);
return theString;
}
char to_ascii(char c)
{
if(c >= ' ' && c <= '~')
return c;
else
return '.';
}
QString bytes_to_hexascii(char *text, int size)
{
QString output;
int i = 0;
int j = 0;
int evenSize = 0; /* stores even size as multiple of row_width */
/* change this to adjust row_width
* row_width in terms of ascii char. width will be
* row_width + 2*row_width for hex part
*/
int row_width = 16;
/* change this to change spacing of hex digits
* note, that we divide this number by two below
* because each character counts as two hex digits.
* make this number even.
*/
int hex_space = 4;
/* change this to change spacing of ascii chars
*/
int ascii_space = 8;
hex_space = hex_space/2;
evenSize = size/row_width*row_width;
if(evenSize < size) evenSize += row_width;
for(i = 0; i <= evenSize; i++)
{
/* ascii stuff, new line, spacing */
if(i % row_width == 0 && i != 0)
{
/* do ascii part for this row */
output.append(" ");
for(j = i - row_width; j < i && j < size; j++)
{
if((j % row_width) % ascii_space == 0 && j % row_width != 0)
{
output.append(' ');
}
output.append(to_ascii(text[j]));
}
output.append('\n');
}
else if(i >= size && i % hex_space == 0 && i % row_width != 0)
{
/* last row, special case, fake the hex space */
output.append(' ');
}
else if((i % row_width) % hex_space == 0 && i % row_width != 0)
{
/* put a space for hex */
output.append(' ');
}
/* process new character or fake one */
if(i < size)
{
/* more characters to process */
output.append(to_hex(text[i]));
}
else
{
/* fake a character */
output.append(' ');
output.append(' ');
}
}
return output;
}
void textbox_pkt_info(struct pkt_info *pinfo) {
struct in_addr ipaddr;
QString string;
switch(pinfo->proto) {
case PKT_TCP:
string.append("TCP");
break;
case PKT_UDP:
string.append("UDP");
break;
default:
string.append("Unknown");
}
string.append(" ");
ipaddr.s_addr = htonl(pinfo->sip);
string.append(inet_ntoa(ipaddr));
string = string.append(":%1").arg(pinfo->sport);
string.append(" -> ");
ipaddr.s_addr = htonl(pinfo->dip);
string.append(inet_ntoa(ipaddr));
string = string.append(":%1").arg(pinfo->dport);
string.append("\n\n");
if(pinfo->payload)
string.append(bytes_to_hexascii(pinfo->payload, pinfo->payload_len));
//std::cout << string.latin1() << std::endl;
//textBox->clear();
textBox->setText(string);
}
void gui_init( int argc, char **argv )
{
a = new QApplication( argc, argv );
textBox = new QLabel(0);
textBox->setFont(QFont("Courier", 12));
textBox->setTextFormat(Qt::PlainText);
textBox->resize(600, 140);
textBox->setAlignment(Qt::AlignTop | Qt::AlignLeft);
textBox->setWindowTitle("SecVis Packet Information Output Window");
textBox->setText("Select a packet by right-clicking into the visualization window");
textBox->show();
}
int gui_run() {
return a->exec();
}