39 #include <sys/sysinfo.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
44 #include <arpa/inet.h>
49 #include "hostIf_utils.h"
51 #include "safec_lib.h"
54 #include "secure_wrapper.h"
56 GMutex* hostIf_IPv4Address::m_mutex = NULL;
57 GHashTable *hostIf_IPv4Address::ifHash = NULL;
59 int hostIf_IPv4Address::getIPv4AddressAndMask (
int instance,
struct in_addr& in_address,
struct in_addr& in_mask)
65 if (getifaddrs (&ifa))
68 int current_instance = 0;
69 for (
struct ifaddrs *ifa_node = ifa; ifa_node; ifa_node = ifa_node->ifa_next)
71 if (ifa_node->ifa_addr->sa_family == AF_INET && !strcmp (ifa_node->ifa_name, nameOfInterface) && (++current_instance == instance))
73 in_address = ((
struct sockaddr_in *) ifa_node->ifa_addr)->sin_addr;
74 in_mask = ((
struct sockaddr_in *) ifa_node->ifa_netmask)->sin_addr;
82 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"[%s]: rc=%d, if=%s, instance=%d\n", __FUNCTION__, rc, nameOfInterface, instance);
88 int hostIf_IPv4Address::setIpOrMask(
int interfaceNo,
char *value,
const char* ipOrMask)
92 if (0 != strcmp (value,
""))
94 if (0 == strcasecmp (backupAddressingType,
"static"))
97 v_secure_system(
"ifconfig %s %s %s", nameOfInterface, ipOrMask, value);
99 char cmd[BUFF_LENGTH] = { 0 };
100 sprintf (cmd,
"ifconfig %s %s %s", nameOfInterface, ipOrMask, value);
103 RDK_LOG (RDK_LOG_INFO, LOG_TR69HOSTIF,
"[%s(),%d] IPv4Address: IPAddress/SubnetMask is set \n", __FUNCTION__, __LINE__);
109 void hostIf_IPv4Address::refreshInterfaceName ()
111 nameOfInterface[0] = 0;
113 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: error getting interface name for Device.IP.Interface.%d\n", __FUNCTION__, dev_id);
129 bCalledSubnetMask(0),
130 bCalledAddressingType(0),
134 backupStatus[0]=
'\0';
135 backupIPAddress[0]=
'\0';
136 backupSubnetMask[0]=
'\0';
137 rc=strcpy_s (backupAddressingType,
sizeof(backupAddressingType),
"Static");
156 ifHash = g_hash_table_new(NULL,NULL);
165 RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,
"Caught exception, not able create MoCA Interface instance..\n");
167 g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
172 pRet->refreshInterfaceName ();
177 GList* hostIf_IPv4Address::getAllInstances()
182 return g_hash_table_get_keys(ifHash);
192 g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
197 void hostIf_IPv4Address::closeAllInstances()
203 GList* tmp_list = g_hash_table_get_values (ifHash);
208 tmp_list = tmp_list->next;
214 void hostIf_IPv4Address::getLock()
218 m_mutex = g_mutex_new();
220 g_mutex_lock(m_mutex);
223 void hostIf_IPv4Address::releaseLock()
225 g_mutex_unlock(m_mutex);
228 int hostIf_IPv4Address::handleGetMsg (
const char* pSubSetting,
int subInstanceNumber,
HOSTIF_MsgData_t* stMsgData)
232 int ret = NOT_HANDLED;
234 if (!strcasecmp (pSubSetting,
"Enable"))
238 else if (!strcasecmp (pSubSetting,
"Status"))
242 else if (!strcasecmp (pSubSetting,
"Alias"))
246 else if (!strcasecmp (pSubSetting,
"SubnetMask"))
250 else if (!strcasecmp (pSubSetting,
"AddressingType"))
254 else if (!strcasecmp (pSubSetting,
"IPAddress"))
260 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] Device.IPv4: Parameter \'%s\' is Not Supported \n", __FUNCTION__, __LINE__, stMsgData->
paramName);
261 stMsgData->
faultCode = fcInvalidParameterName;
267 int hostIf_IPv4Address::handleSetMsg (
const char* pSubSetting,
int subInstanceNumber,
HOSTIF_MsgData_t* stMsgData)
271 int ret = NOT_HANDLED;
273 if (!strcasecmp (pSubSetting,
"Enable"))
277 else if (!strcasecmp (pSubSetting,
"SubnetMask"))
281 else if (!strcasecmp (pSubSetting,
"IPAddress"))
287 stMsgData->
faultCode = fcInvalidParameterName;
317 struct in_addr in_address;
318 struct in_addr in_mask;
319 bool enable = (OK == getIPv4AddressAndMask (subInstanceNo, in_address, in_mask));
321 if (bCalledEnable && pChanged && (backupEnable != enable))
325 bCalledEnable =
true;
326 backupEnable = enable;
328 stMsgData->
paramtype = hostIf_BooleanType;
358 char status[BUFF_LENGTH_16];
359 struct in_addr in_address;
360 struct in_addr in_mask;
362 if(OK == getIPv4AddressAndMask (subInstanceNo, in_address, in_mask))
364 rc=strcpy_s (status,
sizeof(status) ,
"Enabled");
372 rc=strcpy_s (status,
sizeof(status) ,
"Disabled");
379 if (bCalledStatus && pChanged && strncmp (status, backupStatus, BUFF_LENGTH_16))
383 bCalledStatus =
true;
384 strncpy (backupStatus, status, BUFF_LENGTH_16 -1);
385 strncpy (stMsgData->
paramValue, status, BUFF_LENGTH_16);
386 stMsgData->
paramtype = hostIf_StringType;
387 stMsgData->
paramLen = strlen (status);
427 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: Not Implemented\n", __FUNCTION__);
450 struct in_addr in_address;
451 struct in_addr in_mask;
452 if (OK != getIPv4AddressAndMask (subInstanceNo, in_address, in_mask))
455 char ipv4Address[BUFF_LENGTH_64] = {0};
456 inet_ntop (AF_INET, &in_address, ipv4Address, BUFF_LENGTH_64);
457 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"[%s]: ipv4Address = %s\n", __FUNCTION__, ipv4Address);
459 if (bCalledIPAddress && pChanged && strncmp (ipv4Address, backupIPAddress, BUFF_LENGTH_64))
463 bCalledIPAddress =
true;
464 strncpy (backupIPAddress, ipv4Address, BUFF_LENGTH_64);
465 strncpy (stMsgData->
paramValue, ipv4Address, BUFF_LENGTH_64);
466 stMsgData->
paramtype = hostIf_StringType;
467 stMsgData->
paramLen = strlen (ipv4Address);
491 struct in_addr in_address;
492 struct in_addr in_mask;
493 if (OK != getIPv4AddressAndMask (subInstanceNo, in_address, in_mask))
496 char subnetMask[BUFF_LENGTH_64] = {0};
497 inet_ntop (AF_INET, &in_mask, subnetMask, BUFF_LENGTH_64);
498 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"[%s]: subnetMask = %s\n", __FUNCTION__, subnetMask);
500 if (bCalledSubnetMask && pChanged && strncmp (subnetMask, backupSubnetMask, BUFF_LENGTH_64))
504 bCalledSubnetMask =
true;
505 strncpy (backupSubnetMask, subnetMask, BUFF_LENGTH_64);
506 strncpy (stMsgData->
paramValue, subnetMask, BUFF_LENGTH_64);
507 stMsgData->
paramtype = hostIf_StringType;
508 stMsgData->
paramLen = strlen (subnetMask);
527 char ipv4Address[BUFF_LENGTH_64];
528 inet_ntop (AF_INET, &in_address, ipv4Address, BUFF_LENGTH_64);
530 bool isAutoIp = (strncmp (ipv4Address,
"169.254.", 8) == 0);
532 RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF,
"[%s]: ipv4Address = %s, isAutoIp = %d\n", __FUNCTION__, ipv4Address, isAutoIp);
572 struct in_addr in_address;
573 struct in_addr in_mask;
575 if (OK != getIPv4AddressAndMask (subInstanceNo, in_address, in_mask))
578 char ipv4Address[BUFF_LENGTH_64];
579 inet_ntop (AF_INET, &in_address, ipv4Address, BUFF_LENGTH_64);
581 char addressingType[BUFF_LENGTH_16];
582 if (hostIf_IPInterface::isLoopback (nameOfInterface))
584 rc=strcpy_s (addressingType,
sizeof(addressingType),
"Static");
592 rc=strcpy_s (addressingType,
sizeof(addressingType),
"AutoIP");
603 else if (((hasPhysicalInterfaceAs (
getenvOrDefault (
"MOCA_INTERFACE",
""))) &&
604 (0 == strcmp (ipv4Address,
getenvOrDefault (
"DEFAULT_MOCA_IFACE_IP",
"")))) ||
606 (0 == strcmp (ipv4Address,
getenvOrDefault (
"DEFAULT_WIFI_IFACE_IP",
"")))))
608 rc=strcpy_s (addressingType,
sizeof(addressingType),
"Static");
616 rc=strcpy_s (addressingType,
sizeof(addressingType),
"DHCP");
623 if (bCalledAddressingType && pChanged && strncmp (addressingType, backupAddressingType, BUFF_LENGTH_16))
627 bCalledAddressingType =
true;
628 strncpy (backupAddressingType, addressingType, BUFF_LENGTH_16);
629 strncpy (stMsgData->
paramValue, addressingType, BUFF_LENGTH_16);
630 stMsgData->
paramtype = hostIf_StringType;
631 stMsgData->
paramLen = strlen (addressingType);
636 bool hostIf_IPv4Address::hasPhysicalInterfaceAs (
char* phy_if_name)
638 int l = strlen (phy_if_name);
639 return strncmp (nameOfInterface, phy_if_name, l) == 0 && (nameOfInterface[l] == 0 || nameOfInterface[l] ==
':');
662 char cmd[BUFF_LENGTH] = { 0 };
663 if(FALSE == get_boolean(stMsgData->
paramValue))
666 v_secure_system(
"ifconfig %s 0.0.0.0", nameOfInterface);
668 sprintf(cmd,
"ifconfig %s 0.0.0.0", nameOfInterface);
671 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s(),%d] IPv4Address Disabled \n",__FUNCTION__,__LINE__);
677 v_secure_system(
"ifdown %s && ifup %s", nameOfInterface, nameOfInterface);
679 sprintf(cmd,
"ifdown %s && ifup %s", nameOfInterface, nameOfInterface);
682 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s(),%d] IPv4Address Enabled \n",__FUNCTION__,__LINE__);
715 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: Not Implemented\n", __FUNCTION__);
737 setIpOrMask(dev_id, stMsgData->
paramValue ,
"inet");
760 setIpOrMask(dev_id, stMsgData->
paramValue ,
"netmask");