Source-Changes-HG archive

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

[src/trunk]: src/games/hunt/hunt I wish the socket API didn't require casts. ...



details:   https://anonhg.NetBSD.org/src/rev/c9aef799f08d
branches:  trunk
changeset: 328284:c9aef799f08d
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Mar 30 04:57:37 2014 +0000

description:
I wish the socket API didn't require casts. Easy to mess them up.

diffstat:

 games/hunt/hunt/hunt.c   |   8 ++++----
 games/hunt/hunt/server.c |  16 ++++++++++------
 2 files changed, 14 insertions(+), 10 deletions(-)

diffs (106 lines):

diff -r b5ba92076093 -r c9aef799f08d games/hunt/hunt/hunt.c
--- a/games/hunt/hunt/hunt.c    Sun Mar 30 04:40:50 2014 +0000
+++ b/games/hunt/hunt/hunt.c    Sun Mar 30 04:57:37 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hunt.c,v 1.52 2014/03/30 04:40:50 dholland Exp $       */
+/*     $NetBSD: hunt.c,v 1.53 2014/03/30 04:57:37 dholland Exp $       */
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: hunt.c,v 1.52 2014/03/30 04:40:50 dholland Exp $");
+__RCSID("$NetBSD: hunt.c,v 1.53 2014/03/30 04:57:37 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -449,10 +449,10 @@
        printf("\n%s:\n", lookuphost(host, hostlen));
        fflush(stdout);
 
-       s = socket(SOCK_FAMILY, SOCK_STREAM, 0);
+       s = socket(host->ss_family, SOCK_STREAM, 0);
        if (s < 0)
                err(1, "socket");
-       if (connect(s, (struct sockaddr *) &host, sizeof host) < 0)
+       if (connect(s, (const struct sockaddr *)host, hostlen) < 0)
                err(1, "connect");
        while ((cnt = read(s, buf, BUFSIZ)) > 0)
                write(fileno(stdout), buf, cnt);
diff -r b5ba92076093 -r c9aef799f08d games/hunt/hunt/server.c
--- a/games/hunt/hunt/server.c  Sun Mar 30 04:40:50 2014 +0000
+++ b/games/hunt/hunt/server.c  Sun Mar 30 04:57:37 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: server.c,v 1.7 2014/03/30 04:39:40 dholland Exp $      */
+/*     $NetBSD: server.c,v 1.8 2014/03/30 04:57:37 dholland Exp $      */
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: server.c,v 1.7 2014/03/30 04:39:40 dholland Exp $");
+__RCSID("$NetBSD: server.c,v 1.8 2014/03/30 04:57:37 dholland Exp $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -97,13 +97,15 @@
 }
 
 static void
-add_daemon_addr(const struct sockaddr_storage *addr, uint16_t port_num)
+add_daemon_addr(const struct sockaddr_storage *addr, socklen_t addrlen,
+               uint16_t port_num)
 {
        const struct sockaddr_in *sin;
 
        if (addr->ss_family != AF_INET) {
                return;
        }
+       assert(addrlen == sizeof(struct sockaddr_in));
        sin = (const struct sockaddr_in *)addr;
 
        assert(numdaemons <= maxdaemons);
@@ -139,7 +141,7 @@
 }
 
 static bool
-have_daemon_addr(const struct sockaddr_storage *addr)
+have_daemon_addr(const struct sockaddr_storage *addr, socklen_t addrlen)
 {
        unsigned j;
        const struct sockaddr_in *sin;
@@ -147,6 +149,7 @@
        if (addr->ss_family != AF_INET) {
                return false;
        }
+       assert(addrlen == sizeof(struct sockaddr_in));
        sin = (const struct sockaddr_in *)addr;
 
        for (j = 0; j < numdaemons; j++) {
@@ -207,6 +210,7 @@
        int option;
        int i;
 
+       memset(&contactaddr, 0, sizeof(contactaddr));
        contactaddr.sin_family = SOCK_FAMILY;
        contactaddr.sin_port = htons(port);
 
@@ -311,12 +315,12 @@
                        /* trash, ignore it */
                        continue;
                }
-               if (have_daemon_addr(&addr)) {
+               if (have_daemon_addr(&addr, addrlen)) {
                        /* this shouldn't happen */
                        continue;
                }
 
-               add_daemon_addr(&addr, port_num);
+               add_daemon_addr(&addr, addrlen, port_num);
        }
 
        initial = false;



Home | Main Index | Thread Index | Old Index