44 #include <netinet/in.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
49 #include "NetworkMgrMain.h"
54 #ifdef ENABLE_NLMONITOR
55 #include "netlinkifc.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
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"
69 #include "safec_lib.h"
71 #define STRCPY_S(dest,size,source) \
75 #ifdef USE_TELEMETRY_2_0
77 void telemetry_init(
char* name)
82 void telemetry_event_s(
char* marker,
char* value)
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);
89 void telemetry_event_d(
char* marker,
int value)
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);
96 #endif // #ifdef USE_TELEMETRY_2_0
98 static bool loadKeyFile (
const char* filename, GKeyFile* keyFile);
99 static bool writeKeyFile (
const char* filename, GKeyFile* keyFile);
101 using namespace netSrvMgrUtiles;
117 unsigned char *mac = NULL;
121 if(NULL == ifName_in)
123 LOG_TRACE(
"Failed, due to NULL for interface name (\'ifName_in\') .");
127 sd = socket(AF_INET, SOCK_DGRAM, 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;
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 );
140 LOG_ERR(
"Failed in ioctl() with \'%s\'..", strerror (errno));
145 LOG_ERR(
"Failed to create socket() with \'%d\' return as \'%s\'.", sd, strerror (errno) );
174 snprintf (command,
sizeof(command),
"%s %s %s 2>&1",
175 TRIGGER_DHCP_LEASE_FILE, operation, interface ? interface :
"");
177 LOG_INFO(
"Executing command [%s]", command);
179 char scriptLogOutputLine[256];
181 FILE *fp = popen (command,
"r");
184 LOG_ERR(
"Failed to execute command [%s]", command);
189 while (fgets (scriptLogOutputLine,
sizeof (scriptLogOutputLine), fp) != NULL)
191 LOG_INFO(
"%s", scriptLogOutputLine);
194 int pclose_status = pclose (fp);
195 int status = WEXITSTATUS (pclose_status);
197 LOG_INFO(
"Exit code [%d] from command [%s]", status, command);
200 void netSrvMgrUtiles::triggerDhcpRenew(
const char* interface)
205 void netSrvMgrUtiles::triggerDhcpReleaseAndRenew(
const char* interface)
210 static bool getIPv6RouteInterface (
char *devname)
212 bool route_found =
false;
213 FILE *fp = fopen (
"/proc/net/ipv6_route",
"r");
216 char dst[50], gw[50];
218 while ((ret = fscanf (fp,
"%s %*x %*s %*x %s %*x %*x %*x %*x %s", dst, gw, devname)) != EOF)
225 if ((strcmp (dst, gw) == 0) && (strcmp (devname,
"sit0") != 0) && (strcmp (devname,
"lo") != 0))
228 LOG_INFO(
"active interface v6 %s", devname);
238 static bool getIPv4RouteInterface (
char *devname)
240 bool route_found =
false;
241 FILE *fp = fopen (
"/proc/net/route",
"r");
244 char line[100], *ptr, *ctr, *sptr;
245 while (fgets (line,
sizeof (line), fp))
247 ptr = strtok_r (line,
" \t", &sptr);
248 ctr = strtok_r (NULL,
" \t", &sptr);
249 if (ptr != NULL && ctr != NULL && strcmp (ctr,
"00000000") == 0)
251 STRCPY_S(devname, INTERFACE_SIZE, ptr);
253 LOG_INFO(
"ctive interface v4 %s", devname);
266 if (getIPv6RouteInterface (devname) || getIPv4RouteInterface (devname))
277 LOG_ERR(
"Get Route interface failure");
282 LOG_ERR(
"Get Route interface type failure");
285 LOG_TRACE(
"Route interface type %s", devname);
293 GError *error = NULL;
294 gboolean result = FALSE;
295 gchar* devfilebuffer = NULL;
296 const gchar* deviceFile =
"//etc/device.properties";
299 LOG_ERR(
"device name is null");
301 else if (g_file_get_contents (deviceFile, &devfilebuffer, NULL, &error) ==
false)
303 LOG_ERR(
"No contents in device properties");
304 if (!error) g_error_free (error);
308 gchar **tokens = g_strsplit_set(devfilebuffer,
",='\n'", -1);
309 g_free (devfilebuffer);
310 guint toklength = g_strv_length(tokens);
312 LOG_INFO(
"Interface Name %s", deviceName);
313 for (loopvar=0; loopvar<toklength; loopvar++)
315 if (g_strrstr(g_strstrip(tokens[loopvar]),
"ETHERNET_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
317 if ((loopvar+1) < toklength )
319 STRCPY_S(deviceName, INTERFACE_SIZE,
"ETHERNET");
324 else if (g_strrstr(g_strstrip(tokens[loopvar]),
"MOCA_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
326 if ((loopvar+1) < toklength )
328 STRCPY_S(deviceName, INTERFACE_SIZE,
"MOCA");
333 else if (g_strrstr(g_strstrip(tokens[loopvar]),
"WIFI_INTERFACE") && ((g_strcmp0(g_strstrip(tokens[loopvar+1]),deviceName)) == 0))
335 if ((loopvar+1) < toklength )
337 STRCPY_S(deviceName, INTERFACE_SIZE,
"WIFI");
344 LOG_INFO(
"Network Type %s", deviceName);
352 struct ifaddrs *ifAddrStruct,*tmpAddrPtr;
353 bool firstInterface=
false;
355 char tempInterface[10];
356 getifaddrs(&ifAddrStruct);
357 tmpAddrPtr = ifAddrStruct;
358 GString *device = g_string_new(NULL);
362 if (tmpAddrPtr->ifa_addr && (strcmp(tmpAddrPtr->ifa_name,
"sit0") != 0) && (strcmp(tmpAddrPtr->ifa_name,
"lo") != 0))
364 g_stpcpy(tempInterface,tmpAddrPtr->ifa_name);
365 LOG_INFO(
"1 interface [%s].", tempInterface);
366 if((
readDevFile(tempInterface)) && (!g_strrstr(device->str,tempInterface)))
370 g_string_append_printf(device,
"%s",
",");
376 g_string_append_printf(device,
"%s",tempInterface);
377 LOG_INFO(
"interface [%s] interface List [%s]", tempInterface,device->str);
381 tmpAddrPtr = tmpAddrPtr->ifa_next;
383 freeifaddrs(ifAddrStruct);
384 STRCPY_S(devAllInterface, INTERFACE_LIST, device->str);
385 g_string_free(device,
TRUE);
395 tmp=localtime(&cTime);
398 LOG_ERR(
"Error getting local time ");
401 if(strftime(currTime,MAX_TIME_LENGTH,timeFormat,tmp) == 0)
403 LOG_ERR(
"strftime failed in getting time format format %s", timeFormat);
413 char buffer[INTERFACE_STATUS_FILE_PATH_BUFFER]={0};
417 LOG_INFO(
"Device doesnt support Ethernet !!!!");
420 sprintf(buffer,INTERFACE_STATUS_FILE_PATH,interfaceName);
421 file = fopen(buffer,
"r");
424 fscanf(file,
"%s",buffer);
425 if(NULL != strcasestr(buffer,ETHERNET_UP_STATUS))
427 LOG_INFO(
"TELEMETRY_NETWORK_MANAGER_ETHERNET_MODE");
432 LOG_INFO(
"TELEMETRY_NETWORK_MANAGER_WIFI_MODE");
442 bool loadKeyFile (
const char* filename, GKeyFile* keyFile)
448 g_key_file_load_from_file (keyFile, filename, G_KEY_FILE_NONE, &err);
451 LOG_ERR(
"error loading '%s' (error domain=%d code=%d message=%s)",
452 filename, err->domain, err->code, err->message);
462 bool writeKeyFile (
const char* filename, GKeyFile* keyFile)
467 gchar *contents = g_key_file_to_data (keyFile, NULL, NULL);
469 g_file_set_contents (filename, contents, -1, &err);
472 LOG_ERR(
"error writing '%s' (error domain=%d code=%d message=%s)",
473 filename, err->domain, err->code, err->message);
484 #ifdef ENABLE_NLMONITOR
485 char macAddress[MAC_ADDR_BUFF_LEN] = {
'\0'};
486 std::vector<std::string> ipAddr;
489 NetLinkIfc::get_instance()->getIpaddr(ifName,family,ipAddr);
492 LOG_ERR(
"No ipaddress on interface %s", ifName);
499 std::string macAddrStr(macAddress);
501 for (
auto const& s : ipAddr)
510 LOG_INFO(
"skipping ip addr %s since it is based on mac %s", s.c_str(), macAddrStr.c_str());
513 std::string tempStr = s.substr(0, s.find(
"/", 0));
521 LOG_INFO(
"skipping reserved ip %s", tempStr.c_str());
524 STRCPY_S(interfaceIp, INET6_ADDRSTRLEN, tempStr.c_str());
530 LOG_ERR(
"ENABLE_NLMONITOR not set");
541 if (getSTBip_family(stbip,
"ipv6"))
546 else if (getSTBip_family(stbip,
"ipv4"))
553 bool netSrvMgrUtiles::getSTBip_family(
char *stbip,
const char *family)
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);
565 if (s_family ==
"ipv4")
569 else if (s_family ==
"ipv6")
575 LOG_ERR(
"Family [%s] Not recognized.", s_family.c_str());
584 LOG_ERR(
"No routable interface found.");
589 std::string ifcStr(interface);
590 NetLinkIfc::get_instance()->getIpaddr(ifcStr,i_family,ipAddr);
593 LOG_ERR(
"No ipaddress on interface %s, for Family %s", ifcStr.c_str(),s_family.c_str());
598 std::string macAddrStr(macAddress);
600 for (
auto const& s : ipAddr)
609 LOG_INFO(
"skipping ip addr %s since it is based on mac %s", s.c_str(), macAddrStr.c_str());
612 std::string tempStr = s.substr(0, s.find(
"/", 0));
620 LOG_INFO(
"skipping reserved ip %s", tempStr.c_str());
623 STRCPY_S(stbip, MAX_IP_ADDRESS_LEN, tempStr.c_str());
628 LOG_ERR(
"ENABLE_NLMONITOR not set ");
639 struct sockaddr_in6 sa6;
640 struct sockaddr_in sa;
644 inet_pton(AF_INET, stbip, &(sa.sin_addr));
645 return IN_IS_ADDR_LINKLOCAL(sa.sin_addr.s_addr);
647 inet_pton(AF_INET6, stbip, &(sa6.sin6_addr));
648 return IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr);
651 LOG_ERR(
"interface family not supported %d ", family);
660 if (getenv(
"WIFI_INTERFACE") != NULL)
662 if (getenv(
"MOCA_INTERFACE") != NULL)
664 ifcStr=getenv(
"MOCA_INTERFACE");
667 ifcStr=getenv(
"WIFI_INTERFACE");
671 else if (getenv(
"MOCA_INTERFACE") != NULL)
673 ifcStr=getenv(
"MOCA_INTERFACE");
677 LOG_ERR(
"current interface not there ");
680 STRCPY_S(currentInterface, INTERFACE_SIZE, ifcStr.c_str());
691 if((ipv6Addr.empty()) && (macAddr.empty()))
693 LOG_ERR(
"ipv6 addr or mac addr is empty ");
696 else if (macAddr.size() < 12)
698 LOG_ERR(
"mac addr is less than 12");
701 std::string tmpMacAddr(macAddr);
702 std::transform(tmpMacAddr.begin(), tmpMacAddr.end(), tmpMacAddr.begin(), ::tolower);
703 tmpMacAddr.erase(std::remove(tmpMacAddr.begin(), tmpMacAddr.end(),
':'), tmpMacAddr.end());
704 std::string strTmpAdd2Mac=
"fffe";
705 tmpMacAddr.insert(6,strTmpAdd2Mac);
706 tmpMacAddr.replace(0,2,
"");
707 std::string tmpIpv6Str(ipv6Addr);
708 std::transform(tmpIpv6Str.begin(), tmpIpv6Str.end(), tmpIpv6Str.begin(), ::tolower);
709 tmpIpv6Str.erase(std::remove(tmpIpv6Str.begin(), tmpIpv6Str.end(),
':'), tmpIpv6Str.end());
710 if(tmpIpv6Str.find(tmpMacAddr) != std::string::npos)
712 LOG_INFO(
"mac %s based global v6 address %s ", macAddr.c_str(),ipv6Addr.c_str());
724 LOG_ERR(
"ipv6 addr is empty");
728 std::string tmpIpv6Str(ipv6Addr);
729 if ((tmpIpv6Str.rfind(
"fd", 0) != std::string::npos) || (tmpIpv6Str.rfind(
"fc", 0) != std::string::npos))
731 LOG_INFO(
"ULA v6 address %s ", ipv6Addr.c_str());
740 FILE *file = popen(command,
"r");
743 LOG_ERR(
"command failed %s", command);
746 if (fgets(output_buffer, output_buffer_size, file) != NULL)
752 LOG_ERR(
"Failed in getting output from command %s", command);
761 struct sockaddr_in *ipaddr;
764 char address[INET6_ADDRSTRLEN]={0};
766 sock = socket(AF_INET, SOCK_DGRAM, 0);
769 ifr.ifr_addr.sa_family = family;
771 strncpy(ifr.ifr_name, ifName_in, IFNAMSIZ-1);
774 if (!system(
"/lib/rdk/pni_controller.sh uses_virtual_interface"))
776 strcat(ifr.ifr_name,
":0");
780 if (ioctl(sock, SIOCGIFNETMASK, &ifr) != -1)
782 ipaddr = (
struct sockaddr_in *)&ifr.ifr_netmask;
783 if (inet_ntop(family, &ipaddr->sin_addr, address,
sizeof(address)) != NULL)
785 STRCPY_S(netMask_out, INET6_ADDRSTRLEN, address);
786 LOG_INFO(
"Netmask %s", netMask_out);
792 LOG_ERR(
"Failed in ioctl() with \'%s\'..", strerror (errno) );
798 LOG_ERR(
"Failed to create socket() with \'%d\' return as \'%s\'.", sock, strerror (errno) );
805 struct sockaddr_in sa;
806 inet_pton(AF_INET, ipv4_address.c_str(), &(sa.sin_addr));
810 in_addr_t network_address = sa.sin_addr.s_addr & htonl(0xffffff00);
812 return (network_address == htonl (0xc0000200) ||
813 network_address == htonl (0xc6336400) ||
814 network_address == htonl (0xcb007100));