Article View: borland.public.cppbuilder.internet.socket
Article #5134How can I get the public IP address of a router?
From: gangleec
Date: Fri, 08 Oct 2004 20:10
Date: Fri, 08 Oct 2004 20:10
74 lines
2766 bytes
2766 bytes
My home network has one DSL modem, one router and two PCs.
Each PC attached to the router has an IP address, 192.168.0.x .
The router is attached to the DSL modem.
I need the public IP address of the router.
So, I make following codes and run it from one of my home network PC.
But, it displays private IP address.
How can I get the public IP address of the router?
#include<winsock2.h>
#include<stdio.h>
#include<iostream>
#include "iphlpapi.h"
using namespace std;
#pragma link "iphlpapi.lib"
#pragma argsused
int main(int argc, char* argv[])
{
ShowIpNetTable();
return 0;
}
void ShowIpNetTable()
{
DWORD size=0;
char buff[512];
GetIpNetTable((MIB_IPNETTABLE*)buff,&size,true);
if(size==0) return;
char* raw_buff = new char[size];
MIB_IPNETTABLE* pmib_ipnettable=(MIB_IPNETTABLE*)raw_buff;
if(ERROR_INSUFFICIENT_BUFFER!=GetIpNetTable(pmib_ipnettable,&size,true))
{
for(DWORD i=0; i<pmib_ipnettable->dwNumEntries; ++i)
{
cout << "IpNet" << (pmib_ipnettable->table[i].dwIndex) << ": " << endl;
cout << " MAC = " << StrMacAddrFromBytes(pmib_ipnettable->table[i].bPhysAddr ,pmib_ipnettable->table[i].dwPhysAddrLen,buff) << endl;
cout << " IP = " << DottedBytesFromDword(pmib_ipnettable->table[i].dwAddr,buff) << endl;
cout << " Type= " << HexFromDword(pmib_ipnettable->table[i].dwType,buff) << endl;
}
}
delete[] raw_buff;
}
char *StrMacAddrFromBytes(const BYTE* bytes,int size,char*outbuff)
{
for(int i=0 ;i<size ;++i)
{
sprintf(outbuff+i*3,"%02X:",bytes[i]);
}
outbuff[size*3-1]=0;
return outbuff;
}
char* DottedBytesFromDword(DWORD ip,char*outbuff)
{
unsigned char *p = (char*)&ip;
int n=0;
for(int i=0 ;i<sizeof(DWORD) ;++i)
{
n += sprintf(outbuff+n,"%u.",p[i]);
}
outbuff[n-1]=0;
return outbuff;
}
char* HexFromDword(DWORD num,char *outbuff)
{
sprintf(outbuff,"%X",num);
return outbuff;
}
Technical Details
Message-ID:
<4166f446$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,75a5b47936081a55,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "gangleec" <gangleec@yahoo.com> Sender: "gangleec" <gangleec@yahoo.com> Reply-To: "gangleec" <gangleec@yahoo.com> Subject: How can I get the public IP address of a router? Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 218.156.225.85 NNTP-Posting-Host: 207.105.83.66 Message-ID: <4166f446$1@newsgroups.borland.com> Date: 8 Oct 2004 13:10:46 -0700 X-Trace: newsgroups.borland.com 1097266246 207.105.83.66 (8 Oct 2004 13:10:46 -0700) Lines: 76 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail