ObjFW  Check-in [6022cfa458]

Overview
Comment:Work around compiler bugs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6022cfa4589bccd94138c27dab2363cac43ac8119e23a515685804c5e41a7abd
User & Date: js on 2011-03-29 23:27:48
Other Links: manifest | tags
Context
2011-03-29
23:32
Fix missing include in exceptions/common.h. check-in: 48c67b1444 user: js tags: trunk
23:27
Work around compiler bugs. check-in: 6022cfa458 user: js tags: trunk
23:19
Make it possible to let -[bindToPort:onHost:] choose a port. check-in: d983af3d93 user: js tags: trunk
Changes

Modified src/OFTCPSocket.m from [c47e3134d6] to [2a0787d82a].

194
195
196
197
198
199
200

201



202
203
204
205
206
207
208
							    host: host
							    port: port];
}

- (uint16_t)bindToPort: (uint16_t)port
		onHost: (OFString*)host
{

	struct sockaddr_storage addr;



	socklen_t addrLen;

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

#ifdef HAVE_THREADSAFE_GETADDRINFO







>
|
>
>
>







194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
							    host: host
							    port: port];
}

- (uint16_t)bindToPort: (uint16_t)port
		onHost: (OFString*)host
{
	union {
		struct sockaddr_storage storage;
		struct sockaddr_in in;
		struct sockaddr_in6 in6;
	} addr;
	socklen_t addrLen;

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

#ifdef HAVE_THREADSAFE_GETADDRINFO
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
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}

	memset(&addr, 0, sizeof(addr));
	((struct sockaddr_in*)&addr)->sin_family = AF_INET;
	((struct sockaddr_in*)&addr)->sin_port = of_bswap16_if_le(port);

	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}

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

# ifdef OF_THREADS
	[mutex unlock];
# endif
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}
#endif

	if (port > 0)
		return port;

	addrLen = sizeof(addr);
	if (getsockname(sock, (struct sockaddr*)&addr, &addrLen)) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}

	if (addr.ss_family == AF_INET)
		return of_bswap16_if_le(((struct sockaddr_in*)&addr)->sin_port);
	if (addr.ss_family == AF_INET6)
		return of_bswap16_if_le(
		    ((struct sockaddr_in6*)&addr)->sin6_port);

	close(sock);
	sock = INVALID_SOCKET;
	@throw [OFBindFailedException newWithClass: isa
					    socket: self
					      host: host
					      port: port];







|
|










<
|










|












|









|
|
|
|
<







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
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}

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

	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}


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

# ifdef OF_THREADS
	[mutex unlock];
# endif
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr.in, sizeof(addr.in)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}
#endif

	if (port > 0)
		return port;

	addrLen = sizeof(addr.storage);
	if (getsockname(sock, (struct sockaddr*)&addr, &addrLen)) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}

	if (addr.storage.ss_family == AF_INET)
		return of_bswap16_if_le(addr.in.sin_port);
	if (addr.storage.ss_family == AF_INET6)
		return of_bswap16_if_le(addr.in6.sin6_port);


	close(sock);
	sock = INVALID_SOCKET;
	@throw [OFBindFailedException newWithClass: isa
					    socket: self
					      host: host
					      port: port];