]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - hunt/huntd/driver.c
cgram: rename local functions
[bsdgames-darwin.git] / hunt / huntd / driver.c
index 5fcc9b20d455f5f94634f5310cdbdf4ddb2493a7..01fc5945cf60d6dbe5ec373f5a4b1984cc9d6a25 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: driver.c,v 1.15 2009/07/04 01:01:18 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.15 2009/07/04 01:01:18 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/stat.h>
-# include      <sys/time.h>
-# include      <err.h>
-# include      <errno.h>
-# include      <signal.h>
-# include      <stdlib.h>
-# include      <unistd.h>
-# include      "hunt.h"
-
-# ifndef pdp11
-# define       RN      (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff)
-# else
-# define       RN      ((Seed = Seed * 11109 + 13849) & 0x7fff)
-# endif
-
-int    Seed = 0;
-
-
-SOCKET Daemon;
-char   *First_arg;             /* pointer to argv[0] */
-char   *Last_arg;              /* pointer to end of argv/environ */
-# ifdef        INTERNET
-int    Test_socket;            /* test socket to answer datagrams */
-FLAG   inetd_spawned;          /* invoked via inetd */
-FLAG   standard_port = TRUE;   /* true if listening on standard port */
-u_short        sock_port;              /* port # of tcp listen socket */
-u_short        stat_port;              /* port # of statistics tcp socket */
-# define       DAEMON_SIZE     (sizeof Daemon)
-# else
-# define       DAEMON_SIZE     (sizeof Daemon - 1)
-# endif
-
-static void    clear_scores(void);
-static int     havechar(PLAYER *, int);
-static void    init(void);
-       int     main(int, char *[], char *[]);
-static void    makeboots(void);
-static void    send_stats(void);
-static void    zap(PLAYER *, FLAG, int);
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/time.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 <signal.h>
+#include <errno.h>
+#include <err.h>
+
+#include "hunt.h"
+#include "pathnames.h"
+#define INFTIM (-1)
+
+/*
+ * 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 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 bool havechar(PLAYER *, int);
+static void init(void);
+static void makeboots(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:
  *     The main program.
  */
 int
-main(ac, av, ep)
-       int     ac;
-       char    **av, **ep;
+main(int ac, char **av)
 {
-       PLAYER  *pp;
-# ifdef INTERNET
-       u_short msg;
-       short   port_num, reply;
-       socklen_t       namelen;
-       SOCKET  test;
-# endif
-       static FLAG     first = TRUE;
-       static FLAG     server = FALSE;
-       int             c, i;
-       const int       linger = 90 * 1000;
-
-       First_arg = av[0];
-       if (ep == NULL || *ep == NULL)
-               ep = av + ac;
-       while (*ep)
-               ep++;
-       Last_arg = ep[-1] + strlen(ep[-1]);
+       PLAYER *pp;
+       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;
+       const int linger = 90 * 1000;
 
        while ((c = getopt(ac, av, "sp:")) != -1) {
                switch (c) {
                  case 's':
-                       server = TRUE;
+                       server = true;
                        break;
-# ifdef INTERNET
                  case 'p':
-                       standard_port = FALSE;
-                       Test_port = atoi(optarg);
+                       standard_port = false;
+                       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();
 
 
@@ -134,80 +247,76 @@ again:
                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;
-                       port_num = htons(sock_port);
-                       (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)) {
                                        execute(pp);
                                        pp->p_nexec++;
                                }
-# ifdef MONITOR
+#ifdef MONITOR
                        for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++)
                                if (havechar(pp, i + MAXPL + 3)) {
                                        mon_execute(pp);
                                        pp->p_nexec++;
                                }
-# endif
+#endif
                        moveshots();
                        for (pp = Player, i = 0; pp < End_player; )
                                if (pp->p_death[0] != '\0')
-                                       zap(pp, TRUE, i + 3);
+                                       zap(pp, true, i + 3);
                                else
                                        pp++, i++;
-# ifdef MONITOR
+#ifdef MONITOR
                        for (pp = Monitor, i = 0; pp < End_monitor; )
                                if (pp->p_death[0] != '\0')
-                                       zap(pp, FALSE, i + MAXPL + 3);
+                                       zap(pp, false, i + MAXPL + 3);
                                else
                                        pp++, i++;
-# endif
+#endif
                }
                if (fdset[0].revents & POLLIN)
                        if (answer()) {
-# ifdef INTERNET
-                               if (first && standard_port)
-                                       faketalk();
-# endif
-                               first = FALSE;
+                               if (first) {
+                                       /* announce start of game? */
+                               }
+                               first = false;
                        }
                if (fdset[1].revents & POLLIN)
                        send_stats();
@@ -217,14 +326,14 @@ again:
                        pp->p_nexec = 0;
                        (void) fflush(pp->p_output);
                }
-# ifdef MONITOR
+#ifdef MONITOR
                for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) {
                        if (fdset[i + MAXPL + 3].revents & POLLIN)
                                sendcom(pp, READY, pp->p_nexec);
                        pp->p_nexec = 0;
                        (void) fflush(pp->p_output);
                }
-# endif
+#endif
        } while (Nplayer > 0);
 
        if (poll(fdset, 3+MAXPL+MAXMON, linger) > 0) {
@@ -234,17 +343,17 @@ again:
                clear_scores();
                makemaze();
                clearwalls();
-# ifdef BOOTS
+#ifdef BOOTS
                makeboots();
-# endif
-               first = TRUE;
+#endif
+               first = true;
                goto again;
        }
 
-# ifdef MONITOR
+#ifdef MONITOR
        for (pp = Monitor, i = 0; pp < End_monitor; i++)
-               zap(pp, FALSE, i + MAXPL + 3);
-# endif
+               zap(pp, false, i + MAXPL + 3);
+#endif
        cleanup(0);
        /* NOTREACHED */
        return(0);
@@ -255,198 +364,158 @@ again:
  *     Initialize the global parameters.
  */
 static void
-init()
+init(void)
 {
-       int     i;
-# ifdef        INTERNET
-       SOCKET  test_port;
-       int     msg;
-       socklen_t       len;
-# endif
-
-# ifndef DEBUG
-# ifdef TIOCNOTTY
+       int i;
+       struct sockaddr_storage stdinaddr;
+       struct sockaddr_storage contactaddr;
+       socklen_t contactaddrlen;
+       socklen_t len;
+
+#ifndef DEBUG
+#ifdef TIOCNOTTY
        (void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
-# endif
-       (void) setpgrp(getpid(), getpid());
+#endif
+       (void) setpgid(getpid(), getpid());
        (void) signal(SIGHUP, SIG_IGN);
        (void) signal(SIGINT, SIG_IGN);
        (void) signal(SIGQUIT, SIG_IGN);
        (void) signal(SIGTERM, cleanup);
-# endif
+#endif
 
        (void) chdir("/var/tmp");       /* just in case it core dumps */
        (void) umask(0);                /* No privacy at all! */
        (void) signal(SIGPIPE, SIG_IGN);
 
-# ifdef LOG
-# ifdef        SYSLOG_43
+#ifdef LOG
        openlog("huntd", LOG_PID, LOG_DAEMON);
-# endif
-# ifdef        SYSLOG_42
-       openlog("huntd", LOG_PID);
-# endif
-# endif
+#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
-
-       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) {
+       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);
+       }
+
+       /*
+        * 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);
-
-# 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);
+       (void) listen(statsock, 5);
+
+       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
-
-       Seed = getpid() + time((time_t *) NULL);
+       srandom(time(NULL));
        makemaze();
-# ifdef BOOTS
+#ifdef BOOTS
        makeboots();
-# endif
+#endif
 
        for (i = 0; i < NASCII; i++)
-               See_over[i] = TRUE;
-       See_over[DOOR] = FALSE;
-       See_over[WALL1] = FALSE;
-       See_over[WALL2] = FALSE;
-       See_over[WALL3] = FALSE;
-# ifdef REFLECT
-       See_over[WALL4] = FALSE;
-       See_over[WALL5] = FALSE;
-# endif
+               See_over[i] = true;
+       See_over[DOOR] = false;
+       See_over[WALL1] = false;
+       See_over[WALL2] = false;
+       See_over[WALL3] = false;
+#ifdef REFLECT
+       See_over[WALL4] = false;
+       See_over[WALL5] = false;
+#endif
 
 }
 
-# ifdef BOOTS
+#ifdef BOOTS
 /*
  * makeboots:
  *     Put the boots in the maze
  */
 static void
