83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
{
if (sock != INVALID_SOCKET)
@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)
|
|
|
|
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
{
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa
socket: self];
#ifdef HAVE_THREADSAFE_GETADDRINFO
struct addrinfo hints, *res, *res0;
char portCString[7];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portCString, 7, "%" PRIu16, port);
if (getaddrinfo([host cString], portCString, &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)
|
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
if (sock != INVALID_SOCKET)
@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
|
|
|
|
|
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa
socket: self];
#ifdef HAVE_THREADSAFE_GETADDRINFO
struct addrinfo hints, *res;
char portCString[7];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portCString, 7, "%" PRIu16, port);
if (getaddrinfo([host cString], portCString, &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
|
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
backLog: 5];
isListening = YES;
}
- (OFTCPSocket*)accept
{
OFTCPSocket *newsock;
struct sockaddr *addr;
socklen_t addrlen;
int s;
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
socket: self];
}
newsock->sock = s;
newsock->sockAddr = addr;
newsock->sockAddrLen = addrlen;
return newsock;
}
- (void)setKeepAlivesEnabled: (BOOL)enable
{
int v = enable;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
@throw [OFSetOptionFailedException newWithClass: isa
stream: self];
}
- (OFString*)remoteAddress
{
if (sockAddr == NULL || sockAddrLen == 0)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
#ifdef HAVE_THREADSAFE_GETADDRINFO
char *host = [self allocMemoryWithSize: NI_MAXHOST];
@try {
if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL,
0, NI_NUMERICHOST))
@throw [OFAddressTranslationFailedException
newWithClass: isa];
return [OFString stringWithCString: host];
} @finally {
[self freeMemory: host];
}
#else
char *host;
# ifdef OF_THREADS
[mutex lock];
@try {
# endif
host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
<
<
|
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
backLog: 5];
isListening = YES;
}
- (OFTCPSocket*)accept
{
OFTCPSocket *newSocket;
struct sockaddr *addr;
socklen_t addrLen;
int newSock;
newSocket = [[[isa alloc] init] autorelease];
addrLen = sizeof(struct sockaddr);
@try {
addr = [newSocket allocMemoryWithSize: sizeof(struct sockaddr)];
} @catch (id e) {
[newSocket release];
@throw e;
}
if ((newSock = accept(sock, addr, &addrLen)) == INVALID_SOCKET) {
[newSocket release];
@throw [OFAcceptFailedException newWithClass: isa
socket: self];
}
newSocket->sock = newSock;
newSocket->sockAddr = addr;
newSocket->sockAddrLen = addrLen;
return newSocket;
}
- (void)setKeepAlivesEnabled: (BOOL)enable
{
int v = enable;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
@throw [OFSetOptionFailedException newWithClass: isa
stream: self];
}
- (OFString*)remoteAddress
{
char *host;
if (sockAddr == NULL || sockAddrLen == 0)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
#ifdef HAVE_THREADSAFE_GETADDRINFO
host = [self allocMemoryWithSize: NI_MAXHOST];
@try {
if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL,
0, NI_NUMERICHOST))
@throw [OFAddressTranslationFailedException
newWithClass: isa];
return [OFString stringWithCString: host];
} @finally {
[self freeMemory: host];
}
#else
# ifdef OF_THREADS
[mutex lock];
@try {
# endif
host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);
|