5,136,421 members and growing! (13,448 online)
Email Password   helpLost your password?
Web Development » Internet / Network » General     Intermediate

Get all IP numbers of your machine and how to pass through an authenticating proxy

By Nico Cuppen

An article on how to find all IP numbers of your machine. Will also show you how to pass through an authenticating proxy server.
VC6, C++Windows, Win2K, WinXP, MFC, VS6, VS, Dev

Posted: 30 Sep 2002
Updated: 30 Sep 2002
Views: 108,258
Announcements



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.07 Rating: 3.97 out of 5
3 votes, 25.0%
1
2 votes, 16.7%
2
1 vote, 8.3%
3
0 votes, 0.0%
4
6 votes, 50.0%
5

Sample Image - getipnr.gif

Introduction

I needed some code that would give me the IP number of my machine. Not only the number assigned to it by DHCP, either on a LAN or by my ISP when dialing up, but also my IP number as seen from the web, if my machine is behind a router/firewall.

It took a while before I figured it all out, and in the process I have seen a lot of code from others. I borrowed some code here and there, but I don't remember exactly what part, so it is hard to give credits to the authors of that code. Sorry about that. Hope I can make it up by sharing this code with you.

Relevant functions in the sample source code

The sample program does essentially two things: it reports the IP numbers as 'known' by the TCP/IP software on your own machine ('internal' IP numbers) and it reports the IP numbers of your machine as seen from the web ('external' IP numbers). Of course one of the internal numbers will be the same as the external number if there is no router/firewall between your machine and the Internet.

Internal IP numbers

Internal IP numbers are reported by function FindLocalAddress(). It calls GetIpAddrTable(), one of the IP Helper functions, which returns a block of data with this structure:

typedef struct _MIB_IPADDRTABLE
{
    DWORD dwNumEntries;
    MIB_IPADDRROW table[ANY_SIZE];
} MIB_IPADDRTABLE, *PMIB_IPADDRTABLE;

dwNumEntries contains the number of entries in 'table'.

The structure of 'table' looks like this:

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

dwAddr is the address we are after. FindLocalAddresses() fetches that number and converts it to a xxx.xxx.xxx.xxx string by calling function inet_ntoa().

External IP numbers

External IP numbers are reported by function FindWebAddress(). It does this by making a connection to URL 'checkip.dyndns.org'. To see why, open this URL in your browser. It returns the IP number of your machine as seen from this site. If your machine is behind a firewall/router this number will differ from the internal IP numbers found earlier. Of course in this case the IP number found will in fact be the IP number of your router/firewall.

FindWebAddress() uses the WinINet API to make a connection to 'checkip.dyndns.org' and to read and parse its HTML source to find the IP number hidden in this HTML source.

How to pass through an authenticating proxy server

FindWebAddress() also shows you how to pass through a proxy server that requires authentication. The sample program creates its CInternetSession object with option INTERNET_OPEN_TYPE_PRECONFIG. This means that if your Internet Explorer configuration contains a proxy server definition ("Tools - Internet Options - Connection tab - LAN Settings..."), the Internet connection will be made through this proxy server. In that case you will know that you have to pass through an authenticating proxy when your first attempt to connect to 'checkip.dyndns.org' fails with return code 407. The sample program will then ask you to specify a username and a password, and tries to connect to 'checkip.dyndns.org' through the proxy server.

About the author

My name is Nico Cuppen. I live in Haarlem, The Netherlands and I like programming. That's how I became a shareware author. You can find more info about me and my products on www.nicocuppen.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nico Cuppen



Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
Subject  Author Date 
Generalneed C# implementationmemberTauhid Shaikh3:16 24 Sep '07  
Generalhimemberji`21:39 19 Aug '07  
GeneralMessymemberSwinefeaster13:16 23 May '07  
GeneralGetting IP Address of Local Machinememberpaijwar5:08 6 Sep '06  
Generalerror!membermahila0:32 28 Dec '05  
GeneralRe: error!memberNico Cuppen0:39 28 Dec '05  
GeneralRe: error!membermahila0:52 29 Dec '05  
GeneralRe: error!memberAndre Oliveira2:28 5 May '06  
Generalregarding checkip.dyndns.orgmemberkalicharan22:16 7 Aug '04  
GeneralRe: regarding checkip.dyndns.orgmemberBob Stanneveld8:16 30 Sep '04  
GeneralNeed compiled versionmemberEd_N2:34 16 Feb '04  
GeneralRe: Need compiled versionmemberNico Cuppen3:29 17 Feb '04  
Generalerrors with iphlpapi.hmemberM00sejewce5:52 11 Feb '04  
GeneralRe: errors with iphlpapi.hmemberNico Cuppen3:32 17 Feb '04  
GeneralRe: errors with iphlpapi.hmemberDuncan_Aitken2:05 16 Apr '04  
GeneralRe: errors with iphlpapi.hsussAnonymous3:54 29 Jan '05  
GeneralRe: errors with iphlpapi.hsussAnonymous13:18 3 Aug '04  
GeneralRe: errors with iphlpapi.hsussPlanlos23:36 5 Aug '04  
GeneralRe: errors with iphlpapi.hsussAnonymous10:49 30 Nov '04  
GeneralRe: errors with iphlpapi.hsussAnonymous3:34 8 Dec '04  
GeneralRe: errors with iphlpapi.hmembersend_jk21:55 10 Jul '06  
GeneralNetwork MonitoringmemberChechi1:52 15 Dec '03  
GeneralRe: Network MonitoringmemberNico Cuppen2:13 15 Dec '03  
GeneralQuestionmembernonahelmi17:00 30 Jul '03  
GeneralRe: QuestionsussAnonymous22:24 10 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2002
Editor: Nishant Sivakumar
Copyright 2002 by Nico Cuppen
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project