这是一个简单的程序,我编写它是为了找出一个域的所有A record
.
我遵守它并且没有收到任何错误或警告。
然后我运行它,我发现它给出了错误的IP,例如:
./a.out www.google.com
2.0.0.0
2.0.0.0
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
struct addrinfo addrC;
struct addrinfo *addrL;
struct addrinfo *temp;
memset(&addrC, 0, sizeof(addrC));
addrC.ai_family = AF_INET;
addrC.ai_socktype = SOCK_STREAM;
addrC.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(argv[1], "http", &addrC, &addrL) != 0)
{
perror("getaddrinfo!");
exit(1);
}
for (temp = addrL; temp != NULL; temp = temp->ai_next)
{
char addrBuf[BUFSIZ];
void *addrCount = &((struct sockaddr_in*)temp)->sin_addr;
inet_ntop(temp->ai_addr->sa_family, addrCount, addrBuf, sizeof(addrBuf));
printf("%s\n", addrBuf);
}
for (temp = addrL; temp != NULL; temp = addrL)
{
addrL = temp->ai_next;
free(temp);
}
return 0;
}
为什么?以及如何纠正它?