在Linux上创建C语言程序发送DNSquery,“getostbyname”中出现分段错误错误函数和“int_ntoa”功能
#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.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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码运行良好,但有一个问题是当 dns 查询失败时(例如:网络问题)-> 在这种情况下,
gethostbyname
将返回null
在使用之前需要处理检查
gethostbyname
的返回结果,如果您不信任您的代码,则 ,让使用一些工具来验证 dns 查询是否成功,例如挖掘工具、wireshark...
下面是我使用
kbphonemall.com
检查的内容 -> DNS 服务器没有应答 ->host
变量为 null ->您的程序存在段错误,如您在stackoverflow.com 的评论检查中所示 ->它得到了 151.101.1.69,...
the code above work well but one problem is when dns query is failed (ex : networking problem) ->
gethostbyname
will returnnull
in that case, you need handle with check return result of
gethostbyname
before usingif 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 commentcheck with stackoverflow.com -> It got 151.101.1.69,...