使用C ++将IP转换为域

时间:2014-03-15 12:11:50

标签: c++ dns

我想用C ++语言编写一个程序来返回给定IP地址的域列表(我认为它是Revers DNS)。
它类似于BING IP反向方法。
另外我对WinPcap很熟悉。
我测试了gethostbyaddr()和getaddrinfo()以及其他功能,但实际上我无法做到我想要的。
什么是最好的报价?

1 个答案:

答案 0 :(得分:0)

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
}

host_addr = argv[2];

printf("Calling gethostbyaddr with %s\n", host_addr);
if (bIpv6 == 1) {
    {
        iResult = inet_pton(AF_INET6, host_addr, &addr6);
        if (iResult == 0) {
            printf("The IPv6 address entered must be a legal address\n");
            return 1;
        } else
            remoteHost = gethostbyaddr((char *) &addr6, 16, AF_INET6);
    }
} else {
    addr.s_addr = inet_addr(host_addr);
    if (addr.s_addr == INADDR_NONE) {
        printf("The IPv4 address entered must be a legal address\n");
        return 1;
    } else
        remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
}

if (remoteHost == NULL) {
    dwError = WSAGetLastError();
    if (dwError != 0) {
        if (dwError == WSAHOST_NOT_FOUND) {
            printf("Host not found\n");
            return 1;
        } else if (dwError == WSANO_DATA) {
            printf("No data record found\n");
            return 1;
        } else {
            printf("Function failed with error: %ld\n", dwError);
            return 1;
        }
    }