SSF.Net.Util
Class cidrBlock

java.lang.Object
  |
  +--SSF.Net.Util.cidrBlock

public class cidrBlock
extends java.lang.Object

Tree of CIDR blocks, constructed from a Net configuration using the optional "cidr" attributes provided for Net and link attributes. Each CIDR block in this tree represents one of three things: a Net, a link (ptp or LAN), or a placeholder block that doesn't correspond to any DML attribute, containing multiple link sub-blocks.

The block tree does the bookkeeping to maintain three distinct address maps. The NHI map addresses each network, host, and interface as N1:N2:N3:{...}Nn:H(I). The CIDR map addresses each network and link by its CIDR string, L1/L2/L3../Ln. The IP map addresses each network and link by the IP prefix associated with its CIDR block, and each interface of each host by an IP address offset from the prefix assigned to the link in which it appears as an attached interface.

To construct the three maps together, it suffices to construct a single CIDR block, passing the root network's dmlConfig and an IP prefix within which addresses are to be assigned. The code will make one pass over the DML to create the CIDR block tree and NHI map, another to compute the required address blocksizes for the IP map, and a third to assign IP prefixes and addresses.

The cidrBlock tree then serves as a translation table for all three maps, using utility functions {nhi,ip,cidr}_to_{nhi_ip_cidr}, each of which takes a String as an argument and returns another String.

If "cidr" attributes are partially or totally missing, the code will attempt to automatically supply numbers starting from CIDR_BASE. This is not foolproof, because of the amount of checking that would be involved (for example, the need for an additional pass to create a "reserved CIDR block map"). It's better to either uniformly supply CIDR addresses, or uniformly omit them.

BUGS


Nested Class Summary
(package private)  class cidrBlock.blockList
           
 
Field Summary
protected  cidrBlock ancestorBlock
          CIDR block where defined
protected  com.renesys.raceway.DML.Configuration blockConfig
          Cached configuration of the Net or link for which this is the address block descriptor.
static int CIDR_AUTOMATIC
           
static int CIDR_BASE
          CIDR number from which we number default sub-blocks: e.g., 0 or 1
static int CIDR_IN_USE
          Three-way flag for CIDR addressing.
static int CIDR_INDETERMINATE
           
 int cidr_number
          CIDR block number for this address block descriptor.
 java.lang.String cidr_prefix
          Global CIDR block address for this descriptor.
static java.lang.String CIDR_SEPARATOR
          Separator string for CIDR addresses
static int CIDR_status
           
static int CIDR_WIDTH
          Number of address bits that make up the entire address space
protected  java.util.Hashtable interfaces
          Table of interfaces for hosts that appear in this subnet.
static int IP_AUTOMATIC
           
static int IP_IN_USE
          Three-way flag for IP addressing.
static int IP_INDETERMINATE
           
 int ip_prefix
          IP address prefix assigned to this block.
 int ip_prefix_length
          Number of prefix bits in the local block of reserved addresses.
static int IP_status
           
protected  int nextCidr
          Next CIDR block number to be auto-assigned within this address block.
 int nhi_number
          NHI Network Address final component
 java.lang.String nhi_prefix
          NHI Network Address
static java.lang.String NHI_SEPARATOR
          Separator string for NHI addresses
protected  cidrBlock parentBlock
          CIDR block one level up from this one.
 int required
          Number of addresses required by this block, including the reserved net and broadcast addresses.
static boolean strict
          Set 'strict' to false to disable many CIDR/IP rules.
protected  java.util.Hashtable subblocks
          Table of CIDR sub-blocks.
protected  java.util.Hashtable subnets
          Table of sub-Nets.
 
Constructor Summary
private cidrBlock()
          Default constructor for an uninitialized CIDR block.
private cidrBlock(com.renesys.raceway.DML.Configuration cfg, int use_netid, cidrBlock ancestor)
          Internal constructor for an internal CIDR block, given a DML configuration, a network ID number to use, and a reference to the cidrBlock representing the Net in which this Net or Link was defined.
  cidrBlock(com.renesys.raceway.DML.Configuration cfg, java.lang.String use_IP)
          Constructor for a top-level CIDR block, given a DML configuration describing one outermost Net, which may not have an ID or a CIDR block number.
 
