📄 dig.c
字号:
sscanf(++ptr, "%d", &_res.retrans); } } else if (strncmp(option, "ret", 3) == 0) { /* retry */ ptr = strchr(option, '='); if (ptr != NULL) { sscanf(++ptr, "%d", &_res.retry); } } else if (strncmp(option, "i", 1) == 0) { /* ignore */ _res.options |= RES_IGNTC; } else if (strncmp(option, "noi", 3) == 0) { _res.options &= ~RES_IGNTC; } else if (strncmp(option, "pr", 2) == 0) { /* primary */ _res.options |= RES_PRIMARY; } else if (strncmp(option, "nop", 3) == 0) { _res.options &= ~RES_PRIMARY; } else if (strncmp(option, "rec", 3) == 0) { /* recurse */ _res.options |= RES_RECURSE; } else if (strncmp(option, "norec", 5) == 0) { _res.options &= ~RES_RECURSE; } else if (strncmp(option, "v", 1) == 0) { /* vc */ _res.options |= RES_USEVC; } else if (strncmp(option, "nov", 3) == 0) { _res.options &= ~RES_USEVC; } else if (strncmp(option, "pfset", 5) == 0) { ptr = strchr(option, '='); if (ptr != NULL) { _res.pfcode = xstrtonum(++ptr); } } else if (strncmp(option, "pfand", 5) == 0) { ptr = strchr(option, '='); if (ptr != NULL) { _res.pfcode = _res.pfcode & xstrtonum(++ptr); } } else if (strncmp(option, "pfor", 4) == 0) { ptr = strchr(option, '='); if (ptr != NULL) { _res.pfcode |= xstrtonum(++ptr); } } else if (strncmp(option, "pfmin", 5) == 0) { _res.pfcode = PRF_MIN; } else if (strncmp(option, "pfdef", 5) == 0) { _res.pfcode = PRF_DEF; } else if (strncmp(option, "an", 2) == 0) { /* answer section */ _res.pfcode |= RES_PRF_ANS; } else if (strncmp(option, "noan", 4) == 0) { _res.pfcode &= ~RES_PRF_ANS; } else if (strncmp(option, "qu", 2) == 0) { /* question section */ _res.pfcode |= RES_PRF_QUES; } else if (strncmp(option, "noqu", 4) == 0) { _res.pfcode &= ~RES_PRF_QUES; } else if (strncmp(option, "au", 2) == 0) { /* authority section */ _res.pfcode |= RES_PRF_AUTH; } else if (strncmp(option, "noau", 4) == 0) { _res.pfcode &= ~RES_PRF_AUTH; } else if (strncmp(option, "ad", 2) == 0) { /* addition section */ _res.pfcode |= RES_PRF_ADD; } else if (strncmp(option, "noad", 4) == 0) { _res.pfcode &= ~RES_PRF_ADD; } else if (strncmp(option, "tt", 2) == 0) { /* TTL & ID */ _res.pfcode |= RES_PRF_TTLID; } else if (strncmp(option, "nott", 4) == 0) { _res.pfcode &= ~RES_PRF_TTLID; } else if (strncmp(option, "he", 2) == 0) { /* head flags stats */ _res.pfcode |= RES_PRF_HEAD2; } else if (strncmp(option, "nohe", 4) == 0) { _res.pfcode &= ~RES_PRF_HEAD2; } else if (strncmp(option, "H", 1) == 0) { /* header all */ _res.pfcode |= RES_PRF_HEADX; } else if (strncmp(option, "noH", 3) == 0) { _res.pfcode &= ~(RES_PRF_HEADX); } else if (strncmp(option, "qr", 2) == 0) { /* query */ _res.pfcode |= RES_PRF_QUERY; } else if (strncmp(option, "noqr", 4) == 0) { _res.pfcode &= ~RES_PRF_QUERY; } else if (strncmp(option, "rep", 3) == 0) { /* reply */ _res.pfcode |= RES_PRF_REPLY; } else if (strncmp(option, "norep", 5) == 0) { _res.pfcode &= ~RES_PRF_REPLY; } else if (strncmp(option, "cm", 2) == 0) { /* command line */ _res.pfcode |= RES_PRF_CMD; } else if (strncmp(option, "nocm", 4) == 0) { _res.pfcode &= ~RES_PRF_CMD; } else if (strncmp(option, "cl", 2) == 0) { /* class mnemonic */ _res.pfcode |= RES_PRF_CLASS; } else if (strncmp(option, "nocl", 4) == 0) { _res.pfcode &= ~RES_PRF_CLASS; } else if (strncmp(option, "st", 2) == 0) { /* stats*/ _res.pfcode |= RES_PRF_STATS; } else if (strncmp(option, "nost", 4) == 0) { _res.pfcode &= ~RES_PRF_STATS; } else { fprintf(stderr, "; *** Invalid option: %s\n", option); return(ERROR); } res_re_init(); return(SUCCESS);}/* * Force a reinitialization when the domain is changed. */res_re_init(){ static char localdomain[] = "LOCALDOMAIN"; char *buf; long pfcode = _res.pfcode; /* this is ugly but putenv() is more portable than setenv() */ buf = malloc((sizeof localdomain) +strlen(_res.defdname) +10/*fuzz*/); sprintf(buf, "%s=%s", localdomain, _res.defdname); putenv(buf); /* keeps the argument, so we won't free it */ _res.options &= ~RES_INIT; res_init(); _res.pfcode = pfcode;}/* * convert char string (decimal, octal, or hex) to integer */intxstrtonum(p) char *p;{ int v = 0; int i; int b = 10; int flag = 0; while (*p != 0) { if (!flag++) if (*p == '0') { b = 8; p++; continue; } if (isupper(*p)) *p=tolower(*p); if (*p == 'x') { b = 16; p++; continue; } if (isdigit(*p)) { i = *p - '0'; } else if (isxdigit(*p)) { i = *p - 'a' + 10; } else { fprintf(stderr, "; *** Bad char in numeric string..ignored\n"); i = -1; } if (i >= b) { fprintf(stderr, "; *** Bad char in numeric string..ignored\n"); i = -1; } if (i >= 0) v = v * b + i; p++; } return(v);}/* this code was cloned from nslookup/list.c */extern char *p_rr(), *_res_resultcodes[]; /* res_debug.c */typedef union { HEADER qb1; char qb2[PACKETSZ];} querybuf;static intprintZone(zone, sin) char *zone; struct sockaddr_in *sin;{ querybuf buf; HEADER *headerPtr; int msglen; int amtToRead; int numRead; int numAnswers = 0; int result; int soacnt = 0; int sockFD; u_short len; char *cp, *nmp; char dname[2][NAME_LEN]; char file[NAME_LEN]; static char *answer = NULL; static int answerLen = 0; enum { NO_ERRORS, ERR_READING_LEN, ERR_READING_MSG, ERR_PRINTING } error = NO_ERRORS; /* * Create a query packet for the requested zone name. */ msglen = res_mkquery(QUERY, zone, queryClass, T_AXFR, (char *)0, 0, 0, (char *) &buf, sizeof(buf)); if (msglen < 0) { if (_res.options & RES_DEBUG) { fprintf(stderr, ";; res_mkquery failed\n"); } return (ERROR); } /* * Set up a virtual circuit to the server. */ if ((sockFD = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { int e = errno; perror(";; socket"); return(e); } if (connect(sockFD, (struct sockaddr *)sin, sizeof(*sin)) < 0) { int e = errno; perror(";; connect"); (void) close(sockFD); sockFD = -1; return e; } /* * Send length & message for zone transfer */ len = htons(msglen); if (write(sockFD, (char *)&len, sizeof(len)) != sizeof(len) || write(sockFD, (char *) &buf, msglen) != msglen) { int e = errno; perror(";; write"); (void) close(sockFD); sockFD = -1; return(e); } dname[0][0] = '\0'; while (1) { u_int16_t tmp; /* * Read the length of the response. */ cp = (char *) &tmp; amtToRead = sizeof(u_short); while (amtToRead > 0 && (numRead=read(sockFD, cp, amtToRead)) > 0){ cp += numRead; amtToRead -= numRead; } if (numRead <= 0) { error = ERR_READING_LEN; break; } if ((len = htons(tmp)) == 0) { break; /* nothing left to read */ } /* * The server sent too much data to fit the existing buffer -- * allocate a new one. */ if (len > answerLen) { if (answerLen != 0) { free(answer); } answerLen = len; answer = Malloc(answerLen); } /* * Read the response. */ amtToRead = len; cp = answer; while (amtToRead > 0 && (numRead=read(sockFD, cp, amtToRead)) > 0) { cp += numRead; amtToRead -= numRead; } if (numRead <= 0) { error = ERR_READING_MSG; break; } result = printRR(stdout, answer, cp); if (result != 0) { error = ERR_PRINTING; break; } numAnswers++; cp = answer + sizeof(HEADER); if (ntohs(((HEADER* )answer)->qdcount) > 0) cp += dn_skipname((u_char *)cp, (u_char *)answer + len) + QFIXEDSZ; nmp = cp; cp += dn_skipname((u_char *)cp, (u_char *)answer + len); if ((_getshort(cp) == T_SOA)) { dn_expand((u_char *)answer, (u_char *)answer + len, (u_char *)nmp, (u_char *)dname[soacnt], sizeof(dname[0])); if (soacnt) { if (strcmp(dname[0], dname[1]) == 0) break; } else soacnt++; } } fprintf(stdout, ";; Received %d record%s.\n", numAnswers, (numAnswers != 1) ? "s" : ""); (void) close(sockFD); sockFD = -1; switch (error) { case NO_ERRORS: return (0); case ERR_READING_LEN: return(EMSGSIZE); case ERR_PRINTING: return(result); case ERR_READING_MSG: return(EMSGSIZE); default: return(EFAULT); }}static intprintRR(file, msg, eom) FILE *file; u_char *msg, *eom;{ register u_char *cp; HEADER *headerPtr; int type, class, dlen, nameLen; u_int32_t ttl; int n, pref; struct in_addr inaddr; char name[NAME_LEN]; char name2[NAME_LEN]; Boolean stripped; /* * Read the header fields. */ headerPtr = (HEADER *)msg; cp = msg + sizeof(HEADER); if (headerPtr->rcode != NOERROR) { return(headerPtr->rcode); } /* * We are looking for info from answer resource records. * If there aren't any, return with an error. We assume * there aren't any question records. */ if (ntohs(headerPtr->ancount) == 0) { return(NO_INFO); } else { if (ntohs(headerPtr->qdcount) > 0) { nameLen = dn_skipname(cp, eom); if (nameLen < 0) return (ERROR); cp += nameLen + QFIXEDSZ; } cp = (u_char*) p_rr(cp, msg, stdout); } return(SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -