ObjFW  Diff

Differences From Artifact [f13b976cd7]:

To Artifact [a4d491ccd6]:


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
# ifndef _WIN32
#  include <arpa/inet.h>
# endif
#endif

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


#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO)
#import "OFThread.h"

static OFMutex *mutex = nil;
#endif

@implementation OFTCPSocket
#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO)
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		mutex = [[OFMutex alloc] init];
}
#endif








>





|






|







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
# ifndef _WIN32
#  include <arpa/inet.h>
# endif
#endif

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

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
#import "OFThread.h"

static OFMutex *mutex = nil;
#endif

@implementation OFTCPSocket
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		mutex = [[OFMutex alloc] init];
}
#endif

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res, *res0;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

#ifndef HAVE_THREADSAFE_GETADDRINFO
	[mutex lock];
#endif

	if (getaddrinfo([node cString], [service cString], &hints, &res0)) {
#ifndef HAVE_THREADSAFE_GETADDRINFO
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}







|




|







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res, *res0;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	[mutex lock];
#endif

	if (getaddrinfo([node cString], [service cString], &hints, &res0)) {
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}
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
152
153
154
155

156

157
158
159
160
161
162
163
		}

		break;
	}

	freeaddrinfo(res0);

#ifndef HAVE_THREADSAFE_GETADDRINFO
	[mutex unlock];
#endif
#else
	BOOL connected = NO;
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;
	char **ip;


	[mutex lock];


	if ((he = gethostbyname([node cString])) == NULL) {

		[mutex unlock];

		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

	if ((se = getservbyname([service cString], "TCP")) != NULL)
		port = se->s_port;
	else if ((port = OF_BSWAP16_IF_LE(atoi([service cString]))) == 0) {

		[mutex unlock];

		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

	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) {

		[mutex unlock];

		@throw [OFConnectionFailedException
		    newWithClass: isa
			    node: node
			 service: 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, sizeof(addr)) == -1)
			continue;

		connected = YES;
		break;
	}


	[mutex unlock];


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








|










>

>


>

>









>

>












>

>
















>

>







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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		}

		break;
	}

	freeaddrinfo(res0);

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	[mutex unlock];
#endif
#else
	BOOL connected = NO;
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;
	char **ip;

#ifdef OF_THREADS
	[mutex lock];
#endif

	if ((he = gethostbyname([node cString])) == NULL) {
#ifdef OF_THREADS
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

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

	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) {
#ifdef OF_THREADS
		[mutex unlock];
#endif
		@throw [OFConnectionFailedException
		    newWithClass: isa
			    node: node
			 service: 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, sizeof(addr)) == -1)
			continue;

		connected = YES;
		break;
	}

#ifdef OF_THREADS
	[mutex unlock];
#endif

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

185
186
187
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
235
236
237
238
239
240
241
242
243

244

245
246
247
248
249
250
251
252

253

254
255
256
257
258
259
260
#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;





	if (getaddrinfo([node cString], [service cString], &hints, &res))



		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];


	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);



		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service
						    family: family];
	}

	freeaddrinfo(res);




#else
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;

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


	[mutex lock];


	if ((he = gethostbyname([node cString])) == NULL) {

		[mutex unlock];

		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

	if ((se = getservbyname([service cString], "TCP")) != NULL)
		port = se->s_port;
	else if ((port = OF_BSWAP16_IF_LE(atoi([service cString]))) == 0) {

		[mutex unlock];

		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: 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) {

		[mutex unlock];

		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: 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
						      node: node
						   service: service
						    family: family];
#endif







>
>
>
>
|
>
>
>




>



>
>
>







>
>
>
>












>

>


>

>









>

>











>

>








>

>







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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	[mutex lock];
#endif

	if (getaddrinfo([node cString], [service cString], &hints, &res)) {
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
		[mutex unlock];
#endif
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service
						    family: family];
	}

	freeaddrinfo(res);

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	[mutex unlock];
#endif
#else
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;

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

#ifdef OF_THREADS
	[mutex lock];
#endif

	if ((he = gethostbyname([node cString])) == NULL) {
#ifdef OF_THREADS
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

	if ((se = getservbyname([service cString], "TCP")) != NULL)
		port = se->s_port;
	else if ((port = OF_BSWAP16_IF_LE(atoi([service cString]))) == 0) {
#ifdef OF_THREADS
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: 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) {
#ifdef OF_THREADS
		[mutex unlock];
#endif
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

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

#ifdef OF_THREADS
	[mutex unlock];
#endif

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