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

Closed
andrebraga opened this Issue Jul 19, 2012 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

andrebraga commented Jul 19, 2012

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 added a commit that referenced this issue Jul 19, 2012

Fix server.c for OSX
Memset the whole of addr to zero and add sin_len on OSX.
GitHub issue #51.
Owner

nicolasff commented Jul 19, 2012

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

@nicolasff nicolasff closed this Jul 19, 2012

Contributor

andrebraga commented Jul 20, 2012

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

nicolasff added a commit that referenced this issue Jul 20, 2012

Merge pull request #52 from andrebraga/master
Re Issue #51, as mentioned, this fix is appropriate for all BSDs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment