📄 main.c
字号:
exit(0);}voidLocalServer(defaultPtr) HostInfo *defaultPtr;{ char hostName[NAME_LEN]; (void) gethostname(hostName, sizeof(hostName));#ifndef WINNT defaultAddr.s_addr = htonl(INADDR_ANY); (void) GetHostInfoByName(&defaultAddr, C_IN, T_A, "0.0.0.0", defaultPtr, 1);#else /* lgk the 0.0.0.0 doesn't work for nt so set to loopback */ defaultAddr.s_addr = htonl(INADDR_LOOPBACK); (void) GetHostInfoByName(&defaultAddr, C_IN, T_A, "127.0.0.1", defaultPtr, 1);#endif free(defaultPtr->name); defaultPtr->name = Calloc(1, sizeof(hostName)+1); strcpy(defaultPtr->name, hostName);}/* ****************************************************************************** * * Usage -- * * Lists the proper methods to run the program and exits. * ****************************************************************************** */void Usage(){ fprintf(stderr, "Usage:\n"); fprintf(stderr," nslookup [-opt ...] # interactive mode using default server\n"); fprintf(stderr," nslookup [-opt ...] - server # interactive mode using 'server'\n"); fprintf(stderr," nslookup [-opt ...] host # just look up 'host' using default server\n"); fprintf(stderr," nslookup [-opt ...] host server # just look up 'host' using 'server'\n"); exit(1);}/* ****************************************************************************** * * IsAddr -- * * Returns TRUE if the string looks like an Internet address. * A string with a trailing dot is not an address, even if it looks * like one. * ****************************************************************************** */BooleanIsAddr(host, addrPtr) char *host; struct in_addr *addrPtr; /* If return TRUE, contains IP address */{ register char *cp; if (isdigit(host[0])) { /* Make sure it has only digits and dots. */ for (cp = host; *cp; ++cp) { if (!isdigit(*cp) && *cp != '.') return FALSE; } /* If it has a trailing dot, don't treat it as an address. */ if (*--cp != '.') { return inet_aton(host, addrPtr); } } return FALSE;}/* ****************************************************************************** * * SetDefaultServer -- * * Changes the default name server to the one specified by * the first argument. The command "server name" uses the current * default server to lookup the info for "name". The command * "lserver name" uses the original server to lookup "name". * * Side effects: * This routine will cause a core dump if the allocation requests fail. * * Results: * SUCCESS The default server was changed successfully. * NONAUTH The server was changed but addresses of * other servers who know about the requested server * were returned. * Errors No info about the new server was found or * requests to the current server timed-out. * ****************************************************************************** */intSetDefaultServer(string, local) char *string; Boolean local;{ register HostInfo *newDefPtr; struct in_addr *servAddrPtr; struct in_addr addr; char newServer[NAME_LEN]; int result; int i; /* * Parse the command line. It maybe of the form "server name", * "lserver name" or just "name". */ if (local) { i = sscanf(string, " lserver %s", newServer); } else { i = sscanf(string, " server %s", newServer); } if (i != 1) { i = sscanf(string, " %s", newServer); if (i != 1) { fprintf(stderr,"SetDefaultServer: invalid name: %s\n", string); return(ERROR); } } /* * Allocate space for a HostInfo variable for the new server. Don't * overwrite the old HostInfo struct because info about the new server * might not be found and we need to have valid default server info. */ newDefPtr = (HostInfo *) Calloc(1, sizeof(HostInfo)); /* * A 'local' lookup uses the original server that the program was * initialized with. * * Check to see if we have the address of the server or the * address of a server who knows about this domain. * XXX For now, just use the first address in the list. */ if (local) { servAddrPtr = &defaultAddr; } else if (defaultPtr->addrList != NULL) { servAddrPtr = (struct in_addr *) defaultPtr->addrList[0]; } else { servAddrPtr = (struct in_addr *) defaultPtr->servers[0]->addrList[0]; } result = ERROR; if (IsAddr(newServer, &addr)) { result = GetHostInfoByAddr(servAddrPtr, &addr, newDefPtr); /* If we can't get the name, fall through... */ } if (result != SUCCESS && result != NONAUTH) { result = GetHostInfoByName(servAddrPtr, C_IN, T_A, newServer, newDefPtr, 1); } /* If we ask for an A record and get none back, but get an NS record for the NS server, this is the NONAUTH case. We must check whether we got an IP address for the NS server or not. */ if ((result == SUCCESS || result == NONAUTH) && ((newDefPtr->addrList && newDefPtr->addrList[0] != 0) || (newDefPtr->servers && newDefPtr->servers[0] && newDefPtr->servers[0]->addrList[0] != 0))) { /* * Found info about the new server. Free the resources for * the old server. */ FreeHostInfoPtr(defaultPtr); free((char *)defaultPtr); defaultPtr = newDefPtr; strcpy(defaultServer, defaultPtr->name);#ifndef GUI PrintHostInfo(stdout, "Default Server:", defaultPtr);#else PrintHostInfo(outputfile, "Default Server:", defaultPtr);#endif return(SUCCESS); } else {#ifndef GUI fprintf(stderr, "*** Can't find address for server %s: %s\n", newServer, DecodeError(result));#else fprintf(filePtr, "*** Can't find address for server %s: %s\n", newServer, DecodeError(result));#endif free((char *)newDefPtr); return(result); }}/* ****************************************************************************** * * DoLoookup -- * * Common subroutine for LookupHost and LookupHostWithServer. * * Results: * SUCCESS - the lookup was successful. * Misc. Errors - an error message is printed if the lookup failed. * ****************************************************************************** */static intDoLookup(host, servPtr, serverName) char *host; HostInfo *servPtr; char *serverName;{ int result; struct in_addr *servAddrPtr; struct in_addr addr; /* Skip escape character */ if (host[0] == '\\') host++; /* * If the user gives us an address for an address query, * silently treat it as a PTR query. If the query type is already * PTR, then convert the address into the in-addr.arpa format. * * Use the address of the server if it exists, otherwise use the * address of a server who knows about this domain. * XXX For now, just use the first address in the list. */ if (servPtr->addrList != NULL) { servAddrPtr = (struct in_addr *) servPtr->addrList[0]; } else { servAddrPtr = (struct in_addr *) servPtr->servers[0]->addrList[0]; } /* * RFC1123 says we "SHOULD check the string syntactically for a * dotted-decimal number before looking it up [...]" (p. 13). */ if (queryType == T_A && IsAddr(host, &addr)) { result = GetHostInfoByAddr(servAddrPtr, &addr, &curHostInfo); } else { if (queryType == T_PTR) { CvtAddrToPtr(host); } result = GetHostInfoByName(servAddrPtr, queryClass, queryType, host, &curHostInfo, 0); } switch (result) { case SUCCESS: /* * If the query was for an address, then the &curHostInfo * variable can be used by Finger. * There's no need to print anything for other query types * because the info has already been printed. */ if (queryType == T_A) { curHostValid = TRUE; PrintHostInfo(filePtr, "Name:", &curHostInfo); } break; /* * No Authoritative answer was available but we got names * of servers who know about the host. */ case NONAUTH: PrintHostInfo(filePtr, "Name:", &curHostInfo); break; case NO_INFO:#ifndef GUI fprintf(stderr, "*** No %s (%s) records available for %s\n", DecodeType(queryType), p_type(queryType), host);#else fprintf(filePtr, "*** No %s (%s) records available for %s\n", DecodeType(queryType), p_type(queryType), host);#endif break; case TIME_OUT: fprintf(stderr, "*** Request to %s timed-out\n", serverName); break; default:#ifndef GUI fprintf(stderr, "*** %s can't find %s: %s\n", serverName, host, DecodeError(result));#else fprintf(filePtr, "*** %s can't find %s: %s\n", serverName, host, DecodeError(result));#endif } return result;}/* ****************************************************************************** * * LookupHost -- * * Asks the default name server for information about the * specified host or domain. The information is printed * if the lookup was successful. * * Results: * ERROR - the output file could not be opened. * + results of DoLookup * ****************************************************************************** */intLookupHost(string, putToFile) char *string; Boolean putToFile;{ char host[NAME_LEN]; char file[PATH_MAX]; int result; FILE *fileptr = NULL; /* * Invalidate the current host information to prevent Finger * from using bogus info. */ curHostValid = FALSE; /* * Parse the command string into the host and * optional output file name. * */ sscanf(string, " %s", host); /* removes white space */ if (!putToFile) {#ifndef GUI filePtr = stdout;#else fileptr = outputfile;#endif } else { filePtr = AppOpenFile(string, file); if (filePtr == NULL) { fprintf(stderr, "*** Can't open %s for writing\n", file); return(ERROR); } fprintf(filePtr,"> %s\n", string); } PrintHostInfo(filePtr, "Server:", defaultPtr); result = DoLookup(host, defaultPtr, defaultServer); if (putToFile) { fclose(filePtr); filePtr = NULL; } return(result);}/* ****************************************************************************** * * LookupHostWithServer -- * * Asks the name server specified in the second argument for * information about the host or domain specified in the first * argument. The information is printed if the lookup was successful. * * Address info about the requested name server is obtained * from the default name server. This routine will return an * error if the default server doesn't have info about the * requested server. Thus an error return status might not * mean the requested name server doesn't have info about the * requested host. * * Comments from LookupHost apply here, too. * * Results: * ERROR - the output file could not be opened. * + results of DoLookup * ****************************************************************************** */intLookupHostWithServer(string, putToFile) char *string; Boolean putToFile;{ char file[PATH_MAX]; char host[NAME_LEN]; char server[NAME_LEN]; int result; static HostInfo serverInfo; curHostValid = FALSE; sscanf(string, " %s %s", host, server); if (!putToFile) {#ifndef GUI filePtr = stdout;#else filePtr = outputfile;#endif } else { filePtr = AppOpenFile(string, file); if (filePtr == NULL) { fprintf(stderr, "*** Can't open %s for writing\n", file); return(ERROR); } fprintf(filePtr,"> %s\n", string); } result = GetHostInfoByName( defaultPtr->addrList ? (struct in_addr *) defaultPtr->addrList[0] : (struct in_addr *) defaultPtr->servers[0]->addrList[0], C_IN, T_A, server, &serverInfo, 1); if (result != SUCCESS) {#ifndef GUI fprintf(stderr,"*** Can't find address for server %s: %s\n", server, DecodeError(result));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -