Differences From Artifact [f6383b3c0c]:
- File
src/resolver.m
— part of check-in
[fae85e954f]
at
2014-02-27 22:40:33
on branch trunk
— Allow thread-unsafe getaddrinfo() with locks
Before, getaddrinfo() would not be used at all if it is not thread-safe. (user: js, size: 7805) [annotate] [blame] [check-ins using]
To Artifact [d1d2c810b6]:
- File
src/resolver.m
— part of check-in
[898f8c7072]
at
2014-03-04 23:53:48
on branch trunk
— resolver.m: Fix locking
A lock for thread-unsafe getaddrinfo was missing and not all locks were
unlocked / all buffers free'd if an error occurred. (user: js, size: 8392) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
67 68 69 70 71 72 73 74 | hints.ai_socktype = type; hints.ai_flags = AI_NUMERICSERV; snprintf(portCString, 7, "%" PRIu16, port); # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; # endif | > > < | | | | | | | | | | | | > | | > | > | > > | | > | > | | | | | | | | | | | < > | | > | > | > | > > > < > > | | < < < < < | | < | | | | | | | | | > | > | | > | > | > > | | | > | | | | | | | | | | | | | | | < > | | > | > > > > > > | | | | | | | | | | > | | | > | | > > > > > > < > > | | | | | | | | < > | | > | | 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 195 196 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 254 255 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | hints.ai_socktype = type; hints.ai_flags = AI_NUMERICSERV; snprintf(portCString, 7, "%" PRIu16, port); # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @try { # endif if (getaddrinfo([host UTF8String], portCString, &hints, &res0)) @throw [OFAddressTranslationFailedException exceptionWithHost: host]; count = 0; for (res = res0; res != NULL; res = res->ai_next) count++; if (count == 0) { freeaddrinfo(res0); @throw [OFAddressTranslationFailedException exceptionWithHost: host]; } if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) { freeaddrinfo(res0); @throw [OFOutOfMemoryException exceptionWithRequestedSize: (count + 1) * sizeof(*ret)]; } if ((results = malloc(count * sizeof(*results))) == NULL) { freeaddrinfo(res0); free(ret); @throw [OFOutOfMemoryException exceptionWithRequestedSize: count * sizeof(*results)]; } for (retIter = ret, resultsIter = results, res = res0; res != NULL; retIter++, resultsIter++, res = res->ai_next) { resultsIter->family = res->ai_family; resultsIter->type = res->ai_socktype; resultsIter->protocol = res->ai_protocol; resultsIter->address = res->ai_addr; resultsIter->addressLength = res->ai_addrlen; *retIter = resultsIter; } *retIter = NULL; ret[0]->private_ = res0; # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) } @finally { if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif #else struct hostent *he; in_addr_t s_addr; char **ip; struct sockaddr_in *addrs, *addrsIter; /* * If the host is an IP address, don't try resolving it. * On the Wii for example, the resolver will return an error if you * specify an IP address. */ if ((s_addr = inet_addr([host UTF8String])) != INADDR_NONE) { of_resolver_result_t *tmp; struct sockaddr_in *addr; if ((ret = calloc(2, sizeof(*ret))) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: 2 * sizeof(*ret)]; if ((tmp = malloc(sizeof(*tmp))) == NULL) { free(ret); @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*tmp)]; } if ((addr = calloc(1, sizeof(*addr))) == NULL) { free(ret); free(tmp); @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*addr)]; } addr->sin_family = AF_INET; addr->sin_port = OF_BSWAP16_IF_LE(port); addr->sin_addr.s_addr = s_addr; tmp->family = AF_INET; tmp->type = type; tmp->protocol = 0; tmp->address = (struct sockaddr*)addr; tmp->addressLength = sizeof(*addr); ret[0] = tmp; ret[1] = NULL; return ret; } # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @try { # endif if ((he = gethostbyname([host UTF8String])) == NULL || he->h_addrtype != AF_INET) @throw [OFAddressTranslationFailedException exceptionWithHost: host]; count = 0; for (ip = he->h_addr_list; *ip != NULL; ip++) count++; if (count == 0) @throw [OFAddressTranslationFailedException exceptionWithHost: host]; if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: (count + 1) * sizeof(*ret)]; if ((results = malloc(count * sizeof(*results))) == NULL) { free(ret); @throw [OFOutOfMemoryException exceptionWithRequestedSize: count * sizeof(*results)]; } if ((addrs = calloc(count, sizeof(*addrs))) == NULL) { free(ret); free(results); @throw [OFOutOfMemoryException exceptionWithRequestedSize: count * sizeof(*addrs)]; } for (retIter = ret, resultsIter = results, addrsIter = addrs, ip = he->h_addr_list; *ip != NULL; retIter++, resultsIter++, addrsIter++, ip++) { addrsIter->sin_family = he->h_addrtype; addrsIter->sin_port = OF_BSWAP16_IF_LE(port); if (he->h_length > sizeof(addrsIter->sin_addr.s_addr)) @throw [OFOutOfRangeException exception]; memcpy(&addrsIter->sin_addr.s_addr, *ip, he->h_length); resultsIter->family = he->h_addrtype; resultsIter->type = type; resultsIter->protocol = 0; resultsIter->address = (struct sockaddr*)addrsIter; resultsIter->addressLength = sizeof(*addrsIter); *retIter = resultsIter; } # ifdef OF_HAVE_THREADS } @finally { if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif #endif return ret; } void of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength, OFString *__autoreleasing *host, uint16_t *port) { #ifdef HAVE_GETADDRINFO char hostCString[NI_MAXHOST]; char portCString[NI_MAXSERV]; # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @try { # endif /* FIXME: Add NI_DGRAM for UDP? */ if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST, portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)) @throw [OFAddressTranslationFailedException exception]; if (host != NULL) *host = [OFString stringWithUTF8String: hostCString]; if (port != NULL) { char *endptr; long tmp; if ((tmp = strtol(portCString, &endptr, 10)) > UINT16_MAX) @throw [OFOutOfRangeException exception]; if (endptr != NULL && *endptr != '\0') @throw [OFAddressTranslationFailedException exception]; *port = (uint16_t)tmp; } # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) } @finally { if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif #else char *hostCString; if (address->sa_family != AF_INET) @throw [OFInvalidArgumentException exception]; # if OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @try { # endif if ((hostCString = inet_ntoa( ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL) @throw [OFAddressTranslationFailedException exception]; if (host != NULL) *host = [OFString stringWithUTF8String: hostCString]; if (port != NULL) *port = OF_BSWAP16_IF_LE( ((struct sockaddr_in*)(void*)address)->sin_port); # if OF_HAVE_THREADS } @finally { if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif #endif } void of_resolver_free(of_resolver_result_t **results) { #ifdef HAVE_GETADDRINFO freeaddrinfo(results[0]->private_); #else free(results[0]->address); #endif free(results[0]); free(results); } |