The Winamp Remote Control suite
a remote control client and plugin for Winamp 2.x, 5.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
IPAddressResolver.cpp
Go to the documentation of this file.
1 /*
2  * IPAddressResolver.cpp
3  *
4  * Created on: 9 Feb 2013
5  * Author: Patrick
6  */
7 
8 #include "IPAddressResolver.h"
9 
10 #include <winsock.h>
11 
12 
13 namespace WinampRemote
14 {
15 namespace Net
16 {
17 
18 
19 bool GetIPAddress(std::string& const HostName, std::string &ResolvedName,
20  vector<std::string> &Addresses,
21  vector<std::string> &Aliases)
22 {
23 
24  // must NOT attempt to modify this
25  const hostent * hent = NULL;
26  WSADATA wsaData;
27  WORD wVersionRequested = MAKEWORD(2, 2);
28  int err;
29 
30  err = WSAStartup(wVersionRequested, &wsaData);
31  if (err == 0)
32  {
33  hent = gethostbyname(HostName.c_str());
34  WSACleanup();
35  }
36 
37  if (hent)
38  {
39  in_addr addr;
40  Addresses.clear();
41  Aliases.clear();
42  int i = 0;
43 
44  while ( (LPIN_ADDR)hent->h_addr_list[i] != NULL)
45  {
46  addr.S_un.S_addr = ( (LPIN_ADDR)hent->h_addr_list[i] )->s_addr;
47  Addresses.push_back(inet_ntoa( addr ));
48  i++;
49  }
50 
51  i = 0;
52 
53  while ( (char *)hent->h_aliases[i] != NULL)
54  {
55  Aliases.push_back(hent->h_aliases[i]);
56  i++;
57  }
58 
59  ResolvedName = hent->h_name;
60  return true;
61  }
62  else
63  {
64  /*
65  int WSAError = WSAGetLastError();
66  switch(WSAError)
67  {
68  case WSANOTINITIALISED : throw new EIPException("A successful WSAStartup must occur before using this function."); // no break
69  case WSAENETDOWN : throw new EIPException("The network subsystem has failed."); // no break
70  case WSAHOST_NOT_FOUND : throw new EIPException("Authoritative Answer Host not found."); // no break
71  case WSATRY_AGAIN : throw new EIPException("Non-Authoritative Host not found, or server failure."); // no break
72  case WSANO_RECOVERY : throw new EIPException("Non-recoverable error occurred."); // no break
73  case WSANO_DATA : throw new EIPException("Valid name, no data record of requested type."); // no break
74  case WSAEINPROGRESS : throw new EIPException("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function."); // no break
75  case WSAEFAULT : throw new EIPException("The name argument is not a valid part of the user address space."); // no break
76  case WSAEINTR : throw new EIPException("The (blocking) call was canceled through WSACancelBlockingCall."); // no break
77  default : throw new EIPException(AnsiString().sprintf("Unknown WSA Error %d", WSAError));
78  }
79  */
80  return false;
81  }
82 }
83 
84 
85 
86 IPAddressResolver::IPAddressResolver(std::string& const hostname):
87  m_hostname(hostname), m_resolved(),
88  m_addresses(), m_aliases()
89 
90 {
91 
92  GetIPAddress(m_hostname, m_resolved, m_addresses, m_aliases);
93 
94 }
95 
97 {
98 }
99 
100 std::vector<std::string> & IPAddressResolver::getAddresses()
101 {
102  return m_addresses;
103 }
104 
105 std::vector<std::string> & IPAddressResolver::getAliases()
106 {
107  return m_aliases;
108 }
109 std::string const & IPAddressResolver::hostname() const
110 {
111  return m_hostname;
112 }
113 
114 std::string const & IPAddressResolver::resolvedName() const
115 {
116  return m_resolved;
117 }
118 
119 } /* namespace Net */
120 } /* namespace WinampRemote */