summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/IPLookup.cpp
blob: f730110b92072b5bf94689ba43036dcbb1f5ecb3 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

// IPLookup.cpp

// Implements the cIPLookup class representing an IP-to-hostname lookup in progress.

#include "Globals.h"
#include "IPLookup.h"
#include <event2/util.h>
#include "NetworkSingleton.h"
#include "GetAddressInfoError.h"





////////////////////////////////////////////////////////////////////////////////
// cIPLookup:

cIPLookup::cIPLookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks):
	m_Callbacks(std::move(a_Callbacks)),
	m_IP(a_IP)
{
	ASSERT(m_Callbacks != nullptr);
}





void cIPLookup::Lookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks)
{
	cIPLookupPtr Lookup{ new cIPLookup(a_IP, std::move(a_Callbacks)) };  // Cannot use std::make_shared here, constructor is not accessible

	// Note the Lookup object is owned solely by this lambda which is destroyed after it runs
	cNetworkSingleton::Get().GetLookupThread().ScheduleLookup([=]()
	{
		sockaddr_storage sa;
		int salen = sizeof(sa);
		memset(&sa, 0, sizeof(sa));

		int ErrCode = evutil_parse_sockaddr_port(Lookup->m_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen);

		if (ErrCode != 0)
		{
			LOGD("Failed to parse IP address \"%s\".", Lookup->m_IP.c_str());
			Lookup->Callback(ErrCode, nullptr);
			return;
		}

		char Hostname[NI_MAXHOST];
		char ServInfo[NI_MAXSERV];

		ErrCode = getnameinfo(
			reinterpret_cast<sockaddr *>(&sa),
			static_cast<socklen_t>(salen),
			Hostname, sizeof(Hostname),
			ServInfo, sizeof(ServInfo),
			0
		);
		Lookup->Callback(ErrCode, Hostname);
	});
}





void cIPLookup::Callback(int a_Result, const char * a_Address)
{
	// Call the proper callback based on the event received:
	if ((a_Result != 0) || (a_Address == nullptr))
	{
		// An error has occurred, notify the error callback:
		m_Callbacks->OnError(a_Result, ErrorString(a_Result));
	}
	else
	{
		// Call the success handler:
		m_Callbacks->OnNameResolved(a_Address, m_IP);
		m_Callbacks->OnFinished();
	}
}





////////////////////////////////////////////////////////////////////////////////
// cNetwork API:

bool cNetwork::IPToHostName(
	const AString & a_IP,
	cNetwork::cResolveNameCallbacksPtr a_Callbacks
)
{
	cIPLookup::Lookup(a_IP, std::move(a_Callbacks));
	return true;
}