forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.cpp
1715 lines (1508 loc) · 70.9 KB
/
Network.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.
**/
#include "Network.h"
#include <net/if.h>
#include <arpa/inet.h>
#include "UtilsIarm.h"
#include "UtilsJsonRpc.h"
#include "UtilsString.h"
#include "UtilscRunScript.h"
#include "UtilsgetRFCConfig.h"
#include "NetworkConnectivity.h"
using namespace std;
#define DEFAULT_PING_PACKETS 15
#define CIDR_NETMASK_IP_LEN 32
#define API_VERSION_NUMBER_MAJOR 1
#define API_VERSION_NUMBER_MINOR 3
#define API_VERSION_NUMBER_PATCH 11
/* Netsrvmgr Based Macros & Structures */
#define IARM_BUS_NM_SRV_MGR_NAME "NET_SRV_MGR"
#define INTERFACE_SIZE 10
#define INTERFACE_LIST 50
#define MAX_IP_FAMILY_SIZE 10
#define IARM_BUS_NETSRVMGR_API_getActiveInterface "getActiveInterface"
#define IARM_BUS_NETSRVMGR_API_getNetworkInterfaces "getNetworkInterfaces"
#define IARM_BUS_NETSRVMGR_API_getInterfaceList "getInterfaceList"
#define IARM_BUS_NETSRVMGR_API_getDefaultInterface "getDefaultInterface"
#define IARM_BUS_NETSRVMGR_API_setDefaultInterface "setDefaultInterface"
#define IARM_BUS_NETSRVMGR_API_isInterfaceEnabled "isInterfaceEnabled"
#define IARM_BUS_NETSRVMGR_API_setInterfaceEnabled "setInterfaceEnabled"
#define IARM_BUS_NETSRVMGR_API_getSTBip "getSTBip"
#define IARM_BUS_NETSRVMGR_API_setIPSettings "setIPSettings"
#define IARM_BUS_NETSRVMGR_API_getIPSettings "getIPSettings"
#define IARM_BUS_NETSRVMGR_API_getSTBip_family "getSTBip_family"
#define IARM_BUS_NETSRVMGR_API_isAvailable "isAvailable"
#define IARM_BUS_NETSRVMGR_API_getPublicIP "getPublicIP"
#define IARM_BUS_NETSRVMGR_API_configurePNI "configurePNI"
// TODO: remove this
#define registerMethod(...) for (uint8_t i = 1; GetHandler(i); i++) GetHandler(i)->Register<JsonObject, JsonObject>(__VA_ARGS__)
namespace WPEFramework
{
typedef struct _IARM_BUS_NetSrvMgr_Iface_EventData_t {
union {
char activeIface[INTERFACE_SIZE];
char allNetworkInterfaces[INTERFACE_LIST];
char setInterface[INTERFACE_SIZE];
char activeIfaceIpaddr[MAX_IP_ADDRESS_LEN];
};
char interfaceCount;
bool isInterfaceEnabled;
bool persist;
char ipfamily[MAX_IP_FAMILY_SIZE];
} IARM_BUS_NetSrvMgr_Iface_EventData_t;
namespace {
static Plugin::Metadata<Plugin::Network> metadata(
// Version (Major, Minor, Patch)
API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH,
// Preconditions
{},
// Terminations
{},
// Controls
{}
);
}
namespace Plugin
{
SERVICE_REGISTRATION(Network, API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH);
Network* Network::_instance = nullptr;
Network::Network()
: PluginHost::JSONRPC()
, m_service(nullptr)
, m_apiVersionNumber(API_VERSION_NUMBER_MAJOR)
{
Network::_instance = this;
m_isPluginInited = false;
CreateHandler({2});
// Quirk
registerMethod("getQuirks", &Network::getQuirks, this);
// Network_API_Version_1
registerMethod("getInterfaces", &Network::getInterfaces, this);
registerMethod("isInterfaceEnabled", &Network::isInterfaceEnabled, this);
registerMethod("setInterfaceEnabled", &Network::setInterfaceEnabled, this);
registerMethod("getDefaultInterface", &Network::getDefaultInterface, this);
registerMethod("setDefaultInterface", &Network::setDefaultInterface, this);
registerMethod("getStbIp", &Network::getStbIp, this);
registerMethod("trace", &Network::trace, this);
registerMethod("traceNamedEndpoint", &Network::traceNamedEndpoint, this);
registerMethod("getNamedEndpoints", &Network::getNamedEndpoints, this);
registerMethod("ping", &Network::ping, this);
registerMethod("pingNamedEndpoint", &Network::pingNamedEndpoint, this);
Register("setIPSettings", &Network::setIPSettings, this);
GetHandler(2)->Register<JsonObject, JsonObject>("setIPSettings", &Network::setIPSettings2, this);
Register("getIPSettings", &Network::getIPSettings, this);
GetHandler(2)->Register<JsonObject, JsonObject>("getIPSettings", &Network::getIPSettings2, this);
registerMethod("getSTBIPFamily", &Network::getSTBIPFamily, this);
registerMethod("isConnectedToInternet", &Network::isConnectedToInternet, this);
registerMethod("setConnectivityTestEndpoints", &Network::setConnectivityTestEndpoints, this);
registerMethod("getInternetConnectionState", &Network::getInternetConnectionState, this);
registerMethod("startConnectivityMonitoring", &Network::startConnectivityMonitoring, this);
registerMethod("getCaptivePortalURI", &Network::getCaptivePortalURI, this);
registerMethod("stopConnectivityMonitoring", &Network::stopConnectivityMonitoring, this);
registerMethod("getPublicIP", &Network::getPublicIP, this);
registerMethod("setStunEndPoint", &Network::setStunEndPoint, this);
registerMethod("configurePNI", &Network::configurePNI, this);
const char * script1 = R"(grep DEVICE_TYPE /etc/device.properties | cut -d "=" -f2 | tr -d '\n')";
m_isHybridDevice = Utils::cRunScript(script1).substr();
LOGWARN("script1 '%s' result: '%s'", script1, m_isHybridDevice.c_str());
m_defaultInterface = "";
m_gatewayInterface = "";
m_netUtils.InitialiseNetUtils();
m_stunEndPoint = "stun.l.google.com";
m_stunPort = 19302;
m_stunBindTimeout = 30;
m_stunCacheTimeout = 0;
m_stunSync = true;
m_useIpv4Cache = false;
m_useIpv6Cache = false;
m_useStbIPCache = false;
m_stbIpCache = "";
m_useDefInterfaceCache = false;
m_defInterfaceCache = "";
m_defIpversionCache = "";
m_ipv4Cache = {0};
m_ipv6Cache = {0};
}
Network::~Network()
{
}
const string Network::Initialize(PluginHost::IShell* service )
{
m_service = service;
m_service->AddRef();
string msg;
if (Utils::IARM::init())
{
IARM_Result_t res;
IARM_Result_t retVal = IARM_RESULT_SUCCESS;
#ifndef NET_DISABLE_NETSRVMGR_CHECK
char c;
uint32_t retry = 0;
do{
retVal = IARM_Bus_Call_with_IPCTimeout(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isAvailable, (void *)&c, sizeof(c), (1000*10));
if(retVal != IARM_RESULT_SUCCESS){
LOGERR("NetSrvMgr is not available. Failed to activate Network Plugin, retry = %d", retry);
usleep(500*1000);
retry++;
}
}while((retVal != IARM_RESULT_SUCCESS) && (retry < 20));
#endif
if(retVal != IARM_RESULT_SUCCESS)
{
msg = "NetSrvMgr is not available";
LOGERR("NETWORK_NOT_READY: The NetSrvMgr Component is not available.Retrying in separate thread");
retryIarmEventRegistration();
}
else {
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_ENABLED_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_CONNECTION_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_DEFAULT_INTERFACE, eventHandler) );
LOGINFO("Successfully activated Network Plugin");
m_isPluginInited = true;
}
}
else
{
msg = "IARM bus is not available";
LOGERR("IARM bus is not available. Failed to activate Network Plugin");
}
return msg;
}
void Network::Deinitialize(PluginHost::IShell* /* service */)
{
m_isPluginInited = false;
if(m_registrationThread.joinable())
{
m_registrationThread.join();
}
if (Utils::IARM::isConnected())
{
IARM_Result_t res;
IARM_CHECK( IARM_Bus_RemoveEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_ENABLED_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RemoveEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_CONNECTION_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RemoveEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS, eventHandler) );
IARM_CHECK( IARM_Bus_RemoveEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_DEFAULT_INTERFACE, eventHandler) );
}
Unregister("getQuirks");
Unregister("getInterfaces");
Unregister("isInterfaceEnabled");
Unregister("setInterfaceEnabled");
Unregister("getDefaultInterface");
Unregister("setDefaultInterface");
Unregister("getStbIp");
Unregister("trace");
Unregister("traceNamedEndpoint");
Unregister("getNamedEndpoints");
Unregister("ping");
Unregister("pingNamedEndpoint");
Unregister("setIPSettings");
Unregister("getIPSettings");
Unregister("isConnectedToInternet");
Unregister("setConnectivityTestEndpoints");
Unregister("getInternetConnectionState");
Unregister("getCaptivePortalURI");
Unregister("startConnectivityMonitoring");
Unregister("stopConnectivityMonitoring");
Unregister("getPublicIP");
Unregister("setStunEndPoint");
Unregister("configurePNI");
/* stop connectivity monitor if running */
connectivityMonitor.stopContinuousConnectivityMonitoring();
Network::_instance = nullptr;
m_service->Release();
m_service = nullptr;
}
string Network::Information() const
{
return(string());
}
bool Network::isValidCIDRv4(string buf)
{
string CIDR_PREFIXES[CIDR_NETMASK_IP_LEN] = {
"128.0.0.0",
"192.0.0.0",
"224.0.0.0",
"240.0.0.0",
"248.0.0.0",
"252.0.0.0",
"254.0.0.0",
"255.0.0.0",
"255.128.0.0",
"255.192.0.0",
"255.224.0.0",
"255.240.0.0",
"255.248.0.0",
"255.252.0.0",
"255.254.0.0",
"255.255.0.0",
"255.255.128.0",
"255.255.192.0",
"255.255.224.0",
"255.255.240.0",
"255.255.248.0",
"255.255.252.0",
"255.255.254.0",
"255.255.255.0",
"255.255.255.128",
"255.255.255.192",
"255.255.255.224",
"255.255.255.240",
"255.255.255.248",
"255.255.255.252",
"255.255.255.254",
"255.255.255.255",
};
int i = 0;
bool retval = false;
while(i < CIDR_NETMASK_IP_LEN)
{
if((buf.compare(CIDR_PREFIXES[i])) == 0)
{
retval = true;
break;
}
i++;
}
return retval;
}
void Network::retryIarmEventRegistration()
{
m_registrationThread = thread(&Network::threadEventRegistration, this);
}
void Network::threadEventRegistration()
{
IARM_Result_t res = IARM_RESULT_SUCCESS;
IARM_Result_t retVal = IARM_RESULT_SUCCESS;
#ifndef NET_DISABLE_NETSRVMGR_CHECK
do
{
char c;
uint32_t retry = 0;
retVal = IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isAvailable, (void *)&c, sizeof(c));
if(retVal != IARM_RESULT_SUCCESS){
LOGERR("threadEventRegistration: NetSrvMgr is not available. Failed to activate Network Plugin, retrying count = %d", retry);
usleep(500*1000);
retry++;
}
}while((retVal != IARM_RESULT_SUCCESS) && (m_isPluginInited != true ));
#endif
if(retVal != IARM_RESULT_SUCCESS)
{
LOGERR("threadEventRegistration NetSrvMgr is not available. Failed to activate Network Plugin, retrying new cycle");
}
else
{
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_ENABLED_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_CONNECTION_STATUS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS, eventHandler) );
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_EVENT_DEFAULT_INTERFACE, eventHandler) );
LOGINFO("NETWORK_AVAILABILITY_RETRY_SUCCESS: threadEventRegistration successfully subscribed to IARM event for Network Plugin");
m_isPluginInited = true;
}
}
// Wrapper methods
uint32_t Network::getQuirks(const JsonObject& parameters, JsonObject& response)
{
JsonArray array;
array.Add("RDK-20093");
response["quirks"] = array;
returnResponse(true)
}
uint32_t Network::getInterfaces (const JsonObject& parameters, JsonObject& response)
{
IARM_BUS_NetSrvMgr_InterfaceList_t list;
bool result = false;
if(m_isPluginInited)
{
if (IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getInterfaceList, (void*)&list, sizeof(list)))
{
JsonArray networkInterfaces;
for (int i = 0; i < list.size; i++)
{
JsonObject interface;
string iface = m_netUtils.getInterfaceDescription(list.interfaces[i].name);
#ifdef NET_DEFINED_INTERFACES_ONLY
if (iface.empty())
continue; // Skip unrecognised interfaces...
#endif
interface["interface"] = iface;
interface["macAddress"] = string(list.interfaces[i].mac);
interface["enabled"] = ((list.interfaces[i].flags & IFF_UP) != 0);
interface["connected"] = ((list.interfaces[i].flags & IFF_RUNNING) != 0);
networkInterfaces.Add(interface);
}
response["interfaces"] = networkInterfaces;
result = true;
}
else
{
LOGWARN ("Call to %s for %s failed", IARM_BUS_NM_SRV_MGR_NAME, __FUNCTION__);
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::getDefaultInterface (const JsonObject& parameters, JsonObject& response)
{
string interface;
string gateway;
bool result = false;
if(m_isPluginInited)
{
if (m_useDefInterfaceCache)
{
response["interface"] = m_defInterfaceCache;
result = true;
}
else if (_getDefaultInterface(interface, gateway))
{
response["interface"] = m_netUtils.getInterfaceDescription(interface);
m_defInterfaceCache = m_netUtils.getInterfaceDescription(interface);
m_useDefInterfaceCache = true;
result = true;
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::setDefaultInterface (const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
{
if ((parameters.HasLabel("interface")) && (parameters.HasLabel("persist")))
{
string interface = "";
bool persist = false;
getStringParameter("interface", interface)
if (!(strcmp (interface.c_str(), "ETHERNET") == 0 || strcmp (interface.c_str(), "WIFI") == 0))
{
LOGERR ("Call for %s failed due to invalid interface [%s]", IARM_BUS_NETSRVMGR_API_setDefaultInterface, interface.c_str());
returnResponse (result)
}
getBoolParameter("persist", persist)
IARM_BUS_NetSrvMgr_Iface_EventData_t iarmData = { 0 };
strncpy(iarmData.setInterface, interface.c_str(), INTERFACE_SIZE);
iarmData.setInterface[sizeof(iarmData.setInterface) - 1] = '\0';
iarmData.persist = persist;
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setDefaultInterface, (void *)&iarmData, sizeof(iarmData)))
result = true;
else
LOGWARN ("Call to %s for %s failed", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setDefaultInterface);
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::getStbIp(const JsonObject ¶meters, JsonObject &response)
{
IARM_BUS_NetSrvMgr_Iface_EventData_t param;
memset(¶m, 0, sizeof(param));
bool result = false;
if(m_isPluginInited)
{
if(m_useStbIPCache)
{
response["ip"] = m_stbIpCache;
result = true;
}
else if (IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getSTBip, (void*)¶m, sizeof(param)))
{
response["ip"] = string(param.activeIfaceIpaddr, MAX_IP_ADDRESS_LEN - 1);
m_stbIpCache = string(param.activeIfaceIpaddr, MAX_IP_ADDRESS_LEN - 1);
result = true;
if (!m_stbIpCache.empty())
{
m_useStbIPCache = true;
}
}
else
{
response["ip"] = "";
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::getSTBIPFamily(const JsonObject ¶meters, JsonObject &response)
{
bool result = false;
if(m_isPluginInited)
{
if (parameters.HasLabel("family"))
{
int errCode;
JsonObject internal;
JsonObject internalResponse;
internal["ipversion"] = parameters["family"];
internal["interface"] = m_defInterfaceCache;
if (getIPSettingsInternal(internal, internalResponse, errCode))
{
if (NETWORK_IPADDRESS_ACQUIRED == errCode)
{
response["ip"] = internalResponse["ipaddr"];
m_defInterfaceCache = internalResponse["interface"].String();
result = true;
}
else
{
LOGWARN ("Failed to get IP Address for this family");
}
}
}
else
{
LOGWARN ("Required Family Attribute is not provided.");
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::isInterfaceEnabled (const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
{
if (parameters.HasLabel("interface"))
{
string interface = "";
getStringParameter("interface", interface)
if (!(strcmp (interface.c_str(), "ETHERNET") == 0 || strcmp (interface.c_str(), "WIFI") == 0))
{
LOGERR ("Call for %s failed due to invalid interface [%s]", IARM_BUS_NETSRVMGR_API_isInterfaceEnabled, interface.c_str());
returnResponse (result)
}
IARM_BUS_NetSrvMgr_Iface_EventData_t param = {0};
strncpy(param.setInterface, interface.c_str(), INTERFACE_SIZE);
param.setInterface[sizeof(param.setInterface) - 1] = '\0';
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isInterfaceEnabled, (void*)¶m, sizeof(param)))
{
LOGINFO("%s :: Enabled = %d ",__FUNCTION__,param.isInterfaceEnabled);
response["enabled"] = param.isInterfaceEnabled;
result = true;
}
else
LOGWARN ("Call to %s for %s failed", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isInterfaceEnabled);
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::setInterfaceEnabled (const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
{
if ((parameters.HasLabel("interface")) && (parameters.HasLabel("enabled")) && (parameters.HasLabel("persist")))
{
string interface = "";
bool enabled = false;
bool persist = false;
getStringParameter("interface", interface)
if (!(strcmp (interface.c_str(), "ETHERNET") == 0 || strcmp (interface.c_str(), "WIFI") == 0))
{
LOGERR ("Call for %s failed due to invalid interface [%s]", IARM_BUS_NETSRVMGR_API_setInterfaceEnabled, interface.c_str());
returnResponse (result)
}
getBoolParameter("enabled", enabled)
getBoolParameter("persist", persist)
IARM_BUS_NetSrvMgr_Iface_EventData_t iarmData = { 0 };
strncpy(iarmData.setInterface, interface.c_str(), INTERFACE_SIZE);
iarmData.setInterface[sizeof(iarmData.setInterface) - 1] = '\0';
iarmData.isInterfaceEnabled = enabled;
iarmData.persist = persist;
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setInterfaceEnabled, (void *)&iarmData, sizeof(iarmData)))
result = true;
else
LOGWARN ("Call to %s for %s failed", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setInterfaceEnabled);
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::getNamedEndpoints(const JsonObject& parameters, JsonObject& response)
{
JsonArray namedEndpoints;
namedEndpoints.Add("CMTS");
response["endpoints"] = namedEndpoints;
returnResponse(true)
}
uint32_t Network::trace(const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
{
if (!parameters.HasLabel("endpoint"))
LOGERR("No endpoint specified");
else
{
string endpoint = "";
int packets = 0;
getStringParameter("endpoint", endpoint);
if (parameters.HasLabel("packets")) // packets is optional?
getNumberParameter("packets", packets);
if (_doTrace(endpoint, packets, response))
result = true;
else
LOGERR("Failed to perform network trace");
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::traceNamedEndpoint(const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
{
if (!parameters.HasLabel("endpointName"))
LOGERR("No endpointName specified");
else
{
string endpointName = "";
int packets = 0;
getStringParameter("endpointName", endpointName);
if (parameters.HasLabel("packets")) // packets is optional?
getNumberParameter("packets", packets);
if (_doTraceNamedEndpoint(endpointName, packets, response))
result = true;
else
LOGERR("Failed to perform network trace names endpoint");
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::ping (const JsonObject& parameters, JsonObject& response)
{
string guid;
getStringParameter("guid", guid)
uint32_t packets;
getDefaultNumberParameter("packets", packets, DEFAULT_PING_PACKETS);
bool result = false;
if(m_isPluginInited)
{
if (parameters.HasLabel("endpoint"))
{
string endpoint;
getStringParameter("endpoint", endpoint);
response = _doPing(guid, endpoint, packets);
result = response["success"].Boolean();
}
else
{
LOGERR("No endpoint argument");
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::pingNamedEndpoint (const JsonObject& parameters, JsonObject& response)
{
string guid;
getStringParameter("guid", guid)
uint32_t packets;
getDefaultNumberParameter("packets", packets, DEFAULT_PING_PACKETS);
bool result = false;
if(m_isPluginInited)
{
if (parameters.HasLabel("endpointName"))
{
string endpointName;
getDefaultStringParameter("endpointName", endpointName, "")
response = _doPingNamedEndpoint(guid, endpointName, packets);
result = response["success"].Boolean();
}
else
{
LOGERR("No endpointName argument");
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::setIPSettings(const JsonObject& parameters, JsonObject& response)
{
bool result = false;
if(m_isPluginInited)
return setIPSettingsInternal(parameters, response);
else
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
returnResponse(result)
}
uint32_t Network::setIPSettings2(const JsonObject& parameters, JsonObject& response)
{
JsonObject internal;
string interface = "";
string ipversion = "";
string netmask = "";
string gateway = "";
string ipaddr = "";
string primarydns = "";
string secondarydns = "";
bool autoconfig = true;
bool result = false;
if(m_isPluginInited)
{
getDefaultStringParameter("interface", interface, "");
internal ["interface"] = interface;
getDefaultStringParameter("ipversion", ipversion, "");
internal ["ipversion"] = ipversion;
getDefaultBoolParameter("autoconfig", autoconfig, true);
internal ["autoconfig"] = autoconfig;
getDefaultStringParameter("ipaddr", ipaddr, "");
internal ["ipaddr"] = ipaddr;
getDefaultStringParameter("netmask", netmask, "");
internal ["netmask"] = netmask;
getDefaultStringParameter("gateway", gateway, "");
internal ["gateway"] = gateway;
getDefaultStringParameter("primarydns", primarydns, "0.0.0.0");
internal ["primarydns"] = primarydns;
getDefaultStringParameter("secondarydns", secondarydns, "");
internal ["secondarydns"] = secondarydns;
return setIPSettingsInternal(internal, response);
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::setIPSettingsInternal(const JsonObject& parameters, JsonObject& response)
{
bool result = false;
struct in_addr ip_address, gateway_address, mask;
struct in_addr broadcast_addr1, broadcast_addr2;
if ((parameters.HasLabel("interface")) && (parameters.HasLabel("ipversion")) && (parameters.HasLabel("autoconfig")) &&
(parameters.HasLabel("ipaddr")) && (parameters.HasLabel("netmask")) && (parameters.HasLabel("gateway")) &&
(parameters.HasLabel("primarydns")) && (parameters.HasLabel("secondarydns")))
{
string interface = "";
string ipversion = "";
bool autoconfig = false;
string ipaddr = "";
string netmask = "";
string gateway = "";
string primarydns = "";
string secondarydns = "";
getStringParameter("interface", interface);
getStringParameter("ipversion", ipversion);
getBoolParameter("autoconfig", autoconfig);
getStringParameter("ipaddr", ipaddr);
getStringParameter("netmask", netmask);
getStringParameter("gateway", gateway);
getStringParameter("primarydns", primarydns);
getStringParameter("secondarydns", secondarydns);
IARM_BUS_NetSrvMgr_Iface_Settings_t iarmData = {0};
strncpy(iarmData.interface, interface.c_str(), 16);
iarmData.interface[sizeof(iarmData.interface) - 1] = '\0';
strncpy(iarmData.ipversion, ipversion.c_str(), 16);
iarmData.ipversion[sizeof(iarmData.ipversion) - 1] = '\0';
iarmData.autoconfig = autoconfig;
strncpy(iarmData.ipaddress, ipaddr.c_str(), 16);
strncpy(iarmData.netmask, netmask.c_str(), 16);
strncpy(iarmData.gateway, gateway.c_str(), 16);
strncpy(iarmData.primarydns, primarydns.c_str(), 16);
strncpy(iarmData.secondarydns, secondarydns.c_str(), 16);
iarmData.isSupported = false;
if (!autoconfig)
{
RFC_ParamData_t param;
if (Utils::getRFCConfig("Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.Network.ManualIPSettings.Enable", param))
{
if (param.type == WDMP_BOOLEAN && (strncasecmp(param.value,"true",4) == 0))
{
iarmData.isSupported = true;
}
}
if (false == iarmData.isSupported)
{
LOGWARN("Manual IP Settings not Enabled..\n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
bool mask_validation;
mask_validation = isValidCIDRv4(netmask.c_str());
if (false == mask_validation)
{
LOGWARN("Netmask is not valid ..\n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
if (inet_pton(AF_INET, ipaddr.c_str(), &ip_address) == 1 &&
inet_pton(AF_INET, netmask.c_str(), &mask) == 1 &&
inet_pton(AF_INET, gateway.c_str(), &gateway_address) == 1)
{
broadcast_addr1.s_addr = ip_address.s_addr | ~mask.s_addr;
broadcast_addr2.s_addr = gateway_address.s_addr | ~mask.s_addr;
if (ip_address.s_addr == gateway_address.s_addr)
{
LOGWARN("Interface and Gateway IP are same , return false \n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
if (broadcast_addr1.s_addr != broadcast_addr2.s_addr)
{
LOGWARN("Interface and Gateway IP is not in same broadcast domain, return false \n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
if (ip_address.s_addr == broadcast_addr1.s_addr)
{
LOGWARN("Interface and Broadcast IP is same, return false \n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
if (gateway_address.s_addr == broadcast_addr2.s_addr)
{
LOGWARN("Gateway and Broadcast IP is same, return false \n");
response["supported"] = iarmData.isSupported;
result = false;
returnResponse(result)
}
}
}
if (IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setIPSettings, (void *) &iarmData, sizeof(iarmData)))
{
response["supported"] = iarmData.isSupported;
result = true;
}
else
response["supported"] = iarmData.isSupported;
}
returnResponse(result)
}
uint32_t Network::getIPSettings(const JsonObject& parameters, JsonObject& response)
{
JsonObject internal;
JsonObject InternalResponse;
int errCode;
bool result = false;
string interface = "";
string ipversion = "";
if(m_isPluginInited)
{
getDefaultStringParameter("interface", interface,"");
internal ["interface"] = interface;
getDefaultStringParameter("ipversion", ipversion,"");
internal ["ipversion"] = ipversion;
if (getIPSettingsInternal(internal, InternalResponse, errCode))
{
if (NETWORK_IPADDRESS_ACQUIRED == errCode)
{
response["interface"] = InternalResponse["interface"];
response["ipversion"] = InternalResponse["ipversion"];
response["autoconfig"] = InternalResponse["autoconfig"];
response["ipaddr"] = InternalResponse["ipaddr"];
response["netmask"] = InternalResponse["netmask"];
response["gateway"] = InternalResponse["gateway"];
response["primarydns"] = InternalResponse["primarydns"];
response["secondarydns"] = InternalResponse["secondarydns"];
result = true;
}
}
}
else
{
LOGWARN ("Network plugin not initialised yet returning from %s", __FUNCTION__);
}
returnResponse(result)
}
uint32_t Network::getIPSettings2(const JsonObject& parameters, JsonObject& response)
{
JsonObject internal;
JsonObject InternalResponse;
int errCode;
bool result = false;
string interface = "";
string ipversion = "";
if(m_isPluginInited)
{
getDefaultStringParameter("interface", interface, "");
internal ["interface"] = interface;
getDefaultStringParameter("ipversion", ipversion, "");
internal ["ipversion"] = ipversion;
if (getIPSettingsInternal(internal,InternalResponse,errCode))
{
/*If the device was configured to use autoconfig IP but device does not have valid IP yet Could be the Router does not have DHCP Server running or