ObjFW  Diff

Differences From Artifact [fe6d461e93]:

To Artifact [dd20905993]:


49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63







-
+







#endif

- init
{
	self = [super init];

	sock = INVALID_SOCKET;
	saddr = NULL;
	sockAddr = NULL;

	return self;
}

- (void)connectToService: (OFString*)service
		  onNode: (OFString*)node
{
345
346
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
377


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

394
395
396
397
398
399
400
345
346
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

393
394
395
396
397
398
399
400







-
-
+
+














-
+







-
-
+
+















-
+








	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock dealloc];
		@throw [OFAcceptFailedException newWithClass: isa];
	}

	newsock->sock = s;
	newsock->saddr = addr;
	newsock->saddr_len = addrlen;
	newsock->sockAddr = addr;
	newsock->sockAddrLen = addrlen;

	return newsock;
}

- (void)setKeepAlivesEnabled: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa];
}

- (OFString*)remoteAddress
{
	if (saddr == NULL || saddr_len == 0)
	if (sockAddr == NULL || sockAddrLen == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	char *node = [self allocMemoryWithSize: NI_MAXHOST];

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

		return [OFString stringWithCString: node];
	} @finally {
		[self freeMemory: node];
	}
#else
	char *node;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		node = inet_ntoa(((struct sockaddr_in*)saddr)->sin_addr);
		node = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);

		if (node == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
# ifdef OF_THREADS
412
413
414
415
416
417
418
419
420
421



422
423
424
425
426
427
428
429
430
431
412
413
414
415
416
417
418



419
420
421
422
423
424
425
426
427
428
429
430
431







-
-
-
+
+
+










{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	close(sock);
	sock = INVALID_SOCKET;

	[self freeMemory: saddr];
	saddr = NULL;
	saddr_len = 0;
	[self freeMemory: sockAddr];
	sockAddr = NULL;
	sockAddrLen = 0;
}

- (void)dealloc
{
	if (sock != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end