tcllib_ip(3tcl)
πΈ tcllib_ip(3tcl) β Domain Name Service πΈ
okay bestie, welcome to the manual page for tcllib_ip, living in the Domain Name Service section of tcllib. It's giving IP address whisperer, fr fr. Let's get into it π
β¨
β¨ NAME β¨
tcllib_ip β IPv4 and IPv6 address manipulation π¦
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require ip ?1.5.1?
::ip::version address
::ip::is class address
::ip::equal address address
::ip::normalize address
::ip::contract address
::ip::distance ipaddr1 ipaddr2
::ip::nextIp ipaddr ?offset?
::ip::prefix address
::ip::type address
::ip::mask address
::ip::prefixToNative prefix
::ip::nativeToPrefix nativeList|native ?-ipv4?
::ip::intToString number ?-ipv4?
::ip::toInteger ipaddr
::ip::toHex ipaddr
::ip::maskToInt ipmask
::ip::broadcastAddress prefix ?-ipv4?
::ip::maskToLength dottedMask|integerMask|hexMask ?-ipv4?
::ip::lengthToMask maskLength ?-ipv4?
::ip::nextNet ipaddr ipmask ?count? ?-ipv4?
::ip::isOverlap prefix prefix...
::ip::isOverlapNative ?-all? ?-inline? ?-ipv4? hexipaddr hexipmask hexiplist
::ip::ipToLayer2Multicast ipaddr
::ip::ipHostFromPrefix prefix ?-exclude prefixExcludeList?
::ip::reduceToAggregates prefixlist
::ip::longestPrefixMatch ipaddr prefixlist ?-ipv4?
::ip::collapse prefixlist
::ip::subtract prefixlist
β¨ DESCRIPTION β¨
This package is literally a whole toolkit of commands to help you with parsing, displaying, and comparing internet addresses. It's main character energy for real. And the glow-up? It can handle both IPv4 (1) and IPv6 (2) address types, no cap. Periodt. π
π οΈ COMMANDS π οΈ
okay bestie, this is the big one β every single command, no skips, no cap π
::ip::version address
Spills the tea on which protocol version your address is. Returns 4 or 6, or -1 if it's neither IPv4 nor IPv6. Like, neither?? rude.
::ip::is class address
Returns true if the address is a member of the given protocol class. The class parameter may be either ipv4 or ipv6. Basically this is the boolean bestie of version β same vibe, but you get a yes/no instead of a number. Oh and you can shorten the class argument to just 4 or 6 if you're feeling lazy β¨
::ip::equal address address
Compares two address specifications to see if they're ~equivalent~. The arguments get normalized and the address prefix gets determined (if a mask is supplied). Then the normalized addresses get compared bit-by-bit, and the procedure returns true if they match. Matching = slay π«Ά
::ip::normalize address
Converts an IPv4 or IPv6 address into its fully expanded version. There's like a million shorthand ways to write internet addresses β skipping redundant parts, skipping digits β and this is the glow-up that spells it all out. It's the opposite of contract.
::ip::contract address
Takes a normalized internet address and squeezes it into a more compact form, the kind that's actually nice to show to users. The lil makeover π
::ip::distance ipaddr1 ipaddr2
Computes the (integer) distance from IPv4 address ipaddr1 to IPv4 address ipaddr2 β literally "ipaddr2 - ipaddr1".
% ::ip::distance 1.1.1.1 1.1.1.5
4
::ip::nextIp ipaddr ?offset?
Adds the integer offset to the IPv4 address ipaddr and hands you back the new IPv4 address. Like a little step forward β¨
% ::ip::distance 1.1.1.1 4
1.1.1.5
::ip::prefix address
Returns the address prefix that comes from masking the address part with the mask, if one was provided. If there's no mask, it's exactly the same as calling normalize.
::ip::type address
ngl bestie, the original page lists this command and then just... says nothing about it. No description. Nothing. The way that it's just vibes and mystery π Anyway, it's here, it exists, that's what the page gives us.
::ip::mask address
If the address you supplied includes a mask, this returns it. Otherwise you get an empty string. That's the whole deal π
::ip::prefixToNative prefix
Converts the string prefix from dotted form (that's the <ipaddr>/<mask> format) into native (hex) form. You get back a list with two elements β ipaddress and mask, in that order β in hexadecimal notation.
% ip::prefixToNative 1.1.1.0/24
0x01010100 0xffffff00
::ip::nativeToPrefix nativeList|native ?-ipv4?
Converts from native (hex) form into dotted form. It's the complement of ::ip::prefixToNative, they're giving duo energy π
- list
nativeList(in) β a list of several ip addresses in native form. The native form is a list as returned by::ip::prefixToNative. - list
native(in) β a list as returned by::ip::prefixToNative.
The command returns a list of addresses in dotted form if you called it with a list of addresses. Otherwise a single address in dotted form comes back.
% ip::nativeToPrefix {0x01010100 0xffffff00} -ipv4
1.1.1.0/24
::ip::intToString number ?-ipv4?
Converts an ip address that's specified as an integer number into dotted form.
ip::intToString 4294967295
255.255.255.255
::ip::toInteger ipaddr
Converts a dotted form ip into an integer number. numbers are lowkey iconic.
% ::ip::toInteger 1.1.1.0
16843008
::ip::toHex ipaddr
Converts dotted form ip into a hexadecimal number.
% ::ip::toHex 1.1.1.0
0x01010100
::ip::maskToInt ipmask
Converts an ipmask in either dotted form (255.255.255.0) or mask length form (24) into an integer number.
::ip::maskToInt 24
4294967040
::ip::broadcastAddress prefix ?-ipv4?
Returns a broadcast address in dotted form for the given route prefix, either in the form "addr/mask", or in native form. The result is in dotted form.
::ip::broadcastAddress 1.1.1.0/24
1.1.1.255
::ip::broadcastAddress {0x01010100 0xffffff00}
0x010101ff
::ip::maskToLength dottedMask|integerMask|hexMask ?-ipv4?
Converts the dotted or integer form of an ipmask into the mask length form.
::ip::maskToLength 0xffffff00 -ipv4
24
% ::ip::maskToLength 255.255.255.0
24
::ip::lengthToMask maskLength ?-ipv4?
Converts an ipmask in mask length form into its dotted form.
::ip::lengthToMask 24
255.255.255.0
::ip::nextNet ipaddr ipmask ?count? ?-ipv4?
Returns an ipaddress sitting in the same position in the count next network. The default value for count is 1.
The address can be specified as either an integer number or in dotted form. The mask can be specified as either an integer number, dotted form, or mask length form.
The result is in hex form. β¨
::ip::isOverlap prefix prefix...
Checks if the given ip prefixes overlap. All arguments are in dotted "addr/mask" form. Every argument after the first prefix gets compared against the first prefix. The result is a boolean value β it's true if an overlap was found for any of the prefixes.
% ::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
::ip::isOverlapNative ?-all? ?-inline? ?-ipv4? hexipaddr hexipmask hexiplist
Similar to ::ip::isOverlap, except the arguments are in native form and you, the caller, get way more control over the shape of the result. If -all is specified it checks all addresses for overlap, not just until the first one is found. If -inline is specified, the command returns the overlapping prefix instead of index values.
Depending on the options you picked, the result is:
- no options β the index of the first overlap found, or
0if there is none. -allβ a list containing the indices of all overlaps found, or an empty list if there are none.-inlineβ the first overlapping prefix, or an empty string if there is none.-all -inlineβ a list containing the prefixes of all overlaps found, or an empty list if there are none.
% ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff}}
0
% ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff} {0x01010101 0xffffffff}}
2
::ip::ipToLayer2Multicast ipaddr
Converts an ipv4 address in dotted form into a layer 2 multicast address, also in dotted form. glow-up from layer 3 to layer 2 π
% ::ip::ipToLayer2Multicast 224.0.0.2
01.00.5e.00.00.02
::ip::ipHostFromPrefix prefix ?-exclude prefixExcludeList?
Returns a host address from a prefix in the form "ipaddr/masklen", while also making sure the result isn't an address found in the prefixExcludeList. The result is an ip address in dotted form.
%::ip::ipHostFromPrefix 1.1.1.5/24
1.1.1.1
%::ip::ipHostFromPrefix 1.1.1.1/32
1.1.1.1
::ip::reduceToAggregates prefixlist
Finds nets that overlap and filters out the more specific ones. The prefixes are in either addr/mask form or in native format. The result is a list containing the non-overlapping ip prefixes from the input. decluttering, but for networks π§
% ::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
::ip::longestPrefixMatch ipaddr prefixlist ?-ipv4?
Finds the longest prefix match from a set of prefixes, given a specific host address. The prefixes in the list are in either native or dotted form, whereas the host address is in either ipprefix format, dotted form, or integer form. The result is the prefix that's the most specific match to the host address. it's giving main character energy π
% ::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
::ip::collapse prefixlist
Takes a list of prefixes and returns a list of prefixes with the largest possible subnet masks covering the input β collapsing adjacent prefixes into bigger ranges. ate and left no crumbs π₯
This is different from ::ip::reduceToAggregates, because the latter only removes specific nets from a list when they're covered by other elements of the input, whereas this command actively merges nets into larger ranges when they're adjacent to each other.
% ::ip::collapse {1.2.2.0/24 1.2.3.0/24}
1.2.2.0/23
::ip::subtract prefixlist
Takes a list of prefixes, some of which are prefixed by a dash. Those negative prefixes are used to punch holes into the ranges described by the other, positive, prefixes. I.e. the negative prefixes are subtracted from the positive ones, resulting in a larger list that describes the covered ranges only as positives. hole-punching but make it networking π
β¨ EXAMPLES β¨
here's the whole vibe in action, bestie:
% ip::version ::1
6
% ip::version 127.0.0.1
4
% ip::normalize 127/8
127.0.0.0/8
% ip::contract 192.168.0.0
192.168
%
% ip::normalize fec0::1
fec0:0000:0000:0000:0000:0000:0000:0001
% ip::contract fec0:0000:0000:0000:0000:0000:0000:0001
fec0::1
% ip::equal 192.168.0.4/16 192.168.0.0/16
1
% ip::equal fec0::1/10 fec0::fe01/10
1
π REFERENCES π
- [1] Postel, J. "Internet Protocol." RFC 791, September 1981, (http://www.ietf.org/rfc/rfc791.txt)
- [2] Hinden, R. and Deering, S., "Internet Protocol Version 6 (IPv6) Addressing Architecture", RFC 3513, April 2003 (http://www.ietf.org/rfc/rfc3513.txt)
π AUTHORS π
Pat Thoyts
π BUGS, IDEAS, FEEDBACK π
okay bestie, real talk for a sec: this doc? and the whole package it describes? yeah, they absolutely have bugs. other problems too. it's giving "software", you know? π so if you find something cursed, please report it β the category you want is dns in the Tcllib Trackers, over at http://core.tcl.tk/tcllib/reportlist.
and hey β not just bugs!! if you have ideas for making either the package or the docs better? also report those. glow-up suggestions are always welcome, no gatekeeping here π«Ά
β¨ proposing code changes (the important bit) β¨
when you're proposing actual code changes, pls pls pls hand over unified diffs β like, literally the output of:
diff -u
that's the format. that's the one. periodt. π
π¨ and listen: attachments are strongly preferred over patches you just paste inline. like, strongly. so here's the whole move: create the ticket, then immediately go to its Edit form, and use the left-most button in the secondary navigation bar to attach your file. that's it, that's the ritual π
π SEE ALSO
go read those too, they're part of the lore π
π KEYWORDS
internet address, ip, ipv4, ipv6, rfc 3513
(these are the little searchable tags, iykyk π·οΈ)
π CATEGORY
Networking β yeah. obviously. she's a network girlie fr fr π
π COPYRIGHT
- Copyright (c) 2004, Pat Thoyts
- Copyright (c) 2005 Aamer Akhter
<aakhter@cisco.com>
and that's the whole page, bestie!! π this lil masterpiece is from the tcllib collection, version 1.5.1, and it lives its life as tcllib_ip(3tcl) β go forth and address all the internets, you absolute icon πβ¨π¦π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2004, Pat Thoyts
- Copyright (c) 2005 Aamer Akhter <aakhter@cisco\&.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms