ObjFW  Check-in [7a5b65ee4d]

Overview
Comment:Use sockaddr_storage instead of sockaddr in OFTCPSocket.
This ensures it's big enough and correctly aligned.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7a5b65ee4df292ef1dbe7c6db94dc514fa74e61236f0336ba0ce3a3d8fd31033
User & Date: js on 2011-07-17 02:03:44
Other Links: manifest | tags
Context
2011-07-17
02:11
Include sys/types.h in of_asprintf.m. check-in: 506c012ec2 user: js tags: trunk
02:03
Use sockaddr_storage instead of sockaddr in OFTCPSocket.
This ensures it's big enough and correctly aligned.
check-in: 7a5b65ee4d user: js tags: trunk
01:55
Define __NO_EXT_QNX in files using unistd.h or fcntl.h. check-in: da2701ad14 user: js tags: trunk
Changes

Modified src/OFTCPSocket.h from [abc527a77c] to [cffdc41556].

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 * \brief A class which provides functions to create and use TCP sockets.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
 */
@interface OFTCPSocket: OFStreamSocket
{
	BOOL		isListening;
	struct sockaddr	*sockAddr;
	socklen_t	sockAddrLen;
}

/**
 * Connect the OFTCPSocket to the specified destination.
 *
 * \param host The host to connect to
 * \param port The port on the host to connect to







|
|
|







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 * \brief A class which provides functions to create and use TCP sockets.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
 */
@interface OFTCPSocket: OFStreamSocket
{
	BOOL			isListening;
	struct sockaddr_storage	*sockAddr;
	socklen_t		sockAddrLen;
}

/**
 * Connect the OFTCPSocket to the specified destination.
 *
 * \param host The host to connect to
 * \param port The port on the host to connect to

Modified src/OFTCPSocket.m from [d0efc140d4] to [2ff5fb89f7].

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

369
370
371
372
373
374
375

	isListening = YES;
}

- (OFTCPSocket*)accept
{
	OFTCPSocket *newSocket;
	struct sockaddr *addr;
	socklen_t addrLen;
	int newSock;

	newSocket = [[[isa alloc] init] autorelease];
	addrLen = sizeof(struct sockaddr);

	@try {
		addr = [newSocket allocMemoryWithSize: sizeof(struct sockaddr)];
	} @catch (id e) {
		[newSocket release];
		@throw e;
	}

	if ((newSock = accept(sock, addr, &addrLen)) == INVALID_SOCKET) {

		[newSocket release];
		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	newSocket->sock = newSock;
	newSocket->sockAddr = addr;







|













|
>







347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

	isListening = YES;
}

- (OFTCPSocket*)accept
{
	OFTCPSocket *newSocket;
	struct sockaddr_storage *addr;
	socklen_t addrLen;
	int newSock;

	newSocket = [[[isa alloc] init] autorelease];
	addrLen = sizeof(struct sockaddr);

	@try {
		addr = [newSocket allocMemoryWithSize: sizeof(struct sockaddr)];
	} @catch (id e) {
		[newSocket release];
		@throw e;
	}

	if ((newSock = accept(sock, (struct sockaddr*)addr,
	    &addrLen)) == INVALID_SOCKET) {
		[newSocket release];
		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	newSocket->sock = newSock;
	newSocket->sockAddr = addr;
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	host = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL,
		    0, NI_NUMERICHOST))
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: host];
	} @finally {
		[self freeMemory: host];
	}







|
|







396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	host = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo((struct sockaddr*)sockAddr, sockAddrLen, host,
		    NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: host];
	} @finally {
		[self freeMemory: host];
	}