Method Summary
private  void addLinks(java.util.Enumeration linkset)
          Create CIDR blocks for all links defined within a Net, and establish their CIDR addresses.
private  void addLoopbacks(java.util.Enumeration ifs)
          Create CIDR blocks for virtual loopback links, defined implicitly by the existence of virtual interfaces within a Net, and establish their CIDR addresses.
private  void addSubnets(java.util.Enumeration netset)
          Create CIDR blocks for all contained Nets, establish their CIDR addresses, and recurse to allow them to do the same.
private  void assignPrefix(int baseaddr)
          Perform IP prefix assignment over the tree of block descriptors.
 java.util.Enumeration cidr_children()
          Return the CIDR blocks in the CIDR level underneath this one.
static java.lang.String cidr_concat(java.lang.String pre, int post)
          Concatenate the given CIDR addresses.
 cidrBlock cidr_parent()
          Return the CIDR block which is one CIDR level up from this one.
private  void computePrefix()
          Compute the IP prefix for each CIDR block in the tree.
 java.lang.String configCIDR(com.renesys.raceway.DML.Configuration cfg, int offset)
          Configure the CIDR adresses of the given link or subnet.
 java.lang.String configIP(com.renesys.raceway.DML.Configuration cfg, int offset)
          Configure the IP adresses of the given link or subnet.
 boolean contains(int addr)
          Check if the address is inside the CIDR block.
 boolean contains(int addr, int preflen)
          Check if the address is inside the CIDR block.
 java.lang.String defined_in_net()
          Return the NHI address of the network in which this CIDR block (Net or Link) was defined.
 void dump_by_cidr()
           
private  void dump_by_cidr(boolean root)
           
 void dump_by_subnet()
           
private  void dump_by_subnet(boolean root)
           
private  void format(java.lang.String s1, int w1)
           
private  void formatln(java.lang.String s1, int w1)
           
 java.lang.Object getUDA(java.lang.String key)
          Retreive the value of a user-defined attribute from this CIDR block descriptor.
 java.lang.Object getUDA(java.lang.String key, java.lang.String kval, boolean flowdown, boolean override, java.lang.String concatString)
          Retreive the value of a user-defined attribute from this CIDR block descriptor.
private  void insertBlock(java.lang.String cidrspec, cidrBlock newblock)
          Insert a CIDR block into the CIDR tree of which this CIDR block is the root.
 java.lang.String ip_to_nhi(int ipaddr, int ipprefix)
          IP address to NHI address.
 java.lang.String ip()
           
static void main(java.lang.String[] argv)
           
private  void manual_IP_assignment()
          Take the modeler at their word; assign IP addresses to interfaces according to the IP block addresses provided in the 'Net' and 'link' attributes
private static void merge_hashtables(java.util.Hashtable dst, java.util.Hashtable src)
          Merge two Hashtables.
 com.renesys.raceway.DML.Configuration networkConfiguration()
           
 java.util.Enumeration nhi_children()
          Return the CIDR blocks corresponding to subnets within this one.
static java.lang.String nhi_concat(java.lang.String pre, int post)
          Concatenate the given NHI addresses.
static java.lang.String nhi_concat(java.lang.String pre, java.lang.String post)
          Concatenate the given NHI addresses.
static java.lang.String nhi_host_interface(java.lang.String naddr)
          Return 'host(interface)' part of an NHI address: "N1:N2:N3:H(I)" --> "H(I)"
static java.lang.String nhi_host(java.lang.String naddr)
          Return 'host' part of an NHI address: "N1:N2:N3:H(I)" --> "H"
static java.lang.String nhi_interface(java.lang.String naddr)
          Return 'interface' part of an NHI address: "N1:N2:N3:H(I)" --> "I"
static java.lang.String nhi_net_host(java.lang.String naddr)
          Return 'network:host' part of an NHI address: "N1:N2:N3:H(I)" --> "N1:N2:N3:H"
