38 #include "hostIf_utils.h"
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
48 #include <netinet/in.h>
52 #include "safec_lib.h"
55 IPInterface hostIf_IPInterface::stIPInterfaceInstance = {FALSE,FALSE,FALSE,FALSE,{
"Down"},{
'\0'},{
'\0'},0,{
'\0'},{
'\0'},FALSE,0,{
"Normal"},FALSE,0,
61 GHashTable *hostIf_IPInterface::ifHash = NULL;
63 int hostIf_IPInterface::set_Interface_Enable (
int value)
67 char cmd[BUFF_LENGTH] = { 0 };
70 sprintf (cmd,
"ifconfig %s down", nameOfInterface);
73 else if (
TRUE == value)
75 sprintf (cmd,
"ifconfig %s up", nameOfInterface);
79 return (system (cmd) < 0) ? NOK : OK;
82 int hostIf_IPInterface::set_Interface_Reset (
unsigned int value)
88 char cmd[BUFF_LENGTH] = { 0 };
90 sprintf (cmd,
"ifdown %s", nameOfInterface);
94 sprintf (cmd,
"ifup %s", nameOfInterface);
98 RDK_LOG (RDK_LOG_INFO, LOG_TR69HOSTIF,
"%s(): interfaceName = %s Reset Done\n", __FUNCTION__, nameOfInterface);
104 int hostIf_IPInterface::set_Interface_Mtu (
unsigned int value)
108 char cmd[BUFF_LENGTH] = { 0 };
109 sprintf (cmd,
"ifconfig %s mtu %d", nameOfInterface, value);
110 if (system (cmd) < 0)
118 void hostIf_IPInterface::refreshInterfaceName ()
120 nameOfInterface[0] = 0;
122 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: error getting interface name for Device.IP.Interface.%d\n", __FUNCTION__, dev_id);
134 bCalledEnable(false),
135 bCalledIPv4Enable(false),
136 bCalledIPv6Enable(false),
137 bCalledStatus(false),
140 bCalledMaxMTUSize(false),
142 bCalledLoopback(false),
143 bCalledIPv4AddressNumberOfEntries(false),
144 backupLoopback(false),
146 backupIPv4Enable(false),
147 backupIPv6Enable(false),
149 backupIPv4AddressNumberOfEntries(0),
155 backupStatus[0]=
'\0';
170 ifHash = g_hash_table_new(NULL,NULL);
179 RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,
"Caught exception, not able create MoCA Interface instance..\n");
181 g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
186 pRet->refreshInterfaceName ();
191 GList* hostIf_IPInterface::getAllInstances()
196 return g_hash_table_get_keys(ifHash);
206 g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
211 void hostIf_IPInterface::closeAllInstances()
217 GList* tmp_list = g_hash_table_get_values (ifHash);
222 tmp_list = tmp_list->next;
228 int hostIf_IPInterface::handleGetMsg (
const char* pSetting,
HOSTIF_MsgData_t* stMsgData)
232 int ret = NOT_HANDLED;
234 if (!strcasecmp (pSetting,
"Enable"))
238 else if (!strcasecmp (pSetting,
"IPv4Enable"))
242 else if (!strcasecmp (pSetting,
"IPv6Enable"))
244 ret = get_Interface_IPv6Enable (stMsgData);
246 else if (!strcasecmp (pSetting,
"ULAEnable"))
250 else if (!strcasecmp (pSetting,
"Status"))
254 else if (!strcasecmp (pSetting,
"Alias"))
258 else if (!strcasecmp (pSetting,
"Name"))
262 else if (!strcasecmp (pSetting,
"LastChange"))
266 else if (!strcasecmp (pSetting,
"LowerLayers"))
270 else if (!strcasecmp (pSetting,
"Router"))
274 else if (!strcasecmp (pSetting,
"Reset"))
278 else if (!strcasecmp (pSetting,
"MaxMTUSize"))
282 else if (!strcasecmp (pSetting,
"Type"))
286 else if (!strcasecmp (pSetting,
"Loopback"))
290 else if (!strcasecmp (pSetting,
"IPv4AddressNumberOfEntries"))
295 else if (!strcasecmp (pSetting,
"IPv6AddressNumberOfEntries"))
297 ret = get_Interface_IPv6AddressNumberOfEntries (stMsgData);
299 else if (!strcasecmp (pSetting,
"IPv6PrefixNumberOfEntries"))
301 ret = get_Interface_IPv6PrefixNumberOfEntries (stMsgData);
303 #endif // IPV6_SUPPORT
304 else if (!strcasecmp (pSetting,
"AutoIPEnable"))
310 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] IP : Parameter \'%s\' is Not Supported \n", __FUNCTION__, __LINE__, stMsgData->
paramName);
311 stMsgData->
faultCode = fcInvalidParameterName;
318 int hostIf_IPInterface::handleSetMsg (
const char* pSetting,
HOSTIF_MsgData_t* stMsgData)
322 int ret = NOT_HANDLED;
324 if (!strcasecmp (pSetting,
"Enable"))
326 ret = set_Interface_Enable (stMsgData);
328 else if (!strcasecmp (pSetting,
"IPv4Enable"))
332 else if (!strcasecmp (pSetting,
"Reset"))
334 ret = set_Interface_Reset (stMsgData);
336 else if (!strcasecmp (pSetting,
"MaxMTUSize"))
342 stMsgData->
faultCode = fcInvalidParameterName;
370 stIPInterfaceInstance.enable = isEnabled (nameOfInterface);
372 if(bCalledEnable && pChanged && (backupEnable != stIPInterfaceInstance.enable))
376 bCalledEnable =
true;
377 backupEnable = stIPInterfaceInstance.enable;
379 stMsgData->
paramtype = hostIf_BooleanType;
432 if(stIPInterfaceInstance.enable && (stIPInterfaceInstance.iPv4Enable || stIPInterfaceInstance.iPv6Enable))
434 rc=strcpy_s (stIPInterfaceInstance.status,
sizeof(stIPInterfaceInstance.status),STATE_UP);
438 rc=strcpy_s (stIPInterfaceInstance.status,
sizeof(stIPInterfaceInstance.status),STATE_DOWN);
448 if(bCalledIPv4Enable && pChanged && (backupIPv4Enable != stIPInterfaceInstance.iPv4Enable))
452 bCalledIPv4Enable =
true;
453 backupIPv4Enable = stIPInterfaceInstance.iPv4Enable;
455 stMsgData->
paramtype = hostIf_BooleanType;
461 int hostIf_IPInterface::get_Interface_IPv6Enable(
HOSTIF_MsgData_t *stMsgData,
bool *pChanged)
470 if(stIPInterfaceInstance.enable && (stIPInterfaceInstance.iPv4Enable || stIPInterfaceInstance.iPv6Enable))
472 rc=strcpy_s (stIPInterfaceInstance.status,
sizeof(stIPInterfaceInstance.status),STATE_UP);
476 rc=strcpy_s (stIPInterfaceInstance.status,
sizeof(stIPInterfaceInstance.status),STATE_DOWN);
487 if(bCalledIPv6Enable && pChanged && (backupIPv6Enable != stIPInterfaceInstance.iPv6Enable))
491 bCalledIPv6Enable =
true;
492 backupIPv6Enable = stIPInterfaceInstance.iPv6Enable;
494 stMsgData->
paramtype = hostIf_BooleanType;
516 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
521 void hostIf_IPInterface::getInterfaceOperationalState (
char* operationalState)
527 getActiveFlags (nameOfInterface, ifr);
529 if(ifr.ifr_flags & IFF_UP)
531 rc=strcpy_s (operationalState, IP_STATUS_LENGTH,STATE_UP);
535 rc=strcpy_s (operationalState, IP_STATUS_LENGTH,STATE_DOWN);
580 getInterfaceOperationalState (stIPInterfaceInstance.status);
582 if(bCalledStatus && pChanged && strncmp(stIPInterfaceInstance.status, backupStatus,_BUF_LEN_16 ))
586 bCalledStatus =
true;
587 strncpy(stMsgData->
paramValue,stIPInterfaceInstance.status,TR69HOSTIFMGR_MAX_PARAM_LEN );
588 strncpy(backupStatus,stIPInterfaceInstance.status,
sizeof(backupStatus) -1);
589 backupStatus[
sizeof(backupStatus) -1] =
'\0';
590 stMsgData->
paramtype = hostIf_StringType;
591 stMsgData->
paramLen = strlen(stIPInterfaceInstance.status);
643 rc=strcpy_s (stIPInterfaceInstance.name,
sizeof(stIPInterfaceInstance.name), nameOfInterface);
649 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Interface name = %s \n", __FUNCTION__, stIPInterfaceInstance.name);
651 if(bCalledName && pChanged && strncmp(stIPInterfaceInstance.name, backupName,_BUF_LEN_16 ))
656 strncpy(stMsgData->
paramValue,stIPInterfaceInstance.name,TR69HOSTIFMGR_MAX_PARAM_LEN-1 );
657 stMsgData->
paramValue[TR69HOSTIFMGR_MAX_PARAM_LEN-1] =
'\0';
658 strncpy(backupName,stIPInterfaceInstance.name,_BUF_LEN_16-1 );
659 backupName[_BUF_LEN_16-1] =
'\0';
660 stMsgData->
paramtype = hostIf_StringType;
661 stMsgData->
paramLen = strlen(stIPInterfaceInstance.name);
682 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
710 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
738 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
764 stIPInterfaceInstance.reset = FALSE;
766 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Reset: %d\n", __FUNCTION__, stIPInterfaceInstance.reset);
768 if(bCalledReset && pChanged && (backupReset != stIPInterfaceInstance.reset))
773 backupReset = stIPInterfaceInstance.reset;
775 stMsgData->
paramtype = hostIf_BooleanType;
799 stIPInterfaceInstance.maxMTUSize = getMTU (nameOfInterface);
801 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): MaxMTUSize: %u \n", __FUNCTION__, stIPInterfaceInstance.maxMTUSize);
803 if(bCalledMaxMTUSize && pChanged && (backupMaxMTUSize != stIPInterfaceInstance.maxMTUSize))
807 bCalledMaxMTUSize =
true;
808 backupMaxMTUSize = stIPInterfaceInstance.maxMTUSize;
810 stMsgData->
paramtype = hostIf_UnsignedIntType;
836 if (isLoopback (nameOfInterface))
838 rc=strcpy_s (stIPInterfaceInstance.type,
sizeof(stIPInterfaceInstance.type),
"Loopback");
844 else if (0 == strncmp (nameOfInterface,
"eth", 3) || 0 == strncmp (nameOfInterface,
"wlan", 4))
846 rc=strcpy_s (stIPInterfaceInstance.type,
sizeof(stIPInterfaceInstance.type),
"Normal");
854 rc=strcpy_s (stIPInterfaceInstance.type,
sizeof(stIPInterfaceInstance.type),
"Tunneled");
861 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Type: %s \n", __FUNCTION__, stIPInterfaceInstance.type);
863 if(bCalledType && pChanged && strncmp(stIPInterfaceInstance.type, backupType,_BUF_LEN_16 ))
868 strncpy(stMsgData->
paramValue,stIPInterfaceInstance.type,TR69HOSTIFMGR_MAX_PARAM_LEN );
869 strncpy(backupType,stIPInterfaceInstance.type,_BUF_LEN_16 );
870 stMsgData->
paramtype = hostIf_StringType;
871 stMsgData->
paramLen = strlen(stIPInterfaceInstance.type);
876 void hostIf_IPInterface::getActiveFlags (
char* nameOfInterface,
struct ifreq& ifr)
878 memset (&ifr, 0,
sizeof(ifr));
880 rc=strcpy_s (ifr.ifr_name,
sizeof(ifr.ifr_name), nameOfInterface);
885 int fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_IP);
887 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): fd: %d, Failed due to [\'%s\' (%d)] \n", __FUNCTION__, fd, strerror(errno), errno);
890 ioctl (fd, SIOCGIFFLAGS, &ifr);
894 bool hostIf_IPInterface::isLoopback (
char* nameOfInterface)
899 getActiveFlags (nameOfInterface, ifr);
900 return ifr.ifr_flags & IFF_LOOPBACK;
903 bool hostIf_IPInterface::isEnabled (
char* nameOfInterface)
908 getActiveFlags (nameOfInterface, ifr);
909 return ifr.ifr_flags & IFF_UP;
912 int hostIf_IPInterface::getMTU (
char* nameOfInterface)
917 memset (&ifr, 0,
sizeof(ifr));
919 rc=strcpy_s (ifr.ifr_name,
sizeof(ifr.ifr_name), nameOfInterface);
924 int fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_IP);
927 ioctl (fd, SIOCGIFMTU, &ifr);
932 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): fd: %d, Failed due to [\'%s\' (%d)] \n", __FUNCTION__,fd, strerror(errno), errno);
955 stIPInterfaceInstance.loopback = isLoopback (nameOfInterface);
957 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Loopback: %d \n", __FUNCTION__, stIPInterfaceInstance.loopback);
959 if(bCalledLoopback && pChanged && (backupLoopback != stIPInterfaceInstance.loopback))
963 bCalledLoopback =
true;
964 backupLoopback = stIPInterfaceInstance.loopback;
966 stMsgData->
paramtype = hostIf_BooleanType;
972 unsigned int hostIf_IPInterface::getIPAddressNumberOfEntries (sa_family_t address_family)
977 if (getifaddrs (&ifa) == -1) {
978 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s() Failed in getifaddrs(), errno(%d) [%s].\n", __FUNCTION__, errno, strerror( errno) );
982 int ipAddressNumberOfEntries = 0;
983 for (
struct ifaddrs *ifa_node = ifa; ifa_node; ifa_node = ifa_node->ifa_next)
984 if (ifa_node->ifa_addr->sa_family == address_family && !strcmp (ifa_node->ifa_name, nameOfInterface))
985 ipAddressNumberOfEntries++;
989 return ipAddressNumberOfEntries;
1000 return getIPAddressNumberOfEntries (AF_INET);
1022 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Interface %d (%s) has [%u] IPv4 Addresses\n", __FUNCTION__, dev_id, nameOfInterface,
1023 stIPInterfaceInstance.iPv4AddressNumberOfEntries);
1025 put_int(stMsgData->
paramValue,stIPInterfaceInstance.iPv4AddressNumberOfEntries);
1026 stMsgData->
paramtype = hostIf_UnsignedIntType;
1041 return getIPAddressNumberOfEntries (AF_INET6);
1044 unsigned int hostIf_IPInterface::getIPv6PrefixNumberOfEntries ()
1046 return getIPAddressNumberOfEntries (AF_INET6);
1053 int hostIf_IPInterface::get_Interface_IPv6AddressNumberOfEntries(
HOSTIF_MsgData_t *stMsgData)
1059 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Interface %d (%s) has [%u] IPv6 Addresses\n", __FUNCTION__, dev_id, nameOfInterface,
1060 stIPInterfaceInstance.iPv6AddressNumberOfEntries);
1062 put_int(stMsgData->
paramValue,stIPInterfaceInstance.iPv6AddressNumberOfEntries);
1063 stMsgData->
paramtype = hostIf_UnsignedIntType;
1064 stMsgData->
paramLen =
sizeof(
unsigned int);
1069 int hostIf_IPInterface::get_Interface_IPv6PrefixNumberOfEntries(
HOSTIF_MsgData_t *stMsgData)
1073 stIPInterfaceInstance.iPv6PrefixNumberOfEntries = getIPv6PrefixNumberOfEntries ();
1075 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"%s(): Interface %d (%s) has [%u] IPv6 Prefixes\n", __FUNCTION__, dev_id, nameOfInterface,
1076 stIPInterfaceInstance.iPv6PrefixNumberOfEntries);
1079 stMsgData->
paramtype = hostIf_UnsignedIntType;
1080 stMsgData->
paramLen =
sizeof(
unsigned int);
1085 #endif // IPV6_SUPPORT
1113 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
1154 set_Interface_Enable (get_boolean(stMsgData->
paramValue));
1189 set_Interface_Enable (get_boolean (stMsgData->
paramValue));
1209 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
1263 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
1289 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
1324 set_Interface_Reset (get_boolean (stMsgData->
paramValue));
1347 set_Interface_Mtu (get_int (stMsgData->
paramValue));
1373 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);
1401 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s(): Parameter Not Supported \n",__FUNCTION__);