📄 getinfo.c
字号:
/* * Copyright (c) 1985 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that this notice is preserved and that due credit is given * to the University of California at Berkeley. The name of the University * may not be used to endorse or promote products derived from this * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. */#ifndef lintstatic char sccsid[] = "@(#)getinfo.c 1.1 92/07/30 SMI from UCB 5.16 3/11/88";#endif /* not lint *//* ******************************************************************************* * * getinfo.c -- * * Routines to create requests to name servers * and interpret the answers. * * Adapted from 4.3BSD BIND gethostnamadr.c * ******************************************************************************* */#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdio.h>#include <ctype.h>#include <arpa/nameser.h>#include <resolv.h>#include "res.h"extern char *_res_resultcodes[];extern char *res_skip();#define MAXALIASES 35#define MAXADDRS 35#define MAXDOMAINS 35#define MAXSERVERS 10static char *addr_list[MAXADDRS + 1];static char *host_aliases[MAXALIASES];static int host_aliases_len[MAXALIASES];static char hostbuf[BUFSIZ+1];typedef struct { char *name; char *domain[MAXDOMAINS]; int numDomains; char *address[MAXADDRS]; int numAddresses;} ServerTable;ServerTable server[MAXSERVERS];typedef union { HEADER qb1; char qb2[PACKETSZ];} querybuf;static union { long al; char ac;} align;/* ******************************************************************************* * * GetAnswer -- * * Interprets an answer packet and retrieves the following * information: * * Results: * SUCCESS the info was retrieved. * NO_INFO the packet did not contain an answer. * NONAUTH non-authoritative information was found. * ERROR the answer was malformed. * Other errors returned in the packet header. * ******************************************************************************* */static intGetAnswer(nsAddrPtr, queryType, msg, msglen, iquery, hostPtr, isserver) struct in_addr *nsAddrPtr; char *msg; int queryType; int msglen; int iquery; register HostInfo *hostPtr; int isserver;{ register HEADER *headerPtr; register char *cp; querybuf answer; char *eom, *bp, **aliasPtr; char **addrPtr; char *namePtr; char *dnamePtr; int type, class; int qdcount, ancount, arcount, nscount, buflen; int haveanswer, getclass; int numAliases = 0; int numAddresses = 0; int n, i, j; int len; int dlen; int status; int numServers; int found; /* * If the hostPtr was used before, free up * the calloc'd areas. */ FreeHostInfoPtr(hostPtr); status = SendRequest(nsAddrPtr, msg, msglen, (char *) &answer, sizeof(answer), &n, !isserver); if (status != SUCCESS) { if (_res.options & RES_DEBUG2) printf("SendRequest failed\n"); return (status); } eom = (char *) &answer + n; headerPtr = (HEADER *) &answer; qdcount = ntohs(headerPtr->qdcount); ancount = ntohs(headerPtr->ancount); arcount = ntohs(headerPtr->arcount); nscount = ntohs(headerPtr->nscount); if (headerPtr->rcode != NOERROR) { if (_res.options & RES_DEBUG && !isserver) { printf( "Failed: %s, num. answers = %d, ns = %d, additional = %d\n", _res_resultcodes[headerPtr->rcode], ancount, nscount, arcount); } return (headerPtr->rcode); } /* * If there are no answer, n.s. or additional records * then return with an error. */ if (ancount == 0 && nscount == 0 && arcount == 0) { return (NO_INFO); } bp = hostbuf; buflen = sizeof(hostbuf); cp = (char *) &answer + sizeof(HEADER); /* * For inverse queries, the desired information is returned * in the question section. If there are no question records, * return with an error. * */ if (qdcount) { if (iquery) { if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0) { return (ERROR); } cp += n + QFIXEDSZ; len = strlen(bp) + 1; hostPtr->name = Calloc((unsigned)1, (unsigned)len); bcopy(bp, hostPtr->name, len); } else { cp += dn_skipname(cp, eom) + QFIXEDSZ; } while (--qdcount > 0) { cp += dn_skipname(cp, eom) + QFIXEDSZ; } } else if (iquery) { return (NO_INFO); } aliasPtr = host_aliases; addrPtr = addr_list; haveanswer = 0; /* * Scan through the answer resource records. * Answers for address query types are saved. * Other query type answers are just printed. */ if (isserver == 0 && !headerPtr->aa && headerPtr->ancount) printf("Non-authoritative answer:\n"); while (--ancount >= 0 && cp < eom) { if (queryType != T_A) { if ((cp = Print_rr(cp, (char *) &answer, eom, stdout)) == NULL) { return(ERROR); } continue; } else { if ((n = dn_expand((char *) &answer, eom, cp, bp, buflen)) < 0) { return(ERROR); } cp += n; type = _getshort(cp); cp += sizeof(u_short); class = _getshort(cp); cp += sizeof(u_short) + sizeof(u_long); dlen = _getshort(cp); cp += sizeof(u_short); if (type == T_CNAME) { /* * Found an alias. */ cp += dlen; if (aliasPtr >= &host_aliases[MAXALIASES-1]) continue; *aliasPtr++ = bp; n = strlen(bp) + 1; host_aliases_len[numAliases] = n; numAliases++; bp += n; buflen -= n; continue; } else if (type == T_PTR) { /* * Found a "pointer" to the real name. */ if((n= dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0){ cp += n; continue; } cp += n; len = strlen(bp) + 1; hostPtr->name = Calloc((unsigned)1, (unsigned)len); bcopy(bp, hostPtr->name, len); haveanswer = 1; break; } else if (type != T_A) { cp += dlen; continue; } if (haveanswer) { if (n != hostPtr->addrLen) { cp += dlen; continue; } if (class != getclass) { cp += dlen; continue; } } else { hostPtr->addrLen = dlen; getclass = class; hostPtr->addrType = (class == C_IN) ? AF_INET : AF_UNSPEC; if (!iquery) { len = strlen(bp) + 1; hostPtr->name = Calloc((unsigned)1, (unsigned)len); bcopy(bp, hostPtr->name, len); } } bp += (((u_long)bp) % sizeof(align)); if (bp + dlen >= &hostbuf[sizeof(hostbuf)]) { if (_res.options & RES_DEBUG) printf("Size (%d) too big\n", dlen); break; } bcopy(cp, *addrPtr++ = bp, dlen); bp +=dlen; cp += dlen; numAddresses++; haveanswer = 1; } } if (queryType == T_A && haveanswer) { /* * Go through the alias and address lists and return them * in the hostPtr variable. */ if (numAliases > 0) { hostPtr->aliases = (char **) Calloc((unsigned)1+numAliases, sizeof(char *)); for (i = 0; i < numAliases; i++) { hostPtr->aliases[i] = Calloc((unsigned)1, (unsigned)host_aliases_len[i]); bcopy(host_aliases[i], hostPtr->aliases[i], host_aliases_len[i]); } hostPtr->aliases[i] = NULL; } if (numAddresses > 0) { hostPtr->addrList = (char **) Calloc((unsigned)1+numAddresses, sizeof(char *)); for (i = 0; i < numAddresses; i++) { hostPtr->addrList[i] = Calloc((unsigned)1, (unsigned)hostPtr->addrLen); bcopy(addr_list[i], hostPtr->addrList[i], hostPtr->addrLen); } hostPtr->addrList[i] = NULL; } hostPtr->servers= NULL; return (SUCCESS); } /* * At this point, for the T_A query type, only empty answers remain. * For other query types, additional information might be found * in the additional resource records part. */ cp = res_skip((char *) &answer, 2, eom); numServers = 0; while (--nscount >= 0 && cp < eom) { /* * Go through the NS records and retrieve the * names of hosts that server the requested domain, * their addresses and other domains they might serve. */ if ((n = dn_expand((char *) &answer, eom, cp, bp, buflen)) < 0) { return(ERROR); } cp += n; len = strlen(bp) + 1; dnamePtr = Calloc((unsigned)1, (unsigned)len); /* domain name */ bcopy(bp, dnamePtr, len); type = _getshort(cp); cp += sizeof(u_short); class = _getshort(cp); cp += sizeof(u_short) + sizeof(u_long); dlen = _getshort(cp); cp += sizeof(u_short); if (type != T_NS) { cp += dlen; } else { if ((n = dn_expand((char *) &answer, eom, cp, bp, buflen)) < 0) { return(ERROR); } cp += n; len = strlen(bp) + 1; namePtr = Calloc((unsigned)1, (unsigned)len); /* server host name */ bcopy(bp, namePtr, len); /* * Store the information keyed by the server host name. */ found = FALSE; for (j = 0; j < numServers; j++) { if (strcmp(namePtr, server[j].name) == 0) { found = TRUE; free(namePtr); break; } } if (found) { server[j].numDomains++; if (server[j].numDomains <= MAXDOMAINS) { server[j].domain[server[j].numDomains-1] = dnamePtr; } } else { if (numServers > MAXSERVERS) { break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -