RDK Documentation (Open Sourced RDK Components)
Device_IP_ActivePort.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  * @file Device_IP_ActivePort.cpp
22  * @brief This source file contains the APIs of device IP active ports.
23  */
24 
25 /*****************************************************************************
26  * STANDARD INCLUDE FILES
27  *****************************************************************************/
28 
29 
30 
31 /**
32 * @defgroup tr69hostif
33 * @{
34 * @defgroup hostif
35 * @{
36 **/
37 
38 
39 #include <net/if.h>
40 #include <arpa/inet.h>
41 #include "Device_IP_ActivePort.h"
42 
43 #define _LENGTH_PARAMETER 64
44 #define _LENGTH_IPADDR 16
45 #define _LENGTH_STATUS 12
46 #define _HEX_STATE_LEN 3
47 #define _BUF_LEN 1024
48 
49 GHashTable *hostIf_IPActivePort::ifHash = NULL;
50 
51 GMutex *hostIf_IPActivePort::m_mutex = NULL;
52 
53 struct Device_IP_ActivePort hostIf_IPActivePort::activePort = {0,};
54 
55 int hostIf_IPActivePort::getActivePortsFields(unsigned int activePortNo)
56 {
57  FILE *fp = NULL;
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;
63  char *addrPtr = NULL;
64 
65  fp = popen(cmd,"r");
66  if(fp == NULL) {
67  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"Error popen");
68  return 0;
69  }
70 
71  // local_ip:port remote_ip:port st
72  while(fgets(resultBuff, _BUF_LEN, fp) != NULL)
73  {
74  if(activePortNo == activePortCount) {
75  sscanf(resultBuff,"%s %s %s", local_address, rem_address, state);
76  ret = OK;
77  break;
78  }
79  activePortCount++;
80  }
81 
82  pclose(fp);
83 
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;
89 
90  if(ret != OK)
91  return ret;
92 
93  // STATUS
94  /* st = 0A = LISTEN
95  st = 01 = ESTABLISHED */
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"));
100 
101  // LOCAL IP ADDRESS and LOCAL PORT
102  addrPtr = local_address;
103 
104  ipAddr = strtoul(strsep(&addrPtr, ":"), NULL, 16);
105  inet_ntop(AF_INET, &ipAddr, activePort.localIpAddress, INET_ADDRSTRLEN);
106  activePort.localPort = strtoul(addrPtr, NULL, 16);
107 
108  // REMOTE IP ADDRESS and REMOTE PORT
109  addrPtr = rem_address;
110 
111  ipAddr = strtoul(strsep(&addrPtr, ":"), NULL, 16);
112  inet_ntop(AF_INET, &ipAddr, activePort.remoteIpAddress, INET_ADDRSTRLEN);
113  activePort.remotePort = strtoul(addrPtr, NULL, 16);
114 
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);
117 
118  return ret;
119 }
120 
121 /**
122  * @brief Class Constructor of the class hostIf_IPActivePort.
123  *
124  * It will initialize the device id. And it will initialize the local IP address , remote IP address to
125  * empty string.
126  *
127  * @param[in] dev_id Device identification number.
128  */
130  dev_id(dev_id),
131  bCalledLocalIPAddress(false),
132  bCalledLocalPort(false),
133  bCalledRemoteIPAddress(false),
134  bCalledRemotePort(false),
135  bCalledStatus(false),
136  backupLocalPort(0),
137  backupRemotePort(0)
138 {
139  backupLocalIPAddress[0]='\0';
140  backupRemoteIPAddress[0]='\0';
141  backupStatus[0]='\0';
142 
143 }
144 
145 hostIf_IPActivePort* hostIf_IPActivePort::getInstance(int dev_id)
146 {
147  hostIf_IPActivePort* pRet = NULL;
148 
149  if(ifHash)
150  {
151  pRet = (hostIf_IPActivePort *)g_hash_table_lookup(ifHash,(gpointer) dev_id);
152  }
153  else
154  {
155  ifHash = g_hash_table_new(NULL,NULL);
156  }
157 
158  if(!pRet)
159  {
160  try {
161  pRet = new hostIf_IPActivePort(dev_id);
162  } catch(int e)
163  {
164  RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,"Caught exception, not able create MoCA Interface instance..\n");
165  }
166  g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
167  }
168  return pRet;
169 }
170 
171 GList* hostIf_IPActivePort::getAllInstances()
172 {
173  if(ifHash)
174  return g_hash_table_get_keys(ifHash);
175  return NULL;
176 }
177 
178 void hostIf_IPActivePort::closeInstance(hostIf_IPActivePort *pDev)
179 {
180  if(pDev)
181  {
182  g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
183  delete pDev;
184  }
185 }
186 
187 void hostIf_IPActivePort::closeAllInstances()
188 {
189  if(ifHash)
190  {
191  GList* tmp_list_original = g_hash_table_get_values (ifHash);
192  GList* tmp_list = tmp_list_original;
193 
194  while(tmp_list)
195  {
196  hostIf_IPActivePort* pDev = (hostIf_IPActivePort *)tmp_list->data;
197  tmp_list = tmp_list->next;
198  closeInstance(pDev);
199  }
200  g_list_free(tmp_list_original);
201  }
202 }
203 
204 void hostIf_IPActivePort::getLock()
205 {
206  if(!m_mutex)
207  {
208  m_mutex = g_mutex_new();
209  }
210  g_mutex_lock(m_mutex);
211 }
212 
213 void hostIf_IPActivePort::releaseLock()
214 {
215  g_mutex_unlock(m_mutex);
216 }
217 
218 int hostIf_IPActivePort::handleGetMsg (const char* pSetting, HOSTIF_MsgData_t* stMsgData)
219 {
220  int ret = NOT_HANDLED;
221 
222  if (!strcasecmp (pSetting, "LocalIPAddress"))
223  {
224  ret = get_Device_IP_ActivePort_LocalIPAddress (stMsgData);
225  }
226  else if (!strcasecmp (pSetting, "LocalPort"))
227  {
228  ret = get_Device_IP_ActivePort_LocalPort (stMsgData);
229  }
230  else if (!strcasecmp (pSetting, "RemoteIPAddress"))
231  {
233  }
234  else if (!strcasecmp (pSetting, "RemotePort"))
235  {
236  ret = get_Device_IP_ActivePort_RemotePort (stMsgData);
237  }
238  else if (!strcasecmp (pSetting, "Status"))
239  {
240  ret = get_Device_IP_ActivePort_Status (stMsgData);
241  }
242 
243  return ret;
244 }
245 
246 /****************************************************************************************************************************************************/
247 // Device.IP.ActivePort. Profile. Getters:
248 /****************************************************************************************************************************************************/
249 
250 /*
251  * Parameter Name: Device.IP.ActivePort.
252 */
253 
254 /**
255  * @brief This function gets the local IP Address of the connection.
256  *
257  * @param[out] stMsgData TR-069 Host interface message request.
258  * @param[in] pChanged Status of the operation.
259  *
260  * @return Returns the status of the operation.
261  *
262  * @retval OK if is successfully fetch the data from the device.
263  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
264  * @ingroup TR69_HOSTIF_DEVICE_IP_ACTIVEPORT_API
265  */
267 {
268  /*Retrieving value */
269  getActivePortsFields(dev_id);
270 
271  if(bCalledLocalIPAddress && pChanged && strncmp(activePort.localIpAddress,backupLocalIPAddress,_LENGTH_IPADDR ))
272  {
273  *pChanged = true;
274  }
275  bCalledLocalIPAddress = true;
276  strncpy(backupLocalIPAddress,activePort.localIpAddress,sizeof(backupLocalIPAddress) -1); //CID:136360 - Buffer size warning
277  backupLocalIPAddress[sizeof(backupLocalIPAddress) -1] = '\0';
278  strncpy(stMsgData->paramValue,activePort.localIpAddress,sizeof(stMsgData->paramValue) -1);
279  stMsgData->paramValue[sizeof(stMsgData->paramValue) -1] = '\0';
280  stMsgData->paramtype = hostIf_StringType;
281  stMsgData->paramLen = strlen( activePort.localIpAddress);
282 
283  return OK;
284 }
285 
286 /**
287  * @brief This function gets the local port number of the connection.
288  *
289  * @param[out] stMsgData TR-069 Host interface message request.
290  * @param[in] pChanged Status of the operation.
291  *
292  * @return Returns the status of the operation.
293  *
294  * @retval OK if is successfully fetch the data from the device.
295  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
296  * @ingroup TR69_HOSTIF_DEVICE_IP_ACTIVEPORT_API
297  */
299 {
300 
301  /*Retrieving value */
302  getActivePortsFields(dev_id);
303  if(bCalledLocalPort && pChanged && (backupLocalPort != activePort.localPort))
304  {
305  *pChanged = true;
306  }
307  bCalledLocalPort = true;
308  backupLocalPort = activePort.localPort;
309  put_int(stMsgData->paramValue,activePort.localPort);
310  stMsgData->paramtype = hostIf_UnsignedIntType;
311  stMsgData->paramLen = 4;
312 
313  return OK;
314 }
315 
316 /**
317  * @brief This function gets the remote IP Address of the established connection.
318  * It provides the remote IP address of the source of inbound packets.
319  *
320  * @note This will be an empty string for listening connections ESTABLISHED state
321  * have remote addresses.
322  *
323  * @param[out] stMsgData TR-069 Host interface message request.
324  * @param[in] pChanged Status of the operation.
325  *
326  * @return Returns the status of the operation.
327  *
328  * @retval OK if is successfully fetch the data from the device.
329  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
330  * @ingroup TR69_HOSTIF_DEVICE_IP_ACTIVEPORT_API
331  */
333 {
334  /*Retrieving value */
335  getActivePortsFields(dev_id);
336  if(bCalledRemoteIPAddress && pChanged && strncmp(activePort.remoteIpAddress,backupRemoteIPAddress,_LENGTH_IPADDR ))
337  {
338  *pChanged = true;
339  }
340  bCalledRemoteIPAddress = true;
341  strncpy(backupRemoteIPAddress,activePort.remoteIpAddress,sizeof(backupRemoteIPAddress) -1); //CID:136383 - Buffer size warning
342  backupRemoteIPAddress[sizeof(backupRemoteIPAddress) -1] = '\0';
343  strncpy(stMsgData->paramValue,activePort.remoteIpAddress,sizeof(stMsgData->paramValue) -1);
344  stMsgData->paramValue[sizeof(stMsgData->paramValue) -1] = '\0';
345  stMsgData->paramtype = hostIf_StringType;
346  stMsgData->paramLen = strlen( activePort.remoteIpAddress);
347  return OK;
348 }
349 
350 /**
351  * @brief This function gets the remote port number of the established connection. It provides the
352  * remote port number of the source of inbound packets.
353  *
354  * @note This will be 0 for listening connections only ESTABLISHED the state have remote addresses.
355  *
356  * @param[out] stMsgData TR-069 Host interface message request.
357  * @param[in] pChanged Status of the operation.
358  *
359  * @return Returns the status of the operation.
360  *
361  * @retval OK if is successfully fetch the data from the device.
362  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
363  * @ingroup TR69_HOSTIF_DEVICE_IP_ACTIVEPORT_API
364  */
366 {
367 
368  /*Retrieving value */
369  getActivePortsFields(dev_id);
370  if(bCalledRemotePort && pChanged && (backupRemotePort != activePort.remotePort))
371  {
372  *pChanged = true;
373  }
374  bCalledRemotePort = true;
375  backupRemotePort = activePort.remotePort;
376  put_int(stMsgData->paramValue,activePort.remotePort);
377  stMsgData->paramtype = hostIf_UnsignedIntType;
378  stMsgData->paramLen = 4;
379  return OK;
380 }
381 
382 /**
383  * @brief This function gets the current operational status of the connection. The possible values are
384  * 'LISTEN', 'ESTABLISHED'.
385  *
386  * @param[out] stMsgData TR-069 Host interface message request.
387  * @param[in] pChanged Status of the operation.
388  *
389  * @return Returns the status of the operation.
390  *
391  * @retval OK if is successfully fetch the data from the device.
392  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
393  * @ingroup TR69_HOSTIF_DEVICE_IP_ACTIVEPORT_API
394  */
396 {
397  /*Retrieving value */
398  getActivePortsFields(dev_id);
399  if(bCalledStatus && pChanged && strncmp(activePort.status,backupStatus,_LENGTH_IPADDR ))
400  {
401  *pChanged = true;
402  }
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);
408  return OK;
409 }
410 /* End of doxygen group */
411 /**
412  * @}
413  */
414 
415 /* End of file xxx_api.c. */
416 
417 
418 /** @} */
419 /** @} */
hostIf_IPActivePort::get_Device_IP_ActivePort_RemoteIPAddress
int get_Device_IP_ActivePort_RemoteIPAddress(HOSTIF_MsgData_t *, bool *pChanged=NULL)
This function gets the remote IP Address of the established connection. It provides the remote IP add...
Definition: Device_IP_ActivePort.cpp:332
hostIf_IPActivePort
This class provides the hostIf IP active port for getting IP active port information.
Definition: Device_IP_ActivePort.cpp:53
hostIf_IPActivePort::get_Device_IP_ActivePort_RemotePort
int get_Device_IP_ActivePort_RemotePort(HOSTIF_MsgData_t *, bool *pChanged=NULL)
This function gets the remote port number of the established connection. It provides the remote port ...
Definition: Device_IP_ActivePort.cpp:365
Device_IP_ActivePort.h
The header file provides TR069 device IP active port information APIs.
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_IPActivePort::get_Device_IP_ActivePort_LocalIPAddress
int get_Device_IP_ActivePort_LocalIPAddress(HOSTIF_MsgData_t *, bool *pChanged=NULL)
This function gets the local IP Address of the connection.
Definition: Device_IP_ActivePort.cpp:266
hostIf_IPActivePort::get_Device_IP_ActivePort_LocalPort
int get_Device_IP_ActivePort_LocalPort(HOSTIF_MsgData_t *, bool *pChanged=NULL)
This function gets the local port number of the connection.
Definition: Device_IP_ActivePort.cpp:298
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
_HostIf_MsgData_t::paramValue
char paramValue[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:172
Device_IP_ActivePort
The structure holds the required parameters such as local ipaddress, localport, remoteIpAddress etc....
Definition: Device_IP_ActivePort.h:123
hostIf_IPActivePort::get_Device_IP_ActivePort_Status
int get_Device_IP_ActivePort_Status(HOSTIF_MsgData_t *, bool *pChanged=NULL)
This function gets the current operational status of the connection. The possible values are 'LISTEN'...
Definition: Device_IP_ActivePort.cpp:395
put_int
void put_int(char *ptr, int val)
This function converts the input data to integer type.
Definition: hostIf_utils.cpp:152
_HostIf_MsgData_t::paramLen
short paramLen
Definition: hostIf_tr69ReqHandler.h:175
hostIf_IPActivePort::hostIf_IPActivePort
hostIf_IPActivePort(int dev_id)
Class Constructor of the class hostIf_IPActivePort.
Definition: Device_IP_ActivePort.cpp:129