ObjFW  Diff

Differences From Artifact [f3c6437253]:

To Artifact [c47e3134d6]:


191
192
193
194
195
196
197
198
199
200



201
202
203
204
205
206
207
	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							  socket: self
							    host: host
							    port: port];
}

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



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

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char port_s[7];







|
|

>
>
>







191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							  socket: self
							    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
	struct addrinfo hints, *res;
	char port_s[7];
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
						      host: host
						      port: port];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;
	struct sockaddr_in addr;

# ifdef OF_THREADS
	[mutex lock];
# endif

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

								    host: host];
	}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	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

								    host: host];
	}


	memcpy(&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

						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa

						      host: host
						      port: port];
	}
#endif


























}

- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];







<










>




|
|






>



>
|






>







>




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







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
						      host: host
						      port: port];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;


# ifdef OF_THREADS
	[mutex lock];
# endif

	if ((he = gethostbyname([host cString])) == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# 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];
}

- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];