View Code       File Name: ipExtension.tcl
Library Header
Library Name: ::ipExtension::
Author(s)
  Aamer Akhter / [email protected]
Support Alias
  [email protected]
Purpose
  an extension to tcllib ip library
Description
 

A detailed description of the functionality provided by the library.

Usage
 
Requirements
 

package require logger
package require ip
Variables
 

namespace   ::ipExtension::
id:         CVS ID: keyword extraction
version:    current version of package
packageDir: directory where package is located
log:        instance log
Notes
 

1.
Top

Procedures Summary
  ::ip::prefixToNative  
    convert from dotted from to native (hex) form  
  ::ip::nativeToPrefix  
    convert from native (hex) form to dotted form  
  ::ip::intToString  
    convert from an integer/hex to dotted form  
  ::ip::toInteger  
    convert dotted form ip to integer  
  ::ip::toHex  
    convert dotted form ip to hex  
  ::ip::maskToInt  
    convert mask to integer  
  ::ip::broadcastAddress  
    return broadcast address given prefix  
  ::ip::maskToLength  
    converts dotted or integer form of mask to length  
  ::ip::lengthToMask  
    converts mask length to dotted mask form  
  ::ip::nextNet  
    returns next an ipaddress in same position in next network  
  ::ip::isOverlap  
    checks to see if prefixes overlap  
  ::ip::isOverlapNative  
    checks to see if prefixes overlap (optimized native form)  
  ::ip::ipToLayer2Multicast  
    converts ipv4 address to a layer 2 multicast address  
  ::ip::ipHostFromPrefix  
    gives back a host address from a prefix  
  ::ip::reduceToAggregates  
    finds nets that overlap and filters out the more specifc nets  
  ::ip::longestPrefixMatch  
    given host IP finds longest prefix match from set of prefixes  
  ::ip::cmpDotIP  
    helper function for dotted ip address for use in lsort  


Procedure Details

External Procedure Name: ::ip::prefixToNative
Purpose
  convert from dotted from to native (hex) form
Synopsis
 

prefixToNative <prefix>
Arguments
 

<prefix>
    string in the <ipaddr>/<mask> format
Return Output
 

<prefix> in native format {<hexip> <hexmask>}
Examples
 

% ip::prefixToNative 1.1.1.0/24
0x01010100 0xffffff00
Notes
 

fixed bug in C extension that modified 
 calling context variable
Top

External Procedure Name: ::ip::nativeToPrefix
Purpose
  convert from native (hex) form to dotted form
Synopsis
 

nativeToPrefix <nativeList>|<native> [-ipv4]
Arguments
 

<nativeList> 
    list of native form ip addresses native form is:
<native>
    tcllist in format {<hexip> <hexmask>}
-ipv4
    the provided native format addresses are in ipv4 format (default)
Return Output
 

if nativeToPrefix is called with <native> a single (non-listified) address
    is returned
if nativeToPrefix is called with a <nativeList> address list, then 
    a list of addresses is returned

return form is: <ipaddr>/<mask>
Examples
 

% ip::nativeToPrefix {0x01010100 0xffffff00} -ipv4
1.1.1.0/24
Top

External Procedure Name: ::ip::intToString
Purpose
  convert from an integer/hex to dotted form
Synopsis
 

intToString <integer/hex> [-ipv4]
Arguments
 

<integer>
    ip address in integer form
-ipv4
    the provided integer addresses is ipv4 (default)
Return Output
 

ip address in dotted form
Examples
 

ip::intToString 4294967295
255.255.255.255
Top

External Procedure Name: ::ip::toInteger
Purpose
  convert dotted form ip to integer
Synopsis
 

toInteger <ipaddr>
Arguments
 

<ipaddr>
    decimal dotted from ip address
Return Output
 

integer form of <ipaddr>
Examples
 

% ::ip::toInteger 1.1.1.0
16843008
Top

External Procedure Name: ::ip::toHex
Purpose
  convert dotted form ip to hex
Synopsis
 

toHex <ipaddr>
Arguments
 

<ipaddr>
    decimal dotted from ip address
Return Output
 

hex form of <ipaddr>
Examples
 

% ::ip::toHex 1.1.1.0
0x01010100
Top

External Procedure Name: ::ip::maskToInt
Purpose
  convert mask to integer
Synopsis
 

maskToInt <mask>
Arguments
 

<mask>
    mask in either dotted form or mask length form (255.255.255.0 or 24)
Return Output
 

integer form of mask
Examples
 

::ip::maskToInt 24
4294967040
Top

External Procedure Name: ::ip::broadcastAddress
Purpose
  return broadcast address given prefix
Synopsis
 

broadcastAddress <prefix> [-ipv4]
Arguments
 

<prefix>
    route in the form of <ipaddr>/<mask> or native form {<hexip> <hexmask>}
-ipv4
    the provided native format addresses are in ipv4 format (default)
    note: broadcast addresses are not valid in ipv6
Return Output
 

ipaddress of broadcast
Examples
 

::ip::broadcastAddress 1.1.1.0/24
1.1.1.255

::ip::broadcastAddress {0x01010100 0xffffff00}
0x010101ff
Top

External Procedure Name: ::ip::maskToLength
Purpose
  converts dotted or integer form of mask to length
Synopsis
 

maskToLength <dottedMask>|<integerMask>|<hexMask> [-ipv4]
Arguments
 

<dottedMask>
<integerMask>
<hexMask>
    mask to convert to prefix length format (eg /24)
 -ipv4
    the provided integer/hex format masks are ipv4 (default)
Return Output
 

prefix length
Examples
 

::ip::maskToLength 0xffffff00 -ipv4
24

% ::ip::maskToLength 255.255.255.0
24
Top

External Procedure Name: ::ip::lengthToMask
Purpose
  converts mask length to dotted mask form
Synopsis
 

lengthToMask <maskLength> [-ipv4]
Arguments
 

<maskLength>
    mask length   
-ipv4
    the provided mask length is ipv4 (default)
Return Output
 

mask in dotted form
Examples
 

::ip::lengthToMask 24
255.255.255.0
Top

External Procedure Name: ::ip::nextNet
Purpose
  returns next an ipaddress in same position in next network
Synopsis
 

nextNet <ipaddr> <mask> [<count>] [-ipv4]
Arguments
 

<ipaddress>
    in hex/integer/dotted format
<mask>
    mask in hex/integer/dotted/maskLen format
<count>
    number of nets to skip over (default is 1)
-ipv4
    the provided hex/integer addresses are in ipv4 format (default)
Return Output
 

ipaddress in same position in next network in hex
Top

External Procedure Name: ::ip::isOverlap
Purpose
  checks to see if prefixes overlap
Synopsis
 

isOverlap <prefix> <prefix1> <prefix2>...
Arguments
 

<prefix>
    in form <ipaddr>/<mask> prefix to compare <prefixN> against
<prefixN>
    in form <ipaddr>/<mask> prefixes to compare against
Return Output
 

1 if there is an overlap
Examples
 

% ::ip::isOverlap 1.1.1.0/24 2.1.0.1/32
0

::ip::isOverlap 1.1.1.0/24 2.1.0.1/32 1.1.1.1/32
1
Top

External Procedure Name: ::ip::isOverlapNative
Purpose
  checks to see if prefixes overlap (optimized native form)
Synopsis
 

isOverlap <hexipaddr> <hexmask> {{<hexipaddr1> <hexmask1>} {<hexipaddr2> <hexmask2>...}
Arguments
 

-all
    return all overlaps rather than the first one
-inline
    rather than returning index values, return the actual overlap prefixes
<hexipaddr>
    ipaddress in hex/integer form
<hexMask>
    mask in hex/integer form
-ipv4
    the provided native format addresses are in ipv4 format (default)
Return Output
 

non-zero if there is an overlap, value is element # in list with overlap
Description
 

isOverlapNative is avaliabel both as a C extension and in a native tcl form if the extension is loaded (tried automatically), isOverlapNative will be linked to isOverlapNativeC. If an extension is not loaded, then isOverlapNative will be linked to the native tcl proc: ipOverlapNativeTcl.

Examples
 

% ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff}}
0

%::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff} {0x01010101 0xffffffff}}
2
Top

External Procedure Name: ::ip::ipToLayer2Multicast
Purpose
  converts ipv4 address to a layer 2 multicast address
Synopsis
 

ipToLayer2Multicast <ipaddr>
Arguments
 

<ipaddr>
    ipaddress in dotted form
Return Output
 

mac address in xx.xx.xx.xx.xx.xx form
Examples
 

% ::ip::ipToLayer2Multicast 224.0.0.2
01.00.5e.00.00.02
Top

External Procedure Name: ::ip::ipHostFromPrefix
Purpose
  gives back a host address from a prefix
Synopsis
 

::ip::ipHostFromPrefix <prefix> [-exclude <list of prefixes>]
Arguments
 

<prefix>
    prefix is <ipaddr>/<masklen>
-exclude <list of prefixes>
    list if ipprefixes that host should not be in
Return Output
 

ip address
Examples
 

%::ip::ipHostFromPrefix  1.1.1.5/24
1.1.1.1

%::ip::ipHostFromPrefix  1.1.1.1/32
1.1.1.1
Top

External Procedure Name: ::ip::reduceToAggregates
Purpose
  finds nets that overlap and filters out the more specifc nets
Synopsis
 

::ip::reduceToAggregates <prefixList>
Arguments
 

<prefixList>
    prefixList a list in the from of
    is <ipaddr>/<masklen> or native format
Return Output
 

non-overlapping ip prefixes
Examples
 


  % ::ip::reduceToAggregates {1.1.1.0/24 1.1.0.0/8  2.1.1.0/24 1.1.1.1/32 }
  1.0.0.0/8 2.1.1.0/24
Top

External Procedure Name: ::ip::longestPrefixMatch
Purpose
  given host IP finds longest prefix match from set of prefixes
Synopsis
 

::ip::longestPrefixMatch <ipaddr> <prefixList> [-ipv4]
Arguments
 

<prefixList>
    is list of <ipaddr> in native or dotted form
<ipaddr>
    ip address in <ipprefix> format, dotted form, or integer form
-ipv4
    the provided integer format addresses are in ipv4 format (default)
Return Output
 

<ipprefix> that is the most specific match to <ipaddr>
Examples
 

% ::ip::longestPrefixMatch 1.1.1.1 {1.1.1.0/24 1.0.0.0/8  2.1.1.0/24 1.1.1.0/28 }
1.1.1.0/28
Top

External Procedure Name: ::ip::cmpDotIP
Purpose
  helper function for dotted ip address for use in lsort
Synopsis
 

::ip::cmpDotIP <ipaddr1> <ipaddr2>
Arguments
 

<ipaddr1> <ipaddr2>
    prefix is in dotted ip address format
Return Output
 

-1 if ipaddr1 is less that ipaddr2
 1 if ipaddr1 is more that ipaddr2
 0 if ipaddr1 and ipaddr2 are equal
Examples
 

% lsort -command ip::cmpDotIP {1.0.0.0 2.2.0.0 128.0.0.0 3.3.3.3}
1.0.0.0 2.2.0.0 3.3.3.3 128.0.0.0
Top

 

Copyright (c) Cisco Systems, Inc. Internal Use Only