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;
33 static int onceonly = 0;
34 static int is_daemon = 0;
39 unsigned char nick[16];
41 struct user *next, *nextvictim;
44 unsigned char readbuf[MAX_BUFFER_SIZE];
46 unsigned char writbuf[MAX_BUFFER_SIZE];
55 static struct user *user0 = NULL;
57 #define NEXT(u) ((u)->next ? (u)->next : user0)
59 static struct sockaddr_in saddr;
61 static unsigned char realbuf[512], *buf = realbuf + 4;
66 static unsigned long frame_counter = 0;
70 static void syserr(char *s)
73 fprintf(stderr, "fatal: %s failed.\n", s);
77 static void addtobuffer(struct user *u, unsigned char *b, int len)
79 if (u->nwrite + len >= MAX_BUFFER_SIZE)
80 Error(ERR_EXIT, "internal error: network send buffer overflow");
82 memcpy(u->writbuf + u->nwrite, b, len);
86 static void flushuser(struct user *u)
90 write(u->fd, u->writbuf, u->nwrite);
95 static void broadcast(struct user *except, int len, int activeonly)
99 realbuf[0] = realbuf[1] = realbuf[2] = 0;
100 realbuf[3] = (unsigned char)len;
101 for (u=user0; u; u=u->next)
102 if (u != except && (u->active || !activeonly) && u->introduced)
103 addtobuffer(u, realbuf, 4 + len);
106 static void sendtoone(struct user *to, int len)
108 realbuf[0] = realbuf[1] = realbuf[2] = 0;
109 realbuf[3] = (unsigned char)len;
110 addtobuffer(to, realbuf, 4 + len);
113 static void dropuser(struct user *u)
118 printf("RND_SERVER: dropping client %d (%s)\n", u->number, u->nick);
124 for (v=user0; v; v=v->next)
126 if (v->next && v->next == u)
138 buf[1] = OP_PLAYER_DISCONNECTED;
142 for (v=user0; v; v=v->next)
144 if (v->nextvictim == u)
146 for (w=NEXT(v); w!=v; w=NEXT(w))
148 if (w->active && w->playing)
154 if (v->nextvictim == u)
155 v->nextvictim = NULL;
162 if (onceonly && clients == 0)
166 printf("RND_SERVER: no clients left\n");
167 printf("RND_SERVER: aborting\n");
173 static void new_connect(int fd)
178 u = checked_malloc(sizeof (struct user));
183 u->nextvictim = NULL;
191 u->action_received = 0;
201 if (v->number == nxn)
211 printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr));
215 buf[1] = OP_YOUR_NUMBER;
220 static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len)
222 if (len != 5 || buf[2] != PROTOCOL_VERSION_1 || buf[3] != PROTOCOL_VERSION_2)
225 printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
229 buf[2] = PROTOCOL_VERSION_1;
230 buf[3] = PROTOCOL_VERSION_2;
231 buf[4] = PROTOCOL_VERSION_3;
241 printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
245 static void Handle_OP_NUMBER_WANTED(struct user *u)
248 int client_nr = u->number;
249 int nr_wanted = buf[2];
253 printf("RND_SERVER: client %d (%s) wants to switch to # %d\n",
254 u->number, u->nick, nr_wanted);
256 for (v=user0; v; v=v->next)
258 if (v->number == nr_wanted)
268 printf("RND_SERVER: client %d (%s) switches to # %d\n",
269 u->number, u->nick, nr_wanted);
270 else if (u->number == nr_wanted)
271 printf("RND_SERVER: client %d (%s) still has # %d\n",
272 u->number, u->nick, nr_wanted);
274 printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n",
275 u->number, u->nick, nr_wanted);
279 u->number = nr_wanted;
282 buf[1] = OP_NUMBER_WANTED;
290 broadcast(NULL, 4, 0);
293 static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
300 memcpy(u->nick, &buf[2], len-2);
302 for (i=0; i<len-2; i++)
304 if (u->nick[i] < ' ' ||
305 (u->nick[i] > 0x7e && u->nick[i] <= 0xa0))
315 buf[1] = OP_PLAYER_CONNECTED;
320 printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick);
321 buf[1] = OP_NICKNAME;
322 broadcast(u, len, 0);
326 for (v=user0; v; v=v->next)
328 if (v != u && v->introduced)
331 buf[1] = OP_PLAYER_CONNECTED;
332 buf[2] = (v->games >> 8);
333 buf[3] = (v->games & 0xff);
335 buf[1] = OP_NICKNAME;
336 memcpy(&buf[2], v->nick, 14);
337 sendtoone(u, 2+strlen(v->nick));
345 static void Handle_OP_START_PLAYING(struct user *u)
350 printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n",
352 (buf[2] << 8) + buf[3],
353 (buf[4] << 8) + buf[5],
356 for (w=user0; w; w=w->next)
363 w->nextvictim = NULL;
364 for (v=NEXT(w); v!=w; v=NEXT(v))
375 /* reset frame counter */
378 /* reset player actions */
379 for (v=user0; v; v=v->next)
382 v->action_received = 0;
385 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
388 static void Handle_OP_PAUSE_PLAYING(struct user *u)
391 printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick);
392 broadcast(NULL, 2, 0);
395 static void Handle_OP_CONTINUE_PLAYING(struct user *u)
398 printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick);
399 broadcast(NULL, 2, 0);
402 static void Handle_OP_STOP_PLAYING(struct user *u)
405 printf("RND_SERVER: client %d (%s) stops game\n", u->number, u->nick);
406 broadcast(NULL, 2, 0);
409 static void Handle_OP_MOVE_FIGURE(struct user *u)
412 int last_client_nr = 0;
415 /* store player action */
416 for (v=user0; v; v=v->next)
418 if (v->number == u->number)
421 v->action_received = 1;
425 /* check if server received action from each player */
426 for (v=user0; v; v=v->next)
428 if (!v->action_received)
431 if (v->number > last_client_nr)
432 last_client_nr = v->number;
435 /* initialize all player actions to zero */
436 for (i=0; i<last_client_nr; i++)
439 /* broadcast actions of all players to all players */
440 for (v=user0; v; v=v->next)
442 buf[6 + v->number-1] = v->action;
444 v->action_received = 0;
447 buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
448 buf[3] = (unsigned char)((frame_counter >> 16) & 0xff);
449 buf[4] = (unsigned char)((frame_counter >> 8) & 0xff);
450 buf[5] = (unsigned char)((frame_counter >> 0) & 0xff);
452 broadcast(NULL, 6 + last_client_nr, 0);
458 printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n",
460 u->number, u->nick, buf[2]);
464 void NetworkServer(int port, int serveronly)
471 struct protoent *tcpproto;
476 struct sigaction sact;
485 if ((tcpproto = getprotobyname("tcp")) != NULL)
486 tcp = tcpproto->p_proto;
489 signal(SIGPIPE, SIG_IGN);
491 sact.sa_handler = SIG_IGN;
492 sigemptyset(&sact.sa_mask);
494 sigaction(SIGPIPE, &sact, NULL);
498 lfd = socket(PF_INET, SOCK_STREAM, 0);
499 saddr.sin_family = AF_INET;
500 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
501 saddr.sin_port = htons(port);
507 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
508 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
515 /* become a daemon, breaking all ties with the controlling terminal */
517 for (i=0; i<255; i++)
530 /* open a fake stdin, stdout, stderr, just in case */
531 open("/dev/null", O_RDONLY);
532 open("/dev/null", O_WRONLY);
533 open("/dev/null", O_WRONLY);
538 printf("rocksndiamonds network server: started up, listening on port %d\n",
540 printf("rocksndiamonds network server: using protocol version %d.%d.%d\n",
541 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
548 for (u=user0; u; u=u->next)
564 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
578 if (FD_ISSET(lfd, &fds))
582 slen = sizeof(saddr);
583 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
594 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
605 if (FD_ISSET(u->fd, &fds))
607 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
611 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
617 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
620 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
623 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
624 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
629 memcpy(buf, &u->readbuf[4], len);
631 memmove(u->readbuf, u->readbuf + 4 + len, u->nread);
634 if (!u->introduced && buf[1] != OP_NICKNAME)
637 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
647 Handle_OP_NICKNAME(u, len);
650 case OP_PROTOCOL_VERSION:
651 Handle_OP_PROTOCOL_VERSION(u, len);
654 case OP_NUMBER_WANTED:
655 Handle_OP_NUMBER_WANTED(u);
658 case OP_START_PLAYING:
659 Handle_OP_START_PLAYING(u);
662 case OP_PAUSE_PLAYING:
663 Handle_OP_PAUSE_PLAYING(u);
666 case OP_CONTINUE_PLAYING:
667 Handle_OP_CONTINUE_PLAYING(u);
670 case OP_STOP_PLAYING:
671 Handle_OP_STOP_PLAYING(u);
675 Handle_OP_MOVE_FIGURE(u);
681 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
682 broadcast(u, len, 0);
687 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
695 while (u && !interrupt);