1 /* $NetBSD: lwres_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: lwres_test.c,v 1.31 2007/06/19 23:46:59 tbox Exp */ 21 22 #include <config.h> 23 24 #include <assert.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include <isc/mem.h> 29 #include <isc/netaddr.h> 30 #include <isc/util.h> 31 32 #include <lwres/lwres.h> 33 34 #define USE_ISC_MEM 35 36 static inline void 37 CHECK(int val, const char *msg) { 38 if (val != 0) { 39 fprintf(stderr, "%s returned %d\n", msg, val); 40 exit(1); 41 } 42 } 43 44 static void 45 hexdump(const char *msg, void *base, size_t len) { 46 unsigned char *p; 47 unsigned int cnt; 48 49 p = base; 50 cnt = 0; 51 52 printf("*** %s (%lu bytes @ %p)\n", msg, (unsigned long)len, base); 53 54 while (cnt < len) { 55 if (cnt % 16 == 0) 56 printf("%p: ", p); 57 else if (cnt % 8 == 0) 58 printf(" |"); 59 printf(" %02x", *p++); 60 cnt++; 61 62 if (cnt % 16 == 0) 63 printf("\n"); 64 } 65 66 if (cnt % 16 != 0) 67 printf("\n"); 68 } 69 70 static const char *TESTSTRING = "This is a test. This is only a test. !!!"; 71 static lwres_context_t *ctx; 72 73 static void 74 test_noop(void) { 75 int ret; 76 lwres_lwpacket_t pkt, pkt2; 77 lwres_nooprequest_t nooprequest, *nooprequest2; 78 lwres_noopresponse_t noopresponse, *noopresponse2; 79 lwres_buffer_t b; 80 81 pkt.pktflags = 0; 82 pkt.serial = 0x11223344; 83 pkt.recvlength = 0x55667788; 84 pkt.result = 0; 85 86 nooprequest.datalength = strlen(TESTSTRING); 87 /* XXXDCL maybe "nooprequest.data" should be const. */ 88 DE_CONST(TESTSTRING, nooprequest.data); 89 ret = lwres_nooprequest_render(ctx, &nooprequest, &pkt, &b); 90 CHECK(ret, "lwres_nooprequest_render"); 91 92 hexdump("rendered noop request", b.base, b.used); 93 94 /* 95 * Now, parse it into a new structure. 96 */ 97 lwres_buffer_first(&b); 98 ret = lwres_lwpacket_parseheader(&b, &pkt2); 99 CHECK(ret, "lwres_lwpacket_parseheader"); 100 101 hexdump("parsed pkt2", &pkt2, sizeof(pkt2)); 102 103 nooprequest2 = NULL; 104 ret = lwres_nooprequest_parse(ctx, &b, &pkt2, &nooprequest2); 105 CHECK(ret, "lwres_nooprequest_parse"); 106 107 assert(nooprequest.datalength == nooprequest2->datalength); 108 assert(memcmp(nooprequest.data, nooprequest2->data, 109 nooprequest.datalength) == 0); 110 111 lwres_nooprequest_free(ctx, &nooprequest2); 112 113 lwres_context_freemem(ctx, b.base, b.length); 114 b.base = NULL; 115 b.length = 0; 116 117 pkt.pktflags = 0; 118 pkt.serial = 0x11223344; 119 pkt.recvlength = 0x55667788; 120 pkt.result = 0xdeadbeef; 121 122 noopresponse.datalength = strlen(TESTSTRING); 123 /* XXXDCL maybe "noopresponse.data" should be const. */ 124 DE_CONST(TESTSTRING, noopresponse.data); 125 ret = lwres_noopresponse_render(ctx, &noopresponse, &pkt, &b); 126 CHECK(ret, "lwres_noopresponse_render"); 127 128 hexdump("rendered noop response", b.base, b.used); 129 130 /* 131 * Now, parse it into a new structure. 132 */ 133 lwres_buffer_first(&b); 134 ret = lwres_lwpacket_parseheader(&b, &pkt2); 135 CHECK(ret, "lwres_lwpacket_parseheader"); 136 137 hexdump("parsed pkt2", &pkt2, sizeof(pkt2)); 138 139 noopresponse2 = NULL; 140 ret = lwres_noopresponse_parse(ctx, &b, &pkt2, &noopresponse2); 141 CHECK(ret, "lwres_noopresponse_parse"); 142 143 assert(noopresponse.datalength == noopresponse2->datalength); 144 assert(memcmp(noopresponse.data, noopresponse2->data, 145 noopresponse.datalength) == 0); 146 147 lwres_noopresponse_free(ctx, &noopresponse2); 148 149 lwres_context_freemem(ctx, b.base, b.length); 150 b.base = NULL; 151 b.length = 0; 152 } 153 154 static void 155 test_gabn(const char *target) { 156 lwres_gabnresponse_t *res; 157 lwres_addr_t *addr; 158 int ret; 159 unsigned int i; 160 char outbuf[64]; 161 162 res = NULL; 163 ret = lwres_getaddrsbyname(ctx, target, 164 LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6, 165 &res); 166 printf("gabn %s ret == %d\n", target, ret); 167 if (ret != 0) { 168 printf("FAILURE!\n"); 169 if (res != NULL) 170 lwres_gabnresponse_free(ctx, &res); 171 return; 172 } 173 174 printf("Returned real name: (%u, %s)\n", 175 res->realnamelen, res->realname); 176 printf("%u aliases:\n", res->naliases); 177 for (i = 0; i < res->naliases; i++) 178 printf("\t(%u, %s)\n", res->aliaslen[i], res->aliases[i]); 179 printf("%u addresses:\n", res->naddrs); 180 addr = LWRES_LIST_HEAD(res->addrs); 181 for (i = 0; i < res->naddrs; i++) { 182 INSIST(addr != NULL); 183 184 if (addr->family == LWRES_ADDRTYPE_V4) 185 (void)inet_ntop(AF_INET, addr->address, 186 outbuf, sizeof(outbuf)); 187 else 188 (void)inet_ntop(AF_INET6, addr->address, 189 outbuf, sizeof(outbuf)); 190 printf("\tAddr len %u family %08x %s\n", 191 addr->length, addr->family, outbuf); 192 addr = LWRES_LIST_NEXT(addr, link); 193 } 194 195 lwres_gabnresponse_free(ctx, &res); 196 } 197 198 static void 199 test_gnba(const char *target, lwres_uint32_t af) { 200 lwres_gnbaresponse_t *res; 201 int ret; 202 unsigned int i; 203 unsigned char addrbuf[16]; 204 unsigned int len; 205 206 if (af == LWRES_ADDRTYPE_V4) { 207 len = 4; 208 ret = inet_pton(AF_INET, target, addrbuf); 209 assert(ret == 1); 210 } else { 211 len = 16; 212 ret = inet_pton(AF_INET6, target, addrbuf); 213 assert(ret == 1); 214 } 215 216 res = NULL; 217 ret = lwres_getnamebyaddr(ctx, af, len, addrbuf, &res); 218 printf("gnba %s ret == %d\n", target, ret); 219 assert(ret == 0); 220 assert(res != NULL); 221 222 printf("Returned real name: (%u, %s)\n", 223 res->realnamelen, res->realname); 224 printf("%u aliases:\n", res->naliases); 225 for (i = 0; i < res->naliases; i++) 226 printf("\t(%u, %s)\n", res->aliaslen[i], res->aliases[i]); 227 228 lwres_gnbaresponse_free(ctx, &res); 229 } 230 231 #ifdef USE_ISC_MEM 232 /* 233 * Wrappers around our memory management stuff, for the lwres functions. 234 */ 235 static void * 236 mem_alloc(void *arg, size_t size) { 237 return (isc_mem_get(arg, size)); 238 } 239 240 static void 241 mem_free(void *arg, void *mem, size_t size) { 242 isc_mem_put(arg, mem, size); 243 } 244 #endif 245 246 int 247 main(int argc, char *argv[]) { 248 int ret; 249 #ifdef USE_ISC_MEM 250 isc_mem_t *mem; 251 isc_result_t result; 252 #endif 253 254 (void)argc; 255 (void)argv; 256 257 #ifdef USE_ISC_MEM 258 mem = NULL; 259 result = isc_mem_create(0, 0, &mem); 260 INSIST(result == ISC_R_SUCCESS); 261 #endif 262 263 ctx = NULL; 264 #ifdef USE_ISC_MEM 265 ret = lwres_context_create(&ctx, mem, mem_alloc, mem_free, 0); 266 #else 267 ret = lwres_context_create(&ctx, NULL, NULL, NULL, 0); 268 #endif 269 270 CHECK(ret, "lwres_context_create"); 271 272 ret = lwres_conf_parse(ctx, "/etc/resolv.conf"); 273 CHECK(ret, "lwres_conf_parse"); 274 275 lwres_conf_print(ctx, stdout); 276 277 test_noop(); 278 279 /* 280 * The following comments about tests all assume your search path is 281 * nominum.com isc.org flame.org 282 * and ndots is the default of 1. 283 */ 284 test_gabn("alias-05.test"); /* exact, then search. */ 285 test_gabn("f.root-servers.net."); 286 test_gabn("poofball.flame.org."); 287 test_gabn("foo.ip6.int."); 288 test_gabn("notthereatall.flame.org"); /* exact, then search (!found)*/ 289 test_gabn("shell"); /* search (found in nominum.com), then exact */ 290 test_gabn("kechara"); /* search (found in flame.org), then exact */ 291 test_gabn("lkasdjlaksjdlkasjdlkasjdlkasjd"); /* search, exact(!found)*/ 292 293 test_gnba("198.133.199.1", LWRES_ADDRTYPE_V4); 294 test_gnba("204.152.184.79", LWRES_ADDRTYPE_V4); 295 test_gnba("3ffe:8050:201:1860:42::1", LWRES_ADDRTYPE_V6); 296 297 lwres_conf_clear(ctx); 298 lwres_context_destroy(&ctx); 299 300 #ifdef USE_ISC_MEM 301 isc_mem_stats(mem, stdout); 302 isc_mem_destroy(&mem); 303 #endif 304 305 return (0); 306 } 307