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
|
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
|
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
# endif
#endif
return ret;
}
OFString*
of_address_to_string(struct sockaddr *address, socklen_t addressLength)
of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength,
uint16_t *port)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
char host[NI_MAXHOST];
char portCString[NI_MAXSERV];
/* FIXME: Add NI_DGRAM for UDP? */
if (getnameinfo(address, addressLength, host, NI_MAXHOST, NULL, 0,
NI_NUMERICHOST | NI_NUMERICSERV))
if (getnameinfo(address, addressLength, host, NI_MAXHOST,
portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))
@throw [OFAddressTranslationFailedException exception];
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;
}
return [OFString stringWithUTF8String: host];
#else
OFString *ret;
char *host;
if (address->sa_family != AF_INET)
@throw [OFInvalidArgumentException exception];
# if OF_HAVE_THREADS
if (!of_mutex_lock(&mutex))
@throw [OFLockFailedException exception];
# endif
host = inet_ntoa(((struct sockaddr_in*)(void*)address)->sin_addr);
if (host == NULL)
@throw [OFAddressTranslationFailedException exception];
ret = [OFString stringWithUTF8String: host];
if (port != NULL)
*port = OF_BSWAP16_IF_LE(
((struct sockaddr_in*)(void*)address)->sin_port);
# if OF_HAVE_THREADS
if (!of_mutex_unlock(&mutex))
@throw [OFUnlockFailedException exception];
# endif
return ret;
#endif
|