Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > Cisco > A kind of arithmetic for subnetting and ip address management

Thread Tools

A kind of arithmetic for subnetting and ip address management

Posts: n/a
A kind of arithmetic for subnetting and ip address management
Written by

This document describes a kind of data structure and arithmetic for
subnetting. With this arithmetic, the required subnet can be quickly
located in a network with complicated hiberarchy.
Because network segment is identified by IP address and mask, such as And IP address and mask is represented by 32 bits
Binary. The binary tree can be used to describe the net structure and
save IP address management information.
Eg. A organization has the network segment, and there
are two departments( Department A and Department B) under this
organization. Department A was assigned subnet and B was
assigned subnet,the network structure chart is as

Below is the IP address management information structure chart
represented by binary tree :

The data structure of each node is as below:
struct SNetdata
string sIP; //subnet ID, identified by IP address and mask, such as
int iNodeType;
string sLeftIP; // left subtree node, left subtree subnet ID
string sRightIP; //right subtree node, right subtree subnet ID
string sParentIP; //parent node, parent subnet ID
There are four types of node: used, unused, middle node, root node, as
USEDNODE=1, //used node
UNUSEDNODE=2, //unused node
MIDDLENODE=3, //middle node, which is generated during the course of
ROOTNODE=4, //root node

IP address management information is saved in these nodes. All these
nodes can be linked by double linked list. Considering the subnet ID
will be frequently used to perform searching in the IP address
management dataset, thus the map of STL is used to save each node and
subnet ID is used as index key in order to quicken the searching speed.
typedef map<string,SNetdata,less<string> > MAPNetData;
MAPNetData NetDatas;

Assumed that the organization is newly added Department C and
Department D. C is needed to assign a 25-bits mask subnet and D is
needed to assign a 24-bits mask subnet.
The general IP address management software and subnet calculator can't
solve this problem. With the binary tree data structure, the problem
can be solved easily. For the first problem, to assign a 25-bits mask
subnet for Department C, we preorder traversal the binary tree
beginning from the root node, to find the leaf node of
unused type and the mask less or equal 25. In this example, we can find, which is the subnet assigned for Department C.

Traversal arithmetic is as below:
int TraversalTree(string StartIP, int iTarMask, string& ResIP)
StartIP: represents the subnet ID of the beginning node when preorder
traversalling the binary tree. In this example it is root
iTarMask: represents the mask of the target node we are searching for
ResIP: represents the subnet ID of the target node we have found
MAPNetData::iterator MyIterator;
SNetdata NetData;
int iSrcMask; // represents the mask passing the node when traversal
the tree
int iRes,k;
string tmpstr;

return -1;


return 3;

return 1; // Have found it
return 2;

return iRes;

iRes=TraversalTree(NetData.RightIP,iTarMask,ResIP) ;
return iRes;

return 0;

Then for the second problem, to assign a 24-bits mask subnet for
Department D. Following the above mentioned arithmetic to traversal the
binary tree, we can find Cause the assigned subnet is
24-bits mask, we need to perform subnetting under this node. Then we
will get, which is the subnet we needed. After the
assigning, the IP address management information structure chart
represented by binary tree is as below:

For the details, see

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Address of array behavior in pointer arithmetic joshc C Programming 16 06-15-2006 10:14 PM
A kind data structure and arithmetic for subnetting Hell Cisco 0 05-01-2006 02:36 AM
How to manage IP address and how to perform subnetting, i have a good idea joe tomson Cisco 1 03-24-2006 03:03 AM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Arithmetic on function address Stephen Biggs C Programming 21 04-28-2004 11:17 AM