11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <errno.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#import "OFUDPSocket.h"
#import "OFUDPSocket+Private.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFThread.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindFailedException.h"
#import "socket.h"
#import "socket_helpers.h"
@implementation OFUDPSocket
@dynamic delegate;
- (uint16_t)of_bindToAddress: (of_socket_address_t *)address
extraType: (int)extraType OF_DIRECT
{
void *pool = objc_autoreleasePoolPush();
OFString *host;
uint16_t port;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
int flags;
#endif
if ((_socket = socket(address->sockaddr.sockaddr.sa_family,
SOCK_DGRAM | SOCK_CLOEXEC | extraType, 0)) == INVALID_SOCKET) {
host = of_socket_address_ip_string(address, &port);
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: of_socket_errno()];
}
_canBlock = true;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
/* {} needed to avoid warning with Clang 10 if next #if is false. */
if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) {
fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
}
#endif
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
if (of_socket_address_get_port(address) != 0) {
#endif
if (bind(_socket, &address->sockaddr.sockaddr,
address->length) != 0) {
int errNo = of_socket_errno();
closesocket(_socket);
_socket = INVALID_SOCKET;
host = of_socket_address_ip_string(address, &port);
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
int ret;
while (rnd < 1024)
rnd = (uint16_t)rand();
of_socket_address_set_port(address, rnd);
if ((ret = bind(_socket, &address->sockaddr.sockaddr,
address->length)) == 0) {
port = rnd;
break;
}
if (of_socket_errno() != EADDRINUSE) {
int errNo = of_socket_errno();
closesocket(_socket);
_socket = INVALID_SOCKET;
host = of_socket_address_ip_string(
address, &port);
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
#endif
objc_autoreleasePoolPop(pool);
if ((port = of_socket_address_get_port(address)) > 0)
return port;
#if !defined(OF_HPUX) && !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
memset(address, 0, sizeof(*address));
address->length = (socklen_t)sizeof(address->sockaddr);
if (of_getsockname(_socket, &address->sockaddr.sockaddr,
&address->length) != 0) {
int errNo = of_socket_errno();
closesocket(_socket);
_socket = INVALID_SOCKET;
host = of_socket_address_ip_string(address, &port);
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
if (address->sockaddr.sockaddr.sa_family == AF_INET)
return OF_BSWAP16_IF_LE(address->sockaddr.in.sin_port);
# ifdef OF_HAVE_IPV6
else if (address->sockaddr.sockaddr.sa_family == AF_INET6)
return OF_BSWAP16_IF_LE(address->sockaddr.in6.sin6_port);
# endif
else {
closesocket(_socket);
_socket = INVALID_SOCKET;
host = of_socket_address_ip_string(address, &port);
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: EAFNOSUPPORT];
}
#else
closesocket(_socket);
_socket = INVALID_SOCKET;
host = of_socket_address_ip_string(address, &port);
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: EADDRNOTAVAIL];
#endif
}
- (uint16_t)bindToHost: (OFString *)host
port: (uint16_t)port
{
void *pool = objc_autoreleasePoolPush();
OFData *socketAddresses;
of_socket_address_t address;
if (_socket != INVALID_SOCKET)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
socketAddresses = [[OFThread DNSResolver]
resolveAddressesForHost: host
addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY];
address = *(of_socket_address_t *)[socketAddresses itemAtIndex: 0];
of_socket_address_set_port(&address, port);
port = [self of_bindToAddress: &address
extraType: 0];
objc_autoreleasePoolPop(pool);
return port;
}
@end
|
>
>
>
>
>
>
>
<
<
<
|
<
|
<
|
|
|
<
|
|
|
<
|
>
|
|
|
|
|
|
>
>
|
<
<
|
|
|
|
<
|
>
|
|
|
|
|
|
<
|
>
|
|
|
|
<
|
>
|
|
|
|
<
|
|
|
|
|
|
<
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#ifndef _XOPEN_SOURCE_EXTENDED
# define _XOPEN_SOURCE_EXTENDED
#endif
#define _HPUX_ALT_XOPEN_SOCKET_API
#include <errno.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#import "OFUDPSocket.h"
#import "OFUDPSocket+Private.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFSocket.h"
#import "OFSocket+Private.h"
#import "OFThread.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindFailedException.h"
@implementation OFUDPSocket
@dynamic delegate;
- (uint16_t)of_bindToAddress: (OFSocketAddress *)address
extraType: (int)extraType OF_DIRECT
{
void *pool = objc_autoreleasePoolPush();
uint16_t port;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
int flags;
#endif
if ((_socket = socket(address->sockaddr.sockaddr.sa_family,
SOCK_DGRAM | SOCK_CLOEXEC | extraType, 0)) == OFInvalidSocketHandle)
@throw [OFBindFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressPort(address)
socket: self
errNo: OFSocketErrNo()];
_canBlock = true;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
/* {} needed to avoid warning with Clang 10 if next #if is false. */
if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) {
fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
}
#endif
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
if (OFSocketAddressPort(address) != 0) {
#endif
if (bind(_socket, &address->sockaddr.sockaddr,
address->length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressPort(address)
socket: self
errNo: errNo];
}
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
int ret;
while (rnd < 1024)
rnd = (uint16_t)rand();
OFSocketAddressSetPort(address, rnd);
if ((ret = bind(_socket, &address->sockaddr.sockaddr,
address->length)) == 0) {
port = rnd;
break;
}
if (OFSocketErrNo() != EADDRINUSE) {
int errNo = OFSocketErrNo();
OFString *host = OFSocketAddressString(address);
uint16_t port = OFSocketAddressPort(port);
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
#endif
objc_autoreleasePoolPop(pool);
if ((port = OFSocketAddressPort(address)) > 0)
return port;
#if !defined(OF_HPUX) && !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
memset(address, 0, sizeof(*address));
address->length = (socklen_t)sizeof(address->sockaddr);
if (OFGetSockName(_socket, &address->sockaddr.sockaddr,
&address->length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressPort(address)
socket: self
errNo: errNo];
}
if (address->sockaddr.sockaddr.sa_family == AF_INET)
return OFFromBigEndian16(address->sockaddr.in.sin_port);
# ifdef OF_HAVE_IPV6
else if (address->sockaddr.sockaddr.sa_family == AF_INET6)
return OFFromBigEndian16(address->sockaddr.in6.sin6_port);
# endif
else {
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressPort(address)
socket: self
errNo: EAFNOSUPPORT];
}
#else
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressPort(address)
socket: self
errNo: EADDRNOTAVAIL];
#endif
}
- (uint16_t)bindToHost: (OFString *)host port: (uint16_t)port
{
void *pool = objc_autoreleasePoolPush();
OFData *socketAddresses;
OFSocketAddress address;
if (_socket != OFInvalidSocketHandle)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
socketAddresses = [[OFThread DNSResolver]
resolveAddressesForHost: host
addressFamily: OFSocketAddressFamilyAny];
address = *(OFSocketAddress *)[socketAddresses itemAtIndex: 0];
OFSocketAddressSetPort(&address, port);
port = [self of_bindToAddress: &address extraType: 0];
objc_autoreleasePoolPop(pool);
return port;
}
@end
|