aboutsummaryrefslogtreecommitdiff
path: root/gui_packet.c
blob: a142d5048fb06f1b5f51fdb9c4ed879f4e6208eb (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * network-simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <lua.h>

#include "shared.h"
#include "template.h"
#include "gui_packet.h"
#include "lua_functions.h"
#include "helpers.h"

void text_buff_insert_end(GtkTextBuffer* buff, char* text) {
	GtkTextIter end_iter;
	gtk_text_buffer_get_end_iter(buff, &end_iter);
	gtk_text_buffer_insert(buff, &end_iter, text, -1);
}

void frame_to_data_frame(GtkWidget* frame) {
	gtk_widget_add_css_class(frame, "data_frame");
	gtk_widget_set_hexpand(frame, true);
}

void remove_frame_left(GtkWidget* frame) {
	gtk_widget_add_css_class(frame, "frame_no_left");
	gtk_widget_set_margin_start(frame, 2);
}

void remove_frame_right(GtkWidget* frame) {
	gtk_widget_add_css_class(frame, "frame_no_right");
	gtk_widget_set_margin_end(frame, 2);
}

// Format the data, then return the number of bytes formated
struct format_return {size_t used; size_t next;} format_data(char* type, char* segment, int remaining_size, int length_from_prev, GtkBox* data_box, GtkBox* raw_box) {
	struct format_return ret = {0, -1};
	// Add a new grid to the data box
	GtkWidget* data_grid = gtk_grid_new();
	// Insert a label describing the type
	char* type_head = malloc_or_abort(strlen(type) + 48);
	sprintf(type_head, "<span weight=\"bold\" size=\"large\">%s</span>", type);
	GtkWidget* type_label = gtk_label_new(type_head);
	gtk_label_set_use_markup(GTK_LABEL(type_label), true);
	gtk_box_append(GTK_BOX(data_box), type_label);
	gtk_box_append(data_box, data_grid);
	type_label = gtk_label_new(type_head);
	gtk_label_set_use_markup(GTK_LABEL(type_label), true);
	gtk_widget_set_halign(type_label, GTK_ALIGN_START);
	gtk_box_append(GTK_BOX(raw_box), type_label);
	free(type_head);
	if(!my_lua_call(info_lua, type, 1,
					LUA_TSTRING, remaining_size, segment,
					LUA_TNUMBER, remaining_size,
					LUA_TNUMBER, length_from_prev, -1)) return ret;
	// The returned table is now at index 1 on the stack
	// Check if the table is, in fact, a table :D
	if(!lua_istable(info_lua, -1)) {
		fprintf(stderr, "Warning: format function didn't return a table\n");
		return ret;
	}
	int cur_row_pos = 0;
	int cur_row = 0;
	// Now get all the components
	for(int i = 1; ; i++) {
		// Push the index
		lua_pushinteger(info_lua, i);
		// Get the table of the field
		lua_gettable(info_lua, -2);
		// Check if there is no entry left
		if(lua_isnil(info_lua, -1)) {
			break;
		}
		// Otherwise get all the components
		// First get the width
		lua_pushstring(info_lua, "width");
		lua_gettable(info_lua, -2);
		int width = lua_tointeger(info_lua, -1);
		lua_pop(info_lua, 1);
		// Next get the head
		lua_pushstring(info_lua, "head");
		lua_gettable(info_lua, -2);
		const char* head = lua_tostring(info_lua, -1);
		// And the text
		lua_pushstring(info_lua, "text");
		lua_gettable(info_lua, -3);
		const char* text = lua_tostring(info_lua, -1);
		// Check if we got null somewhere
		if(!head || !text) {
			lua_pop(info_lua, 3);
			printf("Got null somewhere while formating the packet\n");
			continue;
		}
		// Add everything to the gui
		// Make a frame
		GtkWidget* frame = gtk_frame_new(NULL);
		frame_to_data_frame(frame);
		GtkWidget* element_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
		gtk_frame_set_child(GTK_FRAME(frame), element_box);
		// First the head
		
		char* head_text = malloc_or_abort(strlen(head) + 42);
		sprintf(head_text, "<span weight=\"bold\">%s</span>: %d", head, width);
		// First add the label to the data gui
		GtkWidget* head_label = gtk_label_new(head_text);
		gtk_label_set_use_markup(GTK_LABEL(head_label), true);
		gtk_box_append(GTK_BOX(element_box), head_label);
		
		// Add the label to the raw gui
		head_label = gtk_label_new(head_text);
		gtk_widget_set_halign(head_label, GTK_ALIGN_START);
		gtk_label_set_use_markup(GTK_LABEL(head_label), true);
		gtk_box_append(GTK_BOX(raw_box), head_label);
		// Next add the text to the the data gui
		GtkTextBuffer* text_buff = gtk_text_buffer_new(NULL);
		gtk_text_buffer_set_text(text_buff, text, -1);
		GtkWidget* text_body_view = gtk_text_view_new_with_buffer(text_buff);
		gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_body_view), GTK_WRAP_CHAR);
		gtk_text_view_set_editable(GTK_TEXT_VIEW(text_body_view), false);
		gtk_box_append(GTK_BOX(element_box), text_body_view);
		// Add bytes in hex representation to the raw tab
		GtkTextBuffer* raw_buff = gtk_text_buffer_new(NULL);
		for(int j = 0; j < width && j < remaining_size; j++) {
			// Break the line after 8 bytes
			if(!(j%8) && j) {
				text_buff_insert_end(raw_buff, "\n");
			}
			char insert_buffer[5] = "";
			sprintf(insert_buffer, "%02X ", (unsigned char)(*(segment+j)));
			text_buff_insert_end(raw_buff, insert_buffer);
		}
		GtkWidget* raw_byte_view = gtk_text_view_new_with_buffer(raw_buff);
		gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(raw_byte_view), GTK_WRAP_NONE);
		gtk_text_view_set_editable(GTK_TEXT_VIEW(raw_byte_view), false);
		gtk_text_view_set_monospace(GTK_TEXT_VIEW(raw_byte_view), true);
		gtk_widget_set_vexpand(raw_byte_view, false);
   		gtk_box_append(GTK_BOX(raw_box), raw_byte_view);
		// Pop the subtable, head and text from the stack
		lua_pop(info_lua, 3);
		// Add the used bit
		ret.used += width;
		segment += width;
		// Add everything to the data tab
		int remaining_width = width;
		while(remaining_width) {
			// Make sure there is space in the current row
			if(cur_row_pos >= 4) {
				cur_row_pos = 0;
				cur_row++;
			}
			// Calculate the width in this row
			int this_width = remaining_width > (4 - cur_row_pos) ? 4 - cur_row_pos : remaining_width;
			if(gtk_widget_get_parent(frame)) {
				frame = gtk_frame_new(NULL);
				GtkWidget* nodata_label = gtk_label_new(head_text);
				gtk_label_set_use_markup(GTK_LABEL(nodata_label), true);
				gtk_frame_set_child(GTK_FRAME(frame), nodata_label);
				frame_to_data_frame(frame);
				remove_frame_left(frame);
			}

			// Append the frame
			gtk_grid_attach(GTK_GRID(data_grid), frame, cur_row_pos, cur_row, this_width, 1);
			if(this_width < remaining_width) {
				remove_frame_right(frame);
			}
			
			remaining_width -= this_width;
			cur_row_pos += this_width;
		}
		if(cur_row_pos < 4) {
			GtkWidget* empty_label = gtk_label_new("");
			gtk_grid_attach(GTK_GRID(data_grid), empty_label, cur_row_pos, cur_row, 4 - cur_row_pos, 1);
		}
		free(head_text);
	}
	lua_pushstring(info_lua, "next_width");
	lua_gettable(info_lua, -3);
	if(!lua_isnil(info_lua, -1)) {
		ret.next = lua_tointeger(info_lua, -1);
	}
	return ret;
}

