ObjFW  Check-in [839112196f]

Overview
Comment:Add Win32 support to OFTCPSocket. Even IPv6 works!
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 839112196f145ed488e055338d34ad1034b7768162a2f939eae0d4872691fa58
User & Date: js on 2008-12-23 16:51:34
Other Links: manifest | tags
Context
2008-12-23
17:10
errno is not thread-safe on Win32, therefore use something else. check-in: cda65a1899 user: js tags: trunk
16:51
Add Win32 support to OFTCPSocket. Even IPv6 works! check-in: 839112196f user: js tags: trunk
15:09
New OFInitializationFailedException. check-in: 10be0cc7e6 user: js tags: trunk
Changes

Modified configure.ac from [73e2564fba] to [9ecb996b22].

62
63
64
65
66
67
68




69
70
71
72
73
74
75
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79







+
+
+
+







		ac_cv_snprintf_useful_ret="no")])
AC_MSG_RESULT($ac_cv_snprintf_useful_ret)

test x"$have_asprintf" != x"yes" -a x"$ac_cv_snprintf_useful_ret" != x"yes" && \
	AC_MSG_ERROR(No asprintf and no snprintf returning required space!)

ACX_PTHREAD

AC_CHECK_LIB(ws2_32, main,
	AC_SUBST(WS2_LIBS, "-lws2_32"),
	AC_SUBST(WS2_LIBS, ""))

AC_MSG_CHECKING(whether we have IPv6 support)
AC_CACHE_VAL(ac_cv_have_ipv6, [
	AC_TRY_RUN([
		#include <stdlib.h>
		#include <string.h>

Modified extra.mk.in from [5fb6b5b23a] to [27489ce473].

1
2
3

4
5
1
2
3
4
5
6



+


ASPRINTF = @ASPRINTF@
PTHREAD_CFLAGS = @PTHREAD_CFLAGS@
PTHREAD_LIBS = @PTHREAD_LIBS@
WS2_LIBS = @WS2_LIBS@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@

Modified src/Makefile from [fff34e12c1] to [6024c12020].

24
25
26
27
28
29
30
31

24
25
26
27
28
29
30

31







-
+
include ../buildsys.mk

CPPFLAGS += -I.. ${PTHREAD_CFLAGS}
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS} ${PTHREAD_LIBS}
LIBS += -lobjc
LIBS += -lobjc ${WS2_LIBS}

Modified src/OFTCPSocket.h from [6c14a9bc26] to [4f744ac404].

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



64
65
66
67
68
69
70







+









+

+
+
+




+
+
+
+
+








-
-
-








/*
 * Headers for Win32
 *
 * These must be imported after objc/Object and thus OFObject!
 */
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#import <winsock2.h>
#import <ws2tcpip.h>
#endif

/**
 * The OFTCPSocket class provides functions to create and use sockets.
 */
@interface OFTCPSocket: OFObject <OFStream>
{
#ifndef _WIN32
	int	  sock;
#else
	SOCKET	  sock;
#endif
	struct	  sockaddr *saddr;
	socklen_t saddr_len;
}

/**
 * This needs to be called before any socket can be used.
 */
+ (void)startup;

/**
 * Initializes an already allocated OFTCPSocket.
 *
 * \return An initialized OFTCPSocket
 */
- init;

- free;
- setSocket: (int)socket;
- setSocketAddress: (struct sockaddr*)sockaddr
	withLength: (socklen_t)len;

/**
 * Connect the OFTCPSocket to the specified destination.
 *
 * \param host The host or IP to connect to
 * \param port The port of the host to connect to
 */

Modified src/OFTCPSocket.m from [03aa5d92f4] to [546d4d81ab].

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







-
+
+
+
+

+
+
+
+
+
+
+
+
+
+



-
+









-
+







#import <string.h>
#import <unistd.h>
#import <fcntl.h>

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

#ifndef _WIN32	/* FIXME */
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

@implementation OFTCPSocket
+ (void)startup
{
#ifdef _WIN32
	WSADATA wsa;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		@throw [OFInitializationFailedException newWithClass: self];
#endif
}

- init
{
	if ((self = [super init])) {
		sock = -1;
		sock = INVALID_SOCKET;
		saddr = NULL;
		saddr_len = 0;
	}

	return self;
}

- free
{
	if (sock >= 0)
	if (sock != INVALID_SOCKET)
		close(sock);

	return [super free];
}

- setSocket: (int)socket
{
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
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

182
183
184
185
186
187
188
189







-
+
















-
+


-
+

-
+








-
+

















-
+


-
+

















-
+














-
+


-
+








-
+


-
+







{
	struct addrinfo hints, *res, *res0;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithObject: self];

	if (sock >= 0)
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithObject: self];

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithObject: self
			  andNode: host
		       andService: portstr];

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

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

		break;
	}

	freeaddrinfo(res0);

	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithObject: self
							  andHost: host
							  andPort: port];

	return self;
}

