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
371
372
|
#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
# include <windows.h>
# include <wininet.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <errno.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 "heightScore.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 countRoundInc()
{
}
char *getSetting(char *env, char *param, char *default_val)
{
return getParamElse(param, getServerConfigFileValue(env, default_val) );
}
static int registerPublicServer()
{
#ifndef __WIN32
int s;
#else
SOCKET s;
#endif
/* TODO: dodelat TCP makro */
struct sockaddr_in server;
char *master_server_ip;
master_server_ip = getIPFormDNS(NET_MASTER_SERVER_DOMAIN);
//printf("master_server_ip = %s\n", master_server_ip);
if( master_server_ip == NULL ) // master server is down
{
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, 0)) < 0) {
return 0;
}
/* Set to nonblocking socket mode */
#ifndef __WIN32__
int oldFlag;
oldFlag = fcntl (s, F_GETFL, 0);
if( fcntl(s, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
{
return -1;
}
#else
unsigned long arg = 1;
// Operation is FIONBIO. Parameter is pointer on non-zero number.
if( ioctlsocket(s, FIONBIO, &arg) == SOCKET_ERROR )
{
WSACleanup();
return -1;
}
#endif
if( connect (s, (struct sockaddr *) &server, sizeof (server)) == -1 ) {
#ifndef __WIN32__
if (errno != EINPROGRESS)
return -1;
#else
if (WSAGetLastError() != WSAEWOULDBLOCK) {
WSACleanup();
return -1;
}
#endif
}
struct timeval tv;
fd_set myset;
FD_ZERO(&myset);
FD_SET(s, &myset);
tv.tv_sec = 3;
tv.tv_usec = 0;
int ret = select (s+1, NULL, &myset, NULL, &tv);
if (ret == -1)
return -1;
if (ret == 0)
return -1;
FD_ZERO(&myset);
FD_SET(s, &myset);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select (s+1, NULL, &myset, NULL, &tv);
if (ret == -1)
return -1;
if (ret == 0)
return -1;
typedef struct {
unsigned char cmd;
unsigned port;
unsigned ip;
} __PACKED__ register_head;
char *str = (char *) malloc (sizeof (register_head));
register_head *head = (register_head *) str;
head->cmd = 'p';
head->port = atoi( getSetting("PORT4", "--port4", "2200") );
head->ip = 0; // TODO
/* send request for server list */
int r = send (s, str, 9, 0);
free (str);
if (r == -1) {
#ifndef __WIN32
close(s);
#else
closesocket(s);
#endif
return -1;
}
#ifndef __WIN32
close(s);
#else
closesocket(s);
#endif
return 0;
}
static int initPublicServerNetwork()
{
char ip4[STR_IP_SIZE];
char ip6[STR_IP_SIZE];
char *p_ip4, *p_ip6;
int port4;
int port6;
int ret;
ret = -1;
strcpy(ip4, getSetting("IP4", "--ip4", "none" ) );
strcpy(ip6, getSetting("IP6", "--ip6", "none" ) );
p_ip4 = ip4;
if( strcmp(ip4, "none") == 0 )
{
p_ip4 = NULL;
}
p_ip6 = ip6;
if( strcmp(ip6, "none") == 0 )
{
p_ip6 = NULL;
}
port4 = atoi( getSetting("PORT4", "--port4", "2200") );
port6 = atoi( getSetting("PORT6", "--port6", "2200") );
//ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6);
ret = initNetMuliplayerForGameServer(p_ip4, port4, p_ip6, port6);
return ret;
}
static void loadArena()
{
choice_arenaFile = getArenaFileFormNetName( getSetting("ARENA", "--arena", "FAGN") );
arena = getArena(choice_arenaFile);
setCurrentArena(arena);
}
int initPublicServer()
{
int ret;
int i;
initListID();
initModule();
initArenaFile();
initTux();
initItem();
initShot();
initServerConfigFile();
ret = initLog(getSetting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log") );
if( ret < 0 )
{
fprintf(stderr, _("I was unable to open config file!\n"));
return -1;
}
initHeightScore( getSetting("SCORE_FILE", "--score-file", "/tmp/heightscore") );
loadArena();
for( i = 0 ; i < atoi(getSetting("ITEM", "--item", "10")) ; i++ )
{
addNewItem(arena->spaceItem, ID_UNKNOWN);
}
isSignalEnd = FALSE;
ret = initPublicServerNetwork();
if( ret < 0 )
{
printf(_("Unable to initialize network socket!\n"));
return -1;
}
setServerMaxClients( atoi( getSetting("MAX_CLIENTS", "--max-clients", "32") ));
if (registerPublicServer() < 0)
{
printf(_("Unable to contact MasterServer!)\n"));
}
return 0;
}
arenaFile_t* getChoiceArena()
{
return choice_arenaFile;
}
void eventPublicServer()
{
static my_time_t lastActive = 0;
my_time_t interval;
eventNetMultiplayer();
if( isSignalEnd == TRUE )
{
quitPublicServer();
}
if( lastActive == 0 )
{
lastActive = getMyTime();
}
interval = getMyTime() - lastActive;
if( interval < 50 )
{
return;
}
//printf("interval = %d\n", interval);
lastActive = getMyTime();
eventArena(arena);
}
void my_handler_quit(int n)
{
#ifdef DEBUG
printf("my_handler_quit\n");
#endif
isSignalEnd = TRUE;
}
void quitPublicServer()
{
#ifdef DEBUG
printf(_("Quitting public server\n"));
#endif
quitNetMultiplayer();
destroyArena(arena);
quitTux();
quitItem();
quitShot();
quitArenaFile();
quitServerConfigFile();
quitModule();
quitListID();
quitHeightScore();
quitLog();
exit(0);
}
int startPublicServer()
{
#ifndef __WIN32__
signal(SIGINT, my_handler_quit);
signal(SIGTERM, my_handler_quit);
signal(SIGQUIT, my_handler_quit);
#endif
if( initPublicServer() < 0 )
{
quitPublicServer();
return -1;
}
while(1)
{
eventPublicServer();
}
return 0;
}
|