40 #include <arpa/inet.h>
43 #define _LENGTH_PARAMETER 64
44 #define _LENGTH_IPADDR 16
45 #define _LENGTH_STATUS 12
46 #define _HEX_STATE_LEN 3
49 GHashTable *hostIf_IPActivePort::ifHash = NULL;
51 GMutex *hostIf_IPActivePort::m_mutex = NULL;
55 int hostIf_IPActivePort::getActivePortsFields(
unsigned int activePortNo)
58 char resultBuff[_BUF_LEN]= {
'\0'};
59 const char *cmd =
"cat /proc/net/tcp | awk '$4 == \"0A\" || $4 == \"01\" {print $2\" \"$3\" \"$4}'";
60 char local_address[_LENGTH_IPADDR]= {
'\0'}, rem_address[_LENGTH_IPADDR]= {
'\0'}, state[_HEX_STATE_LEN] = {
'\0'};
61 int activePortCount = 1, ret = -1;
62 unsigned long ipAddr = 0;
67 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error popen");
72 while(fgets(resultBuff, _BUF_LEN, fp) != NULL)
74 if(activePortNo == activePortCount) {
75 sscanf(resultBuff,
"%s %s %s", local_address, rem_address, state);
84 memset(activePort.localIpAddress,
'\0', _LENGTH_IPADDR);
85 memset(activePort.remoteIpAddress,
'\0', _LENGTH_IPADDR);
86 memset(activePort.status,
'\0', _LENGTH_STATUS);
87 activePort.localPort = 0;
88 activePort.remotePort = 0;
96 if (strtoul(state, NULL, 16) == 10)
97 strncpy(activePort.status,
"LISTEN", strlen(
"LISTEN"));
98 else if (strtoul(state, NULL, 16) == 1)
99 strncpy(activePort.status,
"ESTABLISHED", strlen(
"ESTABLISHED"));
102 addrPtr = local_address;
104 ipAddr = strtoul(strsep(&addrPtr,
":"), NULL, 16);
105 inet_ntop(AF_INET, &ipAddr, activePort.localIpAddress, INET_ADDRSTRLEN);
106 activePort.localPort = strtoul(addrPtr, NULL, 16);
109 addrPtr = rem_address;
111 ipAddr = strtoul(strsep(&addrPtr,
":"), NULL, 16);
112 inet_ntop(AF_INET, &ipAddr, activePort.remoteIpAddress, INET_ADDRSTRLEN);
113 activePort.remotePort = strtoul(addrPtr, NULL, 16);
115 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"ActivePort: %d local addr: %s:%d remote addr: %s:%d Status: %s\n", activePortNo, activePort.localIpAddress,
116 activePort.localPort, activePort.remoteIpAddress, activePort.remotePort, activePort.status);
131 bCalledLocalIPAddress(false),
132 bCalledLocalPort(false),
133 bCalledRemoteIPAddress(false),
134 bCalledRemotePort(false),
135 bCalledStatus(false),
139 backupLocalIPAddress[0]=
'\0';
140 backupRemoteIPAddress[0]=
'\0';
141 backupStatus[0]=
'\0';
155 ifHash = g_hash_table_new(NULL,NULL);
164 RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,
"Caught exception, not able create MoCA Interface instance..\n");
166 g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
171 GList* hostIf_IPActivePort::getAllInstances()
174 return g_hash_table_get_keys(ifHash);
182 g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
187 void hostIf_IPActivePort::closeAllInstances()
191 GList* tmp_list_original = g_hash_table_get_values (ifHash);
192 GList* tmp_list = tmp_list_original;
197 tmp_list = tmp_list->next;
200 g_list_free(tmp_list_original);
204 void hostIf_IPActivePort::getLock()
208 m_mutex = g_mutex_new();
210 g_mutex_lock(m_mutex);
213 void hostIf_IPActivePort::releaseLock()
215 g_mutex_unlock(m_mutex);
218 int hostIf_IPActivePort::handleGetMsg (
const char* pSetting,
HOSTIF_MsgData_t* stMsgData)
220 int ret = NOT_HANDLED;
222 if (!strcasecmp (pSetting,
"LocalIPAddress"))
226 else if (!strcasecmp (pSetting,
"LocalPort"))
230 else if (!strcasecmp (pSetting,
"RemoteIPAddress"))
234 else if (!strcasecmp (pSetting,
"RemotePort"))
238 else if (!strcasecmp (pSetting,
"Status"))
269 getActivePortsFields(dev_id);
271 if(bCalledLocalIPAddress && pChanged && strncmp(activePort.localIpAddress,backupLocalIPAddress,_LENGTH_IPADDR ))
275 bCalledLocalIPAddress =
true;
276 strncpy(backupLocalIPAddress,activePort.localIpAddress,
sizeof(backupLocalIPAddress) -1);
277 backupLocalIPAddress[
sizeof(backupLocalIPAddress) -1] =
'\0';
280 stMsgData->
paramtype = hostIf_StringType;
281 stMsgData->
paramLen = strlen( activePort.localIpAddress);
302 getActivePortsFields(dev_id);
303 if(bCalledLocalPort && pChanged && (backupLocalPort != activePort.localPort))
307 bCalledLocalPort =
true;
308 backupLocalPort = activePort.localPort;
310 stMsgData->
paramtype = hostIf_UnsignedIntType;
335 getActivePortsFields(dev_id);
336 if(bCalledRemoteIPAddress && pChanged && strncmp(activePort.remoteIpAddress,backupRemoteIPAddress,_LENGTH_IPADDR ))
340 bCalledRemoteIPAddress =
true;
341 strncpy(backupRemoteIPAddress,activePort.remoteIpAddress,
sizeof(backupRemoteIPAddress) -1);
342 backupRemoteIPAddress[
sizeof(backupRemoteIPAddress) -1] =
'\0';
345 stMsgData->
paramtype = hostIf_StringType;
346 stMsgData->
paramLen = strlen( activePort.remoteIpAddress);
369 getActivePortsFields(dev_id);
370 if(bCalledRemotePort && pChanged && (backupRemotePort != activePort.remotePort))
374 bCalledRemotePort =
true;
375 backupRemotePort = activePort.remotePort;
377 stMsgData->
paramtype = hostIf_UnsignedIntType;
398 getActivePortsFields(dev_id);
399 if(bCalledStatus && pChanged && strncmp(activePort.status,backupStatus,_LENGTH_IPADDR ))
403 bCalledStatus =
true;
404 strncpy(backupStatus,activePort.status,_LENGTH_STATUS-1);
405 strncpy(stMsgData->
paramValue,activePort.status,_LENGTH_STATUS-1 );
406 stMsgData->
paramtype = hostIf_StringType;
407 stMsgData->
paramLen = strlen( activePort.status);