Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -294,10 +294,14 @@ 4B6C8ADE17BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD717BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h */; }; 4B7161AD17A6FC7600B74970 /* OFHTTPResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B7161AE17A6FC7600B74970 /* OFHTTPResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */; }; 4B745BA5168B25E600A6C20E /* OFSystemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B745BA6168B25E600A6C20E /* OFSystemInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */; }; + 4B7DD58218942FE200990FD6 /* socket_helpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD58118942FE200990FD6 /* socket_helpers.h */; }; + 4B7DD5851894358500990FD6 /* OFMoveItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD5831894358400990FD6 /* OFMoveItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4B7DD5861894358500990FD6 /* OFMoveItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7DD5841894358400990FD6 /* OFMoveItemFailedException.m */; }; + 4B7DD58818943D4A00990FD6 /* socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD58718943D4A00990FD6 /* socket.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B7FF3B4133CED6200000324 /* OFConditionStillWaitingException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B7FF3B5133CED6200000324 /* OFConditionStillWaitingException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */; }; 4B837D7916829C5F007A3E83 /* block.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7716829C5F007A3E83 /* block.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B837D7A16829C5F007A3E83 /* instance.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7816829C5F007A3E83 /* instance.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B83F0F4142FDEFD00E4A821 /* OFStreamObserver_kqueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B83F0F2142FDEFD00E4A821 /* OFStreamObserver_kqueue.h */; }; @@ -409,12 +413,10 @@ 4BF33B12133807A20059CEF7 /* TestsAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6EF6811235358D0076B512 /* TestsAppDelegate.m */; }; 4BF33B4713380CE20059CEF7 /* testfile.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4BF33B4313380CD40059CEF7 /* testfile.txt */; }; 4BF33B4813380D2D0059CEF7 /* testfile.bin in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4BF33B4213380CD40059CEF7 /* testfile.bin */; }; 4BFF3714177E17C100192782 /* OFRemoveItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BFF3715177E17C100192782 /* OFRemoveItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */; }; - 4BFF3716177E17C100192782 /* OFRenameItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFF3712177E17C100192782 /* OFRenameItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4BFF3717177E17C100192782 /* OFRenameItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BFF3713177E17C100192782 /* OFRenameItemFailedException.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ 4B23CA8E133811B20047A1D9 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; @@ -730,10 +732,14 @@ 4B6EF685123535C80076B512 /* test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = test.m; path = tests/objc_sync/test.m; sourceTree = SOURCE_ROOT; }; 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFHTTPResponse.h; path = src/OFHTTPResponse.h; sourceTree = ""; }; 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFHTTPResponse.m; path = src/OFHTTPResponse.m; sourceTree = ""; }; 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSystemInfo.h; path = src/OFSystemInfo.h; sourceTree = ""; }; 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSystemInfo.m; path = src/OFSystemInfo.m; sourceTree = ""; }; + 4B7DD58118942FE200990FD6 /* socket_helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = socket_helpers.h; path = src/socket_helpers.h; sourceTree = ""; }; + 4B7DD5831894358400990FD6 /* OFMoveItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMoveItemFailedException.h; path = src/exceptions/OFMoveItemFailedException.h; sourceTree = ""; }; + 4B7DD5841894358400990FD6 /* OFMoveItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFMoveItemFailedException.m; path = src/exceptions/OFMoveItemFailedException.m; sourceTree = ""; }; + 4B7DD58718943D4A00990FD6 /* socket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = socket.h; path = src/socket.h; sourceTree = ""; }; 4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFConditionStillWaitingException.h; path = src/exceptions/OFConditionStillWaitingException.h; sourceTree = ""; }; 4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFConditionStillWaitingException.m; path = src/exceptions/OFConditionStillWaitingException.m; sourceTree = ""; }; 4B837D7716829C5F007A3E83 /* block.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = block.h; path = src/block.h; sourceTree = ""; }; 4B837D7816829C5F007A3E83 /* instance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = instance.h; path = src/instance.h; sourceTree = ""; }; 4B83F0F2142FDEFD00E4A821 /* OFStreamObserver_kqueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFStreamObserver_kqueue.h; path = src/OFStreamObserver_kqueue.h; sourceTree = ""; }; @@ -870,12 +876,10 @@ 4BF33B4213380CD40059CEF7 /* testfile.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = testfile.bin; path = tests/testfile.bin; sourceTree = ""; }; 4BF33B4313380CD40059CEF7 /* testfile.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = testfile.txt; path = tests/testfile.txt; sourceTree = ""; }; 4BFBDD1610A0724800051AFB /* unicode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = unicode.m; path = src/unicode.m; sourceTree = ""; }; 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFRemoveItemFailedException.h; path = src/exceptions/OFRemoveItemFailedException.h; sourceTree = ""; }; 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFRemoveItemFailedException.m; path = src/exceptions/OFRemoveItemFailedException.m; sourceTree = ""; }; - 4BFF3712177E17C100192782 /* OFRenameItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFRenameItemFailedException.h; path = src/exceptions/OFRenameItemFailedException.h; sourceTree = ""; }; - 4BFF3713177E17C100192782 /* OFRenameItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFRenameItemFailedException.m; path = src/exceptions/OFRenameItemFailedException.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 4B187E0A163EA29F0049A832 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; @@ -989,10 +993,12 @@ 4B6743EC163C384A00EB1E59 /* OFLockFailedException.m */, 4B17FF9F133A3331003E6DCD /* OFMalformedXMLException.h */, 4B17FFA0133A3332003E6DCD /* OFMalformedXMLException.m */, 4B17FF8D133A3034003E6DCD /* OFMemoryNotPartOfObjectException.h */, 4B17FF8E133A3034003E6DCD /* OFMemoryNotPartOfObjectException.m */, + 4B7DD5831894358400990FD6 /* OFMoveItemFailedException.h */, + 4B7DD5841894358400990FD6 /* OFMoveItemFailedException.m */, 4B90B789133AD46700BD33CB /* OFNotConnectedException.h */, 4B90B78A133AD46700BD33CB /* OFNotConnectedException.m */, 4B17FF75133A2B18003E6DCD /* OFNotImplementedException.h */, 4B17FF76133A2B18003E6DCD /* OFNotImplementedException.m */, 4B55A107133AC05100B58A93 /* OFOpenFileFailedException.h */, @@ -1005,12 +1011,10 @@ 4B55A10D133AC24500B58A93 /* OFReadFailedException.m */, 4B55A10E133AC24500B58A93 /* OFReadOrWriteFailedException.h */, 4B55A10F133AC24500B58A93 /* OFReadOrWriteFailedException.m */, 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */, 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */, - 4BFF3712177E17C100192782 /* OFRenameItemFailedException.h */, - 4BFF3713177E17C100192782 /* OFRenameItemFailedException.m */, 4B29BC39133AC4E80004B236 /* OFSeekFailedException.h */, 4B29BC3A133AC4E80004B236 /* OFSeekFailedException.m */, 4B90B78B133AD46700BD33CB /* OFSetOptionFailedException.h */, 4B90B78C133AD46700BD33CB /* OFSetOptionFailedException.m */, 4B6743ED163C384A00EB1E59 /* OFStillLockedException.h */, @@ -1344,10 +1348,12 @@ 4BF1BCBF11C9663F0025511F /* objfw-defs.h.in */, 4BB50DCF12F863C700C9393F /* of_asprintf.h */, 4BB50DD012F863C700C9393F /* of_asprintf.m */, 4BA355BC14879BF700442EF4 /* of_strptime.h */, 4BA355B914879BDD00442EF4 /* of_strptime.m */, + 4B7DD58718943D4A00990FD6 /* socket.h */, + 4B7DD58118942FE200990FD6 /* socket_helpers.h */, 4B67998B1099E7C50041064A /* threading.h */, 4B67998C1099E7C50041064A /* unicode.h */, 4BFBDD1610A0724800051AFB /* unicode.m */, 4B6AF97210A8D42E0003FB0A /* windows_1252.m */, ); @@ -1551,10 +1557,11 @@ 4B837D7A16829C5F007A3E83 /* instance.h in Headers */, 4B3D23E81337FCB000DD29B8 /* macros.h in Headers */, 4BD98C03133814220048DD5B /* objfw-defs.h in Headers */, 4B3D23E91337FCB000DD29B8 /* of_asprintf.h in Headers */, 4BA355BD14879BF700442EF4 /* of_strptime.h in Headers */, + 4B7DD58818943D4A00990FD6 /* socket.h in Headers */, 4B3D23EA1337FCB000DD29B8 /* threading.h in Headers */, 4B3D23EB1337FCB000DD29B8 /* unicode.h in Headers */, 4B90B79E133AD87D00BD33CB /* OFAcceptFailedException.h in Headers */, 4B90B7A0133AD87D00BD33CB /* OFAddressTranslationFailedException.h in Headers */, 4B17FF7F133A2D17003E6DCD /* OFAllocFailedException.h in Headers */, @@ -1584,19 +1591,19 @@ 4B29BC61133AC9CB0004B236 /* OFLinkFailedException.h in Headers */, 4B90B7A6133AD87D00BD33CB /* OFListenFailedException.h in Headers */, 4B6743F1163C384A00EB1E59 /* OFLockFailedException.h in Headers */, 4B17FFA1133A3335003E6DCD /* OFMalformedXMLException.h in Headers */, 4B17FF8F133A3036003E6DCD /* OFMemoryNotPartOfObjectException.h in Headers */, + 4B7DD5851894358500990FD6 /* OFMoveItemFailedException.h in Headers */, 4B90B78F133AD46700BD33CB /* OFNotConnectedException.h in Headers */, 4B17FF77133A2B18003E6DCD /* OFNotImplementedException.h in Headers */, 4B55A10A133AC05100B58A93 /* OFOpenFileFailedException.h in Headers */, 4B17FF83133A2DFB003E6DCD /* OFOutOfMemoryException.h in Headers */, 4B17FF7B133A2C15003E6DCD /* OFOutOfRangeException.h in Headers */, 4B55A112133AC24600B58A93 /* OFReadFailedException.h in Headers */, 4B55A114133AC24600B58A93 /* OFReadOrWriteFailedException.h in Headers */, 4BFF3714177E17C100192782 /* OFRemoveItemFailedException.h in Headers */, - 4BFF3716177E17C100192782 /* OFRenameItemFailedException.h in Headers */, 4B29BC41133AC4E80004B236 /* OFSeekFailedException.h in Headers */, 4B90B791133AD46700BD33CB /* OFSetOptionFailedException.h in Headers */, 4B6743F3163C384A00EB1E59 /* OFStillLockedException.h in Headers */, 4B17FFA9133A34E7003E6DCD /* OFTruncatedDataException.h in Headers */, 4B55A0FF133ABEA900B58A93 /* OFThreadJoinFailedException.h in Headers */, @@ -1631,10 +1638,11 @@ 4BD653C5143B8489006182F0 /* OFTCPSocket+SOCKS5.h in Headers */, 4B6C8ADC17BD5C2E00B194F2 /* OFThread+Private.h in Headers */, 4B6C8ADD17BD5C2E00B194F2 /* OFTimer+Private.h in Headers */, 4B6C8ADE17BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h in Headers */, 4BDF37B51338055600F9A81A /* config.h in Headers */, + 4B7DD58218942FE200990FD6 /* socket_helpers.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXHeadersBuildPhase section */ @@ -1951,19 +1959,19 @@ 4B29BC62133AC9CB0004B236 /* OFLinkFailedException.m in Sources */, 4B90B7A7133AD87D00BD33CB /* OFListenFailedException.m in Sources */, 4B6743F2163C384A00EB1E59 /* OFLockFailedException.m in Sources */, 4B17FFA2133A3335003E6DCD /* OFMalformedXMLException.m in Sources */, 4B17FF90133A3036003E6DCD /* OFMemoryNotPartOfObjectException.m in Sources */, + 4B7DD5861894358500990FD6 /* OFMoveItemFailedException.m in Sources */, 4B90B790133AD46700BD33CB /* OFNotConnectedException.m in Sources */, 4B17FF78133A2B18003E6DCD /* OFNotImplementedException.m in Sources */, 4B55A10B133AC05100B58A93 /* OFOpenFileFailedException.m in Sources */, 4B17FF84133A2DFB003E6DCD /* OFOutOfMemoryException.m in Sources */, 4B17FF7C133A2C15003E6DCD /* OFOutOfRangeException.m in Sources */, 4B55A113133AC24600B58A93 /* OFReadFailedException.m in Sources */, 4B55A115133AC24600B58A93 /* OFReadOrWriteFailedException.m in Sources */, 4BFF3715177E17C100192782 /* OFRemoveItemFailedException.m in Sources */, - 4BFF3717177E17C100192782 /* OFRenameItemFailedException.m in Sources */, 4B29BC42133AC4E80004B236 /* OFSeekFailedException.m in Sources */, 4B90B792133AD46700BD33CB /* OFSetOptionFailedException.m in Sources */, 4B6743F4163C384A00EB1E59 /* OFStillLockedException.m in Sources */, 4B55A100133ABEA900B58A93 /* OFThreadJoinFailedException.m in Sources */, 4B55A102133ABEA900B58A93 /* OFThreadStartFailedException.m in Sources */, Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -618,10 +618,11 @@ AC_ARG_ENABLE(sockets, AS_HELP_STRING([--disable-sockets], [disable socket support])) AS_IF([test x"$enable_sockets" != x"no"], [ AC_DEFINE(OF_HAVE_SOCKETS, 1, [Whether we have sockets]) AC_SUBST(USE_SRCS_SOCKETS, '${SRCS_SOCKETS}') + AC_SUBST(USE_INCLUDES_SOCKETS, '${INCLUDES_SOCKET}') AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket") AC_CHECK_LIB(network, socket, LIBS="$LIBS -lnetwork") AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32") Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -89,10 +89,11 @@ SRCS_THREADS = OFCondition.m \ OFMutex.m \ OFRecursiveMutex.m \ OFThreadPool.m +INCLUDES_SOCKETS = socket.h INCLUDES_THREADS = threading.h INCLUDES := ${SRCS:.m=.h} \ OFCollection.h \ OFHash.h \ OFJSONRepresentation.h \ @@ -106,10 +107,11 @@ ${ATOMIC_H} \ block.h \ instance.h \ macros.h \ objfw-defs.h \ + ${USE_INCLUDES_SOCKETS} \ ${USE_INCLUDES_THREADS} SRCS += OFArray_adjacent.m \ OFArray_adjacentSubarray.m \ OFCountedSet_hashtable.m \ Index: src/OFHTTPResponse.h ================================================================== --- src/OFHTTPResponse.h +++ src/OFHTTPResponse.h @@ -15,14 +15,10 @@ */ #import "OFStream.h" #import "OFHTTPRequest.h" -#ifndef OF_HAVE_SOCKETS -# error No sockets available! -#endif - @class OFDictionary; /*! * @brief A class for representing an HTTP request reply as a stream. */ Index: src/OFStreamObserver.h ================================================================== --- src/OFStreamObserver.h +++ src/OFStreamObserver.h @@ -14,27 +14,11 @@ * file. */ #import "OFObject.h" -#ifndef OF_HAVE_SOCKETS -# error No sockets available! -#endif - -#ifdef OF_HAVE_SYS_SOCKET_H -# include -#endif - -#ifdef _WIN32 -# include -#endif - -#ifdef __wii__ -# define BOOL OGC_BOOL -# include -# undef BOOL -#endif +#import "socket.h" @class OFStream; @class OFMutableArray; @class OFMutableDictionary; @class OFDataArray; Index: src/OFStreamObserver.m ================================================================== --- src/OFStreamObserver.m +++ src/OFStreamObserver.m @@ -45,29 +45,16 @@ #endif #if defined(HAVE_SYS_SELECT_H) || defined(_WIN32) # import "OFStreamObserver_select.h" #endif -#ifdef _WIN32 -# include -#endif - #import "OFInitializationFailedException.h" #import "OFOutOfRangeException.h" #import "autorelease.h" #import "macros.h" - -#ifdef __wii__ -# define BOOL OGC_BOOL -# include -# undef BOOL -# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen) -# define sendto(sock, buf, len, flags, addr, addrlen) \ - net_sendto(sock, buf, len, flags, addr, addrlen) -# define socket(domain, type, proto) net_socket(domain, type, proto) -#endif +#import "socket_helpers.h" enum { QUEUE_ADD = 0, QUEUE_REMOVE = 1, QUEUE_READ = 0, Index: src/OFStreamObserver_poll.m ================================================================== --- src/OFStreamObserver_poll.m +++ src/OFStreamObserver_poll.m @@ -31,19 +31,15 @@ #import "OFOutOfRangeException.h" #import "autorelease.h" #import "macros.h" +#import "socket_helpers.h" #ifdef __wii__ -# define BOOL OGC_BOOL -# include -# undef BOOL -# define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout) # define pollfd pollsd # define fd socket -typedef u32 nfds_t; #endif @implementation OFStreamObserver_poll - init { Index: src/OFStreamSocket.h ================================================================== --- src/OFStreamSocket.h +++ src/OFStreamSocket.h @@ -14,17 +14,11 @@ * file. */ #import "OFStream.h" -#ifndef OF_HAVE_SOCKETS -# error No sockets available! -#endif - -#ifdef _WIN32 -# include -#endif +#import "socket.h" /*! * @brief A class which provides functions to create and use stream sockets. */ @interface OFStreamSocket: OFStream Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -22,38 +22,19 @@ #include #include -#include -#ifdef OF_HAVE_SYS_SOCKET_H -# include -#endif - #import "OFStreamSocket.h" #import "OFInitializationFailedException.h" #import "OFNotConnectedException.h" #import "OFReadFailedException.h" #import "OFSetOptionFailedException.h" #import "OFWriteFailedException.h" -#ifdef __wii__ -# define BOOL OGC_BOOL -# include -# undef BOOL -# define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags) -# define send(sock, buf, len, flags) net_send(sock, buf, len, flags) -#endif - -#ifndef INVALID_SOCKET -# define INVALID_SOCKET -1 -#endif - -#ifdef _WIN32 -# define close(sock) closesocket(sock) -#endif +#import "socket_helpers.h" @implementation OFStreamSocket + (void)initialize { #ifdef _WIN32 Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -12,34 +12,13 @@ * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ -#ifndef __STDC_LIMIT_MACROS -# define __STDC_LIMIT_MACROS -#endif -#ifndef __STDC_CONSTANT_MACROS -# define __STDC_CONSTANT_MACROS -#endif - -#import "objfw-defs.h" - -#ifdef OF_HAVE_SYS_SOCKET_H -# include -#endif - #import "OFStreamSocket.h" -#ifdef _WIN32 -# include -#endif - -#ifdef __wii__ -# define BOOL OGC_BOOL -# include -# undef BOOL -#endif +#import "socket.h" /*! @file */ @class OFTCPSocket; @class OFString; @@ -76,11 +55,11 @@ * To create a server, create a socket, bind it and listen on it. */ @interface OFTCPSocket: OFStreamSocket { bool _listening; - struct sockaddr_storage *_sockAddr; + struct sockaddr_storage _sockAddr; socklen_t _sockAddrLen; OFString *_SOCKS5Host; uint16_t _SOCKS5Port; } Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -23,20 +23,10 @@ #include #include #include -#ifdef HAVE_NETINET_IN_H -# include -#endif -#ifdef HAVE_ARPA_INET_H -# include -#endif -#ifdef HAVE_NETDB_H -# include -#endif - #import "OFTCPSocket.h" #import "OFTCPSocket+SOCKS5.h" #import "OFString.h" #import "OFThread.h" #import "OFTimer.h" @@ -54,68 +44,17 @@ #import "OFNotImplementedException.h" #import "OFSetOptionFailedException.h" #import "autorelease.h" #import "macros.h" - -#ifndef INVALID_SOCKET -# define INVALID_SOCKET -1 -#endif - -#ifdef HAVE_THREADSAFE_GETADDRINFO -# ifndef AI_NUMERICSERV -# define AI_NUMERICSERV 0 -# endif -# ifndef AI_NUMERICHOST -# define AI_NUMERICHOST 0 -# endif -#endif - -#ifndef SOMAXCONN -# define SOMAXCONN 32 -#endif +#import "socket_helpers.h" #if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) # import "OFMutex.h" # import "OFDataArray.h" static OFMutex *mutex = nil; -#endif - -#ifdef _WIN32 -# define close(sock) closesocket(sock) -#endif - -#ifdef _PSP -/* PSP defines AF_INET6, even though sockaddr_in6 is missing */ -# undef AF_INET6 -struct sockaddr_storage { - uint8_t ss_len; - sa_family_t ss_family; - in_port_t ss_data1; - struct in_addr ss_data2; - int8_t ss_data3[8]; -}; -#endif - -#ifdef __wii__ -# define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen) -# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen) -# define close(sock) net_close(sock) -# define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen) -# define gethostbyname(name) net_gethostbyname(name) -# define listen(sock, backlog) net_listen(sock, backlog) -# define setsockopt(sock, level, name, value, len) \ - net_setsockopt(sock, level, name, value, len) -# define socket(domain, type, proto) net_socket(domain, type, proto) -typedef u32 in_addr_t; - -struct sockaddr_storage { - u8 ss_len; - u8 ss_family; - u8 ss_data[14]; -}; #endif /* References for static linking */ void _references_to_categories_of_OFTCPSocket(void) { @@ -300,11 +239,11 @@ { self = [super init]; @try { _socket = INVALID_SOCKET; - _sockAddr = NULL; + _sockAddrLen = sizeof(struct sockaddr_storage); _SOCKS5Host = [defaultSOCKS5Host copy]; _SOCKS5Port = defaultSOCKS5Port; } @catch (id e) { [self release]; @throw e; @@ -676,10 +615,15 @@ _socket = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithHost: host port: port socket: self]; } + +- (void)listen +{ + [self listenWithBackLog: SOMAXCONN]; +} - (void)listenWithBackLog: (int)backLog { if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; @@ -689,33 +633,18 @@ backLog: backLog]; _listening = true; } -- (void)listen -{ - [self listenWithBackLog: SOMAXCONN]; -} - - (instancetype)accept { - OFTCPSocket *client; - struct sockaddr_storage *addr; - socklen_t addrLen; - int socket; - - client = [[[[self class] alloc] init] autorelease]; - addrLen = sizeof(*addr); - addr = [client allocMemoryWithSize: addrLen]; - - if ((socket = accept(_socket, (struct sockaddr*)addr, - &addrLen)) == INVALID_SOCKET) - @throw [OFAcceptFailedException exceptionWithSocket: self]; - - client->_socket = socket; - client->_sockAddr = addr; - client->_sockAddrLen = addrLen; + OFTCPSocket *client = [[[[self class] alloc] init] autorelease]; + + if ((client->_socket = accept(_socket, + (struct sockaddr*)&client->_sockAddr, + &client->_sockAddrLen)) == INVALID_SOCKET) + @throw [OFAcceptFailedException exceptionWithSocket: self]; return client; } - (void)asyncAcceptWithTarget: (id)target @@ -744,19 +673,19 @@ - (OFString*)remoteAddress { char *host; - if (_sockAddr == NULL || _sockAddrLen == 0) + if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; #ifdef HAVE_THREADSAFE_GETADDRINFO host = [self allocMemoryWithSize: NI_MAXHOST]; @try { - if (getnameinfo((struct sockaddr*)_sockAddr, _sockAddrLen, host, - NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) + if (getnameinfo((struct sockaddr*)&_sockAddr, _sockAddrLen, + host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; return [OFString stringWithUTF8String: host]; } @finally { @@ -766,11 +695,11 @@ # ifdef OF_HAVE_THREADS [mutex lock]; @try { # endif - host = inet_ntoa(((struct sockaddr_in*)_sockAddr)->sin_addr); + host = inet_ntoa(((struct sockaddr_in*)&_sockAddr)->sin_addr); if (host == NULL) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; @@ -794,10 +723,8 @@ - (void)close { [super close]; _listening = false; - [self freeMemory: _sockAddr]; - _sockAddr = NULL; - _sockAddrLen = 0; + _sockAddrLen = sizeof(struct sockaddr_storage); } @end ADDED src/socket.h Index: src/socket.h ================================================================== --- src/socket.h +++ src/socket.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "objfw-defs.h" + +#ifndef OF_HAVE_SOCKETS +# error No sockets available! +#endif + +#ifdef OF_HAVE_SYS_SOCKET_H +# include +#endif + +#ifdef _WIN32 +# include +#endif + +#ifdef __wii__ +# define BOOL OGC_BOOL +# include +# undef BOOL + +struct sockaddr_storage { + u8 ss_len; + u8 ss_family; + u8 ss_data[14]; +}; +#endif + +#ifdef _PSP +# include + +struct sockaddr_storage { + uint8_t ss_len; + sa_family_t ss_family; + in_port_t ss_data1; + struct in_addr ss_data2; + int8_t ss_data3[8]; +}; +#endif ADDED src/socket_helpers.h Index: src/socket_helpers.h ================================================================== --- src/socket_helpers.h +++ src/socket_helpers.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif +#ifdef HAVE_NETDB_H +# include +#endif + +#ifndef INVALID_SOCKET +# define INVALID_SOCKET -1 +#endif + +#ifdef HAVE_THREADSAFE_GETADDRINFO +# ifndef AI_NUMERICSERV +# define AI_NUMERICSERV 0 +# endif +# ifndef AI_NUMERICHOST +# define AI_NUMERICHOST 0 +# endif +#endif + +#ifndef SOMAXCONN +# define SOMAXCONN 32 +#endif + +#ifdef _WIN32 +# define close(sock) closesocket(sock) +#endif + +#ifdef _PSP +/* PSP defines AF_INET6, even though sockaddr_in6 is missing */ +# undef AF_INET6 +#endif + +#ifdef __wii__ +# define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen) +# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen) +# define close(sock) net_close(sock) +# define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen) +# define gethostbyname(name) net_gethostbyname(name) +# define listen(sock, backlog) net_listen(sock, backlog) +# define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout) +# define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags) +# define recvfrom(sock, buf, len, flags, addr, addrlen) \ + net_recvfrom(sock, buf, len, flags, addr, addrlen) +# define send(sock, buf, len, flags) net_send(sock, buf, len, flags) +# define sendto(sock, buf, len, flags, addr, addrlen) \ + net_sendto(sock, buf, len, flags, addr, addrlen) +# define setsockopt(sock, level, name, value, len) \ + net_setsockopt(sock, level, name, value, len) +# define socket(domain, type, proto) net_socket(domain, type, proto) +typedef u32 in_addr_t; +typedef u32 nfds_t; +#endif