Click here to Skip to main content
Licence CPOL
First Posted 30 Sep 2002
Views 150,585
Bookmarked 54 times

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

By | 30 Sep 2002 | Article
An article on how to find all IP numbers of your machine. Will also show you how to pass through an authenticating proxy server.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nico Cuppen

Web Developer

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMichael Haephrati9:43 1 Feb '12  
GeneralThank you Pinmember3box6:36 6 May '11  
GeneralMy vote of 5 Pinmemberzzz7net4:59 18 Jan '11  
Generalneed C# implementation PinmemberTauhid Shaikh2:16 24 Sep '07  
Generalhi Pinmemberji`20:39 19 Aug '07  
GeneralMessy PinmemberSwinefeaster12:16 23 May '07  
GeneralGetting IP Address of Local Machine Pinmemberpaijwar4:08 6 Sep '06  
Hi,
 
I think the simplest way, by which we can get the IP address of local machine by following.
1. gethostname
2. gethostbyname [as if you know any one of your ip you can also use gethostbyaddress]
 
Using the name returned by step one, in the step two. As gethostbyname returns hostent* , it will have all the IP address those are allocated to your machine.
 
and If none of them is your public IP address (This you can check by class of IP Address, if it is of class C) most probably it will be public.
 
And if you are behind NAT, as most of the ISP give us the connectivity, by checking with many of the free IP checker websites, like www.whatismyip.com, you can get what is your interface for public.
 

 

Generalerror! Pinmembermahila23:32 27 Dec '05  
GeneralRe: error! PinmemberNico Cuppen23:39 27 Dec '05  
GeneralRe: error! Pinmembermahila23:52 28 Dec '05  
GeneralRe: error! PinmemberAndre Oliveira1:28 5 May '06  
Generalregarding checkip.dyndns.org Pinmemberkalicharan21:16 7 Aug '04  
GeneralRe: regarding checkip.dyndns.org PinmemberBob Stanneveld7:16 30 Sep '04  
GeneralNeed compiled version PinmemberEd_N1:34 16 Feb '04  
GeneralRe: Need compiled version PinmemberNico Cuppen2:29 17 Feb '04  
Generalerrors with iphlpapi.h PinmemberM00sejewce4:52 11 Feb '04  
GeneralRe: errors with iphlpapi.h PinmemberNico Cuppen2:32 17 Feb '04  
GeneralRe: errors with iphlpapi.h PinmemberDuncan_Aitken1:05 16 Apr '04  
GeneralRe: errors with iphlpapi.h PinsussAnonymous2:54 29 Jan '05  
GeneralRe: errors with iphlpapi.h PinsussAnonymous12:18 3 Aug '04  
GeneralRe: errors with iphlpapi.h PinsussPlanlos22:36 5 Aug '04  
GeneralRe: errors with iphlpapi.h PinsussAnonymous9:49 30 Nov '04  
GeneralRe: errors with iphlpapi.h PinsussAnonymous2:34 8 Dec '04  
GeneralRe: errors with iphlpapi.h PinmemberApollo810:27 23 Sep '08  
GeneralRe: errors with iphlpapi.h Pinmembersend_jk20:55 10 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 1 Oct 2002
Article Copyright 2002 by Nico Cuppen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid