Solved

How do I get the TCP/IP subnet mask?

Posted on 1997-06-23
16
1,005 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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