1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
[vset IP_VERSION 1.3]
[manpage_begin tcllib_ip n [vset IP_VERSION]]
[see_also inet(3)]
[see_also ip(7)]
[see_also ipv6(7)]
[keywords {internet address}]
[keywords ip]
[keywords ipv4]
[keywords ipv6]
[keywords {rfc 3513}]
[copyright {2004, Pat Thoyts}]
[copyright {2005 Aamer Akhter <aakhter@cisco.com>}]
[moddesc {Domain Name Service}]
[titledesc {IPv4 and IPv6 address manipulation}]
[category Networking]
[require Tcl 8.2]
[require ip [opt [vset IP_VERSION]]]
[description]
[para]
This package provides a set of commands to help in parsing, displaying
and comparing internet addresses. The package can handle both IPv4 (1)
and IPv6 (2) address types.
[section COMMANDS]
[list_begin definitions]
[call [cmd ::ip::version] [arg address]]
Returns the protocol version of the address (4 or 6) or 0 if the
address is neither IPv4 or IPv6.
[call [cmd ::ip::is] [arg class] [arg address]]
Returns true if the address is a member of the given protocol
class. The class parameter may be either [arg ipv4] or [arg ipv6]
This is effectively a boolean equivalent of the [cmd version]
command. The [arg class] argument may be shortened to [arg 4] or
[arg 6].
[call [cmd ::ip::equal] [arg address] [arg address]]
Compare two address specifications for equivalence. The arguments are
normalized and the address prefix determined (if a mask is
supplied). The normalized addresses are then compared bit-by-bit and
the procedure returns true if they match.
[call [cmd ::ip::normalize] [arg address]]
Convert an IPv4 or IPv6 address into a fully expanded version. There
are various shorthand ways to write internet addresses, missing out
redundant parts or digts.. This procedure is the opposite of
[cmd contract].
[call [cmd ::ip::contract] [arg address]]
Convert a [cmd normalize]d internet address into a more compact form
suitable for displaying to users.
[call [cmd ::ip::distance] [arg ipaddr1] [arg ipaddr2]]
This command computes the (integer) distance from IPv4 address
[arg ipaddr1] to IPv4 address [arg ipaddr2], i.e. "ipaddr2 - ipaddr1"
[para]
[example {
% ::ip::distance 1.1.1.1 1.1.1.5
4
}]
[call [cmd ::ip::nextIp] [arg ipaddr] [opt [arg offset]]]
This command adds the integer [arg offset] to the IPv4 address [arg ipaddr]
and returns the new IPv4 address.
[para]
[example {
% ::ip::distance 1.1.1.1 4
1.1.1.5
}]
[call [cmd ::ip::prefix] [arg address]]
Returns the address prefix generated by masking the address part with
the mask if provided. If there is no mask then it is equivalent to
calling [cmd normalize]
[call [cmd ::ip::type] [arg address]]
[call [cmd ::ip::mask] [arg address]]
If the address supplied includes a mask then this is returned
otherwise returns an empty string.
[call [cmd ::ip::prefixToNative] [arg prefix]]
This command converts the string [arg prefix] from dotted form
(<ipaddr>/<mask> format) to native (hex) form. Returns a list
containing two elements, ipaddress and mask, in this order, in
hexadecimal notation.
[para]
[example {
% ip::prefixToNative 1.1.1.0/24
0x01010100 0xffffff00
}]
[call [cmd ::ip::nativeToPrefix] [arg nativeList]|[arg native] \
[opt [option -ipv4]]]
This command converts from native (hex) form to dotted form.
It is the complement of [cmd ::ip::prefixToNative].
[para]
[list_begin arguments]
[arg_def list nativeList in]
List of several ip addresses in native form. The native form is a list
as returned by [cmd ::ip::prefixToNative].
[arg_def list native in]
A list as returned by [cmd ::ip::prefixToNative].
[list_end]
[para]
The command returns a list of addresses in dotted form if it was
called with a list of addresses. Otherwise a single address in dotted
form is returned.
[para]
[example {
% ip::nativeToPrefix {0x01010100 0xffffff00} -ipv4
1.1.1.0/24
}]
[call [cmd ::ip::intToString] [arg number] [opt [option -ipv4]]]
This command converts from an ip address specified as integer number
to dotted form.
[para]
[example {
ip::intToString 4294967295
255.255.255.255
}]
[call [cmd ::ip::toInteger] [arg ipaddr]]
This command converts a dotted form ip into an integer number.
[para]
[example {
% ::ip::toInteger 1.1.1.0
16843008
}]
[call [cmd ::ip::toHex] [arg ipaddr]]
This command converts dotted form ip into a hexadecimal number.
[para]
[example {
% ::ip::toHex 1.1.1.0
0x01010100
}]
[call [cmd ::ip::maskToInt] [arg ipmask]]
This command convert an ipmask in either dotted (255.255.255.0) form
or mask length form (24) into an integer number.
[para]
[example {
::ip::maskToInt 24
4294967040
}]
[call [cmd ::ip::broadcastAddress] [arg prefix] [opt [option -ipv4]]]
This commands returns a broadcast address in dotted form for the given
route [arg prefix], either in the form "addr/mask", or in native
form. The result is in dotted form.
[para]
[example {
::ip::broadcastAddress 1.1.1.0/24
1.1.1.255
::ip::broadcastAddress {0x01010100 0xffffff00}
0x010101ff
}]
[call [cmd ::ip::maskToLength] \
[arg dottedMask]|[arg integerMask]|[arg hexMask] \
[opt [option -ipv4]]]
This command converts the dotted or integer form of an ipmask to
the mask length form.
[para]
[example {
::ip::maskToLength 0xffffff00 -ipv4
24
% ::ip::maskToLength 255.255.255.0
24
}]
[call [cmd ::ip::lengthToMask] [arg maskLength] \
[opt [option -ipv4]]]
This command converts an ipmask in mask length form to its dotted
form.
[para]
[example {
::ip::lengthToMask 24
255.255.255.0
}]
[call [cmd ::ip::nextNet] [arg ipaddr] [arg ipmask] \
[opt [arg count]] \
[opt [option -ipv4]]]
This command returns an ipaddress in the same position in the
[arg count] next network. The default value for [arg count] is
[const 1].
[para]
The address can be specified as either integer number or in dotted
form. The mask can be specified as either integer number, dotted form,
or mask length form.
[para]
The result is in hex form.
[call [cmd ::ip::isOverlap] [arg prefix] [arg prefix]...]
This command checks if the given ip prefixes overlap. All arguments
are in dotted "addr/mask" form. All arguments after the first prefix
are compared against the first prefix. The result is a boolean
value. It is true if an overlap was found for any of the prefixes.
[para]
[example {
% ::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
}]
[call [cmd ::ip::isOverlapNative] \
[opt [option -all]] \
[opt [option -inline]] \
[opt [option -ipv4]] \
[arg hexipaddr] [arg hexipmask] [arg hexiplist]]
This command is similar to [cmd ::ip::isOverlap], however the
arguments are in the native form, and the form of the result is under
greater control of the caller.
If the option [option -all] is specified it checks all addresses for
overlap, not only until the first one is found.
If the option [option -inline] is specified the command returns the
overlapping prefix instead of index values.
[para]
The result of the command is, depending on the specified options,
[list_begin definitions]
[def {no options}]
The index of the first overlap found, or 0 if there is none.
[def -all]
A list containing the indices of all overlaps found, or an empty list
if there are none.
[def -inline]
The first overlapping prefix, or an empoty string if there is none.
[def {-all -inline}]
A list containing the prefixes of all overlaps found, or an empty list
if there are none.
[list_end]
[para]
[example {
% ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff}}
0
% ::ip::isOverlapNative 0x01010100 0xffffff00 {{0x02010001 0xffffffff} {0x01010101 0xffffffff}}
2
}]
[call [cmd ::ip::ipToLayer2Multicast] [arg ipaddr]]
This command an converts ipv4 address in dotted form into a layer 2
multicast address, also in dotted form.
[para]
[example {
% ::ip::ipToLayer2Multicast 224.0.0.2
01.00.5e.00.00.02
}]
[call [cmd ::ip::ipHostFromPrefix] [arg prefix] \
[opt "[option -exclude] [arg prefixExcludeList]"]]
This command returns a host address from a prefix in the form
"ipaddr/masklen", also making sure that the result is not an address
found in the [arg prefixExcludeList].
The result is an ip address in dotted form.
[para]
[example {
%::ip::ipHostFromPrefix 1.1.1.5/24
1.1.1.1
%::ip::ipHostFromPrefix 1.1.1.1/32
1.1.1.1
}]
[call [cmd ::ip::reduceToAggregates] [arg prefixlist]]
This command finds nets that overlap and filters out the more specific
nets. 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.
[para]
[example {
% ::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
}]
[call [cmd ::ip::longestPrefixMatch] [arg ipaddr] [arg prefixlist] \
[opt [option -ipv4]]]
This command finds longest prefix match from 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 which is the most specific match to the host
address.
[para]
[example {
% ::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
}]
[call [cmd ::ip::collapse] [arg prefixlist]]
This commands takes a list of prefixes and returns a list prefixes
with the largest possible subnet masks covering the input, in this
manner collapsing adjacent prefixes into larger ranges.
[para] This is different from [cmd ::ip::reduceToAggregates] in that
the latter only removes specific nets from a list when they are
covered by other elements of the input whereas this command actively
merges nets into larger ranges when they are adjacent to each other.
[para]
[example {
% ::ip::collapse {1.2.2.0/24 1.2.3.0/24}
1.2.2.0/23
}]
[call [cmd ::ip::subtract] [arg prefixlist]]
This command takes a list of prefixes, some of which are prefixed by a
dash. These latter [term negative] prefixes are used to punch holes
into the ranges described by the other, [term positive],
prefixes. I.e. the negative prefixes are subtracted frrom the positive
ones, resulting in a larger list of describes describing the covered
ranges only as positives.
[list_end]
[comment { ----------------------------------------------------------- }]
[section EXAMPLES]
[para]
[example {
% ip::version ::1
6
% ip::version 127.0.0.1
4
}]
[example {
% 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
}]
[example {
% ip::equal 192.168.0.4/16 192.168.0.0/16
1
% ip::equal fec0::1/10 fec0::fe01/10
1
}]
[comment { ----------------------------------------------------------- }]
[section {REFERENCES}]
[list_begin enumerated]
[enum]
Postel, J. "Internet Protocol." RFC 791, September 1981,
([uri http://www.ietf.org/rfc/rfc791.txt])
[enum]
Hinden, R. and Deering, S.,
"Internet Protocol Version 6 (IPv6) Addressing Architecture",
RFC 3513, April 2003
([uri http://www.ietf.org/rfc/rfc3513.txt])
[list_end]
[section AUTHORS]
Pat Thoyts
[vset CATEGORY dns]
[include ../doctools2base/include/feedback.inc]
[manpage_end]
|