Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Enable binding to hostnames as well as IPs #46

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants
Contributor

patricklucas commented Sep 19, 2011

Disclaimer: I am not an expert C programmer by any means - please tell me everything I've done wrong!

I wanted to give a try to letting one connect to a hostname instead of requiring an IP to be passed to listen(). Unfortunately, since you're using -std=c99, getaddrinfo, the preferred way to do that, is unavailable.

In order to use getaddrinfo from netdb.h, I had to add '-D_POSIX_C_SOURCE=200112L' to the build flags. This may be a deal killer for this change, but hopefully we can find a way around it if necessary since I think this is a worthwhile feature.

Notes:

  • Since server_init takes an int port but getaddrinfo wants a string, I truncate the string version of the input port to six digits, which should be sufficient (for ipv4?) since getaddrinfo fails with port > 65535. I realize there may be a better way to handle this.
  • Theoretically, this takes bjoern a step forward to being ipv6 compatible but it still doesn't work with such an address.
Owner

jonashaag commented Sep 19, 2011

The port shouldn't be passed to getaddrinfo in the first place -- from http://tools.ietf.org/html/rfc3493:

The nodename and servname arguments are either null pointers or pointers to null-terminated strings. One or both of these two arguments must be a non-null pointer.

In terms of std99 vs getaddrinfo: I don't like using that cryptic symbol. There must be another way.

Contributor

patricklucas commented Sep 19, 2011

Excellent - I corrected the port issue and now ipv6 works. :)

prl@debian-base:~/dev/oss/bjoern$ python app.py
Listening on fe80::20c:29ff:fe22:2bed:8000...

Owner

jonashaag commented Sep 19, 2011

Wow, great! Now get rid of the C99/getaddrinfo hack and I'll love you :-)

patricklucas added some commits Sep 19, 2011

Enable binding to hostnames as well as IPs
Use getaddrinfo to let user use a hostname ('localhost') instead of
having to specify and IP address.

Also, if given the empty string, default to '0.0.0.0'.
Contributor

temoto commented Oct 23, 2011

-D_POSIX_SOURCE is enough, if you consider that a lesser hack.

Contributor

patricklucas commented Feb 28, 2012

I'm going to revisit this after my other PRs are closed.

@jonashaag jonashaag closed this Apr 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment