在Linux上创建C语言程序发送DNSquery,“getostbyname”中出现分段错误错误函数和“int_ntoa”功能

发布于 2025-01-13 07:55:09 字数 1679 浏览 1 评论 0原文

#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 内

like this

我认为这个txt在c上打开=> “www.naver.com\nwww.google.co.kr\nwww.stackoverflow.com”

因此“\n”被用作键并分为二维数组。

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

这是相同的字符串,但我不知道为什么会出现错误。帮我。

#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]));
}

Inside input.txt

like this

I think this txt open on c => "www.naver.com\nwww.google.co.kr\nwww.stackoverflow.com"

so "\n" was used as a key and divided into two-dimensional arrays.

so i think (name == ListList) and print this Value is same!

It's the same string but I don't know why there's an error. Help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安稳善良 2025-01-20 07:55:09

上面的代码运行良好,但有一个问题是当 dns 查询失败时(例如:网络问题)-> 在这种情况下, gethostbyname将返回null

在使用之前需要处理检查gethostbyname的返回结果

,如果您不信任您的代码,则 ,让使用一些工具来验证 dns 查询是否成功,例如挖掘工具、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

stackoverflow.com 的评论检查中所示 ->它得到了 151.101.1.69,...

 dig stackoverflow.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> stackoverflow.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:
;stackoverflow.com.             IN      A

;; ANSWER SECTION:
stackoverflow.com.      35      IN      A       151.101.1.69
stackoverflow.com.      35      IN      A       151.101.65.69
stackoverflow.com.      35      IN      A       151.101.193.69
stackoverflow.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

the code above work well but one problem is when dns query is failed (ex : networking problem) -> gethostbyname will return null

in that case, you need handle with check return result of gethostbyname before using

if you don't trust your code , lets use some tool to verify dns query is success or not with dig tool, wireshark,...

below is what i check with kbphonemall.com -> no answer from dns server -> host variable is null -> your program is segfaulted as in your comment

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

check with stackoverflow.com -> It got 151.101.1.69,...

 dig stackoverflow.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> stackoverflow.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:
;stackoverflow.com.             IN      A

;; ANSWER SECTION:
stackoverflow.com.      35      IN      A       151.101.1.69
stackoverflow.com.      35      IN      A       151.101.65.69
stackoverflow.com.      35      IN      A       151.101.193.69
stackoverflow.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文