aboutsummaryrefslogtreecommitdiff
path: root/device.c
blob: ca1e6465d3705a716918df29ffcfab0666f17e21 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 *
 * 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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "shared.h"
#include "interface.h"
#include "template.h"
#include "lua_functions.h"
#include "gui.h"
#include "device.h"
#include "helpers.h"

struct device* start_device = NULL;

void dev_register_lua_function(lua_State* lua_vm, struct device* dev, char* name, lua_CFunction fn) {
	lua_pushlightuserdata(lua_vm, dev);
	lua_pushcclosure(lua_vm, fn, 1);
	lua_setglobal(lua_vm, name);
}

void rename_device(struct device* dev, const char* name) {
	free(dev->name);
	dev->name = malloc(strlen(name)+1);
	strcpy(dev->name, name);
}

struct device* get_dev_in_cfunc(lua_State* lua_vm) {
	return (struct device*)lua_topointer(lua_vm, lua_upvalueindex(1));
}

// Take in all the infos from the caller
int device_send_frame_lua (lua_State *lua_vm) {
	int largc = lua_gettop(lua_vm);
	if(largc != 4) {
		lua_pushliteral(lua_vm, "Incorrect arguments for send_packet");
		lua_error(lua_vm);
		return 0;
	}
	struct device* send_device = get_dev_in_cfunc(lua_vm);

	if(!(lua_isinteger(lua_vm, 1) && lua_isstring(lua_vm, 2) && lua_isinteger(lua_vm, 3))) {
		lua_pushliteral(lua_vm, "An argument passed to send_frame is not of the right type, expected:\n"
		"interface index (integer), frame (string), frame length (integer)");
		lua_error(lua_vm);
		return 0;
	}
	int interface_index = lua_tointeger(lua_vm, 1);
	const char* frame = lua_tostring(lua_vm, 2);
	int frame_len = lua_tointeger(lua_vm, 3);
	const char* frame_type = lua_tostring(lua_vm, 4);

	return interface_send_frame(&send_device->interfaces[interface_index], frame, frame_len, frame_type);
}

// This will get called by a lua device if it wants to know stuff about an interface
int device_get_interface(lua_State* lua_vm) {
	int largc = lua_gettop(lua_vm);
	if(largc != 1) {
		lua_pushliteral(lua_vm, "Incorrect arguments for get_interface");
		lua_error(lua_vm);
		return -1;
	}
	struct device* calling_device = get_dev_in_cfunc(lua_vm);

	if(!lua_isinteger(lua_vm, 1)) {
		lua_pushliteral(lua_vm, "get_interface expects a single integer as an argument!");
		lua_error(lua_vm);
		return -1;
	}
	int interface_index = lua_tointeger(lua_vm, 1);
	if(interface_index >= calling_device->template->slots) {
		lua_pushliteral(lua_vm, "The requested interface is out of range!");
		lua_error(lua_vm);
		return -1;
	}
	struct interface* info_interface = &(calling_device->interfaces[interface_index]);
	// Create the table we'll return
	lua_newtable(lua_vm);
	lua_pushstring(lua_vm, "available");
	// If no template is set no interface is pluged in
	if(!info_interface->template) {
		lua_pushboolean(lua_vm, 0);
		lua_settable(lua_vm, -3);
	} else {
		lua_pushboolean(lua_vm, 1);
		lua_settable(lua_vm, -3);
		// If an interface is connected, get the rest of the infos here
		lua_pushstring(lua_vm, "connected");
		lua_pushboolean(lua_vm, info_interface->connection ? 1 : 0);
		lua_settable(lua_vm, -3);
		// Get the interface mac
		if(lua_getglobal(info_interface->lua_vm, "mac")) {
			// MAC should always be 6, can never be sure tho
			size_t mac_len = 0;
			const char* mac = lua_tolstring(info_interface->lua_vm, -1, &mac_len);
			lua_pushstring(lua_vm, "mac");
			lua_pushlstring(lua_vm, mac, mac_len);
			lua_settable(lua_vm, -3);
		}
	}

	return 1;
}

// Append a line to the buffer of a device
void append_cmdline(struct device* dev, const char* in_string, int in_string_len) {
	GtkTextIter end_iter;
	gtk_text_buffer_get_end_iter(dev->cmd_buffer, &end_iter);
	gtk_text_buffer_place_cursor(dev->cmd_buffer, &end_iter);
	gtk_text_buffer_insert_at_cursor(dev->cmd_buffer, in_string, in_string_len);
	gtk_text_buffer_insert_at_cursor(dev->cmd_buffer, "\n", 1);
}

