summaryrefslogtreecommitdiff
path: root/resolv/tst-res_use_inet6.c
blob: 6f3db088929027b7dfcfa33320acc3e0e209d4ec (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Basic functionality tests for inet6 option processing.
   Copyright (C) 2016-2017 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <netdb.h>
#include <resolv.h>
#include <string.h>
#include <support/check_nss.h>
#include <support/resolv_test.h>
#include <support/xthread.h>

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  bool include_both =  strcmp (qname, "both.example") == 0;
  bool include_a = qtype == T_A || include_both;
  bool include_aaaa = qtype == T_AAAA || include_both;

  resolv_response_init (b, (struct resolv_response_flags) {});
  resolv_response_add_question (b, qname, qclass, qtype);
  resolv_response_section (b, ns_s_an);
  if (include_a)
    {
      char ipv4[4] = {192, 0, 2, 17};
      resolv_response_open_record (b, qname, qclass, T_A, 0);
      resolv_response_add_data (b, &ipv4, sizeof (ipv4));
      resolv_response_close_record (b);
    }
  if (include_aaaa)
    {
        char ipv6[16]
          = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
        resolv_response_open_record (b, qname, qclass, T_AAAA, 0);
        resolv_response_add_data (b, &ipv6, sizeof (ipv6));
        resolv_response_close_record (b);
    }
}

/* Test that getaddrinfo is not influenced by RES_USE_INET6.  */
static void
test_gai (void)
{
  {
    struct addrinfo hints =
      {
        .ai_family = AF_UNSPEC,
        .ai_socktype = SOCK_STREAM,
        .ai_protocol = IPPROTO_TCP,
      };
    struct addrinfo *ai;
    int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
    check_addrinfo ("getaddrinfo AF_UNSPEC www1.example", ai, ret,
                    "address: STREAM/TCP 192.0.2.17 80\n"
                    "address: STREAM/TCP 2001:db8::1 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
    ret = getaddrinfo ("both.example", "80", &hints, &ai);
    /* Combined A/AAAA responses currently result in address
       duplication.  */
    check_addrinfo ("getaddrinfo AF_UNSPEC both.example", ai, ret,
                    "address: STREAM/TCP 192.0.2.17 80\n"
                    "address: STREAM/TCP 192.0.2.17 80\n"
                    "address: STREAM/TCP 2001:db8::1 80\n"
                    "address: STREAM/TCP 2001:db8::1 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
  }
  {
    struct addrinfo hints =
      {
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM,
        .ai_protocol = IPPROTO_TCP,
      };
    struct addrinfo *ai;
    int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
    check_addrinfo ("getaddrinfo AF_INET www1.example", ai, ret,
                    "address: STREAM/TCP 192.0.2.17 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
    ret = getaddrinfo ("both.example", "80", &hints, &ai);
    check_addrinfo ("getaddrinfo AF_INET both.example", ai, ret,
                    "address: STREAM/TCP 192.0.2.17 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
  }
  {
    struct addrinfo hints =
      {
        .ai_family = AF_INET6,
        .ai_socktype = SOCK_STREAM,
        .ai_protocol = IPPROTO_TCP,
      };
    struct addrinfo *ai;
    int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
    check_addrinfo ("getaddrinfo (AF_INET6)", ai, ret,
                    "address: STREAM/TCP 2001:db8::1 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
    ret = getaddrinfo ("both.example", "80", &hints, &ai);
    check_addrinfo ("getaddrinfo AF_INET6 both.example", ai, ret,
                    "address: STREAM/TCP 2001:db8::1 80\n");
    if (ret == 0)
      freeaddrinfo (ai);
  }
}

/* Test that gethostbyname2 is not influenced by RES_USE_INET6.  */
static void
test_get2 (void)
{
  check_hostent ("gethostbyname2 AF_INET www1.example",
                 gethostbyname2 ("www1.example", AF_INET),
                 "name: www1.example\n"
                 "address: 192.0.2.17\n");
  check_hostent ("gethostbyname2 AF_INET both.example",
                 gethostbyname2 ("both.example", AF_INET),
                 "name: both.example\n"
                 "address: 192.0.2.17\n");

  check_hostent ("gethostbyname2 AF_INET6 www1.example",
                 gethostbyname2 ("www1.example", AF_INET6),
                 "name: www1.example\n"
                 "address: 2001:db8::1\n");
  check_hostent ("gethostbyname2 AF_INET6 both.example",
                 gethostbyname2 ("both.example", AF_INET6),
                 "name: both.example\n"
                 "address: 2001:db8::1\n");
}

static void *
threadfunc (void *ignored)
{
  struct resolv_test *obj = resolv_test_start
    ((struct resolv_redirect_config)
     {
       .response_callback = response
     });

  check_hostent ("gethostbyname (\"www1.example\")",
                 gethostbyname ("www1.example"),
                 "name: www1.example\n"
                 "address: 192.0.2.17\n");
  check_hostent ("gethostbyname (\"both.example\")",
                 gethostbyname ("both.example"),
                 "name: both.example\n"
                 "address: 192.0.2.17\n");
  test_get2 ();
  test_gai ();

  _res.options |= RES_USE_INET6;
  check_hostent ("gethostbyname (\"www1.example\")",
                 gethostbyname ("www1.example"),
                 "name: www1.example\n"
                 "address: 2001:db8::1\n");
  check_hostent ("gethostbyname (\"both.example\")",
                 gethostbyname ("both.example"),
                 "name: both.example\n"
                 "address: 2001:db8::1\n");
  test_get2 ();
  test_gai ();

  resolv_test_end (obj);

  return NULL;
}

static int
do_test (void)
{
  resolv_test_init ();

  /* Attempt to run on a non-main thread first.  */
  {
    pthread_t thr = xpthread_create (NULL, threadfunc, NULL);
    xpthread_join (thr);
  }

  /* Try the main thread next.  */
  threadfunc (NULL);

  return 0;
}

#include <support/test-driver.c>