getaddrinfo,我没有得到任何canonname [英] getaddrinfo, I am not getting any canonname

查看:762
本文介绍了getaddrinfo,我没有得到任何canonname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取特定主机的所有信息并打印出每个信息。
我可以读取和打印所有的地址,但我没有读任何ai_canonname!

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!

首先我想我的例子(www.google.com | www。 irs.gov | ...)没有标准名称,但过了一会儿,我想我没有得到任何名称。

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?

这是我的代码,

#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;
}


推荐答案

getaddrinfo() 说你应添加:

The specification for getaddrinfo() says you should add:

hints.ai_flags = AI_CANONNAME;

,你会得到你要求的。

$ ./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
$

我不知道如何改变IRS的身份。 Canonical并不意味着所有的规范,似乎。

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.

这篇关于getaddrinfo,我没有得到任何canonname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