在Linux上创建C语言程序发送DNSquery,在"getostbyname"函数和"int_ntoa"函数中出现segmentation fault错误

Creates a C language program on Linux sends DNSquery, and the segmentation fault error appears in the "getostbyname" function and "int_ntoa" function

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define IPATH "input.txt"

int main(int argc, char *argv[]){

  char *ListBuffer;

  int ListSize;
  int ListCount = 0;

  FILE *InputFile = fopen(IPATH, "r");

  fseek(InputFile, 0, SEEK_END);
  ListSize = ftell(InputFile); 


  ListBuffer = malloc(ListSize + 1); 
  memset(ListBuffer, 0, ListSize + 1); 

  fseek(InputFile, 0, SEEK_SET);
  fread(ListBuffer, ListSize, 1, InputFile); 

  for (int i = 0; ListBuffer[i] != 0; i++) {
      if (ListBuffer[i] == '\n')
          ListCount++; 
  }

  int ListHeight = ListCount + 1;
  char *ListList[ListHeight - 1];

  char *str1 = NULL;
  char *temp1 = strtok_r(ListBuffer, "\n", &str1);

  int len = 0;

  while (temp1 != NULL){
      ListList[len] = temp1;
      temp1 = strtok_r(NULL, "\n", &str1);
      len++;
  }


  char *name = "www.naver.com";


  struct hostent *host;       //i think ListList[0] == "www.naver.com" 
  host = gethostbyname(name); //<= i want change variable name to ListList[0] 
  //host = gethostbyname(ListList[0]); like this
  printf("%s\n", name);


  printf("%s\n", inet_ntoa(*(struct in_addr *)host->h_addr_list[0]));
}

里面input.txt

我认为此 txt 在 c 上打开 => "www.naver.com\nwww.google.co.kr\nwww.whosebug.com"

所以以"\n"为key,分成二维数组

所以我认为 (name == ListList) 和打印这个值是一样的!

一样的字符串,不知道为什么会出错。帮帮我。

上面的代码运行良好,但一个问题是当 dns 查询失败时(例如:网络问题)-> gethostbyname 将 return null

在这种情况下,您需要在使用

之前处理 gethostbyname 的检查 return 结果

如果您不信任您的代码,让我们使用一些工具来验证 dns 查询是否成功,包括 dig 工具、wireshark、...

下面是我用 kbphonemall.com 检查的内容 -> 没有来自 dns 服务器的回答 -> host 变量为 null -> 你的程序在你的评论中出现段错误

dig kbphonemall.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> kbphonemall.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 14201
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;kbphonemall.com.               IN      A

;; AUTHORITY SECTION:
com.                    759     IN      SOA     a.gtld-servers.net. nstld.verisign-grs.com. 1646983748 1800 900 604800 86400

;; Query time: 86 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Fri Mar 11 14:31:46 +07 2022
;; MSG SIZE  rcvd: 117

检查 whosebug.com -> 它得到 151.101.1.69,...

 dig whosebug.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> whosebug.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52635
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;whosebug.com.             IN      A

;; ANSWER SECTION:
whosebug.com.      35      IN      A       151.101.1.69
whosebug.com.      35      IN      A       151.101.65.69
whosebug.com.      35      IN      A       151.101.193.69
whosebug.com.      35      IN      A       151.101.129.69

;; Query time: 84 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Fri Mar 11 14:50:20 +07 2022
;; MSG SIZE  rcvd: 110