July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

IP Helper API: Managing IP Addresses

  • September 21, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Our previous article described basic network information retrieving. We've got General Network Configuration parameters and different info about adapters and interfaces. Now, we will do the next step and discuss various aspects of IP management on Pocket PC devices.

Listing a Device's IP Addresses

This topic returns us to a previously discussed theme: the GetAdaptersInfo function. Its output is an array of IP_ADAPTER_INFO structs; each one contains IpAddressList implemented as a linked list. Thus, a simple code snippet provides all the job:

void CNWParamsDlg::ShowIPAddress(PIP_ADAPTER_INFO pAdapterInfo)
{
   m_ListBox.ResetContent();
   do
   {
      m_ListBox.AddString(L"Adapter: " +
                          CString(pAdapterInfo->AdapterName));
      m_ListBox.AddString(L"   Desc: " +
                          CString(pAdapterInfo->Description));

      PIP_ADDR_STRING pAddressList = &(pAdapterInfo->IpAddressList);
      do
      {
         m_ListBox.AddString(CString(L"   IP: ") +
                             pAddressList->IpAddress.String);
         pAddressList = pAddressList->Next;
      }
      while (pAddressList != NULL);

      pAdapterInfo = pAdapterInfo->Next;
   }
   while(pAdapterInfo);

   ((CStatic*)GetDlgItem(IDC_STATIC_INFO))->
      SetWindowText(L"IP Addresses:");
}

void CNWParamsDlg::OnIPAddrList()
{
   IP_ADAPTER_INFO *pAdapterInfo = NULL;
   ULONG ulBufLen = 0;
   int nErr = ERROR_SUCCESS;

   nErr = GetAdaptersInfo(pAdapterInfo,&ulBufLen);

   // If buffer size is smaller - allocate memory and try again
   if ( nErr == ERROR_BUFFER_OVERFLOW )
   {
      pAdapterInfo = (IP_ADAPTER_INFO *)new char[ulBufLen];

      nErr = GetAdaptersInfo(pAdapterInfo,&ulBufLen);

      if( nErr == ERROR_SUCCESS )
      {
         ShowIPAddress(pAdapterInfo);
         delete pAdapterInfo;
         return;
      }
      delete pAdapterInfo;
   }
}

Surfing Through Interfaces

In some situations, you may be required to look up an IP address for a specific interface or detect all defined addresses. IP Helper API provides an appropriate function for such purpose: GetIpAddrTable:

DWORD
WINAPI
GetIpAddrTable(
    OUT    PMIB_IPADDRTABLE pIpAddrTable,
    IN OUT PULONG           pdwSize,
    IN     BOOL             bOrder
    );

As an output, you will get a MIB_IPADDRTABLE table—an array of MIB_IPADDRROW structs. This last structure is listed below:

typedef struct _MIB_IPADDRROW
{
    DWORD             dwAddr;
    DWORD             dwIndex;
    DWORD             dwMask;
    DWORD             dwBCastAddr;
    DWORD             dwReasmSize;
    unsigned short    unused1;
    unsigned short    unused2;
} MIB_IPADDRROW, *PMIB_IPADDRROW;

It keeps important info—such as the IP address, subnet mask, and broadcast address—for the specified interface. For instance, knowing the broadcasting address may be quite profitable when you need to send some discovering request to any suitable hosts. If you want more details about the corresponding network interface, dwIndex member gives you a shortcut. The following sample produces an IP-to-interface listing:

void CNWParamsDlg::ShowIp2Inteface(PMIB_IPADDRTABLE pIpAddrTable)
{
   m_ListBox.ResetContent();
   CString sTmp;
   sTmp.Format(L"Num Entries: %d",pIpAddrTable->dwNumEntries);
   m_ListBox.AddString(sTmp);

   in_addr addr;
   in_addr bcaddr;
   TCHAR szBuffer[128];
   for (int i = 0; i < pIpAddrTable->dwNumEntries; i++)
   {
      MIB_IFROW mibRow;
      memset(&mibRow,0,sizeof(mibRow));
      mibRow.dwIndex = pIpAddrTable->table[i].dwIndex;
      GetIfEntry(&mibRow);
      sTmp.Format(L"Idx: %lu Name: %s", pIpAddrTable->
         table[i].dwIndex, mibRow.wszName);
      m_ListBox.AddString(sTmp);

      addr.S_un.S_addr = pIpAddrTable->table[i].dwAddr;
      char *szAddr = inet_ntoa(addr);
      memset(szBuffer,0,sizeof(szBuffer));
      MultiByteToWideChar(CP_ACP,0,szAddr,strlen(szAddr),
                          szBuffer,sizeof(szBuffer));
      sTmp.Format(L"   IP: %s", szBuffer);
      m_ListBox.AddString(sTmp);

      bcaddr.S_un.S_addr = pIpAddrTable->table[i].dwBCastAddr;
      char *szBCAddr = inet_ntoa(bcaddr);
      memset(szBuffer,0,sizeof(szBuffer));
      MultiByteToWideChar(CP_ACP,0,szBCAddr,strlen(szBCAddr),
                          szBuffer,sizeof(szBuffer));
      sTmp.Format(L"   BC IP: %s", szBuffer);
      m_ListBox.AddString(sTmp);
   }

   ((CStatic*)GetDlgItem(IDC_STATIC_INFO))->
      SetWindowText(L"Ip to Interfaces:");
}

void CNWParamsDlg::OnIp2Intf()
{
   PMIB_IPADDRTABLE pIpAddrTable = NULL;
   ULONG dwSize = 0;
   DWORD dwRes = GetIpAddrTable(pIpAddrTable, &dwSize, FALSE);

   pIpAddrTable = (PMIB_IPADDRTABLE)new BYTE[dwSize];
   dwRes = GetIpAddrTable(pIpAddrTable, &dwSize, FALSE);

   TRACE(L"dwRes = %lu\n",dwRes);
   if ( dwRes == 0 )
      ShowIp2Inteface(pIpAddrTable);
   delete pIpAddrTable;
}




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel