@@ -13,10 +13,15 @@ * file. */ #include "config.h" +#ifndef _XOPEN_SOURCE_EXTENDED +# define _XOPEN_SOURCE_EXTENDED +#endif +#define _HPUX_ALT_XOPEN_SOCKET_API + #include #include #ifdef HAVE_FCNTL_H # include @@ -25,10 +30,12 @@ #import "OFSequencedPacketSocket.h" #import "OFSequencedPacketSocket+Private.h" #import "OFData.h" #import "OFRunLoop+Private.h" #import "OFRunLoop.h" +#import "OFSocket.h" +#import "OFSocket+Private.h" #import "OFAcceptFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFListenFailedException.h" @@ -36,22 +43,19 @@ #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFSetOptionFailedException.h" #import "OFWriteFailedException.h" -#import "socket.h" -#import "socket_helpers.h" - @implementation OFSequencedPacketSocket @synthesize listening = _listening, delegate = _delegate; + (void)initialize { if (self != [OFSequencedPacketSocket class]) return; - if (!of_socket_init()) + if (!OFSocketInit()) @throw [OFInitializationFailedException exceptionWithClass: self]; } + (instancetype)socket @@ -67,11 +71,11 @@ if (self.class == [OFSequencedPacketSocket class]) { [self doesNotRecognizeSelector: _cmd]; abort(); } - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; _canBlock = true; } @catch (id e) { [self release]; @throw e; } @@ -79,11 +83,11 @@ return self; } - (void)dealloc { - if (_socket != INVALID_SOCKET) + if (_socket != OFInvalidSocketHandle) [self close]; [super dealloc]; } @@ -93,11 +97,11 @@ int errNo; socklen_t len = sizeof(errNo); if (getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char *)&errNo, &len) != 0) - return of_socket_errno(); + return OFSocketErrNo(); return errNo; } #endif @@ -129,62 +133,60 @@ @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; _canBlock = canBlock; #elif defined(OF_WINDOWS) - u_long v = canBlock; + u_long v = !canBlock; if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException exceptionWithObject: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; _canBlock = canBlock; #else OF_UNRECOGNIZED_SELECTOR #endif } -- (size_t)receiveIntoBuffer: (void *)buffer - length: (size_t)length +- (size_t)receiveIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_WINDOWS if ((ret = recv(_socket, buffer, length, 0)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if ((ret = recv(_socket, buffer, (int)length, 0)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #endif return ret; } -- (void)asyncReceiveIntoBuffer: (void *)buffer - length: (size_t)length +- (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length { [self asyncReceiveIntoBuffer: buffer length: length - runLoopMode: of_run_loop_mode_default]; + runLoopMode: OFDefaultRunLoopMode]; } - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - runLoopMode: (of_run_loop_mode_t)runLoopMode + runLoopMode: (OFRunLoopMode)runLoopMode { [OFRunLoop of_addAsyncReceiveForSequencedPacketSocket: self buffer: buffer length: length mode: runLoopMode @@ -193,28 +195,25 @@ # endif delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS -- (void) - asyncReceiveIntoBuffer: (void *)buffer - length: (size_t)length - block: (of_sequenced_packet_socket_async_receive_block_t) - block +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + block: (OFSequencedPacketSocketAsyncReceiveBlock)block { [self asyncReceiveIntoBuffer: buffer length: length - runLoopMode: of_run_loop_mode_default + runLoopMode: OFDefaultRunLoopMode block: block]; } - (void) asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_sequenced_packet_socket_async_receive_block_t) - block + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFSequencedPacketSocketAsyncReceiveBlock)block { [OFRunLoop of_addAsyncReceiveForSequencedPacketSocket: self buffer: buffer length: length mode: runLoopMode @@ -221,14 +220,13 @@ block: block delegate: nil]; } #endif -- (void)sendBuffer: (const void *)buffer - length: (size_t)length +- (void)sendBuffer: (const void *)buffer length: (size_t)length { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_WINDOWS ssize_t bytesWritten; @@ -238,11 +236,11 @@ if ((bytesWritten = send(_socket, (void *)buffer, length, 0)) < 0) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #else int bytesWritten; if (length > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -250,11 +248,11 @@ if ((bytesWritten = send(_socket, buffer, (int)length, 0)) < 0) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #endif if ((size_t)bytesWritten != length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length @@ -262,16 +260,14 @@ errNo: 0]; } - (void)asyncSendData: (OFData *)data { - [self asyncSendData: data - runLoopMode: of_run_loop_mode_default]; + [self asyncSendData: data runLoopMode: OFDefaultRunLoopMode]; } -- (void)asyncSendData: (OFData *)data - runLoopMode: (of_run_loop_mode_t)runLoopMode +- (void)asyncSendData: (OFData *)data runLoopMode: (OFRunLoopMode)runLoopMode { [OFRunLoop of_addAsyncSendForSequencedPacketSocket: self data: data mode: runLoopMode # ifdef OF_HAVE_BLOCKS @@ -280,20 +276,20 @@ delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS - (void)asyncSendData: (OFData *)data - block: (of_sequenced_packet_socket_async_send_data_block_t)block + block: (OFSequencedPacketSocketAsyncSendDataBlock)block { [self asyncSendData: data - runLoopMode: of_run_loop_mode_default + runLoopMode: OFDefaultRunLoopMode block: block]; } - (void)asyncSendData: (OFData *)data - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_sequenced_packet_socket_async_send_data_block_t)block + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFSequencedPacketSocketAsyncSendDataBlock)block { [OFRunLoop of_addAsyncSendForSequencedPacketSocket: self data: data mode: runLoopMode block: block @@ -306,18 +302,18 @@ [self listenWithBacklog: SOMAXCONN]; } - (void)listenWithBacklog: (int)backlog { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; if (listen(_socket, backlog) == -1) @throw [OFListenFailedException exceptionWithSocket: self backlog: backlog - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; _listening = true; } - (instancetype)accept @@ -335,28 +331,29 @@ #if defined(HAVE_PACCEPT) && defined(SOCK_CLOEXEC) if ((client->_socket = paccept(_socket, &client->_remoteAddress.sockaddr.sockaddr, &client->_remoteAddress.length, NULL, SOCK_CLOEXEC)) == - INVALID_SOCKET) + OFInvalidSocketHandle) @throw [OFAcceptFailedException exceptionWithSocket: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #elif defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) if ((client->_socket = accept4(_socket, &client->_remoteAddress.sockaddr.sockaddr, - &client->_remoteAddress.length, SOCK_CLOEXEC)) == INVALID_SOCKET) + &client->_remoteAddress.length, SOCK_CLOEXEC)) == + OFInvalidSocketHandle) @throw [OFAcceptFailedException exceptionWithSocket: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #else if ((client->_socket = accept(_socket, &client->_remoteAddress.sockaddr.sockaddr, - &client->_remoteAddress.length)) == INVALID_SOCKET) + &client->_remoteAddress.length)) == OFInvalidSocketHandle) @throw [OFAcceptFailedException exceptionWithSocket: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; # if defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(client->_socket, F_GETFD, 0)) != -1) fcntl(client->_socket, F_SETFD, flags | FD_CLOEXEC); # endif @@ -365,65 +362,63 @@ assert(client->_remoteAddress.length <= (socklen_t)sizeof(client->_remoteAddress.sockaddr)); switch (client->_remoteAddress.sockaddr.sockaddr.sa_family) { case AF_INET: - client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPV4; + client->_remoteAddress.family = OFSocketAddressFamilyIPv4; break; #ifdef OF_HAVE_IPV6 case AF_INET6: - client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPV6; + client->_remoteAddress.family = OFSocketAddressFamilyIPv6; break; #endif #ifdef OF_HAVE_IPX case AF_IPX: - client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPX; + client->_remoteAddress.family = OFSocketAddressFamilyIPX; break; #endif default: - client->_remoteAddress.family = - OF_SOCKET_ADDRESS_FAMILY_UNKNOWN; + client->_remoteAddress.family = OFSocketAddressFamilyUnknown; break; } return client; } - (void)asyncAccept { - [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default]; + [self asyncAcceptWithRunLoopMode: OFDefaultRunLoopMode]; } -- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode +- (void)asyncAcceptWithRunLoopMode: (OFRunLoopMode)runLoopMode { [OFRunLoop of_addAsyncAcceptForSocket: self mode: runLoopMode block: NULL delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS -- (void)asyncAcceptWithBlock: (of_sequenced_packet_socket_async_accept_block_t) - block +- (void)asyncAcceptWithBlock: (OFSequencedPacketSocketAsyncAcceptBlock)block { - [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default - block: block]; + [self asyncAcceptWithRunLoopMode: OFDefaultRunLoopMode block: block]; } -- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_sequenced_packet_socket_async_accept_block_t)block +- (void) + asyncAcceptWithRunLoopMode: (OFRunLoopMode)runLoopMode + block: (OFSequencedPacketSocketAsyncAcceptBlock)block { [OFRunLoop of_addAsyncAcceptForSocket: self mode: runLoopMode block: block delegate: nil]; } #endif -- (const of_socket_address_t *)remoteAddress +- (const OFSocketAddress *)remoteAddress { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; if (_remoteAddress.length == 0) @throw [OFInvalidArgumentException exception]; @@ -434,19 +429,19 @@ } - (void)cancelAsyncRequests { [OFRunLoop of_cancelAsyncRequestsForObject: self - mode: of_run_loop_mode_default]; + mode: OFDefaultRunLoopMode]; } - (int)fileDescriptorForReading { #ifndef OF_WINDOWS return _socket; #else - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) return -1; if (_socket > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -457,11 +452,11 @@ - (int)fileDescriptorForWriting { #ifndef OF_WINDOWS return _socket; #else - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) return -1; if (_socket > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -469,15 +464,15 @@ #endif } - (void)close { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; _listening = false; memset(&_remoteAddress, 0, sizeof(_remoteAddress)); closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; } @end