kea-custom-hooks
FeM custom hooks libraries for Kea DHCP
ArpwatchRequestHandler.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <stdexcept>
3
4#include <asiolink/io_address.h>
5#include <dhcp/hwaddr.h>
6
7#include <hiredis/hiredis.h>
8
10#include "XmlRpcServer.hpp"
12
13namespace
14{
15inline std::string get_ip2mac_key(std::string_view suffix)
16{
17 return "ip2mac-" + std::string{suffix};
18}
19
20inline std::string sanitize_ip_address_string(const std::string& ip_str)
21{
22 const isc::asiolink::IOAddress ip_addr{ip_str};
23 return ip_addr.toText();
24}
25
26inline std::string sanitize_mac_address_string(const std::string& mac_str)
27{
28 const auto mac = isc::dhcp::HWAddr::fromText(mac_str);
29 return mac.toText(false);
30}
31} // namespace
32
33namespace aai
34{
36{
37 xmlrpc_server.register_method_handler(
39 [this](const std::string& ip) { return resolve_ip_to_mac(ip); });
40}
41
42std::optional<std::string> ArpwatchRequestHandler::resolve_ip_to_mac(std::string_view ip)
43{
44 const std::string redis_key = get_ip2mac_key(sanitize_ip_address_string(std::string{ip}));
45 const auto redis_reply = redis.get(redis_key);
46 assert(redis_reply);
47
48 if (redis_reply->type == REDIS_REPLY_NIL) {
49 return {};
50 }
51 if (redis_reply->type != REDIS_REPLY_STRING) {
52 throw std::runtime_error("Unexpected Redis reply type");
53 }
54
55 assert(redis_reply->str);
56 return {redis_reply->str};
57}
58} // namespace aai
virtual get_reply_t get(const std::string &key)
void register_method_handlers(XmlRpcServer &rpc_server)
Register this instance's method handlers in the aai::XmlRpcServer.
std::optional< std::string > resolve_ip_to_mac(std::string_view ip)
Abstraction for an XML-RPC server.
void register_method_handler(std::string_view name, std::function< std::optional< std::string >(const std::string &)> handler)
Register a method handler.