static java.lang.String nhi_net(java.lang.String naddr)
          Return 'network' part of an NHI address: "N1:N2:N3:H(I)" --> "N1:N2:N3"
 cidrBlock nhi_parent()
          Return the CIDR block corresponding to the Net in which this block was defined: not necessarily the parent when multilevel CIDR strings are in use.
 java.lang.String nhi_to_cidr(java.lang.String nhi_address)
          NHI address to CIDR address
 java.lang.String nhi_to_ip(java.lang.String nhi_address)
          NHI address to IP address.
 cidrBlock nhi(java.lang.String naddr)
          NHI address to CIDR block.
 int reserved()
          Utility function to compute number of addresses reserved in block.
private  void setCIDR(cidrBlock newBlock, java.lang.Integer blocknum)
          Insert a CIDR block in the current block.
 java.lang.String toString()
           
 void updateCidrBlock(com.renesys.raceway.DML.Configuration cfg)
          Update the IP adresses of all interface Called after a modification in the tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CIDR_BASE

public static final int CIDR_BASE
CIDR number from which we number default sub-blocks: e.g., 0 or 1

See Also:
Constant Field Values

CIDR_WIDTH

public static final int CIDR_WIDTH
Number of address bits that make up the entire address space

See Also:
Constant Field Values

NHI_SEPARATOR

public static final java.lang.String NHI_SEPARATOR
Separator string for NHI addresses

See Also:
Constant Field Values

CIDR_SEPARATOR

public static final java.lang.String CIDR_SEPARATOR
Separator string for CIDR addresses

See Also:
Constant Field Values

cidr_number

public int cidr_number
CIDR block number for this address block descriptor.


cidr_prefix

public java.lang.String cidr_prefix
Global CIDR block address for this descriptor.


nhi_number

public int nhi_number
NHI Network Address final component


nhi_prefix

public java.lang.String nhi_prefix
NHI Network Address


ip_prefix

public int ip_prefix
IP address prefix assigned to this block.


ip_prefix_length

public int ip_prefix_length
Number of prefix bits in the local block of reserved addresses.


strict

public static boolean strict
Set 'strict' to false to disable many CIDR/IP rules. If 'strict' is true, you may not specify CIDR and IP in the same file, and you must be ruthlessly uniform in your application or omission of CIDR or IP attributes.


CIDR_IN_USE

public static final int CIDR_IN_USE
Three-way flag for CIDR addressing. CIDR_IN_USE signifies that the modeler wishes to specify explicit CIDR addresses, and their omission should be flagged as an error. CIDR_AUTOMATIC signifies that the modeler wishes to omit explicit CIDR addresses and let the framework choose them automatically, flagging "cidr" attributes as errors. CIDR_INDETERMINATE signifies that the decision has not yet been made. It is an error to manually specify both CIDR and IP in the same model.

See Also:
Constant Field Values

CIDR_AUTOMATIC

public static final int CIDR_AUTOMATIC
See Also:
Constant Field Values

CIDR_INDETERMINATE

public static final int CIDR_INDETERMINATE
See Also:
Constant Field Values

CIDR_status

public static int CIDR_status

IP_IN_USE

public static final int IP_IN_USE
Three-way flag for IP addressing. IP_IN_USE signifies that the modeler wishes to specify explicit IP addresses, and their omission should be flagged as an error. IP_AUTOMATIC signifies that the modeler wishes to omit explicit IP addresses and let the framework choose them automatically, flagging "ip" attributes as errors. IP_INDETERMINATE signifies that the decision has not yet been made. It is an error to manually specify both CIDR and IP in the same model.

See Also:
Constant Field Values

IP_AUTOMATIC

public static final int IP_AUTOMATIC
See Also:
Constant Field Values

IP_INDETERMINATE

public static final int IP_INDETERMINATE
See Also:
Constant Field Values

IP_status

public static int IP_status

blockConfig

protected com.renesys.raceway.DML.Configuration blockConfig
Cached configuration of the Net or link for which this is the address block descriptor. If blockConfig is null, this is a 'placeholder' cidrBlock created by a link or net with a multilevel cidr attribute; it does not correspond to any particular link or net.


