1tcllib_ip(n)                  Domain Name Service                 tcllib_ip(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       tcllib_ip - IPv4 and IPv6 address manipulation
9

SYNOPSIS

11       package require Tcl  8.2
12
13       package require ip  ?1.1.1?
14
15       ::ip::version address
16
17       ::ip::is class address
18
19       ::ip::equal address address
20
21       ::ip::normalize address
22
23       ::ip::contract address
24
25       ::ip::prefix address
26
27       ::ip::type address
28
29       ::ip::mask address
30
31       ::ip::prefixToNative prefix
32
33       ::ip::nativeToPrefix nativeList|native ?-ipv4?
34
35       ::ip::intToString number ?-ipv4?
36
37       ::ip::toInteger ipaddr
38
39       ::ip::toHex ipaddr
40
41       ::ip::maskToInt ipmask
42
43       ::ip::broadcastAddress prefix ?-ipv4?
44
45       ::ip::maskToLength dottedMask|integerMask|hexMask ?-ipv4?
46
47       ::ip::lengthToMask maskLength ?-ipv4?
48
49       ::ip::nextNet ipaddr ipmask ?count? ?-ipv4?
50
51       ::ip::isOverlap prefix prefix...
52
53       ::ip::isOverlapNative ?-all? ?-inline? ?-ipv4? hexipaddr hexipmask hex‐
54       iplist
55
56       ::ip::ipToLayer2Multicast ipaddr
57
58       ::ip::ipHostFromPrefix prefix ?-exclude prefixExcludeList?
59
60       ::ip::reduceToAggregates prefixlist
61
62       ::ip::longestPrefixMatch ipaddr prefixlist ?-ipv4?
63
64_________________________________________________________________
65

DESCRIPTION

67       This package provides a set of commands to help in parsing,  displaying
68       and  comparing internet addresses. The package can handle both IPv4 (1)
69       and IPv6 (2) address types.
70

COMMANDS

72       ::ip::version address
73              Returns the protocol version of the address (4 or 6) or 0 if the
74              address is neither IPv4 or IPv6.
75
76       ::ip::is class address
77              Returns  true  if  the address is a member of the given protocol
78              class. The class parameter may be either ipv4 or  ipv6  This  is
79              effectively  a  boolean  equivalent  of the version command. The
80              class argument may be shortened to 4 or 6.
81
82       ::ip::equal address address
83              Compare two address specifications for  equivalence.  The  argu‐
84              ments  are  normalized  and  the address prefix determined (if a
85              mask is supplied). The normalized addresses  are  then  compared
86              bit-by-bit and the procedure returns true if they match.
87
88       ::ip::normalize address
89              Convert  an  IPv4 or IPv6 address into a fully expanded version.
90              There are various shorthand ways to  write  internet  addresses,
91              missing  out  redundant  parts  or digts.. This procedure is the
92              opposite of contract.
93
94       ::ip::contract address
95              Convert a normalized internet address into a more  compact  form
96              suitable for displaying to users.
97
98       ::ip::prefix address
99              Returns the address prefix generated by masking the address part
100              with the mask if provided. If there is no mask then it is equiv‐
101              alent to calling normalize
102
103       ::ip::type address
104
105       ::ip::mask address
106              If  the  address  supplied includes a mask then this is returned
107              otherwise returns an empty string.
108
109       ::ip::prefixToNative prefix
110              This  command  converts  the  string  prefix  from  dotted  form
111              (<ipaddr>/<mask>  format)  to  native (hex) form. Returns a list
112              containing two elements, ipaddress and mask, in this  order,  in
113              hexadecimal notation.
114
115
116                 % ip::prefixToNative 1.1.1.0/24
117                 0x01010100 0xffffff00
118
119
120       ::ip::nativeToPrefix nativeList|native ?-ipv4?
121              This command converts from native (hex) form to dotted form.  It
122              is the complement of ::ip::prefixToNative.
123
124
125              nativeList list (in)
126                     List of several ip addresses in native form.  The  native
127                     form is a list as returned by ::ip::prefixToNative.
128
129              native list (in)
130                     A list as returned by ::ip::prefixToNative.
131
132       The command returns a list of addresses in dotted form if it was called
133       with a list of addresses. Otherwise a single address in dotted form  is
134       returned.
135
136
137          % ip::nativeToPrefix {0x01010100 0xffffff00} -ipv4
138          1.1.1.0/24
139
140
141       ::ip::intToString number ?-ipv4?
142              This  command  converts  from an ip address specified as integer
143              number to dotted form.
144
145
146                     ip::intToString 4294967295
147                     255.255.255.255
148
149
150       ::ip::toInteger ipaddr
151              This command converts a dotted form ip into an integer number.
152
153
154                 % ::ip::toInteger 1.1.1.0
155                 16843008
156
157
158       ::ip::toHex ipaddr
159              This command converts dotted form ip into a hexadecimal number.
160
161
162                 % ::ip::toHex 1.1.1.0
163                 0x01010100
164
165
166       ::ip::maskToInt ipmask
167              This command convert an ipmask in either dotted  (255.255.255.0)
168              form or mask length form (24) into an integer number.
169
170
171                 ::ip::maskToInt 24
172                 4294967040
173
174
175       ::ip::broadcastAddress prefix ?-ipv4?
176              This commands returns a broadcast address in dotted form for the
177              given route prefix, either in the form "addr/mask", or in native
178              form. The result is in dotted form.
179
180
181                 ::ip::broadcastAddress 1.1.1.0/24
182                 1.1.1.255
183
184                 ::ip::broadcastAddress {0x01010100 0xffffff00}
185                 0x010101ff
186
187
188
189
190
191       ::ip::maskToLength dottedMask|integerMask|hexMask ?-ipv4?
192
193
194              This command converts the dotted or integer form of an ipmask to
195              the mask length form.
196
197
198
199
200
201
202                 ::ip::maskToLength 0xffffff00 -ipv4
203                 24
204
205                 % ::ip::maskToLength 255.255.255.0
206                 24
207
208
209       ::ip::lengthToMask maskLength ?-ipv4?
210              This command converts an ipmask in mask length form to its  dot‐
211              ted form.
212
213
214                 ::ip::lengthToMask 24
215                 255.255.255.0
216
217
218       ::ip::nextNet ipaddr ipmask ?count? ?-ipv4?
219              This  command  returns  an ipaddress in the same position in the
220              count next network. The default value for count is 1.
221
222              The address can be specified as either integer number or in dot‐
223              ted  form.  The  mask can be specified as either integer number,
224              dotted form, or mask length form.
225
226              The result is in hex form.
227
228       ::ip::isOverlap prefix prefix...
229              This command checks if the given ip prefixes overlap.  All argu‐
230              ments  are  in  dotted "addr/mask" form. All arguments after the
231              first prefix are compared against the first prefix.  The  result
232              is  a  boolean value. It is true if an overlap was found for any
233              of the prefixes.
234
235
236                % ::ip::isOverlap 1.1.1.0/24 2.1.0.1/32
237                0
238
239                ::ip::isOverlap 1.1.1.0/24 2.1.0.1/32 1.1.1.1/32
240                1
241
242
243       ::ip::isOverlapNative ?-all? ?-inline? ?-ipv4? hexipaddr hexipmask hex‐
244       iplist
245              This  command  is  similar to ::ip::isOverlap, however the argu‐
246              ments are in the native form, and the  form  of  the  result  is
247              under  greater  control  of  the  caller.  If the option -all is
248              specified it checks all addresses for overlap,  not  only  until
249              the  first one is found.  If the option -inline is specified the
250              command returns the overlapping prefix instead of index values.
251
252              The result  of  the  command  is,  depending  on  the  specified
253              options,
254
255              no options
256                     The  index  of  the first overlap found, or 0 if there is
257                     none.
258
259              -all   A list containing the indices of all overlaps  found,  or
260                     an empty list if there are none.
261
262              -inline
263                     The  first  overlapping  prefix,  or  an empoty string if
264                     there is none.
265
266              -all -inline
267                     A list containing the prefixes of all overlaps found,  or
268                     an empty list if there are none.
269
270
271         % ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff}}
272         0
273
274         % ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff} {0x01010101 0xffffffff}}
275         2
276
277
278       ::ip::ipToLayer2Multicast ipaddr
279              This  command  an  converts  ipv4  address in dotted form into a
280              layer 2 multicast address, also in dotted form.
281
282
283                % ::ip::ipToLayer2Multicast 224.0.0.2
284                01.00.5e.00.00.02
285
286
287       ::ip::ipHostFromPrefix prefix ?-exclude prefixExcludeList?
288              This command returns a host address from a prefix  in  the  form
289              "ipaddr/masklen",  also  making  sure  that the result is not an
290              address found in the prefixExcludeList.  The  result  is  an  ip
291              address in dotted form.
292
293
294                %::ip::ipHostFromPrefix  1.1.1.5/24
295                1.1.1.1
296
297                %::ip::ipHostFromPrefix  1.1.1.1/32
298                1.1.1.1
299
300
301       ::ip::reduceToAggregates prefixlist
302              This  command  finds  nets that overlap and filters out the more
303              specifc nets. The prefixes are in either addr/mask  form  or  in
304              native format.  The result is a list containing the non-overlap‐
305              ping ip prefixes from the input.
306
307
308                % ::ip::reduceToAggregates {1.1.1.0/24 1.1.0.0/8  2.1.1.0/24 1.1.1.1/32 }
309                1.0.0.0/8 2.1.1.0/24
310
311
312       ::ip::longestPrefixMatch ipaddr prefixlist ?-ipv4?
313              This command finds longest prefix match from  set  of  prefixes,
314              given  a  specific host address. The prefixes in the list are in
315              either native or dotted form, whereas the  host  address  is  in
316              either  ipprefix  format,  dotted  form,  or  integer form.  The
317              result is the prefix which is the most  specific  match  to  the
318              host address.
319
320
321                % ::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 }
322                1.1.1.0/28
323
324

