Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Getaddrinfo #64

Closed
wants to merge 2 commits into from

2 participants

@patricklucas

I rewrote my patch from the old PR.

Note the change from -std=c99 to -std=gnu99. Using 'c99' causes _POSIX_SOURCE not to be set - that was the cause of the problem before of getaddrinfo not being available without setting that manually.

I'm not sure of any reason to use -std=c99 instead of -std=gnu99; is there anything I'm missing?

This patch lets you bind to hostnames, *, and IPv6 addresses.

@jonashaag
Owner

Superseded by the patch for #73

@jonashaag jonashaag closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 29, 2012
  1. @patricklucas
Commits on Mar 1, 2012
  1. @patricklucas
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 14 deletions.
  1. +1 −1  Makefile
  2. +27 −12 bjoern/server.c
  3. +1 −1  setup.py
View
2  Makefile
@@ -13,7 +13,7 @@ objects = $(HTTP_PARSER_OBJ) \
$(wildcard $(SOURCE_DIR)/*.c))
CPPFLAGS += $(PYTHON_INCLUDE) -I . -I $(SOURCE_DIR) -I $(HTTP_PARSER_DIR)
-CFLAGS += $(FEATURES) -std=c99 -fno-strict-aliasing -fcommon -fPIC -Wall \
+CFLAGS += $(FEATURES) -std=gnu99 -fno-strict-aliasing -fcommon -fPIC -Wall \
-Wextra -Wno-unused-parameter -Wno-missing-field-initializers -g -O0
LDFLAGS += $(PYTHON_LDFLAGS) -l ev -shared -fcommon
View
39 bjoern/server.c
@@ -1,5 +1,6 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
@@ -99,22 +100,36 @@ bool server_init(const char* hostaddr, const int port)
goto err;
}
else {
- /* IP bind */
- if((sockinfo.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- return false;
+ struct addrinfo hints;
+ struct addrinfo *p, *servinfo;
+ char strport[6];
- struct sockaddr_in sockaddr;
- memset(&sockaddr, 0, sizeof(sockaddr));
- sockaddr.sin_family = PF_INET;
- inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
- sockaddr.sin_port = htons(port);
+ // Stringify the port
+ snprintf(strport, 6, "%d", port);
- /* Set SO_REUSEADDR t make the IP address available for reuse */
- int optval = true;
- setsockopt(sockinfo.fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
+ // Set up the hints struct
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC; // Allow IPv4 and IPv6
+ hints.ai_socktype = SOCK_STREAM;
- if(bind(sockinfo.fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
+ if (getaddrinfo(hostaddr, strport, &hints, &servinfo) != 0)
goto err;
+
+ // Iterate through the results, trying to bind
+ for (p = servinfo; p != NULL; p = p->ai_next) {
+ if ((sockinfo.fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
+ continue;
+
+ if (bind(sockinfo.fd, p->ai_addr, p->ai_addrlen) < 0)
+ continue;
+
+ break; // success
+ }
+
+ if (p == NULL) // Bind failed
+ goto err;
+
+ freeaddrinfo(servinfo);
}
if(listen(sockinfo.fd, LISTEN_BACKLOG) < 0)
View
2  setup.py
@@ -12,7 +12,7 @@
include_dirs = ['http-parser'],
define_macros = [('WANT_SENDFILE', '1'),
('WANT_SIGINT_HANDLING', '1')],
- extra_compile_args = ['-std=c99', '-fno-strict-aliasing', '-fcommon',
+ extra_compile_args = ['-std=gnu99', '-fno-strict-aliasing', '-fcommon',
'-fPIC', '-Wall', '-Wextra', '-Wno-unused-parameter',
'-Wno-missing-field-initializers', '-g']
)
Something went wrong with that request. Please try again.