int lua_append_cmdline(lua_State* lua_vm) {
	int largc = lua_gettop(lua_vm);
	if(largc != 1) {
		lua_pushliteral(lua_vm, "Incorrect arguments for append_cmdline");
		lua_error(lua_vm);
		return -1;
	}
	size_t in_string_len = 0;
	const char* in_string = lua_tolstring(lua_vm, 1, &in_string_len);
	if(!in_string || strchr(in_string, '\n')) {
		lua_pushliteral(lua_vm, "nsprint expects a single line of text");
	}

	struct device* calling_device = get_dev_in_cfunc(lua_vm);

	append_cmdline(calling_device, in_string, in_string_len);
	lua_pushnumber(lua_vm, 0);
	// Look for open windows of this device and update the cmdline if necessary

	return 1;
}

// Setup all the device specific lua stuff
void setup_dev_lua(lua_State *lua_vm, struct device* setup_device) {
	dev_register_lua_function(lua_vm, setup_device, "send_frame", device_send_frame_lua);
	dev_register_lua_function(lua_vm, setup_device, "get_interface", device_get_interface);
	dev_register_lua_function(lua_vm, setup_device, "nsprint", lua_append_cmdline);
	lua_pushinteger(lua_vm, setup_device->template->slots);
	lua_setglobal(lua_vm, "NS_SLOTS_N");
}

void append_device(struct device** start, struct device* dev) {
	dev->next = NULL;
	// Append the device to the end of the linked list
	if(*start == NULL) {
		*start = dev;
	}
	else {
		struct device* last_device = NULL;
		LL_LAST(*start, last_device);
		last_device->next = dev;
	}
	// Connect existing connections
	for(int i = 0; i < dev->template->slots; i++) {
		if(!dev->interfaces[i].connection) continue;
		if(&dev->interfaces[i] == dev->interfaces[i].connection->interface1) {
			dev->interfaces[i].connection->interface2->connection = dev->interfaces[i].connection;
		} else {
			dev->interfaces[i].connection->interface1->connection = dev->interfaces[i].connection;			
		}
	}
	dev->deleted = false;
}

struct device* new_device_from_template_name(struct device** start, char* device_name, const char* template_name, bool create_interfaces) {
	struct dev_template* cur_template = device_templates;
	bool template_found = false;
	for(int i = 0; i < device_template_n; i++, cur_template = &device_templates[i]) {
		if(strcmp(cur_template->name, template_name) == 0) {
			template_found = true;
			break;
		}
	}

	if(!template_found) {
		fprintf(stderr, "Template %s was not found", template_name);
		return *start;
	}
	
	return new_device(start, device_name, cur_template, create_interfaces);
}

lua_State* init_dev_lua_state(struct device* dev) {
	lua_State *lua_vm = luaL_newstate();
	luaL_openlibs(lua_vm);
	setup_dev_lua(lua_vm, dev);
	register_lua_helpers(lua_vm);
	if(!dev->template->kernel) {
		fprintf(stderr, "This device has no kernel\n");
	} else if(luaL_dofile(lua_vm, dev->template->kernel) != 0) {
		fprintf(stderr, "LUA ERROR: %s\n", lua_tostring(lua_vm, -1));
	}
	return lua_vm;
}

// Make sure that when this function is called device_name will stay allocated
// start_device is the first device in the linked list of all devices or 0 when
// this is the first device
// This will insert itself to the start of the linked list!
// create_interfaces should be set to true if this should create the interfaces in the template
struct device* new_device(struct device** start, char* device_name, struct dev_template* cur_template, bool create_interfaces) {
	for(struct device* test_dev = *start; test_dev; test_dev = test_dev->next) {
		if(strcmp(device_name, test_dev->name) == 0) {
			fprintf(stderr, "Device %s already exists\n", device_name);
			return NULL;
		}
	}

	struct device* new_device = malloc_or_abort(sizeof(struct device));
	new_device->template = cur_template;

	new_device->interfaces = calloc_or_abort(sizeof(struct interface), cur_template->slots);

	// Add the default interfaces
	if(create_interfaces)
		for(int i = 0; i < cur_template->slots; i++) {
			char* int_template_name = cur_template->interfaces[i];
			if(int_template_name) {
				new_interface_from_template_name(new_device, i, int_template_name);
			}
		}

