-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaelstrom-netd.c
376 lines (328 loc) · 9.04 KB
/
Maelstrom-netd.c
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
373
374
375
/* Here we go... */
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
/* We wait in a loop for players to connect and tell us how many people
are playing. Then, once all players have connected, then we broadcast
the addresses of all players, and then start again.
Note: We rely on our MAX_PLAYERS and the client's MAX_PLAYERS being
the same. Otherwise we crash.
*/
#include "protocol.h"
#define MAX_CONNECTIONS 64
#define UNCONNECTED 0
#define CONNECTED 1
#define ACTIVE 2
#define TIMEOUT 300 /* timeout in seconds */
typedef struct {
int state;
int sockfd;
time_t timestamp;
int player;
int numplayers;
unsigned char *packet;
int packetlen;
struct sockaddr_in raddr;
} connection;
connection players[MAX_CONNECTIONS];
/* This function disconnects a player */
void DisconnectPlayer(int which)
{
if ( players[which].state == ACTIVE )
(void) free(players[which].packet);
close(players[which].sockfd);
players[which].state = UNCONNECTED;
printf("Player on slot %d has been disconnected.\n", which);
}
void SendError(int which, char *message)
{
unsigned char mesgbuf[BUFSIZ];
int mesglen;
mesglen = (2+strlen(message)+1);
if ( mesglen > BUFSIZ ) {
fprintf(stderr, "Fatal error: error message too long!\n");
exit(3);
}
mesgbuf[0] = mesglen;
mesgbuf[1] = NET_ABORT;
strcpy((char *)&mesgbuf[2], message);
printf("Sending error '%s' to player in slot %d\n", message, which);
(void) write(players[which].sockfd, mesgbuf, mesglen);
DisconnectPlayer(which);
}
/* Uh oh, a fatal error. Tell all currently connected players, and exit. */
void Fatal(char *message)
{
int i;
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state != UNCONNECTED )
SendError(i, message);
}
exit(3);
}
/* This function checks for player timeouts */
void CheckPlayers(void)
{
int i;
time_t now;
now = time(NULL);
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state != CONNECTED )
continue;
if ( (now-players[i].timestamp) > TIMEOUT ) {
SendError(i, "Server timed out waiting for packet");
}
}
}
/* This is the function that makes the server go 'round. :) */
/* We check to see if all advertised players have connected, and
if so we blast everyone's address to each player.
*/
void CheckNewGame(void)
{
unsigned char buffer[BUFSIZ];
int first, i;
int numplayers, players_on;
int positions[MAX_PLAYERS];
/* Find the first player */
for ( i=0, first=(-1); i<MAX_CONNECTIONS; ++i ) {
if ( (players[i].state == ACTIVE) && (players[i].player == 0) )
break;
}
if ( i == MAX_CONNECTIONS ) /* No first player... */
return;
/* Now make sure everyone else agrees with the first player */
first = i;
numplayers = players[first].numplayers;
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state != ACTIVE )
continue;
if ( players[i].numplayers != numplayers ) {
sprintf(buffer,
"There are %d, not %d players in this game",
numplayers, players[i].numplayers);
SendError(i, (char *)buffer);
}
}
/* Now see if there are enough players! */
for ( i=0, players_on=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state == ACTIVE ) {
positions[players[i].player] = i;
++players_on;
}
}
/* We've rejected all duplicate players, and extra players,
so players_on shouldn't be greater than numplayers.
*/
if ( players_on == numplayers ) { /* Let's party!! */
char *ptr;
int len;
printf("Let's party!!\n");
len = (2+players[first].packetlen);
buffer[1] = NEW_GAME;
memcpy(&buffer[2], players[first].packet,
players[first].packetlen);
ptr = (char *)&buffer[len];
for ( i=0; i<numplayers; ++i ) {
connection *player = &players[positions[i]];
strcpy(ptr, (char *)inet_ntoa(player->raddr.sin_addr));
printf("Setting up player %d at host %s and port ", i+1, ptr);
len += strlen(ptr)+1;
ptr += strlen(ptr)+1;
sprintf(ptr, "%d", ntohs(player->raddr.sin_port));
printf("%s\n", ptr);
len += strlen(ptr)+1;
ptr += strlen(ptr)+1;
}
buffer[0] = len;
for ( i=0; i<numplayers; ++i ) {
(void) write(players[positions[i]].sockfd, buffer, len);
DisconnectPlayer(positions[i]);
}
}
}
void I_Crashed(int sig)
{
if ( sig == SIGSEGV )
Fatal("Server crashed!");
else
Fatal("Server shut down!");
exit(sig);
}
main(int argc, char *argv[])
{
int netfd, i, slot;
struct sockaddr_in serv_addr;
/******************************************************
*
* Phase 1: Initialization
*
******************************************************/
/* Create a socket */
if ( (netfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
perror("Can't open stream socket");
exit(3);
}
/* Bind it to our address */
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(NETPLAY_PORT-1);
if (bind(netfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Can't bind local address");
exit(3);
}
if ( listen(netfd, MAX_PLAYERS) < 0 ) {
perror("listen() failed");
exit(3);
}
/* Initialize player structures */
for ( i=0; i<MAX_CONNECTIONS; ++i )
players[i].state = UNCONNECTED;
signal(SIGINT, I_Crashed);
signal(SIGSEGV, I_Crashed);
/******************************************************
*
* Phase 2: Wait for all players
*
******************************************************/
printf("Waiting for players...\n");
for ( ; ; ) {
fd_set fdset;
struct timeval tv;
int maxfd = 0;
FD_ZERO(&fdset);
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state != UNCONNECTED ) {
if ( maxfd < players[i].sockfd )
maxfd = players[i].sockfd;
FD_SET(players[i].sockfd, &fdset);
}
}
if ( maxfd < netfd )
maxfd = netfd;
FD_SET(netfd, &fdset);
tv.tv_sec = 60;
tv.tv_usec = 0;
if ( select(maxfd+1, &fdset, NULL, NULL, &tv) <= 0 ) {
if ( errno != 0 ) {
perror("select() error");
exit(3);
}
CheckPlayers();
continue;
}
/* Check for new players first */
if ( FD_ISSET(netfd, &fdset) ) {
int sockfd, clilen;
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state == UNCONNECTED )
break;
}
if ( i == MAX_CONNECTIONS ) {
fprintf(stderr, "Out of connections!!\n");
exit(3);
}
slot = i;
clilen = sizeof(players[slot].raddr);
if ( (sockfd=accept(netfd, (struct sockaddr *)
&players[slot].raddr, &clilen)) < 0 ) {
perror("accept() error");
exit(3);
}
players[slot].timestamp = time(NULL);
players[slot].sockfd = sockfd;
players[slot].state = CONNECTED;
printf("Connection received on port %d\n", i);
/* We continue so that we can get all players quickly */
continue;
}
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
unsigned char data[BUFSIZ];
unsigned long cliport;
int len, player;
int numplayers;
if ( (players[i].state == UNCONNECTED) ||
! FD_ISSET(players[i].sockfd, &fdset) )
continue;
/* A player with active data! */
slot = i;
if ( (len=read(players[slot].sockfd, data, BUFSIZ))
<= 0 ) {
/* Wierd. Close connection */
DisconnectPlayer(slot);
continue;
}
/* Are they cancelling a connection? */
if ( data[0] == NET_ABORT ) {
DisconnectPlayer(slot);
continue;
}
if ( data[0] != NEW_GAME ) {
fprintf(stderr,
"Unknown client packet: 0x%.2x\n",
data[0]);
DisconnectPlayer(slot);
continue;
}
if ( len != NEW_PACKETLEN+4+1 ) {
fprintf(stderr,
"Short client packet! (len was %d, expected %d)\n",
len, NEW_PACKETLEN+4+1);
SendError(slot, "Server received short packet");
continue;
}
/* Yay! Active connection! */
player = data[1];
/* Be wary, client sizeof(unsigned long) and our
sizeof(unsigned long) may differ. We assume
sizeof(unsigned long) is 4.
*/
memcpy(&cliport, &data[NEW_PACKETLEN], sizeof(cliport));
numplayers = data[NEW_PACKETLEN+4];
for ( i=0; i<MAX_CONNECTIONS; ++i ) {
if ( players[i].state == ACTIVE ) {
if ( player == players[i].player )
break;
}
}
/* Is there already player N? */
if ( i != MAX_CONNECTIONS ) {
char message[BUFSIZ];
sprintf(message, "Player %d is already on!",
player+1);
SendError(slot, message);
continue;
}
/* Set the player up */
players[slot].state = ACTIVE;
players[slot].player = player;
players[slot].numplayers = numplayers;
players[slot].packetlen = len-(2+4+1);
if ( (players[slot].packet = (unsigned char *)
malloc(players[slot].packetlen)) == NULL) {
perror("Out of memory");
Fatal("Server ran out of memory");
}
memcpy(players[slot].packet, &data[2],
players[slot].packetlen);
/* This is important! */
players[slot].raddr.sin_port =
htons((short)ntohl(cliport));
printf("Player %d arrived on port %d\n", player+1, slot);
printf(" the remote address is %s:%lu\n",
inet_ntoa(players[slot].raddr.sin_addr),
ntohs(players[slot].raddr.sin_port));
}
CheckNewGame();
CheckPlayers();
}
/* Never reached */
}