RDK Documentation (Open Sourced RDK Components)
netsrvmgrUtiles.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /*****************************************************************************
21  * STANDARD INCLUDE FILES
22  *****************************************************************************/
23 /**
24 * @defgroup netsrvmgr
25 * @{
26 * @defgroup netsrvmgr
27 * @{
28 **/
29 
30 
31 /**
32  * @file netSrvMgrUtiles.cpp
33  *
34  * @brief DeviceInfo X_RDKCENTRAL-COM_xBlueTooth API Implementation.
35  *
36  * This is the implementation of the netsrvmgr utility API.
37  *
38  * @par Document
39  * TBD Relevant design or API documentation.
40  *
41  */
42 
43 
44 #include <netinet/in.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <ifaddrs.h>
48 #include <algorithm>
49 #include "NetworkMgrMain.h"
50 #include "netsrvmgrUtiles.h"
51 #include <string.h>
52 #include <errno.h>
53 #include "netsrvmgrIarm.h"
54 #ifdef ENABLE_NLMONITOR
55 #include "netlinkifc.h"
56 #else
57 #include <net/if.h>
58 #endif //ENABLE_NLMONITOR
59 #include <arpa/inet.h>
60 #define IN_IS_ADDR_LINKLOCAL(a) (((a) & htonl(0xffff0000)) == htonl (0xa9fe0000))
61 #define INTERFACE_SIZE 10
62 
63 #define TRIGGER_DHCP_LEASE_FILE "/lib/rdk/triggerDhcpLease.sh"
64 #define MAX_TIME_LENGTH 32
65 #define INTERFACE_STATUS_FILE_PATH_BUFFER 100
66 #define INTERFACE_STATUS_FILE_PATH "/sys/class/net/%s/operstate"
67 #define ETHERNET_UP_STATUS "UP"
68 #ifdef SAFEC_RDKV
69 #include "safec_lib.h"
70 #else
71 #define STRCPY_S(dest,size,source) \
72  strcpy(dest, source);
73 #endif
74 
75 #ifdef USE_TELEMETRY_2_0
76 
77 void telemetry_init(char* name)
78 {
79  t2_init(name);
80 }
81 
82 void telemetry_event_s(char* marker, char* value)
83 {
84  T2ERROR t2error = t2_event_s(marker, value);
85  if (t2error != T2ERROR_SUCCESS)
86  LOG_ERR("t2_event_s(\"%s\", \"%s\") returned error code %d", marker, value, t2error);
87 }
88 
89 void telemetry_event_d(char* marker, int value)
90 {
91  T2ERROR t2error = t2_event_d(marker, value);
92  if (t2error != T2ERROR_SUCCESS)
93  LOG_ERR("t2_event_d(\"%s\", %d) returned error code %d", marker, value, t2error);
94 }
95 
96 #endif // #ifdef USE_TELEMETRY_2_0
97 
98 static bool loadKeyFile (const char* filename, GKeyFile* keyFile);
99 static bool writeKeyFile (const char* filename, GKeyFile* keyFile);
100 
101 using namespace netSrvMgrUtiles;
102 
103 /**
104  * @fn netSrvMgrUtiles::getMacAddress_IfName(const char *ifName_in, char *macAddress_out)
105  * @brief This function gets the MAC address of the AspectRatio against the id specified, only if
106  * the id passed is valid.
107  *
108  * @param[in] ifName_in Indicates the interface name which the mac address is required.
109  * @param[out] macAddress_out Indicates the mac address of ifname_in interface name.
110  *
111  * @return Returns true if successfully gets the mac address of interface provided or else false.
112  */
113 bool netSrvMgrUtiles::getMacAddress_IfName(const char *ifName_in, char macAddress_out[MAC_ADDR_BUFF_LEN])
114 {
115  struct ifreq ifr;
116  int sd;
117  unsigned char *mac = NULL;
118  bool ret = false;
119  LOG_ENTRY_EXIT;
120 
121  if(NULL == ifName_in)
122  {
123  LOG_TRACE("Failed, due to NULL for interface name (\'ifName_in\') .");
124  return ret;
125  }
126 
127  sd = socket(AF_INET, SOCK_DGRAM, 0);
128  if (sd > 0) {
129  ifr.ifr_addr.sa_family = AF_INET;
130  strncpy(ifr.ifr_name, ifName_in, IFNAMSIZ-1);
131  if (0 == ioctl(sd, SIOCGIFHWADDR, &ifr)) {
132  mac = (unsigned char *) ifr.ifr_hwaddr.sa_data;
133  if(mac) {
134  snprintf( macAddress_out, MAC_ADDR_BUFF_LEN, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
135  LOG_DBG("The Mac address is \'%s\' (for provided interface %s).", macAddress_out, ifName_in );
136  ret = true;
137  }
138  }
139  else {
140  LOG_ERR("Failed in ioctl() with \'%s\'..", strerror (errno));
141  }
142  close(sd);
143  }
144  else {
145  LOG_ERR("Failed to create socket() with \'%d\' return as \'%s\'.", sd, strerror (errno) );
146  }
147 
148  return ret;
149 }
150 
151 /* End of doxygen group */
152 /**
153  * @}
154  */
155 /* End of file netSrvMgrUtiles.cpp */
156 
157 /** @} */
158 
159 
160 
161 /**
162  * @fn netSrvMgrUtiles::triggerDhcpLease(const char* operation, const char* interface)
163  * @brief Triggers a "renew" or "release and renew" of DHCP lease on specified interface.
164  * If DHCP lease renew is requested, the specified interface may be NULL in which case
165  * a DHCP lease renew will be triggered for all interfaces running the DHCPv4 client.
166  * If DHCP lease release and renew is requested, the specified interface must not be NULL.
167  *
168  * @param[in] operation DHCP lease operation to perform ("renew" or "release and renew")
169  * @param[in] interface interface (wlan0, eth0, etc.) to perform DHCP lease operation on
170  */
171 void netSrvMgrUtiles::triggerDhcpLease(const char* operation, const char* interface)
172 {
173  char command[200];
174  snprintf (command, sizeof(command), "%s %s %s 2>&1",
175  TRIGGER_DHCP_LEASE_FILE, operation, interface ? interface : "");
176 
177  LOG_INFO("Executing command [%s]", command);
178 
179  char scriptLogOutputLine[256];
180 
181  FILE *fp = popen (command, "r");
182  if (fp == NULL)
183  {
184  LOG_ERR("Failed to execute command [%s]", command);
185  return;
186  }
187 
188  // log script output at INFO level
189  while (fgets (scriptLogOutputLine, sizeof (scriptLogOutputLine), fp) != NULL)
190  {
191  LOG_INFO("%s", scriptLogOutputLine);
192  }
193 
194  int pclose_status = pclose (fp);
195  int status = WEXITSTATUS (pclose_status);
196 
197  LOG_INFO("Exit code [%d] from command [%s]", status, command);
198 }
199 
200 void netSrvMgrUtiles::triggerDhcpRenew(const char* interface)
201 {
202  netSrvMgrUtiles::triggerDhcpLease("renew", interface);
203 }
204 
205 void netSrvMgrUtiles::triggerDhcpReleaseAndRenew(const char* interface)
206 {
207  netSrvMgrUtiles::triggerDhcpLease("release_and_renew", interface);
208 }
209 
210 static bool getIPv6RouteInterface (char *devname)
211 {
212  bool route_found = false;
213  FILE *fp = fopen ("/proc/net/ipv6_route", "r");
214  if (fp != NULL)
215  {
216  char dst[50], gw[50];
217  int ret;
218  while ((ret = fscanf (fp, "%s %*x %*s %*x %s %*x %*x %*x %*x %s", dst, gw, devname)) != EOF)
219  {
220  /*return value must be parameter's number*/
221  if (ret != 3)
222  {
223  continue;
224  }
225  if ((strcmp (dst, gw) == 0) && (strcmp (devname, "sit0") != 0) && (strcmp (devname, "lo") != 0))
226  {
227 // readDevFile(devname);
228  LOG_INFO( "active interface v6 %s", devname);
229  route_found = true;
230  break;
231  }
232  }
233  fclose (fp);
234  }
235  return route_found;
236 }
237 
238 static bool getIPv4RouteInterface (char *devname)
239 {
240  bool route_found = false;
241  FILE *fp = fopen ("/proc/net/route", "r");
242  if (fp != NULL)
243  {
244  char line[100], *ptr, *ctr, *sptr;
245  while (fgets (line, sizeof (line), fp))
246  {
247  ptr = strtok_r (line, " \t", &sptr);
248  ctr = strtok_r (NULL, " \t", &sptr);
249  if (ptr != NULL && ctr != NULL && strcmp (ctr, "00000000") == 0)
250  {
251  STRCPY_S(devname, INTERFACE_SIZE, ptr);
252 // readDevFile(devname);
253  LOG_INFO("ctive interface v4 %s", devname);
254  route_found = true;
255  break;
256  }
257  }
258  fclose (fp);
259  }
260  return route_found;
261 }
262 
264 {
265  LOG_ENTRY_EXIT;
266  if (getIPv6RouteInterface (devname) || getIPv4RouteInterface (devname))
267  return true;
268  LOG_INFO("No Route Found");
269  return false;
270 }
271 
273 {
274  LOG_ENTRY_EXIT;
275  if(!getRouteInterface(devname))
276  {
277  LOG_ERR("Get Route interface failure");
278  return false;
279  }
280  if(!readDevFile(devname))
281  {
282  LOG_ERR("Get Route interface type failure");
283  return false;
284  }
285  LOG_TRACE("Route interface type %s", devname);
286  return true;
287 }
288 
289 bool netSrvMgrUtiles::readDevFile(char *deviceName)
290 {
291  LOG_ENTRY_EXIT;
292 
293  GError *error = NULL;
294  gboolean result = FALSE;
295  gchar* devfilebuffer = NULL;
296  const gchar* deviceFile = "//etc/device.properties";
297  if(!deviceName)
298  {
299  LOG_ERR("device name is null");
300  }
301  else if (g_file_get_contents (deviceFile, &devfilebuffer, NULL, &error) == false)
302  {
303  LOG_ERR("No contents in device properties");
304  if (!error) g_error_free (error);
305  }
306  else
307  {
308  gchar **tokens = g_strsplit_set(devfilebuffer,",='\n'", -1);
309  g_free (devfilebuffer);
310  guint toklength = g_strv_length(tokens);
311  guint loopvar=0;
312  LOG_INFO("Interface Name %s", deviceName);
313  for (loopvar=0; loopvar<toklength; loopvar++)
314  {
315  if (g_strrstr(g_strstrip(tokens[loopvar]), "ETHERNET_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
316  {
317  if ((loopvar+1) < toklength )
318  {
319  STRCPY_S(deviceName, INTERFACE_SIZE, "ETHERNET");
320  result=TRUE;
321  break;
322  }
323  }
324  else if (g_strrstr(g_strstrip(tokens[loopvar]), "MOCA_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
325  {
326  if ((loopvar+1) < toklength )
327  {
328  STRCPY_S(deviceName, INTERFACE_SIZE, "MOCA");
329  result=TRUE;
330  break;
331  }
332  }
333  else if (g_strrstr(g_strstrip(tokens[loopvar]), "WIFI_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
334  {
335  if ((loopvar+1) < toklength )
336  {
337  STRCPY_S(deviceName, INTERFACE_SIZE, "WIFI");
338  result=TRUE;
339  break;
340  }
341  }
342  }
343  g_strfreev (tokens);
344  LOG_INFO("Network Type %s", deviceName);
345  }
346  return result;
347 }
348 
349 char netSrvMgrUtiles::getAllNetworkInterface(char* devAllInterface)
350 {
351  LOG_ENTRY_EXIT;
352  struct ifaddrs *ifAddrStruct,*tmpAddrPtr;
353  bool firstInterface=false;
354  char count=0;
355  char tempInterface[10];
356  getifaddrs(&ifAddrStruct);
357  tmpAddrPtr = ifAddrStruct;
358  GString *device = g_string_new(NULL);
359 
360  while (tmpAddrPtr)
361  {
362  if (tmpAddrPtr->ifa_addr && (strcmp(tmpAddrPtr->ifa_name,"sit0") != 0) && (strcmp(tmpAddrPtr->ifa_name,"lo") != 0))
363  {
364  g_stpcpy(tempInterface,tmpAddrPtr->ifa_name);
365  LOG_INFO("1 interface [%s].", tempInterface);
366  if((readDevFile(tempInterface)) && (!g_strrstr(device->str,tempInterface)))
367  {
368  if(firstInterface)
369  {
370  g_string_append_printf(device,"%s",",");
371  }
372  else
373  {
374  firstInterface=true;
375  }
376  g_string_append_printf(device,"%s",tempInterface);
377  LOG_INFO("interface [%s] interface List [%s]", tempInterface,device->str);
378  count++;
379  }
380  }
381  tmpAddrPtr = tmpAddrPtr->ifa_next;
382  }
383  freeifaddrs(ifAddrStruct);
384  STRCPY_S(devAllInterface, INTERFACE_LIST, device->str);
385  g_string_free(device,TRUE);
386  return count;
387 }
388 
389 bool netSrvMgrUtiles::getCurrentTime(char* currTime, const char *timeFormat)
390 {
391  LOG_ENTRY_EXIT;
392  time_t cTime;
393  struct tm *tmp;
394  time(&cTime);
395  tmp=localtime(&cTime);
396  if(tmp == NULL)
397  {
398  LOG_ERR("Error getting local time ");
399  return false;
400  }
401  if(strftime(currTime,MAX_TIME_LENGTH,timeFormat,tmp) == 0)
402  {
403  LOG_ERR("strftime failed in getting time format format %s", timeFormat);
404  return false;
405  }
406  return true;
407 }
408 
409 bool netSrvMgrUtiles::checkInterfaceActive(char *interfaceName)
410 {
411  LOG_ENTRY_EXIT;
412  FILE * file;
413  char buffer[INTERFACE_STATUS_FILE_PATH_BUFFER]={0};
414  bool ret=false;
415  if(!interfaceName)
416  {
417  LOG_INFO("Device doesnt support Ethernet !!!!");
418  return ret;
419  }
420  sprintf(buffer,INTERFACE_STATUS_FILE_PATH,interfaceName);
421  file = fopen(buffer, "r");
422  if (file)
423  {
424  fscanf(file,"%s",buffer);
425  if(NULL != strcasestr(buffer,ETHERNET_UP_STATUS))
426  {
427  LOG_INFO("TELEMETRY_NETWORK_MANAGER_ETHERNET_MODE");
428  ret=true;
429  }
430  else
431  {
432  LOG_INFO("TELEMETRY_NETWORK_MANAGER_WIFI_MODE");
433  }
434  fclose(file);
435  }
436  return ret;
437 }
438 
439 // returns:
440 // true if file with given 'filename' could be loaded into 'keyFile'
441 // false otherwise
442 bool loadKeyFile (const char* filename, GKeyFile* keyFile)
443 {
444  LOG_ENTRY_EXIT;
445 
446  bool ret = true;
447  GError* err = NULL;
448  g_key_file_load_from_file (keyFile, filename, G_KEY_FILE_NONE, &err);
449  if (err != NULL)
450  {
451  LOG_ERR("error loading '%s' (error domain=%d code=%d message=%s)",
452  filename, err->domain, err->code, err->message);
453  g_error_free (err);
454  ret = false;
455  }
456  return ret;
457 }
458 
459 // returns:
460 // true if 'keyFile' could be written into file with given 'filename'
461 // false otherwise
462 bool writeKeyFile (const char* filename, GKeyFile* keyFile)
463 {
464  LOG_ENTRY_EXIT;
465 
466  bool ret = true;
467  gchar *contents = g_key_file_to_data (keyFile, NULL, NULL);
468  GError* err = NULL;
469  g_file_set_contents (filename, contents, -1, &err);
470  if (err != NULL)
471  {
472  LOG_ERR("error writing '%s' (error domain=%d code=%d message=%s)",
473  filename, err->domain, err->code, err->message);
474  g_error_free (err);
475  ret = false;
476  }
477  g_free (contents);
478  return ret;
479 }
480 
481 bool netSrvMgrUtiles::getInterfaceConfig(const char *ifName, const unsigned int family, char *interfaceIp, char *netMask)
482 {
483  LOG_ENTRY_EXIT;
484 #ifdef ENABLE_NLMONITOR
485  char macAddress[MAC_ADDR_BUFF_LEN] = {'\0'};
486  std::vector<std::string> ipAddr;
487 
488  /*to get IP address based on interface and family input parameter */
489  NetLinkIfc::get_instance()->getIpaddr(ifName,family,ipAddr);
490  if(ipAddr.empty())
491  {
492  LOG_ERR("No ipaddress on interface %s", ifName);
493  return false;
494  }
495  else
496  {
497  /*to get MAC address based on interface*/
498  netSrvMgrUtiles::getMacAddress_IfName(ifName,macAddress);
499  std::string macAddrStr(macAddress);
500 
501  for (auto const& s : ipAddr)
502  {
504  {
505  LOG_INFO("skipping ULA V6 IP. ");
506  continue;
507  }
508  if(((ipAddr.size()) > 1) && (netSrvMgrUtiles::check_global_v6_based_macaddress(s,macAddrStr)))
509  {
510  LOG_INFO("skipping ip addr %s since it is based on mac %s", s.c_str(), macAddrStr.c_str());
511  continue;
512  }
513  std::string tempStr = s.substr(0, s.find("/", 0)); //remove /64 in ip 2601:a40:300:164:aa11:fcff:fefd:1e8d/64
514  if (chk_ipaddr_linklocal(tempStr.c_str(),family))
515  {
516  LOG_INFO("skipping link local ip");
517  continue;
518  }
519  if (family == AF_INET && isIPv4AddressScopeDocumentation(tempStr))
520  {
521  LOG_INFO("skipping reserved ip %s", tempStr.c_str());
522  continue;
523  }
524  STRCPY_S(interfaceIp, INET6_ADDRSTRLEN, tempStr.c_str());
525  }
526  }
527  /* To get Net Mask */
528  netSrvMgrUtiles::getNetMask_IfName(ifName,family,netMask);
529 #else
530  LOG_ERR("ENABLE_NLMONITOR not set");
531 #endif
532  return true;
533 }
534 
535 bool netSrvMgrUtiles::getSTBip(char *stbip,bool *isIpv6)
536 {
537  LOG_ENTRY_EXIT;
538  bool ret=false;
539  *isIpv6 = false;
540 
541  if (getSTBip_family(stbip,"ipv6"))
542  {
543  ret = true;
544  *isIpv6 = true;
545  }
546  else if (getSTBip_family(stbip,"ipv4"))
547  {
548  ret = true;
549  }
550  return ret;
551 }
552 
553 bool netSrvMgrUtiles::getSTBip_family(char *stbip,const char *family)
554 {
555  LOG_ENTRY_EXIT;
556  bool ret=false;
557 #ifdef ENABLE_NLMONITOR
558  std::vector<std::string> ipAddr;
559  char interface[INTERFACE_SIZE]={0};
560  char macAddress[MAC_ADDR_BUFF_LEN] = {'\0'};
561  std::string s_family(family);
562  int i_family = 0;
563 
564  //1. Filter for family.
565  if (s_family == "ipv4")
566  {
567  i_family = AF_INET;
568  }
569  else if (s_family == "ipv6")
570  {
571  i_family = AF_INET6;
572  }
573  else
574  {
575  LOG_ERR("Family [%s] Not recognized.", s_family.c_str());
576  return ret;
577  }
578 
579  //2. Extract interface.
580  if(!netSrvMgrUtiles::getRouteInterface(interface))
581  {
583  {
584  LOG_ERR("No routable interface found.");
585  return ret;
586  }
587  }
588 
589  std::string ifcStr(interface);
590  NetLinkIfc::get_instance()->getIpaddr(ifcStr,i_family,ipAddr);
591  if(ipAddr.empty())
592  {
593  LOG_ERR("No ipaddress on interface %s, for Family %s", ifcStr.c_str(),s_family.c_str());
594  return ret;
595  }
596 
597  netSrvMgrUtiles::getMacAddress_IfName(interface,macAddress);
598  std::string macAddrStr(macAddress);
599 
600  for (auto const& s : ipAddr)
601  {
603  {
604  LOG_INFO("skipping ULA V6 IP.");
605  continue;
606  }
607  if(((ipAddr.size()) > 1) && (netSrvMgrUtiles::check_global_v6_based_macaddress(s,macAddrStr)))
608  {
609  LOG_INFO("skipping ip addr %s since it is based on mac %s", s.c_str(), macAddrStr.c_str());
610  continue;
611  }
612  std::string tempStr = s.substr(0, s.find("/", 0)); //remove /64 in ip 2601:a40:300:164:aa11:fcff:fefd:1e8d/64
613  if (chk_ipaddr_linklocal(tempStr.c_str(),i_family))
614  {
615  LOG_INFO("skipping link local ip");
616  continue;
617  }
618  if (i_family == AF_INET && isIPv4AddressScopeDocumentation(tempStr))
619  {
620  LOG_INFO("skipping reserved ip %s", tempStr.c_str());
621  continue;
622  }
623  STRCPY_S(stbip, MAX_IP_ADDRESS_LEN, tempStr.c_str());
624  ret=true;
625  }
626 
627 #else
628  LOG_ERR("ENABLE_NLMONITOR not set ");
629 #endif
630  return ret;
631 }
632 
633 //Check IP string(IPv4 and IPv6) is link local address
634 // Check for 169.254 since it will be come as Global scope.
635 
636 bool netSrvMgrUtiles::chk_ipaddr_linklocal(const char *stbip,unsigned int family)
637 {
638  LOG_ENTRY_EXIT;
639  struct sockaddr_in6 sa6;
640  struct sockaddr_in sa;
641  switch(family)
642  {
643  case AF_INET:
644  inet_pton(AF_INET, stbip, &(sa.sin_addr));
645  return IN_IS_ADDR_LINKLOCAL(sa.sin_addr.s_addr);
646  case AF_INET6:
647  inet_pton(AF_INET6, stbip, &(sa6.sin6_addr));
648  return IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr);
649  break;
650  default:
651  LOG_ERR("interface family not supported %d ", family);
652  return false;
653  }
654 }
655 
656 bool netSrvMgrUtiles::currentActiveInterface(char *currentInterface)
657 {
658  LOG_ENTRY_EXIT;
659  std::string ifcStr;
660  if (getenv("WIFI_INTERFACE") != NULL)
661  {
662  if (getenv("MOCA_INTERFACE") != NULL)
663  {
664  ifcStr=getenv("MOCA_INTERFACE");
665  if(!checkInterfaceActive((char *)ifcStr.c_str()))
666  {
667  ifcStr=getenv("WIFI_INTERFACE");
668  }
669  }
670  }
671  else if (getenv("MOCA_INTERFACE") != NULL)
672  {
673  ifcStr=getenv("MOCA_INTERFACE");
674  }
675  else
676  {
677  LOG_ERR("current interface not there ");
678  return false;
679  }
680  STRCPY_S(currentInterface, INTERFACE_SIZE, ifcStr.c_str());
681  return true;
682 }
683 
684 
685 //IPV6 Address based on mac XXXX:XXXX:XXXX:XXXX:ABAA:AAff:feAA:AAAA XXXX:XXXX:XXXX:XXXX is the prefix mac address is AA:AA:AA:AA:AA:AA B is A XOR 2 Inserted FF:FE
686 //global ipv6 based on mac address
687 
688 bool netSrvMgrUtiles::check_global_v6_based_macaddress(std::string ipv6Addr,std::string macAddr)
689 {
690  LOG_ENTRY_EXIT;
691  if((ipv6Addr.empty()) && (macAddr.empty()))
692  {
693  LOG_ERR("ipv6 addr or mac addr is empty ");
694  return false;
695  }
696  else if (macAddr.size() < 12)
697  {
698  LOG_ERR("mac addr is less than 12");
699  return false;
700  }
701  std::string tmpMacAddr(macAddr);//A8:11:FC:FD:1E:8D
702  std::transform(tmpMacAddr.begin(), tmpMacAddr.end(), tmpMacAddr.begin(), ::tolower); //a8:11:fc:fd:1e:8d
703  tmpMacAddr.erase(std::remove(tmpMacAddr.begin(), tmpMacAddr.end(), ':'), tmpMacAddr.end()); //a811fcfd1e8d
704  std::string strTmpAdd2Mac="fffe";
705  tmpMacAddr.insert(6,strTmpAdd2Mac);//a811fcfffefd1e8d
706  tmpMacAddr.replace(0,2,"");//11fcfffefd1e8d
707  std::string tmpIpv6Str(ipv6Addr);//2601:a40:300:164:aa11:fcff:fefd:1e8d
708  std::transform(tmpIpv6Str.begin(), tmpIpv6Str.end(), tmpIpv6Str.begin(), ::tolower);//2601:a40:300:164:aa11:fcff:fefd:1e8d
709  tmpIpv6Str.erase(std::remove(tmpIpv6Str.begin(), tmpIpv6Str.end(), ':'), tmpIpv6Str.end());//2601a40300164aa11fcfffefd1e8d
710  if(tmpIpv6Str.find(tmpMacAddr) != std::string::npos)
711  {
712  LOG_INFO("mac %s based global v6 address %s ", macAddr.c_str(),ipv6Addr.c_str());
713  return true;
714  }
715  return false;
716 }
717 
718 //Check if v6 address is ULA; If the IPv6 address begins with fd or fc, it is a ULA
720 {
721  LOG_ENTRY_EXIT;
722  if(ipv6Addr.empty())
723  {
724  LOG_ERR("ipv6 addr is empty");
725  return false;
726  }
727 
728  std::string tmpIpv6Str(ipv6Addr);
729  if ((tmpIpv6Str.rfind("fd", 0) != std::string::npos) || (tmpIpv6Str.rfind("fc", 0) != std::string::npos))
730  {
731  LOG_INFO("ULA v6 address %s ", ipv6Addr.c_str());
732  return true;
733  }
734  return false;
735 }
736 
737 bool netSrvMgrUtiles::getCommandOutput(const char *command, char *output_buffer, size_t output_buffer_size)
738 {
739  bool ret = false;
740  FILE *file = popen(command, "r");
741  if (!file)
742  {
743  LOG_ERR("command failed %s", command);
744  return ret;
745  }
746  if (fgets(output_buffer, output_buffer_size, file) != NULL)
747  {
748  ret = true;
749  }
750  else
751  {
752  LOG_ERR("Failed in getting output from command %s", command);
753  }
754  pclose(file);
755  return ret;
756 }
757 bool netSrvMgrUtiles::getNetMask_IfName(const char *ifName_in,const unsigned int family, char *netMask_out)
758 {
759  LOG_ENTRY_EXIT;
760  struct ifreq ifr;
761  struct sockaddr_in *ipaddr;
762  bool ret = false;
763  int sock;
764  char address[INET6_ADDRSTRLEN]={0};
765 
766  sock = socket(AF_INET, SOCK_DGRAM, 0);
767  if (sock > 0)
768  {
769  ifr.ifr_addr.sa_family = family;
770 
771  strncpy(ifr.ifr_name, ifName_in, IFNAMSIZ-1);
772 
773  /* If the device uses virtual interface, then get the subnet mask of the virtual interface */
774  if (!system("/lib/rdk/pni_controller.sh uses_virtual_interface"))
775  {
776  strcat(ifr.ifr_name, ":0");
777  }
778 
779  /* To get Net Mask */
780  if (ioctl(sock, SIOCGIFNETMASK, &ifr) != -1)
781  {
782  ipaddr = (struct sockaddr_in *)&ifr.ifr_netmask;
783  if (inet_ntop(family, &ipaddr->sin_addr, address, sizeof(address)) != NULL)
784  {
785  STRCPY_S(netMask_out, INET6_ADDRSTRLEN, address);
786  LOG_INFO("Netmask %s", netMask_out);
787  ret = true;
788  }
789  }
790  else
791  {
792  LOG_ERR("Failed in ioctl() with \'%s\'..", strerror (errno) );
793  }
794  close(sock);
795  }
796  else
797  {
798  LOG_ERR("Failed to create socket() with \'%d\' return as \'%s\'.", sock, strerror (errno) );
799  }
800  return ret;
801 }
802 
803 bool netSrvMgrUtiles::isIPv4AddressScopeDocumentation(const std::string& ipv4_address)
804 {
805  struct sockaddr_in sa;
806  inet_pton(AF_INET, ipv4_address.c_str(), &(sa.sin_addr));
807 
808  // IPv4 unicast address blocks reserved for documentation are all /24 networks (RFC 5737)
809  // so extract the network part of the address by bitwise-ANDing with 255.255.255.0
810  in_addr_t network_address = sa.sin_addr.s_addr & htonl(0xffffff00);
811 
812  return (network_address == htonl (0xc0000200) || // 192.0.2.0
813  network_address == htonl (0xc6336400) || // 198.51.100.0
814  network_address == htonl (0xcb007100)); // 203.0.113.0
815 }
netSrvMgrUtiles::chk_ipaddr_linklocal
bool chk_ipaddr_linklocal(const char *stbip, unsigned int family)
This function checks whether IP string(IPv4 and IPv6) is link local address or not.
Definition: netsrvmgrUtiles.cpp:636
netSrvMgrUtiles::check_global_v6_based_macaddress
bool check_global_v6_based_macaddress(std::string ipv6Addr, std::string macAddr)
This function checks whether the input ipv6 address is based on specified mac address.
Definition: netsrvmgrUtiles.cpp:688
netsrvmgrIarm.h
The header file provides components netSrvMgrIarm information APIs.
netSrvMgrUtiles::check_global_v6_ula_address
bool check_global_v6_ula_address(std::string ipv6Addr)
This function checks whether the input ipv6 address is Unique Local Address.
Definition: netsrvmgrUtiles.cpp:719
getCurrentTime
void getCurrentTime(struct timespec *timer)
Get Current time.
Definition: hostIf_utils.cpp:466
netSrvMgrUtiles::checkInterfaceActive
bool checkInterfaceActive(char *interfaceName)
This function returns the Ethernet active status of the interface, if finds the status from interface...
Definition: netsrvmgrUtiles.cpp:409
netSrvMgrUtiles::getSTBip
bool getSTBip(char *stbip, bool *isIpv6)
This function is used to get STB IP address and its IP version.
Definition: netsrvmgrUtiles.cpp:535
netSrvMgrUtiles::getAllNetworkInterface
char getAllNetworkInterface(char *devAllInterface)
This function parse all the device interface details and gives all the network interface device name ...
Definition: netsrvmgrUtiles.cpp:349
netSrvMgrUtiles::getInterfaceConfig
bool getInterfaceConfig(const char *ifName, const unsigned int family, char *interfaceIp, char *netMask)
This function is used to get Interface IP address and on which interface.
Definition: netsrvmgrUtiles.cpp:481
readDevFile
gboolean readDevFile(const char *deviceFile)
This function is used to get partner ID.
Definition: xcal-device-library.c:385
netsrvmgrUtiles.h
The header file provides components netSrvMgrUtiles information APIs.
netSrvMgrUtiles::getMacAddress_IfName
bool getMacAddress_IfName(const char *ifName_in, char macAddress_out[18])
This function is used to get the MAC address for the provided interface.
netSrvMgrUtiles::triggerDhcpLease
void triggerDhcpLease(const char *operation, const char *interface)
This function trigger the process by which the DHCP client renews or updates its IP address configura...
Definition: netsrvmgrUtiles.cpp:171
netSrvMgrUtiles::getRouteInterfaceType
bool getRouteInterfaceType(char *devname)
This function gets the active interface device type(Ethernet/MOCA/WIFI).
Definition: netsrvmgrUtiles.cpp:272
netSrvMgrUtiles::getCommandOutput
bool getCommandOutput(const char *command, char *output_buffer, size_t output_buffer_size)
This function is used to get the output of running the specified command.
Definition: netsrvmgrUtiles.cpp:737
netSrvMgrUtiles::isIPv4AddressScopeDocumentation
bool isIPv4AddressScopeDocumentation(const std::string &ipv4_address)
This function checks if the input ipv4 address is a unicast address reserved for documentation.
Definition: netsrvmgrUtiles.cpp:803
netSrvMgrUtiles::currentActiveInterface
bool currentActiveInterface(char *currentInterface)
This function is used to get the current active interface(WIFI/MOCA).
Definition: netsrvmgrUtiles.cpp:656
LOG_INFO
#define LOG_INFO(AAMP_JS_OBJECT, FORMAT,...)
Definition: jsutils.h:39
netSrvMgrUtiles::getRouteInterface
bool getRouteInterface(char *devname)
This function retrieves information about the active routing interface.
Definition: netsrvmgrUtiles.cpp:263
netSrvMgrUtiles::getNetMask_IfName
bool getNetMask_IfName(const char *ifName_in, const unsigned int, char *netMask_out)
This function is used to get the NetMask address for the provided interface.
Definition: netsrvmgrUtiles.cpp:757
TRUE
#define TRUE
Defines for TRUE/FALSE/ENABLE flags.
Definition: wifi_common_hal.h:199
LOG_TRACE
#define LOG_TRACE
Definition: rdk_debug_priv.c:83