⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nslookup.c

📁 UNIX/Linux环境下的使用gethostbyname()实现域名解析的代码。
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <netdb.h>#include <arpa/inet.h>extern int h_errno;int main(int argc, char **argv){  if (argc < 2)  {    fprintf(stdout, "Usage: %s <domain name/ip>\n", argv[0]);    exit(1);  }  struct hostent *h;  //struct hostent *gethostbyname(const char *name);  h = gethostbyname(argv[1]);  /*   * The gethostbyname() and gethostbyaddr() functions return the hostent structure or a NULL pointer  if  an   * error  occurs.   On  error, the h_errno variable holds an error number.  When non-NULL, the return value   * may point at static data, see the Notes below.   */  if (h == NULL)  {    //fprintf(stderr, "gethostbyname() failed: %s\n", strerror(h_errno));    switch (h_errno)    {      case HOST_NOT_FOUND:	fprintf(stderr, "The specified host is unknown.\n");	break;      case NO_ADDRESS:		//NO_DATA:	fprintf(stderr, "The requested name is valid but does not have an IP address.\n");	break;      case NO_RECOVERY:	fprintf(stderr, "A non - recoverable name server error occurred.\n");	break;      case TRY_AGAIN:	fprintf(stderr, "A temporary error occurred on an authoritative name server.Try again later.\n");	break;      default:	break;    }  }  fprintf(stdout, "gethostbyname() successed.\n");#if 0  struct hostent  {    char *h_name;		/* official name of host */    char **h_aliases;		/* alias list */    int h_addrtype;		/* host address type */    int h_length;		/* length of address */    char **h_addr_list;		/* list of addresses */  }#  define h_addr  h_addr_list[0]	/* for backward compatibility */#endif  fprintf(stdout, "official name: %s\n", h->h_name);  // XXX: output h_aliases  char **p;  p = h->h_aliases;  while (p && *p)  {    fprintf(stdout, "alias: %s\n", *p);    p++;  }  fprintf(stdout, "host address type: %d(AF_INET: %d, AF_INET6: %d)\n", h->h_addrtype, AF_INET, AF_INET6);  fprintf(stdout, "address length: %d\n", h->h_length);  // XXX: output h_addr_list  p = h->h_addr_list;  while (p && *p)  {    fprintf(stdout, "Address: %s\n", inet_ntoa(*(struct in_addr *) *p));    p++;  }  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -