Permalink
Browse files

Consolidated windows IPv6 support, compiled and tested gem

  • Loading branch information...
1 parent ec4a004 commit ba78ffa41d27edb1b3f9a33c710e43295e9aa56f @stakach committed Mar 6, 2012
Showing with 35 additions and 7 deletions.
  1. +30 −7 ext/em.cpp
  2. +5 −0 ext/project.h
View
@@ -133,6 +133,10 @@ EventMachine_t::~EventMachine_t()
close (epfd);
if (kqfd != -1)
close (kqfd);
+
+ #ifdef OS_WIN32
+ WSACleanup(); // Clean up the windows network library
+ #endif
}
@@ -1481,25 +1485,42 @@ struct sockaddr *name2address (const char *server, int port, int *family, int *b
struct addrinfo hints;
memset (&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET6;
+ hints.ai_family = AF_UNSPEC;
if(getaddrinfo((char*)server, NULL, &hints, &result) == 0) {
for(ptr=result; ptr != NULL; ptr=ptr->ai_next) {
- if(ptr->ai_family == AF_INET6) {
+ switch(ptr->ai_family) {
+
+ case AF_INET6:
+ memcpy((void*) &in6, (const void*)(ptr->ai_addr), sizeof(in6)); // Copy result
+ freeaddrinfo(result); // Prevent memory leaks
+
if (family)
*family = AF_INET6;
if (bind_size)
*bind_size = sizeof(in6);
-
- memcpy((void*) &in6, (const void*)(ptr->ai_addr), sizeof(in6)); // Copy result
- freeaddrinfo(result); // Prevent memory leaks
+
+ in6.sin6_family = AF_INET6;
+ in6.sin6_port = htons (port);
return (struct sockaddr*)&in6;
+ case AF_INET:
+ memcpy((void*) &in4, (const void*)(ptr->ai_addr), sizeof(in4)); // Copy result
+ freeaddrinfo(result); // Prevent memory leaks
+
+ if (family)
+ *family = AF_INET;
+ if (bind_size)
+ *bind_size = sizeof(in4);
+
+ in4.sin_family = AF_INET;
+ in4.sin_port = htons (port);
+ return (struct sockaddr*)&in4;
}
}
}
- #endif
+ #else
- hp = gethostbyname ((char*)server); // Windows requires the cast.
+ hp = gethostbyname ((char*)server); // Windows requires the cast, although no longer executed on windows
if (hp) {
in4.sin_addr.s_addr = ((in_addr*)(hp->h_addr))->s_addr;
if (family)
@@ -1511,6 +1532,8 @@ struct sockaddr *name2address (const char *server, int port, int *family, int *b
return (struct sockaddr*)&in4;
}
+ #endif
+
return NULL;
}
View
@@ -85,6 +85,8 @@ typedef int SOCKET;
#define FD_SETSIZE 2048
#define WIN32_LEAN_AND_MEAN
+// Add support for IPv6
+#define _WIN32_WINNT 0x501
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
@@ -93,6 +95,9 @@ typedef int SOCKET;
#include <assert.h>
#include <stdio.h>
+// link with Ws2_32.lib
+#pragma comment (lib, "Ws2_32.lib")
+
typedef int socklen_t;
typedef int pid_t;
#endif

0 comments on commit ba78ffa

Please sign in to comment.