summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2012-03-01 16:18:37 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2012-03-01 16:18:37 (GMT)
commitce1c547246ad8455b3f516a6505919b21e663cf1 (patch)
tree6e51b1c93cf38cabb51f08083149aad2edfd4ac4
parentf1626a862492449d27efa680b95962d9774e399c (diff)
downloadgrv-ce1c547246ad8455b3f516a6505919b21e663cf1.zip
grv-ce1c547246ad8455b3f516a6505919b21e663cf1.tar.gz
grv-ce1c547246ad8455b3f516a6505919b21e663cf1.tar.bz2
grv-ce1c547246ad8455b3f516a6505919b21e663cf1.tar.xz
Fix Windows code; move baseline to WinXP or newer
Fix compiling for Windows; drop support for versions of Windows older than WinXP. This should allow us to use stdio even on Windows at some point, too.
-rw-r--r--Makefile.w322
-rw-r--r--netopen.c48
2 files changed, 14 insertions, 36 deletions
diff --git a/Makefile.w32 b/Makefile.w32
index ff5451e..ae9b810 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -24,7 +24,7 @@ CFLAGS = $(CWARN) $(REQFLAGS) $(OPTFLAGS) $(INCLUDE)
LDFLAGS = -mwindows -L$(SDL)/lib
# We need an extra helping of -lmingw32 before -lSDLmain to make sure
# WinMain is pulled in correctly
-LIBS = -lmingw32 -lSDLmain -lSDL -lwsock32
+LIBS = -lmingw32 -lSDLmain -lSDL -lws2_32
X = .exe
ALL = grv$(X)
diff --git a/netopen.c b/netopen.c
index 9fc0ece..6dabec9 100644
--- a/netopen.c
+++ b/netopen.c
@@ -10,10 +10,12 @@
#ifdef WIN32
+#define _WIN32_WINNT 0x0501 /* Windows XP */
#include <string.h>
#include <io.h>
#include <fcntl.h>
-#include <winsock.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
/* This is basically a lame subset of stdio functionality */
#define NETCON_BUF 32768
@@ -157,37 +159,14 @@ int fputs_network(const char *s, netcon_t nc)
return 0;
}
-netcon_t fopen_network(const char *name, int port, int is_write)
+netcon_t sock_to_netcon(SOCKET sock, int is_write)
{
- struct hostent *he;
- SOCKET sock;
- struct in_addr **haddr;
- struct sockaddr_in si;
struct netcon *nc = malloc(sizeof(struct netcon));
- if ( !nc )
+ if ( !nc ) {
+ closesocket(sock);
return NULL;
-
- /* FIX: We should use getaddrinfo() instead */
- he = gethostbyname(name);
- if ( !he || he->h_addrtype != AF_INET )
- goto bail1;
-
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if ( sock == INVALID_SOCKET )
- goto bail1;
-
- for ( haddr = (struct in_addr **)he->h_addr_list ; *haddr ; haddr++ ) {
- memset(&si, 0, sizeof si);
- si.sin_family = AF_INET;
- si.sin_port = htons(port);
- memcpy(&si.sin_addr, *haddr, sizeof si.sin_addr);
-
- if ( !connect(sock, (struct sockaddr *)&si, sizeof si) )
- break;
}
- if ( !*haddr )
- goto bail2;
/* Construct a struct netcon object */
nc->bufdata = nc->buffer;
@@ -196,12 +175,6 @@ netcon_t fopen_network(const char *name, int port, int is_write)
nc->is_output = is_write;
return nc;
-
- bail2:
- closesocket(sock);
- bail1:
- free(nc);
- return NULL;
}
int fclose_network(netcon_t nc)
@@ -233,6 +206,7 @@ int fclose_network(netcon_t nc)
typedef int SOCKET;
#define INVALID_SOCKET (-1)
#define start_winsock() do { } while (0)
+#define closesocket(x) close(x)
static netcon_t sock_to_netcon(int sock, int is_write)
{
@@ -249,6 +223,10 @@ static netcon_t sock_to_netcon(int sock, int is_write)
#ifdef AF_INET6
+#ifndef AI_ADDRCONFIG
+# define AI_ADDRCONFIG 0
+#endif
+
static SOCKET open_socket(const char *name, int port)
{
SOCKET sock;
@@ -277,7 +255,7 @@ static SOCKET open_socket(const char *name, int port)
if ( !connect(sock, ap->ai_addr, ap->ai_addrlen) )
break;
- close(sock);
+ closesocket(sock);
sock = INVALID_SOCKET;
}
@@ -317,7 +295,7 @@ static SOCKET open_socket(const char *name, int port)
break;
}
if ( !*haddr ) {
- close(sock);
+ closesocket(sock);
return INVALID_SOCKET;
}