nicolasff / webdis

A Redis HTTP interface with JSON output
https://webd.is
BSD 2-Clause "Simplified" License
2.82k stars 305 forks source link

EADDRNOTAVAIL when trying to bind to 127.0.0.1 on Snow Leopard #51

Closed andrebraga closed 12 years ago

andrebraga commented 12 years ago

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... */
nicolasff commented 12 years ago

Thanks, I've zero'd addr and added sin_len on OSX.

andrebraga commented 12 years ago

Thanks! But it might be better to test for BSD, this is not Apple-specific :)