void show_packet_window(int packet_size, char* packet_data, char* packet_type) {
	GtkWidget* data_box = NULL;
	GtkWidget* raw_box = NULL;
	
	GtkWidget* packet_window = gtk_window_new();
	gtk_window_set_default_size(GTK_WINDOW(packet_window), 400, 450);
	g_signal_connect(G_OBJECT(packet_window), "close-request", G_CALLBACK(gtk_window_destroy), NULL);
	GtkWidget* window_notebook = gtk_notebook_new();
	gtk_window_set_child(GTK_WINDOW(packet_window), window_notebook);
	// Create the overview tab
	data_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
	GtkWidget* data_label = gtk_label_new("Data");
	// Create a raw hex view tab
	raw_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
	GtkWidget* raw_label = gtk_label_new("Raw");

	GtkWidget* data_box_scroll = gtk_scrolled_window_new();
	gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(data_box_scroll), data_box);
	GtkWidget* raw_box_scroll = gtk_scrolled_window_new();
	gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(raw_box_scroll), raw_box);
	
	// For each type in packet_type beautify things and display them
	size_t type_len = strlen(packet_type) + 1;
	char* stringcopy = malloc_or_abort(type_len);
	memcpy(stringcopy, packet_type, type_len);
	char* segment = packet_data;
	int remaining_size = packet_size;
	struct format_return ret = {0};
	for(char* token = strtok(stringcopy, ","); token && remaining_size > 0; token = strtok(NULL, ",")) {
		// Format the data
		ret = format_data(token, segment, remaining_size, ret.next, GTK_BOX(data_box), GTK_BOX(raw_box));
		int segment_size = ret.used;
		// Move the segment forward
		segment = segment + segment_size;
		remaining_size -= segment_size;
		// Add a seperator
		GtkWidget* segment_seperator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
		gtk_box_append(GTK_BOX(data_box), segment_seperator);
		segment_seperator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
		gtk_box_append(GTK_BOX(raw_box), segment_seperator);
	}

	gtk_notebook_append_page(GTK_NOTEBOOK(window_notebook), data_box_scroll, data_label);
	gtk_notebook_append_page(GTK_NOTEBOOK(window_notebook), raw_box_scroll, raw_label);
	
	free(stringcopy);
	gtk_widget_set_visible(packet_window, true);
}
Jeremias Stotters git repositories generated by CGIT