1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <arpa/inet.h>
29 static int clients = 0;
30 static int onceonly = 0;
31 static int is_daemon = 0;
33 struct NetworkServerPlayerInfo
36 unsigned char player_name[16];
38 struct NetworkServerPlayerInfo *next;
41 unsigned char readbuf[MAX_BUFFER_SIZE];
42 unsigned char writbuf[MAX_BUFFER_SIZE];
45 boolean action_received;
48 static struct NetworkServerPlayerInfo *user0 = NULL;
50 #define NEXT(u) ((u)->next ? (u)->next : user0)
52 static struct sockaddr_in saddr;
54 static unsigned char realbuf[512], *buf = realbuf + 4;
59 static unsigned long ServerFrameCounter = 0;
63 static void syserr(char *s)
66 fprintf(stderr, "fatal: %s failed.\n", s);
70 static void addtobuffer(struct NetworkServerPlayerInfo *u,
71 unsigned char *b, int len)
73 if (u->nwrite + len >= MAX_BUFFER_SIZE)
74 Error(ERR_EXIT, "internal error: network send buffer overflow");
76 memcpy(u->writbuf + u->nwrite, b, len);
80 static void flushuser(struct NetworkServerPlayerInfo *u)
84 write(u->fd, u->writbuf, u->nwrite);
89 static void broadcast(struct NetworkServerPlayerInfo *except,
90 int len, int activeonly)
92 struct NetworkServerPlayerInfo *u;
94 realbuf[0] = realbuf[1] = realbuf[2] = 0;
95 realbuf[3] = (unsigned char)len;
96 for (u=user0; u; u=u->next)
97 if (u != except && (u->active || !activeonly) && u->introduced)
98 addtobuffer(u, realbuf, 4 + len);
101 static void sendtoone(struct NetworkServerPlayerInfo *to, int len)
103 realbuf[0] = realbuf[1] = realbuf[2] = 0;
104 realbuf[3] = (unsigned char)len;
105 addtobuffer(to, realbuf, 4 + len);
108 static void dropuser(struct NetworkServerPlayerInfo *u)
110 struct NetworkServerPlayerInfo *v;
113 Error(ERR_NETWORK_SERVER, "dropping client %d (%s)",
114 u->number, u->player_name);
120 for (v=user0; v; v=v->next)
122 if (v->next && v->next == u)
134 buf[1] = OP_PLAYER_DISCONNECTED;
141 if (onceonly && clients == 0)
145 Error(ERR_NETWORK_SERVER, "no clients left");
146 Error(ERR_NETWORK_SERVER, "aborting");
152 static void new_connect(int fd)
154 struct NetworkServerPlayerInfo *u, *v;
157 u = checked_malloc(sizeof (struct NetworkServerPlayerInfo));
160 u->player_name[0] = 0;
167 u->action_received = FALSE;
177 if (v->number == nxn)
187 Error(ERR_NETWORK_SERVER, "client %d connecting from %s",
188 nxn, inet_ntoa(saddr.sin_addr));
192 buf[1] = OP_YOUR_NUMBER;
197 static void Handle_OP_PROTOCOL_VERSION(struct NetworkServerPlayerInfo *u,
200 if (len != 5 || buf[2] != PROTOCOL_VERSION_1 || buf[3] != PROTOCOL_VERSION_2)
203 Error(ERR_NETWORK_SERVER,
204 "client %d (%s) has wrong protocol version %d.%d.%d",
205 u->number, u->player_name, buf[2], buf[3], buf[4]);
208 buf[1] = OP_BAD_PROTOCOL_VERSION;
209 buf[2] = PROTOCOL_VERSION_1;
210 buf[3] = PROTOCOL_VERSION_2;
211 buf[4] = PROTOCOL_VERSION_3;
221 Error(ERR_NETWORK_SERVER,
222 "client %d (%s) uses protocol version %d.%d.%d",
223 u->number, u->player_name, buf[2], buf[3], buf[4]);
227 static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *u)
229 struct NetworkServerPlayerInfo *v;
230 int client_nr = u->number;
231 int nr_wanted = buf[2];
235 Error(ERR_NETWORK_SERVER, "client %d (%s) wants to switch to # %d",
236 u->number, u->player_name, nr_wanted);
238 for (v=user0; v; v=v->next)
240 if (v->number == nr_wanted)
250 Error(ERR_NETWORK_SERVER, "client %d (%s) switches to # %d",
251 u->number, u->player_name, nr_wanted);
252 else if (u->number == nr_wanted)
253 Error(ERR_NETWORK_SERVER, "client %d (%s) still has # %d",
254 u->number, u->player_name, nr_wanted);
256 Error(ERR_NETWORK_SERVER,
257 "client %d (%s) cannot switch (client %d still exists)",
258 u->number, u->player_name, nr_wanted);
262 u->number = nr_wanted;
265 buf[1] = OP_NUMBER_WANTED;
273 broadcast(NULL, 4, 0);
276 static void Handle_OP_PLAYER_NAME(struct NetworkServerPlayerInfo *u,
279 struct NetworkServerPlayerInfo *v;
284 memcpy(u->player_name, &buf[2], len-2);
285 u->player_name[len-2] = 0;
286 for (i=0; i<len-2; i++)
288 if (u->player_name[i] < ' ' ||
289 (u->player_name[i] > 0x7e && u->player_name[i] <= 0xa0))
291 u->player_name[i] = 0;
299 buf[1] = OP_PLAYER_CONNECTED;
304 Error(ERR_NETWORK_SERVER, "client %d calls itself \"%s\"",
305 u->number, u->player_name);
306 buf[1] = OP_PLAYER_NAME;
307 broadcast(u, len, 0);
311 for (v=user0; v; v=v->next)
313 if (v != u && v->introduced)
316 buf[1] = OP_PLAYER_CONNECTED;
318 buf[1] = OP_PLAYER_NAME;
319 memcpy(&buf[2], v->player_name, 14);
320 sendtoone(u, 2+strlen(v->player_name));
328 static void Handle_OP_START_PLAYING(struct NetworkServerPlayerInfo *u)
330 struct NetworkServerPlayerInfo *v, *w;
333 Error(ERR_NETWORK_SERVER,
334 "client %d (%s) starts game [level %d from levedir %d (%s)]",
335 u->number, u->player_name,
336 (buf[2] << 8) + buf[3],
337 (buf[4] << 8) + buf[5],
340 for (w=user0; w; w=w->next)
344 /* reset frame counter */
345 ServerFrameCounter = 0;
347 /* reset player actions */
348 for (v=user0; v; v=v->next)
351 v->action_received = FALSE;
354 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
357 static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *u)
360 Error(ERR_NETWORK_SERVER, "client %d (%s) pauses game",
361 u->number, u->player_name);
362 broadcast(NULL, 2, 0);
365 static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *u)
368 Error(ERR_NETWORK_SERVER, "client %d (%s) continues game",
369 u->number, u->player_name);
370 broadcast(NULL, 2, 0);
373 static void Handle_OP_STOP_PLAYING(struct NetworkServerPlayerInfo *u)
376 Error(ERR_NETWORK_SERVER, "client %d (%s) stops game",
377 u->number, u->player_name);
378 broadcast(NULL, 2, 0);
381 static void Handle_OP_MOVE_FIGURE(struct NetworkServerPlayerInfo *u)
383 struct NetworkServerPlayerInfo *v;
384 int last_client_nr = 0;
387 /* store player action */
388 for (v=user0; v; v=v->next)
390 if (v->number == u->number)
393 v->action_received = TRUE;
397 /* check if server received action from each player */
398 for (v=user0; v; v=v->next)
400 if (!v->action_received)
403 if (v->number > last_client_nr)
404 last_client_nr = v->number;
407 /* initialize all player actions to zero */
408 for (i=0; i<last_client_nr; i++)
411 /* broadcast actions of all players to all players */
412 for (v=user0; v; v=v->next)
414 buf[6 + v->number-1] = v->action;
416 v->action_received = FALSE;
419 buf[2] = (unsigned char)((ServerFrameCounter >> 24) & 0xff);
420 buf[3] = (unsigned char)((ServerFrameCounter >> 16) & 0xff);
421 buf[4] = (unsigned char)((ServerFrameCounter >> 8) & 0xff);
422 buf[5] = (unsigned char)((ServerFrameCounter >> 0) & 0xff);
424 broadcast(NULL, 6 + last_client_nr, 0);
426 ServerFrameCounter++;
429 void NetworkServer(int port, int serveronly)
432 struct NetworkServerPlayerInfo *u;
436 struct protoent *tcpproto;
441 struct sigaction sact;
445 port = DEFAULT_SERVER_PORT;
450 if ((tcpproto = getprotobyname("tcp")) != NULL)
451 tcp = tcpproto->p_proto;
454 signal(SIGPIPE, SIG_IGN);
456 sact.sa_handler = SIG_IGN;
457 sigemptyset(&sact.sa_mask);
459 sigaction(SIGPIPE, &sact, NULL);
463 lfd = socket(PF_INET, SOCK_STREAM, 0);
464 saddr.sin_family = AF_INET;
465 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
466 saddr.sin_port = htons(port);
472 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
473 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
480 /* become a daemon, breaking all ties with the controlling terminal */
482 for (i=0; i<255; i++)
495 /* open a fake stdin, stdout, stderr, just in case */
496 open("/dev/null", O_RDONLY);
497 open("/dev/null", O_WRONLY);
498 open("/dev/null", O_WRONLY);
503 Error(ERR_NETWORK_SERVER, "started up, listening on port %d", port);
504 Error(ERR_NETWORK_SERVER, "using protocol version %d.%d.%d",
505 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
512 for (u=user0; u; u=u->next)
528 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
542 if (FD_ISSET(lfd, &fds))
546 slen = sizeof(saddr);
547 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
558 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
569 if (FD_ISSET(u->fd, &fds))
571 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
575 Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
576 u->number, u->player_name);
582 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
585 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
588 Error(ERR_NETWORK_SERVER, "crap from client %d (%s)",
589 u->number, u->player_name);
590 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
595 memcpy(buf, &u->readbuf[4], len);
597 memmove(u->readbuf, u->readbuf + 4 + len, u->nread);
600 if (!u->introduced && buf[1] != OP_PLAYER_NAME)
603 Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buf[1]==%d (expected OP_PLAYER_NAME)", buf[0], buf[1]);
613 Handle_OP_PLAYER_NAME(u, len);
616 case OP_PROTOCOL_VERSION:
617 Handle_OP_PROTOCOL_VERSION(u, len);
620 case OP_NUMBER_WANTED:
621 Handle_OP_NUMBER_WANTED(u);
624 case OP_START_PLAYING:
625 Handle_OP_START_PLAYING(u);
628 case OP_PAUSE_PLAYING:
629 Handle_OP_PAUSE_PLAYING(u);
632 case OP_CONTINUE_PLAYING:
633 Handle_OP_CONTINUE_PLAYING(u);
636 case OP_STOP_PLAYING:
637 Handle_OP_STOP_PLAYING(u);
641 Handle_OP_MOVE_FIGURE(u);
644 case OP_BROADCAST_MESSAGE:
647 Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s",
648 u->number, u->player_name, &buf[2]);
649 broadcast(u, len, 0);
654 Error(ERR_NETWORK_SERVER,
655 "unknown opcode %d from client %d (%s)",
656 buf[0], u->number, u->player_name);
664 while (u && !interrupt);