Solved

How do I get the TCP/IP subnet mask?

Posted on 1997-06-23
16
982 Views
Last Modified: 2013-12-03
I am writing a Winsocks program in "C" using the MS Visual C++ compiler and I need to get the subnet mask of the network that the program is executed on.

This program is to be run on many different Win95/WINNT systems. Each system's networking may be configured differently (i.e. some may be using Microsoft's tcp/ip stack, others may use a third party tcp/ip stack, or
some systems may be configured for DHCP).

I am aware that Microsoft's stack stores the subnet mask under:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans\xxxx\IPMask

I assume third party tcp/ip stack vendors may store the subnet mask else where.  Another assumption is, systems configured for DHCP do not store the subnet mask in the registry.  Are these assumptions correct?

There must be an API call to get this information (I find it hard to believe that developers at MicroSoft write registry searches when they need this information).  Does anyone know of an API call to get the subnet mask?

Thanks,

Jim Santmyer
0
Comment
Question by:santj
  • 7
  • 4
  • 4
  • +1
16 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398941
You can obtain ip address and subnet mask from registry only.
For more information, see the MS knowledge base article  Q120642.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Adapter Name>\Parameters\Tcpip\SubnetMask

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Adapter Name>\Parameters\Tcpip\DhcpSubnetMask
0
 

Author Comment

by:santj
ID: 1398942
Wrong answer :). I have since found an API call that will provide me the subnet mask on Win95 and WinNT platforms.  

I expected a better answer then RTFM from an "expert".  I also expected a correct answer.  Since you like to read the manual I will give you a hint were to find the correct answer, start with  ioctl calls.  
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398943
Well, I am interesting in your answer. Some time ago I've used the registry to obtain IP address to solve some problems. Unfortunately, I have no time to look for API call you mentioned.
I have only 125 'points' to ask the question. And if you will be so kind to answer to MY question about subnet mask, I'll give you them all with grade 'A'.

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398944
Also, please tell me why my answer is wrong. Am I wrong with API call, or I am wrong at all?
Also, does your API call cares about multihomed systems with multiply IP addresses?
0
 

Author Comment

by:santj
ID: 1398945
I have posted the answer to the news groups:

microsoft.public.win32.programmer.networks,
comp.os.ms-windows.programmer.networks,
comp.os.mis-windows.programmer.misc
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398946
I've reviewed the discussion in microsoft...networks newsgroup right now. And I see now that you was unjust with me.
My anwer was right and still right.
Other methods to obtain the subnet mask are non-portable (now at least).

As to ipconfig, this program obtains the subnet mask from registry only, not from any API call.
0
 
LVL 3

Accepted Solution

by:
vinniew earned 200 total points
ID: 1398947
Nick was right.  Even if you look at the Vxd level, MS drivers even use the registry to get the mask.  It's a configuration setting, not part of the SNMP protocol.  Check the RFC if you don't believe me.

0
 
LVL 1

Expert Comment

by:stas
ID: 1398948
Hi , Santj!

As part of this descussion you are point to news groups. I am lost this information. Could you resend me it (stas@vdo.net).

Thank you
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:vinniew
ID: 1398949
Ioctl calls point to vxd's.


0
 

Author Comment

by:santj
ID: 1398950
How do you get the IP address and/or subnetmask on a system that is configured for PPP and dynamic address assignment?  Or a system configured to use DHCP to get it's address and subnet mask?  In either of these cases I do not think this information is in the registy.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398951
Although you DON'T like to read the manual  :),
you may find this information in knowledge base article mentioned in my rejected answer.
Registry entries are dynamically updating by system while PPP link is established/dropped. It's true for DHCP also.

'I expected a better answer then RTFM from an "expert" '.
Well, only an expert can grade the another expert's answer  ;)
0
 

Author Comment

by:santj
ID: 1398952
Wrong again :-). As I write this comment I am using a dial-up PPP connection with the IP address dynamiclly assigned.  A search of the registry does not find my IP address.  The only IP addresses in the registry are the default gateway, the name server and an entry in

           HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans\xxxx\IPAddress

which is set to 0.0.0.0.  The IPMask is set to 0.0.0.0 also, yet when I run winipcfg, it shows my ip address and subnet mask.

I do not claim to be an expert, but I do verify my answers to be correct before responding :).  

Thinking about your lastest answer, I wonder how the registry is updated dynamiclly with the IP address, if it is not currently know by the system.  It must be stored some place else, possibly in the IP stack?  In which case there must be a way to get the IP address without using the registy.
0
 
LVL 3

Expert Comment

by:vinniew
ID: 1398953
socket()
bind()

*THAT* gets your IP.
0
 
LVL 3

Expert Comment

by:vinniew
ID: 1398954
That uses the installed IP stack, too, BTW.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398955
Santj,

try this.

Run PPP connection.

Run ipconfig.exe.

It writes (for example):

Token Ring adapter tl3c3x91:

        IP Address. . . . . . . . . : 194.84.100.71
        Subnet Mask . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . : 194.84.100.101


Now look for this key in registry and see your dynamically assigned ip address and mask!

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Adapter Name>\Parameters\Tcpip\

(<Adapter name> is 'tl3c3x91'in my case)

When PPP connection is dropped, all values are 0.

I've checked my previous answer before I submitted it.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398956
P.S. It's possible to have got an adapter and PPP connection at the same time. In this case there will be a two adapters in your PC - one is real and second is virtual. And you should check two registry keys in this case (refer to ipconfig.exe)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now