이것은 간단한 프로그램입니다, 나는 도메인 A record
을 모두 알아 내기 위해 그것을 작성합니다.리눅스 네트워크 프로그래밍 : getaddrinfo() 잘못된 결과를 얻으십시오
필자는 오류를 경고하고 경고를받지 않습니다.
./a.out www.google.com
2.0.0.0
2.0.0.0
: 그럼 난, 난 단지 그와 같은 잘못된 IP를 제공 발견 실행
내 코드 :
#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;
}
왜? 어떻게 수정해야할까요?
'struct addrinfo'는'struct sockaddr_in'이 아니며 그 사이에 캐스팅하면 쓰레기가 생길 수 있습니다. 대신에'temp-> ai_addr'을 쓰면됩니다. 이것은'sockadr_in '입니다. (왜 'addrCount'가 나를 넘어선 결과로 이름 지었는가? 그것은 주소 일 뿐이며, 무엇이든 아닌). –