about summary refs log tree commit diff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/client/ec_client.c47
-rw-r--r--src/engine/e_config.c37
-rw-r--r--src/engine/e_config.h4
-rw-r--r--src/engine/e_config_variables.h4
-rw-r--r--src/engine/e_console.c25
-rw-r--r--src/engine/e_console.h3
-rw-r--r--src/engine/e_engine.c45
-rw-r--r--src/engine/e_engine.h6
-rw-r--r--src/engine/e_if_client.h44
-rw-r--r--src/engine/e_if_modc.h12
-rw-r--r--src/engine/e_protocol.h23
-rw-r--r--src/engine/e_ringbuffer.c2
-rw-r--r--src/engine/server/es_register.c2
-rw-r--r--src/engine/server/es_server.c85
14 files changed, 264 insertions, 75 deletions
diff --git a/src/engine/client/ec_client.c b/src/engine/client/ec_client.c
index 0ddcf7d9..56c817a6 100644
--- a/src/engine/client/ec_client.c
+++ b/src/engine/client/ec_client.c
@@ -58,6 +58,7 @@ static int snapcrcerrors = 0;
 
 static int ack_game_tick = -1;
 static int current_recv_tick = 0;
+static int rcon_authed = 0;
 
 /* pinging */
 static int64 ping_start_time = 0;
@@ -323,10 +324,23 @@ static void client_send_ready()
 	client_send_msg();
 }
 
+int client_rcon_authed()
+{
+	return rcon_authed;
+}
+
+void client_rcon_auth(const char *name, const char *password)
+{
+	msg_pack_start_system(NETMSG_RCON_AUTH, MSGFLAG_VITAL);
+	msg_pack_string(name, 32);
+	msg_pack_string(password, 32);
+	msg_pack_end();
+	client_send_msg();
+}
+
 void client_rcon(const char *cmd)
 {
-	msg_pack_start_system(NETMSG_CMD, MSGFLAG_VITAL);
-	msg_pack_string(config.rcon_password, 32);
+	msg_pack_start_system(NETMSG_RCON_CMD, MSGFLAG_VITAL);
 	msg_pack_string(cmd, 256);
 	msg_pack_end();
 	client_send_msg();
@@ -470,17 +484,18 @@ void client_connect(const char *server_address_str)
 	if(net_host_lookup(buf, port, &server_address) != 0)
 		dbg_msg("client", "could not find the address of %s, connecting to localhost", buf);
 	
+	rcon_authed = 0;
 	netclient_connect(net, &server_address);
 	client_set_state(CLIENTSTATE_CONNECTING);
 	
 	graph_init(&intra_graph, 0.0f, 1.0f);
 	graph_init(&input_late_graph, 0.0f, 1.0f);
 	graph_init(&predict_graph, 0.0f, 200.0f);
-	
 }
 
 void client_disconnect_with_reason(const char *reason)
 {
+	rcon_authed = 0;
 	netclient_disconnect(net, reason);
 	client_set_state(CLIENTSTATE_OFFLINE);
 	map_unload();
@@ -722,7 +737,7 @@ static void client_process_packet(NETPACKET *packet)
 		if(sys)
 		{
 			/* system message */
-			if(msg == NETMSG_MAP)
+			if(msg == NETMSG_MAP_CHANGE)
 			{
 				const char *map = msg_unpack_string();
 				int map_crc = msg_unpack_int();
@@ -830,6 +845,21 @@ static void client_process_packet(NETPACKET *packet)
 				msg_pack_end();
 				client_send_msg();
 			}
+			else if(msg == NETMSG_RCON_AUTH_STATUS)
+			{
+				int result = msg_unpack_int();
+				if(msg_unpack_error() == 0)
+					rcon_authed = result;
+			}
+			else if(msg == NETMSG_RCON_LINE)
+			{
+				const char *line = msg_unpack_string();
+				if(msg_unpack_error() == 0)
+				{
+					/*dbg_msg("remote", "%s", line);*/
+					modc_rcon_line(line);
+				}
+			}
 			else if(msg == NETMSG_PING_REPLY)
 				dbg_msg("client/network", "latency %.2f", (time_get() - ping_start_time)*1000 / (float)time_freq());
 			else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPSINGLE || msg == NETMSG_SNAPEMPTY)
@@ -1451,7 +1481,12 @@ int main(int argc, char **argv)
 	/* run the client*/
 	client_run();
 	
-	/* write down the config and quit */	
-	engine_writeconfig();
+	/* write down the config and quit */
+	if(engine_config_write_start() == 0)
+	{
+		config_save();
+		engine_config_write_stop();
+	}
+	
 	return 0;
 }
diff --git a/src/engine/e_config.c b/src/engine/e_config.c
index e71d2ad3..9cef342f 100644
--- a/src/engine/e_config.c
+++ b/src/engine/e_config.c
@@ -7,6 +7,7 @@
 #include "e_system.h"
 #include "e_config.h"
 #include "e_linereader.h"
+#include "e_engine.h"
 
 CONFIGURATION config;
 
@@ -63,6 +64,7 @@ void config_set(const char *line)
 
 void config_load(const char *filename)
 {
+	/*
 	IOHANDLE file;
 	dbg_msg("config/load", "loading %s", filename);
 	file = io_open(filename, IOFLAG_READ);
@@ -77,37 +79,20 @@ void config_load(const char *filename)
 			config_set(line);
 
 		io_close(file);
-	}
+	}*/
 }
 
-void config_save(const char *filename)
+void config_save()
 {
-	IOHANDLE file;
-	dbg_msg("config/save", "saving config to %s", filename);
+	char linebuf[512];
+	
+	#define MACRO_CONFIG_INT(name,def,min,max) { str_format(linebuf, sizeof(linebuf), "%s %i", #name, config.name); engine_config_write_line(linebuf); }
+	#define MACRO_CONFIG_STR(name,len,def) { str_format(linebuf, sizeof(linebuf), "%s \"%s\"", #name, config.name); engine_config_write_line(linebuf); }
 
-	file = io_open(filename, IOFLAG_WRITE);
+	#include "e_config_variables.h" 
 
-	if(file)
-	{
-#if defined(CONF_FAMILY_WINDOWS)
-		const char newline[] = "\r\n";
-#else
-		const char newline[] = "\n";
-#endif
-		const int newline_len = sizeof(newline)-1;
-		
-    	#define MACRO_CONFIG_INT(name,def,min,max) { char str[256]; str_format(str, sizeof(str), "%s=%i%s", #name, config.name, newline); io_write(file, str, strlen(str)); }
-    	#define MACRO_CONFIG_STR(name,len,def) { io_write(file, #name, strlen(#name)); io_write(file, "=", 1); io_write(file, config.name, strlen(config.name)); io_write(file, newline, newline_len); }
- 
-    	#include "e_config_variables.h" 
- 
-    	#undef MACRO_CONFIG_INT 
-    	#undef MACRO_CONFIG_STR 
-
-		io_close(file);
-	}
-	else
-		dbg_msg("config/save", "couldn't open %s for writing. :(", filename);
+	#undef MACRO_CONFIG_INT 
+	#undef MACRO_CONFIG_STR 
 }
 
 #define MACRO_CONFIG_INT(name,def,min,max) int config_get_ ## name (CONFIGURATION *c) { return c->name; }
diff --git a/src/engine/e_config.h b/src/engine/e_config.h
index 78ffdd02..514a6ceb 100644
--- a/src/engine/e_config.h
+++ b/src/engine/e_config.h
@@ -20,8 +20,8 @@ extern CONFIGURATION config;
 void config_init();
 void config_set(const char *line);
 void config_reset();
-void config_load(const char *filename);
-void config_save(const char *filename);
+/*void config_load(const char *filename);*/
+void config_save();
 
 typedef int (*CONFIG_INT_GETTER)(CONFIGURATION *c);
 typedef const char *(*CONFIG_STR_GETTER)(CONFIGURATION *c);
