Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DHCPServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ find_package(CompileSettingsDebug CONFIG REQUIRED)

add_library(${MODULE_NAME} SHARED
DHCPServer.cpp
DHCPServerJsonRpc.cpp
DHCPServerImplementation.cpp
Module.cpp)

Expand Down
124 changes: 121 additions & 3 deletions DHCPServer/DHCPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include "DHCPServer.h"
#include <interfaces/json/JsonData_DHCPServer.h>

namespace Thunder {
namespace Plugin {
Expand All @@ -45,14 +44,14 @@ PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
DHCPServer::DHCPServer()
: _skipURL(0)
, _servers()
, _persistentPath()
, _adminLock()
{
RegisterAll();
}
POP_WARNING()

/* virtual */ DHCPServer::~DHCPServer()
{
UnregisterAll();
}

/* virtual */ const string DHCPServer::Initialize(PluginHost::IShell* service)
Expand Down Expand Up @@ -93,12 +92,16 @@ POP_WARNING()
}
}

Exchange::JDHCPServer::Register(*this, this);

// On success return empty, to indicate there is no error text.
return (result);
}

/* virtual */ void DHCPServer::Deinitialize(PluginHost::IShell*)
{
Exchange::JDHCPServer::Unregister(*this);

std::map<const string, DHCPServerImplementation>::iterator index(_servers.begin());

while (index != _servers.end()) {
Expand Down Expand Up @@ -255,5 +258,120 @@ POP_WARNING()
}
}

Core::hresult DHCPServer::Activate(const string& interface)
{
Core::hresult result = Core::ERROR_UNKNOWN_KEY;

if (interface.empty() == false) {
_adminLock.Lock();
auto server(_servers.find(interface));

if (server != _servers.end()) {

if (server->second.IsActive() == false) {
result = server->second.Open();
}
else {
result = Core::ERROR_ILLEGAL_STATE;
}
}
_adminLock.Unlock();
}

return (result);
}

Core::hresult DHCPServer::Deactivate(const string& interface)
{
Core::hresult result = Core::ERROR_UNKNOWN_KEY;

if (interface.empty() == false) {
_adminLock.Lock();
auto server(_servers.find(interface));

if (server != _servers.end()) {

if (server->second.IsActive() == true) {
result = server->second.Close();
} else {
result = Core::ERROR_ILLEGAL_STATE;
}
}
_adminLock.Unlock();
}

return (result);
}

void DHCPServer::Fill(Exchange::IDHCPServer::Server& data, const DHCPServerImplementation& server) const
{
data.interface = server.Interface();
data.active = server.IsActive();

if (server.IsActive()) {
data.begin = server.BeginPool().HostAddress();
data.end = server.EndPool().HostAddress();
data.router = server.Router().HostAddress();

data.leases.Value() = std::vector<Exchange::IDHCPServer::Lease>();
auto it(server.Leases());

while (it.Next() == true) {
Exchange::IDHCPServer::Lease lease;
lease.name = it.Current().Id().Text();
lease.ip = it.Current().Address().HostAddress();

if (it.Current().Expiration() != 0) {
lease.expires.Value() = Core::Time(it.Current().Expiration());
}
data.leases.Value().push_back(lease);
}
}
}

Core::hresult DHCPServer::Status(const Core::OptionalType<string>& interface, Exchange::IDHCPServer::IServerIterator*& servers) const
{
Core::hresult result = Core::ERROR_NONE;
std::list<Exchange::IDHCPServer::Server> serverList;

_adminLock.Lock();

if (interface.IsSet() == false) {

DHCPServerMap::const_iterator index = _servers.begin();

while (index != _servers.end()) {
Exchange::IDHCPServer::Server server;
Fill(server, index->second);
serverList.push_back(server);
index++;
}
}
else {
DHCPServerMap::const_iterator index = _servers.find(interface);

if (index != _servers.end()) {
Exchange::IDHCPServer::Server server;
Fill(server, index->second);
serverList.push_back(server);
}
else {
result = Core::ERROR_UNKNOWN_KEY;
}
}
_adminLock.Unlock();

if (serverList.empty() == false) {
using Iterator = Exchange::IDHCPServer::IServerIterator;
servers = Core::ServiceType<RPC::IteratorType<Iterator>>::Create<Iterator>(serverList);
ASSERT(servers != nullptr);
}
else {
servers = nullptr;
}

return (result);
}

} // namespace Plugin
} // namespace Thunder
24 changes: 15 additions & 9 deletions DHCPServer/DHCPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
#pragma once

#include "DHCPServerImplementation.h"
#include <interfaces/json/JsonData_DHCPServer.h>
#include <interfaces/IDHCPServer.h>
#include <interfaces/json/JDHCPServer.h>
#include "Module.h"

namespace Thunder {
namespace Plugin {

class DHCPServer : public PluginHost::IPlugin, public PluginHost::IWeb, public PluginHost::JSONRPC {
class DHCPServer : public Exchange::IDHCPServer
, public PluginHost::IPlugin
, public PluginHost::IWeb
, public PluginHost::JSONRPC {
public:
class Data : public Core::JSON::Container {
private:
Expand Down Expand Up @@ -253,6 +257,7 @@ namespace Plugin {
INTERFACE_ENTRY(PluginHost::IPlugin)
INTERFACE_ENTRY(PluginHost::IWeb)
INTERFACE_ENTRY(PluginHost::IDispatcher)
INTERFACE_ENTRY(Exchange::IDHCPServer)
END_INTERFACE_MAP

public:
Expand All @@ -267,12 +272,10 @@ namespace Plugin {
void Inbound(Web::Request& request) override;
Core::ProxyType<Web::Response> Process(const Web::Request& request) override;

// JsonRpc
void RegisterAll();
void UnregisterAll();
uint32_t endpoint_activate(const JsonData::DHCPServer::ActivateParamsInfo& params);
uint32_t endpoint_deactivate(const JsonData::DHCPServer::ActivateParamsInfo& params);
uint32_t get_status(const string& index, Core::JSON::ArrayType<JsonData::DHCPServer::ServerData>& response) const;
Core::hresult Activate(const string& interface) override;
Core::hresult Deactivate(const string& interface) override;
void Fill(Exchange::IDHCPServer::Server& data, const DHCPServerImplementation& server) const;
Core::hresult Status(const Core::OptionalType<string>& interface /* @index */, Exchange::IDHCPServer::IServerIterator*& servers /* @out */) const override;

// Lease permanent storage
// -------------------------------------------------------------------------------------------------------
Expand All @@ -282,9 +285,12 @@ namespace Plugin {
// Callbacks
void OnNewIPRequest(const string& interface, const DHCPServerImplementation::Lease* lease);
private:
using DHCPServerMap = std::map<const string, DHCPServerImplementation>;

uint16_t _skipURL;
std::map<const string, DHCPServerImplementation> _servers;
DHCPServerMap _servers;
std::string _persistentPath;
mutable Core::CriticalSection _adminLock;
};

} // namespace Plugin
Expand Down
1 change: 0 additions & 1 deletion DHCPServer/DHCPServer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
<ItemGroup>
<ClCompile Include="DHCPServer.cpp" />
<ClCompile Include="DHCPServerImplementation.cpp" />
<ClCompile Include="DHCPServerJsonRpc.cpp" />
<ClCompile Include="Module.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions DHCPServer/DHCPServer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<ClCompile Include="DHCPServerImplementation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DHCPServerJsonRpc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Module.h">
Expand Down
Loading
Loading