-makeboots()
+makeboots(void)
 {
-       int     x, y;
-       PLAYER  *pp;
+       int x, y;
+       PLAYER *pp;
 
        do {
                x = rand_num(WIDTH - 1) + 1;
@@ -456,7 +525,7 @@ makeboots()
        for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
                pp->p_flying = -1;
 }
-# endif
+#endif
 
 
 /*
@@ -464,17 +533,14 @@ makeboots()
  *     Check the damage to the given player, and see if s/he is killed
  */
 void
-checkdam(ouch, gotcha, credit, amt, this_shot_type)
-       PLAYER  *ouch, *gotcha;
-       IDENT   *credit;
-       int     amt;
-       char    this_shot_type;
+checkdam(PLAYER *ouch, PLAYER *gotcha, IDENT *credit, int amt,
+        char this_shot_type)
 {
-       const char      *cp;
+       const char *cp;
 
        if (ouch->p_death[0] != '\0')
                return;
-# ifdef BOOTS
+#ifdef BOOTS
        if (this_shot_type == SLIME)
                switch (ouch->p_nboots) {
                  default:
@@ -487,7 +553,7 @@ checkdam(ouch, gotcha, credit, amt, this_shot_type)
                                message(gotcha, "He has boots on!");
                        return;
                }
-# endif
+#endif
        ouch->p_damage += amt;
        if (ouch->p_damage <= ouch->p_damcap) {
                (void) snprintf(Buf, sizeof(Buf), "%2d", ouch->p_damage);
@@ -501,11 +567,11 @@ checkdam(ouch, gotcha, credit, amt, this_shot_type)
          default:
                cp = "Killed";
                break;
-# ifdef FLY
+#ifdef FLY
          case FALL:
                cp = "Killed on impact";
                break;
-# endif
+#endif
          case KNIFE:
                cp = "Stabbed to death";
                ouch->p_ammo = 0;               /* No exploding */
@@ -522,23 +588,23 @@ checkdam(ouch, gotcha, credit, amt, this_shot_type)
          case GMINE:
                cp = "Blown apart";
                break;
-# ifdef        OOZE
+#ifdef OOZE
          case SLIME:
                cp = "Slimed";
                if (credit != NULL)
                        credit->i_slime++;
                break;
-# endif
-# ifdef        VOLCANO
+#endif
+#ifdef VOLCANO
          case LAVA:
                cp = "Baked";
                break;
-# endif
-# ifdef DRONE
+#endif
+#ifdef DRONE
          case DSHOT:
                cp = "Eliminated";
                break;
-# endif
+#endif
        }
        if (credit == NULL) {
                (void) snprintf(ouch->p_death, sizeof(ouch->p_death),
@@ -588,13 +654,13 @@ checkdam(ouch, gotcha, credit, amt, this_shot_type)
                        STAT_NAME_COL);
                outstr(ouch, Buf, 5);
        }
-# ifdef MONITOR
+#ifdef MONITOR
        for (ouch = Monitor; ouch < End_monitor; ouch++) {
                cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
                        STAT_NAME_COL);
                outstr(ouch, Buf, 5);
        }
-# endif
+#endif
 }
 
 /*
@@ -602,21 +668,17 @@ checkdam(ouch, gotcha, credit, amt, this_shot_type)
  *     Kill off a player and take him out of the game.
  */
 static void
