A C library for asynchronous DNS requests (grpc依赖)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

938 lines
33 KiB

/* MIT License
*
* Copyright (c) Massachusetts Institute of Technology
* Copyright (c) Daniel Stenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* SPDX-License-Identifier: MIT
*/
#ifndef ARES__H
#define ARES__H
#include "ares_version.h" /* c-ares version defines */
#include "ares_build.h" /* c-ares build definitions */
#if defined(_WIN32)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
#endif
#ifdef CARES_HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef CARES_HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef CARES_HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifdef CARES_HAVE_WINSOCK2_H
# include <winsock2.h>
/* To aid with linking against a static c-ares build, lets tell the microsoft
* compiler to pull in needed dependencies */
# ifdef _MSC_VER
# pragma comment(lib, "ws2_32")
# pragma comment(lib, "advapi32")
# pragma comment(lib, "iphlpapi")
# endif
#endif
#ifdef CARES_HAVE_WS2TCPIP_H
# include <ws2tcpip.h>
#endif
#ifdef CARES_HAVE_WINDOWS_H
# include <windows.h>
#endif
/* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
libc5-based Linux systems. Only include it on system that are known to
require it! */
#if defined(_AIX) || defined(__NOVELL_LIBC__) || defined(__NetBSD__) || \
defined(__minix) || defined(__SYMBIAN32__) || defined(__INTEGRITY) || \
defined(ANDROID) || defined(__ANDROID__) || defined(__OpenBSD__) || \
defined(__QNXNTO__) || defined(__MVS__) || defined(__HAIKU__)
# include <sys/select.h>
#endif
#if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
# include <sys/bsdskt.h>
#endif
#if !defined(_WIN32)
# include <netinet/in.h>
#endif
#ifdef WATT32
# include <tcp.h>
#endif
#if defined(ANDROID) || defined(__ANDROID__)
# include <jni.h>
#endif
typedef CARES_TYPEOF_ARES_SOCKLEN_T ares_socklen_t;
typedef CARES_TYPEOF_ARES_SSIZE_T ares_ssize_t;
#ifdef __cplusplus
extern "C" {
#endif
/*
** c-ares external API function linkage decorations.
*/
Autotools: rework to simplify and fix recent issues (#674) Completely rework the autotools build system, issues have cropped up due to the complexity and could cause issues on even semi-modern Linux systems (Ubuntu 20.04 for example). Changes include: Remove all curl/xc/cares m4 helper files, they go overboard on detections of functions and datatypes. Go back to more plain autoconf macros as they've come a long way over the years. Use known systems and heuristics to determine datatypes for functions like send() and recv(), rather than the error prone detection which required thousands of permutations and might still get it wrong. Remove unneeded configure arguments like --enable-debug or --enable-optimize, its more common for people to simply pass their own CFLAGS on the command line. Only require CARES_STATICLIB definition on Windows static builds, its not necessary ever for other systems, even when hiding non-public symbols. Remove some function and definition detections that were never used in c-ares The test framework is now embedded into the toplevel configure system, there was no need to chain build the test system as it is never built externally to c-ares. As a side-effect of the changes, a configure run completes in about 25% of the original time. This has been tested on various Linux distributions (of varying age), FreeBSD, MacOS, Windows (via MSYS2 with Mingw), and Solaris10/11 (by @dfandrich), AIX 7.3 (by @dfandrich). It is not unlikely that this may have broken more esoteric or legacy systems, and we'll likely need to be ready to accept bug reports and patches, but it has removed over 10k lines of build system code. It is very likely any issues that crop up will add far fewer lines of code to fix such systems. Fixes Bug: #670 Fix By: Brad House (@bradh352)
11 months ago
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__SYMBIAN32__)
# ifdef CARES_STATICLIB
# define CARES_EXTERN
# else
Autotools: rework to simplify and fix recent issues (#674) Completely rework the autotools build system, issues have cropped up due to the complexity and could cause issues on even semi-modern Linux systems (Ubuntu 20.04 for example). Changes include: Remove all curl/xc/cares m4 helper files, they go overboard on detections of functions and datatypes. Go back to more plain autoconf macros as they've come a long way over the years. Use known systems and heuristics to determine datatypes for functions like send() and recv(), rather than the error prone detection which required thousands of permutations and might still get it wrong. Remove unneeded configure arguments like --enable-debug or --enable-optimize, its more common for people to simply pass their own CFLAGS on the command line. Only require CARES_STATICLIB definition on Windows static builds, its not necessary ever for other systems, even when hiding non-public symbols. Remove some function and definition detections that were never used in c-ares The test framework is now embedded into the toplevel configure system, there was no need to chain build the test system as it is never built externally to c-ares. As a side-effect of the changes, a configure run completes in about 25% of the original time. This has been tested on various Linux distributions (of varying age), FreeBSD, MacOS, Windows (via MSYS2 with Mingw), and Solaris10/11 (by @dfandrich), AIX 7.3 (by @dfandrich). It is not unlikely that this may have broken more esoteric or legacy systems, and we'll likely need to be ready to accept bug reports and patches, but it has removed over 10k lines of build system code. It is very likely any issues that crop up will add far fewer lines of code to fix such systems. Fixes Bug: #670 Fix By: Brad House (@bradh352)
11 months ago
# ifdef CARES_BUILDING_LIBRARY
# define CARES_EXTERN __declspec(dllexport)
# else
# define CARES_EXTERN __declspec(dllimport)
# endif
# endif
#else
Autotools: rework to simplify and fix recent issues (#674) Completely rework the autotools build system, issues have cropped up due to the complexity and could cause issues on even semi-modern Linux systems (Ubuntu 20.04 for example). Changes include: Remove all curl/xc/cares m4 helper files, they go overboard on detections of functions and datatypes. Go back to more plain autoconf macros as they've come a long way over the years. Use known systems and heuristics to determine datatypes for functions like send() and recv(), rather than the error prone detection which required thousands of permutations and might still get it wrong. Remove unneeded configure arguments like --enable-debug or --enable-optimize, its more common for people to simply pass their own CFLAGS on the command line. Only require CARES_STATICLIB definition on Windows static builds, its not necessary ever for other systems, even when hiding non-public symbols. Remove some function and definition detections that were never used in c-ares The test framework is now embedded into the toplevel configure system, there was no need to chain build the test system as it is never built externally to c-ares. As a side-effect of the changes, a configure run completes in about 25% of the original time. This has been tested on various Linux distributions (of varying age), FreeBSD, MacOS, Windows (via MSYS2 with Mingw), and Solaris10/11 (by @dfandrich), AIX 7.3 (by @dfandrich). It is not unlikely that this may have broken more esoteric or legacy systems, and we'll likely need to be ready to accept bug reports and patches, but it has removed over 10k lines of build system code. It is very likely any issues that crop up will add far fewer lines of code to fix such systems. Fixes Bug: #670 Fix By: Brad House (@bradh352)
11 months ago
# if defined(__GNUC__) && __GNUC__ >= 4
10 months ago
# define CARES_EXTERN __attribute__((visibility("default")))
Autotools: rework to simplify and fix recent issues (#674) Completely rework the autotools build system, issues have cropped up due to the complexity and could cause issues on even semi-modern Linux systems (Ubuntu 20.04 for example). Changes include: Remove all curl/xc/cares m4 helper files, they go overboard on detections of functions and datatypes. Go back to more plain autoconf macros as they've come a long way over the years. Use known systems and heuristics to determine datatypes for functions like send() and recv(), rather than the error prone detection which required thousands of permutations and might still get it wrong. Remove unneeded configure arguments like --enable-debug or --enable-optimize, its more common for people to simply pass their own CFLAGS on the command line. Only require CARES_STATICLIB definition on Windows static builds, its not necessary ever for other systems, even when hiding non-public symbols. Remove some function and definition detections that were never used in c-ares The test framework is now embedded into the toplevel configure system, there was no need to chain build the test system as it is never built externally to c-ares. As a side-effect of the changes, a configure run completes in about 25% of the original time. This has been tested on various Linux distributions (of varying age), FreeBSD, MacOS, Windows (via MSYS2 with Mingw), and Solaris10/11 (by @dfandrich), AIX 7.3 (by @dfandrich). It is not unlikely that this may have broken more esoteric or legacy systems, and we'll likely need to be ready to accept bug reports and patches, but it has removed over 10k lines of build system code. It is very likely any issues that crop up will add far fewer lines of code to fix such systems. Fixes Bug: #670 Fix By: Brad House (@bradh352)
11 months ago
# elif defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 900
10 months ago
# define CARES_EXTERN __attribute__((visibility("default")))
Autotools: rework to simplify and fix recent issues (#674) Completely rework the autotools build system, issues have cropped up due to the complexity and could cause issues on even semi-modern Linux systems (Ubuntu 20.04 for example). Changes include: Remove all curl/xc/cares m4 helper files, they go overboard on detections of functions and datatypes. Go back to more plain autoconf macros as they've come a long way over the years. Use known systems and heuristics to determine datatypes for functions like send() and recv(), rather than the error prone detection which required thousands of permutations and might still get it wrong. Remove unneeded configure arguments like --enable-debug or --enable-optimize, its more common for people to simply pass their own CFLAGS on the command line. Only require CARES_STATICLIB definition on Windows static builds, its not necessary ever for other systems, even when hiding non-public symbols. Remove some function and definition detections that were never used in c-ares The test framework is now embedded into the toplevel configure system, there was no need to chain build the test system as it is never built externally to c-ares. As a side-effect of the changes, a configure run completes in about 25% of the original time. This has been tested on various Linux distributions (of varying age), FreeBSD, MacOS, Windows (via MSYS2 with Mingw), and Solaris10/11 (by @dfandrich), AIX 7.3 (by @dfandrich). It is not unlikely that this may have broken more esoteric or legacy systems, and we'll likely need to be ready to accept bug reports and patches, but it has removed over 10k lines of build system code. It is very likely any issues that crop up will add far fewer lines of code to fix such systems. Fixes Bug: #670 Fix By: Brad House (@bradh352)
11 months ago
# elif defined(__SUNPRO_C)
# define CARES_EXTERN _global
# else
# define CARES_EXTERN
# endif
#endif
#ifdef __GNUC__
8 months ago
# define CARES_GCC_VERSION \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#else
8 months ago
# define CARES_GCC_VERSION 0
#endif
#ifndef __has_attribute
8 months ago
# define __has_attribute(x) 0
#endif
#ifdef CARES_NO_DEPRECATED
8 months ago
# define CARES_DEPRECATED
# define CARES_DEPRECATED_FOR(f)
#else
8 months ago
# if CARES_GCC_VERSION >= 30200 || __has_attribute(__deprecated__)
# define CARES_DEPRECATED __attribute__((__deprecated__))
# else
# define CARES_DEPRECATED
# endif
8 months ago
# if CARES_GCC_VERSION >= 40500 || defined(__clang__)
# define CARES_DEPRECATED_FOR(f) \
__attribute__((deprecated("Use " #f " instead")))
# elif defined(_MSC_VER)
# define CARES_DEPRECATED_FOR(f) __declspec(deprecated("Use " #f " instead"))
# else
# define CARES_DEPRECATED_FOR(f) CARES_DEPRECATED
# endif
#endif
typedef enum {
ARES_SUCCESS = 0,
/* Server error codes (ARES_ENODATA indicates no relevant answer) */
ARES_ENODATA = 1,
ARES_EFORMERR = 2,
ARES_ESERVFAIL = 3,
ARES_ENOTFOUND = 4,
ARES_ENOTIMP = 5,
ARES_EREFUSED = 6,
/* Locally generated error codes */
ARES_EBADQUERY = 7,
ARES_EBADNAME = 8,
ARES_EBADFAMILY = 9,
ARES_EBADRESP = 10,
ARES_ECONNREFUSED = 11,
ARES_ETIMEOUT = 12,
ARES_EOF = 13,
ARES_EFILE = 14,
ARES_ENOMEM = 15,
ARES_EDESTRUCTION = 16,
ARES_EBADSTR = 17,
/* ares_getnameinfo error codes */
ARES_EBADFLAGS = 18,
/* ares_getaddrinfo error codes */
ARES_ENONAME = 19,
ARES_EBADHINTS = 20,
/* Uninitialized library error code */
ARES_ENOTINITIALIZED = 21, /* introduced in 1.7.0 */
/* ares_library_init error codes */
ARES_ELOADIPHLPAPI = 22, /* introduced in 1.7.0 */
ARES_EADDRGETNETWORKPARAMS = 23, /* introduced in 1.7.0 */
/* More error codes */
ARES_ECANCELLED = 24, /* introduced in 1.7.0 */
/* More ares_getaddrinfo error codes */
Add flag to not use a default local named server on channel initialization (#713) Hello, I work on an application for Microsoft which uses c-ares to perform DNS lookups. We have made some minor changes to the library over time, and would like to contribute these back to the project in case they are useful more widely. This PR adds a new channel init flag, described below. Please let me know if I can include any more information to make this PR better/easier for you to review. Thanks! **Summary** When initializing a channel with `ares_init_options()`, if there are no nameservers available (because `ARES_OPT_SERVERS` is not used and `/etc/resolv.conf` is either empty or not available) then a default local named server will be added to the channel. However in some applications a local named server will never be available. In this case, all subsequent queries on the channel will fail. If we know this ahead of time, then it may be preferred to fail channel initialization directly rather than wait for the queries to fail. This gives better visibility, since we know that the failure is due to missing servers rather than something going wrong with the queries. This PR adds a new flag `ARES_FLAG_NO_DFLT_SVR`, to indicate that a default local named server should not be added to a channel in this scenario. Instead, a new error `ARES_EINITNOSERVER` is returned and initialization fails. **Testing** I have added 2 new FV tests: - `ContainerNoDfltSvrEmptyInit` to test that initialization fails when no nameservers are available and the flag is set. - `ContainerNoDfltSvrFullInit` to test that initialization still succeeds when the flag is set but other nameservers are available. Existing FVs are all passing. **Documentation** I have had a go at manually updating the docs to describe the new flag/error, but couldn't see any contributing guidance about testing this. Please let me know if you'd like anything more here. --------- Fix By: Oliver Welsh (@oliverwelsh)
9 months ago
ARES_ESERVICE = 25, /* ares_getaddrinfo() was passed a text service name that
* is not recognized. introduced in 1.16.0 */
ARES_ENOSERVER = 26 /* No DNS servers were configured */
} ares_status_t;
typedef enum {
ARES_FALSE = 0,
ARES_TRUE = 1
} ares_bool_t;
/*! Values for ARES_OPT_EVENT_THREAD */
typedef enum {
/*! Default (best choice) event system */
ARES_EVSYS_DEFAULT = 0,
/*! Win32 IOCP/AFD_POLL event system */
10 months ago
ARES_EVSYS_WIN32 = 1,
/*! Linux epoll */
10 months ago
ARES_EVSYS_EPOLL = 2,
/*! BSD/MacOS kqueue */
10 months ago
ARES_EVSYS_KQUEUE = 3,
/*! POSIX poll() */
10 months ago
ARES_EVSYS_POLL = 4,
/*! last fallback on Unix-like systems, select() */
10 months ago
ARES_EVSYS_SELECT = 5
} ares_evsys_t;
/* Flag values */
#define ARES_FLAG_USEVC (1 << 0)
#define ARES_FLAG_PRIMARY (1 << 1)
#define ARES_FLAG_IGNTC (1 << 2)
#define ARES_FLAG_NORECURSE (1 << 3)
#define ARES_FLAG_STAYOPEN (1 << 4)
#define ARES_FLAG_NOSEARCH (1 << 5)
#define ARES_FLAG_NOALIASES (1 << 6)
#define ARES_FLAG_NOCHECKRESP (1 << 7)
#define ARES_FLAG_EDNS (1 << 8)
Add flag to not use a default local named server on channel initialization (#713) Hello, I work on an application for Microsoft which uses c-ares to perform DNS lookups. We have made some minor changes to the library over time, and would like to contribute these back to the project in case they are useful more widely. This PR adds a new channel init flag, described below. Please let me know if I can include any more information to make this PR better/easier for you to review. Thanks! **Summary** When initializing a channel with `ares_init_options()`, if there are no nameservers available (because `ARES_OPT_SERVERS` is not used and `/etc/resolv.conf` is either empty or not available) then a default local named server will be added to the channel. However in some applications a local named server will never be available. In this case, all subsequent queries on the channel will fail. If we know this ahead of time, then it may be preferred to fail channel initialization directly rather than wait for the queries to fail. This gives better visibility, since we know that the failure is due to missing servers rather than something going wrong with the queries. This PR adds a new flag `ARES_FLAG_NO_DFLT_SVR`, to indicate that a default local named server should not be added to a channel in this scenario. Instead, a new error `ARES_EINITNOSERVER` is returned and initialization fails. **Testing** I have added 2 new FV tests: - `ContainerNoDfltSvrEmptyInit` to test that initialization fails when no nameservers are available and the flag is set. - `ContainerNoDfltSvrFullInit` to test that initialization still succeeds when the flag is set but other nameservers are available. Existing FVs are all passing. **Documentation** I have had a go at manually updating the docs to describe the new flag/error, but couldn't see any contributing guidance about testing this. Please let me know if you'd like anything more here. --------- Fix By: Oliver Welsh (@oliverwelsh)
9 months ago
#define ARES_FLAG_NO_DFLT_SVR (1 << 9)
DNS 0x20 implementation (#800) This PR enables DNS 0x20 as per https://datatracker.ietf.org/doc/html/draft-vixie-dnsext-dns0x20-00 . DNS 0x20 adds additional entropy to the request by randomly altering the case of the DNS question to help prevent cache poisoning attacks. Google DNS has implemented this support as of 2023, even though this is a proposed and expired standard from 2008: https://groups.google.com/g/public-dns-discuss/c/KxIDPOydA5M There have been documented cases of name server and caching server non-conformance, though it is expected to become more rare, especially since Google has started using this. This can be enabled via the `ARES_FLAG_DNS0x20` flag, which is currently disabled by default. The test cases do however enable this flag to validate this feature. Implementors using this flag will notice that responses will retain the mixed case, but since DNS names are case-insensitive, any proper implementation should not be impacted. There is currently no fallback mechanism implemented as it isn't immediately clear how this may affect a stub resolver like c-ares where we aren't querying the authoritative name server, but instead an intermediate recursive resolver where some domains may return invalid results while others return valid results, all while querying the same nameserver. Likely using DNS cookies as suggested by #620 is a better mechanism to fight cache poisoning attacks for stub resolvers. TCP queries do not use this feature even if the `ARES_FLAG_DNS0x20` flag is specified since they are not subject to cache poisoning attacks. Fixes Issue: #795 Fix By: Brad House (@bradh352)
5 months ago
#define ARES_FLAG_DNS0x20 (1 << 10)
/* Option mask values */
Configuration option to limit number of UDP queries per ephemeral port (#549) Add a new ARES_OPT_UDP_MAX_QUERIES option with udp_max_queries parameter that can be passed to ares_init_options(). This value defaults to 0 (unlimited) to maintain existing compatibility, any positive number will cause new UDP ephemeral ports to be created once the threshold is reached, we'll call these 'connections' even though its technically wrong for UDP. Implementation Details: * Each server entry in a channel now has a linked-list of connections/ports for udp and tcp. The first connection in the list is the one most likely to be eligible to accept new queries. * Queries are now tracked by connection rather than by server. * Every time a query is detached from a connection, the connection that it was attached to will be checked to see if it needs to be cleaned up. * Insertion, lookup, and searching for connections has been implemented as O(1) complexity so the number of connections will not impact performance. * Remove is_broken from the server, it appears it would be set and immediately unset, so must have been invalidated via a prior patch. A future patch should probably track consecutive server errors and de-prioritize such servers. The code right now will always try servers in the order of configuration, so a bad server in the list will always be tried and may rely on timeout logic to try the next. * Various other cleanups to remove code duplication and for clarification. Fixes Bug: #444 Fix By: Brad House (@bradh352)
1 year ago
#define ARES_OPT_FLAGS (1 << 0)
#define ARES_OPT_TIMEOUT (1 << 1)
#define ARES_OPT_TRIES (1 << 2)
#define ARES_OPT_NDOTS (1 << 3)
#define ARES_OPT_UDP_PORT (1 << 4)
#define ARES_OPT_TCP_PORT (1 << 5)
#define ARES_OPT_SERVERS (1 << 6)
#define ARES_OPT_DOMAINS (1 << 7)
#define ARES_OPT_LOOKUPS (1 << 8)
#define ARES_OPT_SOCK_STATE_CB (1 << 9)
#define ARES_OPT_SORTLIST (1 << 10)
#define ARES_OPT_SOCK_SNDBUF (1 << 11)
#define ARES_OPT_SOCK_RCVBUF (1 << 12)
#define ARES_OPT_TIMEOUTMS (1 << 13)
#define ARES_OPT_ROTATE (1 << 14)
#define ARES_OPT_EDNSPSZ (1 << 15)
#define ARES_OPT_NOROTATE (1 << 16)
#define ARES_OPT_RESOLVCONF (1 << 17)
#define ARES_OPT_HOSTS_FILE (1 << 18)
#define ARES_OPT_UDP_MAX_QUERIES (1 << 19)
#define ARES_OPT_MAXTIMEOUTMS (1 << 20)
#define ARES_OPT_QUERY_CACHE (1 << 21)
#define ARES_OPT_EVENT_THREAD (1 << 22)
#define ARES_OPT_SERVER_FAILOVER (1 << 23)
/* Nameinfo flag values */
#define ARES_NI_NOFQDN (1 << 0)
#define ARES_NI_NUMERICHOST (1 << 1)
#define ARES_NI_NAMEREQD (1 << 2)
#define ARES_NI_NUMERICSERV (1 << 3)
#define ARES_NI_DGRAM (1 << 4)
#define ARES_NI_TCP 0
#define ARES_NI_UDP ARES_NI_DGRAM
#define ARES_NI_SCTP (1 << 5)
#define ARES_NI_DCCP (1 << 6)
#define ARES_NI_NUMERICSCOPE (1 << 7)
#define ARES_NI_LOOKUPHOST (1 << 8)
#define ARES_NI_LOOKUPSERVICE (1 << 9)
/* Reserved for future use */
#define ARES_NI_IDN (1 << 10)
#define ARES_NI_IDN_ALLOW_UNASSIGNED (1 << 11)
#define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
/* Addrinfo flag values */
#define ARES_AI_CANONNAME (1 << 0)
#define ARES_AI_NUMERICHOST (1 << 1)
#define ARES_AI_PASSIVE (1 << 2)
#define ARES_AI_NUMERICSERV (1 << 3)
#define ARES_AI_V4MAPPED (1 << 4)
#define ARES_AI_ALL (1 << 5)
#define ARES_AI_ADDRCONFIG (1 << 6)
#define ARES_AI_NOSORT (1 << 7)
#define ARES_AI_ENVHOSTS (1 << 8)
/* Reserved for future use */
#define ARES_AI_IDN (1 << 10)
#define ARES_AI_IDN_ALLOW_UNASSIGNED (1 << 11)
#define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
#define ARES_AI_CANONIDN (1 << 13)
#define ARES_AI_MASK \
(ARES_AI_CANONNAME | ARES_AI_NUMERICHOST | ARES_AI_PASSIVE | \
ARES_AI_NUMERICSERV | ARES_AI_V4MAPPED | ARES_AI_ALL | ARES_AI_ADDRCONFIG)
#define ARES_GETSOCK_MAXNUM \
16 /* ares_getsock() can return info about this \
many sockets */
#define ARES_GETSOCK_READABLE(bits, num) (bits & (1 << (num)))
#define ARES_GETSOCK_WRITABLE(bits, num) \
(bits & (1 << ((num) + ARES_GETSOCK_MAXNUM)))
/* c-ares library initialization flag values */
#define ARES_LIB_INIT_NONE (0)
#define ARES_LIB_INIT_WIN32 (1 << 0)
#define ARES_LIB_INIT_ALL (ARES_LIB_INIT_WIN32)
/* Server state callback flag values */
#define ARES_SERV_STATE_UDP (1 << 0) /* Query used UDP */
#define ARES_SERV_STATE_TCP (1 << 1) /* Query used TCP */
/*
* Typedef our socket type
*/
#ifndef ares_socket_typedef
# if defined(_WIN32) && !defined(WATT32)
typedef SOCKET ares_socket_t;
# define ARES_SOCKET_BAD INVALID_SOCKET
# else
typedef int ares_socket_t;
# define ARES_SOCKET_BAD -1
# endif
# define ares_socket_typedef
#endif /* ares_socket_typedef */
typedef void (*ares_sock_state_cb)(void *data, ares_socket_t socket_fd,
int readable, int writable);
struct apattern;
/* Options controlling server failover behavior.
* The retry chance is the probability (1/N) by which we will retry a failed
* server instead of the best server when selecting a server to send queries
* to.
* The retry delay is the minimum time in milliseconds to wait between doing
* such retries (applied per-server).
*/
struct ares_server_failover_options {
unsigned short retry_chance;
size_t retry_delay;
};
/* NOTE about the ares_options struct to users and developers.
This struct will remain looking like this. It will not be extended nor
shrunk in future releases, but all new options will be set by ares_set_*()
options instead of with the ares_init_options() function.
Eventually (in a galaxy far far away), all options will be settable by
ares_set_*() options and the ares_init_options() function will become
deprecated.
When new options are added to c-ares, they are not added to this
struct. And they are not "saved" with the ares_save_options() function but
instead we encourage the use of the ares_dup() function. Needless to say,
if you add config options to c-ares you need to make sure ares_dup()
duplicates this new option.
*/
struct ares_options {
int flags;
int timeout; /* in seconds or milliseconds, depending on options */
int tries;
int ndots;
unsigned short udp_port; /* host byte order */
unsigned short tcp_port; /* host byte order */
int socket_send_buffer_size;
int socket_receive_buffer_size;
struct in_addr *servers;
int nservers;
char **domains;
int ndomains;
char *lookups;
ares_sock_state_cb sock_state_cb;
void *sock_state_cb_data;
struct apattern *sortlist;
int nsort;
int ednspsz;
char *resolvconf_path;
char *hosts_path;
int udp_max_queries;
int maxtimeout; /* in milliseconds */
1 year ago
unsigned int qcache_max_ttl; /* Maximum TTL for query cache, 0=disabled */
10 months ago
ares_evsys_t evsys;
struct ares_server_failover_options server_failover_opts;
};
struct hostent;
struct timeval;
struct sockaddr;
struct ares_channeldata;
struct ares_addrinfo;
struct ares_addrinfo_hints;
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
/* Legacy typedef, don't use, you can't specify "const" */
typedef struct ares_channeldata *ares_channel;
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
/* Current main channel typedef */
1 year ago
typedef struct ares_channeldata ares_channel_t;
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
Add function ares_search_dnrec() to search for records using the new DNS record parser (#719) This PR adds a new function `ares_search_dnsrec()` to search for records using the new DNS record parser. The function takes an arbitrary DNS record object to search (that must represent a query for a single name). The function takes a new callback type, `ares_callback_dnsrec`, that is invoked with a parsed DNS record object rather than the raw buffer(+length). The original motivation for this change is to provide support for [draft-kaplan-enum-sip-routing-04](https://datatracker.ietf.org/doc/html/draft-kaplan-enum-sip-routing-04); when routing phone calls using an ENUM server, it can be useful to include identifying source information in an OPT RR options value, to help select the appropriate route for the call. The new function allows for more customisable searches like this. **Summary of code changes** A new function `ares_search_dnsrec()` has been added and exposed. Moreover, the entire `ares_search_int()` internal code flow has been refactored to use parsed DNS record objects and the new DNS record parser. The DNS record object is passed through the `search_query` structure by encoding/decoding to/from a buffer (if multiple search domains are used). A helper function `ares_dns_write_query_altname()` is used to re-write the DNS record object with a new query name (used to append search domains). `ares_search()` is now a wrapper around the new internal code, where the DNS record object is created based on the name, class and type parameters. The new function uses a new callback type, `ares_callback_dnsrec`. This is invoked with a parsed DNS record object. For now, we convert from `ares_callback` to this new type using `ares__dnsrec_convert_cb()`. Some functions that are common to both `ares_query()` and `ares_search()` have been refactored using the new DNS record parser. See `ares_dns_record_create_query()` and `ares_dns_query_reply_tostatus()`. **Testing** A new FV has been added to test the new function, which searches for a DNS record containing an OPT RR with custom options value. As part of this, I needed to enhance the mock DNS server to expect request text (and assert that it matches actual request text). This is because the FV needs to check that the request contains the correct OPT RR. **Documentation** The man page docs have been updated to describe the new feature. **Futures** In the future, a new variant of `ares_send()` could be introduced in the same vein (`ares_send_dnsrec()`). This could be used by `ares_search_dnsrec()`. Moreover, we could migrate internal code to use `ares_callback_dnsrec` as the default callback. This will help to make the new DNS record parser the norm in C-Ares. --------- Co-authored-by: Oliver Welsh (@oliverwelsh)
9 months ago
/*
* NOTE: before c-ares 1.7.0 we would most often use the system in6_addr
* struct below when ares itself was built, but many apps would use this
* private version since the header checked a HAVE_* define for it. Starting
* with 1.7.0 we always declare and use our own to stop relying on the
* system's one.
*/
struct ares_in6_addr {
union {
unsigned char _S6_u8[16];
} _S6_un;
};
struct ares_addr {
int family;
union {
struct in_addr addr4;
struct ares_in6_addr addr6;
} addr;
};
/* DNS record parser, writer, and helpers */
#include "ares_dns_record.h"
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
8 months ago
typedef void (*ares_callback)(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
8 months ago
typedef void (*ares_callback_dnsrec)(void *arg, ares_status_t status,
size_t timeouts,
Add function ares_search_dnrec() to search for records using the new DNS record parser (#719) This PR adds a new function `ares_search_dnsrec()` to search for records using the new DNS record parser. The function takes an arbitrary DNS record object to search (that must represent a query for a single name). The function takes a new callback type, `ares_callback_dnsrec`, that is invoked with a parsed DNS record object rather than the raw buffer(+length). The original motivation for this change is to provide support for [draft-kaplan-enum-sip-routing-04](https://datatracker.ietf.org/doc/html/draft-kaplan-enum-sip-routing-04); when routing phone calls using an ENUM server, it can be useful to include identifying source information in an OPT RR options value, to help select the appropriate route for the call. The new function allows for more customisable searches like this. **Summary of code changes** A new function `ares_search_dnsrec()` has been added and exposed. Moreover, the entire `ares_search_int()` internal code flow has been refactored to use parsed DNS record objects and the new DNS record parser. The DNS record object is passed through the `search_query` structure by encoding/decoding to/from a buffer (if multiple search domains are used). A helper function `ares_dns_write_query_altname()` is used to re-write the DNS record object with a new query name (used to append search domains). `ares_search()` is now a wrapper around the new internal code, where the DNS record object is created based on the name, class and type parameters. The new function uses a new callback type, `ares_callback_dnsrec`. This is invoked with a parsed DNS record object. For now, we convert from `ares_callback` to this new type using `ares__dnsrec_convert_cb()`. Some functions that are common to both `ares_query()` and `ares_search()` have been refactored using the new DNS record parser. See `ares_dns_record_create_query()` and `ares_dns_query_reply_tostatus()`. **Testing** A new FV has been added to test the new function, which searches for a DNS record containing an OPT RR with custom options value. As part of this, I needed to enhance the mock DNS server to expect request text (and assert that it matches actual request text). This is because the FV needs to check that the request contains the correct OPT RR. **Documentation** The man page docs have been updated to describe the new feature. **Futures** In the future, a new variant of `ares_send()` could be introduced in the same vein (`ares_send_dnsrec()`). This could be used by `ares_search_dnsrec()`. Moreover, we could migrate internal code to use `ares_callback_dnsrec` as the default callback. This will help to make the new DNS record parser the norm in C-Ares. --------- Co-authored-by: Oliver Welsh (@oliverwelsh)
9 months ago
const ares_dns_record_t *dnsrec);
8 months ago
typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
struct hostent *hostent);
8 months ago
typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
char *node, char *service);
8 months ago
typedef int (*ares_sock_create_callback)(ares_socket_t socket_fd, int type,
void *data);
8 months ago
typedef int (*ares_sock_config_callback)(ares_socket_t socket_fd, int type,
void *data);
8 months ago
typedef void (*ares_addrinfo_callback)(void *arg, int status, int timeouts,
struct ares_addrinfo *res);
typedef void (*ares_server_state_callback)(const char *server_string,
ares_bool_t success, int flags,
void *data);
CARES_EXTERN int ares_library_init(int flags);
CARES_EXTERN int ares_library_init_mem(int flags, void *(*amalloc)(size_t size),
8 months ago
void (*afree)(void *ptr),
void *(*arealloc)(void *ptr,
size_t size));
#if defined(ANDROID) || defined(__ANDROID__)
CARES_EXTERN void ares_library_init_jvm(JavaVM *jvm);
CARES_EXTERN int ares_library_init_android(jobject connectivity_manager);
CARES_EXTERN int ares_library_android_initialized(void);
#endif
8 months ago
CARES_EXTERN int ares_library_initialized(void);
8 months ago
CARES_EXTERN void ares_library_cleanup(void);
8 months ago
CARES_EXTERN const char *ares_version(int *version);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_init_options) int ares_init(
ares_channel_t **channelptr);
CARES_EXTERN int ares_init_options(ares_channel_t **channelptr,
const struct ares_options *options,
int optmask);
CARES_EXTERN int ares_save_options(const ares_channel_t *channel,
struct ares_options *options, int *optmask);
CARES_EXTERN void ares_destroy_options(struct ares_options *options);
CARES_EXTERN int ares_dup(ares_channel_t **dest, const ares_channel_t *src);
CARES_EXTERN ares_status_t ares_reinit(ares_channel_t *channel);
1 year ago
CARES_EXTERN void ares_destroy(ares_channel_t *channel);
1 year ago
CARES_EXTERN void ares_cancel(ares_channel_t *channel);
/* These next 3 configure local binding for the out-going socket
* connection. Use these to specify source IP and/or network device
* on multi-homed systems.
*/
1 year ago
CARES_EXTERN void ares_set_local_ip4(ares_channel_t *channel,
unsigned int local_ip);
/* local_ip6 should be 16 bytes in length */
1 year ago
CARES_EXTERN void ares_set_local_ip6(ares_channel_t *channel,
const unsigned char *local_ip6);
/* local_dev_name should be null terminated. */
1 year ago
CARES_EXTERN void ares_set_local_dev(ares_channel_t *channel,
const char *local_dev_name);
1 year ago
CARES_EXTERN void ares_set_socket_callback(ares_channel_t *channel,
ares_sock_create_callback callback,
void *user_data);
1 year ago
CARES_EXTERN void ares_set_socket_configure_callback(
ares_channel_t *channel, ares_sock_config_callback callback, void *user_data);
CARES_EXTERN void
ares_set_server_state_callback(ares_channel_t *channel,
ares_server_state_callback callback,
void *user_data);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN int ares_set_sortlist(ares_channel_t *channel,
const char *sortstr);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN void ares_getaddrinfo(ares_channel_t *channel, const char *node,
const char *service,
const struct ares_addrinfo_hints *hints,
ares_addrinfo_callback callback, void *arg);
CARES_EXTERN void ares_freeaddrinfo(struct ares_addrinfo *ai);
/*
* Virtual function set to have user-managed socket IO.
* Note that all functions need to be defined, and when
* set, the library will not do any bind nor set any
* socket options, assuming the client handles these
* through either socket creation or the
* ares_sock_config_callback call.
*/
struct iovec;
struct ares_socket_functions {
ares_socket_t (*asocket)(int, int, int, void *);
8 months ago
int (*aclose)(ares_socket_t, void *);
int (*aconnect)(ares_socket_t, const struct sockaddr *, ares_socklen_t,
void *);
ares_ssize_t (*arecvfrom)(ares_socket_t, void *, size_t, int,
struct sockaddr *, ares_socklen_t *, void *);
ares_ssize_t (*asendv)(ares_socket_t, const struct iovec *, int, void *);
};
CARES_EXTERN void
8 months ago
ares_set_socket_functions(ares_channel_t *channel,
const struct ares_socket_functions *funcs,
void *user_data);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_send_dnsrec) void ares_send(
ares_channel_t *channel, const unsigned char *qbuf, int qlen,
ares_callback callback, void *arg);
/*! Send a DNS query as an ares_dns_record_t with a callback containing the
* parsed DNS record.
*
* \param[in] channel Pointer to channel on which queries will be sent.
* \param[in] dnsrec DNS Record to send
* \param[in] callback Callback function invoked on completion or failure of
* the query sequence.
* \param[in] arg Additional argument passed to the callback function.
* \param[out] qid Query ID
* \return One of the c-ares status codes.
*/
8 months ago
CARES_EXTERN ares_status_t ares_send_dnsrec(ares_channel_t *channel,
const ares_dns_record_t *dnsrec,
8 months ago
ares_callback_dnsrec callback,
void *arg, unsigned short *qid);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_query_dnsrec) void ares_query(
ares_channel_t *channel, const char *name, int dnsclass, int type,
ares_callback callback, void *arg);
/*! Perform a DNS query with a callback containing the parsed DNS record.
*
* \param[in] channel Pointer to channel on which queries will be sent.
* \param[in] name Query name
* \param[in] dnsclass DNS Class
* \param[in] type DNS Record Type
* \param[in] callback Callback function invoked on completion or failure of
* the query sequence.
* \param[in] arg Additional argument passed to the callback function.
* \param[out] qid Query ID
* \return One of the c-ares status codes.
*/
CARES_EXTERN ares_status_t ares_query_dnsrec(ares_channel_t *channel,
const char *name,
ares_dns_class_t dnsclass,
ares_dns_rec_type_t type,
ares_callback_dnsrec callback,
8 months ago
void *arg, unsigned short *qid);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_search_dnsrec) void ares_search(
ares_channel_t *channel, const char *name, int dnsclass, int type,
ares_callback callback, void *arg);
Add function ares_search_dnrec() to search for records using the new DNS record parser (#719) This PR adds a new function `ares_search_dnsrec()` to search for records using the new DNS record parser. The function takes an arbitrary DNS record object to search (that must represent a query for a single name). The function takes a new callback type, `ares_callback_dnsrec`, that is invoked with a parsed DNS record object rather than the raw buffer(+length). The original motivation for this change is to provide support for [draft-kaplan-enum-sip-routing-04](https://datatracker.ietf.org/doc/html/draft-kaplan-enum-sip-routing-04); when routing phone calls using an ENUM server, it can be useful to include identifying source information in an OPT RR options value, to help select the appropriate route for the call. The new function allows for more customisable searches like this. **Summary of code changes** A new function `ares_search_dnsrec()` has been added and exposed. Moreover, the entire `ares_search_int()` internal code flow has been refactored to use parsed DNS record objects and the new DNS record parser. The DNS record object is passed through the `search_query` structure by encoding/decoding to/from a buffer (if multiple search domains are used). A helper function `ares_dns_write_query_altname()` is used to re-write the DNS record object with a new query name (used to append search domains). `ares_search()` is now a wrapper around the new internal code, where the DNS record object is created based on the name, class and type parameters. The new function uses a new callback type, `ares_callback_dnsrec`. This is invoked with a parsed DNS record object. For now, we convert from `ares_callback` to this new type using `ares__dnsrec_convert_cb()`. Some functions that are common to both `ares_query()` and `ares_search()` have been refactored using the new DNS record parser. See `ares_dns_record_create_query()` and `ares_dns_query_reply_tostatus()`. **Testing** A new FV has been added to test the new function, which searches for a DNS record containing an OPT RR with custom options value. As part of this, I needed to enhance the mock DNS server to expect request text (and assert that it matches actual request text). This is because the FV needs to check that the request contains the correct OPT RR. **Documentation** The man page docs have been updated to describe the new feature. **Futures** In the future, a new variant of `ares_send()` could be introduced in the same vein (`ares_send_dnsrec()`). This could be used by `ares_search_dnsrec()`. Moreover, we could migrate internal code to use `ares_callback_dnsrec` as the default callback. This will help to make the new DNS record parser the norm in C-Ares. --------- Co-authored-by: Oliver Welsh (@oliverwelsh)
9 months ago
/*! Search for a complete DNS message.
*
* \param[in] channel Pointer to channel on which queries will be sent.
* \param[in] dnsrec Pointer to initialized and filled DNS record object.
* \param[in] callback Callback function invoked on completion or failure of
* the query sequence.
* \param[in] arg Additional argument passed to the callback function.
* \return One of the c-ares status codes. In all cases, except
* ARES_EFORMERR due to misuse, this error code will also be sent
* to the provided callback.
Add function ares_search_dnrec() to search for records using the new DNS record parser (#719) This PR adds a new function `ares_search_dnsrec()` to search for records using the new DNS record parser. The function takes an arbitrary DNS record object to search (that must represent a query for a single name). The function takes a new callback type, `ares_callback_dnsrec`, that is invoked with a parsed DNS record object rather than the raw buffer(+length). The original motivation for this change is to provide support for [draft-kaplan-enum-sip-routing-04](https://datatracker.ietf.org/doc/html/draft-kaplan-enum-sip-routing-04); when routing phone calls using an ENUM server, it can be useful to include identifying source information in an OPT RR options value, to help select the appropriate route for the call. The new function allows for more customisable searches like this. **Summary of code changes** A new function `ares_search_dnsrec()` has been added and exposed. Moreover, the entire `ares_search_int()` internal code flow has been refactored to use parsed DNS record objects and the new DNS record parser. The DNS record object is passed through the `search_query` structure by encoding/decoding to/from a buffer (if multiple search domains are used). A helper function `ares_dns_write_query_altname()` is used to re-write the DNS record object with a new query name (used to append search domains). `ares_search()` is now a wrapper around the new internal code, where the DNS record object is created based on the name, class and type parameters. The new function uses a new callback type, `ares_callback_dnsrec`. This is invoked with a parsed DNS record object. For now, we convert from `ares_callback` to this new type using `ares__dnsrec_convert_cb()`. Some functions that are common to both `ares_query()` and `ares_search()` have been refactored using the new DNS record parser. See `ares_dns_record_create_query()` and `ares_dns_query_reply_tostatus()`. **Testing** A new FV has been added to test the new function, which searches for a DNS record containing an OPT RR with custom options value. As part of this, I needed to enhance the mock DNS server to expect request text (and assert that it matches actual request text). This is because the FV needs to check that the request contains the correct OPT RR. **Documentation** The man page docs have been updated to describe the new feature. **Futures** In the future, a new variant of `ares_send()` could be introduced in the same vein (`ares_send_dnsrec()`). This could be used by `ares_search_dnsrec()`. Moreover, we could migrate internal code to use `ares_callback_dnsrec` as the default callback. This will help to make the new DNS record parser the norm in C-Ares. --------- Co-authored-by: Oliver Welsh (@oliverwelsh)
9 months ago
*/
8 months ago
CARES_EXTERN ares_status_t ares_search_dnsrec(ares_channel_t *channel,
const ares_dns_record_t *dnsrec,
8 months ago
ares_callback_dnsrec callback,
void *arg);
Add function ares_search_dnrec() to search for records using the new DNS record parser (#719) This PR adds a new function `ares_search_dnsrec()` to search for records using the new DNS record parser. The function takes an arbitrary DNS record object to search (that must represent a query for a single name). The function takes a new callback type, `ares_callback_dnsrec`, that is invoked with a parsed DNS record object rather than the raw buffer(+length). The original motivation for this change is to provide support for [draft-kaplan-enum-sip-routing-04](https://datatracker.ietf.org/doc/html/draft-kaplan-enum-sip-routing-04); when routing phone calls using an ENUM server, it can be useful to include identifying source information in an OPT RR options value, to help select the appropriate route for the call. The new function allows for more customisable searches like this. **Summary of code changes** A new function `ares_search_dnsrec()` has been added and exposed. Moreover, the entire `ares_search_int()` internal code flow has been refactored to use parsed DNS record objects and the new DNS record parser. The DNS record object is passed through the `search_query` structure by encoding/decoding to/from a buffer (if multiple search domains are used). A helper function `ares_dns_write_query_altname()` is used to re-write the DNS record object with a new query name (used to append search domains). `ares_search()` is now a wrapper around the new internal code, where the DNS record object is created based on the name, class and type parameters. The new function uses a new callback type, `ares_callback_dnsrec`. This is invoked with a parsed DNS record object. For now, we convert from `ares_callback` to this new type using `ares__dnsrec_convert_cb()`. Some functions that are common to both `ares_query()` and `ares_search()` have been refactored using the new DNS record parser. See `ares_dns_record_create_query()` and `ares_dns_query_reply_tostatus()`. **Testing** A new FV has been added to test the new function, which searches for a DNS record containing an OPT RR with custom options value. As part of this, I needed to enhance the mock DNS server to expect request text (and assert that it matches actual request text). This is because the FV needs to check that the request contains the correct OPT RR. **Documentation** The man page docs have been updated to describe the new feature. **Futures** In the future, a new variant of `ares_send()` could be introduced in the same vein (`ares_send_dnsrec()`). This could be used by `ares_search_dnsrec()`. Moreover, we could migrate internal code to use `ares_callback_dnsrec` as the default callback. This will help to make the new DNS record parser the norm in C-Ares. --------- Co-authored-by: Oliver Welsh (@oliverwelsh)
9 months ago
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_getaddrinfo) void ares_gethostbyname(
ares_channel_t *channel, const char *name, int family,
ares_host_callback callback, void *arg);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN int ares_gethostbyname_file(ares_channel_t *channel,
const char *name, int family,
struct hostent **host);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN void ares_gethostbyaddr(ares_channel_t *channel, const void *addr,
int addrlen, int family,
ares_host_callback callback, void *arg);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN void ares_getnameinfo(ares_channel_t *channel,
const struct sockaddr *sa,
ares_socklen_t salen, int flags,
ares_nameinfo_callback callback, void *arg);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(
ARES_OPT_EVENT_THREAD or
ARES_OPT_SOCK_STATE_CB) int ares_fds(const ares_channel_t *channel,
8 months ago
fd_set *read_fds, fd_set *write_fds);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(
ARES_OPT_EVENT_THREAD or
ARES_OPT_SOCK_STATE_CB) int ares_getsock(const ares_channel_t *channel,
8 months ago
ares_socket_t *socks, int numsocks);
CARES_EXTERN struct timeval *ares_timeout(const ares_channel_t *channel,
struct timeval *maxtv,
struct timeval *tv);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_process_fd) void ares_process(
ares_channel_t *channel, fd_set *read_fds, fd_set *write_fds);
`ares_channel` -> `ares_channel_t *`: don't bury the pointer (#595) `ares_channel` is defined as `typedef struct ares_channeldata *ares_channel;`. The problem with this, is it embeds the pointer into the typedef, which means an `ares_channel` can never be declared as `const` as if you write `const ares_channel channel`, that expands to `struct ares_channeldata * const ares_channel` and not `const struct ares_channeldata *channel`. We will now typedef `ares_channel_t` as `typedef struct ares_channeldata ares_channel_t;`, so if you write `const ares_channel_t *channel`, it properly expands to `const struct ares_channeldata *channel`. We are maintaining the old typedef for API compatibility with existing integrations, and due to typedef expansion this should not even cause any compiler warnings for existing code. There are no ABI implications with this change. I could be convinced to keep existing public functions as `ares_channel` if a sufficient argument exists, but internally we really need make this change for modern best practices. This change will allow us to internally use `const ares_channel_t *` where appropriate. Whether or not we decide to change any public interfaces to use `const` may require further discussion on if there might be ABI implications (I don't think so, but I'm also not 100% sure what a compiler internally does with `const` when emitting machine code ... I think more likely ABI implications would occur going the opposite direction). FYI, This PR was done via a combination of sed and clang-format, the only manual code change was the addition of the new typedef, and a couple doc fixes :) Fix By: Brad House (@bradh352)
1 year ago
CARES_EXTERN void ares_process_fd(ares_channel_t *channel,
ares_socket_t read_fd,
ares_socket_t write_fd);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_record_create) int ares_create_query(
const char *name, int dnsclass, int type, unsigned short id, int rd,
unsigned char **buf, int *buflen, int max_udp_size);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_record_create) int ares_mkquery(
const char *name, int dnsclass, int type, unsigned short id, int rd,
unsigned char **buf, int *buflen);
8 months ago
CARES_EXTERN int ares_expand_name(const unsigned char *encoded,
const unsigned char *abuf, int alen, char **s,
long *enclen);
8 months ago
CARES_EXTERN int ares_expand_string(const unsigned char *encoded,
const unsigned char *abuf, int alen,
unsigned char **s, long *enclen);
struct ares_addrttl {
struct in_addr ipaddr;
int ttl;
};
struct ares_addr6ttl {
struct ares_in6_addr ip6addr;
int ttl;
};
struct ares_caa_reply {
struct ares_caa_reply *next;
int critical;
unsigned char *property;
size_t plength; /* plength excludes null termination */
unsigned char *value;
size_t length; /* length excludes null termination */
};
struct ares_srv_reply {
struct ares_srv_reply *next;
char *host;
unsigned short priority;
unsigned short weight;
unsigned short port;
};
struct ares_mx_reply {
struct ares_mx_reply *next;
char *host;
unsigned short priority;
};
struct ares_txt_reply {
struct ares_txt_reply *next;
unsigned char *txt;
size_t length; /* length excludes null termination */
};
/* NOTE: This structure is a superset of ares_txt_reply
*/
struct ares_txt_ext {
struct ares_txt_ext *next;
unsigned char *txt;
size_t length;
/* 1 - if start of new record
* 0 - if a chunk in the same record */
unsigned char record_start;
};
struct ares_naptr_reply {
struct ares_naptr_reply *next;
unsigned char *flags;
unsigned char *service;
unsigned char *regexp;
char *replacement;
unsigned short order;
unsigned short preference;
};
struct ares_soa_reply {
char *nsname;
char *hostmaster;
unsigned int serial;
unsigned int refresh;
unsigned int retry;
unsigned int expire;
unsigned int minttl;
};
struct ares_uri_reply {
struct ares_uri_reply *next;
unsigned short priority;
unsigned short weight;
char *uri;
int ttl;
};
/*
* Similar to addrinfo, but with extra ttl and missing canonname.
*/
struct ares_addrinfo_node {
int ai_ttl;
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
ares_socklen_t ai_addrlen;
struct sockaddr *ai_addr;
struct ares_addrinfo_node *ai_next;
};
/*
* alias - label of the resource record.
* name - value (canonical name) of the resource record.
* See RFC2181 10.1.1. CNAME terminology.
*/
struct ares_addrinfo_cname {
int ttl;
char *alias;
char *name;
struct ares_addrinfo_cname *next;
};
struct ares_addrinfo {
struct ares_addrinfo_cname *cnames;
struct ares_addrinfo_node *nodes;
Reimplement ares_gethostbyname() by wrapping ares_getaddrinfo() (#428) ares_gethostbyname() and ares_getaddrinfo() do a lot of similar things, however ares_getaddrinfo() has some desirable behaviors that should be imported into ares_gethostbyname(). For one, it sorts the address lists for the most likely to succeed based on the current system routes. Next, when AF_UNSPEC is specified, it properly handles search lists instead of first searching all of AF_INET6 then AF_INET, since ares_gethostbyname() searches in parallel. Therefore, this PR should also resolve the issues attempted in #94. A few things this PR does: 1. ares_parse_a_reply() and ares_parse_aaaa_reply() had very similar code to translate struct ares_addrinfo into a struct hostent as well as into struct ares_addrttl/ares_addr6ttl this has been split out into helper functions of ares__addrinfo2hostent() and ares__addrinfo2addrttl() to prevent this duplicative code. 2. ares_getaddrinfo() was apparently never honoring HOSTALIASES, and this was discovered once ares_gethostbyname() was turned into a wrapper, the affected test cases started failing. 3. A slight API modification to save the query hostname into struct ares_addrinfo as the last element of name. Since this is the last element, and all user-level instances of struct ares_addrinfo are allocated internally by c-ares, this is not an ABI-breaking change nor would it impact any API compatibility. This was needed since struct hostent has an h_name element. 4. Test Framework: MockServer tests via TCP would fail if more than 1 request was received at a time which is common when ares_getaddrinfo() queries for both A and AAAA records simultaneously. Infact, this was a long standing issue in which the ares_getaddrinfo() test were bypassing TCP alltogether. This has been corrected, the message is now processed in a loop. 5. Some tests had to be updated for overall correctness as they were invalid but somehow passing prior to this change. Change By: Brad House (@bradh352)
3 years ago
char *name;
};
struct ares_addrinfo_hints {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
};
/*
** Parse the buffer, starting at *abuf and of length alen bytes, previously
** obtained from an ares_search call. Put the results in *host, if nonnull.
** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
** their TTLs in that array, and set *naddrttls to the number of addresses
** so written.
*/
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_a_reply(
const unsigned char *abuf, int alen, struct hostent **host,
struct ares_addrttl *addrttls, int *naddrttls);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_aaaa_reply(
const unsigned char *abuf, int alen, struct hostent **host,
struct ares_addr6ttl *addrttls, int *naddrttls);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_caa_reply(
const unsigned char *abuf, int alen, struct ares_caa_reply **caa_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_ptr_reply(
const unsigned char *abuf, int alen, const void *addr, int addrlen,
int family, struct hostent **host);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_ns_reply(
const unsigned char *abuf, int alen, struct hostent **host);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_srv_reply(
const unsigned char *abuf, int alen, struct ares_srv_reply **srv_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_mx_reply(
const unsigned char *abuf, int alen, struct ares_mx_reply **mx_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_txt_reply(
const unsigned char *abuf, int alen, struct ares_txt_reply **txt_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_txt_reply_ext(
const unsigned char *abuf, int alen, struct ares_txt_ext **txt_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_naptr_reply(
const unsigned char *abuf, int alen, struct ares_naptr_reply **naptr_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_soa_reply(
const unsigned char *abuf, int alen, struct ares_soa_reply **soa_out);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_uri_reply(
const unsigned char *abuf, int alen, struct ares_uri_reply **uri_out);
8 months ago
CARES_EXTERN void ares_free_string(void *str);
8 months ago
CARES_EXTERN void ares_free_hostent(struct hostent *host);
8 months ago
CARES_EXTERN void ares_free_data(void *dataptr);
CARES_EXTERN const char *ares_strerror(int code);
struct ares_addr_node {
struct ares_addr_node *next;
int family;
union {
struct in_addr addr4;
struct ares_in6_addr addr6;
} addr;
};
struct ares_addr_port_node {
struct ares_addr_port_node *next;
int family;
union {
struct in_addr addr4;
struct ares_in6_addr addr6;
} addr;
int udp_port;
int tcp_port;
};
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_set_servers_csv) int ares_set_servers(
ares_channel_t *channel, const struct ares_addr_node *servers);
8 months ago
CARES_EXTERN
CARES_DEPRECATED_FOR(ares_set_servers_ports_csv)
int ares_set_servers_ports(ares_channel_t *channel,
const struct ares_addr_port_node *servers);
/* Incoming string format: host[:port][,host[:port]]... */
8 months ago
CARES_EXTERN int ares_set_servers_csv(ares_channel_t *channel,
const char *servers);
CARES_EXTERN int ares_set_servers_ports_csv(ares_channel_t *channel,
const char *servers);
CARES_EXTERN char *ares_get_servers_csv(const ares_channel_t *channel);
8 months ago
CARES_EXTERN CARES_DEPRECATED_FOR(ares_get_servers_csv) int ares_get_servers(
const ares_channel_t *channel, struct ares_addr_node **servers);
8 months ago
CARES_EXTERN
CARES_DEPRECATED_FOR(ares_get_servers_csv)
int ares_get_servers_ports(const ares_channel_t *channel,
struct ares_addr_port_node **servers);
10 months ago
CARES_EXTERN const char *ares_inet_ntop(int af, const void *src, char *dst,
ares_socklen_t size);
10 months ago
CARES_EXTERN int ares_inet_pton(int af, const char *src, void *dst);
/*! Whether or not the c-ares library was built with threadsafety
*
* \return ARES_TRUE if built with threadsafety, ARES_FALSE if not
*/
10 months ago
CARES_EXTERN ares_bool_t ares_threadsafety(void);
/*! Block until notified that there are no longer any queries in queue, or
* the specified timeout has expired.
*
* \param[in] channel Initialized ares channel
* \param[in] timeout_ms Number of milliseconds to wait for the queue to be
* empty. -1 for Infinite.
* \return ARES_ENOTIMP if not built with threading support, ARES_ETIMEOUT
* if requested timeout expires, ARES_SUCCESS when queue is empty.
*/
CARES_EXTERN ares_status_t ares_queue_wait_empty(ares_channel_t *channel,
10 months ago
int timeout_ms);
/*! Retrieve the total number of active queries pending answers from servers.
* Some c-ares requests may spawn multiple queries, such as ares_getaddrinfo()
* when using AF_UNSPEC, which will be reflected in this number.
*
* \param[in] channel Initialized ares channel
* \return Number of active queries to servers
*/
CARES_EXTERN size_t ares_queue_active_queries(const ares_channel_t *channel);
#ifdef __cplusplus
}
#endif
#endif /* ARES__H */