ObjFW  Diff

Differences From Artifact [3f2fb0c9c6]:

  • File src/OFTCPSocket.m — part of check-in [033054ad75] at 2009-05-29 19:21:57 on branch trunk — A few renames.

    OFExceptions:
    * OFNoMemException to OFOutOfMemoryException.
    * OFMemNotPartOfObjException to OFMemoryNotPartOfObjectException.

    OFObject:
    * -[addItemToMemoryPool:] to -[addMemoryToPool:].
    * -[allocWithSize:] to -[allocMemoryWithSize:].
    * -[allocNItems:withSize] to -[allocMemoryForNItems:withSize:].
    * -[resizeMem:toSize] to -[resizeMemory:toSize:].
    * -[resizeMem:toNItems:withSize:] to
    -[resizeMemoryToNItems:withSize:].
    * -[freeMem] to -[freeMemory:].

    OFString:
    * -[urlencode] to -[urlEncodedString].
    * -[urldecode] to -[urlDecodedString]. (user: js, size: 6709) [annotate] [blame] [check-ins using]

To Artifact [48bd51099f]:


13
14
15
16
17
18
19

20

21
22
23
24
25
26
27
28
29
30
31
32
33

34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42

43
44
45
46
47
48
49
50







+

+












-
+







-
+








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

#ifndef HAVE_GETADDRINFO
#include <stdlib.h>
#ifndef _WIN32
#include <arpa/inet.h>
#endif
#endif

#import "OFTCPSocket.h"
#import "OFExceptions.h"

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#ifndef HAVE_GETADDRINFO
#import "OFThread.h"

static OFObject *lock = nil;
static OFMutex *mutex = nil;
#endif

@implementation OFTCPSocket
#ifndef HAVE_GETADDRINFO
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		lock = [[OFObject alloc] init];
		mutex = [[OFMutex alloc] init];
}
#endif

- (void)dealloc
{
	if (sock != INVALID_SOCKET)
		close(sock);
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
124

125
126

127
128

129
130
131


132


133
134
135



136
137
138
139
140
141
142
143
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
124
125
126
127
128
129
130
131
132

133


134
135

136



137
138
139
140
141



142
143
144

145
146
147
148
149
150
151







+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
-
+
+
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

-
+
-
-
+

-
+
-
-
-
+
+

+
+
-
-
-
+
+
+
-







	BOOL connected = NO;
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;
	char **ip;

	[mutex lock];
	@synchronized (lock) {
		if ((he = gethostbyname([node cString])) == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		if ((se = getservbyname([service cString], "TCP")) != NULL)
			port = se->s_port;
		else if ((port = htons(atoi([service cString]))) == 0)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

	if ((he = gethostbyname([node cString])) == NULL) {
		[mutex unlock];
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

	if ((se = getservbyname([service cString], "TCP")) != NULL)
		port = se->s_port;
	else if ((port = htons(atoi([service cString]))) == 0) {
		[mutex unlock];
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = port;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = port;

		if (he->h_addrtype != AF_INET ||
		    (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
			@throw [OFConnectionFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		for (ip = he->h_addr_list; *ip != NULL; ip++) {
			memcpy(&addr.sin_addr.s_addr, *ip, he->h_length);
	if (he->h_addrtype != AF_INET ||
	    (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
		[mutex unlock];
		@throw [OFConnectionFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

	for (ip = he->h_addr_list; *ip != NULL; ip++) {
		memcpy(&addr.sin_addr.s_addr, *ip, he->h_length);

			if (connect(sock, (struct sockaddr*)&addr,
		if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
			    sizeof(addr)) == -1)
				continue;
			continue;

			connected = YES;
		connected = YES;

			break;
		}
		break;
	}

	[mutex unlock];

		if (!connected) {
			close(sock);
			sock = INVALID_SOCKET;
	if (!connected) {
		close(sock);
		sock = INVALID_SOCKET;
		}
	}
#endif

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andNode: node
						      andService: service];
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212






















213
214
215
216
217
218
219
220











221
222
223
224
225




226
227
228
229
230
231
232
233
234
196
197
198
199
200
201
202
203


















204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226







227
228
229
230
231
232
233
234
235
236
237
238




239
240
241
242
243

244
245
246
247
248
249
250







+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

-
-
-
-
+
+
+
+

-








	if (family != AF_INET)
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];

	[mutex lock];
	@synchronized (lock) {
		if ((he = gethostbyname([node cString])) == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		if ((se = getservbyname([service cString], "TCP")) != NULL)
			port = se->s_port;
		else if ((port = htons(atoi([service cString]))) == 0)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = port;

	if ((he = gethostbyname([node cString])) == NULL) {
		[mutex unlock];
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

	if ((se = getservbyname([service cString], "TCP")) != NULL)
		port = se->s_port;
	else if ((port = htons(atoi([service cString]))) == 0) {
		[mutex unlock];
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = port;

		if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
		[mutex lock];
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];
	}

	memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);

	[mutex unlock];

		if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
			@throw [OFBindFailedException newWithClass: isa
							   andNode: node
							andService: service
	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
							 andFamily: family];
	}
#endif

	return self;
}

- listenWithBackLog: (int)backlog
{