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/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] != PROTOCOL_VERSION_1 || buf[3] != PROTOCOL_VERSION_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] = PROTOCOL_VERSION_1;
247 buf[3] = PROTOCOL_VERSION_2;
248 buf[4] = PROTOCOL_VERSION_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 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_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)
630 if (clients > 0 && clients == bots)
633 printf("RND_SERVER: only bots left... dropping all bots\n");
642 if (FD_ISSET(lfd, &fds))
646 slen = sizeof(saddr);
647 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
658 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
669 if (FD_ISSET(u->fd, &fds))
671 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
675 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
681 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
684 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
687 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
688 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
693 memcpy(buf, &u->readbuf[4], len);
695 memmove(u->readbuf, u->readbuf + 4 + len, u->nread);
698 if (!u->introduced && buf[1] != OP_NICKNAME)
701 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
711 Handle_OP_NICKNAME(u, len);
714 case OP_PROTOCOL_VERSION:
715 Handle_OP_PROTOCOL_VERSION(u, len);
718 case OP_NUMBER_WANTED:
719 Handle_OP_NUMBER_WANTED(u);
722 case OP_START_PLAYING:
723 Handle_OP_START_PLAYING(u);
726 case OP_PAUSE_PLAYING:
727 Handle_OP_PAUSE_PLAYING(u);
730 case OP_CONTINUE_PLAYING:
731 Handle_OP_CONTINUE_PLAYING(u);
734 case OP_STOP_PLAYING:
735 Handle_OP_STOP_PLAYING(u);
739 Handle_OP_MOVE_FIGURE(u);
743 for (v=user0; v; v=v->next)
745 if (v->number == buf[2])
753 printf("RND_SERVER: client %d (%s) kills bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
762 printf("RND_SERVER: client %d (%s) attempting to kill non-bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
770 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"));
771 broadcast(NULL, 3, 0);
779 printf("RND_SERVER: client %d (%s) declares itself to be a bot\n", u->number, u->nick);
785 printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
786 broadcast(NULL, 3, 0);
791 struct user *won = NULL;
794 printf("RND_SERVER: client %d (%s) has lost\n", u->number, u->nick);
798 for (v=user0; v; v=v->next)
800 if (v->nextvictim == u)
802 for (w=NEXT(v); w!=v; w=NEXT(w))
804 if (w->active && w->playing)
810 if (v->nextvictim == u)
811 v->nextvictim = NULL;
814 for (v=user0; v; v=v->next)
824 buf[0] = won->number;
827 broadcast(NULL, 2, 0);
834 broadcast(NULL, 2, 0);
836 if (i < 2 && clients > 1)
837 timetoplay = time(NULL) + 4;
842 broadcast(NULL, 2, 0);
844 printf("RND_SERVER: client %d (%s) resets the game counters\n", u->number, u->nick);
845 for (v=user0; v; v=v->next)
857 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
858 broadcast(u, len, 0);
865 printf("RND_SERVER: client %d (%s) sends crap for an OP_LINES\n", u->number, u->nick);
874 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);
875 sendtoone(u->nextvictim, 3);
876 buf[3] = u->nextvictim->number;
878 broadcast(u->nextvictim, 4, 1);
879 for (v=NEXT(u->nextvictim); v!=u->nextvictim; v=NEXT(v))
881 if (v->active && v != u && v->playing)
888 else if (options.verbose)
889 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");
894 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
902 while (u && !interrupt);