43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
{
if (sock != INVALID_SOCKET)
close(sock);
[super dealloc];
}
- connectTo: (const char*)host
onPort: (uint16_t)port
{
struct addrinfo hints, *res, *res0;
char portstr[6];
if (!port)
@throw [OFInvalidPortException newWithClass: isa];
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, 6, "%d", port);
if (getaddrinfo(host, portstr, &hints, &res0))
@throw [OFAddressTranslationFailedException
newWithClass: isa
andNode: host
andService: portstr];
for (res = res0; res != NULL; res = res->ai_next) {
if ((sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) == INVALID_SOCKET)
continue;
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
close(sock);
sock = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(res0);
if (sock == INVALID_SOCKET)
@throw [OFConnectionFailedException newWithClass: isa
andHost: host
andPort: port];
return self;
}
- bindOn: (const char*)host
withPort: (uint16_t)port
andFamily: (int)family
{
struct addrinfo hints, *res;
char portstr[6];
if (!port)
@throw [OFInvalidPortException newWithClass: isa];
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithClass: isa
andHost: host
andPort: port
andFamily: family];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, 6, "%d", port);
if (getaddrinfo(host, portstr, &hints, &res))
@throw [OFAddressTranslationFailedException
newWithClass: isa
andNode: host
andService: portstr];
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
@throw [OFBindFailedException newWithClass: isa
andHost: host
andPort: port
andFamily: family];
}
freeaddrinfo(res);
return self;
}
|
|
|
<
<
<
<
<
|
<
|
|
|
|
|
|
|
<
<
<
<
|
|
<
|
<
|
|
|
|
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
{
if (sock != INVALID_SOCKET)
close(sock);
[super dealloc];
}
- connectToService: (OFString*)service
onNode: (OFString*)node
{
struct addrinfo hints, *res, *res0;
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo([node cString], [service cString], &hints, &res0))
@throw [OFAddressTranslationFailedException
newWithClass: isa
andNode: node
andService: service];
for (res = res0; res != NULL; res = res->ai_next) {
if ((sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) == INVALID_SOCKET)
continue;
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
close(sock);
sock = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(res0);
if (sock == INVALID_SOCKET)
@throw [OFConnectionFailedException newWithClass: isa
andNode: node
andService: service];
return self;
}
- bindService: (OFString*)service
onNode: (OFString*)node
withFamily: (int)family
{
struct addrinfo hints, *res;
if (sock != INVALID_SOCKET)
@throw [OFAlreadyConnectedException newWithClass: isa];
if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithClass: isa
andNode: node
andService: service
andFamily: family];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo([node cString], [service cString], &hints, &res))
@throw [OFAddressTranslationFailedException
newWithClass: isa
andNode: node
andService: service];
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
@throw [OFBindFailedException newWithClass: isa
andNode: node
andService: service
andFamily: family];
}
freeaddrinfo(res);
return self;
}
|