Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/hunt/huntd Remove all the conditional compilation for ...



details:   https://anonhg.NetBSD.org/src/rev/40d3c42e8835
branches:  trunk
changeset: 795013:40d3c42e8835
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Mar 30 01:44:37 2014 +0000

description:
Remove all the conditional compilation for INTERNET. Now you can run
this on either a local or internet socket (including via inetd on
either) and it will, or is supposed to, DTRT.

Does not really support ipv6 yet, but in a number of places will no
longer vomit or exhibit UB if it encounters an ipv6 address.

diffstat:

 games/hunt/huntd/answer.c |   53 ++++--
 games/hunt/huntd/driver.c |  331 ++++++++++++++++++++++++++-------------------
 games/hunt/huntd/hunt.h   |   11 +-
 games/hunt/huntd/huntd.6  |   10 +-
 4 files changed, 231 insertions(+), 174 deletions(-)

diffs (truncated from 638 to 300 lines):

diff -r ed594f6fb9d7 -r 40d3c42e8835 games/hunt/huntd/answer.c
--- a/games/hunt/huntd/answer.c Sun Mar 30 01:19:20 2014 +0000
+++ b/games/hunt/huntd/answer.c Sun Mar 30 01:44:37 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: answer.c,v 1.20 2014/03/30 00:26:58 dholland Exp $     */
+/*     $NetBSD: answer.c,v 1.21 2014/03/30 01:44:37 dholland Exp $     */
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,16 +32,18 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: answer.c,v 1.20 2014/03/30 00:26:58 dholland Exp $");
+__RCSID("$NetBSD: answer.c,v 1.21 2014/03/30 01:44:37 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <netinet/in.h>
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <assert.h>
 #include "hunt.h"
 
 #define SCOREDECAY     15
@@ -64,33 +66,48 @@
        static socklen_t socklen;
        static uint32_t machine;
        static uint32_t uid;
-       static SOCKET sockstruct;
+       static struct sockaddr_storage newaddr;
        char *cp1, *cp2;
        int flags;
        uint32_t version;
        int i;
 
-#ifdef INTERNET
-       socklen = sizeof sockstruct;
-#else
-       socklen = sizeof sockstruct - 1;
-#endif
-       errno = 0;
-       newsock = accept(huntsock, (struct sockaddr *) &sockstruct, &socklen);
-       if (newsock < 0)
-       {
+       socklen = sizeof(newaddr);
+       newsock = accept(huntsock, (struct sockaddr *)&newaddr, &socklen);
+       if (newsock < 0) {
                if (errno == EINTR)
                        return false;
                complain(LOG_ERR, "accept");
                cleanup(1);
        }
 
-#ifdef INTERNET
-       machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr);
-#else
-       if (machine == 0)
+       /*
+        * XXX this is pretty bollocks
+        */
+       switch (newaddr.ss_family) {
+           case AF_INET:
+               machine = ((struct sockaddr_in *)&newaddr)->sin_addr.s_addr;
+               machine = ntohl(machine);
+               break;
+           case AF_INET6: 
+               {
+                       struct sockaddr_in6 *sin6;
+
+                       sin6 = (struct sockaddr_in6 *)&newaddr;
+                       assert(sizeof(sin6->sin6_addr.s6_addr) >
+                              sizeof(machine));
+                       memcpy(&machine, sin6->sin6_addr.s6_addr,
+                              sizeof(machine));
+               }
+               break;
+           case AF_UNIX:
                machine = gethostid();
-#endif
+               break;
+           default:
+               machine = 0; /* ? */
+               break;
+       }
+
        version = htonl((uint32_t) HUNT_VERSION);
        (void) write(newsock, &version, LONGLEN);
        (void) read(newsock, &uid, LONGLEN);
@@ -127,7 +144,6 @@
                        *cp2++ = *cp1;
        *cp2 = '\0';
 
-#ifdef INTERNET
        if (mode == C_MESSAGE) {
                char    buf[BUFSIZ + 1];
                int     n;
@@ -155,7 +171,6 @@
                return false;
        }
        else
-#endif
 #ifdef MONITOR
        if (mode == C_MONITOR)
                if (End_monitor < &Monitor[MAXMON]) {
diff -r ed594f6fb9d7 -r 40d3c42e8835 games/hunt/huntd/driver.c
--- a/games/hunt/huntd/driver.c Sun Mar 30 01:19:20 2014 +0000
+++ b/games/hunt/huntd/driver.c Sun Mar 30 01:44:37 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: driver.c,v 1.34 2014/03/30 00:26:58 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.
@@ -32,18 +32,25 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: driver.c,v 1.34 2014/03/30 00:26:58 dholland Exp $");
+__RCSID("$NetBSD: driver.c,v 1.35 2014/03/30 01:44:37 dholland Exp $");
 #endif /* not lint */
 
 #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 <signal.h>
+#include <errno.h>
+#include <err.h>
 
 #include "hunt.h"
 #include "pathnames.h"
@@ -94,31 +101,25 @@
  *      ".stats")
  */
 
-#ifdef INTERNET
-static struct sockaddr_in huntaddr;
-static struct sockaddr_in stataddr;
+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 */
-#define STATSOCKADDR_SIZE      (sizeof statsockaddr)
-#else
-static struct sockaddr_un huntaddr;
-static struct sockaddr_un stataddr;
-static const char huntsockpath[] = PATH_HUNTSOCKET;
-static const char statsockpath[] = PATH_STATSOCKET;
-#define STATSOCKADDR_SIZE      (sizeof statsockaddr - 1)
-#endif
-
 int huntsock;                          /* main socket */
 static int statsock;                   /* stat socket */
-static socklen_t huntaddrlen;
-static socklen_t stataddrlen;
-
-#ifdef INTERNET
-static bool inetd_spawned;             /* invoked via inetd */
-static bool standard_port = true;      /* true if listening on standard port */
-#endif
 
 #ifdef VOLCANO
 static int volcano = 0;                        /* Explosion size */
@@ -131,6 +132,68 @@
 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:
@@ -140,12 +203,10 @@
 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;
@@ -156,21 +217,26 @@
                  case 's':
                        server = true;
                        break;
-#ifdef INTERNET
                  case 'p':
                        standard_port = false;
-                       contactport = 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:



Home | Main Index | Thread Index | Old Index