📄 host.c
字号:
/* * Copyright (c) 1986 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. *//* * Actually, this program is from Rutgers University, however it is * based on nslookup and other pieces of named tools, so it needs * that copyright notice. */#if _MINIX#include <sys/types.h>#include <sys/ioctl.h>#include <assert.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <net/hton.h>#include <net/netlib.h>#include <net/gen/in.h>#include <net/gen/inet.h>#include <net/gen/netdb.h>#include <net/gen/nameser.h>#include <net/gen/resolv.h>#include <net/gen/socket.h>#include <net/gen/tcp.h>#include <net/gen/tcp_io.h>typedef u8_t u_char;typedef u16_t u_short;typedef u32_t u_long;#undef ERROR#else#include <stdio.h>#include <sys/types.h>#include <arpa/nameser.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include <resolv.h>#include <sys/param.h>#include <strings.h>#include <ctype.h>#endifextern int h_errno;#define NUMMX 50#define SUCCESS 0#define TIME_OUT -1#define NO_INFO -2#define ERROR -3#define NONAUTH -4#define NAME_LEN 256#ifndef T_TXT#define T_TXT 16#endif#ifndef NO_DATA#define NO_DATA NO_ADDRESS#endif#ifndef C_HS#define C_HS 4#endifFILE *filePtr;struct state orig;extern struct state _res;static u8_t *cname = NULL;int getclass = C_IN;int gettype;int verbose = 0;int list = 0;int server_specified = 0;union querybuf;void main _ARGS(( int c, char *v[] ));static int parsetype _ARGS(( char *s ));static int parseclass _ARGS(( char *s ));static void hperror _ARGS(( int errno ));static void printanswer _ARGS(( struct hostent *hp ));static int ListHosts _ARGS(( char *namePtr, int queryType ));static int gethostinfo _ARGS(( char *name ));static int getdomaininfo _ARGS(( char *name, char *domain ));static int getinfo _ARGS(( char *name, char *domain, int type ));static int printinfo _ARGS(( union querybuf *answer, u8_t *eom, int filter, int isls ));static char *DecodeError _ARGS(( int result ));static u8_t *pr_rr _ARGS(( u8_t *cp, u8_t *msg, FILE *file, int filter ));static u8_t * pr_cdname _ARGS(( u8_t *cp, u8_t *msg, u8_t *name, int namelen ));static char *pr_class _ARGS(( int class ));static char *pr_type _ARGS(( int type ));static int tcpip_writeall _ARGS(( int fd, char *buf, unsigned siz ));voidmain(c, v) char **v;{ ipaddr_t addr; register struct hostent *hp; register char *s; register inverse = 0; register waitmode = 0; u8_t *oldcname; int ncnames; res_init(); _res.retrans = 5; if (c < 2) { fprintf(stderr, "Usage: host [-w] [-v] [-r] [-d] [-t querytype] [-c class] [-a] host [server]\n -w to wait forever until reply\n -v for verbose output\n -r to disable recursive processing\n -d to turn on debugging output\n -t querytype to look for a specific type of information\n -c class to look for non-Internet data\n -a is equivalent to '-v -t *'\n"); exit(1); } while (c > 2 && v[1][0] == '-') { if (strcmp (v[1], "-w") == 0) { _res.retry = 1; _res.retrans = 15; waitmode = 1; v++; c--; } else if (strcmp (v[1], "-r") == 0) { _res.options &= ~RES_RECURSE; v++; c--; } else if (strcmp (v[1], "-d") == 0) { _res.options |= RES_DEBUG; v++; c--; } else if (strcmp (v[1], "-v") == 0) { verbose = 1; v++; c--; } else if (strcmp (v[1], "-l") == 0) { list = 1; v++; c--; } else if (strncmp (v[1], "-t", 2) == 0) { v++; c--; gettype = parsetype(v[1]); v++; c--; } else if (strncmp (v[1], "-c", 2) == 0) { v++; c--; getclass = parseclass(v[1]); v++; c--; } else if (strcmp (v[1], "-a") == 0) { verbose = 1; gettype = T_ANY; v++; c--; } } if (c > 2) { s = v[2]; server_specified++; addr = inet_addr(s); if (addr == -1) { hp = gethostbyname(s); if (hp == NULL) { fprintf(stderr,"Error in looking up server name:\n"); hperror(h_errno); exit(1); } _res.nsaddr= *(ipaddr_t *)hp->h_addr; printf("Using domain server:\n"); printanswer(hp); } else { _res.nsaddr_list[0]= addr; _res.nsport_list[0]= htons(NAMESERVER_PORT); printf("Using domain server %s:\n", inet_ntoa(_res.nsaddr)); } } if (strcmp (v[1], ".") == 0) addr = -1; else addr = inet_addr(v[1]); hp = NULL; h_errno = TRY_AGAIN;/* * we handle default domains ourselves, thank you */ _res.options &= ~RES_DEFNAMES; if (list) exit(ListHosts(v[1], gettype ? gettype : T_A)); oldcname = NULL; ncnames = 5; while (hp == NULL && h_errno == TRY_AGAIN) { if (addr == -1) { cname = NULL; if (oldcname == NULL) hp = (struct hostent *)gethostinfo(v[1]); else hp = (struct hostent *)gethostinfo((char *)oldcname); if (cname) { if (ncnames-- == 0) { printf("Too many cnames. Possible loop.\n"); exit(1); } oldcname = cname; hp = NULL; h_errno = TRY_AGAIN; continue; } } else { hp = gethostbyaddr((char *)&addr, 4, AF_INET); if (hp) printanswer(hp); } if (!waitmode) break; } if (hp == NULL) { hperror(h_errno); exit(1); } exit(0);}static intparsetype(s) char *s;{if (strcmp(s,"a") == 0) return(1);if (strcmp(s,"ns") == 0) return(2);if (strcmp(s,"md") == 0) return(3);if (strcmp(s,"mf") == 0) return(4);if (strcmp(s,"cname") == 0) return(5);if (strcmp(s,"soa") == 0) return(6);if (strcmp(s,"mb") == 0) return(7);if (strcmp(s,"mg") == 0) return(8);if (strcmp(s,"mr") == 0) return(9);if (strcmp(s,"null") == 0) return(10);if (strcmp(s,"wks") == 0) return(11);if (strcmp(s,"ptr") == 0) return(12);if (strcmp(s,"hinfo") == 0) return(13);if (strcmp(s,"minfo") == 0) return(14);if (strcmp(s,"mx") == 0) return(15);if (strcmp(s,"txt") == 0) /* Roy */ return(T_TXT); /* Roy */if (strcmp(s,"uinfo") == 0) return(100);if (strcmp(s,"uid") == 0) return(101);if (strcmp(s,"gid") == 0) return(102);if (strcmp(s,"unspec") == 0) return(103);if (strcmp(s,"any") == 0) return(255);if (strcmp(s,"*") == 0) return(255);if (atoi(s)) return(atoi(s));fprintf(stderr, "Invalid query type: %s\n", s);exit(2);}static intparseclass(s) char *s;{if (strcmp(s,"in") == 0) return(C_IN);if (strcmp(s,"hs") == 0) return(C_HS);if (strcmp(s,"any") == 0) return(C_ANY);if (atoi(s)) return(atoi(s));fprintf(stderr, "Invalid query class: %s\n", s);exit(2);}static voidprintanswer(hp) register struct hostent *hp;{ register char **cp; register ipaddr_t **hptr; printf("Name: %s\n", hp->h_name); printf("Address:"); for (hptr = (ipaddr_t **)hp->h_addr_list; *hptr; hptr++) printf(" %s", inet_ntoa(*(ipaddr_t *)*hptr)); printf("\nAliases:"); for (cp = hp->h_aliases; cp && *cp && **cp; cp++) printf(" %s", *cp); printf("\n\n");}static voidhperror(errno) int errno;{switch(errno) { case HOST_NOT_FOUND: fprintf(stderr,"Host not found.\n"); break; case TRY_AGAIN: fprintf(stderr,"Host not found, try again.\n"); break; case NO_RECOVERY: fprintf(stderr,"No recovery, Host not found.\n"); break; case NO_ADDRESS: fprintf(stderr,"There is an entry for this host, but it doesn't have an Internet address.\n"); break; }}typedef union querybuf { dns_hdr_t qb1; u8_t qb2[PACKETSZ];} querybuf_t;static u8_t hostbuf[BUFSIZ+1];static intgethostinfo(name) char *name;{ register char *cp, **domain; int n; int hp; int nDomain; if (strcmp(name, ".") == 0) return(getdomaininfo(name, NULL)); for (cp = name, n = 0; *cp; cp++) if (*cp == '.') n++; if (n && cp[-1] == '.') { if (cp[-1] == '.') cp[-1] = 0; hp = getdomaininfo(name, (char *)NULL); if (cp[-1] == 0) cp[-1] = '.'; return (hp); } if (n == 0 && (cp = __hostalias(name))) { if (verbose) printf("Aliased to \"%s\"\n", cp); _res.options |= RES_DEFNAMES; return (getdomaininfo(cp, (char *)NULL)); }#ifdef MAXDS for (nDomain = 0; _res.defdname_list[nDomain][0] != 0; nDomain++) { for (domain = _res.dnsrch_list[nDomain]; *domain; domain++) { if (verbose) printf("Trying domain \"%s\"\n", *domain); hp = getdomaininfo(name, *domain); if (hp) return (hp); } }#else for (domain = _res.dnsrch; *domain; domain++) { if (verbose) printf("Trying domain \"%s\"\n", *domain); hp = getdomaininfo(name, *domain); if (hp) return (hp); }#endif if (h_errno != HOST_NOT_FOUND || (_res.options & RES_DNSRCH) == 0) return (0); if (verbose) printf("Trying null domain\n"); return (getdomaininfo(name, (char *)NULL));}static intgetdomaininfo(name, domain) char *name, *domain;{ int val1, val2; if (gettype) return getinfo(name, domain, gettype); else { val1 = getinfo(name, domain, T_A); if (cname || verbose) return val1; val2 = getinfo(name, domain, T_MX); return val1 || val2; }}static intgetinfo(name, domain, type) char *name, *domain;{ dns_hdr_t *hp; u8_t *eom, *bp, *cp; querybuf_t buf, answer; int n, n1, i, j, nmx, ancount, nscount, arcount, qdcount, buflen; u_short pref, class; char host[2*MAXDNAME+2]; if (domain == NULL) (void)sprintf(host, "%.*s", MAXDNAME, name); else (void)sprintf(host, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain); n = res_mkquery(QUERY, host, getclass, type, (char *)NULL, 0, NULL, (char *)&buf, sizeof(buf)); if (n < 0) { if (_res.options & RES_DEBUG) printf("res_mkquery failed\n"); h_errno = NO_RECOVERY; return(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -