-/* $NetBSD: driver.c,v 1.28 2014/03/29 21:27:08 dholland Exp $ */
+/* $NetBSD: driver.c,v 1.35 2014/03/30 01:44:37 dholland Exp $ */
/*
* Copyright (c) 1983-2003, Regents of the University of California.
* All rights reserved.
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: driver.c,v 1.28 2014/03/29 21:27:08 dholland Exp $");
+__RCSID("$NetBSD: driver.c,v 1.35 2014/03/30 01:44:37 dholland Exp $");
#endif /* not lint */
-#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
-#include <err.h>
-#include <errno.h>
-#include <signal.h>
+#include <sys/un.h>
+
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+
#include <stdlib.h>
#include <unistd.h>
-#include"hunt.h"
+#include <signal.h>
+#include <errno.h>
+#include <err.h>
+#include "hunt.h"
+#include "pathnames.h"
+#define INFTIM (-1)
-#ifdef INTERNET
-u_short Test_port = TEST_PORT;
-#else
-char *Sock_name = "/tmp/hunt";
-char *Stat_name = "/tmp/hunt.stats";
-#endif
+/*
+ * There are three listening sockets in this daemon:
+ * - a datagram socket that listens on a well known port
+ * - a stream socket for general player connections
+ * - a second stream socket that prints the stats/scores
+ *
+ * These are (now) named as follows:
+ * - contact (contactsock, contactaddr, etc.)
+ * - hunt (huntsock, huntaddr, etc.)
+ * - stat (statsock, stataddr, etc.)
+ *
+ * Until being cleaned up in 2014 the code used an assortment of
+ * inconsistent and confusing names to refer to the pieces of these:
+ *
+ * test_port -> contactaddr
+ * Test_port -> contactport
+ * Test_socket -> contactsock
+ *
+ * sock_port -> huntport
+ * Socket -> huntsock
+ *
+ * Daemon -> both stataddr and huntaddr
+ * stat_port -> statport
+ * status -> statsock
+ *
+ * It isn't clear to me what purpose contactsocket is supposed to
+ * serve; maybe it was supposed to avoid asking inetd to support
+ * tcp/wait sockets? Anyway, we can't really change the protocol at
+ * this point. (To complicate matters, contactsocket doesn't exist if
+ * using AF_UNIX sockets; you just connect to the game socket.)
+ *
+ * When using internet sockets:
+ * - the contact socket listens on INADDR_ANY using the game port
+ * (either specified or the default: CONTACT_PORT, currently
+ * spelled TEST_PORT)
+ * - the hunt socket listens on INADDR_ANY using whatever port
+ * - the stat socket listens on INADDR_ANY using whatever port
+ *
+ * When using AF_UNIX sockets:
+ * - the contact socket isn't used
+ * - the hunt socket listens on the game socket (either a specified path
+ * or /tmp/hunt)
+ * - the stat socket listens on its own socket (huntsocket's path +
+ * ".stats")
+ */
+
+static bool localmode; /* true -> AF_UNIX; false -> AF_INET */
+static bool inetd_spawned; /* invoked via inetd? */
+static bool standard_port = true; /* listening on standard port? */
+
+static struct sockaddr_storage huntaddr;
+static struct sockaddr_storage stataddr;
+static socklen_t huntaddrlen;
+static socklen_t stataddrlen;
-static SOCKET Daemon;
-
-#ifdef INTERNET
-static int Test_socket; /* test socket to answer datagrams */
-static bool inetd_spawned; /* invoked via inetd */
-static bool standard_port = true; /* true if listening on standard port */
-static u_short sock_port; /* port # of tcp listen socket */
-static u_short stat_port; /* port # of statistics tcp socket */
-#define DAEMON_SIZE (sizeof Daemon)
-#else
-#define DAEMON_SIZE (sizeof Daemon - 1)
+static uint16_t contactport = TEST_PORT;
+static uint16_t huntport; /* port # of tcp listen socket */
+static uint16_t statport; /* port # of statistics tcp socket */
+
+static const char *huntsockpath = PATH_HUNTSOCKET;
+static char *statsockpath;
+
+static int contactsock; /* socket to answer datagrams */
+int huntsock; /* main socket */
+static int statsock; /* stat socket */
+
+#ifdef VOLCANO
+static int volcano = 0; /* Explosion size */
#endif
static void clear_scores(void);
static void send_stats(void);
static void zap(PLAYER *, bool, int);
+static int
+getnum(const char *s, unsigned long *ret)
+{
+ char *t;
+
+ errno = 0;
+ *ret = strtoul(s, &t, 0);
+ if (errno || *t != '\0') {
+ return -1;
+ }
+ return 0;
+}
+
+static __dead void
+usage(const char *av0)
+{
+ fprintf(stderr, "Usage: %s [-s] [-p portnumber|socketpath]\n", av0);
+ exit(1);
+}
+
+static void
+makeaddr(const char *path, uint16_t port,
+ struct sockaddr_storage *ss, socklen_t *len)
+{
+ struct sockaddr_in *sin;
+ struct sockaddr_un *sun;
+
+ if (path == NULL) {
+ sin = (struct sockaddr_in *)ss;
+ sin->sin_family = AF_INET;
+ sin->sin_addr.s_addr = INADDR_ANY;
+ sin->sin_port = htons(port);
+ *len = sizeof(*sin);
+ } else {
+ sun = (struct sockaddr_un *)ss;
+ sun->sun_family = AF_UNIX;
+ strlcpy(sun->sun_path, path, sizeof(sun->sun_path));
+ *len = SUN_LEN(sun);
+ }
+}
+
+static uint16_t
+getsockport(int sock)
+{
+ struct sockaddr_storage addr;
+ socklen_t len;
+
+ len = sizeof(addr);
+ if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) {
+ complain(LOG_ERR, "getsockname");
+ exit(1);
+ }
+ switch (addr.ss_family) {
+ case AF_INET:
+ return ntohs(((struct sockaddr_in *)&addr)->sin_port);
+ case AF_INET6:
+ return ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
+ default:
+ break;
+ }
+ return 0;
+}
/*
* main:
main(int ac, char **av)
{
PLAYER *pp;
-#ifdef INTERNET
- u_short msg;
- short reply;
- socklen_t namelen;
- SOCKET test;
-#endif
+ unsigned long optargnum;
+ uint16_t msg, reply;
+ struct sockaddr_storage msgaddr;
+ socklen_t msgaddrlen;
static bool first = true;
static bool server = false;
int c, i;
case 's':
server = true;
break;
-#ifdef INTERNET
case 'p':
standard_port = false;
- Test_port = atoi(optarg);
+ if (getnum(optarg, &optargnum) < 0) {
+ localmode = true;
+ huntsockpath = optarg;
+ } else if (optargnum < 0xffff) {
+ localmode = false;
+ contactport = optargnum;
+ } else {
+ usage(av[0]);
+ }
break;
-#endif
default:
-erred:
- fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]);
- exit(1);
+ usage(av[0]);
}
}
if (optind < ac)
- goto erred;
+ usage(av[0]);
+ asprintf(&statsockpath, "%s.stats", huntsockpath);
init();
while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
{
if (errno != EINTR)
-#ifdef LOG
- syslog(LOG_WARNING, "poll: %m");
-#else
- warn("poll");
-#endif
+ complain(LOG_WARNING, "poll");
errno = 0;
}
-#ifdef INTERNET
- if (fdset[2].revents & POLLIN) {
- namelen = DAEMON_SIZE;
- (void) recvfrom(Test_socket, &msg, sizeof msg,
- 0, (struct sockaddr *) &test, &namelen);
+ if (!localmode && fdset[2].revents & POLLIN) {
+ msgaddrlen = sizeof(msgaddr);
+ (void) recvfrom(contactsock, &msg, sizeof msg,
+ 0, (struct sockaddr *)&msgaddr, &msgaddrlen);
switch (ntohs(msg)) {
case C_MESSAGE:
if (Nplayer <= 0)
break;
reply = htons((u_short) Nplayer);
- (void) sendto(Test_socket, &reply,
+ (void) sendto(contactsock, &reply,
sizeof reply, 0,
- (struct sockaddr *) &test, DAEMON_SIZE);
+ (struct sockaddr *)&msgaddr,
+ msgaddrlen);
break;
case C_SCORES:
- reply = htons(stat_port);
- (void) sendto(Test_socket, &reply,
+ reply = htons(statport);
+ (void) sendto(contactsock, &reply,
sizeof reply, 0,
- (struct sockaddr *) &test, DAEMON_SIZE);
+ (struct sockaddr *)&msgaddr,
+ msgaddrlen);
break;
case C_PLAYER:
case C_MONITOR:
if (msg == C_MONITOR && Nplayer <= 0)
break;
- reply = htons(sock_port);
- (void) sendto(Test_socket, &reply,
+ reply = htons(huntport);
+ (void) sendto(contactsock, &reply,
sizeof reply, 0,
- (struct sockaddr *) &test, DAEMON_SIZE);
+ (struct sockaddr *)&msgaddr,
+ msgaddrlen);
break;
}
}
-#endif
+
{
for (pp = Player, i = 0; pp < End_player; pp++, i++)
if (havechar(pp, i + 3)) {
init(void)
{
int i;
-#ifdef INTERNET
- SOCKET test_port;
- int msg;
+ struct sockaddr_storage stdinaddr;
+ struct sockaddr_storage contactaddr;
+ socklen_t contactaddrlen;
socklen_t len;
-#endif
#ifndef DEBUG
#ifdef TIOCNOTTY
(void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
#endif
- (void) setpgrp(getpid(), getpid());
+ (void) setpgid(getpid(), getpid());
(void) signal(SIGHUP, SIG_IGN);
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
#endif
/*
- * Initialize statistics socket
+ * check for inetd
*/
-#ifdef INTERNET
- Daemon.sin_family = SOCK_FAMILY;
- Daemon.sin_addr.s_addr = INADDR_ANY;
- Daemon.sin_port = 0;
-#else
- Daemon.sun_family = SOCK_FAMILY;
- (void) strcpy(Daemon.sun_path, Stat_name);
-#endif
-
- Status = socket(SOCK_FAMILY, SOCK_STREAM, 0);
- if (bind(Status, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
- if (errno == EADDRINUSE)
- exit(0);
+ len = sizeof(stdinaddr);
+ if (getsockname(STDIN_FILENO, (struct sockaddr *)&stdinaddr,
+ &len) >= 0) {
+ inetd_spawned = true;
+ /* force localmode, assimilate stdin as appropriate */
+ if (stdinaddr.ss_family == AF_UNIX) {
+ localmode = true;
+ contactsock = -1;
+ huntsock = STDIN_FILENO;
+ }
else {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
- cleanup(1);
+ localmode = false;
+ contactsock = STDIN_FILENO;
+ huntsock = -1;
}
+ } else {
+ /* keep value of localmode; no sockets yet */
+ contactsock = -1;
+ huntsock = -1;
}
- (void) listen(Status, 5);
-#ifdef INTERNET
- len = sizeof (SOCKET);
- if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "getsockname: %m");
-#else
- warn("getsockname");
-#endif
- exit(1);
+ /*
+ * initialize contact socket
+ */
+ if (!localmode && contactsock < 0) {
+ makeaddr(NULL, contactport, &contactaddr, &contactaddrlen);
+ contactsock = socket(AF_INET, SOCK_DGRAM, 0);
+ if (bind(contactsock, (struct sockaddr *) &contactaddr,
+ contactaddrlen) < 0) {
+ complain(LOG_ERR, "bind");
+ exit(1);
+ }
+ (void) listen(contactsock, 5);
}
- stat_port = ntohs(Daemon.sin_port);
-#endif
/*
* Initialize main socket
*/
-#ifdef INTERNET
- Daemon.sin_family = SOCK_FAMILY;
- Daemon.sin_addr.s_addr = INADDR_ANY;
- Daemon.sin_port = 0;
-#else
- Daemon.sun_family = SOCK_FAMILY;
- (void) strcpy(Daemon.sun_path, Sock_name);
-#endif
+ if (huntsock < 0) {
+ makeaddr(localmode ? huntsockpath : NULL, 0, &huntaddr,
+ &huntaddrlen);
+ huntsock = socket(huntaddr.ss_family, SOCK_STREAM, 0);
+ if (bind(huntsock, (struct sockaddr *)&huntaddr,
+ huntaddrlen) < 0) {
+ if (errno == EADDRINUSE)
+ exit(0);
+ else {
+ complain(LOG_ERR, "bind");
+ cleanup(1);
+ }
+ }
+ (void) listen(huntsock, 5);
+ }
- Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0);
-#if defined(INTERNET)
- msg = 1;
- if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
-#ifdef LOG
- syslog(LOG_WARNING, "setsockopt loopback %m");
-#else
- warn("setsockopt loopback");
-#endif
-#endif
- if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
+ /*
+ * Initialize statistics socket
+ */
+ makeaddr(localmode ? statsockpath : NULL, 0, &stataddr, &stataddrlen);
+ statsock = socket(stataddr.ss_family, SOCK_STREAM, 0);
+ if (bind(statsock, (struct sockaddr *)&stataddr, stataddrlen) < 0) {
if (errno == EADDRINUSE)
exit(0);
else {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
+ complain(LOG_ERR, "bind");
cleanup(1);
}
}
- (void) listen(Socket, 5);
+ (void) listen(statsock, 5);
-#ifdef INTERNET
- len = sizeof (SOCKET);
- if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "getsockname: %m");
-#else
- warn("getsockname");
-#endif
- exit(1);
+ if (!localmode) {
+ contactport = getsockport(contactsock);
+ statport = getsockport(statsock);
+ huntport = getsockport(huntsock);
+ if (contactport != TEST_PORT) {
+ standard_port = false;
+ }
}
- sock_port = ntohs(Daemon.sin_port);
-#endif
/*
* Initialize minimal poll mask
*/
- fdset[0].fd = Socket;
+ fdset[0].fd = huntsock;
fdset[0].events = POLLIN;
- fdset[1].fd = Status;
+ fdset[1].fd = statsock;
fdset[1].events = POLLIN;
-
-#ifdef INTERNET
- len = sizeof (SOCKET);
- if (getsockname(0, (struct sockaddr *) &test_port, &len) >= 0
- && test_port.sin_family == AF_INET) {
- inetd_spawned = true;
- Test_socket = 0;
- if (test_port.sin_port != htons((u_short) Test_port)) {
- standard_port = false;
- Test_port = ntohs(test_port.sin_port);
- }
+ if (localmode) {
+ fdset[2].fd = -1;
+ fdset[2].events = 0;
} else {
- test_port = Daemon;
- test_port.sin_port = htons((u_short) Test_port);
-
- Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
- if (bind(Test_socket, (struct sockaddr *) &test_port,
- DAEMON_SIZE) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
- exit(1);
- }
- (void) listen(Test_socket, 5);
+ fdset[2].fd = contactsock;
+ fdset[2].events = POLLIN;
}
- fdset[2].fd = Test_socket;
- fdset[2].events = POLLIN;
-#else
- fdset[2].fd = -1;
-#endif
-
srandom(time(NULL));
makemaze();
#ifdef BOOTS
if (!(fdset[i].revents & POLLIN))
return false;
check_again:
- errno = 0;
- if ((pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf)) <= 0)
- {
+ pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf);
+ if (pp->p_nchar < 0 && errno == EINTR) {
+ goto check_again;
+ } else if (pp->p_nchar <= 0) {
if (errno == EINTR)
- goto check_again;
pp->p_cbuf[0] = 'q';
}
pp->p_ncount = 0;
* Exit with the given value, cleaning up any droppings lying around
*/
void
-cleanup(int eval)
+cleanup(int exitval)
{
PLAYER *pp;
(void) fclose(pp->p_output);
}
#endif
- (void) close(Socket);
+ (void) close(huntsock);
#ifdef AF_UNIX_HACK
- (void) unlink(Sock_name);
+ (void) unlink(huntsockpath);
#endif
- exit(eval);
+ exit(exitval);
}
/*
IDENT *ip;
FILE *fp;
int s;
- SOCKET sockstruct;
+ struct sockaddr_storage newaddr;
socklen_t socklen;
/*
* Get the output stream ready
*/
-#ifdef INTERNET
- socklen = sizeof sockstruct;
-#else
- socklen = sizeof sockstruct - 1;
-#endif
- s = accept(Status, (struct sockaddr *) &sockstruct, &socklen);
+ socklen = sizeof(newaddr);
+ s = accept(statsock, (struct sockaddr *)&newaddr, &socklen);
if (s < 0) {
if (errno == EINTR)
return;
-#ifdef LOG
- syslog(LOG_WARNING, "accept: %m");
-#else
- warn("accept");
-#endif
+ complain(LOG_WARNING, "accept");
return;
}
fp = fdopen(s, "w");
if (fp == NULL) {
-#ifdef LOG
- syslog(LOG_WARNING, "fdopen: %m");
-#else
- warn("fdopen");
-#endif
+ complain(LOG_WARNING, "fdopen");
(void) close(s);
return;
}