1: /* 2: * Copyright (c) 1986, 1989 Regents of the University of California. 3: * All rights reserved. 4: * 5: * Redistribution and use in source and binary forms are permitted provided 6: * that: (1) source distributions retain this entire copyright notice and 7: * comment, and (2) distributions including binaries display the following 8: * acknowledgement: ``This product includes software developed by the 9: * University of California, Berkeley and its contributors'' in the 10: * documentation or other materials provided with the distribution and in 11: * all advertising materials mentioning features or use of this software. 12: * Neither the name of the University nor the names of its contributors may 13: * be used to endorse or promote products derived from this software without 14: * specific prior written permission. 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 18: */ 19: 20: #if !defined(lint) && defined(DOSCCS) 21: char copyright[] = 22: "@(#) Copyright (c) 1986 Regents of the University of California.\n\ 23: All rights reserved.\n"; 24: 25: static char sccsid[] = "@(#)nstest.c 4.12 (Berkeley) 6/1/90"; 26: #endif 27: 28: #include <sys/types.h> 29: #include <sys/socket.h> 30: #include <netinet/in.h> 31: #include <stdio.h> 32: #include <arpa/nameser.h> 33: #include <resolv.h> 34: 35: extern char *inet_ntoa(); 36: char *progname; 37: FILE *log; 38: #define MAXDATA 256 /* really should get definition from named/db.h */ 39: main(argc, argv) 40: char **argv; 41: { 42: register char *cp; 43: u_short port = htons(NAMESERVER_PORT); 44: char buf[BUFSIZ]; 45: char packet[PACKETSZ]; 46: char answer[PACKETSZ]; 47: struct rrec NewRR; 48: int n, dump_packet; 49: 50: NewRR.r_data = (char *) malloc(MAXDATA); 51: NewRR.r_data = (char *) malloc(MAXDATA); 52: progname = argv[0]; 53: dump_packet = 0; 54: _res.options |= RES_DEBUG|RES_RECURSE; 55: (void) res_init(); 56: while (argc > 1 && argv[1][0] == '-') { 57: argc--; 58: cp = *++argv; 59: while (*++cp) 60: switch (*cp) { 61: case 'p': 62: if (--argc <= 0) 63: usage(); 64: port = htons(atoi(*++argv)); 65: break; 66: 67: case 'i': 68: _res.options |= RES_IGNTC; 69: break; 70: 71: case 'v': 72: _res.options |= RES_USEVC|RES_STAYOPEN; 73: break; 74: 75: case 'r': 76: _res.options &= ~RES_RECURSE; 77: break; 78: 79: case 'd': 80: dump_packet++; 81: break; 82: 83: default: 84: usage(); 85: } 86: } 87: _res.nsaddr.sin_family = AF_INET; 88: _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 89: _res.nsaddr.sin_port = port; 90: if (argc > 1) { 91: _res.nsaddr.sin_addr.s_addr = inet_addr(argv[1]); 92: if (_res.nsaddr.sin_addr.s_addr == (u_long) -1) 93: usage(); 94: } 95: if (argc > 2) { 96: log = fopen(argv[2],"w"); 97: if (log == NULL) perror(argv[2]); 98: } 99: for (;;) { 100: printf("> "); 101: fflush(stdout); 102: if ((cp = (char *)gets(buf)) == NULL) 103: break; 104: switch (*cp++) { 105: case 'a': 106: n = res_mkquery(QUERY, cp, C_IN, T_A, (char *)0, 0, 107: NULL, packet, sizeof(packet)); 108: break; 109: 110: case 'A': 111: n = ntohl(inet_addr(cp)); 112: putlong(n, cp); 113: n = res_mkquery(IQUERY, "", C_IN, T_A, cp, sizeof(long), 114: NULL, packet, sizeof(packet)); 115: break; 116: 117: case 'f': 118: n = res_mkquery(QUERY, cp, C_ANY, T_UINFO, (char *)0, 0, 119: NULL, packet, sizeof(packet)); 120: break; 121: 122: case 'g': 123: n = res_mkquery(QUERY, cp, C_ANY, T_GID, (char *)0, 0, 124: NULL, packet, sizeof(packet)); 125: break; 126: 127: case 'G': 128: *(int *)cp = htonl(atoi(cp)); 129: n = res_mkquery(IQUERY, "", C_ANY, T_GID, cp, 130: sizeof(int), NULL, packet, sizeof(packet)); 131: break; 132: 133: case 'c': 134: n = res_mkquery(QUERY, cp, C_IN, T_CNAME, (char *)0, 0, 135: NULL, packet, sizeof(packet)); 136: break; 137: 138: case 'h': 139: n = res_mkquery(QUERY, cp, C_IN, T_HINFO, (char *)0, 0, 140: NULL, packet, sizeof(packet)); 141: break; 142: 143: case 'm': 144: n = res_mkquery(QUERY, cp, C_IN, T_MX, (char *)0, 0, 145: NULL, packet, sizeof(packet)); 146: break; 147: 148: case 'M': 149: n = res_mkquery(QUERY, cp, C_IN, T_MAILB, (char *)0, 0, 150: NULL, packet, sizeof(packet)); 151: break; 152: 153: case 'n': 154: n = res_mkquery(QUERY, cp, C_IN, T_NS, (char *)0, 0, 155: NULL, packet, sizeof(packet)); 156: break; 157: 158: case 'p': 159: n = res_mkquery(QUERY, cp, C_IN, T_PTR, (char *)0, 0, 160: NULL, packet, sizeof(packet)); 161: break; 162: 163: case 's': 164: n = res_mkquery(QUERY, cp, C_IN, T_SOA, (char *)0, 0, 165: NULL, packet, sizeof(packet)); 166: break; 167: 168: case 'T': 169: n = res_mkquery(QUERY, cp, C_IN, T_TXT, (char *)0, 0, 170: NULL, packet, sizeof(packet)); 171: break; 172: 173: case 'u': 174: n = res_mkquery(QUERY, cp, C_ANY, T_UID, (char *)0, 0, 175: NULL, packet, sizeof(packet)); 176: break; 177: 178: case 'U': 179: *(int *)cp = htonl(atoi(cp)); 180: n = res_mkquery(IQUERY, "", C_ANY, T_UID, cp, 181: sizeof(int), NULL, packet, sizeof(packet)); 182: break; 183: 184: case 'x': 185: n = res_mkquery(QUERY, cp, C_IN, T_AXFR, (char *)0, 0, 186: NULL, packet, sizeof(packet)); 187: break; 188: 189: case 'w': 190: n = res_mkquery(QUERY, cp, C_IN, T_WKS, (char *)0, 0, 191: NULL, packet, sizeof(packet)); 192: break; 193: 194: case 'b': 195: n = res_mkquery(QUERY, cp, C_IN, T_MB, (char *)0, 0, 196: NULL, packet, sizeof(packet)); 197: break; 198: 199: case 'B': 200: n = res_mkquery(QUERY, cp, C_IN, T_MG, (char *)0, 0, 201: NULL, packet, sizeof(packet)); 202: break; 203: 204: case 'i': 205: n = res_mkquery(QUERY, cp, C_IN, T_MINFO, (char *)0, 0, 206: NULL, packet, sizeof(packet)); 207: break; 208: 209: case 'r': 210: n = res_mkquery(QUERY, cp, C_IN, T_MR, (char *)0, 0, 211: NULL, packet, sizeof(packet)); 212: break; 213: 214: case '*': 215: n = res_mkquery(QUERY, cp, C_IN, T_ANY, (char *)0, 0, 216: NULL, packet, sizeof(packet)); 217: break; 218: 219: #ifdef ALLOW_UPDATES 220: case '^': 221: { 222: char IType[10], TempStr[50]; 223: int Type, oldnbytes, nbytes, i; 224: #ifdef ALLOW_T_UNSPEC 225: printf("Data type (a = T_A, u = T_UNSPEC): "); 226: gets(IType); 227: if (IType[0] == 'u') { 228: Type = T_UNSPEC; 229: printf("How many data bytes? "); 230: gets(TempStr); /* Throw away CR */ 231: sscanf(TempStr, "%d", &nbytes); 232: for (i = 0; i < nbytes; i++) { 233: (NewRR.r_data)[i] = (char) i; 234: } 235: } else { 236: #endif ALLOW_T_UNSPEC 237: Type = T_A; 238: nbytes = sizeof(u_long); 239: printf("Inet addr for new dname (e.g., 192.4.3.2): "); 240: gets(TempStr); 241: putlong(ntohl(inet_addr(TempStr)), NewRR.r_data); 242: #ifdef ALLOW_T_UNSPEC 243: } 244: #endif ALLOW_T_UNSPEC 245: NewRR.r_class = C_IN; 246: NewRR.r_type = Type; 247: NewRR.r_size = nbytes; 248: NewRR.r_ttl = 99999999; 249: printf("Add, modify, or modify all (a/m/M)? "); 250: gets(TempStr); 251: if (TempStr[0] == 'a') { 252: n = res_mkquery(UPDATEA, cp, C_IN, Type, 253: OldRRData, nbytes, 254: &NewRR, packet, 255: sizeof(packet)); 256: } else { 257: if (TempStr[0] == 'm') { 258: printf("How many data bytes in old RR? "); 259: gets(TempStr); /* Throw away CR */ 260: sscanf(TempStr, "%d", &oldnbytes); 261: for (i = 0; i < oldnbytes; i++) { 262: OldRRData[i] = (char) i; 263: } 264: n = res_mkquery(UPDATEM, cp, C_IN, Type, 265: OldRRData, oldnbytes, 266: &NewRR, packet, 267: sizeof(packet)); 268: } else { /* Modify all */ 269: n = res_mkquery(UPDATEMA, cp, 270: C_IN, Type, NULL, 0, 271: &NewRR, packet, 272: sizeof(packet)); 273: 274: } 275: } 276: } 277: break; 278: 279: #ifdef ALLOW_T_UNSPEC 280: case 'D': 281: n = res_mkquery(UPDATEDA, cp, C_IN, T_UNSPEC, (char *)0, 282: 0, NULL, packet, sizeof(packet)); 283: break; 284: 285: case 'd': 286: { 287: char TempStr[100]; 288: int nbytes, i; 289: printf("How many data bytes in oldrr data? "); 290: gets(TempStr); /* Throw away CR */ 291: sscanf(TempStr, "%d", &nbytes); 292: for (i = 0; i < nbytes; i++) { 293: OldRRData[i] = (char) i; 294: } 295: n = res_mkquery(UPDATED, cp, C_IN, T_UNSPEC, 296: OldRRData, nbytes, NULL, packet, 297: sizeof(packet)); 298: } 299: break; 300: #endif ALLOW_T_UNSPEC 301: #endif ALLOW_UPDATES 302: 303: default: 304: printf("a{host} - query T_A\n"); 305: printf("A{addr} - iquery T_A\n"); 306: printf("b{user} - query T_MB\n"); 307: printf("B{user} - query T_MG\n"); 308: printf("f{host} - query T_UINFO\n"); 309: printf("g{host} - query T_GID\n"); 310: printf("G{gid} - iquery T_GID\n"); 311: printf("h{host} - query T_HINFO\n"); 312: printf("i{host} - query T_MINFO\n"); 313: printf("p{host} - query T_PTR\n"); 314: printf("m{host} - query T_MX\n"); 315: printf("M{host} - query T_MAILB\n"); 316: printf("n{host} - query T_NS\n"); 317: printf("r{host} - query T_MR\n"); 318: printf("s{host} - query T_SOA\n"); 319: printf("T{host} - query T_TXT\n"); 320: printf("u{host} - query T_UID\n"); 321: printf("U{uid} - iquery T_UID\n"); 322: printf("x{host} - query T_AXFR\n"); 323: printf("w{host} - query T_WKS\n"); 324: printf("c{host} - query T_CNAME\n"); 325: printf("*{host} - query T_ANY\n"); 326: #ifdef ALLOW_UPDATES 327: printf("^{host} - add/mod/moda (T_A/T_UNSPEC)\n"); 328: #ifdef ALLOW_T_UNSPEC 329: printf("D{host} - deletea T_UNSPEC\n"); 330: printf("d{host} - delete T_UNSPEC\n"); 331: #endif ALLOW_T_UNSPEC 332: #endif ALLOW_UPDATES 333: continue; 334: } 335: if (n < 0) { 336: printf("res_mkquery: buffer too small\n"); 337: continue; 338: } 339: if (log) { 340: fprintf(log,"SEND QUERY\n"); 341: fp_query(packet, log); 342: } 343: n = res_send(packet, n, answer, sizeof(answer)); 344: if (n < 0) { 345: printf("res_send: send error\n"); 346: if (log) fprintf(log, "res_send: send error\n"); 347: } 348: else { 349: if (dump_packet) { 350: int f; 351: f = creat("ns_packet.dump", 0644); 352: write(f, answer, n); 353: (void) close(f); 354: } 355: if (log) { 356: fprintf(log, "GOT ANSWER\n"); 357: fp_query(answer, log); 358: } 359: } 360: } 361: } 362: 363: usage() 364: { 365: fprintf(stderr, "Usage: %s [-v] [-i] [-r] [-d] [-p port] hostaddr\n", 366: progname); 367: exit(1); 368: }