Cuberite
A lightweight, fast and extensible game server for Minecraft
IPLookup.cpp
Go to the documentation of this file.
1 
2 // IPLookup.cpp
3 
4 // Implements the cIPLookup class representing an IP-to-hostname lookup in progress.
5 
6 #include "Globals.h"
7 #include "IPLookup.h"
8 #include <event2/util.h>
9 #include "NetworkSingleton.h"
10 #include "GetAddressInfoError.h"
11 
12 
13 
14 
15 
17 // cIPLookup:
18 
20  m_Callbacks(std::move(a_Callbacks)),
21  m_IP(a_IP)
22 {
23  ASSERT(m_Callbacks != nullptr);
24 }
25 
26 
27 
28 
29 
31 {
32  cIPLookupPtr Lookup{ new cIPLookup(a_IP, std::move(a_Callbacks)) }; // Cannot use std::make_shared here, constructor is not accessible
33 
34  // Note the Lookup object is owned solely by this lambda which is destroyed after it runs
36  {
37  sockaddr_storage sa;
38  int salen = sizeof(sa);
39  memset(&sa, 0, sizeof(sa));
40 
41  int ErrCode = evutil_parse_sockaddr_port(Lookup->m_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen);
42 
43  if (ErrCode != 0)
44  {
45  LOGD("Failed to parse IP address \"%s\".", Lookup->m_IP.c_str());
46  Lookup->Callback(ErrCode, nullptr);
47  return;
48  }
49 
50  char Hostname[NI_MAXHOST];
51  char ServInfo[NI_MAXSERV];
52 
53  ErrCode = getnameinfo(
54  reinterpret_cast<sockaddr *>(&sa),
55  static_cast<socklen_t>(salen),
56  Hostname, sizeof(Hostname),
57  ServInfo, sizeof(ServInfo),
58  0
59  );
60  Lookup->Callback(ErrCode, Hostname);
61  });
62 }
63 
64 
65 
66 
67 
68 void cIPLookup::Callback(int a_Result, const char * a_Address)
69 {
70  // Call the proper callback based on the event received:
71  if ((a_Result != 0) || (a_Address == nullptr))
72  {
73  // An error has occurred, notify the error callback:
74  m_Callbacks->OnError(a_Result, ErrorString(a_Result));
75  }
76  else
77  {
78  // Call the success handler:
79  m_Callbacks->OnNameResolved(a_Address, m_IP);
80  m_Callbacks->OnFinished();
81  }
82 }
83 
84 
85 
86 
87 
89 // cNetwork API:
90 
92  const AString & a_IP,
94 )
95 {
96  cIPLookup::Lookup(a_IP, std::move(a_Callbacks));
97  return true;
98 }
99 
100 
101 
102 
#define ASSERT(x)
Definition: Globals.h:276
#define LOGD
Definition: LoggerSimple.h:83
AString ErrorString(int a_ErrorCode)
Returns the readable form of a getaddressinfo type error code.
std::shared_ptr< cIPLookup > cIPLookupPtr
Definition: IPLookup.h:42
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
cIPLookup(const AString &a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks)
Creates the lookup object.
Definition: IPLookup.cpp:19
void Callback(int a_Result, const char *a_Address)
Callback that is called by LibEvent when there's an event for the request.
Definition: IPLookup.cpp:68
AString m_IP
The IP that was queried (needed for the callbacks).
Definition: IPLookup.h:34
cNetwork::cResolveNameCallbacksPtr m_Callbacks
The callbacks to call for resolved names / errors.
Definition: IPLookup.h:31
static void Lookup(const AString &a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks)
Creates a lookup object and schedules the lookup.
Definition: IPLookup.cpp:30
static bool IPToHostName(const AString &a_IP, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified IP address to a hostname.
Definition: IPLookup.cpp:91
std::shared_ptr< cResolveNameCallbacks > cResolveNameCallbacksPtr
Definition: Network.h:309
void ScheduleLookup(std::function< void()> a_Lookup)
Schedule a lookup task for execution.
static cNetworkSingleton & Get(void)
Returns the singleton instance of this class.
cNetworkLookup & GetLookupThread()
Returns the thread used to perform hostname and IP lookups.