-zap(pp, was_player, i)
-       PLAYER  *pp;
-       FLAG    was_player;
-       int     i;
+zap(PLAYER *pp, bool was_player, int i)
 {
-       int     n, len;
-       BULLET  *bp;
-       PLAYER  *np;
-       int     x, y;
-       int     savefd;
+       int n, len;
+       BULLET *bp;
+       PLAYER *np;
+       int x, y;
 
        if (was_player) {
                if (pp->p_undershot)
                        fixshots(pp->p_y, pp->p_x, pp->p_over);
-               drawplayer(pp, FALSE);
+               drawplayer(pp, false);
                Nplayer--;
        }
 
@@ -634,11 +696,9 @@ zap(pp, was_player, i)
        outstr(pp, pp->p_death, len);
        cgoto(pp, HEIGHT, 0);
 
-       savefd = pp->p_fd;
-
-# ifdef MONITOR
+#ifdef MONITOR
        if (was_player) {
-# endif
+#endif
                for (bp = Bullets; bp != NULL; bp = bp->b_next) {
                        if (bp->b_owner == pp)
                                bp->b_owner = NULL;
@@ -674,23 +734,23 @@ zap(pp, was_player, i)
                }
                if (x > 0) {
                        (void) add_shot(len, pp->p_y, pp->p_x, pp->p_face, x,
-                               (PLAYER *) NULL, TRUE, SPACE);
+                               NULL, true, SPACE);
                        (void) snprintf(Buf, sizeof(Buf), "%s detonated.",
                                pp->p_ident->i_name);
                        for (np = Player; np < End_player; np++)
                                message(np, Buf);
-# ifdef MONITOR
+#ifdef MONITOR
                        for (np = Monitor; np < End_monitor; np++)
                                message(np, Buf);
-# endif
-# ifdef BOOTS
+#endif
+#ifdef BOOTS
                        while (pp->p_nboots-- > 0) {
                                for (np = Boot; np < &Boot[NBOOTS]; np++)
                                        if (np->p_flying < 0)
                                                break;
                                if (np >= &Boot[NBOOTS])
                                        err(1, "Too many boots");
-                               np->p_undershot = FALSE;
+                               np->p_undershot = false;
                                np->p_x = pp->p_x;
                                np->p_y = pp->p_y;
                                np->p_flying = rand_num(20);
@@ -700,9 +760,9 @@ zap(pp, was_player, i)
                                np->p_face = BOOT;
                                showexpl(np->p_y, np->p_x, BOOT);
                        }
-# endif
+#endif
                }
-# ifdef BOOTS
+#ifdef BOOTS
                else if (pp->p_nboots > 0) {
                        if (pp->p_nboots == 2)
                                Maze[pp->p_y][pp->p_x] = BOOT_PAIR;
@@ -712,9 +772,9 @@ zap(pp, was_player, i)
                                fixshots(pp->p_y, pp->p_x,
                                        Maze[pp->p_y][pp->p_x]);
                }
-# endif
+#endif
 
-# ifdef VOLCANO
+#ifdef VOLCANO
                volcano += pp->p_ammo - x;
                if (rand_num(100) < volcano / 50) {
                        do {
@@ -722,14 +782,14 @@ zap(pp, was_player, i)
                                y = rand_num(HEIGHT / 2) + HEIGHT / 4;
                        } while (Maze[y][x] != SPACE);
                        (void) add_shot(LAVA, y, x, LEFTS, volcano,
-                               (PLAYER *) NULL, TRUE, SPACE);
+                               NULL, true, SPACE);
                        for (np = Player; np < End_player; np++)
                                message(np, "Volcano eruption.");
                        volcano = 0;
                }
-# endif
+#endif
 
-# ifdef        DRONE
+#ifdef DRONE
                if (rand_num(100) < 2) {
                        do {
                                x = rand_num(WIDTH / 2) + WIDTH / 4;
@@ -738,9 +798,9 @@ zap(pp, was_player, i)
                        add_shot(DSHOT, y, x, rand_dir(),
                                shot_req[MINDSHOT +
                                rand_num(MAXBOMB - MINDSHOT)],
-                               (PLAYER *) NULL, FALSE, SPACE);
+                               NULL, false, SPACE);
                }
-# endif
+#endif
 
                sendcom(pp, ENDWIN);
                (void) putc(' ', pp->p_output);
