ObjFW  Diff

Differences From Artifact [367b0cdd47]:

To Artifact [f0c5480103]:

  • File src/OFTCPSocket.m — part of check-in [81d47f4398] at 2014-01-25 19:33:57 on branch trunk — Move socket includes and helpers to separate files

    The new file socket.h includes all headers required for sockets on the
    used platform, while the file socket_helpers.h defines the BSD API
    functions to the platform specific functions if necessary.

    This cleans up the classes dealing with sockets a lot and also reduces
    code duplication. (user: js, size: 15858) [annotate] [blame] [check-ins using]


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif

#import "OFTCPSocket.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"







<
<
<
<
<
<
<
<
<
<







21
22
23
24
25
26
27










28
29
30
31
32
33
34

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>











#import "OFTCPSocket.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#import "OFListenFailedException.h"
#import "OFNotConnectedException.h"
#import "OFNotImplementedException.h"
#import "OFSetOptionFailedException.h"

#import "autorelease.h"
#import "macros.h"

#ifndef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

#ifdef HAVE_THREADSAFE_GETADDRINFO
# ifndef AI_NUMERICSERV
#  define AI_NUMERICSERV 0
# endif
# ifndef AI_NUMERICHOST
#  define AI_NUMERICHOST 0
# endif
#endif

#ifndef SOMAXCONN
# define SOMAXCONN 32
#endif

#if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
# import "OFMutex.h"
# import "OFDataArray.h"

static OFMutex *mutex = nil;
#endif

#ifdef _WIN32
# define close(sock) closesocket(sock)
#endif

#ifdef _PSP
/* PSP defines AF_INET6, even though sockaddr_in6 is missing */
# undef AF_INET6
struct sockaddr_storage {
	uint8_t	       ss_len;
	sa_family_t    ss_family;
	in_port_t      ss_data1;
	struct in_addr ss_data2;
	int8_t	       ss_data3[8];
};
#endif

#ifdef __wii__
# define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen)
# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
# define close(sock) net_close(sock)
# define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
# define gethostbyname(name) net_gethostbyname(name)
# define listen(sock, backlog) net_listen(sock, backlog)
# define setsockopt(sock, level, name, value, len) \
	net_setsockopt(sock, level, name, value, len)
# define socket(domain, type, proto) net_socket(domain, type, proto)
typedef u32 in_addr_t;

struct sockaddr_storage {
	u8 ss_len;
	u8 ss_family;
	u8 ss_data[14];
};
#endif

/* References for static linking */
void _references_to_categories_of_OFTCPSocket(void)
{
	_OFTCPSocket_SOCKS5_reference = 1;
}







|
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







42
43
44
45
46
47
48
49



50













51
52
53
54
55



































56
57
58
59
60
61
62
#import "OFListenFailedException.h"
#import "OFNotConnectedException.h"
#import "OFNotImplementedException.h"
#import "OFSetOptionFailedException.h"

#import "autorelease.h"
#import "macros.h"
#import "socket_helpers.h"

















#if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
# import "OFMutex.h"
# import "OFDataArray.h"

static OFMutex *mutex = nil;



































#endif

/* References for static linking */
void _references_to_categories_of_OFTCPSocket(void)
{
	_OFTCPSocket_SOCKS5_reference = 1;
}
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

- init
{
	self = [super init];

	@try {
		_socket = INVALID_SOCKET;
		_sockAddr = NULL;
		_SOCKS5Host = [defaultSOCKS5Host copy];
		_SOCKS5Port = defaultSOCKS5Port;
	} @catch (id e) {
		[self release];
		@throw e;
	}








|







237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

- init
{
	self = [super init];

	@try {
		_socket = INVALID_SOCKET;
		_sockAddrLen = sizeof(struct sockaddr_storage);
		_SOCKS5Host = [defaultSOCKS5Host copy];
		_SOCKS5Port = defaultSOCKS5Port;
	} @catch (id e) {
		[self release];
		@throw e;
	}

674
675
676
677
678
679
680





681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

711
712
713
714
715
716
717
718
719
720
721
722
723

	close(_socket);
	_socket = INVALID_SOCKET;
	@throw [OFBindFailedException exceptionWithHost: host
						   port: port
						 socket: self];
}






- (void)listenWithBackLog: (int)backLog
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (listen(_socket, backLog) == -1)
		@throw [OFListenFailedException exceptionWithSocket: self
							    backLog: backLog];

	_listening = true;
}

