Solved

How do I get the TCP/IP subnet mask?

Posted on 1997-06-23
16
991 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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