Home > front end >  Creates a C language program on Linux sends DNSquery, and the segmentation fault error appears in th
Creates a C language program on Linux sends DNSquery, and the segmentation fault error appears in th

Time:03-11

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

CodePudding user response:

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
  • Related