- Notifications
You must be signed in to change notification settings - Fork0
IP address manipulation library and utility
License
syzdek/libnetcalc
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Network Calculations Library
Copyright (C) 2025 David M. Syzdekdavid@syzdek.net.
All rights reserved.
Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:
- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.
- Neither the name of David M. Syzdek nor thenames of its contributors may be used to endorse or promote productsderived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICTLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAYOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OFSUCH DAMAGE.
- Overview
- Utility
- info widget
- sort widget
- tree widget
- Maintainers
- Source Code
This package contains a library with miscellaneous functions for manipulatingand sorting IPv4, IPv6, EUI48, and EUI64 addresses. The accompanying utilityallows some of the library functions to be accessed from the command lineor in shell scripts.
Man pages for thelibnetcalclibrary and for thenetcalcutility are available on the projectGithub Pages.
Thenetcalc utility can be used on the command line to access some of thefunctionality of the netcalc library. The utility contains the followingwidgets:
- info display IP address information
- printf display formatted IP address information
- superblock display superblocks containing IP addresses
- sort sorts networks
- syntaxes display supported address syntaxes
- test compares two IP addresses or networks
- tree sorts networks and display is a tree structure
Normally a widget is invoked by specifying the widget name on the command line.The following example invokes the info widget:
netcalc info 203.0.113.192/29Network Broadcast Netmask Wildcard CIDR /32s203.0.113.192 203.0.113.199 255.255.255.248 0.0.0.7 29 8If netcalc is copied or linked to another filename, then netcalc will attemptto determine the widget name from the filename which was executed on thecommand line.
Referencing the netcalc info widget using the 'netcalc-' prefix:
ln -s netcalc netcalc-info./netcalc-info 203.0.113.192/29Network Broadcast Netmask Wildcard CIDR /32s203.0.113.192 203.0.113.199 255.255.255.248 0.0.0.7 29 8Referencing the netcalc info widget using the 'netcalc' prefix:
ln -s netcalc netcalcinfo./netcalcinfo 203.0.113.192/29Network Broadcast Netmask Wildcard CIDR /32s203.0.113.192 203.0.113.199 255.255.255.248 0.0.0.7 29 8Referencing the netcalc info widget using the 'net' prefix:
ln -s netcalc netinfo./netinfo 203.0.113.192/29Network Broadcast Netmask Wildcard CIDR /32s203.0.113.192 203.0.113.199 255.255.255.248 0.0.0.7 29 8The info widget parses one or more addresses and displays information aboutthe address. If more than one address was provided, the information aboutthe superblock network which contains all provided addresses will also bedisplayed. Additionally the info widget can also be used to convert betweenaddress families.
Basic example usage:
netcalc info 2001:db8:e:ffff::/64 2001:db8:d:f000::/64 2001:db8:d:ffff::/64 \ 2001:db8:d:fffe::/64 2001:db8:d:100::/64 2001:db8:d:101::/64 \ 2001:db8:d:100::/56 2001:db8:d::/48 2001:db8:d::/56 2001:db8:d:a::/64 \ 2001:db8:e:a::/64 2001:db8:f:ffff::/64 2001:db8::/64 \ 2001:db8:d:101::1/128 2001:db8:0:e::203.0.113.198/127Address Network Broadcast Netmask Wildcard CIDR /64s2001:db8:e:ffff::/64 2001:db8:e:ffff:: 2001:db8:e:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:f000::/64 2001:db8:d:f000:: 2001:db8:d:f000:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:ffff::/64 2001:db8:d:ffff:: 2001:db8:d:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:fffe::/64 2001:db8:d:fffe:: 2001:db8:d:fffe:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:100::/64 2001:db8:d:100:: 2001:db8:d:100:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:101::/64 2001:db8:d:101:: 2001:db8:d:101:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:100::/56 2001:db8:d:100:: 2001:db8:d:1ff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ff00:: ::ff:ffff:ffff:ffff:ffff 56 2562001:db8:d::/48 2001:db8:d:: 2001:db8:d:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff:ffff 48 655362001:db8:d::/56 2001:db8:d:: 2001:db8:d:ff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ff00:: ::ff:ffff:ffff:ffff:ffff 56 2562001:db8:d:a::/64 2001:db8:d:a:: 2001:db8:d:a:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:e:a::/64 2001:db8:e:a:: 2001:db8:e:a:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:f:ffff::/64 2001:db8:f:ffff:: 2001:db8:f:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8::/64 2001:db8:: 2001:db8:0:0:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:: ::ffff:ffff:ffff:ffff 64 12001:db8:d:101::1 2001:db8:d:101::1 2001:db8:d:101:0:0:0:1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff :: 128 n/a2001:db8:0:e::cb00:71c6/127 2001:db8:0:e::cb00:71c6 2001:db8:0:e:0:0:cb00:71c7 ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe ::1 127 n/aSUPERBLOCK 2001:db8:: 2001:db8:f:ffff:ffff:ffff:ffff:ffff ffff:ffff:fff0:: ::f:ffff:ffff:ffff:ffff:ffff 44 1048576Advanced example IPv4 usage:
netcalc info -vv 203.0.113.198/29 203.0.113.198/29 (IPv4)-----------------------Host Address: 203.0.113.198Network Address: 203.0.113.192Broadcast Address: 203.0.113.199Netmask: 255.255.255.248Wildcard: 0.0.0.7CIDR: 29Usable Range: 203.0.113.193 203.0.113.198DNS ARPA Host: 198.113.0.203.in-addr.arpa.DNS ARPA Zone: 113.0.203.in-addr.arpa.DNS ARPA RR: 198 IN PTRAdvanced example IPv6 usage:
netcalc info -vv 2001:db8:0:e::203.0.113.198/1272001:db8:0:e::203.0.113.198/127 (IPv6)--------------------------------------Host Address: 2001:db8:0:e::cb00:71c6IPv4-mapped Address: 2001:db8:0:e::203.0.113.198Network Address: 2001:db8:0:e::cb00:71c6Broadcast Address: 2001:db8:0:e:0:0:cb00:71c7Netmask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffeWildcard: ::1CIDR: 127Usable Range: 2001:db8:0:e:0:0:cb00:71c6 2001:db8:0:e:0:0:cb00:71c7DNS ARPA Host: 6.c.1.7.0.0.b.c.0.0.0.0.0.0.0.0.e.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.DNS ARPA Zone: c.1.7.0.0.b.c.0.0.0.0.0.0.0.0.e.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.DNS ARPA RR: 6 IN PTRThe sort widget uses the set family of functions to sort the list of networks.
Example usage:
netcalc sort 2001:db8:e:ffff::/64 2001:db8:d:f000::/64 2001:db8:d:ffff::/64 \ 2001:db8:d:fffe::/64 2001:db8:d:100::/64 2001:db8:d:101::/64 \ 2001:db8:d:100::/56 2001:db8:d::/48 2001:db8:d::/56 2001:db8:d:a::/64 \ 2001:db8:e:a::/64 2001:db8:f:ffff::/64 2001:db8::/64 2001:db8:d:101::1/128 \ ::/1 ::/0 8000::/1 2001:db8::/32 203.0.113.198/31 203.0.113.0/24 \ 203.0.113.192/29 203.0.113.196/30 203.0.113.199/32 1: ::/0 2: ::/1 3: 203.0.113.0/24 4: 203.0.113.192/29 5: 203.0.113.196/30 6: 203.0.113.198/31 7: 203.0.113.199 8: 2001:db8::/32 9: 2001:db8::/64 10: 2001:db8:d::/48 11: 2001:db8:d::/56 12: 2001:db8:d:a::/64 13: 2001:db8:d:100::/56 14: 2001:db8:d:100::/64 15: 2001:db8:d:101::/64 16: 2001:db8:d:101::1 17: 2001:db8:d:f000::/64 18: 2001:db8:d:fffe::/64 19: 2001:db8:d:ffff::/64 20: 2001:db8:e:a::/64 21: 2001:db8:e:ffff::/64 22: 2001:db8:f:ffff::/64 23: 8000::/1The test widget compares two addresses according to the operator specified. Ifthe expression evaluates to true, then the utility returns an exit code ofzero. If the expression evaluates to false, then an non-zero exit code isreturned. This allows shell scripts to compare IPv4 and IPv6 addresses muchlike the shell 'test' macro allows the comparison of strings and numbers.
Example usages comparing if an address is contained within a subnet:
netcalc test 209.193.4.7 '<<' 209.193.4.4/30echo $?0netcalc test -- 209.193.4.7 -llt 209.193.4.4/30echo $?0if netcalc test -- 209.193.4.7 -llt 209.193.4.4/30; then echo "is within subnet"else echo "is not in subnet";fiis within subnetExample usages testing if two addresses are the same:
netcalc test 2001:db8:f:300::cb00:712a = 2001:db8:f:300::203.0.113.42echo $?0netcalc test -- 2001:db8:f:300::cb00:712a -eq 2001:db8:f:300::203.0.113.42echo $?0if netcalc test 2001:db8:f:300::cb00:712a = 2001:db8:f:300::203.0.113.42; then echo "addresses are the same"else echo "addresses are not the same";fiaddresses are the sameThe tree widget uses the set family of functions to sort the list of networksand display the sorted networks in a hierarchy of subnets.
Example usage:
netcalc tree 2001:db8:e:ffff::/64 2001:db8:d:f000::/64 2001:db8:d:ffff::/64 \ 2001:db8:d:fffe::/64 2001:db8:d:100::/64 2001:db8:d:101::/64 \ 2001:db8:d:100::/56 2001:db8:d::/48 2001:db8:d::/56 2001:db8:d:a::/64 \ 2001:db8:e:a::/64 2001:db8:f:ffff::/64 2001:db8::/64 2001:db8:d:101::1/128 \ ::/1 ::/0 8000::/1 2001:db8::/32 203.0.113.198/31 203.0.113.0/24 \ 203.0.113.192/29 203.0.113.196/30 203.0.113.199/32 1. ::/0 2. ::/1 3. 203.0.113.0/24 4. 203.0.113.192/29 5. 203.0.113.196/30 6. 203.0.113.198/31 7. 203.0.113.199 8. 2001:db8::/32 9. 2001:db8::/64 10. 2001:db8:d::/48 11. 2001:db8:d::/56 12. 2001:db8:d:a::/64 13. 2001:db8:d:100::/56 14. 2001:db8:d:100::/64 15. 2001:db8:d:101::/64 16. 2001:db8:d:101::1 17. 2001:db8:d:f000::/64 18. 2001:db8:d:fffe::/64 19. 2001:db8:d:ffff::/64 20. 2001:db8:e:a::/64 21. 2001:db8:e:ffff::/64 22. 2001:db8:f:ffff::/64 23. 8000::/1- David M. Syzdekdavid@syzdek.net
The source code for this project is maintained using git (http://git-scm.com).The following contains information to checkout the source code from the gitrepository.
Browse Source:
Git URLs:
- https://github.com/syzdek/netcalc.git
- git@github.com:syzdek/netcalc.git
Preparing Source:
$ git clone https://github.com/syzdek/netcalc.git$ cd netcalc$ ./autogen.shThis is a collection of notes for developers to use when maintaining thispackage.
New Release Checklist:
Switch to 'master' branch in Git repository.
Update version in configure.ac.
Update date and version in ChangeLog.
Commit configure.ac and ChangeLog changes to repository.
Create tag in git repository:
$ git tag -s v${MAJOR}.${MINOR}Push repository to publishing server:
$ git push --tags origin master:master next:next pu:pu
Creating Source Distribution Archives:
$ ./configure$ make update$ make distcheck$ make dist-bzip2$ make dist-xzAbout
IP address manipulation library and utility
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.