ObjFW  Check-in [488da685e4]

Overview
Comment:Fall back to gethostbyname + locking if getaddrinfo is missing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 488da685e40a63a859e15855bf6c20ff053c7b4ef57ed2aab55bc54f0a7dcca5
User & Date: js on 2009-05-24 21:09:25
Other Links: manifest | tags
Context
2009-05-24
21:51
Don't use @defs - it's not available in the ObjC2 ABI. check-in: 4cd4d94ea8 user: js tags: trunk
21:09
Fall back to gethostbyname + locking if getaddrinfo is missing. check-in: 488da685e4 user: js tags: trunk
19:31
Implement objc_sync_enter and objc_sync_exit if they're missing. check-in: de63989ad8 user: js tags: trunk
Changes

Modified configure.ac from [a880605d64] to [f223d1d76e].

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
			], [
			AC_MSG_ERROR(No pthreads or other supported threads!)])
		;;
esac

AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32")



















AC_MSG_CHECKING(whether we have IPv6 support)

AC_CACHE_VAL(ac_cv_have_ipv6, [
	AC_TRY_RUN([
		#ifndef _WIN32
		#include <sys/types.h>
		#include <sys/socket.h>
		#include <netinet/in.h>
		#else
		#include <winsock2.h>
		#include <ws2tcpip.h>
		#endif

		#ifndef INVALID_SOCKET
		#define INVALID_SOCKET -1
		#endif

		int
		main()
		{
			int fd;
			struct sockaddr_in6 addr;

			fd = socket(AF_INET6, SOCK_STREAM, 0);

			exit(fd != INVALID_SOCKET ? 0 : 1);
		}],
		ac_cv_have_ipv6="yes",
		ac_cv_have_ipv6="no",
		ac_cv_have_ipv6="no")])
AC_MSG_RESULT($ac_cv_have_ipv6)
test x"$ac_cv_have_ipv6" = x"yes" && \
	AC_DEFINE(HAVE_IPV6, 1, "Whether we have IPv6 support")

AC_CHECK_FUNC(madvise, [AC_DEFINE(HAVE_MADVISE, 1, [Whether we have madvise])])

AC_LANG([Objective C])







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

>
|
|
|
|
|
|
|
|
|
|

|
|
|

|
|
|
|
|

|

|
|
|
|
|







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
			], [
			AC_MSG_ERROR(No pthreads or other supported threads!)])
		;;
esac

AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32")

AC_MSG_CHECKING(for getaddrinfo)
AC_TRY_COMPILE([
	#include <stddef.h>
	#ifndef _WIN32
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netdb.h>
	#else
	#define _WIN32_WINNT 0x0501
	#include <ws2tcpip.h>
	#endif], [
	struct addrinfo ai;
	getaddrinfo(NULL, NULL, NULL, NULL);
	], [have_getaddrinfo="yes"], [have_getaddrinfo="no"])
AC_MSG_RESULT($have_getaddrinfo)
test x"$have_getaddrinfo" = x"yes" && \
	AC_DEFINE(HAVE_GETADDRINFO, 1, [Whether we have getaddrinfo])

AC_MSG_CHECKING(whether we have IPv6 support)
AS_IF([test x"$have_getaddrinfo" = x"yes"], [
	AC_CACHE_VAL(ac_cv_have_ipv6, [
		AC_TRY_RUN([
			#ifndef _WIN32
			#include <sys/types.h>
			#include <sys/socket.h>
			#include <netinet/in.h>
			#else
			#include <winsock2.h>
			#include <ws2tcpip.h>
			#endif

			#ifndef INVALID_SOCKET
			#define INVALID_SOCKET -1
			#endif

			int
			main()
			{
				int fd;
				struct sockaddr_in6 addr;

				fd = socket(AF_INET6, SOCK_STREAM, 0);

				exit(fd != INVALID_SOCKET ? 0 : 1);
			}],
			ac_cv_have_ipv6="yes",
			ac_cv_have_ipv6="no",
			ac_cv_have_ipv6="no")])], [ac_cv_have_ipv6="no"])
AC_MSG_RESULT($ac_cv_have_ipv6)
test x"$ac_cv_have_ipv6" = x"yes" && \
	AC_DEFINE(HAVE_IPV6, 1, "Whether we have IPv6 support")

AC_CHECK_FUNC(madvise, [AC_DEFINE(HAVE_MADVISE, 1, [Whether we have madvise])])

AC_LANG([Objective C])

Modified src/OFExceptions.m from [f7281a04f1] to [64dbc40d09].

26
27
28
29
30
31
32





33
34
35





36
37
38

39
40
41

42
43
44
45
46
47
48

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

#ifndef _WIN32
#include <errno.h>
#define GET_ERR	     errno