EXAMPLES

326       % ip::version ::1
327       6
328       % ip::version 127.0.0.1
329       4
330
331
332       % ip::normalize 127/8
333       127.0.0.0/8
334       % ip::contract 192.168.0.0
335       192.168
336       %
337       % ip::normalize fec0::1
338       fec0:0000:0000:0000:0000:0000:0000:0001
339       % ip::contract fec0:0000:0000:0000:0000:0000:0000:0001
340       fec0::1
341
342
343       % ip::equal 192.168.0.4/16 192.168.0.0/16
344       1
345       % ip::equal fec0::1/10 fec0::fe01/10
346       1
347
348

REFERENCES

350       [1]    Postel,  J.  "Internet  Protocol."  RFC  791,   September  1981,
351              (http://www.ietf.org/rfc/rfc791.txt)
352
353       [2]    Hinden, R. and Deering, S., "Internet Protocol Version 6  (IPv6)
354              Addressing     Architecture",     RFC     3513,    April    2003
355              (http://www.ietf.org/rfc/rfc3513.txt)
356

AUTHORS

358       Pat Thoyts
359

SEE ALSO

361       inet(3), ip(7), ipv6(7)
362

KEYWORDS

364       internet address, ip, ipv4, ipv6, rfc 3513
365
367       Copyright (c) 2004, Pat Thoyts
368       Copyright (c) 2005 Aamer Akhter <aakhter@cisco.com>
369
370
371
372
373dns                                  1.1.1                        tcllib_ip(n)
Impressum