Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -64,10 +64,14 @@ test x"$have_asprintf" != x"yes" -a x"$ac_cv_snprintf_useful_ret" != x"yes" && \ AC_MSG_ERROR(No asprintf and no snprintf returning required space!) ACX_PTHREAD + +AC_CHECK_LIB(ws2_32, main, + AC_SUBST(WS2_LIBS, "-lws2_32"), + AC_SUBST(WS2_LIBS, "")) AC_MSG_CHECKING(whether we have IPv6 support) AC_CACHE_VAL(ac_cv_have_ipv6, [ AC_TRY_RUN([ #include Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -1,5 +1,6 @@ ASPRINTF = @ASPRINTF@ PTHREAD_CFLAGS = @PTHREAD_CFLAGS@ PTHREAD_LIBS = @PTHREAD_LIBS@ +WS2_LIBS = @WS2_LIBS@ TESTS = @TESTS@ TEST_LAUNCHER = @TEST_LAUNCHER@ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -26,6 +26,6 @@ CPPFLAGS += -I.. ${PTHREAD_CFLAGS} CFLAGS += ${LIB_CFLAGS} OBJCFLAGS += ${LIB_CFLAGS} LD = ${OBJC} LDFLAGS += ${LIB_LDFLAGS} ${PTHREAD_LIBS} -LIBS += -lobjc +LIBS += -lobjc ${WS2_LIBS} Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -27,35 +27,42 @@ * Headers for Win32 * * These must be imported after objc/Object and thus OFObject! */ #ifdef _WIN32 +#define _WIN32_WINNT 0x0501 #import #import #endif /** * The OFTCPSocket class provides functions to create and use sockets. */ @interface OFTCPSocket: OFObject { +#ifndef _WIN32 int sock; +#else + SOCKET sock; +#endif struct sockaddr *saddr; socklen_t saddr_len; } +/** + * This needs to be called before any socket can be used. + */ ++ (void)startup; + /** * Initializes an already allocated OFTCPSocket. * * \return An initialized OFTCPSocket */ - init; - free; -- setSocket: (int)socket; -- setSocketAddress: (struct sockaddr*)sockaddr - withLength: (socklen_t)len; /** * Connect the OFTCPSocket to the specified destination. * * \param host The host or IP to connect to Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -18,26 +18,39 @@ #import #import "OFTCPSocket.h" #import "OFExceptions.h" -#ifndef _WIN32 /* FIXME */ +#ifndef INVALID_SOCKET +#define INVALID_SOCKET -1 +#endif + @implementation OFTCPSocket ++ (void)startup +{ +#ifdef _WIN32 + WSADATA wsa; + + if (WSAStartup(MAKEWORD(2, 0), &wsa)) + @throw [OFInitializationFailedException newWithClass: self]; +#endif +} + - init { if ((self = [super init])) { - sock = -1; + sock = INVALID_SOCKET; saddr = NULL; saddr_len = 0; } return self; } - free { - if (sock >= 0) + if (sock != INVALID_SOCKET) close(sock); return [super free]; } @@ -64,11 +77,11 @@ char portstr[6]; if (!port) @throw [OFInvalidPortException newWithObject: self]; - if (sock >= 0) + if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithObject: self]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -81,25 +94,25 @@ andNode: host andService: portstr]; for (res = res0; res != NULL; res = res->ai_next) { if ((sock = socket(res->ai_family, res->ai_socktype, - res->ai_protocol)) < 0) + res->ai_protocol)) == INVALID_SOCKET) continue; - if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { + if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) { close(sock); - sock = -1; + sock = INVALID_SOCKET; continue; } break; } freeaddrinfo(res0); - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFConnectionFailedException newWithObject: self andHost: host andPort: port]; return self; @@ -113,14 +126,14 @@ char portstr[6]; if (!port) @throw [OFInvalidPortException newWithObject: self]; - if (sock >= 0) + if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithObject: self]; - if ((sock = socket(family, SOCK_STREAM, 0)) < 0) + if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException newWithObject: self andHost: host andPort: port andFamily: family]; @@ -134,11 +147,11 @@ @throw [OFAddressTranslationFailedException newWithObject: self andNode: host andService: portstr]; - if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { + if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); @throw [OFBindFailedException newWithObject: self andHost: host andPort: port andFamily: family]; @@ -149,26 +162,26 @@ return self; } - listenWithBackLog: (int)backlog { - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; - if (listen(sock, backlog) < 0) + if (listen(sock, backlog) == -1) @throw [OFListenFailedException newWithObject: self andBackLog: backlog]; return self; } - listen { - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; - if (listen(sock, 5) < 0) + if (listen(sock, 5) == -1) @throw [OFListenFailedException newWithObject: self andBackLog: 5]; return self; } @@ -188,11 +201,11 @@ } @catch(id e) { [newsock free]; @throw e; } - if ((s = accept(sock, addr, &addrlen)) < 0) { + if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) { [newsock free]; @throw [OFAcceptFailedException newWithObject: self]; } [newsock setSocket: s]; @@ -205,26 +218,26 @@ - (size_t)readNBytes: (size_t)size intoBuffer: (uint8_t*)buf { ssize_t ret; - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; - if ((ret = recv(sock, buf, size, 0)) < 0) + if ((ret = recv(sock, (char*)buf, size, 0)) < 1) @throw [OFReadFailedException newWithObject: self andSize: size]; - /* This is safe, as we already checked < 0 */ + /* This is safe, as we already checked < 1 */ return ret; } - (uint8_t*)readNBytes: (size_t)size { uint8_t *ret; - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; ret = [self getMemWithSize: size]; @try { @@ -241,67 +254,74 @@ - (size_t)writeNBytes: (size_t)size fromBuffer: (const uint8_t*)buf { ssize_t ret; - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; - if ((ret = send(sock, buf, size, 0)) < 0) + if ((ret = send(sock, (char*)buf, size, 0)) == -1) @throw [OFWriteFailedException newWithObject: self andSize: size]; - /* This is safe, as we already checked < 0 */ + /* This is safe, as we already checked for -1 */ return ret; } - (size_t)writeCString: (const char*)str { - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; return [self writeNBytes: strlen(str) fromBuffer: (const uint8_t*)str]; } - setBlocking: (BOOL)enable { +#ifndef _WIN32 int flags; - if ((flags = fcntl(sock, F_GETFL)) < 0) + if ((flags = fcntl(sock, F_GETFL)) == -1) @throw [OFSetOptionFailedException newWithObject: self]; if (enable) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; - if (fcntl(sock, F_SETFL, flags) < 0) + if (fcntl(sock, F_SETFL, flags) == -1) + @throw [OFSetOptionFailedException newWithObject: self]; +#else + u_long v = enable; + + if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException newWithObject: self]; +#endif return self; } - enableKeepAlives: (BOOL)enable { - if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable, - sizeof(enable)) != 0) + int v = enable; + + if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v))) @throw [OFSetOptionFailedException newWithObject: self]; return self; } - close { - if (sock < 0) + if (sock == INVALID_SOCKET) @throw [OFNotConnectedException newWithObject: self]; - sock = -1; + sock = INVALID_SOCKET; if (saddr != NULL) [self freeMem: saddr]; saddr_len = 0; return self; } @end -#endif Index: tests/OFTCPSocket/Makefile ================================================================== --- tests/OFTCPSocket/Makefile +++ tests/OFTCPSocket/Makefile @@ -3,11 +3,11 @@ include ../../buildsys.mk include ../../extra.mk CPPFLAGS += -I../../src -I../.. -LIBS += -L../../src -lobjfw -lobjc +LIBS += -L../../src -lobjfw -lobjc ${WS2_LIBS} .PHONY: run all: run run: ${PROG_NOINST} Index: tests/OFTCPSocket/OFTCPSocket.m ================================================================== --- tests/OFTCPSocket/OFTCPSocket.m +++ tests/OFTCPSocket/OFTCPSocket.m @@ -16,33 +16,32 @@ #import #import "OFTCPSocket.h" #import "OFExceptions.h" -#ifndef _WIN32 /* FIXME */ inline uint16_t get_port() { - uint16_t port = (uint16_t)random(); + uint16_t port = (uint16_t)rand(); if (port < 1024) port += 1024; printf("Using port %d...\n", port); return port; } -#endif int main() { -#ifndef _WIN32 /* FIXME */ uint16_t port; - srandom(time(NULL)); + srand(time(NULL)); @try { + [OFTCPSocket startup]; + OFTCPSocket *server = [OFTCPSocket new]; OFTCPSocket *client = [OFTCPSocket new]; OFTCPSocket *accepted; char buf[7]; @@ -109,9 +108,8 @@ [server free]; } @catch(OFException *e) { printf("EXCEPTION: %s\n", [e cString]); return 1; } -#endif return 0; }