Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame^] | 1 | /* |
| 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> |
| 13 | #include <netlink/route/link.h> |
| 14 | #include <stdexcept> |
| 15 | #include <thread> |
| 16 | #include "utils/log-fwd.h" |
| 17 | |
| 18 | namespace velia::system { |
| 19 | |
| 20 | namespace impl { |
| 21 | class nlCacheMngrWatcher; |
| 22 | } |
| 23 | |
| 24 | /** @brief Wrapper for monitoring changes in NETLINK_ROUTE */ |
| 25 | class Rtnetlink { |
| 26 | public: |
| 27 | using nlCacheManager = std::shared_ptr<nl_cache_mngr>; |
| 28 | using LinkCB = std::function<void(rtnl_link* link, int cacheAction)>; /// cacheAction: NL_ACT_* |
| 29 | |
| 30 | explicit Rtnetlink(LinkCB cbLink); |
| 31 | ~Rtnetlink(); |
| 32 | |
| 33 | private: |
| 34 | velia::Log m_log; |
| 35 | nlCacheManager m_nlCacheManager; |
| 36 | LinkCB m_cbLink; /* (link object ptr, cache action). Cache action specified in libnl's cache.h (NL_ACT_*) */ |
| 37 | std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log |
| 38 | }; |
| 39 | |
| 40 | class RtnetlinkException : public std::runtime_error { |
| 41 | public: |
| 42 | RtnetlinkException(const std::string& msg); |
| 43 | RtnetlinkException(const std::string& funcName, int error); |
| 44 | }; |
| 45 | |
| 46 | } |