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
|
@throw [OFConnectionFailedException newWithClass: isa
node: node
service: service];
}
- (void)bindService: (OFString*)service
onNode: (OFString*)node
withFamily: (int)family
{
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
#ifndef HAVE_THREADSAFE_GETADDRINFO
if (family != AF_INET)
@throw [OFBindFailedException newWithClass: isa
node: node
service: service
family: family];
#endif
if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithClass: isa
node: node
service: service
family: family];
#ifdef HAVE_THREADSAFE_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)) {
close(sock);
sock = INVALID_SOCKET;
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: service];
}
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
close(sock);
sock = INVALID_SOCKET;
@throw [OFBindFailedException newWithClass: isa
node: node
service: service
family: family];
}
freeaddrinfo(res);
#else
struct hostent *he;
struct servent *se;
struct sockaddr_in addr;
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
|
>
>
>
>
|
<
|
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
|
@throw [OFConnectionFailedException newWithClass: isa
node: node
service: service];
}
- (void)bindService: (OFString*)service
onNode: (OFString*)node
{
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
#ifdef HAVE_THREADSAFE_GETADDRINFO
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo([node cString], [service cString], &hints, &res))
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: service];
if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithClass: isa
node: node
service: service];
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
close(sock);
sock = INVALID_SOCKET;
@throw [OFBindFailedException newWithClass: isa
node: node
service: service];
}
freeaddrinfo(res);
#else
struct hostent *he;
struct servent *se;
struct sockaddr_in addr;
|
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
|
if ((se = getservbyname([service cString], "TCP")) != NULL)
port = se->s_port;
else if ((port = OF_BSWAP16_IF_LE(strtol([service cString], NULL,
10))) == 0) {
# ifdef OF_THREADS
[mutex unlock];
# endif
close(sock);
sock = INVALID_SOCKET;
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: 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) {
# ifdef OF_THREADS
[mutex unlock];
# endif
close(sock);
sock = INVALID_SOCKET;
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: service];
}
memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
# ifdef OF_THREADS
[mutex unlock];
# endif
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
close(sock);
sock = INVALID_SOCKET;
@throw [OFBindFailedException newWithClass: isa
node: node
service: service
family: family];
}
#endif
}
- (void)listenWithBackLog: (int)backlog
{
if (sock == INVALID_SOCKET)
|
<
<
<
<
>
>
>
>
|
<
|
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
|
if ((se = getservbyname([service cString], "TCP")) != NULL)
port = se->s_port;
else if ((port = OF_BSWAP16_IF_LE(strtol([service cString], NULL,
10))) == 0) {
# ifdef OF_THREADS
[mutex unlock];
# endif
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: 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) {
# ifdef OF_THREADS
[mutex unlock];
# endif
@throw [OFAddressTranslationFailedException
newWithClass: isa
node: node
service: service];
}
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
node: node
service: service];
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
close(sock);
sock = INVALID_SOCKET;
@throw [OFBindFailedException newWithClass: isa
node: node
service: service];
}
#endif
}
- (void)listenWithBackLog: (int)backlog
{
if (sock == INVALID_SOCKET)
|
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
newsock = [[[isa alloc] init] autorelease];
addrlen = sizeof(struct sockaddr);
@try {
addr = [newsock allocMemoryWithSize: sizeof(struct sockaddr)];
} @catch (id e) {
[newsock dealloc];
@throw e;
}
if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
[newsock dealloc];
@throw [OFAcceptFailedException newWithClass: isa];
}
newsock->sock = s;
newsock->sockAddr = addr;
newsock->sockAddrLen = addrlen;
|
|
|
|
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
newsock = [[[isa alloc] init] autorelease];
addrlen = sizeof(struct sockaddr);
@try {
addr = [newsock allocMemoryWithSize: sizeof(struct sockaddr)];
} @catch (id e) {
[newsock release];
@throw e;
}
if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
[newsock release];
@throw [OFAcceptFailedException newWithClass: isa];
}
newsock->sock = s;
newsock->sockAddr = addr;
newsock->sockAddrLen = addrlen;
|