Index: TODO ================================================================== --- TODO +++ TODO @@ -3,11 +3,10 @@ OFBase64 OFDirectory OFDictionary OFSortedArray -OFSocket OFThread OFAutoreleasePool OFStack OFQueue Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -7,12 +7,12 @@ OFHashes.m \ OFFile.m \ OFList.m \ OFListObject.m \ OFObject.m \ - OFSocket.m \ OFString.m \ + OFTCPSocket.m \ OFXMLFactory.m INCLUDES = ${SRCS:.m=.h} \ OFMacros.h \ OFStream.h DELETED src/OFSocket.h Index: src/OFSocket.h ================================================================== --- src/OFSocket.h +++ src/OFSocket.h @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2008 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of libobjfw. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#import - -#import -#import -#import - -#import "OFObject.h" -#import "OFStream.h" - -/** - * The OFSocketAddress class is a class to build socket addresses. - */ -@interface OFSocketAddress: OFObject -{ - char *hoststr, portstr[6]; - struct addrinfo hints, *res; -} - -/** - * \param host The host of the address - * \param port The port of the address - * \param family The protocol family to use - * \param type The socket type to use - * \param protocol The specific protocol to use - * \return A new OFSocketAddress - */ -+ newWithHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family - andType: (int)type - andProtocol: (int)protocol; - -/** - * Initializes an already allocated OFSocketAddress. - * - * \param host The host of the address - * \param port The port of the address - * \param family The protocol family to use - * \param type The socket type to use - * \param protocol The specific protocol to use - * \return An initialized OFSocketAddress - */ -- initWithHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family - andType: (int)type - andProtocol: (int)protocol; - -/* - * \return The addrinfo struct for the OFSocketAddress - */ -- (struct addrinfo*)getAddressInfo; - -- free; -@end - -/** - * The OFSocket class provides functions to create and use sockets. - */ -@interface OFSocket: OFObject -{ - int sock; -} - -- free; - -/** - * Connect the OFSocket to a destination specified in an OFSocketAddress. - * - * \param addr A OFSocketAddress to connect to. - */ -- connect: (OFSocketAddress*)addr; - -/** - * Receive data from the socket into a buffer. - * - * \param buf The buffer into which the data is read - * \param size The size of the data that should be read. - * The buffer MUST be at least size big! - * \return The number of bytes read - */ -- (size_t)readNBytes: (size_t)size - intoBuffer: (uint8_t*)buf; - -/** - * Receive data from the socket into a new buffer. - * - * \param size The size of the data that should be read - * \return A new buffer with the data read. - * It is part of the memory pool of the OFFile. - */ -- (uint8_t*)readNBytes: (size_t)size; - -/** - * Sends data from a buffer. - * - * \param buf The buffer from which the data is written to the file - * \param size The size of the data that should be written - * \return The number of bytes written - */ -- (size_t)writeNBytes: (size_t)size - fromBuffer: (const uint8_t*)buf; - -/** - * Sends a C string, without the trailing zero. - * - * \param str The C string from which the data is sent - * \return The number of bytes written - */ -- (size_t)writeCString: (const char*)str; -@end DELETED src/OFSocket.m Index: src/OFSocket.m ================================================================== --- src/OFSocket.m +++ src/OFSocket.m @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2008 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of libobjfw. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#import "config.h" - -#import -#import -#import -#import - -#import "OFSocket.h" -#import "OFExceptions.h" - -@implementation OFSocketAddress -+ newWithHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family - andType: (int)type - andProtocol: (int)protocol -{ - return [[OFSocketAddress alloc] initWithHost: host - andPort: port - andFamily: family - andType: type - andProtocol: protocol]; -} - -- initWithHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family - andType: (int)type - andProtocol: (int)protocol -{ - if ((self = [super init])) { - if (port == 0) { - /* FIXME: Throw exception */ - [self free]; - return nil; - } - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = family; - hints.ai_socktype = type; - hints.ai_protocol = protocol; - - hoststr = strdup(host); - snprintf(portstr, 6, "%d", port); - - res = NULL; - } - - return self; -} - -- (struct addrinfo*)getAddressInfo -{ - if (res != NULL) - return res; - - if (getaddrinfo(hoststr, portstr, &hints, &res)) { - /* FIXME: Throw exception */ - return NULL; - } - - return res; -} - -- free -{ - free(hoststr); - - if (res != NULL) - freeaddrinfo(res); - - return [super free]; -} -@end - -@implementation OFSocket -- free -{ - if (sock >= 0) - close(sock); - - return [super free]; -} - -- connect: (OFSocketAddress*)addr -{ - struct addrinfo *ai, *iter; - - ai = [addr getAddressInfo]; - for (iter = ai; iter != NULL; iter = iter->ai_next) { - if ((sock = socket(iter->ai_family, iter->ai_socktype, - iter->ai_protocol)) < 0) - continue; - - if (connect(sock, iter->ai_addr, iter->ai_addrlen) < 0) { - close(sock); - sock = -1; - continue; - } - - break; - } - - if (sock < 0) { - /* FIXME: Throw exception */ - return nil; - } - - return self; -} - -- (size_t)readNBytes: (size_t)size - intoBuffer: (uint8_t*)buf -{ - ssize_t ret; - - if ((ret = recv(sock, buf, size, 0)) < 0) { - /* FIXME: Throw exception */ - return 0; - } - - /* This is safe, as we already checked < 0 */ - return ret; -} - -- (uint8_t*)readNBytes: (size_t)size -{ - uint8_t *ret; - - ret = [self getMemWithSize: size]; - - @try { - [self readNBytes: size - intoBuffer: ret]; - } @catch (id exception) { - [self freeMem: ret]; - @throw exception; - } - - return ret; -} - -- (size_t)writeNBytes: (size_t)size - fromBuffer: (const uint8_t*)buf -{ - ssize_t ret; - - if ((ret = send(sock, buf, size, 0)) < 0) { - /* FIXME: Throw exception */ - return 0; - } - - /* This is safe, as we already checked < 0 */ - return ret; -} - -- (size_t)writeCString: (const char*)str -{ - return [self writeNBytes: strlen(str) - fromBuffer: (const uint8_t*)str]; -} -@end ADDED src/OFTCPSocket.h Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import + +#import +#import +#import + +#import "OFObject.h" +#import "OFStream.h" + +/** + * The OFTCPSocket class provides functions to create and use sockets. + */ +@interface OFTCPSocket: OFObject +{ + int sock; +} + +- free; + +/** + * Connect the OFTCPSocket to the specified destination. + * + * \param host The host to connect to + * \param port The port of the host to connect to + */ +- connectTo: (const char*)host + onPort: (uint16_t)port; + +/** + * Receive data from the socket into a buffer. + * + * \param buf The buffer into which the data is read + * \param size The size of the data that should be read. + * The buffer MUST be at least size big! + * \return The number of bytes read + */ +- (size_t)readNBytes: (size_t)size + intoBuffer: (uint8_t*)buf; + +/** + * Receive data from the socket into a new buffer. + * + * \param size The size of the data that should be read + * \return A new buffer with the data read. + * It is part of the memory pool of the OFFile. + */ +- (uint8_t*)readNBytes: (size_t)size; + +/** + * Sends data from a buffer. + * + * \param buf The buffer from which the data is written to the file + * \param size The size of the data that should be written + * \return The number of bytes written + */ +- (size_t)writeNBytes: (size_t)size + fromBuffer: (const uint8_t*)buf; + +/** + * Sends a C string, without the trailing zero. + * + * \param str The C string from which the data is sent + * \return The number of bytes written + */ +- (size_t)writeCString: (const char*)str; +@end ADDED src/OFTCPSocket.m Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "config.h" + +#import +#import +#import +#import + +#import "OFTCPSocket.h" +#import "OFExceptions.h" + +@implementation OFTCPSocket +- free +{ + if (sock >= 0) + close(sock); + + return [super free]; +} + +- connectTo: (const char*)host + onPort: (uint16_t)port +{ + struct addrinfo hints, *res, *res0; + char portstr[6]; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + snprintf(portstr, 6, "%d", port); + + if (getaddrinfo(host, portstr, &hints, &res0)) { + /* FIXME: Throw exception */ + return NULL; + } + + for (res = res0; res != NULL; res = res->ai_next) { + if ((sock = socket(res->ai_family, res->ai_socktype, + res->ai_protocol)) < 0) + continue; + + if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { + close(sock); + sock = -1; + continue; + } + + break; + } + + freeaddrinfo(res0); + + if (sock < 0) { + /* FIXME: Throw exception */ + return nil; + } + + return self; +} + +- (size_t)readNBytes: (size_t)size + intoBuffer: (uint8_t*)buf +{ + ssize_t ret; + + if ((ret = recv(sock, buf, size, 0)) < 0) { + /* FIXME: Throw exception */ + return 0; + } + + /* This is safe, as we already checked < 0 */ + return ret; +} + +- (uint8_t*)readNBytes: (size_t)size +{ + uint8_t *ret; + + ret = [self getMemWithSize: size]; + + @try { + [self readNBytes: size + intoBuffer: ret]; + } @catch (id exception) { + [self freeMem: ret]; + @throw exception; + } + + return ret; +} + +- (size_t)writeNBytes: (size_t)size + fromBuffer: (const uint8_t*)buf +{ + ssize_t ret; + + if ((ret = send(sock, buf, size, 0)) < 0) { + /* FIXME: Throw exception */ + return 0; + } + + /* This is safe, as we already checked < 0 */ + return ret; +} + +- (size_t)writeCString: (const char*)str +{ + return [self writeNBytes: strlen(str) + fromBuffer: (const uint8_t*)str]; +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -1,9 +1,9 @@ SUBDIRS = OFObject \ OFArray \ OFHashes \ - OFSocket \ OFString \ + OFTCPSocket \ OFList \ OFXMLFactory include ../buildsys.mk DELETED tests/OFSocket/Makefile Index: tests/OFSocket/Makefile ================================================================== --- tests/OFSocket/Makefile +++ tests/OFSocket/Makefile @@ -1,20 +0,0 @@ -PROG_NOINST = ofsocket -SRCS = OFSocket.m - -include ../../buildsys.mk - -CPPFLAGS += -I../../src -I../.. -LIBS += -lobjc -L../../src -lobjfw - -.PHONY: run - -all: run -run: ${PROG_NOINST} - rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib - ln -s ../../src/libobjfw.so libobjfw.so.1 - ln -s ../../src/libobjfw.so libobjfw.so.1.0 - ln -s ../../src/libobjfw.dylib libobjfw.dylib - LD_LIBRARY_PATH=. \ - DYLD_LIBRARY_PATH=. \ - ./${PROG_NOINST} - rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib DELETED tests/OFSocket/OFSocket.m Index: tests/OFSocket/OFSocket.m ================================================================== --- tests/OFSocket/OFSocket.m +++ tests/OFSocket/OFSocket.m @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2008 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of libobjfw. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#import "config.h" - -#import - -#import "OFSocket.h" -#import "OFExceptions.h" - -int -main() -{ - OFSocketAddress *addr; - OFSocket *sock; - - @try { - addr = [OFSocketAddress newWithHost: "webkeks.org" - andPort: 80 - andFamily: AF_UNSPEC - andType: SOCK_STREAM - andProtocol: 0]; - sock = [OFSocket new]; - [sock connect: addr]; - [addr free]; - - [sock writeCString: "GET / HTTP/1.1\r\n" - "Host: webkeks.org\r\n\r\n"]; - puts((char*)[sock readNBytes: 1024]); - - [sock free]; - } @catch(OFException *e) { - printf("EXCEPTION: %s\n", [e cString]); - } - - return 0; -} ADDED tests/OFTCPSocket/Makefile Index: tests/OFTCPSocket/Makefile ================================================================== --- tests/OFTCPSocket/Makefile +++ tests/OFTCPSocket/Makefile @@ -0,0 +1,20 @@ +PROG_NOINST = oftcpsocket +SRCS = OFTCPSocket.m + +include ../../buildsys.mk + +CPPFLAGS += -I../../src -I../.. +LIBS += -lobjc -L../../src -lobjfw + +.PHONY: run + +all: run +run: ${PROG_NOINST} + rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib + ln -s ../../src/libobjfw.so libobjfw.so.1 + ln -s ../../src/libobjfw.so libobjfw.so.1.0 + ln -s ../../src/libobjfw.dylib libobjfw.dylib + LD_LIBRARY_PATH=. \ + DYLD_LIBRARY_PATH=. \ + ./${PROG_NOINST} + rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib ADDED tests/OFTCPSocket/OFTCPSocket.m Index: tests/OFTCPSocket/OFTCPSocket.m ================================================================== --- tests/OFTCPSocket/OFTCPSocket.m +++ tests/OFTCPSocket/OFTCPSocket.m @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "config.h" + +#import + +#import "OFTCPSocket.h" +#import "OFExceptions.h" + +int +main() +{ + OFTCPSocket *sock; + + @try { + sock = [OFTCPSocket new]; + [sock connectTo: "webkeks.org" + onPort: 80]; + [sock writeCString: "GET / HTTP/1.1\r\n" + "Host: webkeks.org\r\n\r\n"]; + puts((char*)[sock readNBytes: 1024]); + [sock free]; + } @catch(OFException *e) { + printf("EXCEPTION: %s\n", [e cString]); + } + + return 0; +}