Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -1042,46 +1042,40 @@ */ @interface OFBindFailedException: OFException { OFString *node; OFString *service; - int family; int errNo; } #ifdef OF_HAVE_PROPERTIES @property (readonly, nonatomic) OFString *node; @property (readonly, nonatomic) OFString *service; -@property (readonly) int family; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception * \param node The node on which binding failed * \param service The service on which binding failed - * \param family The family for which binnding failed * \return A new bind failed exception */ + newWithClass: (Class)class_ node: (OFString*)node - service: (OFString*)service - family: (int)family; + service: (OFString*)service; /** * Initializes an already allocated bind failed exception. * * \param class_ The class of the object which caused the exception * \param node The node on which binding failed * \param service The service on which binding failed - * \param family The family for which binnding failed * \return An initialized bind failed exception */ - initWithClass: (Class)class_ node: (OFString*)node - service: (OFString*)service - family: (int)family; + service: (OFString*)service; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -1093,15 +1087,10 @@ /** * \return The service on which binding failed */ - (OFString*)service; - -/** - * \return The family for which binding failed - */ -- (int)family; @end /** * \brief An exception indicating that listening on the socket failed. */ Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -1519,16 +1519,14 @@ @implementation OFBindFailedException + newWithClass: (Class)class_ node: (OFString*)node service: (OFString*)service - family: (int)family { return [[self alloc] initWithClass: class_ node: node - service: service - family: family]; + service: service]; } - initWithClass: (Class)class_ { Class c = isa; @@ -1538,18 +1536,16 @@ } - initWithClass: (Class)class_ node: (OFString*)node_ service: (OFString*)service_ - family: (int)family_ { self = [super initWithClass: class_]; @try { node = [node_ copy]; service = [service_ copy]; - family = family_; errNo = GET_SOCK_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -1569,13 +1565,13 @@ { if (description != nil) return description; description = [[OFString alloc] initWithFormat: - @"Binding service %s on node %s using family %d failed in class " - @"%s! " ERRFMT, [service cString], [node cString], family, - class_getName(inClass), ERRPARAM]; + @"Binding service %s on node %s failed in class %s! " ERRFMT, + [service cString], [node cString], class_getName(inClass), + ERRPARAM]; return description; } - (int)errNo @@ -1590,15 +1586,10 @@ - (OFString*)service { return service; } - -- (int)family -{ - return family; -} @end @implementation OFListenFailedException + newWithClass: (Class)class_ backLog: (int)backlog Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -56,12 +56,11 @@ * \param node The node to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6 * to bind to all. * \param family The family to use (AF_INET for IPv4 or AF_INET6 for IPv6) */ - (void)bindService: (OFString*)service - onNode: (OFString*)node - withFamily: (int)family; + onNode: (OFString*)node; /** * Listen on the socket. * * \param backlog Maximum length for the queue of pending connections. Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -199,53 +199,39 @@ service: service]; } - (void)bindService: (OFString*)service onNode: (OFString*)node - withFamily: (int)family { if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: isa]; -#ifndef HAVE_THREADSAFE_GETADDRINFO - if (family != AF_INET) - @throw [OFBindFailedException newWithClass: isa - node: node - service: service - family: family]; -#endif - - if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) - @throw [OFBindFailedException newWithClass: isa - node: node - service: service - family: family]; - #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = family; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if (getaddrinfo([node cString], [service cString], &hints, &res)) { - close(sock); - sock = INVALID_SOCKET; + if (getaddrinfo([node cString], [service cString], &hints, &res)) @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; - } + + if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) + @throw [OFBindFailedException newWithClass: isa + node: node + service: service]; if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); close(sock); sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa node: node - service: service - family: family]; + service: service]; } freeaddrinfo(res); #else struct hostent *he; @@ -272,12 +258,10 @@ else if ((port = OF_BSWAP16_IF_LE(strtol([service cString], NULL, 10))) == 0) { # ifdef OF_THREADS [mutex unlock]; # endif - close(sock); - sock = INVALID_SOCKET; @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -288,12 +272,10 @@ if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) { # ifdef OF_THREADS [mutex unlock]; # endif - close(sock); - sock = INVALID_SOCKET; @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -301,18 +283,21 @@ memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length); # ifdef OF_THREADS [mutex unlock]; # endif + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) + @throw [OFBindFailedException newWithClass: isa + node: node + service: service]; if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { close(sock); sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa node: node - service: service - family: family]; + service: service]; } #endif } - (void)listenWithBackLog: (int)backlog Index: tests/OFTCPSocketTests.m ================================================================== --- tests/OFTCPSocketTests.m +++ tests/OFTCPSocketTests.m @@ -47,14 +47,13 @@ TEST(@"+[socket]", (server = [OFTCPSocket socket]) && (client = [OFTCPSocket socket])) msg = [OFString stringWithFormat: - @"-[bindService:onNode:withFamily:] (port %d)", port]; + @"-[bindService:onNode:] (port %d)", port]; TEST(msg, R([server bindService: service - onNode: @"127.0.0.1" - withFamily: AF_INET])) + onNode: @"127.0.0.1"])) TEST(@"-[listen]", R([server listen])) TEST(@"-[connectToService:onNode:]", R([client connectToService: service