#define GET_SOCK_ERR errno
#define ERRFMT	     "Error string was: %s"
#define ERRPARAM     strerror(err)





#else
#include <windows.h>
#define GET_ERR	     GetLastError()

#define GET_SOCK_ERR WSAGetLastError()
#define ERRFMT	     "Error code was: %d"
#define ERRPARAM     err

#endif

#ifndef HAVE_ASPRINTF
#import "asprintf.h"
#endif

@implementation OFAllocFailedException







>
>
>
>
>



>
>
>
>
>



>



>







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
51
52
53
54
55
56
57
58
59
60

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

#ifndef _WIN32
#include <errno.h>
#define GET_ERR	     errno
#ifndef HAVE_GETADDRINFO
#define GET_AT_ERR   h_errno
#else
#define GET_AT_ERR   errno
#endif
#define GET_SOCK_ERR errno
#define ERRFMT	     "Error string was: %s"
#define ERRPARAM     strerror(err)
#ifndef HAVE_GETADDRINFO
#define AT_ERRPARAM  hstrerror(err)
#else
#define AT_ERRPARAM  strerror(err)
#endif
#else
#include <windows.h>
#define GET_ERR	     GetLastError()
#define GET_AT_ERR   WSAGetLastError()
#define GET_SOCK_ERR WSAGetLastError()
#define ERRFMT	     "Error code was: %d"
#define ERRPARAM     err
#define AT_ERRPARAM  err
#endif

#ifndef HAVE_ASPRINTF
#import "asprintf.h"
#endif

@implementation OFAllocFailedException
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
	andNode: (OFString*)node_
     andService: (OFString*)service_
{
	self = [super initWithClass: class_];

	node = [node_ retain];
	service = [service_ retain];
	err = GET_SOCK_ERR;

	return self;
}

- (void)dealloc
{
	[node release];







|







533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
	andNode: (OFString*)node_
     andService: (OFString*)service_
{
	self = [super initWithClass: class_];

	node = [node_ retain];
	service = [service_ retain];
	err = GET_AT_ERR;

	return self;
}

- (void)dealloc
{
	[node release];
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
			    @"an address in class %s. This means that either "
			    @"the node was not found, there is no such service "
			    @"on the node, there was a problem with the name "
			    @"server, there was a problem with your network "
			    @"connection or you specified an invalid node or "
			    @"service. " ERRFMT,
			    [service cString], [node cString], [class name],
			    ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;







|







560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
			    @"an address in class %s. This means that either "
			    @"the node was not found, there is no such service "
			    @"on the node, there was a problem with the name "
			    @"server, there was a problem with your network "
			    @"connection or you specified an invalid node or "
			    @"service. " ERRFMT,
			    [service cString], [node cString], [class name],
			    AT_ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;

Modified src/OFTCPSocket.m from [aef6c3578a] to [804f167c59].

10
11
12
13
14
15
16





17
18
19
20
21
22
23
24






25








26
27
28
29
30
31
32
 */

#include "config.h"

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






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

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif







@implementation OFTCPSocket








+ socket
{
	return [[[OFTCPSocket alloc] init] autorelease];
}

- init
{







>
>
>
>
>








>
>
>
>
>
>

>
>
>
>
>
>
>
>







10
11
12
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
51
 */

#include "config.h"

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

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

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

+ socket
{
	return [[[OFTCPSocket alloc] init] autorelease];
}

- init
{
45
46
47
48
49
50
51
52
53
54
55
56



57
58
59
60
61
62
63

	[super dealloc];
}

- connectToService: (OFString*)service
	    onNode: (OFString*)node
{
	struct addrinfo hints, *res, *res0;

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];




	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







<
<



>
>
>







64
65
66
67
68
69
70


71
72
73
74
75
76
77
78
79
80
81
82
83

	[super dealloc];
}

- 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 (getaddrinfo([node cString], [service cString], &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
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
			continue;
		}

		break;
	}

	freeaddrinfo(res0);





















































	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andNode: node
						      andService: service];

	return self;
}

- bindService: (OFString*)service
       onNode: (OFString*)node
   withFamily: (int)family
{
	struct addrinfo hints, *res;

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

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




	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
			 andNode: node
		      andService: service];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];
	}

	freeaddrinfo(res);















































	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)







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













<
<









>
>
>



















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







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
182
183
184
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
			continue;
		}

		break;
	}

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

	@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->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 (connect(sock, (struct sockaddr*)&addr,
			    sizeof(addr)) == -1)
				continue;

			connected = YES;

			break;
		}

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

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andNode: node
						      andService: service];

	return self;
}

- bindService: (OFString*)service
       onNode: (OFString*)node
   withFamily: (int)family
{


	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

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

#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
			 andNode: node
		      andService: service];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: 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
						   andNode: node
						andService: service
						 andFamily: family];

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

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)