Go to the documentation of this file.
14 #ifdef HAVE_NETINET_TCP_H
15 #include <netinet/tcp.h>
24 #define BYTE_CAST (char *)
25 #define mkdir_(d, p) mkdir(d)
28 #define mkdir_(d, p) mkdir(d, p)
43 std::make_pair<int, bool>(AF_INET,
false),
44 std::make_pair<int, bool>(AF_INET,
true),
45 std::make_pair<int, bool>(
AF_INET6,
false),
46 std::make_pair<int, bool>(
AF_INET6,
true)
49 std::make_pair<int, bool>(AF_INET,
false),
50 std::make_pair<int, bool>(AF_INET,
true)
53 std::make_pair<int, bool>(
AF_INET6,
false),
54 std::make_pair<int, bool>(
AF_INET6,
true)
68 fd_set readers, writers;
79 std::set<int> extrafds = get_extrafds();
80 for (
int extrafd : extrafds) {
81 FD_SET(extrafd, &readers);
82 if (extrafd >= nfds) {
90 count = select(nfds, &readers, &writers,
nullptr, &tv);
92 fprintf(
stderr,
"select() failed, errno %d\n", errno);
100 for (
int extrafd : extrafds) {
101 if (FD_ISSET(extrafd, &readers)) {
102 process_extra(extrafd);
111 assert(nth <= (
int)(8 *
sizeof(
fails_)));
141 if (
verbose) std::cerr <<
"Failing malloc(" <<
size <<
") request" << std::endl;
151 if (
verbose) std::cerr <<
"Failing realloc(" <<
ptr <<
", " <<
size <<
") request" << std::endl;
164 return std::set<int>();
176 : udpport_(
port), tcpport_(
port), qid_(-1) {
181 setsockopt(
tcpfd_, SOL_SOCKET, SO_REUSEADDR,
184 setsockopt(
tcpfd_, IPPROTO_TCP, TCP_NODELAY,
192 if (family == AF_INET) {
193 struct sockaddr_in
addr;
195 addr.sin_family = AF_INET;
196 addr.sin_addr.s_addr = htonl(INADDR_ANY);
205 ares_socklen_t
len =
sizeof(
addr);
212 ares_socklen_t
len =
sizeof(
addr);
232 ares_socklen_t
len =
sizeof(
addr);
239 ares_socklen_t
len =
sizeof(
addr);
246 if (
verbose) std::cerr <<
"Configured "
247 << (family == AF_INET ?
"IPv4" :
"IPv6")
248 <<
" mock server with TCP socket " <<
tcpfd_
250 <<
" and UDP socket " <<
udpfd_
251 <<
" on port " <<
udpport_ << std::endl;
271 int connfd = accept(
tcpfd_, NULL, NULL);
273 std::cerr <<
"Error accepting connection on fd " << fd << std::endl;
281 struct sockaddr_storage
addr;
282 socklen_t addrlen =
sizeof(
addr);
285 (
struct sockaddr *)&
addr, &addrlen);
294 std::cerr <<
"Packet too short (" <<
len <<
")" << std::endl;
297 int tcplen = (
data[0] << 8) +
data[1];
301 std::cerr <<
"Warning: TCP length " << tcplen
302 <<
" doesn't match remaining data length " <<
len << std::endl;
309 std::cerr <<
"Packet too short (" <<
len <<
")" << std::endl;
314 std::cerr <<
"Not a request" << std::endl;
327 byte* question =
data + 12;
330 char *
name =
nullptr;
334 std::cerr <<
"Failed to retrieve name" << std::endl;
343 std::cerr <<
"Unexpected question size (" << qlen
344 <<
" bytes after name)" << std::endl;
358 std::cerr <<
"ProcessRequest(" << qid <<
", '" << namestr
374 OnRequest(
name, rrtype);
381 std::vector<byte> reply =
reply_;
387 if (reply.size() >= 2) {
389 reply[0] = (
byte)((qid >> 8) & 0xff);
390 reply[1] = (
byte)(qid & 0xff);
397 int len = reply.size();
398 std::vector<byte> vlen = {(
byte)((
len & 0xFF00) >> 8), (
byte)(
len & 0xFF)};
399 reply.insert(reply.begin(), vlen.begin(), vlen.end());
405 int rc = sendto(fd,
BYTE_CAST reply.data(), reply.size(), 0,
406 (
struct sockaddr *)
addr, addrlen);
407 if (rc <
static_cast<int>(reply.size())) {
408 std::cerr <<
"Failed to send full reply, rc=" << rc << std::endl;
416 for (
int ii = 0; ii <
count; ii++) {
456 const char *
domains[3] = {
"first.com",
"second.org",
"third.gov"};
480 node->
next =
nullptr;
485 node->
addr.
addr4.s_addr = htonl(0x7F000001);
500 std::cerr <<
"Configured library with servers:";
503 std::cerr <<
" " <<
server;
505 std::cerr << std::endl;
519 std::set<int> serverfds =
server->fds();
520 fds.insert(serverfds.begin(), serverfds.end());
532 using namespace std::placeholders;
543 os <<
"(incomplete)";
553 name_ = hostent->h_name;
554 if (hostent->h_aliases) {
555 char** palias = hostent->h_aliases;
556 while (*palias !=
nullptr) {
562 if (hostent->h_addr_list) {
563 char** paddr = hostent->h_addr_list;
564 while (*paddr !=
nullptr) {
574 os <<
"'" << host.
name_ <<
"' "
576 for (
size_t ii = 0; ii < host.
aliases_.size(); ii++) {
577 if (ii > 0) os <<
", ";
582 for (
size_t ii = 0; ii < host.
addrs_.size(); ii++) {
583 if (ii > 0) os <<
", ";
592 struct hostent *hostent) {
597 result->timeouts_ = timeouts;
599 if (
verbose) std::cerr <<
"HostCallback(" << *
result <<
")" << std::endl;
607 os <<
"(incomplete)";
622 if(next_cname->
alias) {
623 os << next_cname->
alias <<
"->";
625 if(next_cname->
name) {
626 os << next_cname->
name;
628 if((next_cname = next_cname->
next))
639 unsigned short port = 0;
641 if(
next->ai_family == AF_INET) {
642 sockaddr_in* sin = (sockaddr_in*)
next->ai_addr;
643 port = ntohs(sin->sin_port);
652 os <<
"unknown family";
670 result->timeouts_= timeouts;
672 if (
verbose) std::cerr <<
"AddrInfoCallback(" << *
result <<
")" << std::endl;
680 os <<
"(incomplete)";
687 unsigned char *abuf,
int alen) {
692 result->timeouts_ = timeouts;
693 result->data_.assign(abuf, abuf + alen);
694 if (
verbose) std::cerr <<
"SearchCallback(" << *
result <<
")" << std::endl;
702 os <<
"(incomplete)";
714 result->timeouts_ = timeouts;
717 if (
verbose) std::cerr <<
"NameInfoCallback(" << *
result <<
")" << std::endl;
724 std::vector<std::string>
results;
726 std::stringstream ss;
732 if (
server->udp_port != 0) {
736 if (
server->udp_port != 0) {
741 results.push_back(
"<unknown family>");
744 if (
server->udp_port != 0) {
745 ss <<
":" <<
server->udp_port;
756 std::cerr <<
"Failed to create subdirectory '" <<
dirname_ <<
"'" << std::endl;
769 std::cerr <<
"Error: failed to create '" <<
filename <<
"'" << std::endl;
774 std::cerr <<
"Error: failed to write contents of '" <<
filename <<
"'" << std::endl;
784 char *
p = tempnam(dir,
prefix);
const std::vector< int > both_families
absl::string_view get(const Cont &c)
static const ares_socket_functions default_functions
std::set< int > fds() const
CARES_EXTERN void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
static void afree(void *ptr)
static void ** find(grpc_chttp2_stream_map *map, uint32_t key)
return memset(p, 0, total)
std::unique_ptr< ares_addrinfo, AddrInfoDeleter > AddrInfo
static void SetAllocFail(int nth)
const std::vector< int > ipv6_family
const std::vector< std::pair< int, bool > > both_families_both_modes
CARES_EXTERN void ares_free_string(void *str)
static void SetAllocSizeFail(size_t size)
std::vector< std::unique_ptr< NiceMockServer > > NiceMockServers
union ares_addr_port_node::@383 addr
CARES_EXTERN int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
#define ARES_OPT_UDP_PORT
#define DNS_HEADER_OPCODE(h)
std::string RRTypeToString(int rrtype)
#define ARES_OPT_TIMEOUTMS
#define DNS_HEADER_QID(h)
static constexpr int dynamic_port
void ProcessRequest(int fd, struct sockaddr_storage *addr, int addrlen, int qid, const std::string &name, int rrtype)
std::vector< int > families
MockChannelOptsTest(int count, int family, bool force_tcp, struct ares_options *givenopts, int optmask)
TempFile(const std::string &contents)
CARES_EXTERN int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf, int alen, char **s, long *enclen)
std::unique_ptr< Server > server
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
CARES_EXTERN void ares_set_socket_functions(ares_channel channel, const struct ares_socket_functions *funcs, void *user_data)
#define DNS_QUESTION_CLASS(q)
wrapped_grpc_channel * channel
std::string AddressToString(const void *vaddr, int len)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
constexpr size_type size() const noexcept
#define EXPECT_NE(val1, val2)
#define DNS_QUESTION_TYPE(q)
std::string PacketToString(const std::vector< byte > &packet)
Poll< absl::StatusOr< std::tuple< T... > > > fail()
std::vector< byte > reply_
testing::NiceMock< MockServer > NiceMockServer
struct ares_in6_addr addr6
static NiceMockServers BuildServers(int count, int family, int base_port)
void SearchCallback(void *data, int status, int timeouts, unsigned char *abuf, int alen)
RefCountedPtr< Channel > channel_
std::string TempNam(const char *dir, const char *prefix)
std::ostream & operator<<(std::ostream &os, const HostResult &result)
#define ARES_OPT_TCP_PORT
#define DNS_HEADER_QDCOUNT(h)
struct ares_addrinfo_cname * next
static unsigned long long fails_
std::vector< std::string > aliases_
CARES_EXTERN void ares_free_data(void *dataptr)
CARES_EXTERN int ares_set_servers_ports(ares_channel channel, struct ares_addr_port_node *servers)
CARES_EXTERN int ares_get_servers_ports(ares_channel channel, struct ares_addr_port_node **servers)
void HostCallback(void *data, int status, int timeouts, struct hostent *hostent)
static bool ShouldAllocFail(size_t size)
def listen(endpoint, test_case)
std::vector< std::pair< int, bool > > families_modes
const std::vector< int > ipv4_family
void NameInfoCallback(void *data, int status, int timeouts, char *node, char *service)
struct ares_addr_port_node * next
CARES_EXTERN void ares_destroy(ares_channel channel)
TransientDir(const std::string &dirname)
AllocList * next[kMaxLevel]
MockServer(int family, int port)
const std::vector< std::pair< int, bool > > ipv4_family_both_modes
static void * arealloc(void *ptr, size_t size)
static const char prefix[]
VirtualizeIO(ares_channel)
std::set< int > NoExtraFDs()
void ProcessWork(ares_channel channel, std::function< std::set< int >()> get_extrafds, std::function< void(int)> process_extra)
struct ares_in6_addr sin6_addr
std::string StatusToString(int status)
static void * amalloc(size_t size)
static std::map< size_t, int > size_fails_
CARES_EXTERN int ares_init_options(ares_channel *channelptr, struct ares_options *options, int optmask)
std::set< int > fds() const
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
std::vector< std::string > domains
std::vector< std::string > addrs_
constexpr const_pointer data() const noexcept
void AddrInfoCallback(void *data, int status, int timeouts, struct ares_addrinfo *ai)
std::vector< std::string > GetNameServers(ares_channel channel)
TransientFile(const std::string &filename, const std::string &contents)
const std::vector< std::pair< int, bool > > ipv6_family_both_modes
std::vector< std::unique_ptr< ServerData > > servers_
union ares_in6_addr::@381 _S6_un
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:43