blob: 2cf007c6d58fc3fbe666eb23b4501cca6468b3c3 [file] [log] [blame]
Tomáš Pecka2d578c82021-02-23 19:01:35 +01001/*
2 * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@cesnet.cz>
5 *
6 */
7
8#pragma once
9
10#include <functional>
11#include <mutex>
12#include <netlink/netlink.h>
Tomáš Pecka09729382021-03-08 19:36:50 +010013#include <netlink/route/addr.h>
Tomáš Pecka2d578c82021-02-23 19:01:35 +010014#include <netlink/route/link.h>
15#include <stdexcept>
16#include <thread>
17#include "utils/log-fwd.h"
18
19namespace velia::system {
20
21namespace impl {
22class nlCacheMngrWatcher;
23}
24
25/** @brief Wrapper for monitoring changes in NETLINK_ROUTE */
26class Rtnetlink {
27public:
28 using nlCacheManager = std::shared_ptr<nl_cache_mngr>;
29 using LinkCB = std::function<void(rtnl_link* link, int cacheAction)>; /// cacheAction: NL_ACT_*
Tomáš Pecka09729382021-03-08 19:36:50 +010030 using AddrCB = std::function<void(rtnl_addr* addr, int cacheAction)>; /// cacheAction: NL_ACT_*
Tomáš Pecka2d578c82021-02-23 19:01:35 +010031
Tomáš Pecka09729382021-03-08 19:36:50 +010032 Rtnetlink(LinkCB cbLink, AddrCB cbAddr);
Tomáš Pecka2d578c82021-02-23 19:01:35 +010033 ~Rtnetlink();
34
35private:
36 velia::Log m_log;
37 nlCacheManager m_nlCacheManager;
Tomáš Pecka09729382021-03-08 19:36:50 +010038 LinkCB m_cbLink;
39 AddrCB m_cbAddr;
Tomáš Pecka2d578c82021-02-23 19:01:35 +010040 std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log
41};
42
43class RtnetlinkException : public std::runtime_error {
44public:
45 RtnetlinkException(const std::string& msg);
46 RtnetlinkException(const std::string& funcName, int error);
47};
48
49}