1 /*
2  * Copyright (c)2019 ZeroTier, Inc.
3  *
4  * Use of this software is governed by the Business Source License included
5  * in the LICENSE.TXT file in the project's root directory.
6  *
7  * Change Date: 2025-01-01
8  *
9  * On the date above, in accordance with the Business Source License, use
10  * of this software will be governed by version 2.0 of the Apache License.
11  */
12 /****/
13 
14 #ifndef ZT_MANAGEDROUTE_HPP
15 #define ZT_MANAGEDROUTE_HPP
16 
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "../node/InetAddress.hpp"
21 #include "../node/Utils.hpp"
22 #include "../node/SharedPtr.hpp"
23 #include "../node/AtomicCounter.hpp"
24 
25 #include <stdexcept>
26 #include <vector>
27 #include <map>
28 
29 namespace ZeroTier {
30 
31 /**
32  * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
33  */
34 class ManagedRoute
35 {
36 	friend class SharedPtr<ManagedRoute>;
37 
38 public:
39 	ManagedRoute(const InetAddress &target,const InetAddress &via,const InetAddress &src,const char *device);
40 	~ManagedRoute();
41 
42 	/**
43 	 * Set or update currently set route
44 	 *
45 	 * This must be called periodically for routes that shadow others so that
46 	 * shadow routes can be updated. In some cases it has no effect
47 	 *
48 	 * @return True if route add/update was successful
49 	 */
50 	bool sync();
51 
52 	/**
53 	 * Remove and clear this ManagedRoute
54 	 *
55 	 * This does nothing if this ManagedRoute is not set or has already been
56 	 * removed. If this is not explicitly called it is called automatically on
57 	 * destruct.
58 	 */
59 	void remove();
60 
target() const61 	inline const InetAddress &target() const { return _target; }
via() const62 	inline const InetAddress &via() const { return _via; }
src() const63 	inline const InetAddress &src() const { return _src; }
device() const64 	inline const char *device() const { return _device; }
65 
66 private:
ManagedRoute(const ManagedRoute &)67 	ManagedRoute(const ManagedRoute &) {}
operator =(const ManagedRoute &)68 	inline ManagedRoute &operator=(const ManagedRoute &) { return *this; }
69 
70 	InetAddress _target;
71 	InetAddress _via;
72 	InetAddress _src;
73 	InetAddress _systemVia; // for route overrides
74 	std::map<InetAddress,bool> _applied; // routes currently applied
75 	char _device[128];
76 	char _systemDevice[128]; // for route overrides
77 
78 	AtomicCounter __refCount;
79 };
80 
81 } // namespace ZeroTier
82 
83 #endif
84