ObjFW  Diff

Differences From Artifact [a4d491ccd6]:

To Artifact [d088ca13d0]:

  • File src/OFTCPSocket.m — part of check-in [1f7898f109] at 2009-12-17 10:53:23 on branch trunk — Further improve OFTCPSocket.

    Only use getaddrinfo() if it's thread-safe - it does not make much
    sense to allow getaddrinfo() if it's not thread-safe, as then its
    biggest advantage to gethostbyname() is gone.

    Also, copy the result from gethostbyname() so we can release the lock
    before connecting - this should greatly improve the speed when trying
    to connect to multiple hosts without a thread-safe getaddrinfo(). (user: js, size: 7681) [annotate] [blame] [check-ins using]


8
9
10
11
12
13
14

15
16
17
18
19
20
21
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#include <stdio.h>

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

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







>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

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

#ifndef HAVE_GETADDRINFO
# include <stdlib.h>
# ifndef _WIN32
#  include <arpa/inet.h>
49
50
51
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
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
{
	if (sock != INVALID_SOCKET)
		close(sock);

	[super dealloc];
}

/*
 * FIXME: Maybe we could copy the result of the name lookup and release the
 *	  lock so that we don't keep the lock during connection attemps.
 */
- connectToService: (OFString*)service
	    onNode: (OFString*)node
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#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];
	}

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
			continue;

		if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
			close(sock);
			sock = INVALID_SOCKET;
			continue;
		}

		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







<
<
<
<






|






<
<
<
<
|
<
<
<




<
















<
<
<
<







<

>
>
>





>










|
>

>















>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>










|







50
51
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
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
175
176
177
178
179
180
181
{
	if (sock != INVALID_SOCKET)
		close(sock);

	[super dealloc];
}





- connectToService: (OFString*)service
	    onNode: (OFString*)node
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res, *res0;

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





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



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


	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
			continue;

		if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
			close(sock);
			sock = INVALID_SOCKET;
			continue;
		}

		break;
	}

	freeaddrinfo(res0);




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

#ifdef OF_THREADS
	OFDataArray *addrlist;

	addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)];
	[mutex lock];
#endif

	if ((he = gethostbyname([node cString])) == NULL) {
#ifdef OF_THREADS
		[addrlist release];
		[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(strtol([service cString], NULL,
	    10))) == 0) {
#ifdef OF_THREADS
		[addrlist release];
		[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
		[addrlist release];
		[mutex unlock];
#endif
		@throw [OFConnectionFailedException
		    newWithClass: isa
			    node: node
			 service: service];
	}

#ifdef OF_THREADS
	@try {
		for (ip = he->h_addr_list; *ip != NULL; ip++)
			[addrlist addItem: ip];

		/* Add the terminating NULL */
		[addrlist addItem: ip];
	} @catch (OFException *e) {
		[addrlist release];
		@throw e;
	} @finally {
		[mutex unlock];
	}

	for (ip = [addrlist cArray]; *ip != NULL; ip++) {
#else
	for (ip = he->h_addr_list; *ip != NULL; ip++) {
#endif
		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
	[addrlist release];
#endif

	if (!connected) {
		close(sock);
		sock = INVALID_SOCKET;
	}
#endif
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

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service
						    family: family];

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







|






<
<
<
<
|
<
<
<




<



<
<
<







<
<
<
<







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

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service
						    family: family];

#ifdef HAVE_THREADSAFE_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)
254
255
256
257
258
259
260
261

262
263
264
265
266
267
268
		    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];







|
>







247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
		    newWithClass: isa
			    node: node
			 service: service];
	}

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