-    bindOn: (const char*)host
   withPort: (uint16_t)port
  andFamily: (int)family
{
	struct addrinfo hints, *res;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithObject: self];

	if (sock >= 0)
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithObject: self];

	if ((sock = socket(family, SOCK_STREAM, 0)) < 0)
	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithObject: self
						    andHost: host
						    andPort: port
						  andFamily: family];

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithObject: self
			  andNode: host
		       andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithObject: self
						    andHost: host
						    andPort: port
						  andFamily: family];
	}

	freeaddrinfo(res);

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	if (listen(sock, backlog) < 0)
	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithObject: self
						   andBackLog: backlog];

	return self;
}

- listen
{
	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	if (listen(sock, 5) < 0)
	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithObject: self
						   andBackLog: 5];

	return self;
}

- (OFTCPSocket*)accept
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
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
297
298

299
300
301
302
303
304
305
306
307
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
297
298
299
300
301
302
303
304
305
306
307

308

309
310
311
312
313
314
315

316
317
318

319
320
321
322
323
324
325
326
327








-
+
















-
+


-
+



-
+







-
+




















-
+


-
+



-
+





-
+








+


-
+







-
+

+
+
+
+
+
+






+
+
-
+
-







-
+


-
+








-
	@try {
		addr = [newsock getMemWithSize: sizeof(struct sockaddr)];
	} @catch(id e) {
		[newsock free];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) < 0) {
	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock free];
		@throw [OFAcceptFailedException newWithObject: self];
	}

	[newsock setSocket: s];
	[newsock setSocketAddress: addr
		       withLength: addrlen];

	return newsock;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
{
	ssize_t ret;

	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	if ((ret = recv(sock, buf, size, 0)) < 0)
	if ((ret = recv(sock, (char*)buf, size, 0)) < 1)
		@throw [OFReadFailedException newWithObject: self
						    andSize: size];

	/* This is safe, as we already checked < 0 */
	/* This is safe, as we already checked < 1 */
	return ret;
}

- (uint8_t*)readNBytes: (size_t)size
{
	uint8_t *ret;

	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	ret = [self getMemWithSize: size];

	@try {
		[self readNBytes: size
		      intoBuffer: ret];
	} @catch (id exception) {
		[self freeMem: ret];
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;

	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	if ((ret = send(sock, buf, size, 0)) < 0)
	if ((ret = send(sock, (char*)buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithObject: self
						     andSize: size];

	/* This is safe, as we already checked < 0 */
	/* This is safe, as we already checked for -1 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	return [self writeNBytes: strlen(str)
		      fromBuffer: (const uint8_t*)str];
}

- setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) < 0)
	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithObject: self];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) < 0)
	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithObject: self];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithObject: self];
#endif

	return self;
}

- enableKeepAlives: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable,
	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
	    sizeof(enable)) != 0)
		@throw [OFSetOptionFailedException newWithObject: self];

	return self;
}

- close
{
	if (sock < 0)
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithObject: self];

	sock = -1;
	sock = INVALID_SOCKET;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end
#endif

Modified tests/OFTCPSocket/Makefile from [23957c9fc4] to [85095214db].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
1
2
3
4
5
6
7

8
9
10
11
12
13
14
15







-
+







PROG_NOINST = oftcpsocket${PROG_SUFFIX}
SRCS = OFTCPSocket.m

include ../../buildsys.mk
include ../../extra.mk

CPPFLAGS += -I../../src -I../..
LIBS += -L../../src -lobjfw -lobjc
LIBS += -L../../src -lobjfw -lobjc ${WS2_LIBS}

.PHONY: run

all: run
run: ${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib
	ln -s ../../src/libobjfw.so libobjfw.so.1

Modified tests/OFTCPSocket/OFTCPSocket.m from [15dcfbc78a] to [896d312291].

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







-


-
+








-




-


-
+


+
+







#import <time.h>
#import <stdlib.h>
#import <string.h>

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

#ifndef _WIN32	/* FIXME */
inline uint16_t get_port()
{
	uint16_t port = (uint16_t)random();
	uint16_t port = (uint16_t)rand();

	if (port < 1024)
		port += 1024;

	printf("Using port %d...\n", port);

	return port;
}
#endif

int
main()
{
#ifndef _WIN32 /* FIXME */
	uint16_t port;

	srandom(time(NULL));
	srand(time(NULL));

	@try {
		[OFTCPSocket startup];

		OFTCPSocket *server = [OFTCPSocket new];
		OFTCPSocket *client = [OFTCPSocket new];
		OFTCPSocket *accepted;
		char buf[7];

		puts("== IPv4 ==");
		port = get_port();
107
108
109
110
111
112
113
114
115
116
117
106
107
108
109
110
111
112

113
114
115







-



		[accepted free];
		[client free];
		[server free];
	} @catch(OFException *e) {
		printf("EXCEPTION: %s\n", [e cString]);
		return 1;
	}
#endif

	return 0;
}