2000-01-14 20:22:28 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 1999 Internet Software Consortium.
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2000-01-14 21:55:44 +00:00
|
|
|
#include <assert.h>
|
2000-01-14 20:22:28 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2000-02-01 00:29:03 +00:00
|
|
|
#include <isc/assertions.h>
|
|
|
|
|
#include <isc/error.h>
|
|
|
|
|
#include <isc/mem.h>
|
|
|
|
|
#include <isc/netaddr.h>
|
|
|
|
|
|
2000-01-14 20:22:28 +00:00
|
|
|
#include <lwres/context.h>
|
|
|
|
|
#include <lwres/lwbuffer.h>
|
|
|
|
|
#include <lwres/lwres.h>
|
|
|
|
|
#include <lwres/lwpacket.h>
|
|
|
|
|
|
2000-02-01 02:58:55 +00:00
|
|
|
#define USE_ISC_MEM
|
|
|
|
|
|
2000-01-14 21:55:44 +00:00
|
|
|
static inline void
|
|
|
|
|
CHECK(int val, char *msg)
|
|
|
|
|
{
|
|
|
|
|
if (val != 0) {
|
|
|
|
|
fprintf(stderr, "%s returned %d\n", msg, val);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
hexdump(char *msg, void *base, size_t len)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *p;
|
|
|
|
|
unsigned int cnt;
|
|
|
|
|
|
|
|
|
|
p = base;
|
|
|
|
|
cnt = 0;
|
|
|
|
|
|
|
|
|
|
printf("*** %s (%u bytes @ %p)\n", msg, len, base);
|
|
|
|
|
|
|
|
|
|
while (cnt < len) {
|
|
|
|
|
if (cnt % 16 == 0)
|
|
|
|
|
printf("%p: ", p);
|
|
|
|
|
else if (cnt % 8 == 0)
|
|
|
|
|
printf(" |");
|
|
|
|
|
printf(" %02x", *p++);
|
|
|
|
|
cnt++;
|
|
|
|
|
|
|
|
|
|
if (cnt % 16 == 0)
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cnt % 16 != 0)
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *TESTSTRING = "This is a test. This is only a test. !!!";
|
2000-01-18 01:43:12 +00:00
|
|
|
static lwres_context_t *ctx;
|
2000-01-14 21:55:44 +00:00
|
|
|
|
2000-01-18 01:43:12 +00:00
|
|
|
static void
|
|
|
|
|
test_noop(void)
|
2000-01-14 20:22:28 +00:00
|
|
|
{
|
2000-01-14 21:55:44 +00:00
|
|
|
int ret;
|
|
|
|
|
lwres_lwpacket_t pkt, pkt2;
|
|
|
|
|
lwres_nooprequest_t nooprequest, *nooprequest2;
|
|
|
|
|
lwres_noopresponse_t noopresponse, *noopresponse2;
|
|
|
|
|
lwres_buffer_t b;
|
|
|
|
|
|
2000-01-17 21:38:54 +00:00
|
|
|
pkt.flags = 0;
|
2000-01-14 21:55:44 +00:00
|
|
|
pkt.serial = 0x11223344;
|
|
|
|
|
pkt.recvlength = 0x55667788;
|
|
|
|
|
pkt.result = 0;
|
|
|
|
|
|
|
|
|
|
nooprequest.datalength = strlen(TESTSTRING);
|
2000-02-03 22:46:07 +00:00
|
|
|
nooprequest.data = (unsigned char *) TESTSTRING;
|
2000-01-14 21:55:44 +00:00
|
|
|
ret = lwres_nooprequest_render(ctx, &nooprequest, &pkt, &b);
|
|
|
|
|
CHECK(ret, "lwres_nooprequest_render");
|
|
|
|
|
|
|
|
|
|
hexdump("rendered noop request", b.base, b.used);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now, parse it into a new structure.
|
|
|
|
|
*/
|
|
|
|
|
lwres_buffer_first(&b);
|
|
|
|
|
ret = lwres_lwpacket_parseheader(&b, &pkt2);
|
|
|
|
|
CHECK(ret, "lwres_lwpacket_parseheader");
|
|
|
|
|
|
|
|
|
|
hexdump("parsed pkt2", &pkt2, sizeof(pkt2));
|
|
|
|
|
|
|
|
|
|
nooprequest2 = NULL;
|
|
|
|
|
ret = lwres_nooprequest_parse(ctx, &b, &pkt2, &nooprequest2);
|
|
|
|
|
CHECK(ret, "lwres_nooprequest_parse");
|
|
|
|
|
|
|
|
|
|
assert(nooprequest.datalength == nooprequest2->datalength);
|
|
|
|
|
assert(memcmp(nooprequest.data, nooprequest2->data,
|
|
|
|
|
nooprequest.datalength) == 0);
|
|
|
|
|
|
|
|
|
|
lwres_nooprequest_free(ctx, &nooprequest2);
|
|
|
|
|
|
|
|
|
|
lwres_context_freemem(ctx, b.base, b.length);
|
|
|
|
|
b.base = NULL;
|
|
|
|
|
b.length = 0;
|
|
|
|
|
|
2000-01-17 21:38:54 +00:00
|
|
|
pkt.flags = 0;
|
|
|
|
|
pkt.serial = 0x11223344;
|
|
|
|
|
pkt.recvlength = 0x55667788;
|
|
|
|
|
pkt.result = 0xdeadbeef;
|
|
|
|
|
|
|
|
|
|
noopresponse.datalength = strlen(TESTSTRING);
|
2000-02-03 22:46:07 +00:00
|
|
|
noopresponse.data = (unsigned char *) TESTSTRING;
|
2000-01-17 21:38:54 +00:00
|
|
|
ret = lwres_noopresponse_render(ctx, &noopresponse, &pkt, &b);
|
|
|
|
|
CHECK(ret, "lwres_noopresponse_render");
|
|
|
|
|
|
|
|
|
|
hexdump("rendered noop response", b.base, b.used);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now, parse it into a new structure.
|
|
|
|
|
*/
|
|
|
|
|
lwres_buffer_first(&b);
|
|
|
|
|
ret = lwres_lwpacket_parseheader(&b, &pkt2);
|
|
|
|
|
CHECK(ret, "lwres_lwpacket_parseheader");
|
|
|
|
|
|
|
|
|
|
hexdump("parsed pkt2", &pkt2, sizeof(pkt2));
|
|
|
|
|
|
|
|
|
|
noopresponse2 = NULL;
|
|
|
|
|
ret = lwres_noopresponse_parse(ctx, &b, &pkt2, &noopresponse2);
|
|
|
|
|
CHECK(ret, "lwres_noopresponse_parse");
|
|
|
|
|
|
|
|
|
|
assert(noopresponse.datalength == noopresponse2->datalength);
|
|
|
|
|
assert(memcmp(noopresponse.data, noopresponse2->data,
|
|
|
|
|
noopresponse.datalength) == 0);
|
|
|
|
|
|
|
|
|
|
lwres_noopresponse_free(ctx, &noopresponse2);
|
|
|
|
|
|
|
|
|
|
lwres_context_freemem(ctx, b.base, b.length);
|
|
|
|
|
b.base = NULL;
|
|
|
|
|
b.length = 0;
|
2000-01-18 01:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2000-02-02 23:24:04 +00:00
|
|
|
test_gabn(char *target)
|
2000-01-18 01:43:12 +00:00
|
|
|
{
|
|
|
|
|
lwres_gabnresponse_t *res;
|
|
|
|
|
int ret;
|
2000-01-28 00:34:18 +00:00
|
|
|
unsigned int i;
|
2000-01-18 01:43:12 +00:00
|
|
|
|
|
|
|
|
res = NULL;
|
2000-02-02 23:24:04 +00:00
|
|
|
ret = lwres_getaddrsbyname(ctx, target,
|
2000-01-28 00:34:18 +00:00
|
|
|
LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6,
|
2000-01-18 01:43:12 +00:00
|
|
|
&res);
|
2000-02-02 23:24:04 +00:00
|
|
|
printf("gabn %s ret == %d\n", target, ret);
|
2000-01-18 01:43:12 +00:00
|
|
|
assert(ret == 0);
|
2000-01-28 00:34:18 +00:00
|
|
|
assert(res != NULL);
|
|
|
|
|
|
|
|
|
|
printf("Returned real name: (%u, %s)\n",
|
|
|
|
|
res->realnamelen, res->realname);
|
|
|
|
|
printf("%u aliases:\n", res->naliases);
|
|
|
|
|
for (i = 0 ; i < res->naliases ; i++)
|
|
|
|
|
printf("\t(%u, %s)\n", res->aliaslen[i], res->aliases[i]);
|
|
|
|
|
printf("%u addresses:\n", res->naddrs);
|
|
|
|
|
for (i = 0 ; i < res->naddrs ; i++) {
|
|
|
|
|
printf("\tAddr len %u family %08x\n",
|
|
|
|
|
res->addrs[i].length, res->addrs[i].family);
|
|
|
|
|
}
|
2000-01-18 01:43:12 +00:00
|
|
|
|
|
|
|
|
lwres_gabnresponse_free(ctx, &res);
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-01 00:29:03 +00:00
|
|
|
static void
|
2000-02-02 23:24:04 +00:00
|
|
|
test_gnba(char *target)
|
2000-02-01 00:29:03 +00:00
|
|
|
{
|
|
|
|
|
lwres_gnbaresponse_t *res;
|
|
|
|
|
int ret;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
2000-02-02 23:24:04 +00:00
|
|
|
in.s_addr = inet_addr(target);
|
2000-02-01 00:29:03 +00:00
|
|
|
|
|
|
|
|
res = NULL;
|
|
|
|
|
ret = lwres_getnamebyaddr(ctx, LWRES_ADDRTYPE_V4, 4,
|
|
|
|
|
(unsigned char *)&in.s_addr, &res);
|
2000-02-02 23:24:04 +00:00
|
|
|
printf("gnba %s ret == %d\n", target, ret);
|
2000-02-01 00:29:03 +00:00
|
|
|
assert(ret == 0);
|
|
|
|
|
assert(res != NULL);
|
|
|
|
|
|
|
|
|
|
printf("Returned real name: (%u, %s)\n",
|
|
|
|
|
res->realnamelen, res->realname);
|
|
|
|
|
printf("%u aliases:\n", res->naliases);
|
|
|
|
|
for (i = 0 ; i < res->naliases ; i++)
|
|
|
|
|
printf("\t(%u, %s)\n", res->aliaslen[i], res->aliases[i]);
|
|
|
|
|
|
|
|
|
|
lwres_gnbaresponse_free(ctx, &res);
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-01 02:58:55 +00:00
|
|
|
#ifdef USE_ISC_MEM
|
2000-02-01 00:29:03 +00:00
|
|
|
/*
|
|
|
|
|
* Wrappers around our memory management stuff, for the lwres functions.
|
|
|
|
|
*/
|
|
|
|
|
static void *
|
|
|
|
|
mem_alloc(void *arg, size_t size)
|
|
|
|
|
{
|
|
|
|
|
return (isc_mem_get(arg, size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
mem_free(void *arg, void *mem, size_t size)
|
|
|
|
|
{
|
|
|
|
|
isc_mem_put(arg, mem, size);
|
|
|
|
|
}
|
2000-02-01 02:58:55 +00:00
|
|
|
#endif
|
2000-02-01 00:29:03 +00:00
|
|
|
|
2000-01-18 01:43:12 +00:00
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
2000-02-01 02:58:55 +00:00
|
|
|
#ifdef USE_ISC_MEM
|
2000-02-01 00:29:03 +00:00
|
|
|
isc_mem_t *mem;
|
|
|
|
|
isc_result_t result;
|
2000-02-01 02:58:55 +00:00
|
|
|
#endif
|
2000-01-18 01:43:12 +00:00
|
|
|
|
|
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
|
|
|
|
|
2000-02-01 02:58:55 +00:00
|
|
|
#ifdef USE_ISC_MEM
|
2000-02-01 00:29:03 +00:00
|
|
|
mem = NULL;
|
|
|
|
|
result = isc_mem_create(0, 0, &mem);
|
|
|
|
|
INSIST(result == ISC_R_SUCCESS);
|
2000-02-01 02:58:55 +00:00
|
|
|
#endif
|
2000-02-01 00:29:03 +00:00
|
|
|
|
2000-01-18 01:43:12 +00:00
|
|
|
ctx = NULL;
|
2000-02-01 02:58:55 +00:00
|
|
|
#ifdef USE_ISC_MEM
|
2000-02-01 00:29:03 +00:00
|
|
|
ret = lwres_context_create(&ctx, mem, mem_alloc, mem_free);
|
2000-02-01 02:58:55 +00:00
|
|
|
#else
|
|
|
|
|
ret = lwres_context_create(&ctx, NULL, NULL, NULL);
|
|
|
|
|
#endif
|
2000-01-18 01:43:12 +00:00
|
|
|
CHECK(ret, "lwres_context_create");
|
|
|
|
|
|
|
|
|
|
test_noop();
|
2000-02-02 23:24:04 +00:00
|
|
|
test_gabn("notthereatall.flame.org.");
|
|
|
|
|
test_gabn("alias-05.test.flame.org.");
|
|
|
|
|
test_gabn("f.root-servers.net.");
|
|
|
|
|
test_gnba("198.133.199.1");
|
2000-01-17 21:38:54 +00:00
|
|
|
|
2000-01-14 21:55:44 +00:00
|
|
|
lwres_context_destroy(&ctx);
|
|
|
|
|
|
2000-02-01 02:58:55 +00:00
|
|
|
#ifdef USE_ISC_MEM
|
2000-02-01 00:29:03 +00:00
|
|
|
isc_mem_stats(mem, stdout);
|
|
|
|
|
isc_mem_destroy(&mem);
|
2000-02-01 02:58:55 +00:00
|
|
|
#endif
|
2000-02-01 00:29:03 +00:00
|
|
|
|
2000-01-14 21:55:44 +00:00
|
|
|
return (0);
|
2000-01-14 20:22:28 +00:00
|
|
|
}
|