Commit is being made to allow additions of GPL3+ code previously un-addable. With these changes, contributions back to cuberite are possible with the backporting exemtion, as well as adding stuff in minetest with minetest code properly being read through and implimented to upgrade it to GPL3 from GPL2. project still has Apache2.0 license and credits to all its contributers, but now has the freedom of GPL3+ and all the code that can be implimented and shared with it.
112 lines
2.4 KiB
C++
112 lines
2.4 KiB
C++
|
|
/*
|
|
* Copyright 2011-2022 Cuberite Contributors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
// NameLookup.cpp
|
|
|
|
// Implements a DNS name lookup using the LibEvent-based cNetwork API
|
|
|
|
#include "Globals.h"
|
|
#include <thread>
|
|
#include "OSSupport/Event.h"
|
|
#include "OSSupport/Network.h"
|
|
#include "OSSupport/NetworkSingleton.h"
|
|
|
|
|
|
|
|
|
|
|
|
class cFinishLookupCallbacks:
|
|
public cNetwork::cResolveNameCallbacks
|
|
{
|
|
cEvent & m_Event;
|
|
|
|
virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) override
|
|
{
|
|
LOGD("%s resolves to IP %s", a_Name.c_str(), a_IP.c_str());
|
|
}
|
|
|
|
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
|
|
{
|
|
LOGD("Error %d (%s) while performing lookup!", a_ErrorCode, a_ErrorMsg.c_str());
|
|
exit(a_ErrorCode);
|
|
}
|
|
|
|
virtual void OnFinished(void) override
|
|
{
|
|
LOGD("Resolving finished.");
|
|
m_Event.Set();
|
|
}
|
|
|
|
public:
|
|
cFinishLookupCallbacks(cEvent & a_Event):
|
|
m_Event(a_Event)
|
|
{
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void DoTest(void)
|
|
{
|
|
cEvent evtFinish;
|
|
|
|
// Look up google.com (has multiple IP addresses):
|
|
LOGD("Network test: Looking up google.com");
|
|
if (!cNetwork::HostnameToIP("google.com", std::make_shared<cFinishLookupCallbacks>(evtFinish)))
|
|
{
|
|
LOGWARNING("Cannot resolve google.com to IP");
|
|
abort();
|
|
}
|
|
LOGD("Name lookup has been successfully queued");
|
|
evtFinish.Wait();
|
|
LOGD("Lookup finished.");
|
|
|
|
// Look up 8.8.8.8 (Google free DNS):
|
|
LOGD("Network test: Looking up IP 8.8.8.8");
|
|
if (!cNetwork::IPToHostName("8.8.8.8", std::make_shared<cFinishLookupCallbacks>(evtFinish)))
|
|
{
|
|
LOGWARNING("Cannot resolve 8.8.8.8 to name");
|
|
abort();
|
|
}
|
|
LOGD("IP lookup has been successfully queued");
|
|
evtFinish.Wait();
|
|
LOGD("IP lookup finished.");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
{
|
|
LOGD("Initializing cNetwork...");
|
|
cNetworkSingleton::Get().Initialise();
|
|
|
|
LOGD("Running test...");
|
|
DoTest();
|
|
|
|
cNetworkSingleton::Get().Terminate();
|
|
LOGD("Network test finished");
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|