libfetch 0.0.0
A lightweight asynchronous HTTP/1.1 client library implementing a subset of the WHATWG Fetch API.
Loading...
Searching...
No Matches
dns.c File Reference

Cross-platform asynchronous DNS resolver implementation. More...

#include "dns.h"
#include <errno.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>

Data Structures

struct  dns_request
 Internal DNS request structure. More...
 
struct  dns_resolver
 Internal DNS resolver structure. More...
 

Macros

#define PLATFORM_GENERIC
 

Functions

static dns_result_tconvert_generic_result (struct addrinfo *ai, const char *hostname, int error)
 
static bool platform_init (dns_resolver_t *resolver)
 
static void platform_cleanup (dns_resolver_t *resolver)
 
static dns_request_tplatform_resolve_async (dns_request_t *request)
 
static void platform_cancel_request (dns_request_t *request)
 
static void platform_process_events (dns_resolver_t *resolver)
 
dns_config_t dns_config_default (void)
 Get default configuration.
 
dns_resolver_tdns_resolver_create (const dns_config_t *config)
 Create a new DNS resolver instance.
 
void dns_resolver_destroy (dns_resolver_t *resolver)
 Destroy a DNS resolver instance.
 
dns_request_tdns_resolve_async (dns_resolver_t *resolver, const char *hostname, const char *service, dns_callback_t callback, void *user_data)
 Start asynchronous DNS resolution.
 
bool dns_request_cancel (dns_request_t *request)
 Cancel a pending DNS request.
 
void dns_resolver_process (dns_resolver_t *resolver)
 Process pending DNS operations.
 
void dns_result_free (dns_result_t *result)
 Free DNS result structure.
 
const char * dns_error_string (int error_code)
 Get human-readable error message.
 

Detailed Description

Cross-platform asynchronous DNS resolver implementation.

This file implements a cross-platform asynchronous DNS resolver that uses platform-specific APIs for optimal performance:

  • Windows: GetAddrInfoExW with OVERLAPPED I/O
  • Linux: getaddrinfo_a with signalfd and atomic request management
  • macOS: libinfo.dylib async functions with Mach ports
  • Generic: Fallback synchronous implementation

Macro Definition Documentation

◆ PLATFORM_GENERIC

#define PLATFORM_GENERIC

Function Documentation

◆ convert_generic_result()

static dns_result_t * convert_generic_result ( struct addrinfo * ai,
const char * hostname,
int error )
static

◆ dns_config_default()

dns_config_t dns_config_default ( void )

Get default configuration.

Returns a configuration structure initialized with default values. This can be used as a starting point for custom configurations.

Returns
Default configuration structure
See also
dns_resolver_create()

◆ dns_error_string()

const char * dns_error_string ( int error_code)

Get human-readable error message.

Returns a string description of the specified error code.

Parameters
error_codeError code from dns_result_t
Returns
Pointer to error message string (never NULL)
Note
The returned string is statically allocated and should not be freed

◆ dns_request_cancel()

bool dns_request_cancel ( dns_request_t * request)

Cancel a pending DNS request.

Attempts to cancel a pending DNS resolution request. If successful, the callback will not be called for this request.

Parameters
requestPointer to request handle
Returns
true if the request was successfully cancelled, false otherwise
Note
This function may fail if the request has already completed
Warning
Do not use the request pointer after calling this function

◆ dns_resolve_async()

dns_request_t * dns_resolve_async ( dns_resolver_t * resolver,
const char * hostname,
const char * service,
dns_callback_t callback,
void * user_data )

Start asynchronous DNS resolution.

Initiates an asynchronous DNS resolution for the specified hostname. The callback function will be called when the resolution completes.

Parameters
resolverDNS resolver instance
hostnameHostname to resolve (null-terminated string)
serviceService name or port number (may be NULL)
callbackCallback function to call when resolution completes
user_dataUser data to pass to the callback function
Returns
Pointer to request handle, or NULL on failure
Note
The returned request handle can be used to cancel the request
See also
dns_request_cancel()
dns_callback_t

◆ dns_resolver_create()

dns_resolver_t * dns_resolver_create ( const dns_config_t * config)

Create a new DNS resolver instance.

Creates and initializes a new DNS resolver with the specified configuration. The resolver must be destroyed with dns_resolver_destroy() when no longer needed.

Parameters
configPointer to configuration structure (NULL for default config)
Returns
Pointer to new resolver instance, or NULL on failure
See also
dns_resolver_destroy()
dns_config_default()

◆ dns_resolver_destroy()

void dns_resolver_destroy ( dns_resolver_t * resolver)

Destroy a DNS resolver instance.

Destroys the resolver and frees all associated resources. Any pending requests will be cancelled automatically.

Parameters
resolverPointer to resolver instance (may be NULL)
Warning
Do not use the resolver pointer after calling this function
See also
dns_resolver_create()

◆ dns_resolver_process()

void dns_resolver_process ( dns_resolver_t * resolver)

Process pending DNS operations.

This function must be called regularly (typically in your main event loop) to process completed DNS operations and invoke callbacks. It is non-blocking and will return immediately if no operations are ready.

Parameters
resolverDNS resolver instance
Note
This function should be called frequently for timely callback execution
Thread-safe: can be called from multiple threads

◆ dns_result_free()

void dns_result_free ( dns_result_t * result)

Free DNS result structure.

Frees all memory associated with a DNS result structure, including the addresses array and hostname string.

Parameters
resultPointer to result structure (may be NULL)
Note
This function must be called for each result passed to callbacks
Warning
Do not use the result pointer after calling this function

◆ platform_cancel_request()

static void platform_cancel_request ( dns_request_t * request)
static

◆ platform_cleanup()

static void platform_cleanup ( dns_resolver_t * resolver)
static

◆ platform_init()

static bool platform_init ( dns_resolver_t * resolver)
static

◆ platform_process_events()

static void platform_process_events ( dns_resolver_t * resolver)
static

◆ platform_resolve_async()

static dns_request_t * platform_resolve_async ( dns_request_t * request)
static