	// Copy the name
	new_device->name = malloc_or_abort(strlen(device_name) + 1);
	strcpy(new_device->name, device_name);
	new_device->X = 0;
	new_device->Y = 0;
	new_device->next = NULL;
	new_device->cmd_buffer = gtk_text_buffer_new(NULL);
	g_object_ref(new_device->cmd_buffer);
	new_device->history = calloc_or_abort(sizeof(struct cmd_history), 1);
	//memset(new_device->cmd_buffer, 0, sizeof(char*) * 512);
	//new_device->cur_line = 0;
	append_device(start, new_device);

	// initiate lua vm for this device
	new_device->lua_vm = init_dev_lua_state(new_device);
	
	return new_device;
}

// This function will take the device out of the devices list but not free it and its interfaces
int delete_device(struct device** start, struct device* del_device) {
	// Find the device which is pointing to the device which we want to delete
	struct device* neighbour_device = *start;
	// Make sure the chain of devices stays intact after deleting the device
	if(del_device == *start) {
		*start = del_device->next;
	} else {
		while(neighbour_device->next != NULL && neighbour_device->next != del_device) {
			neighbour_device = neighbour_device->next;
		}
		neighbour_device->next = del_device->next;
	}

	// Disconnect the connections
	for(int i = 0; i < del_device->template->slots; i++) {
		if(!del_device->interfaces[i].connection) continue;
		if(&del_device->interfaces[i] == del_device->interfaces[i].connection->interface1) {
			del_device->interfaces[i].connection->interface2->connection = NULL;
		} else {
			del_device->interfaces[i].connection->interface1->connection = NULL;
		}		
	}
	
	// Mark the device as deleted
	del_device->deleted = true;
	return 0;
}

void append_cmd_history(struct device* dev, const char* text) {
	struct cmd_history* last_entry;
	dev->cmdline_current_index = 0;
	LL_LAST(dev->history, last_entry) {
		dev->cmdline_current_index++;
	}
	struct cmd_history* new_entry = malloc_or_abort(sizeof(struct cmd_history));
	new_entry->prev = last_entry;
	new_entry->next = NULL;
	new_entry->text = malloc(strlen(text) +1);
	strcpy(new_entry->text, text);
	last_entry->next = new_entry;
}

void free_cmd_history(struct cmd_history* hist) {
	if(hist->next) {
		free_cmd_history(hist->next);
	}
	free(hist->text);
}

// This function will free the memory a device uses
int free_device(struct device* del_device) {
	// Go through all the interfaces and delete them
	for(int i = 0; i < del_device->template->slots; i++) {
		delete_interface(&(del_device->interfaces[i]));
	}
	free(del_device->interfaces);
	free(del_device->name);
	// Unreference the commandline buffer
	g_object_unref(del_device->cmd_buffer);

	free_cmd_history(del_device->history);
	
	if(del_device->lua_vm) lua_close(del_device->lua_vm);
	free(del_device);
	return 0;
}

int device_cmdline(struct device* target_device, const char* cmdline) {
	if(!my_lua_call(target_device->lua_vm, "cmdline", 1, LUA_TSTRING, strlen(cmdline), cmdline, -1))
		return -1;

	// Get the result and pop it of the stack
	int result = (int)lua_tointeger(target_device->lua_vm, -1);
	lua_pop(target_device->lua_vm, 1);

	// Call post_cmdline for prompts and stuff
	my_lua_call(target_device->lua_vm, "post_cmdline", 0, -1);
	return result;
}

struct device* dev_from_name(struct device* start, const char* name) {
	LL_FOR(struct device, start, dev) {
		if(strcmp(dev->name, name) == 0) return dev;
	}
	return NULL;
}

// Reload all the templates lua files
void reload_templates() {
	LL_FOR(struct device, start_device, dev) {
		// Ask the device to save
		if(my_lua_call(dev->lua_vm, "save_state", 1, -1)) {
			size_t state_len = 0;
			const char* state = lua_tolstring(dev->lua_vm, -1, &state_len);

			// Destroy the lua vm
			lua_close(dev->lua_vm);
			// Create the new vm
			dev->lua_vm = init_dev_lua_state(dev);
			// Load the state in
			
			my_lua_call(dev->lua_vm, "load_state", 1, LUA_TSTRING, state_len, state, -1);
		}
	}
	// @todo also add something like this for interfaces
}
Jeremias Stotters git repositories generated by CGIT