diff --git a/src/engine/e_config_variables.h b/src/engine/e_config_variables.h
index 01b35dc0..9b3f5cf3 100644
--- a/src/engine/e_config_variables.h
+++ b/src/engine/e_config_variables.h
@@ -5,7 +5,6 @@
 MACRO_CONFIG_STR(player_name, 32, "nameless tee")
 MACRO_CONFIG_STR(clan_name, 32, "")
 MACRO_CONFIG_STR(password, 32, "")
-MACRO_CONFIG_STR(rcon_password, 32, "")
 
 MACRO_CONFIG_INT(cl_cpu_throttle, 0, 0, 1)
 /*MACRO_CONFIG_STR(cl_connect, 32, "")*/
@@ -52,12 +51,13 @@ MACRO_CONFIG_STR(sv_name, 128, "unnamed server")
 MACRO_CONFIG_STR(sv_bindaddr, 128, "")
 MACRO_CONFIG_INT(sv_port, 8303, 0, 0)
 MACRO_CONFIG_INT(sv_external_port, 0, 0, 0)
-MACRO_CONFIG_INT(sv_sendheartbeats, 1, 0, 1)
 MACRO_CONFIG_STR(sv_map, 128, "dm1")
 MACRO_CONFIG_INT(sv_map_reload, 0, 0, 1)
 MACRO_CONFIG_INT(sv_max_clients, 8, 1, 12)
 MACRO_CONFIG_INT(sv_high_bandwidth, 0, 0, 1)
 MACRO_CONFIG_INT(sv_status, 0, 0, 1)
+MACRO_CONFIG_INT(sv_register, 1, 0, 1)
+MACRO_CONFIG_STR(sv_rcon_password, 32, "")
 
 MACRO_CONFIG_INT(debug, 0, 0, 1)
 MACRO_CONFIG_INT(dbg_stress, 0, 0, 0)
diff --git a/src/engine/e_console.c b/src/engine/e_console.c
index db0398b4..27add750 100644
--- a/src/engine/e_console.c
+++ b/src/engine/e_console.c
@@ -1,6 +1,7 @@
 #include "e_system.h"
 #include "e_console.h"
 #include "e_config.h"
+#include "e_linereader.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -339,7 +340,7 @@ void console_print(const char *str)
 		print_callback(str);
 }
 
-void console_execute(const char *str)
+void console_execute_line(const char *str)
 {
 	LEXER_RESULT result;
 	int error;
@@ -374,6 +375,28 @@ void console_execute(const char *str)
 	}
 }
 
+void console_execute_file(const char *filename)
+{
+	IOHANDLE file;
+	file = io_open(filename, IOFLAG_READ);
+	
+	if(file)
+	{
+		char *line;
+		LINEREADER lr;
+		
+		dbg_msg("console", "executing '%s'", filename);
+		linereader_init(&lr, file);
+
+		while((line = linereader_get(&lr)))
+			console_execute_line(line);
+
+		io_close(file);
+	}
+	else
+		dbg_msg("console", "failed to open '%s'", filename);
+}
+
 static void echo_command(void *result, void *user_data)
 {
 	const char *str;
diff --git a/src/engine/e_console.h b/src/engine/e_console.h
index 10d77a3a..730779f8 100644
--- a/src/engine/e_console.h
+++ b/src/engine/e_console.h
@@ -18,7 +18,8 @@ typedef struct COMMAND_t
 
 void console_init();
 void console_register(COMMAND *cmd);
-void console_execute(const char *str);
+void console_execute_line(const char *str);
+void console_execute_file(const char *filename);
 void console_print(const char *str);
 void console_register_print_callback(void (*callback)(const char *));
 
diff --git a/src/engine/e_engine.c b/src/engine/e_engine.c
index 6fe61efe..04094573 100644
--- a/src/engine/e_engine.c
+++ b/src/engine/e_engine.c
@@ -78,7 +78,7 @@ void engine_parse_arguments(int argc, char **argv)
 	/* load the configuration */
 	int i;
 	int abs = 0;
-	const char *config_filename = "default.cfg";
+	const char *config_filename = "settings.cfg";
 	char buf[1024];
 	for(i = 1; i < argc; i++)
 	{
@@ -91,15 +91,15 @@ void engine_parse_arguments(int argc, char **argv)
 	}
 
 	if(abs)
-		config_load(config_filename);
+		console_execute_file(config_filename);
 	else
-		config_load(engine_savepath(config_filename, buf, sizeof(buf)));
+		console_execute_file(engine_savepath(config_filename, buf, sizeof(buf)));
 	
 	/* search arguments for overrides */
 	{
 		int i;
 		for(i = 1; i < argc; i++)
-			config_set(argv[i]);
+			console_execute_line(argv[i]);
 	}
 	
 	/* set default servers and load from disk*/
@@ -107,12 +107,43 @@ void engine_parse_arguments(int argc, char **argv)
 	mastersrv_load();
 }
 
