问题
I am trying to read all the information about specific host and print out every information. I can read and print out all the addresses but I am not reading any ai_canonname!
First I thought my examples(www.google.com|www.irs.gov|...) don't have canon name, but after a while I figured I am not getting any name at all. Do you think I am doing something wrong or do you have an example that would work?
Here is my code,
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
struct addrinfo *result, *rp, hints;
int error;
char canonname[32][256];
int canonnum = 0;
char ip[32][64];
int ipnum = 0;
struct timeval tv;
uint64_t starttime, endtime;
if(argc<2)
{
printf("Usage: %s <address>\n", argv[0]);
return 0;
}
/* Record Start time */
gettimeofday(&tv, NULL);
starttime = tv.tv_usec;
memset(&hints, 0, sizeof(hints));
memset(canonname, 0, 32*256*sizeof(char));
memset(ip, 0, 32*64*sizeof(char));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
error = getaddrinfo(argv[1], NULL, &hints, &result);
if (error != 0)
{
if (error == EAI_SYSTEM)
{
perror("getaddrinfo");
}
else
{
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
}
return -1;
}
strcpy(canonname[0], "");
if(result->ai_canonname != NULL)
strcpy(canonname[0], result->ai_canonname);
canonnum++;
for(rp = result; rp != NULL; rp = rp->ai_next) {
if(rp !=result && rp->ai_canonname != NULL)
{
if(strcmp(rp->ai_canonname, "")!=0)
{
strcpy(canonname[canonnum],rp->ai_canonname);
canonnum++;
}
}
struct sockaddr_in *inaddr_ptr;
if (rp->ai_addr->sa_family == AF_INET)
inaddr_ptr = (struct sockaddr_in *)rp->ai_addr;
sprintf(ip[ipnum],"%s\n", inet_ntoa(inaddr_ptr->sin_addr));
ipnum++;
}
/* Gets the end time and prints out the execution time */
gettimeofday(&tv, NULL);
endtime = tv.tv_usec;
printf("Execution time: %llu milliseconds\n",(endtime - starttime)/100);
printf("Official name: %s\n", canonname[0]);
printf("Aliases:\n");
for(int i=1;i<canonnum;i++)
printf("%s\n",canonname[i]);
printf("Addresses:\n");
for(int i=0;i<ipnum;i++)
printf("%s",ip[i]);
freeaddrinfo(result);
return 0;
}
回答1:
The specification for getaddrinfo() says you should add:
hints.ai_flags = AI_CANONNAME;
and you will get what you ask for.
$ ./gai www.ibm.com
Execution time: 4499 milliseconds
Official name: www.ibm.com
Aliases:
Addresses:
129.42.60.216
$ ./gai www.google.com
Execution time: 248 milliseconds
Official name: www.google.com
Aliases:
Addresses:
74.125.239.50
74.125.239.49
74.125.239.48
74.125.239.52
74.125.239.51
$ ./gai www.irs.gov
Execution time: 2872 milliseconds
Official name: 63-146-70-67.dia.static.qwest.net
Aliases:
Addresses:
63.146.70.67
63.146.70.96
$ ./gai www.irs.gov
Execution time: 2299 milliseconds
Official name: 63-146-70-96.dia.static.qwest.net
Aliases:
Addresses:
63.146.70.96
63.146.70.67
$
I'm not sure what to make of the changing identity of the IRS. Canonical doesn't mean as canonical as all that, it seems.
来源:https://stackoverflow.com/questions/18884251/getaddrinfo-i-am-not-getting-any-canonname