I assume it would fail on any other address, but I was just attempting to bind to loopback. I also assume it will (eventually, but that depends on the compiler, on defaults, on fallbacks, on magic numbers...) fail on other BSDs.
Two things solved it for me. One was bzero-ing the whole struct sockaddr_in (which is standard practice), the other was filling in sin_len. You will need to #ifdef filling sin_len as not every OS sports this field, but all the BSDs do, as far as I can tell.
"Works-for-me" patch follows.
diff --git a/server.c b/server.c
index 8b85519..0aed41c 100644
--- a/server.c
+++ b/server.c
@@ -25,10 +25,12 @@ socket_setup(const char *ip, short port) {
struct sockaddr_in addr;
int fd, ret;
+ bzero(&addr, sizeof(addr));
+ addr.sin_len = sizeof(struct sockaddr_in);
+
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
- memset(&(addr.sin_addr), 0, sizeof(addr.sin_addr));
addr.sin_addr.s_addr = inet_addr(ip);
/* this sad list of tests could use a Maybe monad... */
I assume it would fail on any other address, but I was just attempting to bind to loopback. I also assume it will (eventually, but that depends on the compiler, on defaults, on fallbacks, on magic numbers...) fail on other BSDs.
Two things solved it for me. One was bzero-ing the whole struct sockaddr_in (which is standard practice), the other was filling in sin_len. You will need to #ifdef filling sin_len as not every OS sports this field, but all the BSDs do, as far as I can tell.
"Works-for-me" patch follows.