subnets

protected java.util.Hashtable subnets
Table of sub-Nets. Key is Net.id (Integer); value is instance of cidrBlock corresponding to that sub-Net ID, if one exists.


interfaces

protected java.util.Hashtable interfaces
Table of interfaces for hosts that appear in this subnet. Key is relative NHI spec (String, "H(I)"), and value is a reference to the attachSpec describing the link.


ancestorBlock

protected cidrBlock ancestorBlock
CIDR block where defined


parentBlock

protected cidrBlock parentBlock
CIDR block one level up from this one.


subblocks

protected java.util.Hashtable subblocks
Table of CIDR sub-blocks. Key is Integer; value is an instance of cidrBlock corresponding to that CIDR ID at this level.


nextCidr

protected int nextCidr
Next CIDR block number to be auto-assigned within this address block.


required

public int required
Number of addresses required by this block, including the reserved net and broadcast addresses. May be smaller than reserved() because of blocksize rounding requirements.

Constructor Detail

cidrBlock

public cidrBlock(com.renesys.raceway.DML.Configuration cfg,
                 java.lang.String use_IP)
          throws com.renesys.raceway.DML.configException
Constructor for a top-level CIDR block, given a DML configuration describing one outermost Net, which may not have an ID or a CIDR block number. Its ID and CIDR block number are both zero.

Parameters:
cfg - The Net configuration
use_IP - The IP adress to use for this Net.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

cidrBlock

private cidrBlock(com.renesys.raceway.DML.Configuration cfg,
                  int use_netid,
                  cidrBlock ancestor)
           throws com.renesys.raceway.DML.configException
Internal constructor for an internal CIDR block, given a DML configuration, a network ID number to use, and a reference to the cidrBlock representing the Net in which this Net or Link was defined. Only the NHI addressing is established here; CIDR addressing is established externally (in addSubnets() and addLinks()), and IP addressing is handled later via tree traversal from top-level.

Parameters:
cfg - The subNet configuration
use_netid - The id of this subnet.
ancestor - The CIDR block that is at the top level.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

cidrBlock

private cidrBlock()
Default constructor for an uninitialized CIDR block.

Method Detail

ip

public java.lang.String ip()

ip_to_nhi

public java.lang.String ip_to_nhi(int ipaddr,
                                  int ipprefix)
IP address to NHI address.

Parameters:
ipaddr - The IP address to translate
ipprefix - The IP prefix
Returns:
The NHI address

nhi

public cidrBlock nhi(java.lang.String naddr)
NHI address to CIDR block. If the nhi address specifies an interface, return the CIDR block corresponding to the link in which that interface is attached; otherwise, return the CIDR block corresponding to the specified net. Note: nhi("1:2") is "host 2 in net 1", not "net 2 of net 1" --- the final component is always interpreted as a host with an optional interface specification following.

Parameters:
naddr - The NHI adress of a link or a subnet.
Returns:
the CIDR block of the link or the subnet.

nhi_to_cidr

public java.lang.String nhi_to_cidr(java.lang.String nhi_address)
NHI address to CIDR address

Parameters:
nhi_address - The NHI address to translate
Returns:
the CIDR adress.

nhi_to_ip

public java.lang.String nhi_to_ip(java.lang.String nhi_address)
NHI address to IP address.

Parameters:
nhi_address - The NHI address to translate
Returns:
the IP adress

defined_in_net

public java.lang.String defined_in_net()
Return the NHI address of the network in which this CIDR block (Net or Link) was defined.

Returns:
The NHI address of the network

nhi_concat

public static java.lang.String nhi_concat(java.lang.String pre,
                                          java.lang.String post)
Concatenate the given NHI addresses.

Parameters:
pre - The prefix of the NHI address
post - The end of the NHI address
Returns:
the concatenated NHI address

nhi_concat

public static java.lang.String nhi_concat(java.lang.String pre,
                                          int post)
Concatenate the given NHI addresses.

Parameters:
pre - The prefix of the NHI address
post - The end of the NHI address
Returns:
the concatenated NHI address

cidr_concat

