forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.h
284 lines (245 loc) · 12 KB
/
Network.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#pragma once
#include <string>
#include <atomic>
#include "Module.h"
#include "NetUtils.h"
#include "libIARM.h"
#include "UtilsLogging.h"
#include "NetworkConnectivity.h"
// Define this to use netlink calls (where there may be an alternative method but netlink could provide
// the information or perform the action required)
//#define USE_NETLINK
#define MAX_IP_ADDRESS_LEN 46
#define NETSRVMGR_INTERFACES_MAX 16
#define MAX_ENDPOINTS 5
#define MAX_ENDPOINT_SIZE 512
#define MAX_URI_LEN 512
#define MAX_HOST_NAME_LEN 128
typedef enum _NetworkManager_EventId_t {
IARM_BUS_NETWORK_MANAGER_EVENT_SET_INTERFACE_ENABLED=50,
IARM_BUS_NETWORK_MANAGER_EVENT_SET_INTERFACE_CONTROL_PERSISTENCE,
IARM_BUS_NETWORK_MANAGER_EVENT_WIFI_INTERFACE_STATE,
IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_ENABLED_STATUS,
IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_CONNECTION_STATUS,
IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS,
IARM_BUS_NETWORK_MANAGER_EVENT_DEFAULT_INTERFACE,
IARM_BUS_NETWORK_MANAGER_EVENT_INTERNET_CONNECTION_CHANGED,
IARM_BUS_NETWORK_MANAGER_MAX,
} IARM_Bus_NetworkManager_EventId_t;
typedef struct {
char name[16];
char mac[20];
unsigned int flags;
} NetSrvMgr_Interface_t;
typedef struct {
unsigned char size;
NetSrvMgr_Interface_t interfaces[NETSRVMGR_INTERFACES_MAX];
} IARM_BUS_NetSrvMgr_InterfaceList_t;
typedef enum _NetworkManager_GetIPSettings_ErrorCode_t
{
NETWORK_IPADDRESS_ACQUIRED,
NETWORK_IPADDRESS_NOTFOUND,
NETWORK_NO_ROUTE_INTERFACE,
NETWORK_NO_DEFAULT_ROUTE,
NETWORK_DNS_NOT_CONFIGURED,
NETWORK_INVALID_IPADDRESS,
} NetworkManager_GetIPSettings_ErrorCode_t;
typedef struct {
char interface[16];
char ipversion[16];
bool autoconfig;
char ipaddress[MAX_IP_ADDRESS_LEN];
char netmask[MAX_IP_ADDRESS_LEN];
char gateway[MAX_IP_ADDRESS_LEN];
char dhcpserver[MAX_IP_ADDRESS_LEN];
char primarydns[MAX_IP_ADDRESS_LEN];
char secondarydns[MAX_IP_ADDRESS_LEN];
bool isSupported;
NetworkManager_GetIPSettings_ErrorCode_t errCode;
} IARM_BUS_NetSrvMgr_Iface_Settings_t;
typedef struct {
char interface[16];
char gateway[MAX_IP_ADDRESS_LEN];
} IARM_BUS_NetSrvMgr_DefaultRoute_t;
typedef struct {
char interface[16];
bool status;
} IARM_BUS_NetSrvMgr_Iface_EventInterfaceStatus_t;
typedef IARM_BUS_NetSrvMgr_Iface_EventInterfaceStatus_t IARM_BUS_NetSrvMgr_Iface_EventInterfaceEnabledStatus_t;
typedef IARM_BUS_NetSrvMgr_Iface_EventInterfaceStatus_t IARM_BUS_NetSrvMgr_Iface_EventInterfaceConnectionStatus_t;
typedef struct {
char interface[16];
char ip_address[MAX_IP_ADDRESS_LEN];
bool is_ipv6;
bool acquired;
} IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t;
typedef struct {
char oldInterface[16];
char newInterface[16];
} IARM_BUS_NetSrvMgr_Iface_EventDefaultInterface_t;
typedef struct
{
char server[MAX_HOST_NAME_LEN];
uint16_t port;
bool ipv6;
char interface[16];
uint16_t bind_timeout;
uint16_t cache_timeout;
bool sync;
char public_ip[MAX_IP_ADDRESS_LEN];
} IARM_BUS_NetSrvMgr_Iface_StunRequest_t;
typedef struct
{
bool disableConnectivityTest;
} IARM_BUS_NetSrvMgr_configurePNI_t;
namespace WPEFramework {
namespace Plugin {
// This is a server for a JSONRPC communication channel.
// For a plugin to be capable to handle JSONRPC, inherit from PluginHost::JSONRPC.
// By inheriting from this class, the plugin realizes the interface PluginHost::IDispatcher.
// This realization of this interface implements, by default, the following methods on this plugin
// - exists
// - register
// - unregister
// Any other methood to be handled by this plugin can be added can be added by using the
// templated methods Register on the PluginHost::JSONRPC class.
// As the registration/unregistration of notifications is realized by the class PluginHost::JSONRPC,
// this class exposes a public method called, Notify(), using this methods, all subscribed clients
// will receive a JSONRPC message as a notification, in case this method is called.
class Network : public PluginHost::IPlugin
, public PluginHost::JSONRPC
, public PluginHost::ISubSystem::IInternet
{
public:
static void notifyInternetStatusChange(nsm_internetState InternetConnectionState);
private:
// We do not allow this plugin to be copied !!
Network(const Network&) = delete;
Network& operator=(const Network&) = delete;
//Private variables
std::atomic_bool m_isPluginInited{false};
std::thread m_registrationThread;
//Begin methods
uint32_t getQuirks(const JsonObject& parameters, JsonObject& response);
// Network_API_Version_1
uint32_t getInterfaces(const JsonObject& parameters, JsonObject& response);
uint32_t isInterfaceEnabled(const JsonObject& parameters, JsonObject& response);
uint32_t setInterfaceEnabled(const JsonObject& parameters, JsonObject& response);
uint32_t getDefaultInterface(const JsonObject& parameters, JsonObject& response);
uint32_t setDefaultInterface(const JsonObject& parameters, JsonObject& response);
uint32_t getStbIp(const JsonObject& parameters, JsonObject& response);
uint32_t trace(const JsonObject& parameters, JsonObject& response);
uint32_t traceNamedEndpoint(const JsonObject& parameters, JsonObject& response);
uint32_t getNamedEndpoints(const JsonObject& parameters, JsonObject& response);
uint32_t ping(const JsonObject& parameters, JsonObject& response);
uint32_t pingNamedEndpoint(const JsonObject& parameters, JsonObject& response);
uint32_t setIPSettings(const JsonObject& parameters, JsonObject& response);
uint32_t setIPSettings2(const JsonObject& parameters, JsonObject& response);
uint32_t getIPSettings(const JsonObject& parameters, JsonObject& response);
uint32_t getIPSettings2(const JsonObject& parameters, JsonObject& response);
uint32_t getSTBIPFamily(const JsonObject& parameters, JsonObject& response);
uint32_t isConnectedToInternet(const JsonObject& parameters, JsonObject& response);
uint32_t setConnectivityTestEndpoints(const JsonObject& parameters, JsonObject& response);
uint32_t getInternetConnectionState(const JsonObject& parameters, JsonObject& response);
uint32_t startConnectivityMonitoring(const JsonObject& parameters, JsonObject& response);
uint32_t getCaptivePortalURI(const JsonObject& parameters, JsonObject& response);
uint32_t stopConnectivityMonitoring(const JsonObject& parameters, JsonObject& response);
uint32_t getPublicIP(const JsonObject& parameters, JsonObject& response);
uint32_t setStunEndPoint(const JsonObject& parameters, JsonObject& response);
uint32_t configurePNI(const JsonObject& parameters, JsonObject& response);
bool getIPIARMWrapper(IARM_BUS_NetSrvMgr_Iface_Settings_t& iarmData, const string interface, const string ipversion);
void onInterfaceEnabledStatusChanged(std::string interface, bool enabled);
void onInterfaceConnectionStatusChanged(std::string interface, bool connected);
void onInternetStatusChange(nsm_internetState InternetConnectionState);
void onInterfaceIPAddressChanged(std::string interface, std::string ipv6Addr, std::string ipv4Addr, bool acquired);
void onDefaultInterfaceChanged(std::string oldInterface, std::string newInterface);
void iarmEventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len);
// Netmask Validation
bool isValidCIDRv4(std::string interface);
// Internal methods
bool _getDefaultInterface(std::string& interface, std::string& gateway);
void retryIarmEventRegistration();
void threadEventRegistration();
bool _doTrace(std::string &endpoint, int packets, JsonObject& response);
bool _doTraceNamedEndpoint(std::string &endpointName, int packets, JsonObject& response);
JsonObject _doPing(const std::string& guid, const std::string& endPoint, int packets);
JsonObject _doPingNamedEndpoint(const std::string& guid, const std::string& endpointName, int packets);
bool getIPSettingsInternal(const JsonObject& parameters, JsonObject& response,int& errCode);
uint32_t setIPSettingsInternal(const JsonObject& parameters, JsonObject& response);
void setInternetSubsystem();
public:
Network();
virtual ~Network();
static void eventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len);
//Build QueryInterface implementation, specifying all possible interfaces to be returned.
BEGIN_INTERFACE_MAP(Network)
INTERFACE_ENTRY(PluginHost::IPlugin)
INTERFACE_ENTRY(PluginHost::IDispatcher)
INTERFACE_ENTRY(PluginHost::ISubSystem::IInternet)
END_INTERFACE_MAP
/*
* ------------------------------------------------------------------------------------------------------------
* ISubSystem::INetwork methods
* ------------------------------------------------------------------------------------------------------------
*/
string PublicIPAddress() const override
{
return m_publicIPAddress;
}
network_type NetworkType() const override
{
return (m_publicIPAddress.empty() == true ? PluginHost::ISubSystem::IInternet::UNKNOWN : (m_ipversion == "IPV6" ? PluginHost::ISubSystem::IInternet::IPV6 : PluginHost::ISubSystem::IInternet::IPV4));
}
//IPlugin methods
virtual const std::string Initialize(PluginHost::IShell* service) override;
virtual void Deinitialize(PluginHost::IShell* service) override;
virtual std::string Information() const override;
uint32_t getPublicIPInternal(const JsonObject& parameters, JsonObject& response);
public:
static Network *_instance;
static Network *getInstance() {return _instance;}
ConnectivityMonitor connectivityMonitor;
private:
PluginHost::IShell* m_service;
NetUtils m_netUtils;
string m_stunEndPoint;
string m_isHybridDevice;
string m_defaultInterface;
string m_gatewayInterface;
string m_publicIPAddress;
string m_ipversion;
uint16_t m_stunPort;
uint16_t m_stunBindTimeout;
uint16_t m_stunCacheTimeout;
bool m_stunSync;
uint32_t m_apiVersionNumber;
std::atomic<bool> m_useIpv4Cache;
std::atomic<bool> m_useIpv6Cache;
std::atomic<bool> m_useStbIPCache;
string m_stbIpCache;
std::atomic<bool> m_useDefInterfaceCache;
string m_defInterfaceCache;
string m_defIpversionCache;
IARM_BUS_NetSrvMgr_Iface_Settings_t m_ipv4Cache;
IARM_BUS_NetSrvMgr_Iface_Settings_t m_ipv6Cache;
};
} // namespace Plugin
} // namespace WPEFramework