Differences From Artifact [fd8f9dff29]:
- File src/OFUDPSocket.m — part of check-in [72db6b3c6f] at 2022-10-22 16:00:37 on branch trunk — Split OFBindSocketFailedException into subclasses (user: js, size: 5099) [annotate] [blame] [check-ins using] [more...]
To Artifact [f8c53776df]:
- File
src/OFUDPSocket.m
— part of check-in
[a54730b88f]
at
2022-10-22 18:41:45
on branch trunk
— OFSocket: Don't combine port for IP and IPX
While it works for those two protocols, it's a bad precedent as other
protocols have different types for ports. (user: js, size: 5117) [annotate] [blame] [check-ins using] [more...]
| ︙ | ︙ | |||
50 51 52 53 54 55 56 | #endif if ((_socket = socket( ((struct sockaddr *)&address->sockaddr)->sa_family, SOCK_DGRAM | SOCK_CLOEXEC | extraType, 0)) == OFInvalidSocketHandle) @throw [OFBindIPSocketFailedException exceptionWithHost: OFSocketAddressString(address) | | | | | 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 |
#endif
if ((_socket = socket(
((struct sockaddr *)&address->sockaddr)->sa_family,
SOCK_DGRAM | SOCK_CLOEXEC | extraType, 0)) == OFInvalidSocketHandle)
@throw [OFBindIPSocketFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressIPPort(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 (OFSocketAddressIPPort(address) != 0) {
#endif
if (bind(_socket, (struct sockaddr *)&address->sockaddr,
address->length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressIPPort(address)
socket: self
errNo: errNo];
}
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
|
| ︙ | ︙ | |||
98 99 100 101 102 103 104 |
(struct sockaddr *)&address->sockaddr,
address->length)) == 0)
break;
if (OFSocketErrNo() != EADDRINUSE) {
int errNo = OFSocketErrNo();
OFString *host = OFSocketAddressString(address);
| | | | | | | | 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 |
(struct sockaddr *)&address->sockaddr,
address->length)) == 0)
break;
if (OFSocketErrNo() != EADDRINUSE) {
int errNo = OFSocketErrNo();
OFString *host = OFSocketAddressString(address);
port = OFSocketAddressIPPort(address);
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
#endif
objc_autoreleasePoolPop(pool);
if ((port = OFSocketAddressIPPort(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, (struct sockaddr *)&address->sockaddr,
&address->length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressIPPort(address)
socket: self
errNo: errNo];
}
switch (((struct sockaddr *)&address->sockaddr)->sa_family) {
case AF_INET:
return OFFromBigEndian16(address->sockaddr.in.sin_port);
# ifdef OF_HAVE_IPV6
case AF_INET6:
return OFFromBigEndian16(address->sockaddr.in6.sin6_port);
# endif
default:
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressIPPort(address)
socket: self
errNo: EAFNOSUPPORT];
}
#else
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: OFSocketAddressString(address)
port: OFSocketAddressIPPort(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];
OFSocketAddressSetIPPort(&address, port);
port = [self of_bindToAddress: &address extraType: 0];
objc_autoreleasePoolPop(pool);
return port;
}
@end
|