- (void)listen
{
	[self listenWithBackLog: SOMAXCONN];
}

- (instancetype)accept
{
	OFTCPSocket *client;
	struct sockaddr_storage *addr;
	socklen_t addrLen;
	int socket;

	client = [[[[self class] alloc] init] autorelease];
	addrLen = sizeof(*addr);
	addr = [client allocMemoryWithSize: addrLen];

	if ((socket = accept(_socket, (struct sockaddr*)addr,

	    &addrLen)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];

	client->_socket = socket;
	client->_sockAddr = addr;
	client->_sockAddrLen = addrLen;

	return client;
}

- (void)asyncAcceptWithTarget: (id)target
		     selector: (SEL)selector
{







>
>
>
>
>













<
<
<
<
<


<
<
<
<
<
|
<
<

|
>
|

<
<
<
<







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637





638
639





640


641
642
643
644
645




646
647
648
649
650
651
652

	close(_socket);
	_socket = INVALID_SOCKET;
	@throw [OFBindFailedException exceptionWithHost: host
						   port: port
						 socket: self];
}

- (void)listen
{
	[self listenWithBackLog: SOMAXCONN];
}

- (void)listenWithBackLog: (int)backLog
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (listen(_socket, backLog) == -1)
		@throw [OFListenFailedException exceptionWithSocket: self
							    backLog: backLog];

	_listening = true;
}






- (instancetype)accept
{





	OFTCPSocket *client = [[[[self class] alloc] init] autorelease];



	if ((client->_socket = accept(_socket,
	    (struct sockaddr*)&client->_sockAddr,
	    &client->_sockAddrLen)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];





	return client;
}

- (void)asyncAcceptWithTarget: (id)target
		     selector: (SEL)selector
{
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}

- (OFString*)remoteAddress
{
	char *host;

	if (_sockAddr == NULL || _sockAddrLen == 0)
		@throw [OFNotConnectedException exceptionWithSocket: self];

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

	@try {
		if (getnameinfo((struct sockaddr*)_sockAddr, _sockAddrLen, host,
		    NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV))
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		return [OFString stringWithUTF8String: host];
	} @finally {
		[self freeMemory: host];
	}
#else
# ifdef OF_HAVE_THREADS
	[mutex lock];

	@try {
# endif
		host = inet_ntoa(((struct sockaddr_in*)_sockAddr)->sin_addr);

		if (host == NULL)
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		return [OFString stringWithUTF8String: host];
# ifdef OF_HAVE_THREADS







|






|
|













|







671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}

- (OFString*)remoteAddress
{
	char *host;

	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

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

	@try {
		if (getnameinfo((struct sockaddr*)&_sockAddr, _sockAddrLen,
		    host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV))
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		return [OFString stringWithUTF8String: host];
	} @finally {
		[self freeMemory: host];
	}
#else
# ifdef OF_HAVE_THREADS
	[mutex lock];

	@try {
# endif
		host = inet_ntoa(((struct sockaddr_in*)&_sockAddr)->sin_addr);

		if (host == NULL)
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		return [OFString stringWithUTF8String: host];
# ifdef OF_HAVE_THREADS
792
793
794
795
796
797
798
799
800
801
802
803
}

- (void)close
{
	[super close];

	_listening = false;
	[self freeMemory: _sockAddr];
	_sockAddr = NULL;
	_sockAddrLen = 0;
}
@end







<
<
|


721
722
723
724
725
726
727


728
729
730
}

- (void)close
{
	[super close];

	_listening = false;


	_sockAddrLen = sizeof(struct sockaddr_storage);
}
@end