2012-03-22 67 views
1

我有一个包含IP地址的变量。我试图做一个NSLOOKUP而不是返回的DNS名称我得到0.我在一个Linux环境。目标IP来自一个向量(字符串dest_ip =向量[2])。gethostbyaddr返回0

#include <stdio.h> 
#include <iostream> 
#include <string.h> 
#include <arpa/inet.h> 
#include <netdb.h> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <sstream> 

using namespace std; 

void split(const std::string& str, std::vector<std::string>& v) { 
    std::stringstream ss(str); 
    ss >> std::noskipws; 
    std::string field; 
    char ws_delim; 
    while(1) { 
     if(ss >> field) 
      v.push_back(field); 
     else if (ss.eof()) 
      break; 
     else 
      v.push_back(std::string()); 
     ss.clear(); 
     ss >> ws_delim; 
    } 
} 

int main() 
{ 

    string input_line; 

    while(cin){ 

    getline(cin, input_line); 

    for(int i=0; input_line[i]; i++) 
         if(input_line[i] == ':') input_line[i] = ' '; 
        for(int i=0; input_line[i]; i++) 
         if(input_line[i] == '/') input_line[i] = ' '; 

    std::vector<std::string> v; 
    split(input_line, v); 

    string dest_ip = v[4]; 

    struct hostent *he; 
    int i,len,type; 
    len = dest_ip.length(); 
    type=AF_INET; 

    he = gethostbyaddr(dest_ip.c_str(),len,type); 

    cout<<"Hostname: "<<he<<"\n"; 

    return 0; 
} 

再次,而不是接收的主机名,我得到0

+3

为什么不显示你的真实代码?你的例子甚至不会编译。 – 2012-03-22 15:14:53

+0

什么是在dest_ip? – 2012-03-22 15:15:22

+0

“desp_ip”在哪里设置? – 2012-03-22 15:16:12

回答

4

您不能直接传递C风格的字符串(即空值终止),以gethostbyaddr

您需要创建一个struct in_addr并将指针传递给创建的结构,作为gethostbyaddr的第一个参数。要使用inet_atonchar const*生成struct in_addr

下面的例子是从人gethostbyaddr采取:


实施例

  • 打印出与特定的IP地址相关联的主机名:

    const char *ipstr = "127.0.0.1"; 
    struct in_addr ip; 
    struct hostent *hp; 
    
    if (!inet_aton(ipstr, &ip)) 
         errx(1, "can't parse IP address %s", ipstr); 
    
    if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL) 
         errx(1, "no name associated with %s", ipstr); 
    
    printf("name associated with %s is %s\n", ipstr, hp->h_name); 
    

我该如何进一步检查以确定哪里出了问题?

如果使用gethostbyaddr回报NULL你应该检查什么地方出了错通过查看变量h_errno

h_errno可以有下面定义的值之一:

  1. HOST_NOT_FOUND
  2. TRY_AGAIN
  3. NO_RECOVERY
  4. NO_DATA

请咨询您的手册,了解有关问题的更多细节。


您的片段是完全错误的..

您提供甚至不编译的代码片段,但你的方式显示你正在尝试什么来完成,但我不知道这对某些。 这篇文章包含的细节应该被认为是“受过教育的猜测”。

OP改变了他的帖子..

2

gethostbyaddr每当发生一些错误返回一个空指针。错误可能是一个错误的IP地址,一个未知的主机,错误配置的DNS设置等。您需要检查实际的错误代码。在Winsock上,这将意味着拨打WSAGetLastError,而在POSIX上(我认为)您需要检查h_errno的值。 (我可能是错的POSIX,我没有经验,有)

+0

+1并遵循MSDN''gethostbyaddr()''弃用'getnameinfo()'。 – hochl 2012-03-22 15:26:30

+0

这篇文章有点令人误解,OP“试图传递一个无效值作为'gethostbyaddr'的第一个参数是”显而易见的“。 – 2012-03-22 15:33:37

+0

@refp:当我最初发布此答案时发布的代码未编译,并且OP未发布详细信息。我能做的最好的事情是发布文档。这个问题已经被更新,使这不是最好的答案(尽管如此,我认为,不是一个错误的答案)。虽然我已经为你的答案+1了,最好给OP的更新代码。 – 2012-03-22 16:16:30

0
class CIPManager 
{ 
public: 
    CIPManager() 
    { 
     WSADATA wsaData; 
     int iResult; 
     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); 
     if (iResult != 0) { 
      printf("WSAStartup failed: %d\n", iResult); 
      //return 1; 
     } 
    } 

    bool getPCName(unsigned long ip,char* hostname)//string& strIP) 
    { 
     DWORD dwRetval; 
     //char hostname[NI_MAXHOST]; 
     char servInfo[NI_MAXSERV]; 
     u_short port = 27015; 
     //hostname=NULL; 

     // Validate the parameters 
     /*if (argc != 2) { 
      printf("usage: %s IPv4 address\n", argv[0]); 
      printf(" to return hostname\n"); 
      printf("  %s 127.0.0.1\n", argv[0]); 
      return 1; 
     }*/ 
     // Initialize Winsock 
     /* 
     */ 
     //----------------------------------------- 
     // Set up sockaddr_in structure which is passed 
     // to the getnameinfo function 
     saGNI.sin_family = AF_INET; 
     saGNI.sin_addr.s_addr =ip;// inet_addr(strIP); 
     saGNI.sin_port = htons(port); 

     //----------------------------------------- 
     // Call getnameinfo 
     dwRetval = getnameinfo((struct sockaddr *) &saGNI, 
      sizeof (struct sockaddr), 
      hostname, 
      NI_MAXHOST, servInfo, 
      NI_MAXSERV, NI_NUMERICSERV); 

     if (dwRetval != 0) { 
      printf("getnameinfo failed with error # %ld\n", WSAGetLastError()); 
      //return 1; 
      return NULL; 
     } else { 
     // printf("getnameinfo returned hostname = %s\n", hostname); 
      //return hostname; 
      return 1; 
     } 
    } 

protected: 
private: 
    struct sockaddr_in saGNI; 

}; 

使用则getnameinfo是一个更好的解决方案!