@@ -759,12 +819,12 @@ zap(pp, was_player, i)
                                cgoto(np, n, STAT_NAME_COL);
                                outstr(np, Buf, STAT_NAME_LEN);
                        }
-# ifdef MONITOR
+#ifdef MONITOR
                        for (np = Monitor; np < End_monitor; np++) {
                                cgoto(np, n, STAT_NAME_COL);
                                outstr(np, Buf, STAT_NAME_LEN);
                        }
-# endif
+#endif
                } else
                        fdset[i].fd = -1;
 
@@ -774,7 +834,7 @@ zap(pp, was_player, i)
                        cgoto(np, n, STAT_NAME_COL);
                        ce(np);
                }
-# ifdef MONITOR
+#ifdef MONITOR
                for (np = Monitor; np < End_monitor; np++) {
                        cgoto(np, n, STAT_NAME_COL);
                        ce(np);
@@ -816,7 +876,7 @@ zap(pp, was_player, i)
                        ce(np);
                }
        }
-# endif
+#endif
 }
 
 /*
@@ -824,49 +884,45 @@ zap(pp, was_player, i)
  *     Return a random number in a given range.
  */
 int
-rand_num(range)
-       int     range;
+rand_num(int range)
 {
-       return (range == 0 ? 0 : RN % range);
+       return (range == 0 ? 0 : random() % range);
 }
 
 /*
  * havechar:
  *     Check to see if we have any characters in the input queue; if
- *     we do, read them, stash them away, and return TRUE; else return
- *     FALSE.
+ *     we do, read them, stash them away, and return true; else return
+ *     false.
  */
-static int
-havechar(pp, i)
-       PLAYER  *pp;
-       int     i;
+static bool
+havechar(PLAYER *pp, int i)
 {
 
        if (pp->p_ncount < pp->p_nchar)
-               return TRUE;
+               return true;
        if (!(fdset[i].revents & POLLIN))
-               return FALSE;
+               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;
-       return TRUE;
+       return true;
 }
 
 /*
  * cleanup:
  *     Exit with the given value, cleaning up any droppings lying around
  */
-SIGNAL_TYPE
-cleanup(eval)
-       int     eval;
+void
+cleanup(int exitval)
 {
-       PLAYER  *pp;
+       PLAYER *pp;
 
        for (pp = Player; pp < End_player; pp++) {
                cgoto(pp, HEIGHT, 0);
@@ -874,20 +930,20 @@ cleanup(eval)
                (void) putc(LAST_PLAYER, pp->p_output);
                (void) fclose(pp->p_output);
        }
-# ifdef MONITOR
+#ifdef MONITOR
        for (pp = Monitor; pp < End_monitor; pp++) {
                cgoto(pp, HEIGHT, 0);
                sendcom(pp, ENDWIN);
                (void) putc(LAST_PLAYER, pp->p_output);
                (void) fclose(pp->p_output);
        }
-# endif
-       (void) close(Socket);
-# ifdef AF_UNIX_HACK
-       (void) unlink(Sock_name);
-# endif
+#endif
+       (void) close(huntsock);
+#ifdef AF_UNIX_HACK
+       (void) unlink(huntsockpath);
+#endif
 
-       exit(eval);
+       exit(exitval);
 }
 
 /*
@@ -895,40 +951,28 @@ cleanup(eval)
  *     Print stats to requestor
  */
 static void
-send_stats()
+send_stats(void)
 {
-       IDENT   *ip;
-       FILE    *fp;
-       int     s;
-       SOCKET  sockstruct;
-       socklen_t       socklen;
+       IDENT *ip;
+       FILE *fp;
+       int s;
+       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;
        }
@@ -971,9 +1015,9 @@ send_stats()
  *     Clear out the scores so the next session start clean
  */
 static void
-clear_scores()
+clear_scores(void)
 {
-       IDENT   *ip, *nextip;
+       IDENT *ip, *nextip;
 
        for (ip = Scores; ip != NULL; ip = nextip) {
                nextip = ip->i_next;