-void engine_writeconfig()
+
+static IOHANDLE config_file = 0;
+
+int engine_config_write_start()
 {
-	char buf[1024];
-	config_save(engine_savepath("default.cfg", buf, sizeof(buf)));
+	char filename[1024];
+	config_save(engine_savepath("settings.cfg", filename, sizeof(filename)));
+	
+	config_file = io_open(filename, IOFLAG_WRITE);
+	if(config_file == 0)
+		return -1;
+	return 0;
 }
 
+void engine_config_write_line(const char *line)
+{
+	if(config_file)
+	{
+#if defined(CONF_FAMILY_WINDOWS)
+		static const char newline[] = "\r\n";
+#else
+		static const char newline[] = "\n";
+#endif
+		io_write(config_file, line, strlen(line));
+		io_write(config_file, newline, sizeof(newline)-1);
+	}
+}
+
+void engine_config_write_stop()
+{
+	io_close(config_file);
+	config_file = 0;
+}
+/*
+void engine_writeconfig()
+{
+}*/
 
 static int perf_tick = 1;
 static PERFORMACE_INFO *current = 0;
diff --git a/src/engine/e_engine.h b/src/engine/e_engine.h
index 6818d96d..7b5baec9 100644
--- a/src/engine/e_engine.h
+++ b/src/engine/e_engine.h
@@ -3,7 +3,11 @@
 const char *engine_savepath(const char *filename, char *buffer, int max);
 void engine_init(const char *appname);
 void engine_parse_arguments(int argc, char **argv);
-void engine_writeconfig();
+
+int engine_config_write_start();
+void engine_config_write_line(const char *line);
+void engine_config_write_stop();
+
 int engine_stress(float probability);
 
 
diff --git a/src/engine/e_if_client.h b/src/engine/e_if_client.h
index 9173cfe1..e921f9e9 100644
--- a/src/engine/e_if_client.h
+++ b/src/engine/e_if_client.h
@@ -319,9 +319,40 @@ void client_entergame();
 	
 	Remarks:
 		The client must have the correct rcon password to connect.
+
+ 	See Also:
+		<client_rcon_auth, client_rcon_authed>
 */
 void client_rcon(const char *cmd);
 
+/*
+	Function: client_rcon_auth
+		TODO
+	
+	Arguments:
+		arg1 - desc
+	
+	Returns:
+
+	See Also:
+		<client_rcon, client_rcon_authed>
+*/
+void client_rcon_auth(const char *name, const char *password);
+
+/*
+	Function: client_rcon_authed
+		TODO
+	
+	Arguments:
+		arg1 - desc
+	
+	Returns:
+
+	See Also:
+		<client_rcon, client_rcon_auth>
+*/
+int client_rcon_authed();
+
 /**********************************************************************************
 	Group: Other
 **********************************************************************************/
@@ -414,5 +445,18 @@ int client_mapdownload_amount();
 */
 int client_mapdownload_totalsize();
 
+/*
+	Function: client_save_line
+		TODO
+	
+	Arguments:
+		arg1 - desc
+	
+	Returns:
+
+	See Also:
+		<other_func>
+*/
+void client_save_line(const char *line);
 
 #endif