public static java.lang.String cidr_concat(java.lang.String pre,
                                           int post)
Concatenate the given CIDR addresses.

Parameters:
pre - The prefix of the CIDR address
post - The end of the CIDR address
Returns:
the concatenated CIDR address

nhi_net

public static java.lang.String nhi_net(java.lang.String naddr)
Return 'network' part of an NHI address: "N1:N2:N3:H(I)" --> "N1:N2:N3"

Parameters:
naddr - The NHI address
Returns:
The 'network' part of the NHI address

nhi_net_host

public static java.lang.String nhi_net_host(java.lang.String naddr)
Return 'network:host' part of an NHI address: "N1:N2:N3:H(I)" --> "N1:N2:N3:H"

Parameters:
naddr - The NHI address
Returns:
The 'network:host' part of the NHI address

nhi_host_interface

public static java.lang.String nhi_host_interface(java.lang.String naddr)
Return 'host(interface)' part of an NHI address: "N1:N2:N3:H(I)" --> "H(I)"

Parameters:
naddr - The NHI address
Returns:
The 'host(interface)' part of the NHI address

nhi_interface

public static java.lang.String nhi_interface(java.lang.String naddr)
Return 'interface' part of an NHI address: "N1:N2:N3:H(I)" --> "I"

Parameters:
naddr - The NHI address
Returns:
The 'interface' part of the NHI address

nhi_host

public static java.lang.String nhi_host(java.lang.String naddr)
Return 'host' part of an NHI address: "N1:N2:N3:H(I)" --> "H"

Parameters:
naddr - The NHI address
Returns:
The 'host' part of the NHI address

cidr_parent

public cidrBlock cidr_parent()
Return the CIDR block which is one CIDR level up from this one.

Returns:
The parent CIDR block.

nhi_parent

public cidrBlock nhi_parent()
Return the CIDR block corresponding to the Net in which this block was defined: not necessarily the parent when multilevel CIDR strings are in use.

Returns:
The CIDR block corresponding to the Net in which this block was defined.

cidr_children

public java.util.Enumeration cidr_children()
Return the CIDR blocks in the CIDR level underneath this one.

Returns:
The CIDR blocks underneath this one.

nhi_children

public java.util.Enumeration nhi_children()
Return the CIDR blocks corresponding to subnets within this one.

Returns:
The CIDR blocks corresponding to subnets

networkConfiguration

public com.renesys.raceway.DML.Configuration networkConfiguration()

reserved

public final int reserved()
Utility function to compute number of addresses reserved in block.

Returns:
The number of addresses reserved in block.

contains

public boolean contains(int addr,
                        int preflen)
Check if the address is inside the CIDR block.

Parameters:
addr - The address to check
preflen - The prefix length of the address
Returns:
true if the adress is inside the CIDR block

contains

public boolean contains(int addr)
Check if the address is inside the CIDR block.

Parameters:
addr - The address to check
Returns:
true if the adress is inside the CIDR block

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

addSubnets

private void addSubnets(java.util.Enumeration netset)
                 throws com.renesys.raceway.DML.configException
Create CIDR blocks for all contained Nets, establish their CIDR addresses, and recurse to allow them to do the same.

Parameters:
netset - The set of subnets.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

addLinks

private void addLinks(java.util.Enumeration linkset)
               throws com.renesys.raceway.DML.configException
Create CIDR blocks for all links defined within a Net, and establish their CIDR addresses.

Parameters:
linkset - The enumeration of links.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

addLoopbacks

private void addLoopbacks(java.util.Enumeration ifs)
                   throws com.renesys.raceway.DML.configException
Create CIDR blocks for virtual loopback links, defined implicitly by the existence of virtual interfaces within a Net, and establish their CIDR addresses.

Parameters:
ifs - The enumeration of virtual loopback links.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

merge_hashtables

private static void merge_hashtables(java.util.Hashtable dst,
                                     java.util.Hashtable src)
Merge two Hashtables. The second is merged in the first one.

Parameters:
dst - The first Hashtable
src - The second Hashtable

setCIDR

