Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -933,165 +933,154 @@ /** * \brief An exception indicating the translation of an address failed. */ @interface OFAddressTranslationFailedException: OFException { - OFString *node; - OFString *service; + OFString *host; int errNo; } #ifdef OF_HAVE_PROPERTIES -@property (readonly, nonatomic) OFString *node; -@property (readonly, nonatomic) OFString *service; +@property (readonly, nonatomic) OFString *host; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception - * \param node The node for which translation was requested - * \param service The service of the node for which translation was requested + * \param host The host for which translation was requested * \return A new address translation failed exception */ + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host; /** * Initializes an already allocated address translation failed exception. * * \param class_ The class of the object which caused the exception - * \param node The node for which translation was requested - * \param service The service of the node for which translation was requested + * \param host The host for which translation was requested * \return An initialized address translation failed exception */ - initWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host; /** * \return The errno from when the exception was created */ - (int)errNo; /** - * /return The node for which translation was requested - */ -- (OFString*)node; - -/** - * \return The service of the node for which translation was requested - */ -- (OFString*)service; + * /return The host for which translation was requested + */ +- (OFString*)host; @end /** * \brief An exception indicating that a connection could not be established. */ @interface OFConnectionFailedException: OFException { - OFString *node; - OFString *service; + OFString *host; + uint16_t port; int errNo; } #ifdef OF_HAVE_PROPERTIES -@property (readonly, nonatomic) OFString *node; -@property (readonly, nonatomic) OFString *service; +@property (readonly, nonatomic) OFString *host; +@property (readonly) uint16_t port; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception - * \param node The node to which the connection failed - * \param service The service on the node to which the connection failed + * \param host The host to which the connection failed + * \param port The port on the host to which the connection failed * \return A new connection failed exception */ + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host + port: (uint16_t)port; /** * Initializes an already allocated connection failed exception. * * \param class_ The class of the object which caused the exception - * \param node The node to which the connection failed - * \param service The service on the node to which the connection failed + * \param host The host to which the connection failed + * \param port The port on the host to which the connection failed * \return An initialized connection failed exception */ - initWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host + port: (uint16_t)port; /** * \return The errno from when the exception was created */ - (int)errNo; /** - * \return The node to which the connection failed + * \return The host to which the connection failed */ -- (OFString*)node; +- (OFString*)host; /** - * \return The service on the node to which the connection failed + * \return The port on the host to which the connection failed */ -- (OFString*)service; +- (uint16_t)port; @end /** * \brief An exception indicating that binding a socket failed. */ @interface OFBindFailedException: OFException { - OFString *node; - OFString *service; + OFString *host; + uint16_t port; int errNo; } #ifdef OF_HAVE_PROPERTIES -@property (readonly, nonatomic) OFString *node; -@property (readonly, nonatomic) OFString *service; +@property (readonly, nonatomic) OFString *host; +@property (readonly) uint16_t port; @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 host The host on which binding failed + * \param port The port on which binding failed * \return A new bind failed exception */ + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host + port: (uint16_t)port; /** * 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 host The host on which binding failed + * \param port The port on which binding failed * \return An initialized bind failed exception */ - initWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service; + host: (OFString*)host + port: (uint16_t)port; /** * \return The errno from when the exception was created */ - (int)errNo; /** - * \return The node on which binding failed + * \return The host on which binding failed */ -- (OFString*)node; +- (OFString*)host; /** - * \return The service on which binding failed + * \return The port on which binding failed */ -- (OFString*)service; +- (uint16_t)port; @end /** * \brief An exception indicating that listening on the socket failed. */ Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -1357,16 +1357,14 @@ } @end @implementation OFAddressTranslationFailedException + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service + host: (OFString*)host { return [[self alloc] initWithClass: class_ - node: node - service: service]; + host: host]; } - initWithClass: (Class)class_ { self = [super initWithClass: class_]; @@ -1375,19 +1373,17 @@ return self; } - initWithClass: (Class)class_ - node: (OFString*)node_ - service: (OFString*)service_ + host: (OFString*)host_ { self = [super initWithClass: class_]; @try { - node = [node_ copy]; - service = [service_ copy]; - errNo = GET_AT_ERRNO; + host = [host_ copy]; + errNo = GET_AT_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -1394,30 +1390,28 @@ return self; } - (void)dealloc { - [node release]; - [service release]; + [host release]; [super dealloc]; } - (OFString*)description { if (description != nil) return description; - if (node != nil && service != nil) + if (host != nil) description = [[OFString alloc] initWithFormat: - @"The service %@ on %@ could not be translated to an " - @"address in class %s. This means that either the node was " - @"not found, there is no such service on the node, there " - @"was a problem with the name server, there was a problem " - @"with your network connection or you specified an invalid " - @"node or service. " ERRFMT, service, node, - class_getName(inClass), AT_ERRPARAM]; + @"The host %@ could not be translated to an address in " + @"class %s. This means that either the host was not found, " + @"there was a problem with the name server, there was a " + @"problem with your network connection or you specified an " + @"invalid host. " ERRFMT, host, class_getName(inClass), + AT_ERRPARAM]; else description = [[OFString alloc] initWithFormat: @"An address translation failed in class %s! " ERRFMT, class_getName(inClass), AT_ERRPARAM]; @@ -1427,29 +1421,24 @@ - (int)errNo { return errNo; } -- (OFString*)node -{ - return node; -} - -- (OFString*)service -{ - return service; +- (OFString*)host +{ + return host; } @end @implementation OFConnectionFailedException + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service + host: (OFString*)host + port: (uint16_t)port { return [[self alloc] initWithClass: class_ - node: node - service: service]; + host: host + port: port]; } - initWithClass: (Class)class_ { Class c = isa; @@ -1457,19 +1446,19 @@ @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } - initWithClass: (Class)class_ - node: (OFString*)node_ - service: (OFString*)service_ + host: (OFString*)host_ + port: (uint16_t)port_ { self = [super initWithClass: class_]; @try { - node = [node_ copy]; - service = [service_ copy]; - errNo = GET_SOCK_ERRNO; + host = [host_ copy]; + port = port_; + errNo = GET_SOCK_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -1476,12 +1465,11 @@ return self; } - (void)dealloc { - [node release]; - [service release]; + [host release]; [super dealloc]; } - (OFString*)description @@ -1488,12 +1476,12 @@ { if (description != nil) return description; description = [[OFString alloc] initWithFormat: - @"A connection to service %@ on node %@ could not be established " - @"in class %s! " ERRFMT, service, node, class_getName(inClass), + @"A connection to %@ on port %" @PRIu16 @"could not be established " + @"in class %s! " ERRFMT, host, port, class_getName(inClass), ERRPARAM]; return description; } @@ -1500,29 +1488,29 @@ - (int)errNo { return errNo; } -- (OFString*)node +- (OFString*)host { - return node; + return host; } -- (OFString*)service +- (uint16_t)port { - return service; + return port; } @end @implementation OFBindFailedException + newWithClass: (Class)class_ - node: (OFString*)node - service: (OFString*)service + host: (OFString*)host + port: (uint16_t)port { return [[self alloc] initWithClass: class_ - node: node - service: service]; + host: host + port: port]; } - initWithClass: (Class)class_ { Class c = isa; @@ -1530,19 +1518,19 @@ @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } - initWithClass: (Class)class_ - node: (OFString*)node_ - service: (OFString*)service_ + host: (OFString*)host_ + port: (uint16_t)port_ { self = [super initWithClass: class_]; @try { - node = [node_ copy]; - service = [service_ copy]; - errNo = GET_SOCK_ERRNO; + host = [host_ copy]; + port = port_; + errNo = GET_SOCK_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -1549,12 +1537,11 @@ return self; } - (void)dealloc { - [node release]; - [service release]; + [host release]; [super dealloc]; } - (OFString*)description @@ -1561,29 +1548,29 @@ { if (description != nil) return description; description = [[OFString alloc] initWithFormat: - @"Binding service %@ on node %@ failed in class %s! " ERRFMT, - service, node, class_getName(inClass), ERRPARAM]; + @"Binding to port %" @PRIu16 @" on host %@ failed in class %s! " + ERRFMT, port, host, class_getName(inClass), ERRPARAM]; return description; } - (int)errNo { return errNo; } -- (OFString*)node +- (OFString*)host { - return node; + return host; } -- (OFString*)service +- (uint16_t)port { - return service; + return port; } @end @implementation OFListenFailedException + newWithClass: (Class)class_ Index: src/OFHTTPRequest.m ================================================================== --- src/OFHTTPRequest.m +++ src/OFHTTPRequest.m @@ -115,12 +115,12 @@ @throw [OFUnsupportedProtocolException newWithClass: isa URL: URL]; pool = [[OFAutoreleasePool alloc] init]; sock = [OFTCPSocket socket]; - [sock connectToService: [OFString stringWithFormat: @"%d", [URL port]] - onNode: [URL host]]; + [sock connectToHost: [URL host] + onPort: [URL port]]; @try { OFString *line; OFMutableDictionary *s_headers; OFDataArray *data; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -14,10 +14,14 @@ * file. */ #include #include + +#ifdef HAVE_INTTYPES_H +# include +#endif #import "OFObject.h" typedef uint32_t of_unichar_t; Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -41,26 +41,25 @@ } /** * Connect the OFTCPSocket to the specified destination. * - * \param service The service on the node to connect to - * \param node The node to connect to + * \param host The host to connect to + * \param port The port on the host to connect to */ -- (void)connectToService: (OFString*)service - onNode: (OFString*)node; +- (void)connectToHost: (OFString*)host + onPort: (uint16_t)port; /** - * Bind socket on the specified node and service. + * Bind the socket on the specified port and host. * - * \param service The service to bind - * \param node The node to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6 + * \param port The port to bind to + * \param host The host 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; +- (void)bindToPort: (uint16_t)port + onHost: (OFString*)host; /** * Listen on the socket. * * \param backlog Maximum length for the queue of pending connections. Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -66,28 +66,28 @@ sockAddr = NULL; return self; } -- (void)connectToService: (OFString*)service - onNode: (OFString*)node +- (void)connectToHost: (OFString*)host + onPort: (uint16_t)port { if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: isa]; #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res, *res0; + char port_s[7]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; + snprintf(port_s, 7, "%" PRIu16, port); - if (getaddrinfo([node cString], [service cString], &hints, &res0)) - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; + if (getaddrinfo([host cString], port_s, &hints, &res0)) + @throw [OFAddressTranslationFailedException newWithClass: isa + host: host]; for (res = res0; res != NULL; res = res->ai_next) { if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == INVALID_SOCKET) continue; @@ -103,82 +103,41 @@ freeaddrinfo(res0); #else BOOL connected = NO; struct hostent *he; -# ifndef _PSP - struct servent *se; -# endif struct sockaddr_in addr; - uint16_t port; char **ip; # ifdef OF_THREADS OFDataArray *addrlist; addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)]; [mutex lock]; # endif - if ((he = gethostbyname([node cString])) == NULL) { -# ifdef OF_THREADS - [addrlist release]; - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; - } - -# ifndef _PSP - if ((se = getservbyname([service cString], "tcp")) != NULL) - port = se->s_port; - else { -# endif - @try { - intmax_t p = [service decimalValue]; - - if (p < 1 || p > 65535) - @throw [OFOutOfRangeException - newWithClass: isa]; - - port = of_bswap16_if_le(p); - } @catch (OFInvalidFormatException *e) { - [e release]; -# ifdef OF_THREADS - [addrlist release]; - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; - } @catch (id e) { -# ifdef OF_THREADS - [addrlist release]; - [mutex unlock]; -# endif - @throw e; - } -# ifndef _PSP - } -# endif - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = port; + if ((he = gethostbyname([host cString])) == NULL) { +# ifdef OF_THREADS + [addrlist release]; + [mutex unlock]; +# endif + @throw [OFAddressTranslationFailedException newWithClass: isa + host: host]; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = of_bswap16_if_le(port); if (he->h_addrtype != AF_INET || (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { # ifdef OF_THREADS [addrlist release]; [mutex unlock]; # endif - @throw [OFConnectionFailedException - newWithClass: isa - node: node - service: service]; + @throw [OFConnectionFailedException newWithClass: isa + host: host + port: port]; } # ifdef OF_THREADS @try { for (ip = he->h_addr_list; *ip != NULL; ip++) @@ -216,132 +175,92 @@ } #endif if (sock == INVALID_SOCKET) @throw [OFConnectionFailedException newWithClass: isa - node: node - service: service]; + host: host + port: port]; } -- (void)bindService: (OFString*)service - onNode: (OFString*)node +- (void)bindToPort: (uint16_t)port + onHost: (OFString*)host { if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: isa]; #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res; + char port_s[7]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; + snprintf(port_s, 7, "%" PRIu16, port); - if (getaddrinfo([node cString], [service cString], &hints, &res)) - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; + if (getaddrinfo([host cString], port_s, &hints, &res)) + @throw [OFAddressTranslationFailedException newWithClass: isa + host: host]; if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException newWithClass: isa - node: node - service: service]; + host: host + port: port]; 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]; + host: host + port: port]; } freeaddrinfo(res); #else struct hostent *he; -# ifndef _PSP - struct servent *se; -# endif struct sockaddr_in addr; - uint16_t port; # ifdef OF_THREADS [mutex lock]; # endif - if ((he = gethostbyname([node cString])) == NULL) { -# ifdef OF_THREADS - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; - } - -# ifndef _PSP - if ((se = getservbyname([service cString], "tcp")) != NULL) - port = se->s_port; - else { -# endif - @try { - intmax_t p = [service decimalValue]; - - if (p < 1 || p > 65535) - @throw [OFOutOfRangeException - newWithClass: isa]; - - port = of_bswap16_if_le(p); - } @catch (OFInvalidFormatException *e) { - [e release]; -# ifdef OF_THREADS - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; - } @catch (id e) { -# ifdef OF_THREADS - [mutex unlock]; -# endif - @throw e; - } -# ifndef _PSP - } -# endif - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = port; + if ((he = gethostbyname([host cString])) == NULL) { +# ifdef OF_THREADS + [mutex unlock]; +# endif + @throw [OFAddressTranslationFailedException newWithClass: isa + host: host]; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = of_bswap16_if_le(port); if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) { # ifdef OF_THREADS [mutex unlock]; # endif - @throw [OFAddressTranslationFailedException - newWithClass: isa - node: node - service: service]; + @throw [OFAddressTranslationFailedException newWithClass: isa + host: host]; } 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]; + host: host + port: port]; if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { close(sock); sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa - node: node - service: service]; + host: host + port: port]; } #endif } - (void)listenWithBackLog: (int)backlog @@ -410,37 +329,37 @@ if (sockAddr == NULL || sockAddrLen == 0) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; #ifdef HAVE_THREADSAFE_GETADDRINFO - char *node = [self allocMemoryWithSize: NI_MAXHOST]; + char *host = [self allocMemoryWithSize: NI_MAXHOST]; @try { - if (getnameinfo(sockAddr, sockAddrLen, node, NI_MAXHOST, NULL, + if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) @throw [OFAddressTranslationFailedException newWithClass: isa]; - return [OFString stringWithCString: node]; + return [OFString stringWithCString: host]; } @finally { - [self freeMemory: node]; + [self freeMemory: host]; } #else - char *node; + char *host; # ifdef OF_THREADS [mutex lock]; @try { # endif - node = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr); + host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr); - if (node == NULL) + if (host == NULL) @throw [OFAddressTranslationFailedException newWithClass: isa]; - return [OFString stringWithCString: node]; + return [OFString stringWithCString: host]; # ifdef OF_THREADS } @finally { [mutex unlock]; } # endif Index: tests/OFTCPSocketTests.m ================================================================== --- tests/OFTCPSocketTests.m +++ tests/OFTCPSocketTests.m @@ -33,33 +33,32 @@ @implementation TestsAppDelegate (OFTCPSocketTests) - (void)TCPSocketTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFTCPSocket *server, *client = nil, *accepted; - OFString *service, *msg; + OFString *msg; uint16_t port; char buf[6]; srand(time(NULL)); port = (uint16_t)rand(); if (port < 1024) port += 1024; - service = [OFString stringWithFormat: @"%d", port]; TEST(@"+[socket]", (server = [OFTCPSocket socket]) && (client = [OFTCPSocket socket])) msg = [OFString stringWithFormat: - @"-[bindService:onNode:] (port %d)", port]; - TEST(msg, R([server bindService: service - onNode: @"127.0.0.1"])) + @"-[bindToPort:onHost:] (port " @PRIu16 @")", port]; + TEST(msg, R([server bindToPort: port + onHost: @"127.0.0.1"])) TEST(@"-[listen]", R([server listen])) - TEST(@"-[connectToService:onNode:]", - R([client connectToService: service - onNode: @"127.0.0.1"])) + TEST(@"-[connectToHost:onPort:]", + R([client connectToHost: @"127.0.0.1" + onPort: port])) TEST(@"-[accept]", (accepted = [server accept])) TEST(@"-[remoteAddress]", [[accepted remoteAddress] isEqual: @"127.0.0.1"])