2008. 8. 26. 23:32
자신의 IP 얻어오는 소스!! ㅋ
2008. 8. 26. 23:32 in 개발/Network Driver
//------------------------------------------------------------------------------
//
// Get machine ip addresses by sockaddr for sockaddr_in/sockaddr_in6.
//
// AUTHOR : Yubin Lim
// DATE : 2004-11-05
// EMAIL : purewell at purewell dot biz
// REQUIRE : MICROSOFT PLATFORM SDK(IPHLPAPI.LIB),
// WINDOWS XP,
// Visual C/C++ Compiler
//
//------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iptypes.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi")
#pragma comment(lib, "ws2_32")
static bool win32_getMyIP(int nFamily)
{
DWORD dwRet;
PIP_ADAPTER_ADDRESSES pAdpAddrs;
PIP_ADAPTER_ADDRESSES pThis;
PIP_ADAPTER_UNICAST_ADDRESS pThisAddrs;
unsigned long ulBufLen = sizeof(IP_ADAPTER_ADDRESSES);
pAdpAddrs = (PIP_ADAPTER_ADDRESSES)malloc( ulBufLen );
if ( !pAdpAddrs ) return false;
dwRet = GetAdaptersAddresses(nFamily, 0, NULL, pAdpAddrs, &ulBufLen);
if (dwRet == ERROR_BUFFER_OVERFLOW)
{
free ( pAdpAddrs );
pAdpAddrs = (PIP_ADAPTER_ADDRESSES)malloc( ulBufLen );
if ( !pAdpAddrs ) return false;
}
dwRet = GetAdaptersAddresses(nFamily, 0, NULL, pAdpAddrs, &ulBufLen);
if ( dwRet != NO_ERROR )
{
free ( pAdpAddrs );
return false;
}
for ( pThis = pAdpAddrs; NULL != pThis; pThis = pThis->Next)
{
fprintf(stderr," FN: %Sn", pThis->FriendlyName);
fprintf(stderr," DS: %Sn", pThis->Description);
fprintf(stderr," AN: %Sn", pThis->AdapterName);
for ( pThisAddrs = pThis->FirstUnicastAddress;
NULL != pThisAddrs;
pThisAddrs = pThisAddrs->Next )
{
if ( nFamily == AF_INET )
{
struct sockaddr_in* pAddr
= (struct sockaddr_in*)pThisAddrs->Address.lpSockaddr;
cerr << " IP v4: " << inet_ntoa(pAddr->sin_addr) << endl;
}
else if ( nFamily == AF_INET6 )
{
struct sockaddr_in6* pAddr
= (sockaddr_in6*)pThisAddrs->Address.lpSockaddr;
//! @todo convert binary address type to string type
cerr << " IP v6" << endl;
}
}//for
}//for
free ( pAdpAddrs );
return true;
}
int main(void)
{
win32_getMyIP(AF_INET);
win32_getMyIP(AF_INET6);
return 0;
}
//
// Get machine ip addresses by sockaddr for sockaddr_in/sockaddr_in6.
//
// AUTHOR : Yubin Lim
// DATE : 2004-11-05
// EMAIL : purewell at purewell dot biz
// REQUIRE : MICROSOFT PLATFORM SDK(IPHLPAPI.LIB),
// WINDOWS XP,
// Visual C/C++ Compiler
//
//------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iptypes.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi")
#pragma comment(lib, "ws2_32")
static bool win32_getMyIP(int nFamily)
{
DWORD dwRet;
PIP_ADAPTER_ADDRESSES pAdpAddrs;
PIP_ADAPTER_ADDRESSES pThis;
PIP_ADAPTER_UNICAST_ADDRESS pThisAddrs;
unsigned long ulBufLen = sizeof(IP_ADAPTER_ADDRESSES);
pAdpAddrs = (PIP_ADAPTER_ADDRESSES)malloc( ulBufLen );
if ( !pAdpAddrs ) return false;
dwRet = GetAdaptersAddresses(nFamily, 0, NULL, pAdpAddrs, &ulBufLen);
if (dwRet == ERROR_BUFFER_OVERFLOW)
{
free ( pAdpAddrs );
pAdpAddrs = (PIP_ADAPTER_ADDRESSES)malloc( ulBufLen );
if ( !pAdpAddrs ) return false;
}
dwRet = GetAdaptersAddresses(nFamily, 0, NULL, pAdpAddrs, &ulBufLen);
if ( dwRet != NO_ERROR )
{
free ( pAdpAddrs );
return false;
}
for ( pThis = pAdpAddrs; NULL != pThis; pThis = pThis->Next)
{
fprintf(stderr," FN: %Sn", pThis->FriendlyName);
fprintf(stderr," DS: %Sn", pThis->Description);
fprintf(stderr," AN: %Sn", pThis->AdapterName);
for ( pThisAddrs = pThis->FirstUnicastAddress;
NULL != pThisAddrs;
pThisAddrs = pThisAddrs->Next )
{
if ( nFamily == AF_INET )
{
struct sockaddr_in* pAddr
= (struct sockaddr_in*)pThisAddrs->Address.lpSockaddr;
cerr << " IP v4: " << inet_ntoa(pAddr->sin_addr) << endl;
}
else if ( nFamily == AF_INET6 )
{
struct sockaddr_in6* pAddr
= (sockaddr_in6*)pThisAddrs->Address.lpSockaddr;
//! @todo convert binary address type to string type
cerr << " IP v6" << endl;
}
}//for
}//for
free ( pAdpAddrs );
return true;
}
int main(void)
{
win32_getMyIP(AF_INET);
win32_getMyIP(AF_INET6);
return 0;
}