private void setCIDR(cidrBlock newBlock,
                     java.lang.Integer blocknum)
Insert a CIDR block in the current block.

Parameters:
newBlock - The CIDRBlock to insert.

insertBlock

private void insertBlock(java.lang.String cidrspec,
                         cidrBlock newblock)
                  throws com.renesys.raceway.DML.configException
Insert a CIDR block into the CIDR tree of which this CIDR block is the root. This may require recursion and traversal if the modeler has specified multilevel CIDR addresses. If there already exists a placeholder block with the same CIDR number, merge with it.

Parameters:
cidrspec - The CIDR adress of the new block.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

manual_IP_assignment

private void manual_IP_assignment()
                           throws com.renesys.raceway.DML.configException
Take the modeler at their word; assign IP addresses to interfaces according to the IP block addresses provided in the 'Net' and 'link' attributes

Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

computePrefix

private void computePrefix()
                    throws com.renesys.raceway.DML.configException
Compute the IP prefix for each CIDR block in the tree.

com.renesys.raceway.DML.configException

assignPrefix

private void assignPrefix(int baseaddr)
                   throws com.renesys.raceway.DML.configException
Perform IP prefix assignment over the tree of block descriptors. For best address packing, at each level sort the list of blocks into descending order, and allocate largest blocks at the low end of the available address range, smallest blocks at the high end.

Parameters:
baseaddr - The base adress for IP adresses.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

formatln

private void formatln(java.lang.String s1,
                      int w1)

format

private void format(java.lang.String s1,
                    int w1)

dump_by_subnet

public void dump_by_subnet()

dump_by_subnet

private void dump_by_subnet(boolean root)

dump_by_cidr

public void dump_by_cidr()

dump_by_cidr

private void dump_by_cidr(boolean root)

configIP

public java.lang.String configIP(com.renesys.raceway.DML.Configuration cfg,
                                 int offset)
                          throws com.renesys.raceway.DML.configException
Configure the IP adresses of the given link or subnet.

Parameters:
cfg - The configuration of the link or the subnet
offset - The offset for IP adresses
Returns:
the IP adress of the link or subnet.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

configCIDR

public java.lang.String configCIDR(com.renesys.raceway.DML.Configuration cfg,
                                   int offset)
                            throws com.renesys.raceway.DML.configException
Configure the CIDR adresses of the given link or subnet.

Parameters:
cfg - The configuration of the link or the subnet
offset - The offset for CIDR adresses
Returns:
the CIDR adress of the link or subnet.
Throws:
com.renesys.raceway.DML.configException - if a DML configuration error occurs.

getUDA

public java.lang.Object getUDA(java.lang.String key)
                        throws com.renesys.raceway.DML.configException
Retreive the value of a user-defined attribute from this CIDR block descriptor. If the attribute is omitted, attempt to use the default value specified in the containing network; otherwise, the local value overrides the default. Return single attribute values, not a concatenation of parent and child values leading back to the root.

com.renesys.raceway.DML.configException

getUDA

public java.lang.Object getUDA(java.lang.String key,
                               java.lang.String kval,
                               boolean flowdown,
                               boolean override,
                               java.lang.String concatString)
                        throws com.renesys.raceway.DML.configException
Retreive the value of a user-defined attribute from this CIDR block descriptor.

Parameters:
key - Attribute name to search for.
kval - Attribute value that must appear (null if any value okay)
flowdown - If true, allow parent network values to flow down as defaults for subnets that fail to specify the attribute.
override - If true, allow specified values to override any default value that flows down from a parent network; otherwise raise an error if both parent and child specify the attribute.
concatString - if non-null, return the parent value, plus the given concatenation string, plus the local value.
com.renesys.raceway.DML.configException

main

public static void main(java.lang.String[] argv)

updateCidrBlock

public void updateCidrBlock(com.renesys.raceway.DML.Configuration cfg)
                     throws com.renesys.raceway.DML.configException
Update the IP adresses of all interface Called after a modification in the tree. CHANGED BY NIST : Added for recomputation of IP addresses.

Parameters:
cfg - The configuration that contains the links.
com.renesys.raceway.DML.configException