Skip to content

Commit e7df0cc

Browse files
committed
PR feedback
1 parent 6f64c86 commit e7df0cc

File tree

2 files changed

+41
-83
lines changed

2 files changed

+41
-83
lines changed

src/libraries/Native/Unix/System.Native/pal_networking.c

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ static int32_t GetHostEntries(const uint8_t* address, struct addrinfo* info, Hos
344344
}
345345
}
346346
#else
347-
// Actually not needed, but to use the parameter in case of HAVE_GETIFADDRS expands to 0.
348-
address = NULL;
347+
(void)address;
349348
#endif
350349

351350
if (entry->IPAddressCount > 0)
@@ -425,7 +424,6 @@ static int32_t GetHostEntries(const uint8_t* address, struct addrinfo* info, Hos
425424
#if HAVE_GETADDRINFO_A
426425
struct GetAddrInfoAsyncState
427426
{
428-
struct addrinfo hint;
429427
struct gaicb gai_request;
430428
struct gaicb* gai_requests;
431429
struct sigevent sigevent;
@@ -443,59 +441,43 @@ static void GetHostEntryForNameAsyncComplete(sigval_t context)
443441

444442
GetHostEntryForNameCallback callback = state->callback;
445443

446-
int result = gai_error(&state->gai_request);
447-
HostEntry* entry = state->entry;
444+
int ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal(gai_error(&state->gai_request));
448445

449-
int ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
450-
451-
if (result == 0)
446+
if (ret == 0)
452447
{
453448
const uint8_t* address = state->address;
454449
struct addrinfo* info = state->gai_request.ar_result;
455450

456-
ret = GetHostEntries(address, info, entry);
451+
ret = GetHostEntries(address, info, state->entry);
457452
}
458453

459-
free(state);
460-
461454
if (callback != NULL)
462455
{
463-
callback(entry, ret);
456+
callback(state->entry, ret);
464457
}
465-
}
466-
#endif
467458

468-
static void GetHostEntryForNameCreateHint(struct addrinfo* hint)
469-
{
470-
// Get all address families and the canonical name
471-
472-
memset(hint, 0, sizeof(struct addrinfo));
473-
hint->ai_family = AF_UNSPEC;
474-
hint->ai_flags = AI_CANONNAME;
459+
free(state);
475460
}
461+
#endif
476462

477463
int32_t SystemNative_PlatformSupportsGetAddrInfoAsync()
478464
{
479-
#if HAVE_GETADDRINFO_A
480-
return 1;
481-
#else
482-
return 0;
483-
#endif
465+
return HAVE_GETADDRINFO_A;
484466
}
485467

468+
// Get all address families and the canonical name
469+
static const struct addrinfo s_hostEntryForNameHints = {.ai_family = AF_UNSPEC, .ai_flags = AI_CANONNAME};
470+
486471
int32_t SystemNative_GetHostEntryForName(const uint8_t* address, HostEntry* entry)
487472
{
488473
if (address == NULL || entry == NULL)
489474
{
490475
return GetAddrInfoErrorFlags_EAI_BADARG;
491476
}
492477

493-
struct addrinfo hint;
494-
GetHostEntryForNameCreateHint(&hint);
495-
496478
struct addrinfo* info = NULL;
497479

498-
int result = getaddrinfo((const char*)address, NULL, &hint, &info);
480+
int result = getaddrinfo((const char*)address, NULL, &s_hostEntryForNameHints, &info);
499481
if (result != 0)
500482
{
501483
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
@@ -512,22 +494,32 @@ int32_t SystemNative_GetHostEntryForNameAsync(const uint8_t* address, HostEntry*
512494
return GetAddrInfoErrorFlags_EAI_BADARG;
513495
}
514496

515-
struct GetAddrInfoAsyncState* state = malloc(sizeof(struct GetAddrInfoAsyncState));
516-
517-
GetHostEntryForNameCreateHint(&state->hint);
518-
state->gai_request.ar_name = (const char*)address;
519-
state->gai_request.ar_service = NULL;
520-
state->gai_request.ar_request = &state->hint;
521-
state->gai_request.ar_result = NULL;
522-
state->gai_requests = &state->gai_request;
497+
struct GetAddrInfoAsyncState* state = malloc(sizeof(*state));
523498

524-
state->sigevent.sigev_notify = SIGEV_THREAD;
525-
state->sigevent.sigev_value.sival_ptr = state;
526-
state->sigevent.sigev_notify_function = GetHostEntryForNameAsyncComplete;
499+
if (state == NULL)
500+
{
501+
return GetAddrInfoErrorFlags_EAI_MEMORY;
502+
}
527503

528-
state->address = address;
529-
state->entry = entry;
530-
state->callback = callback;
504+
*state = (struct GetAddrInfoAsyncState){
505+
.gai_request = {
506+
.ar_name = (const char*)address,
507+
.ar_service = NULL,
508+
.ar_request = &s_hostEntryForNameHints,
509+
.ar_result = NULL
510+
},
511+
.gai_requests = &state->gai_request,
512+
.sigevent = {
513+
.sigev_notify = SIGEV_THREAD,
514+
.sigev_value = {
515+
.sival_ptr = state
516+
},
517+
.sigev_notify_function = GetHostEntryForNameAsyncComplete
518+
},
519+
.address = address,
520+
.entry = entry,
521+
.callback = callback
522+
};
531523

532524
atomic_thread_fence(memory_order_release);
533525

@@ -541,11 +533,9 @@ int32_t SystemNative_GetHostEntryForNameAsync(const uint8_t* address, HostEntry*
541533

542534
return result;
543535
#else
544-
// Actually not needed, but otherwise the parameters are unused.
545-
if (address != NULL && entry != NULL && callback != NULL)
546-
{
547-
return -1;
548-
}
536+
(void)address;
537+
(void)entry;
538+
(void)callback;
549539

550540
// GetHostEntryForNameAsync is not supported on this platform.
551541
return -1;

src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,11 @@ namespace System.Net
1414
{
1515
internal static partial class NameResolutionPal
1616
{
17-
private static volatile bool s_initialized;
18-
private static readonly object s_initializedLock = new object();
19-
2017
private static readonly unsafe Interop.Sys.GetHostEntryForNameCallback s_getHostEntryForNameCallback = GetHostEntryForNameCallback;
21-
private static bool s_platformSupportsGetAddrInfoAsync;
22-
23-
public static void EnsureSocketsAreInitialized()
24-
{
25-
// Actually a No-op for Unix as sockets don't need a startup, but used
26-
// to check whether async address resolution is available or not.
2718

28-
if (!s_initialized)
29-
{
30-
Initialize();
31-
}
19+
public static bool SupportsGetAddrInfoAsync { get; } = Interop.Sys.PlatformSupportsGetAddrInfoAsync();
3220

33-
static void Initialize()
34-
{
35-
lock (s_initializedLock)
36-
{
37-
if (!s_initialized)
38-
{
39-
s_platformSupportsGetAddrInfoAsync = Interop.Sys.PlatformSupportsGetAddrInfoAsync();
40-
s_initialized = true;
41-
}
42-
}
43-
}
44-
}
45-
46-
public static bool SupportsGetAddrInfoAsync
47-
{
48-
get
49-
{
50-
EnsureSocketsAreInitialized();
51-
return s_platformSupportsGetAddrInfoAsync;
52-
}
53-
}
21+
public static void EnsureSocketsAreInitialized() { } // No-op for Unix
5422

5523
public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, out string? hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode)
5624
{

0 commit comments

Comments
 (0)