1: #ifndef lint
   2: static char sccsid[] = "@(#)nstest.c	4.1 (Berkeley) 5/12/86";
   3: #endif
   4: 
   5: /*
   6:  * Copyright (c) 1986 Regents of the University of California
   7:  *	All Rights Reserved
   8:  */
   9: 
  10: #include <sys/types.h>
  11: #include <sys/socket.h>
  12: #include <netinet/in.h>
  13: #include <stdio.h>
  14: #include <arpa/nameser.h>
  15: #include <resolv.h>
  16: 
  17: extern char *inet_ntoa();
  18: char *progname;
  19: FILE *log;
  20: 
  21: main(argc, argv)
  22:     char **argv;
  23: {
  24:     register char *cp;
  25:     struct hostent *hp;
  26:     short port = htons(NAMESERVER_PORT);
  27:     char buf[BUFSIZ];
  28:     char packet[PACKETSZ];
  29:     char answer[PACKETSZ];
  30:     int n;
  31: 
  32:     progname = argv[0];
  33:     _res.options |= RES_INIT|RES_DEBUG|RES_RECURSE;
  34:     while (argc > 1 && argv[1][0] == '-') {
  35:         argc--;
  36:         cp = *++argv;
  37:         while (*++cp)
  38:             switch (*cp) {
  39:             case 'p':
  40:                 if (--argc <= 0)
  41:                     usage();
  42:                 port = htons(atoi(*++argv));
  43:                 break;
  44: 
  45:             case 'i':
  46:                 _res.options |= RES_IGNTC;
  47:                 break;
  48: 
  49:             case 'v':
  50:                 _res.options |= RES_USEVC|RES_STAYOPEN;
  51:                 break;
  52: 
  53:             case 'r':
  54:                 _res.options &= ~RES_RECURSE;
  55:                 break;
  56: 
  57:             default:
  58:                 usage();
  59:             }
  60:     }
  61:     _res.nsaddr.sin_family = AF_INET;
  62:     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  63:     _res.nsaddr.sin_port = port;
  64:     if (argc > 1)
  65:         _res.nsaddr.sin_addr.s_addr = inet_addr(argv[1]);
  66:     if (argc > 2) {
  67:         log = fopen(argv[2],"w");
  68:         if (log == NULL) perror(argv[2]);
  69:     }
  70:     for (;;) {
  71:         printf("> ");
  72:         fflush(stdout);
  73:         if ((cp = (char *)gets(buf)) == NULL)
  74:             break;
  75:         switch (*cp++) {
  76:         case 'a':
  77:             n = res_mkquery(QUERY, cp, C_ANY, T_A, (char *)0, 0, NULL,
  78:                 packet, sizeof(packet));
  79:             break;
  80: 
  81:         case 'A':
  82:             n = ntohl(inet_addr(cp));
  83:             putlong(n, cp);
  84:             n = res_mkquery(IQUERY, "", C_IN, T_A, cp, sizeof(long), NULL,
  85:                 packet, sizeof(packet));
  86:             break;
  87: 
  88:         case 'f':
  89:             n = res_mkquery(QUERY, cp, C_ANY, T_UINFO, (char *)0, 0, NULL,
  90:                 packet, sizeof(packet));
  91:             break;
  92: 
  93:         case 'g':
  94:             n = res_mkquery(QUERY, cp, C_ANY, T_GID, (char *)0, 0, NULL,
  95:                 packet, sizeof(packet));
  96:             break;
  97: 
  98:         case 'G':
  99:             *(int *)cp = htonl(atoi(cp));
 100:             n = res_mkquery(IQUERY, "", C_ANY, T_GID, cp, sizeof(int), NULL,
 101:                 packet, sizeof(packet));
 102:             break;
 103: 
 104:         case 'h':
 105:             n = res_mkquery(QUERY, cp, C_IN, T_HINFO, (char *)0, 0, NULL,
 106:                 packet, sizeof(packet));
 107:             break;
 108: 
 109:         case 'm':
 110:             n = res_mkquery(QUERY, cp, C_IN, T_MX, (char *)0, 0, NULL,
 111:                 packet, sizeof(packet));
 112:             break;
 113: 
 114:         case 'M':
 115:             n = res_mkquery(QUERY, cp, C_IN, T_MAILB, (char *)0, 0, NULL,
 116:                 packet, sizeof(packet));
 117:             break;
 118: 
 119:         case 'n':
 120:             n = res_mkquery(QUERY, cp, C_IN, T_NS, (char *)0, 0, NULL,
 121:                 packet, sizeof(packet));
 122:             break;
 123: 
 124:         case 'p':
 125:             n = res_mkquery(QUERY, cp, C_IN, T_PTR, (char *)0, 0, NULL,
 126:                 packet, sizeof(packet));
 127:             break;
 128: 
 129:         case 's':
 130:             n = res_mkquery(QUERY, cp, C_IN, T_SOA, (char *)0, 0, NULL,
 131:                 packet, sizeof(packet));
 132:             break;
 133: 
 134:         case 'u':
 135:             n = res_mkquery(QUERY, cp, C_ANY, T_UID, (char *)0, 0, NULL,
 136:                 packet, sizeof(packet));
 137:             break;
 138: 
 139:         case 'U':
 140:             *(int *)cp = htonl(atoi(cp));
 141:             n = res_mkquery(IQUERY, "", C_ANY, T_UID, cp, sizeof(int), NULL,
 142:                 packet, sizeof(packet));
 143:             break;
 144: 
 145:         case 'x':
 146:             n = res_mkquery(QUERY, cp, C_IN, T_AXFR, (char *)0, 0, NULL,
 147:                 packet, sizeof(packet));
 148:             break;
 149: 
 150:         case 'w':
 151:             n = res_mkquery(QUERY, cp, C_IN, T_WKS, (char *)0, 0, NULL,
 152:                 packet, sizeof(packet));
 153:             break;
 154: 
 155:         case '*':
 156:             n = res_mkquery(QUERY, cp, C_IN, T_ANY, (char *)0, 0, NULL,
 157:                 packet, sizeof(packet));
 158:             break;
 159: 
 160:         default:
 161:             printf("a{host} - query  T_A\n");
 162:             printf("A{addr} - iquery T_A\n");
 163:             printf("f{host} - query  T_UINFO\n");
 164:             printf("g{host} - query  T_GID\n");
 165:             printf("G{gid}  - iquery T_GID\n");
 166:             printf("h{host} - query  T_HINFO\n");
 167:             printf("p{host} - query  T_PTR\n");
 168:             printf("m{host} - query  T_MX\n");
 169:             printf("M{host} - query  T_MAILB\n");
 170:             printf("n{host} - query  T_NS\n");
 171:             printf("s{host} - query  T_SOA\n");
 172:             printf("u{host} - query  T_UID\n");
 173:             printf("U{uid}  - iquery T_UID\n");
 174:             printf("x{host} - query  T_AXFR\n");
 175:             printf("w{host} - query  T_WKS\n");
 176:             printf("*{host} - query  T_ANY\n");
 177:             continue;
 178:         }
 179:         if (n < 0) {
 180:             printf("res_mkquery: buffer too small\n");
 181:             continue;
 182:         }
 183:         if (log) {
 184:             fprintf(log,"SEND QUERY\n");
 185:             fp_query(packet, log);
 186:         }
 187:         n = res_send(packet, n, answer, sizeof(answer));
 188:         if (n < 0) {
 189:             printf("res_send: send error\n");
 190:             if (log) fprintf(log, "res_send: send error\n");
 191:         }
 192:         else {
 193:             int f;
 194: 
 195:             f = creat("r", 0644);
 196:             write(f, answer, n);
 197:             (void) close(f);
 198:             if (log) {
 199:                 fprintf(log, "GOT ANSWER\n");
 200:                 fp_query(answer, log);
 201:             }
 202:         }
 203:     }
 204: }
 205: 
 206: usage()
 207: {
 208:     fprintf(stderr, "Usage: %s [-v] [-i] [-r] [-p port] hostaddr\n",
 209:         progname);
 210:     exit(1);
 211: }

Defined functions

main defined in line 21; never used
usage defined in line 206; used 2 times

Defined variables

progname defined in line 18; used 2 times
sccsid defined in line 2; never used
Last modified: 1986-05-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1458
Valid CSS Valid XHTML 1.0 Strict