-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkCore.h
More file actions
91 lines (84 loc) · 3.91 KB
/
NetworkCore.h
File metadata and controls
91 lines (84 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @file NetworkCore.h
* @brief A 'Real Subject' class for network core operations
*
* Real Subject network class headers
*
* @author Amine BAGGA (2025)
*/
#ifndef __NETWORK_CORE__
#define __NETWORK_CORE__
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <variant>
#include <thread>
#include <functional>
#include <mutex>
#include <atomic>
class Network;
class NetworkCore {
public:
using Callback = std::function<void(const std::variant<std::tuple<std::string, std::string>, std::tuple<std::string, std::string, int>, std::tuple<std::string, std::string, std::map<std::string, int>>>&)>;
const std::string ETH_1_INTERFACE{"eth0"};
const std::string ETH_2_INTERFACE{"eth1"};
const std::string BRIDGE_INTERFACE{"br0"};
const std::string WIFI_CLIENT_INTERFACE{"mlan0"};
const std::string WIFI_AP_INTERFACE{"uap0"};
const std::string SYSTEMD_ETH_1_CLIENT_FILE{"/lib/systemd/network/80-eth0.network"};
const std::string SYSTEMD_ETH_2_CLIENT_FILE{"/lib/systemd/network/81-eth1.network"};
const std::string SYSTEMD_WIFI_CLIENT_FILE{"/lib/systemd/network/82-mlan0.network"};
NetworkCore(Network*);
virtual ~NetworkCore();
bool setNetworkConfigFileDhcp(const std::string& interfaceName, bool dhcp);
bool setNetworkConfigFileDhcpServer(const std::string& interfaceName, bool dhcpServer);
bool setNetworkConfigFileIpMask(const std::string& interfaceName, const std::string& ip, const std::string& mask);
bool setNetworkConfigFileGw(const std::string& interfaceName, const std::string& gw);
bool setNetworkConfigFileDns(const std::string& interfaceName, const std::string& dns);
bool networkReloadConfig(void);
bool enableDisableWifiAp(bool enable);
bool verifyAndRecreateHostapdConf(void);
bool setWifiApSsid(const std::string& ssid);
bool setWifiApWpaKey(const std::string& wpaKey);
bool getWifiApCredentials(std::string& ssid, std::string& wpaKey);
bool setWifiClientSsidWpakey(const std::string& ssid, const std::string& wpaKey);
bool wifiClientEnableDisable(bool enable);
bool wifiClientRestart(void);
bool wifiClientServiceCheck(void);
std::map<std::string, bool> getInterfaces();
std::vector<std::tuple<std::string, std::string, std::string>> getInterfaceAddresses(void);
std::tuple<std::string, std::string> getInterfaceAddresses(const std::string& interfaceName);
std::tuple<std::string, std::string> getAddressesFromNetworkFile(const std::string& interfaceName) const;
std::string getGwFromNetworkFile(const std::string& interfaceName) const;
std::string getMacAddress(const std::string& interfaceName) const;
std::string getDnsFromNetworkFile(const std::string& interfaceName) const;
bool getDhcpFromNetworkFile(const std::string& interfaceName) const;
std::string getGw(const std::string& interfaceName) const;
std::string getDns(const std::string& interfaceName) const;
std::string getWifiSSID(void) const;
void setCallback(Callback callback);
void startStopWifiApConnectedClientsThread(bool);
void startStopWifiClientDiscoveryThread(bool);
protected:
Callback m_callback;
std::thread m_listenerThread;
std::thread m_listenerWifiThread;
std::thread m_wifiApClientsThread;
std::thread m_wifiClientDiscoveryThread;
bool m_listenerThreadRunning;
bool m_listenerWifiThreadRunning;
std::atomic<bool> m_wifiApClientsThreadRunning;
std::atomic<bool> m_wifiClientDiscoveryThreadRunning;
private:
std::tuple<std::string, std::vector<std::string>> _getNetworkConfigFileContent(const std::string& interfaceName);
int _maskToCidr(const std::string& mask);
std::string _cidrToMask(const std::string& cidr) const;
int _createNetlinkSocket();
void _listener(void);
void _listenerWifi(void);
std::string _getDefaultWifiApSsid(void) const;
void _getWifiApConnectedClients(void);
void _wifiClientDiscovery(void);
};
#endif // __NETWORK_CORE__