summaryrefslogtreecommitdiff
path: root/src/server/publicServer.c
blob: 01f65cbc34c0af1bbb743a4c7fdbd5a88191972f (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <time.h>

#ifndef __WIN32__
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#else /* __WIN32__ */
#include <windows.h>
#include <wininet.h>
#endif /* __WIN32__ */

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <sys/types.h> 
#include <sys/stat.h> 

#include "main.h"
#include "list.h"
#include "tux.h"
#include "idManager.h"
#include "myTimer.h"
#include "arena.h"
#include "item.h"
#include "shot.h"
#include "arenaFile.h"
#include "proto.h"
#include "modules.h"
#include "net_multiplayer.h"

#include "publicServer.h"
#include "serverConfigFile.h"
#include "highScore.h"
#include "log.h"

#include "dns.h"

static arena_t *arena;
static bool_t isSignalEnd;
static arenaFile_t *choice_arenaFile;

extern int errno;

#define	__PACKED__ __attribute__ ((__packed__))

void world_inc_round()
{
}

char *public_server_get_setting(char *env, char *param, char *default_val)
{
	return getParamElse(param, server_configFile_get_value(env, default_val));
}

/**
 * Send the process to the background
 */
static void daemonize()
{
#ifndef __WIN32__
	pid_t pid, sid;

	FILE *pid_file;

	/* are we already a daemon? */
	if (getppid() == 1) {
		return;
	}

	/* fork off the parent process */
	pid = fork();
	if (pid < 0) {
		exit(1);
	/* if we got a good PID, then we can exit the parent process */
	} else if (pid > 0) {
		exit(0);
	}

	/* ↓ at this point we are executing as the child process ↓ */

	/* change the file mode mask */
	umask(027);

	/* create a new SID for the child process */
	sid = setsid();
	if (sid < 0) {
		exit(1);
	}

	/* Change the current working directory. This prevents the current
	 * directory from being locked; hence not being able to remove it.
	 * /tmp seems to be the best place where to create temporary files ;c)
	 */
	if (chdir("/tmp") < 0) {
		exit(1);
	}

	/* say loudly what PID we got */
	printf("The Tuxanci game server started with the PID %d\n", sid);

	/* write the PID to the PID file */
	pid_file = fopen(public_server_get_setting("PID_FILE", "--pid-file", "/var/run/tuxanci-server.pid"), "w");
	fprintf(pid_file, "%d\n", sid);
	fclose(pid_file);

	/* redirect standard files to /dev/null */
	freopen("/dev/null", "r", stdin);
	freopen("/dev/null", "w", stdout);
	freopen("/dev/null", "w", stderr);
#endif /* __WIN32__ */
}

static int public_server_register()
{
#ifndef __WIN32__
	int s;
#else /* __WIN32__ */
	SOCKET s;
#endif /* __WIN32__ */

	struct sockaddr_in server;
	char *master_server_ip;

	master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN);

	if (master_server_ip == NULL) {
		warning("Did not obtained IP of the MasterServer");
		return -1;
	}

	server.sin_family = AF_INET;
	server.sin_port = htons(NET_MASTER_PORT);
	server.sin_addr.s_addr = inet_addr(master_server_ip);

	free(master_server_ip);

	if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		warning("Unable to open socket for connection to MasterServer");
		return -1;
	}

	/* connect to MasterServer */
	if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
		warning("Unable to estabilish connection to MasterServer");
		return -1;
	}

	typedef struct {
		unsigned char cmd;
		unsigned port;
		unsigned ip;
	} __PACKED__ register_head;

	register_head *head = (register_head *) malloc(sizeof(register_head));

	head->cmd = 'p';
	head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800"));
	head->ip = 0;				/* TODO */

	/* send request for server list */
	int r = send(s, head, sizeof(register_head), 0);

	free(head);

#ifndef __WIN32__
	close(s);
#else /* __WIN32__ */
	closesocket(s);
#endif /* __WIN32__ */

	if (r == -1) {
		return -1;
	} else {
		return 0;
	}
}

