| 1 |
/* |
| 2 |
* Copyright (c) 1995 - 2016 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
|
| 35 |
#include <config.h> |
| 36 |
|
| 37 |
#include "roken.h" |
| 38 |
#include "getarg.h" |
| 39 |
#include <assert.h> |
| 40 |
#ifdef HAVE_ARPA_NAMESER_H |
| 41 |
#include <arpa/nameser.h> |
| 42 |
#endif |
| 43 |
#ifdef HAVE_RESOLV_H |
| 44 |
#include <resolv.h> |
| 45 |
#endif |
| 46 |
#include "resolve.h" |
| 47 |
|
| 48 |
static int srv_rr_order = 1; |
| 49 |
static int loop_integer = 1; |
| 50 |
static int version_flag = 0; |
| 51 |
static int help_flag = 0; |
| 52 |
|
| 53 |
static struct getargs args[] = { |
| 54 |
{"srv-rr-order", 0, |
| 55 |
arg_negative_flag, &srv_rr_order, |
| 56 |
"do not test SRV RR ordering", NULL }, |
| 57 |
{"loop", 0, arg_integer, &loop_integer, |
| 58 |
"loop resolving", NULL }, |
| 59 |
{"version", 0, arg_flag, &version_flag, |
| 60 |
"print version", NULL }, |
| 61 |
{"help", 0, arg_flag, &help_flag, |
| 62 |
NULL, NULL } |
| 63 |
}; |
| 64 |
|
| 65 |
static void |
| 66 |
usage (int ret) |
| 67 |
{ |
| 68 |
arg_printusage (args, |
| 69 |
sizeof(args)/sizeof(*args), |
| 70 |
NULL, |
| 71 |
"dns-record resource-record-type"); |
| 72 |
exit (ret); |
| 73 |
} |
| 74 |
|
| 75 |
#define NUMRRS 16 |
| 76 |
|
| 77 |
static |
| 78 |
int |
| 79 |
test_rk_dns_srv_order(size_t run) |
| 80 |
{ |
| 81 |
struct rk_dns_reply reply; |
| 82 |
struct rk_resource_record rrs[NUMRRS]; |
| 83 |
struct rk_resource_record *rr; |
| 84 |
struct rk_srv_record srvs[NUMRRS]; |
| 85 |
size_t i, prio0; |
| 86 |
int fail = 0; |
| 87 |
|
| 88 |
(void) memset(&reply, 0, sizeof(reply)); |
| 89 |
(void) memset(srvs, 0, sizeof(srvs)); |
| 90 |
(void) memset(rrs, 0, sizeof(rrs)); |
| 91 |
|
| 92 |
/* Test with two equal weight zero SRV records */ |
| 93 |
rrs[0].type = rk_ns_t_srv; |
| 94 |
rrs[0].u.srv = &srvs[0]; |
| 95 |
rrs[0].next = &rrs[1]; |
| 96 |
srvs[0].priority = 10; |
| 97 |
srvs[0].weight = 0; |
| 98 |
|
| 99 |
rrs[1].type = rk_ns_t_srv; |
| 100 |
rrs[1].u.srv = &srvs[1]; |
| 101 |
rrs[1].next = NULL; |
| 102 |
srvs[1].priority = 10; |
| 103 |
srvs[1].weight = 0; |
| 104 |
reply.head = &rrs[0]; |
| 105 |
|
| 106 |
rk_dns_srv_order(&reply); |
| 107 |
assert(reply.head != NULL); |
| 108 |
printf("%p %p\n", &rrs[0], rrs[0].next); |
| 109 |
|
| 110 |
/* |
| 111 |
* Test four priority groups with priority 1--5 and weigths 0--3 in the |
| 112 |
* first two groups, and 1--4 in the last two groups. Test multiple zero |
| 113 |
* weights, by further coercing the weight to zero if <= run/2. |
| 114 |
*/ |
| 115 |
for (i = 0; i < NUMRRS; i++) { |
| 116 |
rrs[i].type = rk_ns_t_srv; |
| 117 |
rrs[i].u.srv = &srvs[i]; |
| 118 |
srvs[i].priority = 1 + i / 4; |
| 119 |
srvs[i].weight = i % 4 + i / 8; |
| 120 |
if (srvs[i].weight <= run/2) |
| 121 |
srvs[i].weight = 0; |
| 122 |
} |
| 123 |
/* Shuffle the RRs */ |
| 124 |
for (i = 0; i < NUMRRS - 1; i++) { |
| 125 |
struct rk_resource_record tmp; |
| 126 |
size_t j = rk_random() % (NUMRRS - i); |
| 127 |
|
| 128 |
if (j > 0) { |
| 129 |
tmp = rrs[i+j]; |
| 130 |
rrs[i+j] = rrs[i]; |
| 131 |
rrs[i] = tmp; |
| 132 |
} |
| 133 |
} |
| 134 |
for (i = 0; i < NUMRRS; i++) |
| 135 |
rrs[i].next = &rrs[i + 1]; |
| 136 |
rrs[i - 1].next = NULL; |
| 137 |
reply.head = &rrs[0]; |
| 138 |
|
| 139 |
for (i = 0, rr = reply.head; i < NUMRRS; i++) { |
| 140 |
if (rr == NULL) |
| 141 |
break; |
| 142 |
printf("SRV RR order run %lu input: prio %lu weight %lu\n", |
| 143 |
(unsigned long)run, (unsigned long)rr->u.srv->priority, |
| 144 |
(unsigned long)rr->u.srv->weight); |
| 145 |
rr = rr->next; |
| 146 |
} |
| 147 |
|
| 148 |
rk_dns_srv_order(&reply); |
| 149 |
assert(reply.head != NULL); |
| 150 |
|
| 151 |
/* |
| 152 |
* After sorting, ensure monotone priority ordering with jumps by 1 at |
| 153 |
* group boundaries. |
| 154 |
*/ |
| 155 |
prio0 = 0; |
| 156 |
for (i = 0, rr = reply.head; i < NUMRRS; i++) { |
| 157 |
if (rr == NULL) |
| 158 |
break; |
| 159 |
if (rr->u.srv->priority < prio0 || |
| 160 |
(rr->u.srv->priority != prio0 && |
| 161 |
(i % 4 != 0 || rr->u.srv->priority > prio0 + 1))) { |
| 162 |
printf("SRV RR order run %lu failed\n", run); |
| 163 |
fail = 1; |
| 164 |
} |
| 165 |
prio0 = rr->u.srv->priority; |
| 166 |
printf("SRV RR order run %lu output: prio %lu weight %lu\n", |
| 167 |
(unsigned long)run, (unsigned long)rr->u.srv->priority, |
| 168 |
(unsigned long)rr->u.srv->weight); |
| 169 |
rr = rr->next; |
| 170 |
} |
| 171 |
assert(i == NUMRRS); |
| 172 |
|
| 173 |
return fail; |
| 174 |
} |
| 175 |
|
| 176 |
int |
| 177 |
main(int argc, char **argv) |
| 178 |
{ |
| 179 |
struct rk_dns_reply *r; |
| 180 |
struct rk_resource_record *rr; |
| 181 |
int optidx = 0, i, exit_code = 0; |
| 182 |
|
| 183 |
setprogname (argv[0]); |
| 184 |
rk_random_init(); |
| 185 |
|
| 186 |
if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) |
| 187 |
usage(1); |
| 188 |
|
| 189 |
if (help_flag) |
| 190 |
usage (0); |
| 191 |
|
| 192 |
if (version_flag) { |
| 193 |
printf("some version\n"); |
| 194 |
exit(0); |
| 195 |
} |
| 196 |
|
| 197 |
argc -= optidx; |
| 198 |
argv += optidx; |
| 199 |
|
| 200 |
if (argc != 2 && argc != 0 && !srv_rr_order) |
| 201 |
usage(1); |
| 202 |
|
| 203 |
if (srv_rr_order) { |
| 204 |
exit_code += test_rk_dns_srv_order(0); |
| 205 |
exit_code += test_rk_dns_srv_order(1); |
| 206 |
exit_code += test_rk_dns_srv_order(2); |
| 207 |
exit_code += test_rk_dns_srv_order(3); |
| 208 |
exit_code += test_rk_dns_srv_order(4); |
| 209 |
exit_code += test_rk_dns_srv_order(5); |
| 210 |
} |
| 211 |
|
| 212 |
if (srv_rr_order && argc == 0) |
| 213 |
exit(exit_code ? 1 : 0); |
| 214 |
|
| 215 |
if (argc != 2) |
| 216 |
usage(1); |
| 217 |
|
| 218 |
for (i = 0; i < loop_integer; i++) { |
| 219 |
|
| 220 |
r = rk_dns_lookup(argv[0], argv[1]); |
| 221 |
if (r == NULL) { |
| 222 |
printf("No reply.\n"); |
| 223 |
exit_code = 1; |
| 224 |
break; |
| 225 |
} |
| 226 |
if (r->q.type == rk_ns_t_srv) |
| 227 |
rk_dns_srv_order(r); |
| 228 |
|
| 229 |
for (rr = r->head; rr;rr=rr->next) { |
| 230 |
printf("%-30s %-5s %-6d ", rr->domain, rk_dns_type_to_string(rr->type), rr->ttl); |
| 231 |
switch (rr->type) { |
| 232 |
case rk_ns_t_ns: |
| 233 |
case rk_ns_t_cname: |
| 234 |
case rk_ns_t_ptr: |
| 235 |
printf("%s\n", (char*)rr->u.data); |
| 236 |
break; |
| 237 |
case rk_ns_t_a: |
| 238 |
printf("%s\n", inet_ntoa(*rr->u.a)); |
| 239 |
break; |
| 240 |
case rk_ns_t_mx: |
| 241 |
case rk_ns_t_afsdb: { |
| 242 |
printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain); |
| 243 |
break; |
| 244 |
} |
| 245 |
case rk_ns_t_srv: { |
| 246 |
struct rk_srv_record *srv = rr->u.srv; |
| 247 |
printf("%d %d %d %s\n", srv->priority, srv->weight, |
| 248 |
srv->port, srv->target); |
| 249 |
break; |
| 250 |
} |
| 251 |
case rk_ns_t_txt: { |
| 252 |
printf("%s\n", rr->u.txt); |
| 253 |
break; |
| 254 |
} |
| 255 |
case rk_ns_t_sig: { |
| 256 |
struct rk_sig_record *sig = rr->u.sig; |
| 257 |
const char *type_string = rk_dns_type_to_string (sig->type); |
| 258 |
|
| 259 |
printf("type %u (%s), algorithm %u, labels %u, orig_ttl %u, " |
| 260 |
"sig_expiration %u, sig_inception %u, key_tag %u, " |
| 261 |
"signer %s\n", |
| 262 |
sig->type, type_string ? type_string : "", |
| 263 |
sig->algorithm, sig->labels, sig->orig_ttl, |
| 264 |
sig->sig_expiration, sig->sig_inception, sig->key_tag, |
| 265 |
sig->signer); |
| 266 |
break; |
| 267 |
} |
| 268 |
case rk_ns_t_key: { |
| 269 |
struct rk_key_record *key = rr->u.key; |
| 270 |
|
| 271 |
printf("flags %u, protocol %u, algorithm %u\n", |
| 272 |
key->flags, key->protocol, key->algorithm); |
| 273 |
break; |
| 274 |
} |
| 275 |
case rk_ns_t_sshfp: { |
| 276 |
struct rk_sshfp_record *sshfp = rr->u.sshfp; |
| 277 |
size_t j; |
| 278 |
|
| 279 |
printf ("alg %u type %u length %lu data ", sshfp->algorithm, |
| 280 |
sshfp->type, (unsigned long)sshfp->sshfp_len); |
| 281 |
for (j = 0; j < sshfp->sshfp_len; j++) |
| 282 |
printf("%02X", sshfp->sshfp_data[j]); |
| 283 |
printf("\n"); |
| 284 |
|
| 285 |
break; |
| 286 |
} |
| 287 |
case rk_ns_t_ds: { |
| 288 |
struct rk_ds_record *ds = rr->u.ds; |
| 289 |
size_t j; |
| 290 |
|
| 291 |
printf("key tag %u alg %u type %u length %lu data ", |
| 292 |
ds->key_tag, ds->algorithm, ds->digest_type, |
| 293 |
(unsigned long)ds->digest_len); |
| 294 |
for (j = 0; j < ds->digest_len; j++) |
| 295 |
printf("%02X", ds->digest_data[j]); |
| 296 |
printf("\n"); |
| 297 |
|
| 298 |
break; |
| 299 |
} |
| 300 |
default: |
| 301 |
printf("\n"); |
| 302 |
break; |
| 303 |
} |
| 304 |
} |
| 305 |
rk_dns_free_data(r); |
| 306 |
} |
| 307 |
|
| 308 |
return exit_code ? 1 : 0; |
| 309 |
} |