diff --git a/src/engine/e_if_modc.h b/src/engine/e_if_modc.h
index 33069a6f..8839d5f1 100644
--- a/src/engine/e_if_modc.h
+++ b/src/engine/e_if_modc.h
@@ -12,6 +12,18 @@
 void modc_console_init();
 
 /*
+	Function: modc_rcon_line
+		TODO
+*/
+void modc_rcon_line(const char *line);
+
+/*
+	Function: modc_save_config
+		TODO
+*/
+void modc_save_config();
+
+/*
 	Function: modc_init
 		Called when the client starts.
 	
diff --git a/src/engine/e_protocol.h b/src/engine/e_protocol.h
index a0213483..0bca00a4 100644
--- a/src/engine/e_protocol.h
+++ b/src/engine/e_protocol.h
@@ -33,19 +33,22 @@ enum
 	NETMSG_INFO=1,
 	
 	/* sent by server */
-	NETMSG_MAP,
-	NETMSG_MAP_DATA,
-	NETMSG_SNAP,
-	NETMSG_SNAPEMPTY,
-	NETMSG_SNAPSINGLE,
-	NETMSG_SNAPSMALL,
+	NETMSG_MAP_CHANGE,		/* sent when client should switch map */
+	NETMSG_MAP_DATA,		/* map transfer, contains a chunk of the map file */
+	NETMSG_SNAP,			/* normal snapshot, multiple parts */
+	NETMSG_SNAPEMPTY,		/* empty snapshot */
+	NETMSG_SNAPSINGLE,		/* ? */
+	NETMSG_SNAPSMALL,		/* */
+	NETMSG_RCON_AUTH_STATUS,/* result of the authentication */
+	NETMSG_RCON_LINE,		/* line that should be printed to the remote console */
 	
 	/* sent by client */
-	NETMSG_READY,
+	NETMSG_READY,			/* */
 	NETMSG_ENTERGAME,
-	NETMSG_INPUT,
-	NETMSG_CMD,
-	NETMSG_REQUEST_MAP_DATA,
+	NETMSG_INPUT,			/* contains the inputdata from the client */
+	NETMSG_RCON_CMD,		/* */ 
+	NETMSG_RCON_AUTH,		/* */
+	NETMSG_REQUEST_MAP_DATA,/* */
 	
 	/* sent by both */
 	NETMSG_PING,
diff --git a/src/engine/e_ringbuffer.c b/src/engine/e_ringbuffer.c
index 02350140..6a9c12f6 100644
--- a/src/engine/e_ringbuffer.c
+++ b/src/engine/e_ringbuffer.c
@@ -215,5 +215,7 @@ void *ringbuf_first(RINGBUFFER *rb)
 
 void *ringbuf_last(RINGBUFFER *rb)
 {
+	if(rb->last_alloc == 0)
+		return 0;
 	return rb->last_alloc+1;
 }
diff --git a/src/engine/server/es_register.c b/src/engine/server/es_register.c
index 65c56821..47712350 100644
--- a/src/engine/server/es_register.c
+++ b/src/engine/server/es_register.c
@@ -88,7 +88,7 @@ void register_update()
 	int64 now = time_get();
 	int64 freq = time_freq();
 	
-	if(!config.sv_sendheartbeats)
+	if(!config.sv_register)
 		return;
 	
 	mastersrv_update();
diff --git a/src/engine/server/es_server.c b/src/engine/server/es_server.c
index 9fe75e39..a177ced8 100644
--- a/src/engine/server/es_server.c
+++ b/src/engine/server/es_server.c
@@ -98,6 +98,7 @@ typedef struct
 	char name[MAX_NAME_LENGTH];
 	char clan[MAX_CLANNAME_LENGTH];
 	int score;
+	int authed;
 } CLIENT;
 
 static CLIENT clients[MAX_CLIENTS];
@@ -511,6 +512,7 @@ static int new_client_callback(int cid, void *user)
 	clients[cid].last_acked_snapshot = -1;
 	clients[cid].snap_rate = SRVCLIENT_SNAPRATE_INIT;
 	clients[cid].score = 0;
+	clients[cid].authed = 0;
 	return 0;
 }
 
@@ -526,19 +528,45 @@ static int del_client_callback(int cid, void *user)
 	clients[cid].state = SRVCLIENT_STATE_EMPTY;
 	clients[cid].name[0] = 0;
 	clients[cid].clan[0] = 0;
+	clients[cid].authed = 0;
 	snapstorage_purge_all(&clients[cid].snapshots);
 	return 0;
 }
 
 static void server_send_map(int cid)
 {
-	msg_pack_start_system(NETMSG_MAP, MSGFLAG_VITAL);
+	msg_pack_start_system(NETMSG_MAP_CHANGE, MSGFLAG_VITAL);
 	msg_pack_string(config.sv_map, 0);
 	msg_pack_int(current_map_crc);
 	msg_pack_end();
 	server_send_msg(cid);
 }
 
+static void server_send_rcon_line(int cid, const char *line)
+{
+	msg_pack_start_system(NETMSG_RCON_LINE, MSGFLAG_VITAL);
+	msg_pack_string(line, 512);
+	msg_pack_end();
+	server_send_msg(cid);
+}
+
+static void server_send_rcon_line_authed(const char *line)
+{
+	static volatile int reentry_guard = 0;
+	int i;
+	
+	if(reentry_guard) return;
+	reentry_guard++;
+	
+	for(i = 0; i < MAX_CLIENTS; i++)
+	{
+		if(clients[i].state != SRVCLIENT_STATE_EMPTY && clients[i].authed)
+			server_send_rcon_line(i, line);
+	}
+	
+	reentry_guard--;
+}
+
 static void server_process_client_packet(NETPACKET *packet)
 {
 	int cid = packet->client_id;
@@ -658,14 +686,43 @@ static void server_process_client_packet(NETPACKET *packet)
 			/* call the mod with the fresh input data */
 			mods_client_direct_input(cid, clients[cid].latestinput.data);
 		}
-		else if(msg == NETMSG_CMD)
+		else if(msg == NETMSG_RCON_CMD)
 		{
-			const char *pw = msg_unpack_string();
 			const char *cmd = msg_unpack_string();
-			if(config.rcon_password[0] != 0 && strcmp(pw, config.rcon_password) == 0)
+			
+			if(msg_unpack_error() == 0 && clients[cid].authed)
 			{
 				dbg_msg("server", "cid=%d rcon='%s'", cid, cmd);
-				console_execute(cmd);
+				console_execute_line(cmd);
+			}
+		}
+		else if(msg == NETMSG_RCON_AUTH)
+		{
+			const char *pw;
+			msg_unpack_string(); /* login name, not used */
+			pw = msg_unpack_string();
+			
+			if(msg_unpack_error() == 0)
+			{
+				if(config.sv_rcon_password[0] == 0)
+				{
+					server_send_rcon_line(cid, "No rcon password set on server. Set sv_rcon_password to enable the remote console.");
+				}
+				else if(strcmp(pw, config.sv_rcon_password) == 0)
+				{
+					msg_pack_start_system(NETMSG_RCON_AUTH_STATUS, MSGFLAG_VITAL);
+					msg_pack_int(1);
+					msg_pack_end();
+					server_send_msg(cid);
+					
+					clients[cid].authed = 1;
+					dbg_msg("server", "cid=%d authed", cid);
+					server_send_rcon_line(cid, "Authentication successful. Remote console access granted.");
+				}
+				else
+				{
+					server_send_rcon_line(cid, "Wrong password.");
+				}
 			}
 		}
 		else if(msg == NETMSG_PING)
@@ -838,8 +895,10 @@ static int server_run()
 	NETADDR4 bindaddr;
 
 	net_init();
-	
 	snap_init_id();
+	
+	/* */
+	console_register_print_callback(server_send_rcon_line_authed);
 
 	/* load map */
 	if(!server_load_map(config.sv_map))
@@ -1022,16 +1081,6 @@ static int server_run()
 			
 			/* wait for incomming data */
 			net_socket_read_wait(netserver_socket(net), 5);
-	
-			/*
-			if(config.dbg_hitch)
-			{
-				thread_sleep(config.dbg_hitch);
-				config.dbg_hitch = 0;
-			}
-			else
-				thread_sleep(1);
-			*/
 		}
 	}
 
@@ -1043,10 +1092,10 @@ static int server_run()
 
 static void server_register_commands()
 {
-	
+	/* kick */
+	/* status */
 }
 
-
 int main(int argc, char **argv)
 {
 	/* init the engine */