<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=KOI8-R" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Here is several ways to determine IP address..<br>
<br>
<hr size="2" width="100%"><b>#1 Using WSAIoctl Winsock2 with
SIO_ADDRESS_LIST_QUERY</b><br>
<br>
#include <iostream><br>
#include <winsock2.h><br>
<br>
#pragma comment (lib, "ws2_32.lib")<br>
<br>
int main(void)<br>
{<br>
using std::cout;<br>
using std::cerr;<br>
using std::endl;<br>
<br>
WSADATA wd;<br>
SOCKET sock;<br>
char buf[65535];<br>
DWORD dwSize = 65535;<br>
int nRet;<br>
SOCKET_ADDRESS_LIST *pAddr;<br>
SOCKADDR* saddr;<br>
<br>
nRet = WSAStartup(MAKEWORD(2,0), &wd);<br>
if(nRet != 0){<br>
cerr << "WSAStartup fail with code " << nRet
<< endl;<br>
return -1;<br>
}<br>
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);<br>
if(sock == INVALID_SOCKET){<br>
cerr << "socket fail with code " <<
WSAGetLastError() << endl;<br>
return -2;<br>
}<br>
ZeroMemory(buf, 65535);<br>
nRet = WSAIoctl(sock, SIO_ADDRESS_LIST_QUERY, NULL, 0, buf, 65535,
&dwSize, NULL, NULL);<br>
if(nRet != SOCKET_ERROR){<br>
pAddr = reinterpret_cast<SOCKET_ADDRESS_LIST*>(buf);<br>
nRet = pAddr->iAddressCount;<br>
saddr = pAddr->Address->lpSockaddr;<br>
while (0 < nRet--){<br>
cout <<
inet_ntoa(reinterpret_cast<SOCKADDR_IN*>(saddr)->sin_addr)
<< endl;<br>
saddr++;<br>
}<br>
nRet = 0;<br>
}else {<br>
cerr << "WSAIoctl fail with code " <<
WSAGetLastError() << endl;<br>
nRet = -3;<br>
}<br>
closesocket(sock);<br>
WSACleanup();<br>
return nRet;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#2 Using IP Helper API</b><br>
<br>
#include <iostream><br>
#include <windows.h><br>
#include <iphlpapi.h><br>
<br>
#pragma comment (lib, "iphlpapi")<br>
<br>
void main (void)<br>
{<br>
using std::cout;<br>
using std::cerr;<br>
using std::endl;<br>
<br>
char* buf;<br>
PIP_ADAPTER_INFO pAdaptersInfo;<br>
PIP_ADDR_STRING pAddr;<br>
DWORD dwSize = 0;<br>
<br>
if(GetAdaptersInfo(NULL, &dwSize) != ERROR_BUFFER_OVERFLOW){<br>
cerr << "GetAdaptersInfo fail" << endl;<br>
return;<br>
}<br>
buf = new char[dwSize];<br>
if (!buf) return;<br>
pAdaptersInfo = reinterpret_cast<PIP_ADAPTER_INFO>(buf);<br>
if (GetAdaptersInfo (pAdaptersInfo, &dwSize) == ERROR_SUCCESS){<br>
while (pAdaptersInfo){<br>
pAddr = &pAdaptersInfo->IpAddressList;<br>
while (pAddr){<br>
cout <<"Address: " <<
pAddr->IpAddress.String << "/" << <br>
pAddr->IpMask.String << endl;<br>
pAddr = pAddr->Next;<br>
}<br>
pAdaptersInfo = pAdaptersInfo->Next;<br>
}<br>
}<br>
delete[] buf;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#3 The easiest one.</b><br>
<br>
#include <iostream><br>
#include <winsock2.h><br>
<br>
#pragma comment (lib, "ws2_32.lib")<br>
<br>
int main(void)<br>
{<br>
using std::cout;<br>
using std::cerr;<br>
using std::endl;<br>
<br>
WSADATA wd;<br>
char buf[65535];<br>
int nRet;<br>
struct hostent *phe;<br>
struct in_addr *paddr;<br>
<br>
nRet = WSAStartup(MAKEWORD(1, 1), &wd);<br>
if(nRet != 0){<br>
cerr << "WSAStartup fail with code " << nRet
<< endl;<br>
return -1;<br>
}<br>
if(gethostname(buf, 65535) != SOCKET_ERROR){<br>
phe = gethostbyname(buf);<br>
if(phe != 0){<br>
while(phe->h_addr_list[nRet]){<br>
paddr = reinterpret_cast<struct
in_addr*>(phe->h_addr_list[nRet++]);<br>
cout << inet_ntoa(*paddr) << endl;<br>
}<br>
}<br>
}<br>
WSACleanup();<br>
return nRet;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#4 Broadcasting and determining network
configuration</b><br>
<br>
#include <ioctl.h><br>
#include <stdio.h><br>
#include <stdlib.h><br>
#include <string.h><br>
#include <sys/socket.h><br>
#include <arpa/inet.h><br>
#include <net/if.h><br>
#include <netinet/in.h><br>
int <br>
main (int argc, char *argv[])<br>
{<br>
int sock;<br>
struct ifconf d;<br>
struct ifreq *ifr, *end, *cur, *temp;<br>
char buffer[128];<br>
if ((sock= socket (AF_INET, SOCK_DGRAM, 0)) == -1) {<br>
perror ("socket");<br>
exit (EXIT_FAILURE);<br>
}<br>
/* temporary storage for getting broadcast address */<br>
temp= (struct ifreq *)buffer;<br>
d.ifc_len= 4096;<br>
d.ifc_buf= malloc (d.ifc_len);<br>
if (ioctl (sock, SIOCGIFCONF, &d) == -1) {<br>
perror ("ioctl (SIOCGIFCONF)");<br>
exit (EXIT_FAILURE);<br>
}<br>
/*<br>
* Note that the ifreq structure is variable length so we have to
step <br>
* along the structure by the size of the prev structure <br>
*/<br>
ifr= (struct ifreq *) d.ifc_req;<br>
end= (struct ifreq *) ((char *) ifr + d.ifc_len);<br>
while (ifr < end) {<br>
cur= ifr;<br>
/* step along the array by the size of the current
structure */<br>
ifr=(struct ifreq *)((char *)ifr + ifr->ifr_addr.sa_len
+ IFNAMSIZ);<br>
/* if this isn't in the INET address family ignore */<br>
if (cur->ifr_addr.sa_family != AF_INET)<br>
continue; <br>
/* save aside the ifr structure to get the broadcast addr */<br>
memcpy(temp, cur, cur->ifr_addr.sa_len + IFNAMSIZ);<br>
<br>
printf ("%s (%s) ", cur->ifr_name, inet_ntoa(((struct
sockaddr_in *)<br>
&cur->ifr_addr)->sin_addr));<br>
/* get the flags for this interface */<br>
if (ioctl (sock, SIOCGIFFLAGS, (char *) cur) < 0) {<br>
perror ("ioctl (SIOCGIFFLAGS)");<br>
exit (EXIT_FAILURE);<br>
}<br>
/* if the interface is up, print out some information about
it */<br>
if (cur->ifr_flags & IFF_UP) {<br>
if (cur->ifr_flags & IFF_BROADCAST) {<br>
printf ("broadcast ");<br>
if (ioctl(sock, SIOCGIFBRDADDR, (char *)temp)
!= -1)<br>
printf("%s ", inet_ntoa(((struct
sockaddr_in *)<br>
&temp->ifr_addr)->sin_addr));<br>
else {<br>
perror("ioctl (SIOCGIFBRDADDR)");<br>
exit(EXIT_FAILURE);<br>
}<br>
}<br>
if (cur->ifr_flags & IFF_POINTOPOINT) {<br>
printf ("point-to-point dst ");<br>
if (ioctl(sock, SIOCGIFDSTADDR, (char *)temp)
!= -1)<br>
printf("%s ", inet_ntoa(((struct
sockaddr_in *)<br>
&cur->ifr_addr)->sin_addr));<br>
else {<br>
perror("ioctl (SIOCGIFDSTADDR)");<br>
exit(EXIT_FAILURE);<br>
}<br>
}<br>
}<br>
putc ('\n', stdout);<br>
}<br>
return EXIT_SUCCESS;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#5 Using SNMP Extension Agent API</b><br>
<br>
#include <iostream><br>
#include <winsock2.h><br>
#include <snmp.h><br>
<br>
#pragma comment(lib, "snmpapi.lib")<br>
#pragma comment(lib, "ws2_32.lib")<br>
<br>
namespace bsnmp {<br>
PFNSNMPEXTENSIONINIT SnmpExtensionInit;<br>
PFNSNMPEXTENSIONQUERY SnmpExtensionQuery;<br>
};<br>
<br>
int main(void)<br>
{<br>
HANDLE hPollForTrapEvent;<br>
AsnObjectIdentifier SupportedView;<br>
HMODULE hLib;<br>
UINT OID[] = { 1, 3, 6, 1, 2, 1, 4 , 20, 1 ,1 };<br>
SnmpVarBindList SVBList;<br>
SnmpVarBind SVBVars[1];<br>
AsnObjectIdentifier AsnOID = { sizeof(OID)/sizeof(UINT), OID
};<br>
AsnInteger32 ErrorStatus, ErrorIndex;<br>
<br>
hLib = LoadLibrary("inetmib1.dll");<br>
if (!hLib){<br>
std::cerr << "Loadlibrary fail" << std::endl;<br>
return -1;<br>
}<br>
bsnmp::SnmpExtensionInit =
reinterpret_cast<PFNSNMPEXTENSIONINIT>(GetProcAddress(hLib,
"SnmpExtensionInit"));<br>
bsnmp::SnmpExtensionQuery =
reinterpret_cast<PFNSNMPEXTENSIONQUERY>(GetProcAddress(hLib,
"SnmpExtensionQuery"));<br>
if(!bsnmp::SnmpExtensionInit || !bsnmp::SnmpExtensionQuery){<br>
std::cerr << "GetProcAddress fail" << std::endl;<br>
return -2;<br>
}<br>
if(bsnmp::SnmpExtensionInit(0, &hPollForTrapEvent,
&SupportedView)){<br>
SVBList.len = 1;<br>
SVBList.list = SVBVars;<br>
SnmpUtilOidCpy(&SVBVars[0].name, &AsnOID);<br>
while(true){<br>
if(bsnmp::SnmpExtensionQuery(ASN_RFC1157_GETNEXTREQUEST,
&SVBList, &ErrorStatus, &ErrorIndex) != 0){<br>
if(SnmpUtilOidNCmp(&SVBVars[0].name, &AsnOID,
AsnOID.idLength)) break;<br>
std::cout <<
inet_ntoa(*reinterpret_cast<struct
in_addr*>(SVBVars[0].value.asnValue.address.stream))<br>
<< std::endl;<br>
}else break;<br>
}<br>
SnmpUtilOidFree(&SVBVars[0].name);<br>
}else {<br>
std::cerr << "SnmpExtensionInit fail" << std::endl;<br>
}<br>
FreeLibrary(hLib);<br>
return 0;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#6 Using WinPCAP</b><br>
<br>
#include "pcap.h"<br>
#include "pcap-int.h"<br>
<br>
#include <ntddndis.h><br>
#pragma comment(lib, "packet.lib")<br>
<br>
#ifndef WIN32<br>
#include <sys/socket.h><br>
#include <netinet/in.h><br>
#else<br>
#include <winsock.h><br>
#endif<br>
<br>
void ifprint(pcap_if_t *d);<br>
char *iptos(u_long in);<br>
int getmac(pcap_t* ha, char* pStr);<br>
<br>
int main()<br>
{<br>
pcap_if_t *alldevs;<br>
pcap_if_t *d;<br>
char errbuf[PCAP_ERRBUF_SIZE+1];<br>
<br>
if (pcap_findalldevs(&alldevs, errbuf) == -1)<br>
{<br>
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);<br>
exit(1);<br>
}<br>
<br>
for(d=alldevs;d;d=d->next)<br>
{<br>
ifprint(d);<br>
}<br>
<br>
return 1;<br>
}<br>
<br>
void ifprint(pcap_if_t *d)<br>
{<br>
pcap_addr_t *a;<br>
pcap_t *hadapter;<br>
char pMAC[20];<br>
printf("%s\n",d->name);<br>
if (d->description)<br>
printf("\tDescription: %s\n",d->description);<br>
printf("\tLoopback: %s\n",(d->flags &
PCAP_IF_LOOPBACK)?"yes":"no");<br>
hadapter = pcap_open_live(d->name, 0, 0, 0, 0);<br>
if(getmac(hadapter, pMAC)){<br>
printf("\tMAC Address: %s\n", pMAC);<br>
}<br>
pcap_close(hadapter);<br>
for(a=d->addresses;a;a=a->next) {<br>
printf("\tAddress Family: #%d\n",a->addr->sa_family);<br>
<br>
switch(a->addr->sa_family)<br>
{<br>
case AF_INET:<br>
printf("\tAddress Family Name: AF_INET\n");<br>
if (a->addr)<br>
printf("\tAddress: %s\n",iptos(((struct sockaddr_in
*)a->addr)->sin_addr.s_addr));<br>
if (a->netmask)<br>
printf("\tNetmask: %s\n",iptos(((struct sockaddr_in
*)a->netmask)->sin_addr.s_addr));<br>
if (a->broadaddr)<br>
printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in
*)a->broadaddr)->sin_addr.s_addr));<br>
if (a->dstaddr)<br>
printf("\tDestination Address: %s\n",iptos(((struct
sockaddr_in *)a->dstaddr)->sin_addr.s_addr));<br>
break;<br>
default:<br>
printf("\tAddress Family Name: Unknown\n");<br>
break;<br>
}<br>
}<br>
printf("\n");<br>
}<br>
<br>
#define IPTOSBUFFERS 12<br>
char *iptos(u_long in)<br>
{<br>
static char output[IPTOSBUFFERS][3*4+3+1];<br>
static short which;<br>
u_char *p;<br>
<br>
p = (u_char *)&in;<br>
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);<br>
sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);<br>
return output[which];<br>
}<br>
int getmac(pcap_t* ha, char* pStr)<br>
{<br>
PPACKET_OID_DATA pOidData;<br>
CHAR pAddr[sizeof(PACKET_OID_DATA)+5];<br>
ZeroMemory(pAddr, sizeof(PACKET_OID_DATA)+5);<br>
pOidData = (PPACKET_OID_DATA) pAddr;<br>
pOidData->Oid = OID_802_3_CURRENT_ADDRESS;<br>
pOidData->Length = 6;<br>
if(PacketRequest(ha->adapter, FALSE, pOidData))<br>
{ <br>
sprintf(pStr, "%.02X:%.02X:%.02X:%.02X:%.02X:%.02X", <br>
pOidData->Data[0],pOidData->Data[1],pOidData->Data[2],<br>
pOidData->Data[3],pOidData->Data[4],pOidData->Data[5]);<br>
}else{<br>
return 0;<br>
}<br>
return 1;<br>
}<br>
<br>
<hr size="2" width="100%"><b>#7 Using VB</b><br>
<br>
strComputer = "."<br>
'strComputer = "ΣΕΤΕΧΟΕ ΙΝΡ ΥΔΑΜΕΞΞΟΗΟ ΛΟΝΠΑ"<br>
Set objWMIService = GetObject(<a class="moz-txt-link-rfc2396E" href="winmgmts:\\">"winmgmts:\\"</a> & strComputer &
"\root\cimv2")<br>
Set IPConfigSet = objWMIService.ExecQuery _<br>
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")<br>
For Each IPConfig in IPConfigSet<br>
If Not IsNull(IPConfig.IPAddress) Then<br>
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)<br>
WScript.Echo IPConfig.IPAddress(i), IPConfig.Description(i)<br>
Next<br>
End If<br>
Next<br>
<br>
<hr size="2" width="100%"><br>
-- <br>
Best regards. Vasily Borovyak <a class="moz-txt-link-rfc2396E" href="mailto:vbor@isd.dp.ua"><vbor@isd.dp.ua></a><br>
</body>
</html>