ObjFW  Diff

Differences From Artifact [2b016c1ada]:

To Artifact [c2279eed1e]:


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







-
+
+












+







	return self;
}

- (void)connectToHost: (OFString*)host
	       onPort: (uint16_t)port
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

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

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);

	if (getaddrinfo([host cString], port_s, &hints, &res0))
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];

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

140
141
142
143
144
145
146

147
148
149
150
151
152
153
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156







+







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

# ifdef OF_THREADS
	@try {
		for (ip = he->h_addr_list; *ip != NULL; ip++)
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
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







+








-
+
+












+




+








+







		close(sock);
		sock = INVALID_SOCKET;
	}
#endif

	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];
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

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

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);

	if (getaddrinfo([host cString], port_s, &hints, &res))
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];

	if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;
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
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







-
+
+



+








-
+
+



+







	}
#endif
}

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

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: isa
						      socket: self
						     backLog: backlog];

	listening = YES;
}

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

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: isa
						      socket: self
						     backLog: 5];

	listening = YES;
}

- (OFTCPSocket*)accept
{
312
313
314
315
316
317
318
319


320
321
322
323
324
325
326
324
325
326
327
328
329
330

331
332
333
334
335
336
337
338
339







-
+
+







	} @catch (id e) {
		[newsock release];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock release];
		@throw [OFAcceptFailedException newWithClass: isa];
		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	newsock->sock = s;
	newsock->sockAddr = addr;
	newsock->sockAddrLen = addrlen;

	return newsock;
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
396
397
398
399
400
401
402








403







-
-
-
-
-
-
-
-

{
	[super close];

	[self freeMemory: sockAddr];
	sockAddr = NULL;
	sockAddrLen = 0;
}

- (void)dealloc
{
	if (sock != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end