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>
35 extern void copydown(char *, char *, int);
36 extern void fatal(char *);
40 static int clients = 0;
42 static int onceonly = 0;
43 static int timetoplay = 0;
44 static int is_daemon = 0;
47 static int paused = 0;
52 unsigned char nick[16];
54 struct user *next, *nextvictim;
57 unsigned char readbuf[MAX_BUFFER_SIZE];
59 unsigned char writbuf[MAX_BUFFER_SIZE];
69 static struct user *user0 = NULL;
71 #define NEXT(u) ((u)->next ? (u)->next : user0)
73 static struct sockaddr_in saddr;
75 static unsigned char realbuf[512], *buf = realbuf + 4;
80 static unsigned long frame_counter = 0;
84 static void syserr(char *s)
87 fprintf(stderr, "fatal: %s failed.\n", s);
91 static void addtobuffer(struct user *u, unsigned char *b, int len)
93 if (u->nwrite + len >= MAX_BUFFER_SIZE)
94 fatal("Internal error: send buffer overflow");
95 memcpy(u->writbuf + u->nwrite, b, len);
99 static void flushuser(struct user *u)
103 write(u->fd, u->writbuf, u->nwrite);
108 static void broadcast(struct user *except, int len, int activeonly)
112 realbuf[0] = realbuf[1] = realbuf[2] = 0;
113 realbuf[3] = (unsigned char)len;
114 for (u=user0; u; u=u->next)
115 if (u != except && (u->active || !activeonly) && u->introduced)
116 addtobuffer(u, realbuf, 4 + len);
119 static void sendtoone(struct user *to, int len)
121 realbuf[0] = realbuf[1] = realbuf[2] = 0;
122 realbuf[3] = (unsigned char)len;
123 addtobuffer(to, realbuf, 4 + len);
126 static void dropuser(struct user *u)
131 printf("RND_SERVER: dropping client %d (%s)\n", u->number, u->nick);
137 for (v=user0; v; v=v->next)
139 if (v->next && v->next == u)
151 buf[1] = OP_PLAYER_DISCONNECTED;
155 for (v=user0; v; v=v->next)
157 if (v->nextvictim == u)
159 for (w=NEXT(v); w!=v; w=NEXT(w))
161 if (w->active && w->playing)
167 if (v->nextvictim == u)
168 v->nextvictim = NULL;
178 if (onceonly && clients == bots)
182 printf("RND_SERVER: no clients left\n");
183 printf("RND_SERVER: aborting\n");
196 static void new_connect(int fd)
201 u = malloc(sizeof (struct user));
203 fatal("Out of memory");
207 u->nextvictim = NULL;
216 u->action_received = 0;
226 if (v->number == nxn)
236 printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr));
240 buf[1] = OP_YOUR_NUMBER;
245 static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len)
247 if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2)
250 printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
254 buf[2] = PROT_VERS_1;
255 buf[3] = PROT_VERS_2;
256 buf[4] = PROT_VERS_3;
266 printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
270 static void Handle_OP_NUMBER_WANTED(struct user *u)
273 int client_nr = u->number;
274 int nr_wanted = buf[2];
278 printf("RND_SERVER: client %d (%s) wants to switch to # %d\n",
279 u->number, u->nick, nr_wanted);
281 for (v=user0; v; v=v->next)
283 if (v->number == nr_wanted)
293 printf("RND_SERVER: client %d (%s) switches to # %d\n",
294 u->number, u->nick, nr_wanted);
295 else if (u->number == nr_wanted)
296 printf("RND_SERVER: client %d (%s) still has # %d\n",
297 u->number, u->nick, nr_wanted);
299 printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n",
300 u->number, u->nick, nr_wanted);
304 u->number = nr_wanted;
307 buf[1] = OP_NUMBER_WANTED;
315 broadcast(NULL, 4, 0);
318 static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
325 memcpy(u->nick, &buf[2], len-2);
327 for (i=0; i<len-2; i++)
329 if (u->nick[i] < ' ' ||
330 (u->nick[i] > 0x7e && u->nick[i] <= 0xa0))
340 buf[1] = OP_PLAYER_CONNECTED;
345 printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick);
346 buf[1] = OP_NICKNAME;
347 broadcast(u, len, 0);
351 for (v=user0; v; v=v->next)
353 if (v != u && v->introduced)
356 buf[1] = OP_PLAYER_CONNECTED;
357 buf[2] = (v->games >> 8);
358 buf[3] = (v->games & 0xff);
360 buf[1] = OP_NICKNAME;
361 memcpy(&buf[2], v->nick, 14);
362 sendtoone(u, 2+strlen(v->nick));
383 static void Handle_OP_START_PLAYING(struct user *u)
388 printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n",
390 (buf[2] << 8) + buf[3],
391 (buf[4] << 8) + buf[5],
395 for (w=user0; w; w=w->next)
402 w->nextvictim = NULL;
403 for (v=NEXT(w); v!=w; v=NEXT(v))
419 broadcast(NULL, 2, 0);
421 buf[1] = OP_START_PLAYING;
422 broadcast(NULL, 2, 0);
425 /* reset frame counter */
428 /* reset player actions */
429 for (v=user0; v; v=v->next)
432 v->action_received = 0;
435 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
438 static void Handle_OP_PAUSE_PLAYING(struct user *u)
441 printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick);
442 broadcast(NULL, 2, 0);
446 static void Handle_OP_CONTINUE_PLAYING(struct user *u)
449 printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick);
450 broadcast(NULL, 2, 0);
454 static void Handle_OP_STOP_PLAYING(struct user *u)
457 printf("RND_SERVER: client %d (%s) stops game\n", u->number, u->nick);
458 broadcast(NULL, 2, 0);
461 static void Handle_OP_MOVE_FIGURE(struct user *u)
464 int last_client_nr = 0;
467 /* store player action */
468 for (v=user0; v; v=v->next)
470 if (v->number == u->number)
473 v->action_received = 1;
477 /* check if server received action from each player */
478 for (v=user0; v; v=v->next)
480 if (!v->action_received)
483 if (v->number > last_client_nr)
484 last_client_nr = v->number;
487 /* initialize all player actions to zero */
488 for (i=0; i<last_client_nr; i++)
491 /* broadcast actions of all players to all players */
492 for (v=user0; v; v=v->next)
494 buf[6 + v->number-1] = v->action;
496 v->action_received = 0;
499 buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
500 buf[3] = (unsigned char)((frame_counter >> 16) & 0xff);
501 buf[4] = (unsigned char)((frame_counter >> 8) & 0xff);
502 buf[5] = (unsigned char)((frame_counter >> 0) & 0xff);
504 broadcast(NULL, 6 + last_client_nr, 0);
510 printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n",
512 u->number, u->nick, buf[2]);
516 void NetworkServer(int port, int serveronly)
519 struct user *u, *v, *w;
523 struct protoent *tcpproto;
528 struct sigaction sact;
537 if ((tcpproto = getprotobyname("tcp")) != NULL)
538 tcp = tcpproto->p_proto;
541 signal(SIGPIPE, SIG_IGN);
543 sact.sa_handler = SIG_IGN;
544 sigemptyset(&sact.sa_mask);
546 sigaction(SIGPIPE, &sact, NULL);
550 lfd = socket(PF_INET, SOCK_STREAM, 0);
551 saddr.sin_family = AF_INET;
552 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
553 saddr.sin_port = htons(port);
559 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
560 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
567 /* become a daemon, breaking all ties with the controlling terminal */
569 for (i=0; i<255; i++)
582 /* open a fake stdin, stdout, stderr, just in case */
583 open("/dev/null", O_RDONLY);
584 open("/dev/null", O_WRONLY);
585 open("/dev/null", O_WRONLY);
590 printf("rocksndiamonds network server: started up, listening on port %d\n",
592 printf("rocksndiamonds network server: using protocol version %d.%d.%d\n",
593 PROT_VERS_1, PROT_VERS_2, PROT_VERS_3);
601 if (timetoplay && time(NULL) >= timetoplay)
606 printf("RND_SERVER: everyone lost... restarting game\n");
611 for (u=user0; u; u=u->next)
627 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
635 if (clients > 0 && clients == bots)
638 printf("RND_SERVER: only bots left... dropping all bots\n");
647 if (FD_ISSET(lfd, &fds))
651 slen = sizeof(saddr);
652 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
663 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
674 if (FD_ISSET(u->fd, &fds))
676 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
680 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
686 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
689 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
692 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
693 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
698 memcpy(buf, &u->readbuf[4], len);
700 copydown(u->readbuf, u->readbuf + 4 + len, u->nread);
703 if (!u->introduced && buf[1] != OP_NICKNAME)
706 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
716 Handle_OP_NICKNAME(u, len);
719 case OP_PROTOCOL_VERSION:
720 Handle_OP_PROTOCOL_VERSION(u, len);
723 case OP_NUMBER_WANTED:
724 Handle_OP_NUMBER_WANTED(u);
727 case OP_START_PLAYING:
728 Handle_OP_START_PLAYING(u);
731 case OP_PAUSE_PLAYING:
732 Handle_OP_PAUSE_PLAYING(u);
735 case OP_CONTINUE_PLAYING:
736 Handle_OP_CONTINUE_PLAYING(u);
739 case OP_STOP_PLAYING:
740 Handle_OP_STOP_PLAYING(u);
744 Handle_OP_MOVE_FIGURE(u);
748 for (v=user0; v; v=v->next)
750 if (v->number == buf[2])
758 printf("RND_SERVER: client %d (%s) kills bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
767 printf("RND_SERVER: client %d (%s) attempting to kill non-bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
775 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"));
776 broadcast(NULL, 3, 0);
784 printf("RND_SERVER: client %d (%s) declares itself to be a bot\n", u->number, u->nick);
790 printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
791 broadcast(NULL, 3, 0);
796 struct user *won = NULL;
799 printf("RND_SERVER: client %d (%s) has lost\n", u->number, u->nick);
803 for (v=user0; v; v=v->next)
805 if (v->nextvictim == u)
807 for (w=NEXT(v); w!=v; w=NEXT(w))
809 if (w->active && w->playing)
815 if (v->nextvictim == u)
816 v->nextvictim = NULL;
819 for (v=user0; v; v=v->next)
829 buf[0] = won->number;
832 broadcast(NULL, 2, 0);
839 broadcast(NULL, 2, 0);
841 if (i < 2 && clients > 1)
842 timetoplay = time(NULL) + 4;
847 broadcast(NULL, 2, 0);
849 printf("RND_SERVER: client %d (%s) resets the game counters\n", u->number, u->nick);
850 for (v=user0; v; v=v->next)
862 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
863 broadcast(u, len, 0);
870 printf("RND_SERVER: client %d (%s) sends crap for an OP_LINES\n", u->number, u->nick);
879 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);
880 sendtoone(u->nextvictim, 3);
881 buf[3] = u->nextvictim->number;
883 broadcast(u->nextvictim, 4, 1);
884 for (v=NEXT(u->nextvictim); v!=u->nextvictim; v=NEXT(v))
886 if (v->active && v != u && v->playing)
894 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");
899 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
907 while (u && !interrupt);