Enumerate all IP addresses
Is there any good way to enumerate all IP addresses that a system currently has with native Windows API or system call?
It may includes multiple NIC card, or VPN connection on a single NIC card...
It needs to be portable between win2k, winxp and win2003, and support .Net Framework 1.1 and above (I'm not sure if IP Helper supports .Net Framework 1.1 or not, it seems for me that it only exist in .Net Framework 2.0 and above)
Thanks for any help!
Hi,
I am a new programmer. You can certainly experiment with the suggestion I give you and if it doesn't work maybe someone more experienced network programmer may have better suggestions. I suggest you looking into IP Helper API. It has wealth of information. Look into function like GetAdaptersInfo API. Often with API you need to fill in some information such as a structure, flags etc and then pass them to a function. Many functions report back with data structures filled with new information that you can parse to find out the info you are looking for. Also, you may need to determine the size of the data as well to properly allocate memory for it. You can find great sample code at MSDN. Start with sample code, compile it, tweak it a little bit-see what that does and eventually modify it completely to your needs. I have started to learn WMI and it provides some great management interface for system resources but it is a bit advanced. However, there are great number of scripts available that utilize WMI which is an easier approach than using lanugage like C++. Hope this helps.
thanks for your reply, but IP Helper is not exactly what i want.
i would like to use native C++ function instead of .Net Framework APIs. because the runtime envionrment maynot have .net framework 2.0 or higher.
What i found is WSAIoctl, which is included in #include <winsock2.h>
Hello,
Maybe I can learn something from you. I skimmed over WSAIoctl. It offers tremendous potential.
Isn't WSAIoctl related to .NET framework? Assuming it is not, if I understand correctly, your code will be portable across different not just across various versions of Windows but other OS including Linux, UNIX etc because native C/C++ functions are an industry standard. Maybe you can explain it. If you can briefly explain how would you get IP address information out of this function?
SIO_ADDRESS_LIST_QUERY (opcode setting: I, O, T==1) Obtains a list of local transport addresses of the socket's protocol family to which the application can bind. Above parameter is an op code that seems to get IP address information. I will greatly appreciate if you can give me some insight on how you will go about getting IP address information. You have my thanks.
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
char szErrorString[100];
#define MAX_ADAPTERS (20)
typedef struct _MY_SOCKET_ADDRESS_LIST {
INT iAddressCount;
SOCKET_ADDRESS Address[MAX_ADAPTERS];
} MY_SOCKET_ADDRESS_LIST;
/****************************************************************
get ip address of adapter #num in ifx
****************************************************************/
int GetAdapter(SOCKET s, SOCKADDR_IN *ifx, int num)
{
MY_SOCKET_ADDRESS_LIST slist;
DWORD dwBytesRet;
if (SOCKET_ERROR == WSAIoctl(s, SIO_ADDRESS_LIST_QUERY,
NULL, 0, &slist, sizeof(MY_SOCKET_ADDRESS_LIST), &dwBytesRet, NULL, NULL)) {
sprintf(szErrorString, "WSAIoctl(SIO_ADDRESS_LIST_QUERY) failed - error code: %u\n",
WSAGetLastError());
printf(szErrorString);
return 1;
}
if (num >= slist.iAddressCount)
return 2;
ifx->sin_addr.s_addr = ((SOCKADDR_IN *)slist.Address[num].lpSockaddr)->sin_addr.s_addr;
return 0;
}
/****************************************************************
get the list of all available adapters
return 0 on success
****************************************************************/
int GetAdapterList(MY_SOCKET_ADDRESS_LIST *slist)
{
SOCKET sock;
DWORD dwBytesRet;
int ret;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (SOCKET_ERROR == sock) {
sprintf(szErrorString, "socket(AF_INET, SOCK_STREAM, IPPROTO_IP) failed - error code: %u\n",
WSAGetLastError());
printf(szErrorString);
return 1;
}
ret = WSAIoctl(sock, SIO_ADDRESS_LIST_QUERY, NULL, 0, slist, sizeof(MY_SOCKET_ADDRESS_LIST), &dwBytesRet, NULL, NULL);
if (SOCKET_ERROR == ret) {
sprintf(szErrorString, "WSAIoctl(sock, SIO_ADDRESS_LIST_QUERY ...) failed - error code: %u\n",
WSAGetLastError());
printf(szErrorString);
return 2;
}
closesocket(sock);
return 0;
}
/****************************************************************
list all available net adapters by address
****************************************************************/
int ListAdapters(void)
{
MY_SOCKET_ADDRESS_LIST slist;
int iCounter;
if (0 == GetAdapterList(&slist)) {
fprintf(stdout, "Found %u adapters:\n\n", slist.iAddressCount);
for (iCounter = 0; iCounter <= (slist.iAddressCount-1); iCounter++) {
/* bugfix for error with more than one network adapter on some machines -> sprintf(buf,"#%d -> ",i); */
fprintf(stdout,"#%d -> %s\n", iCounter,
inet_ntoa(((SOCKADDR_IN *)slist.Address[iCounter].lpSockaddr)->sin_addr));
}
return slist.iAddressCount;
}
return 0;
}
int main()
{
WSADATA wsadata;
DWORD dwVersion;
dwVersion = GetVersion();
if ((dwVersion >= 0x80000000) || (5 > LOBYTE(LOWORD(dwVersion)))) {
printf("Windows 2000 or higher version is requred!\n");
exit(10);
}
if (WSAStartup(0x202, &wsadata)) /* init winsock */
return FALSE;
ListAdapters();
for(;
;
return 0;
}