How does Winipcfg get IP addresses

I would like some detailed workable Win32 'C' code of how to get my IP address when connected to the Internet, and I want to take into account that my computer may or may not have a network card !!!!!!!!

Just like Windows winipcfg proggy does????????
haastAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
For a DIAL-UP connection, this code will get information about the currently established DUN connection.  YOUR IP address is returned in this line:

strcpy(hostip, prasip->szIpAddress);
       ^^^^^^



//
// Check the state of any RAS connection
//
char *CWindow::CheckRAS(DWORD *state, char *hostip)
{

RASCONN *ras_buf;
RASPPPIP *prasip;
DWORD sz_ras_buf, numconn, errcd, szrasip;
HRASCONN hrc;
static char rasname[256];

// Allocate space for RASCONN buffers
ras_buf = (RASCONN *)malloc(32 * sizeof(RASCONN));
sz_ras_buf = 32 * sizeof(RASCONN);

ras_buf->dwSize = sizeof(RASCONN);

if((errcd = RasEnumConnections(ras_buf, &sz_ras_buf, &numconn)) != 0){
    *state = RAS_ERROR;
    free(ras_buf);
return NULL;
}

if(numconn > 0){
hrc = ras_buf->hrasconn;

prasip = (RASPPPIP *)malloc(sizeof(RASPPPIP));
szrasip = sizeof(RASPPPIP);
prasip->dwSize = szrasip;

errcd = RasGetProjectionInfo(hrc, RASP_PppIp, prasip, &szrasip);
switch(errcd){

default:
    *state = RAS_ERROR;
    free(ras_buf);
    return NULL;
case 0:
*state = RAS_CONN;
strcpy(rasname, ras_buf->szEntryName);
strcpy(hostip, prasip->szIpAddress);
if(strlen(rasname) > 0){
    free(ras_buf);
     return(rasname);
     }
     else{
         *state = RAS_ERROR;
         free(ras_buf);
         return NULL;
     }

case 731:
    *state = RAS_DIAL;
    strcpy(rasname, ras_buf->szEntryName);
if(strlen(rasname) > 0){
    free(ras_buf);
     return(rasname);
     }
     else{
         *state = RAS_ERROR;
         free(ras_buf);
         return NULL;
     }
}
}
else{
    *state = RAS_NOCONN;
    free(ras_buf);
    return NULL;
}
}
0
 
NickRepinCommented:
It's hard enough, and it's different for NT and 95.
Keep in mind that may be more than one adapter card, more than one RAS (dial-up) connections, DHCP...

It cannot be described with two or two hundred words.

I can write for you working code for 1000 pts at least.
0
 
haastAuthor Commented:
nickrepin

OK, how about setting limits on this 1 ras connection, 1 adapter card win95/98.

Can you help with that scenario ?????

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
NickRepinCommented:
The problem is not in how many cards we have.
To obtain this info we must:

1) enum all adapters
2) enum all RAS connections
3) obtain TCP/IP and DHCP settings for them
These all must be done through registry, here is no API at all!

I have not got any code you want.
I have got some info about registry keys for NT, and have not for 95.

I can find these all, but it's all absolute free for me. Moreover, I'll waste my time. It's indeed very hard job. For me, at least. I'll do it for 1000 pts, not less.

The Qs like yours were asked here with no success (if I'm not wrong).
0
 
haastAuthor Commented:
No DHCP !!
0
 
NickRepinCommented:
I'm sorry, but I really have no time.
The main problem is to enumerate adapters and RAS connections.
May be, other experts can help you...
0
 
CodeMonkeyCommented:
This code sample will give you a list of valid network interfaces that are available to your computer. If you have Dial up networking then your as your new connection becomes established, extra interafaces will appear in the list. You can use the RAS functions to decide if they are Dial up related connections

char szHostname[100];
if( gethostname( szHostname, sizeof( szHostname ))==0)
{
  HOSTENT* pHostEnt=gethostbyname(szHostname);
  if (pHostEnt)
  {
    int nAdapter = 0;
    while ( pHostEnt->h_addr_list[nAdapter] )
    {
      print("%s\n",inet_ntoa( *(IN_ADDR*)(pHostEnt->h_addr_list[nAdapter]));
    }
  }
}
0
 
haastAuthor Commented:
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections

0
 
haastAuthor Commented:
Codemonkey, I realise that the list of IP's in network byte order is here: pHostEnt->h_addr_list[nAdapter], but how do establish which IP is the actual current ISP dynamic IP.

(RAS connection) I have had a brief look at RASCON & RASPPPIP structs and HRASCON. As Nickrepin has suggested, I probably have to enumerate the connections with RasEnumConnections and then RasGetProjectionInfo and also he is right about NT being different to Win95/98.

So this is the partof the question that I need answered !!

thanks,


0
 
NickRepinCommented:
haast, as I know, it's need to enumerate registry entries that relates to RAS connections (their like 'fake' adapters). The addresses in these entries are changes dynamically when RAS connected/disconnected.
0
 
NickRepinCommented:
I can give you some info about that entries in NT, but now I have no info about 95.
0
 
haastAuthor Commented:
NickRepin could you please contact me at: westwebs@ihug.co.nz
0
 
haastAuthor Commented:
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections
0
 
haastAuthor Commented:
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections
0
 
haastAuthor Commented:
jhance, give me a bit of time and i will check it out, thanks
0
 
jhanceCommented:
BTW, Winipcfg does this via undocumented WINSOCK calls.  I've traced some out enough to verify that they are indeed undocumented calls.  

The only other reference I have on this subject which you might also find useful is:

http://www.caip.rutgers.edu/~arni/ws2/stas.htm

but it's techniques don't apply to DUN.

Enjoy...
0
 
CodeMonkeyCommented:
Sorry, I didn't answer but I was away
0
 
CodeMonkeyCommented:
Sorry, I didn't answer but I was away. jhance's answer seems okay
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.