?
Solved

IP adreses calc from subnet mask

Posted on 2009-03-30
7
Medium Priority
?
1,875 Views
Last Modified: 2013-11-23
How to write procedure where I can from example 192.168.0.0/24 get all IP adress 192.168.0.1- 192.168.0.255 into stringlist. where is mask is 24 there is simple but how to make where ip with mask is 213.21.192.0/18 or 192.168.0.30/28
0
Comment
Question by:KarlisB
[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
  • 5
  • 2
7 Comments
 
LVL 3

Author Comment

by:KarlisB
ID: 24026123
Maybe someone can give procedure to make like :
http://www.aboutmyip.com/AboutMyXApp/SubnetCalculator.jsp?ipAddress=192.168.0.0&cidr=20

and calculate first IP address and last IP address
0
 
LVL 7

Expert Comment

by:ahalya
ID: 24042094
I am not conversant in IP traffic, but it appears the number after the slash shows you the number of bit to turn on in the mask.

16 -> Mask: 255.255.0.0
17-> Mask: 255.255.128.0

I'll try to implement an algorithm to give you a list. (Correct me if my interpretation is wrong).

0
 
LVL 7

Accepted Solution

by:
ahalya earned 2000 total points
ID: 24042865
Here is the code to list the IP in tha range given the IP & the mask bits.  I have simply used logical operands to expand the list.

Call the 'GetIPList' function with two arguments: IP-v4 address, and the MaskBits.

e.g., GetIPList('192.168.0.0', 22);


type TIPSegments = array[1..4] of byte;
 
function NumberToSegments(Number:longword):TIPSegments;
 
var j : 0..4;
 
begin;
  for j := 4 downto 1 do
  begin;
    result[j] := Number mod 256;
    Number := Number div 256;
  end;
end;
 
function IPToSegments(ip:string):TIPSegments;
 
var i : byte;
    j : 0..4;
    s : string;
 
begin;
  for j := 1 to 4 do
  begin;
    i := pos('.', ip);
    if j < 4 then s := copy(ip, 1, i-1) else s := ip;
    delete(ip, 1, i); //remove '.'
    result[j] := StrToInt(s);
  end;
end;
 
 
function IPToNumber(ip:string):longword;
 
var segment : TIPSegments;
    i : 1..4;
 
begin;
  segment := IPToSegments(ip);
  result := 0;
  for i := 1 to 4 do
    result := result * 256 + segment[i];
end;
 
function NumberToIP(Number:longword):string;
 
var segment : TIPSegments;
    i : 1..4;
 
begin;
  result := '';
  segment := NumberToSegments(Number);
  result := Format('%d.%d.%d.%d', [segment[1], segment[2], segment[3], segment[4]]);
end;
 
 
procedure TForm4.GetIPList(IPStr:string; MaskBits:byte);
 
var
  IP   :LongWord;
  i, w : longword;
 
begin;
  IP := IPToNumber(IPStr);
 
  WildCard := 1 shl (32 - MaskBits) - 1;
  NetMask := not WildCard;
 
  leWildCard.text := NumberToIP(WildCard);
  leNetMask.text  := NumberToIP(NetMask);
 
  ListBox1.Items.Clear;
  for w := 1 to WildCard - 1 do
  begin;
    i := IP or w;
    ListBox1.Items.Add(NumberToIP(i));
  end;
  leCount.text    := IntToStr(ListBox1.Items.Count);
end;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:ahalya
ID: 24042994
by the way, it appears that you'd need a Stringlist with duplicates set to dupIgnore, instead of a ListBox to make this work for any generic IP address.

0
 
LVL 3

Author Comment

by:KarlisB
ID: 24046891
please give what type of variables is:

Wildcard
NetMask
IeWildcard

bacause I have:

[Error] Unit2.pas(106): Undeclared identifier: 'WildCard'
[Error] Unit2.pas(107): Undeclared identifier: 'NetMask'
[Error] Unit2.pas(109): Undeclared identifier: 'leWildCard'
0
 
LVL 7

Expert Comment

by:ahalya
ID: 24048094
Type for WildCard and NetMask is LongWord.  Anything where name starts with le* is just a TEdit which I used to display the calculated value.

Just to give a quick comment about what I am doing:

1. Convert the IP address in to an integer (There are four 'segments' each going from 0 to 255, i.e. 8 bits. so you need a total of 32 bits hence the LongWord  -for IP-v4)

2. WildCard is an integer correspoding to the Mask.

3. NetMask is the (binary) inverse of the WildCard which has to be 'Logical-OR'ed with the original IP to get the list.



0
 
LVL 7

Expert Comment

by:ahalya
ID: 24048116
My wording isn't clear above:

2.  WildCard corresponds to the number of bits in the Mask, and that is the one you have to "Logical-OR' with the IP to get the list.

3.  NetMask is the binary inverse of WildCard, and gives you the 'SubNet Mask' in the traditional form (e.g. 255.255.255.0).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

777 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