2 * A server for a multi-player version of Tetris
4 * Copyright (C) 1996 Roger Espel Llima <roger.espel.llima@pobox.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation. See the file COPYING for details.
18 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
32 static int clients = 0;
34 static int onceonly = 0;
35 static int timetoplay = 0;
36 static int is_daemon = 0;
37 static int levelnr = 5;
39 static int paused = 0;
44 unsigned char nick[16];
46 struct user *next, *nextvictim;
49 unsigned char readbuf[MAX_BUFFER_SIZE];
51 unsigned char writbuf[MAX_BUFFER_SIZE];
61 static struct user *user0 = NULL;
63 #define NEXT(u) ((u)->next ? (u)->next : user0)
65 static struct sockaddr_in saddr;
67 static unsigned char realbuf[512], *buf = realbuf + 4;
72 static unsigned long frame_counter = 0;
76 static void syserr(char *s)
79 fprintf(stderr, "fatal: %s failed.\n", s);
83 static void addtobuffer(struct user *u, unsigned char *b, int len)
85 if (u->nwrite + len >= MAX_BUFFER_SIZE)
86 Error(ERR_EXIT, "internal error: network send buffer overflow");
88 memcpy(u->writbuf + u->nwrite, b, len);
92 static void flushuser(struct user *u)
96 write(u->fd, u->writbuf, u->nwrite);
101 static void broadcast(struct user *except, int len, int activeonly)
105 realbuf[0] = realbuf[1] = realbuf[2] = 0;
106 realbuf[3] = (unsigned char)len;
107 for (u=user0; u; u=u->next)
108 if (u != except && (u->active || !activeonly) && u->introduced)
109 addtobuffer(u, realbuf, 4 + len);
112 static void sendtoone(struct user *to, int len)
114 realbuf[0] = realbuf[1] = realbuf[2] = 0;
115 realbuf[3] = (unsigned char)len;
116 addtobuffer(to, realbuf, 4 + len);
119 static void dropuser(struct user *u)
124 printf("RND_SERVER: dropping client %d (%s)\n", u->number, u->nick);
130 for (v=user0; v; v=v->next)
132 if (v->next && v->next == u)
144 buf[1] = OP_PLAYER_DISCONNECTED;
148 for (v=user0; v; v=v->next)
150 if (v->nextvictim == u)
152 for (w=NEXT(v); w!=v; w=NEXT(w))
154 if (w->active && w->playing)
160 if (v->nextvictim == u)
161 v->nextvictim = NULL;
171 if (onceonly && clients == bots)
175 printf("RND_SERVER: no clients left\n");
176 printf("RND_SERVER: aborting\n");
189 static void new_connect(int fd)
194 u = checked_malloc(sizeof (struct user));
199 u->nextvictim = NULL;
208 u->action_received = 0;
218 if (v->number == nxn)
228 printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr));
232 buf[1] = OP_YOUR_NUMBER;
237 static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len)
239 if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2)
242 printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
246 buf[2] = PROT_VERS_1;
247 buf[3] = PROT_VERS_2;
248 buf[4] = PROT_VERS_3;
258 printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
262 static void Handle_OP_NUMBER_WANTED(struct user *u)
265 int client_nr = u->number;
266 int nr_wanted = buf[2];
270 printf("RND_SERVER: client %d (%s) wants to switch to # %d\n",
271 u->number, u->nick, nr_wanted);
273 for (v=user0; v; v=v->next)
275 if (v->number == nr_wanted)
285 printf("RND_SERVER: client %d (%s) switches to # %d\n",
286 u->number, u->nick, nr_wanted);
287 else if (u->number == nr_wanted)
288 printf("RND_SERVER: client %d (%s) still has # %d\n",
289 u->number, u->nick, nr_wanted);
291 printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n",
292 u->number, u->nick, nr_wanted);
296 u->number = nr_wanted;
299 buf[1] = OP_NUMBER_WANTED;
307 broadcast(NULL, 4, 0);
310 static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
317 memcpy(u->nick, &buf[2], len-2);
319 for (i=0; i<len-2; i++)
321 if (u->nick[i] < ' ' ||
322 (u->nick[i] > 0x7e && u->nick[i] <= 0xa0))
332 buf[1] = OP_PLAYER_CONNECTED;
337 printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick);
338 buf[1] = OP_NICKNAME;
339 broadcast(u, len, 0);
343 for (v=user0; v; v=v->next)
345 if (v != u && v->introduced)
348 buf[1] = OP_PLAYER_CONNECTED;
349 buf[2] = (v->games >> 8);
350 buf[3] = (v->games & 0xff);
352 buf[1] = OP_NICKNAME;
353 memcpy(&buf[2], v->nick, 14);
354 sendtoone(u, 2+strlen(v->nick));
375 static void Handle_OP_START_PLAYING(struct user *u)
380 printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n",
382 (buf[2] << 8) + buf[3],
383 (buf[4] << 8) + buf[5],
387 for (w=user0; w; w=w->next)
394 w->nextvictim = NULL;
395 for (v=NEXT(w); v!=w; v=NEXT(v))
411 broadcast(NULL, 2, 0);
413 buf[1] = OP_START_PLAYING;
414 broadcast(NULL, 2, 0);
417 /* reset frame counter */
420 /* reset player actions */
421 for (v=user0; v; v=v->next)
424 v->action_received = 0;
427 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
430 static void Handle_OP_PAUSE_PLAYING(struct user *u)
433 printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick);
434 broadcast(NULL, 2, 0);
438 static void Handle_OP_CONTINUE_PLAYING(struct user *u)
441 printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick);
442 broadcast(NULL, 2, 0);
446 static void Handle_OP_STOP_PLAYING(struct user *u)
449 printf("RND_SERVER: client %d (%s) stops game\n", u->number, u->nick);
450 broadcast(NULL, 2, 0);
453 static void Handle_OP_MOVE_FIGURE(struct user *u)
456 int last_client_nr = 0;
459 /* store player action */
460 for (v=user0; v; v=v->next)
462 if (v->number == u->number)
465 v->action_received = 1;
469 /* check if server received action from each player */
470 for (v=user0; v; v=v->next)
472 if (!v->action_received)
475 if (v->number > last_client_nr)
476 last_client_nr = v->number;
479 /* initialize all player actions to zero */
480 for (i=0; i<last_client_nr; i++)
483 /* broadcast actions of all players to all players */
484 for (v=user0; v; v=v->next)
486 buf[6 + v->number-1] = v->action;
488 v->action_received = 0;
491 buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
492 buf[3] = (unsigned char)((frame_counter >> 16) & 0xff);
493 buf[4] = (unsigned char)((frame_counter >> 8) & 0xff);
494 buf[5] = (unsigned char)((frame_counter >> 0) & 0xff);
496 broadcast(NULL, 6 + last_client_nr, 0);
502 printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n",
504 u->number, u->nick, buf[2]);
508 void NetworkServer(int port, int serveronly)
511 struct user *u, *v, *w;
515 struct protoent *tcpproto;
520 struct sigaction sact;
529 if ((tcpproto = getprotobyname("tcp")) != NULL)
530 tcp = tcpproto->p_proto;
533 signal(SIGPIPE, SIG_IGN);
535 sact.sa_handler = SIG_IGN;
536 sigemptyset(&sact.sa_mask);
538 sigaction(SIGPIPE, &sact, NULL);
542 lfd = socket(PF_INET, SOCK_STREAM, 0);
543 saddr.sin_family = AF_INET;
544 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
545 saddr.sin_port = htons(port);
551 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
552 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
559 /* become a daemon, breaking all ties with the controlling terminal */
561 for (i=0; i<255; i++)
574 /* open a fake stdin, stdout, stderr, just in case */
575 open("/dev/null", O_RDONLY);
576 open("/dev/null", O_WRONLY);
577 open("/dev/null", O_WRONLY);
582 printf("rocksndiamonds network server: started up, listening on port %d\n",
584 printf("rocksndiamonds network server: using protocol version %d.%d.%d\n",
585 PROT_VERS_1, PROT_VERS_2, PROT_VERS_3);
593 if (timetoplay && time(NULL) >= timetoplay)
598 printf("RND_SERVER: everyone lost... restarting game\n");
603 for (u=user0; u; u=u->next)
619 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
627 if (clients > 0 && clients == bots)
630 printf("RND_SERVER: only bots left... dropping all bots\n");
639 if (FD_ISSET(lfd, &fds))
643 slen = sizeof(saddr);
644 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
655 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
666 if (FD_ISSET(u->fd, &fds))
668 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
672 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
678 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
681 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
684 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
685 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
690 memcpy(buf, &u->readbuf[4], len);
692 memmove(u->readbuf, u->readbuf + 4 + len, u->nread);
695 if (!u->introduced && buf[1] != OP_NICKNAME)
698 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
708 Handle_OP_NICKNAME(u, len);
711 case OP_PROTOCOL_VERSION:
712 Handle_OP_PROTOCOL_VERSION(u, len);
715 case OP_NUMBER_WANTED:
716 Handle_OP_NUMBER_WANTED(u);
719 case OP_START_PLAYING:
720 Handle_OP_START_PLAYING(u);
723 case OP_PAUSE_PLAYING:
724 Handle_OP_PAUSE_PLAYING(u);
727 case OP_CONTINUE_PLAYING:
728 Handle_OP_CONTINUE_PLAYING(u);
731 case OP_STOP_PLAYING:
732 Handle_OP_STOP_PLAYING(u);
736 Handle_OP_MOVE_FIGURE(u);
740 for (v=user0; v; v=v->next)
742 if (v->number == buf[2])
750 printf("RND_SERVER: client %d (%s) kills bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
759 printf("RND_SERVER: client %d (%s) attempting to kill non-bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
767 printf("RND_SERVER: client %d (%s) sets mode %d (%s)\n", u->number, u->nick, buf[2], buf[2] == 0 ? "normal" : (buf[2] == 1 ? "fun" : "unknown"));
768 broadcast(NULL, 3, 0);
776 printf("RND_SERVER: client %d (%s) declares itself to be a bot\n", u->number, u->nick);
782 printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
783 broadcast(NULL, 3, 0);
788 struct user *won = NULL;
791 printf("RND_SERVER: client %d (%s) has lost\n", u->number, u->nick);
795 for (v=user0; v; v=v->next)
797 if (v->nextvictim == u)
799 for (w=NEXT(v); w!=v; w=NEXT(w))
801 if (w->active && w->playing)
807 if (v->nextvictim == u)
808 v->nextvictim = NULL;
811 for (v=user0; v; v=v->next)
821 buf[0] = won->number;
824 broadcast(NULL, 2, 0);
831 broadcast(NULL, 2, 0);
833 if (i < 2 && clients > 1)
834 timetoplay = time(NULL) + 4;
839 broadcast(NULL, 2, 0);
841 printf("RND_SERVER: client %d (%s) resets the game counters\n", u->number, u->nick);
842 for (v=user0; v; v=v->next)
854 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
855 broadcast(u, len, 0);
862 printf("RND_SERVER: client %d (%s) sends crap for an OP_LINES\n", u->number, u->nick);
871 printf("RND_SERVER: client %d (%s) sends %d %s to client %d (%s)\n", u->number, u->nick, (int)buf[2], buf[2] == 1 ? "line" : "lines", u->nextvictim->number, u->nextvictim->nick);
872 sendtoone(u->nextvictim, 3);
873 buf[3] = u->nextvictim->number;
875 broadcast(u->nextvictim, 4, 1);
876 for (v=NEXT(u->nextvictim); v!=u->nextvictim; v=NEXT(v))
878 if (v->active && v != u && v->playing)
886 printf("RND_SERVER: client %d (%s) makes %d %s but has no victim\n", u->number, u->nick, (int)buf[2], buf[2] == 1 ? "line" : "lines");
891 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
899 while (u && !interrupt);