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