dns_app.cpp

(plain text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cassert>
#include <cerrno>
#include <cstring>
#include <iostream>

#ifdef _WIN32
#include <windows.h>
#else
#include <netdb.h>
#include <arpa/inet.h>
#endif

using namespace std;

int main (int argc, char *argv[])
{
#ifdef _WIN32
  WSAData data;
  assert(WSAStartup(MAKEWORD(1, 1), &data) == 0);
#endif

  for (int i = 1; i < argc; ++i)
    {
      struct hostent *entry = gethostbyname(argv[i]);

      if (entry)
	{
	  struct in_addr addr;
	  memcpy(&addr, entry->h_addr, entry->h_length);
	  cout << argv[i] << " => " << inet_ntoa(addr) << endl;
	}

      else cerr << "ERROR: could not resolve "
		<< argv[i] << ": " << hstrerror(h_errno) << endl;
    }

#ifdef _WIN32
  WSACleanup();
#endif

  return 0;
}