static int public_server_initNetwork()
{
	char ip4[STR_IP_SIZE];
	char ip6[STR_IP_SIZE];
	char *p_ip4, *p_ip6;
	int port;
	int ret;

	ret = -1;

	strcpy(ip4, public_server_get_setting("IP4", "--ip4", "none"));
	strcpy(ip6, public_server_get_setting("IP6", "--ip6", "none"));

	p_ip4 = ip4;

	if (strcmp(ip4, "none") == 0 || strcmp(ip6, "::") == 0) {
		p_ip4 = NULL;
	}

	p_ip6 = ip6;

	if (strcmp(ip6, "none") == 0) {
		p_ip6 = NULL;
	}

	port = atoi(public_server_get_setting("PORT", "--port", "6800"));

	ret = net_multiplayer_init_for_game_server(p_ip4, p_ip6, port);

	return ret;
}

static void load_arena()
{
	choice_arenaFile = arena_file_get_file_format_net_name(public_server_get_setting("ARENA", "--arena", "FAGN"));

	if (choice_arenaFile == NULL) {
		error("Unable to load arena [%s]", public_server_get_setting("ARENA", "--arena", "FAGN"));
		/* TODO: Why not to log it? */
		exit(-1);
	}

	arena = arena_file_get_arena(choice_arenaFile);

	arena_set_current(arena);
}

int public_server_init()
{
	int ret;
	int i;

	id_init_list();
	module_init();
	arena_file_init();
	tux_init();
	item_init();
	shot_init();
	server_configFile_init();

	ret = log_init(public_server_get_setting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log"));

	if (ret < 0) {
		/* Error message has been already printed */
		return -1;
	}

	high_score_init(public_server_get_setting("SCORE_FILE", "--score-file", "/tmp/highscore.txt"));

	load_arena();

	for (i = 0; i < atoi(public_server_get_setting("ITEM", "--item", "10")); i++) {
		item_add_new_item(arena->spaceItem, ID_UNKNOWN);
	}

	isSignalEnd = FALSE;

	ret = public_server_initNetwork();

	if (ret < 0) {
		error("Unable to initialize network socket");
		/* TODO: Why not to log it? */
		return -1;
	}

	server_set_max_clients(atoi(public_server_get_setting("MAX_CLIENTS", "--max-clients", "32")));

	if (atoi(public_server_get_setting("LAN_ONLY", "--lan-only", "0")) == 0) {
		if (public_server_register() < 0) {
			error("Unable to contact MasterServer");
			/* TODO: Why not to log it? */
		}
	} else {
		debug("Starting LAN-only server");
	}

	return 0;
}

arenaFile_t *choice_arena_get()
{
	return choice_arenaFile;
}

void public_server_event()
{
	static my_time_t lastActive = 0;
	my_time_t interval;

	net_multiplayer_event();

	if (isSignalEnd == TRUE) {
		public_server_quit();
	}

	if (lastActive == 0) {
		lastActive = timer_get_current_time();
	}

	interval = timer_get_current_time() - lastActive;

	if (interval < 50) {
		return;
	}

	lastActive = timer_get_current_time();

	arena_event(arena);
}

void my_handler_quit(int n)
{
	debug("Received signal %d", n);
	/* TODO: Why not to log it? */

	isSignalEnd = TRUE;

	public_server_quit();
}

void public_server_quit()
{
	debug("Shutting down the Tuxanci game server");
	/* TODO: Why not to log it? */

	net_multiplayer_quit();
	arena_destroy(arena);

	tux_quit();
	item_quiy();
	shot_quit();

	arena_file_quit();
	server_configFile_quit();
	module_quit();
	id_quit_list();
	high_score_quit();
	log_quit();

	exit(0);
}

int public_server_start()
{
#ifndef __WIN32__
	signal(SIGINT, my_handler_quit);
	signal(SIGTERM, my_handler_quit);
	signal(SIGQUIT, my_handler_quit);
#endif /* __WIN32__ */
	if (public_server_init() < 0) {
		public_server_quit();
		return -1;
	}

	char *s = public_server_get_setting("DAEMON", "--daemon", "0");

	if (atoi(s)) {
		daemonize();
	}

	for (;;) {
		public_server_event();
	}

	return 0;
}