@@ -14,13 +14,13 @@ * file. */ #include "config.h" -#include - #include +#include +#include #import "OFUDPSocket.h" #ifdef OF_HAVE_THREADS # import "OFThread.h" #endif @@ -279,11 +279,11 @@ + (void)initialize { if (self != [OFUDPSocket class]) return; - if (!of_init_sockets()) + if (!of_socket_init()) @throw [OFInitializationFailedException exceptionWithClass: self]; } + (instancetype)socket @@ -400,26 +400,32 @@ #endif if ((_socket = socket(results[0]->family, results[0]->type | SOCK_CLOEXEC, results[0]->protocol)) == INVALID_SOCKET) - @throw [OFBindFailedException exceptionWithHost: host - port: port - socket: self]; + @throw [OFBindFailedException + exceptionWithHost: host + port: port + socket: self + errNo: of_socket_errno()]; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif if (bind(_socket, results[0]->address, results[0]->addressLength) == -1) { + int errNo = of_socket_errno(); + close(_socket); _socket = INVALID_SOCKET; + @throw [OFBindFailedException exceptionWithHost: host port: port - socket: self]; + socket: self + errNo: errNo]; } } @finally { of_resolver_free(results); } @@ -427,15 +433,19 @@ return port; #ifndef __wii__ addrLen = (socklen_t)sizeof(addr.storage); if (getsockname(_socket, (struct sockaddr*)&addr.storage, &addrLen)) { + int errNo = of_socket_errno(); + close(_socket); _socket = INVALID_SOCKET; + @throw [OFBindFailedException exceptionWithHost: host port: port - socket: self]; + socket: self + errNo: errNo]; } if (addr.storage.ss_family == AF_INET) return OF_BSWAP16_IF_LE(addr.in.sin_port); # ifdef AF_INET6 @@ -446,11 +456,12 @@ close(_socket); _socket = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithHost: host port: port - socket: self]; + socket: self + errNo: EAFNOSUPPORT]; } - (size_t)receiveIntoBuffer: (void*)buffer length: (size_t)length sender: (of_udp_socket_address_t*)sender @@ -463,20 +474,24 @@ sender->length = (socklen_t)sizeof(sender->address); #ifndef _WIN32 if ((ret = recvfrom(_socket, buffer, length, 0, (struct sockaddr*)&sender->address, &sender->length)) < 0) - @throw [OFReadFailedException exceptionWithObject: self - requestedLength: length]; + @throw [OFReadFailedException + exceptionWithObject: self + requestedLength: length + errNo: of_socket_errno()]; #else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if ((ret = recvfrom(_socket, buffer, (int)length, 0, (struct sockaddr*)&sender->address, &sender->length)) < 0) - @throw [OFReadFailedException exceptionWithObject: self - requestedLength: length]; + @throw [OFReadFailedException + exceptionWithObject: self + requestedLength: length + errNo: of_socket_errno()]; #endif return ret; } @@ -512,20 +527,24 @@ @throw [OFNotConnectedException exceptionWithSocket: self]; #ifndef _WIN32 if (sendto(_socket, buffer, length, 0, (struct sockaddr*)&receiver->address, receiver->length) < length) - @throw [OFWriteFailedException exceptionWithObject: self - requestedLength: length]; + @throw [OFWriteFailedException + exceptionWithObject: self + requestedLength: length + errNo: of_socket_errno()]; #else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if (sendto(_socket, buffer, (int)length, 0, (struct sockaddr*)&receiver->address, receiver->length) < length) - @throw [OFWriteFailedException exceptionWithObject: self - requestedLength: length]; + @throw [OFWriteFailedException + exceptionWithObject: self + requestedLength: length + errNo: of_socket_errno()]; #endif } - (void)cancelAsyncRequests {