@@ -344,8 +344,7 @@ static int32_t GetHostEntries(const uint8_t* address, struct addrinfo* info, Hos
344
344
}
345
345
}
346
346
#else
347
- // Actually not needed, but to use the parameter in case of HAVE_GETIFADDRS expands to 0.
348
- address = NULL ;
347
+ (void )address ;
349
348
#endif
350
349
351
350
if (entry -> IPAddressCount > 0 )
@@ -425,7 +424,6 @@ static int32_t GetHostEntries(const uint8_t* address, struct addrinfo* info, Hos
425
424
#if HAVE_GETADDRINFO_A
426
425
struct GetAddrInfoAsyncState
427
426
{
428
- struct addrinfo hint ;
429
427
struct gaicb gai_request ;
430
428
struct gaicb * gai_requests ;
431
429
struct sigevent sigevent ;
@@ -443,59 +441,43 @@ static void GetHostEntryForNameAsyncComplete(sigval_t context)
443
441
444
442
GetHostEntryForNameCallback callback = state -> callback ;
445
443
446
- int result = gai_error (& state -> gai_request );
447
- HostEntry * entry = state -> entry ;
444
+ int ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal (gai_error (& state -> gai_request ));
448
445
449
- int ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal (result );
450
-
451
- if (result == 0 )
446
+ if (ret == 0 )
452
447
{
453
448
const uint8_t * address = state -> address ;
454
449
struct addrinfo * info = state -> gai_request .ar_result ;
455
450
456
- ret = GetHostEntries (address , info , entry );
451
+ ret = GetHostEntries (address , info , state -> entry );
457
452
}
458
453
459
- free (state );
460
-
461
454
if (callback != NULL )
462
455
{
463
- callback (entry , ret );
456
+ callback (state -> entry , ret );
464
457
}
465
- }
466
- #endif
467
458
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 );
475
460
}
461
+ #endif
476
462
477
463
int32_t SystemNative_PlatformSupportsGetAddrInfoAsync ()
478
464
{
479
- #if HAVE_GETADDRINFO_A
480
- return 1 ;
481
- #else
482
- return 0 ;
483
- #endif
465
+ return HAVE_GETADDRINFO_A ;
484
466
}
485
467
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
+
486
471
int32_t SystemNative_GetHostEntryForName (const uint8_t * address , HostEntry * entry )
487
472
{
488
473
if (address == NULL || entry == NULL )
489
474
{
490
475
return GetAddrInfoErrorFlags_EAI_BADARG ;
491
476
}
492
477
493
- struct addrinfo hint ;
494
- GetHostEntryForNameCreateHint (& hint );
495
-
496
478
struct addrinfo * info = NULL ;
497
479
498
- int result = getaddrinfo ((const char * )address , NULL , & hint , & info );
480
+ int result = getaddrinfo ((const char * )address , NULL , & s_hostEntryForNameHints , & info );
499
481
if (result != 0 )
500
482
{
501
483
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal (result );
@@ -512,22 +494,32 @@ int32_t SystemNative_GetHostEntryForNameAsync(const uint8_t* address, HostEntry*
512
494
return GetAddrInfoErrorFlags_EAI_BADARG ;
513
495
}
514
496
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 ));
523
498
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
+ }
527
503
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
+ };
531
523
532
524
atomic_thread_fence (memory_order_release );
533
525
@@ -541,11 +533,9 @@ int32_t SystemNative_GetHostEntryForNameAsync(const uint8_t* address, HostEntry*
541
533
542
534
return result ;
543
535
#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 ;
549
539
550
540
// GetHostEntryForNameAsync is not supported on this platform.
551
541
return -1 ;
0 commit comments