@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2021 Jonathan Schleifer + * Copyright (c) 2008-2022 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 @@ -45,21 +45,21 @@ OF_DIRECT_MEMBERS @interface OFSPXStreamSocketAsyncConnectDelegate: OFObject { OFSPXStreamSocket *_socket; - unsigned char _node[IPX_NODE_LEN]; uint32_t _network; + unsigned char _node[IPX_NODE_LEN]; uint16_t _port; #ifdef OF_HAVE_BLOCKS OFSPXStreamSocketAsyncConnectBlock _block; #endif } - (instancetype)initWithSocket: (OFSPXStreamSocket *)socket - node: (unsigned char [IPX_NODE_LEN])node network: (uint32_t)network + node: (unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS block: (OFSPXStreamSocketAsyncConnectBlock)block #endif ; @@ -66,23 +66,23 @@ - (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode; @end @implementation OFSPXStreamSocketAsyncConnectDelegate - (instancetype)initWithSocket: (OFSPXStreamSocket *)sock - node: (unsigned char [IPX_NODE_LEN])node network: (uint32_t)network + node: (unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS block: (OFSPXStreamSocketAsyncConnectBlock)block #endif { self = [super init]; @try { _socket = [sock retain]; - memcpy(_node, node, IPX_NODE_LEN); _network = network; + memcpy(_node, node, IPX_NODE_LEN); _port = port; #ifdef OF_HAVE_BLOCKS _block = [block copy]; #endif } @catch (id e) { @@ -104,11 +104,11 @@ } - (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode { OFSocketAddress address = - OFSocketAddressMakeIPX(_node, _network, _port); + OFSocketAddressMakeIPX(_network, _node, _port); id exception = nil; int errNo; if (![_socket of_createSocketForAddress: &address errNo: &errNo]) { exception = [self of_connectionFailedExceptionForErrNo: errNo]; @@ -116,11 +116,15 @@ } _socket.canBlock = false; if (![_socket of_connectSocketToAddress: &address errNo: &errNo]) { +#ifdef OF_WINDOWS + if (errNo == EINPROGRESS || errNo == EWOULDBLOCK) { +#else if (errNo == EINPROGRESS) { +#endif [OFRunLoop of_addAsyncConnectForSocket: _socket mode: runLoopMode delegate: self]; return; } @@ -149,28 +153,28 @@ if (_block != NULL) _block(exception); else { #endif if ([delegate respondsToSelector: - @selector(socket:didConnectToNode:network:port:exception:)]) - [delegate socket: _socket - didConnectToNode: _node - network: _network - port: _port - exception: exception]; + @selector(socket:didConnectToNetwork:node:port:exception:)]) + [delegate socket: _socket + didConnectToNetwork: _network + node: _node + port: _port + exception: exception]; #ifdef OF_HAVE_BLOCKS } #endif } - (id)of_connectionFailedExceptionForErrNo: (int)errNo { - return [OFConnectionFailedException exceptionWithNode: _node - network: _network - port: _port - socket: _socket - errNo: errNo]; + return [OFConnectionFailedException exceptionWithNetwork: _network + node: _node + port: _port + socket: _socket + errNo: errNo]; } @end @implementation OFSPXStreamSocket @dynamic delegate; @@ -204,11 +208,11 @@ errNo: (int *)errNo { if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; - if (connect(_socket, &address->sockaddr.sockaddr, + if (connect(_socket, (struct sockaddr *)&address->sockaddr, address->length) != 0) { *errNo = OFSocketErrNo(); return false; } @@ -219,58 +223,58 @@ { closesocket(_socket); _socket = OFInvalidSocketHandle; } -- (void)connectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node - network: (uint32_t)network - port: (uint16_t)port +- (void)connectToNetwork: (uint32_t)network + node: (unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port { - OFSocketAddress address = OFSocketAddressMakeIPX(node, network, port); + OFSocketAddress address = OFSocketAddressMakeIPX(network, node, port); int errNo; if (![self of_createSocketForAddress: &address errNo: &errNo]) @throw [OFConnectionFailedException - exceptionWithNode: node - network: network - port: port - socket: self - errNo: errNo]; + exceptionWithNetwork: network + node: node + port: port + socket: self + errNo: errNo]; if (![self of_connectSocketToAddress: &address errNo: &errNo]) { [self of_closeSocket]; @throw [OFConnectionFailedException - exceptionWithNode: node - network: network - port: port - socket: self - errNo: errNo]; - } -} - -- (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node - network: (uint32_t)network - port: (uint16_t)port -{ - [self asyncConnectToNode: node - network: network - port: port - runLoopMode: OFDefaultRunLoopMode]; -} - -- (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node - network: (uint32_t)network - port: (uint16_t)port - runLoopMode: (OFRunLoopMode)runLoopMode + exceptionWithNetwork: network + node: node + port: port + socket: self + errNo: errNo]; + } +} + +- (void)asyncConnectToNetwork: (uint32_t)network + node: (unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port +{ + [self asyncConnectToNetwork: network + node: node + port: port + runLoopMode: OFDefaultRunLoopMode]; +} + +- (void)asyncConnectToNetwork: (uint32_t)network + node: (unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode { void *pool = objc_autoreleasePoolPush(); [[[[OFSPXStreamSocketAsyncConnectDelegate alloc] initWithSocket: self - node: node network: network + node: node port: port #ifdef OF_HAVE_BLOCKS block: NULL #endif ] autorelease] startWithRunLoopMode: runLoopMode]; @@ -277,34 +281,34 @@ objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS -- (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node - network: (uint32_t)network - port: (uint16_t)port - block: (OFSPXStreamSocketAsyncConnectBlock)block -{ - [self asyncConnectToNode: node - network: network - port: port - runLoopMode: OFDefaultRunLoopMode - block: block]; -} - -- (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node - network: (uint32_t)network - port: (uint16_t)port - runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFSPXStreamSocketAsyncConnectBlock)block +- (void)asyncConnectToNetwork: (uint32_t)network + node: (unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port + block: (OFSPXStreamSocketAsyncConnectBlock)block +{ + [self asyncConnectToNetwork: network + node: node + port: port + runLoopMode: OFDefaultRunLoopMode + block: block]; +} + +- (void)asyncConnectToNetwork: (uint32_t)network + node: (unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFSPXStreamSocketAsyncConnectBlock)block { void *pool = objc_autoreleasePoolPush(); [[[[OFSPXStreamSocketAsyncConnectDelegate alloc] initWithSocket: self - node: node network: network + node: node port: port block: block ] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); @@ -320,13 +324,13 @@ #endif if (_socket != OFInvalidSocketHandle) @throw [OFAlreadyConnectedException exceptionWithSocket: self]; - address = OFSocketAddressMakeIPX(zeroNode, 0, port); + address = OFSocketAddressMakeIPX(0, zeroNode, port); - if ((_socket = socket(address.sockaddr.sockaddr.sa_family, + if ((_socket = socket(address.sockaddr.ipx.sipx_family, SOCK_STREAM | SOCK_CLOEXEC, NSPROTO_SPX)) == OFInvalidSocketHandle) @throw [OFBindFailedException exceptionWithPort: port packetType: SPXPacketType socket: self @@ -337,11 +341,12 @@ #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL_H) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif - if (bind(_socket, &address.sockaddr.sockaddr, address.length) != 0) { + if (bind(_socket, (struct sockaddr *)&address.sockaddr, + address.length) != 0) { int errNo = OFSocketErrNo(); closesocket(_socket); _socket = OFInvalidSocketHandle; @@ -353,11 +358,11 @@ memset(&address, 0, sizeof(address)); address.family = OFSocketAddressFamilyIPX; address.length = (socklen_t)sizeof(address.sockaddr); - if (OFGetSockName(_socket, &address.sockaddr.sockaddr, + if (OFGetSockName(_socket, (struct sockaddr *)&address.sockaddr, &address.length) != 0) { int errNo = OFSocketErrNo(); closesocket(_socket); _socket = OFInvalidSocketHandle; @@ -366,11 +371,11 @@ packetType: SPXPacketType socket: self errNo: errNo]; } - if (address.sockaddr.sockaddr.sa_family != AF_IPX) { + if (address.sockaddr.ipx.sipx_family != AF_IPX) { closesocket(_socket); _socket = OFInvalidSocketHandle; @throw [OFBindFailedException exceptionWithPort: port packetType: SPXPacketType