File: ares_queryloop.c

package info (click to toggle)
c-ares 1.34.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,732 kB
  • sloc: ansic: 31,201; cpp: 13,236; sh: 4,454; makefile: 124
file content (179 lines) | stat: -rw-r--r-- 5,043 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* MIT License
 *
 * Copyright (c) The c-ares project and its contributors
 *
 * 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
 */

/* This test program is meant to loop indefinitely performing a query for the
 * same domain once per second.  The purpose of this is to test the event loop
 * configuration change detection.  You can modify the system configuration
 * and verify queries work or don't work as expected. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#  include <winsock2.h>
#  include <windows.h>
#else
#  include <unistd.h>
#  include <signal.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#endif
#include "ares.h"

static void ai_callback(void *arg, int status, int timeouts,
                        struct ares_addrinfo *result)
{
  struct ares_addrinfo_node *node = NULL;
  (void)timeouts;


  if (status != ARES_SUCCESS) {
    fprintf(stderr, "%s: %s\n", (char *)arg, ares_strerror(status));
    return;
  }

  for (node = result->nodes; node != NULL; node = node->ai_next) {
    char        addr_buf[64] = "";
    const void *ptr          = NULL;
    if (node->ai_family == AF_INET) {
      const struct sockaddr_in *in_addr =
        (const struct sockaddr_in *)((void *)node->ai_addr);
      ptr = &in_addr->sin_addr;
    } else if (node->ai_family == AF_INET6) {
      const struct sockaddr_in6 *in_addr =
        (const struct sockaddr_in6 *)((void *)node->ai_addr);
      ptr = &in_addr->sin6_addr;
    } else {
      continue;
    }
    ares_inet_ntop(node->ai_family, ptr, addr_buf, sizeof(addr_buf));
    printf("%-32s\t%s\n", result->name, addr_buf);
  }

  ares_freeaddrinfo(result);
}

static volatile ares_bool_t is_running = ARES_TRUE;


#ifdef _WIN32
static BOOL WINAPI ctrlc_handler(_In_ DWORD dwCtrlType)
{
  switch (dwCtrlType) {
    case CTRL_C_EVENT:
      is_running = ARES_FALSE;
      return TRUE;
    default:
      break;
  }
  return FALSE;
}
#else
static void ctrlc_handler(int sig)
{
  switch (sig) {
    case SIGINT:
      is_running = ARES_FALSE;
      break;
    default:
      break;
  }
}
#endif

int main(int argc, char *argv[])
{
  struct ares_options options;
  int                 optmask = 0;
  ares_channel_t     *channel;
  size_t              count;
  ares_status_t       status;

#ifdef _WIN32
  WORD    wVersionRequested = MAKEWORD(2, 2);
  WSADATA wsaData;
  WSAStartup(wVersionRequested, &wsaData);
#endif

  if (argc != 2) {
    printf("Usage: %s domain\n", argv[0]);
    return 1;
  }

  status = (ares_status_t)ares_library_init(ARES_LIB_INIT_ALL);
  if (status != ARES_SUCCESS) {
    fprintf(stderr, "ares_library_init: %s\n", ares_strerror((int)status));
    return 1;
  }

  memset(&options, 0, sizeof(options));
  optmask                |= ARES_OPT_EVENT_THREAD;
  options.evsys           = ARES_EVSYS_DEFAULT;
  optmask                |= ARES_OPT_QUERY_CACHE;
  options.qcache_max_ttl  = 0;

  status = (ares_status_t)ares_init_options(&channel, &options, optmask);
  if (status != ARES_SUCCESS) {
    fprintf(stderr, "ares_init: %s\n", ares_strerror((int)status));
    return 1;
  }

#ifdef _WIN32
  SetConsoleCtrlHandler(ctrlc_handler, TRUE);
#else
  signal(SIGINT, ctrlc_handler);
#endif

  printf("Querying for %s every 1s, press CTRL-C to quit...\n", argv[1]);

  for (count = 1; is_running == ARES_TRUE; count++) {
    struct ares_addrinfo_hints hints;
    char                      *servers = ares_get_servers_csv(channel);

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    printf("Attempt %u using server list: %s ...\n", (unsigned int)count,
           servers);
    ares_free_string(servers);

    ares_getaddrinfo(channel, argv[1], NULL, &hints, ai_callback, argv[1]);
#ifdef _WIN32
    Sleep(1000);
#else
    sleep(1);
#endif
  }

  printf("CTRL-C captured, cleaning up...\n");
  ares_destroy(channel);
  ares_library_cleanup();

#ifdef _WIN32
  WSACleanup();
#endif
  return 0;
}