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_LINUXETHERNETTAP_HPP
15 #define ZT_LINUXETHERNETTAP_HPP
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include <string>
21 #include <vector>
22 #include <stdexcept>
23 #include <atomic>
24 #include <array>
25 #include <thread>
26 #include <mutex>
27 #include "../node/MulticastGroup.hpp"
28 #include "EthernetTap.hpp"
29 
30 namespace ZeroTier {
31 
32 class LinuxEthernetTap : public EthernetTap
33 {
34 public:
35 	LinuxEthernetTap(
36 		const char *homePath,
37 		const MAC &mac,
38 		unsigned int mtu,
39 		unsigned int metric,
40 		uint64_t nwid,
41 		const char *friendlyName,
42 		void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
43 		void *arg);
44 
45 	virtual ~LinuxEthernetTap();
46 
47 	virtual void setEnabled(bool en);
48 	virtual bool enabled() const;
49 	virtual bool addIp(const InetAddress &ip);
50 	virtual bool addIps(std::vector<InetAddress> ips);
51 	virtual bool removeIp(const InetAddress &ip);
52 	virtual std::vector<InetAddress> ips() const;
53 	virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
54 	virtual std::string deviceName() const;
55 	virtual void setFriendlyName(const char *friendlyName);
56 	virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
57 	virtual void setMtu(unsigned int mtu);
setDns(const char * domain,const std::vector<InetAddress> & servers)58 	virtual void setDns(const char *domain, const std::vector<InetAddress> &servers) {}
59 
60 private:
61 	void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
62 	void *_arg;
63 	uint64_t _nwid;
64 	MAC _mac;
65 	std::string _homePath;
66 	std::string _dev;
67 	std::vector<MulticastGroup> _multicastGroups;
68 	unsigned int _mtu;
69 	int _fd;
70 	int _shutdownSignalPipe[2];
71 	std::atomic_bool _enabled;
72 	std::atomic_bool _run;
73 	std::thread _tapReaderThread;
74 };
75 
76 } // namespace ZeroTier
77 
78 #endif
79