Bogus IP address returned by resolver class
Brought to you by:
chris_kohlhoff
I am writing this small piece of code to test the resolver class:
string host;
io_service my_io_service;
for(;;)
{
cout << "Enter host name:" << endl;
cin.clear();
getline(cin, host);
if(host == "q") break;
ip::tcp::resolver resolver(my_io_service);
ip::tcp::resolver::query query(host, "http");
ip::tcp::resolver::iterator iter = resolver.resolve(query);
ip::tcp::resolver::iterator end; // End marker.
while (iter != end)
{
ip::tcp::endpoint endpoint = *iter++;
std::cout << endpoint << std::endl;
}
}
When the query is empty (""), resolver returns an arbitrary IP address.
This does not reproduce on my system, Mac OS 10.6.3. What does getaddrinfo with an empty hostname parameter return on your system?
Sorry, it might be a false report.
I meant if host is an arbitrary string (like '?' or '<>') the returned address looks suspicious.
I am no familiar with TCP/IP and the socket thing, I thought it would return nothing because the host name is apparently invalid.
I did test using the following code, and it seems the WinSock API returns the same IP address, which seems like a router.
WSADATA wsaData;
int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (err)
{
puts("WSAStartup error");
return 0;
}
char* ip = "<>!@#%@%";
char* port = "80";
struct addrinfo aiHints;
struct addrinfo *aiList = NULL;
//--------------------------------
// Setup the hints address info structure
// which is passed to the getaddrinfo() function
memset(&aiHints, 0, sizeof(aiHints));
aiHints.ai_family = AF_INET;
aiHints.ai_socktype = SOCK_STREAM;
aiHints.ai_protocol = IPPROTO_TCP;
//--------------------------------
// Call getaddrinfo(). If the call succeeds,
// the aiList variable will hold a linked list
// of addrinfo structures containing response
// information about the host
err = getaddrinfo(ip, port, &aiHints, &aiList);
if(err)
{
printf("getaddrinfo() failed. %s\n", gai_strerror(err));
}
else
{
for(addrinfo* p = aiList; p; p = p->ai_next)
{
sockaddr_in* psock = (sockaddr_in*)p->ai_addr;
printf("family:%d,sock:%d,proto:%d,name:%s,ip:%s:%d\n",
p->ai_family, p->ai_socktype,
p->ai_protocol, p->ai_canonname,
inet_ntoa(psock->sin_addr), (int)psock->sin_port);
}
freeaddrinfo(aiList);
}
WSACleanup();
BTW, I am on Windows platform.