| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
/*! \file */ |
| 15 |
|
| 16 |
#include <inttypes.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <time.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#include <isc/buffer.h> |
| 23 |
#include <isc/file.h> |
| 24 |
#include <isc/hash.h> |
| 25 |
#include <isc/job.h> |
| 26 |
#include <isc/loop.h> |
| 27 |
#include <isc/managers.h> |
| 28 |
#include <isc/mem.h> |
| 29 |
#include <isc/netmgr.h> |
| 30 |
#include <isc/os.h> |
| 31 |
#include <isc/random.h> |
| 32 |
#include <isc/result.h> |
| 33 |
#include <isc/stdio.h> |
| 34 |
#include <isc/string.h> |
| 35 |
#include <isc/timer.h> |
| 36 |
#include <isc/util.h> |
| 37 |
|
| 38 |
#include <dns/cache.h> |
| 39 |
#include <dns/db.h> |
| 40 |
#include <dns/dispatch.h> |
| 41 |
#include <dns/fixedname.h> |
| 42 |
#include <dns/log.h> |
| 43 |
#include <dns/name.h> |
| 44 |
#include <dns/view.h> |
| 45 |
#include <dns/zone.h> |
| 46 |
|
| 47 |
#include <ns/client.h> |
| 48 |
#include <ns/hooks.h> |
| 49 |
#include <ns/interfacemgr.h> |
| 50 |
#include <ns/server.h> |
| 51 |
|
| 52 |
#include <tests/ns.h> |
| 53 |
|
| 54 |
dns_dispatchmgr_t *dispatchmgr = NULL; |
| 55 |
ns_interfacemgr_t *interfacemgr = NULL; |
| 56 |
ns_server_t *sctx = NULL; |
| 57 |
|
| 58 |
static isc_result_t |
| 59 |
matchview(isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr, |
| 60 |
dns_message_t *message, dns_aclenv_t *env, ns_server_t *lsctx, |
| 61 |
isc_loop_t *loop, isc_job_cb cb, void *cbarg, |
| 62 |
isc_result_t *sigresultp, isc_result_t *viewmatchresultp, |
| 63 |
dns_view_t **viewp) { |
| 64 |
UNUSED(srcaddr); |
| 65 |
UNUSED(destaddr); |
| 66 |
UNUSED(message); |
| 67 |
UNUSED(env); |
| 68 |
UNUSED(lsctx); |
| 69 |
UNUSED(loop); |
| 70 |
UNUSED(cb); |
| 71 |
UNUSED(cbarg); |
| 72 |
UNUSED(sigresultp); |
| 73 |
UNUSED(viewp); |
| 74 |
|
| 75 |
*viewmatchresultp = ISC_R_NOTIMPLEMENTED; |
| 76 |
return ISC_R_NOTIMPLEMENTED; |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
scan_interfaces(void *arg) { |
| 81 |
UNUSED(arg); |
| 82 |
ns_interfacemgr_scan(interfacemgr, true, false); |
| 83 |
} |
| 84 |
|
| 85 |
int |
| 86 |
setup_server(void **state) { |
| 87 |
isc_result_t result; |
| 88 |
ns_listenlist_t *listenon = NULL; |
| 89 |
in_port_t port = 5300 + isc_random8(); |
| 90 |
|
| 91 |
setup_managers(state); |
| 92 |
|
| 93 |
ns_server_create(mctx, matchview, &sctx); |
| 94 |
|
| 95 |
result = dns_dispatchmgr_create(mctx, loopmgr, netmgr, &dispatchmgr); |
| 96 |
if (result != ISC_R_SUCCESS) { |
| 97 |
goto cleanup; |
| 98 |
} |
| 99 |
|
| 100 |
result = ns_interfacemgr_create(mctx, sctx, loopmgr, netmgr, |
| 101 |
dispatchmgr, NULL, &interfacemgr); |
| 102 |
if (result != ISC_R_SUCCESS) { |
| 103 |
goto cleanup; |
| 104 |
} |
| 105 |
|
| 106 |
result = ns_listenlist_default(mctx, port, true, AF_INET, &listenon); |
| 107 |
if (result != ISC_R_SUCCESS) { |
| 108 |
goto cleanup; |
| 109 |
} |
| 110 |
|
| 111 |
ns_interfacemgr_setlistenon4(interfacemgr, listenon); |
| 112 |
ns_listenlist_detach(&listenon); |
| 113 |
|
| 114 |
isc_loop_setup(mainloop, scan_interfaces, NULL); |
| 115 |
|
| 116 |
return 0; |
| 117 |
|
| 118 |
cleanup: |
| 119 |
teardown_server(state); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
shutdown_interfacemgr(void *arg ISC_ATTR_UNUSED) { |
| 125 |
if (interfacemgr != NULL) { |
| 126 |
ns_interfacemgr_shutdown(interfacemgr); |
| 127 |
ns_interfacemgr_detach(&interfacemgr); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
int |
| 132 |
teardown_server(void **state) { |
| 133 |
shutdown_interfacemgr(NULL); |
| 134 |
|
| 135 |
if (dispatchmgr != NULL) { |
| 136 |
dns_dispatchmgr_detach(&dispatchmgr); |
| 137 |
} |
| 138 |
|
| 139 |
if (sctx != NULL) { |
| 140 |
ns_server_detach(&sctx); |
| 141 |
} |
| 142 |
|
| 143 |
teardown_managers(state); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
static dns_zone_t *served_zone = NULL; |
| 148 |
|
| 149 |
isc_result_t |
| 150 |
ns_test_serve_zone(const char *zonename, const char *filename, |
| 151 |
dns_view_t *view) { |
| 152 |
isc_result_t result; |
| 153 |
dns_db_t *db = NULL; |
| 154 |
|
| 155 |
/* |
| 156 |
* Prepare zone structure for further processing. |
| 157 |
*/ |
| 158 |
result = dns_test_makezone(zonename, &served_zone, view, false); |
| 159 |
if (result != ISC_R_SUCCESS) { |
| 160 |
return result; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Start zone manager. |
| 165 |
*/ |
| 166 |
dns_test_setupzonemgr(); |
| 167 |
|
| 168 |
/* |
| 169 |
* Add the zone to the zone manager. |
| 170 |
*/ |
| 171 |
result = dns_test_managezone(served_zone); |
| 172 |
if (result != ISC_R_SUCCESS) { |
| 173 |
goto close_zonemgr; |
| 174 |
} |
| 175 |
|
| 176 |
view->nocookieudp = 512; |
| 177 |
|
| 178 |
/* |
| 179 |
* Set path to the master file for the zone and then load it. |
| 180 |
*/ |
| 181 |
dns_zone_setfile(served_zone, filename, dns_masterformat_text, |
| 182 |
&dns_master_style_default); |
| 183 |
result = dns_zone_load(served_zone, false); |
| 184 |
if (result != ISC_R_SUCCESS) { |
| 185 |
goto release_zone; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* The zone should now be loaded; test it. |
| 190 |
*/ |
| 191 |
result = dns_zone_getdb(served_zone, &db); |
| 192 |
if (result != ISC_R_SUCCESS) { |
| 193 |
goto release_zone; |
| 194 |
} |
| 195 |
if (db != NULL) { |
| 196 |
dns_db_detach(&db); |
| 197 |
} |
| 198 |
|
| 199 |
return ISC_R_SUCCESS; |
| 200 |
|
| 201 |
release_zone: |
| 202 |
dns_test_releasezone(served_zone); |
| 203 |
close_zonemgr: |
| 204 |
dns_test_closezonemgr(); |
| 205 |
dns_zone_detach(&served_zone); |
| 206 |
|
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
ns_test_cleanup_zone(void) { |
| 212 |
dns_test_releasezone(served_zone); |
| 213 |
dns_test_closezonemgr(); |
| 214 |
|
| 215 |
dns_zone_detach(&served_zone); |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
ns_test_getclient(ns_interface_t *ifp0, bool tcp, ns_client_t **clientp) { |
| 220 |
ns_client_t *client; |
| 221 |
ns_clientmgr_t *clientmgr; |
| 222 |
int i; |
| 223 |
|
| 224 |
UNUSED(ifp0); |
| 225 |
UNUSED(tcp); |
| 226 |
|
| 227 |
clientmgr = ns_interfacemgr_getclientmgr(interfacemgr); |
| 228 |
|
| 229 |
client = isc_mem_get(clientmgr->mctx, sizeof(*client)); |
| 230 |
ns__client_setup(client, clientmgr, true); |
| 231 |
|
| 232 |
for (i = 0; i < 32; i++) { |
| 233 |
if (atomic_load(&client_addrs[i]) == (uintptr_t)NULL || |
| 234 |
atomic_load(&client_addrs[i]) == (uintptr_t)client) |
| 235 |
{ |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
REQUIRE(i < 32); |
| 240 |
|
| 241 |
atomic_store(&client_refs[i], 2); |
| 242 |
atomic_store(&client_addrs[i], (uintptr_t)client); |
| 243 |
client->handle = (isc_nmhandle_t *)client; /* Hack */ |
| 244 |
*clientp = client; |
| 245 |
} |
| 246 |
|
| 247 |
/*% |
| 248 |
* Synthesize a DNS message based on supplied QNAME, QTYPE and flags, then |
| 249 |
* parse it and store the results in client->message. |
| 250 |
*/ |
| 251 |
static isc_result_t |
| 252 |
attach_query_msg_to_client(ns_client_t *client, const char *qnamestr, |
| 253 |
dns_rdatatype_t qtype, unsigned int qflags) { |
| 254 |
dns_rdataset_t *qrdataset = NULL; |
| 255 |
dns_message_t *message = NULL; |
| 256 |
unsigned char query[65535]; |
| 257 |
dns_name_t *qname = NULL; |
| 258 |
isc_buffer_t querybuf; |
| 259 |
dns_compress_t cctx; |
| 260 |
isc_result_t result; |
| 261 |
|
| 262 |
REQUIRE(client != NULL); |
| 263 |
REQUIRE(qnamestr != NULL); |
| 264 |
|
| 265 |
/* |
| 266 |
* Create a new DNS message holding a query. |
| 267 |
*/ |
| 268 |
dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTRENDER, |
| 269 |
&message); |
| 270 |
|
| 271 |
/* |
| 272 |
* Set query ID to a random value. |
| 273 |
*/ |
| 274 |
message->id = isc_random16(); |
| 275 |
|
| 276 |
/* |
| 277 |
* Set query flags as requested by the caller. |
| 278 |
*/ |
| 279 |
message->flags = qflags; |
| 280 |
|
| 281 |
/* |
| 282 |
* Allocate structures required to construct the query. |
| 283 |
*/ |
| 284 |
dns_message_gettemprdataset(message, &qrdataset); |
| 285 |
dns_message_gettempname(message, &qname); |
| 286 |
|
| 287 |
/* |
| 288 |
* Convert "qnamestr" to a DNS name, create a question rdataset of |
| 289 |
* class IN and type "qtype", link the two and add the result to the |
| 290 |
* QUESTION section of the query. |
| 291 |
*/ |
| 292 |
result = dns_name_fromstring(qname, qnamestr, dns_rootname, 0, mctx); |
| 293 |
if (result != ISC_R_SUCCESS) { |
| 294 |
goto put_name; |
| 295 |
} |
| 296 |
dns_rdataset_makequestion(qrdataset, dns_rdataclass_in, qtype); |
| 297 |
ISC_LIST_APPEND(qname->list, qrdataset, link); |
| 298 |
dns_message_addname(message, qname, DNS_SECTION_QUESTION); |
| 299 |
|
| 300 |
/* |
| 301 |
* Render the query. |
| 302 |
*/ |
| 303 |
dns_compress_init(&cctx, mctx, 0); |
| 304 |
isc_buffer_init(&querybuf, query, sizeof(query)); |
| 305 |
result = dns_message_renderbegin(message, &cctx, &querybuf); |
| 306 |
if (result != ISC_R_SUCCESS) { |
| 307 |
goto destroy_message; |
| 308 |
} |
| 309 |
result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0); |
| 310 |
if (result != ISC_R_SUCCESS) { |
| 311 |
goto destroy_message; |
| 312 |
} |
| 313 |
result = dns_message_renderend(message); |
| 314 |
if (result != ISC_R_SUCCESS) { |
| 315 |
goto destroy_message; |
| 316 |
} |
| 317 |
dns_compress_invalidate(&cctx); |
| 318 |
|
| 319 |
/* |
| 320 |
* Destroy the created message as it was rendered into "querybuf" and |
| 321 |
* the latter is all we are going to need from now on. |
| 322 |
*/ |
| 323 |
dns_message_detach(&message); |
| 324 |
|
| 325 |
/* |
| 326 |
* Parse the rendered query, storing results in client->message. |
| 327 |
*/ |
| 328 |
isc_buffer_first(&querybuf); |
| 329 |
return dns_message_parse(client->message, &querybuf, 0); |
| 330 |
|
| 331 |
put_name: |
| 332 |
dns_message_puttempname(message, &qname); |
| 333 |
dns_message_puttemprdataset(message, &qrdataset); |
| 334 |
destroy_message: |
| 335 |
dns_message_detach(&message); |
| 336 |
|
| 337 |
return result; |
| 338 |
} |
| 339 |
|
| 340 |
/*% |
| 341 |
* A hook action which stores the query context pointed to by "arg" at |
| 342 |
* "data". Causes execution to be interrupted at hook insertion |
| 343 |
* point. |
| 344 |
*/ |
| 345 |
static ns_hookresult_t |
| 346 |
extract_qctx(void *arg, void *data, isc_result_t *resultp) { |
| 347 |
query_ctx_t **qctxp; |
| 348 |
query_ctx_t *qctx; |
| 349 |
|
| 350 |
REQUIRE(arg != NULL); |
| 351 |
REQUIRE(data != NULL); |
| 352 |
REQUIRE(resultp != NULL); |
| 353 |
|
| 354 |
/* |
| 355 |
* qctx is a stack variable in lib/ns/query.c. Its contents need to be |
| 356 |
* duplicated or otherwise they will become invalidated once the stack |
| 357 |
* gets unwound. |
| 358 |
*/ |
| 359 |
qctx = isc_mem_get(mctx, sizeof(*qctx)); |
| 360 |
if (qctx != NULL) { |
| 361 |
memmove(qctx, (query_ctx_t *)arg, sizeof(*qctx)); |
| 362 |
} |
| 363 |
|
| 364 |
qctxp = (query_ctx_t **)data; |
| 365 |
/* |
| 366 |
* If memory allocation failed, the supplied pointer will simply be set |
| 367 |
* to NULL. We rely on the user of this hook to react properly. |
| 368 |
*/ |
| 369 |
*qctxp = qctx; |
| 370 |
*resultp = ISC_R_UNSET; |
| 371 |
|
| 372 |
return NS_HOOK_RETURN; |
| 373 |
} |
| 374 |
|
| 375 |
/*% |
| 376 |
* Initialize a query context for "client" and store it in "qctxp". |
| 377 |
* |
| 378 |
* Requires: |
| 379 |
* |
| 380 |
* \li "client->message" to hold a parsed DNS query. |
| 381 |
*/ |
| 382 |
static isc_result_t |
| 383 |
create_qctx_for_client(ns_client_t *client, query_ctx_t **qctxp) { |
| 384 |
ns_hooktable_t *saved_hook_table = NULL, *query_hooks = NULL; |
| 385 |
const ns_hook_t hook = { |
| 386 |
.action = extract_qctx, |
| 387 |
.action_data = qctxp, |
| 388 |
}; |
| 389 |
|
| 390 |
REQUIRE(client != NULL); |
| 391 |
REQUIRE(qctxp != NULL); |
| 392 |
REQUIRE(*qctxp == NULL); |
| 393 |
|
| 394 |
/* |
| 395 |
* Call ns_query_start() to initialize a query context for given |
| 396 |
* client, but first hook into query_setup() so that we can just |
| 397 |
* extract an initialized query context, without kicking off any |
| 398 |
* further processing. Make sure we do not overwrite any previously |
| 399 |
* set hooks. |
| 400 |
*/ |
| 401 |
|
| 402 |
ns_hooktable_create(mctx, &query_hooks); |
| 403 |
ns_hook_add(query_hooks, mctx, NS_QUERY_SETUP, &hook); |
| 404 |
|
| 405 |
saved_hook_table = ns__hook_table; |
| 406 |
ns__hook_table = query_hooks; |
| 407 |
|
| 408 |
ns_query_start(client, client->handle); |
| 409 |
|
| 410 |
ns__hook_table = saved_hook_table; |
| 411 |
ns_hooktable_free(mctx, (void **)&query_hooks); |
| 412 |
|
| 413 |
isc_nmhandle_detach(&client->reqhandle); |
| 414 |
|
| 415 |
if (*qctxp == NULL) { |
| 416 |
return ISC_R_NOMEMORY; |
| 417 |
} else { |
| 418 |
return ISC_R_SUCCESS; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
isc_result_t |
| 423 |
ns_test_qctx_create(const ns_test_qctx_create_params_t *params, |
| 424 |
query_ctx_t **qctxp) { |
| 425 |
ns_client_t *client = NULL; |
| 426 |
isc_result_t result; |
| 427 |
isc_nmhandle_t *handle = NULL; |
| 428 |
|
| 429 |
REQUIRE(params != NULL); |
| 430 |
REQUIRE(params->qname != NULL); |
| 431 |
REQUIRE(qctxp != NULL); |
| 432 |
REQUIRE(*qctxp == NULL); |
| 433 |
|
| 434 |
/* |
| 435 |
* Allocate and initialize a client structure. |
| 436 |
*/ |
| 437 |
ns_test_getclient(NULL, false, &client); |
| 438 |
client->tnow = isc_time_now(); |
| 439 |
|
| 440 |
/* |
| 441 |
* Every client needs to belong to a view. |
| 442 |
*/ |
| 443 |
result = dns_test_makeview("view", false, params->with_cache, |
| 444 |
&client->view); |
| 445 |
if (result != ISC_R_SUCCESS) { |
| 446 |
goto detach_client; |
| 447 |
} |
| 448 |
|
| 449 |
/* |
| 450 |
* Synthesize a DNS query using given QNAME, QTYPE and flags, storing |
| 451 |
* it in client->message. |
| 452 |
*/ |
| 453 |
result = attach_query_msg_to_client(client, params->qname, |
| 454 |
params->qtype, params->qflags); |
| 455 |
if (result != ISC_R_SUCCESS) { |
| 456 |
goto detach_view; |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
* Allow recursion for the client. As NS_CLIENTATTR_RA normally gets |
| 461 |
* set in ns_client_request(), i.e. earlier than the unit tests hook |
| 462 |
* into the call chain, just set it manually. |
| 463 |
*/ |
| 464 |
client->attributes |= NS_CLIENTATTR_RA; |
| 465 |
|
| 466 |
/* |
| 467 |
* Create a query context for a client sending the previously |
| 468 |
* synthesized query. |
| 469 |
*/ |
| 470 |
result = create_qctx_for_client(client, qctxp); |
| 471 |
if (result != ISC_R_SUCCESS) { |
| 472 |
goto detach_query; |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
* The reference count for "client" is now at 2, so we need to |
| 477 |
* decrement it in order for it to drop to zero when "qctx" gets |
| 478 |
* destroyed. |
| 479 |
*/ |
| 480 |
handle = client->handle; |
| 481 |
isc_nmhandle_detach(&handle); |
| 482 |
|
| 483 |
return ISC_R_SUCCESS; |
| 484 |
|
| 485 |
detach_query: |
| 486 |
dns_message_detach(&client->message); |
| 487 |
detach_view: |
| 488 |
dns_view_detach(&client->view); |
| 489 |
detach_client: |
| 490 |
isc_nmhandle_detach(&client->handle); |
| 491 |
|
| 492 |
return result; |
| 493 |
} |
| 494 |
|
| 495 |
void |
| 496 |
ns_test_qctx_destroy(query_ctx_t **qctxp) { |
| 497 |
query_ctx_t *qctx; |
| 498 |
|
| 499 |
REQUIRE(qctxp != NULL); |
| 500 |
REQUIRE(*qctxp != NULL); |
| 501 |
|
| 502 |
qctx = *qctxp; |
| 503 |
*qctxp = NULL; |
| 504 |
|
| 505 |
if (qctx->zone != NULL) { |
| 506 |
dns_zone_detach(&qctx->zone); |
| 507 |
} |
| 508 |
if (qctx->db != NULL) { |
| 509 |
dns_db_detach(&qctx->db); |
| 510 |
} |
| 511 |
if (qctx->client != NULL) { |
| 512 |
isc_nmhandle_detach(&qctx->client->handle); |
| 513 |
} |
| 514 |
|
| 515 |
isc_mem_put(mctx, qctx, sizeof(*qctx)); |
| 516 |
} |
| 517 |
|
| 518 |
ns_hookresult_t |
| 519 |
ns_test_hook_catch_call(void *arg, void *data, isc_result_t *resultp) { |
| 520 |
UNUSED(arg); |
| 521 |
UNUSED(data); |
| 522 |
|
| 523 |
*resultp = ISC_R_UNSET; |
| 524 |
|
| 525 |
return NS_HOOK_RETURN; |
| 526 |
} |
| 527 |
|
| 528 |
isc_result_t |
| 529 |
ns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin, |
| 530 |
const char *testfile) { |
| 531 |
isc_result_t result; |
| 532 |
dns_fixedname_t fixed; |
| 533 |
dns_name_t *name = NULL; |
| 534 |
const char *dbimp = (dbtype == dns_dbtype_zone) ? ZONEDB_DEFAULT |
| 535 |
: CACHEDB_DEFAULT; |
| 536 |
|
| 537 |
name = dns_fixedname_initname(&fixed); |
| 538 |
|
| 539 |
result = dns_name_fromstring(name, origin, dns_rootname, 0, NULL); |
| 540 |
if (result != ISC_R_SUCCESS) { |
| 541 |
return result; |
| 542 |
} |
| 543 |
|
| 544 |
result = dns_db_create(mctx, dbimp, name, dbtype, dns_rdataclass_in, 0, |
| 545 |
NULL, db); |
| 546 |
if (result != ISC_R_SUCCESS) { |
| 547 |
return result; |
| 548 |
} |
| 549 |
|
| 550 |
result = dns_db_load(*db, testfile, dns_masterformat_text, 0); |
| 551 |
return result; |
| 552 |
} |
| 553 |
|
| 554 |
static int |
| 555 |
fromhex(char c) { |
| 556 |
if (c >= '0' && c <= '9') { |
| 557 |
return c - '0'; |
| 558 |
} else if (c >= 'a' && c <= 'f') { |
| 559 |
return c - 'a' + 10; |
| 560 |
} else if (c >= 'A' && c <= 'F') { |
| 561 |
return c - 'A' + 10; |
| 562 |
} |
| 563 |
|
| 564 |
printf("bad input format: %02x\n", c); |
| 565 |
exit(3); |
| 566 |
} |
| 567 |
|
| 568 |
isc_result_t |
| 569 |
ns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz, |
| 570 |
size_t *sizep) { |
| 571 |
isc_result_t result; |
| 572 |
unsigned char *bp; |
| 573 |
char *rp, *wp; |
| 574 |
char s[BUFSIZ]; |
| 575 |
size_t len, i; |
| 576 |
FILE *f = NULL; |
| 577 |
int n; |
| 578 |
|
| 579 |
result = isc_stdio_open(file, "r", &f); |
| 580 |
if (result != ISC_R_SUCCESS) { |
| 581 |
return result; |
| 582 |
} |
| 583 |
|
| 584 |
bp = buf; |
| 585 |
while (fgets(s, sizeof(s), f) != NULL) { |
| 586 |
rp = s; |
| 587 |
wp = s; |
| 588 |
len = 0; |
| 589 |
while (*rp != '\0') { |
| 590 |
if (*rp == '#') { |
| 591 |
break; |
| 592 |
} |
| 593 |
if (*rp != ' ' && *rp != '\t' && *rp != '\r' && |
| 594 |
*rp != '\n') |
| 595 |
{ |
| 596 |
*wp++ = *rp; |
| 597 |
len++; |
| 598 |
} |
| 599 |
rp++; |
| 600 |
} |
| 601 |
if (len == 0U) { |
| 602 |
continue; |
| 603 |
} |
| 604 |
if (len % 2 != 0U) { |
| 605 |
result = ISC_R_UNEXPECTEDEND; |
| 606 |
goto cleanup; |
| 607 |
} |
| 608 |
if (len > bufsiz * 2) { |
| 609 |
result = ISC_R_NOSPACE; |
| 610 |
goto cleanup; |
| 611 |
} |
| 612 |
rp = s; |
| 613 |
for (i = 0; i < len; i += 2) { |
| 614 |
n = fromhex(*rp++); |
| 615 |
n *= 16; |
| 616 |
n += fromhex(*rp++); |
| 617 |
*bp++ = n; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
*sizep = bp - buf; |
| 622 |
|
| 623 |
result = ISC_R_SUCCESS; |
| 624 |
|
| 625 |
cleanup: |
| 626 |
isc_stdio_close(f); |
| 627 |
return result; |
| 628 |
} |