ipaddress_link_local 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,995 @@
1
+ = IPAddress
2
+
3
+ IPAddress is a Ruby library designed to make the use of IPv4 and IPv6
4
+ addresses simple, powerful and enjoyable. It provides a complete set of
5
+ methods to handle IP addresses for any need, from simple scripting to
6
+ full network design.
7
+
8
+ IPAddress is written with a full OO interface, and its code is easy to
9
+ read, maintain and extend. The documentation is full of examples, to
10
+ let you start being productive immediately.
11
+
12
+ This document provides a brief introduction to the library and
13
+ examples of typical usage.
14
+
15
+ == Requirements
16
+
17
+ * Ruby 1.9.3 or later
18
+
19
+ Please refer to {Travis CI}[https://travis-ci.org/ipaddress-gem/ipaddress] for Build Tests on specific versions of Ruby.
20
+
21
+ {<img src="https://travis-ci.org/ipaddress-gem/ipaddress.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/ipaddress-gem/ipaddress] {<img src="https://codeclimate.com/github/ipaddress-gem/ipaddress/badges/gpa.svg" />}[https://codeclimate.com/github/ipaddress-gem/ipaddress] {<img src="https://www.versioneye.com/user/projects/57001305fcd19a0051853bde/badge.svg?style=flat" alt="Dependency Status" />}[https://www.versioneye.com/user/projects/57001305fcd19a0051853bde]
22
+
23
+ IPAddress 0.8.2 was manually tested on:
24
+
25
+ * ruby-1.8.7-p334 [ i386 ]
26
+ * ree-1.8.7-2011.03 [ i386 ]
27
+ * rbx-head [ ]
28
+ * jruby-1.6.1 [ linux-i386-java ]
29
+ * ruby-1.9.1-p431 [ i386 ]
30
+ * ruby-1.9.2-p180 [ i386 ]
31
+ * ruby-2.0.0-p353 [ x86_64-darwin14.0.0 ]
32
+ * ruby-2.1.3-p242 [ x86_64-darwin14.0.0 ]
33
+
34
+ If you want to contribute, please refer to {Contributing.md}[https://github.com/ipaddress-gem/ipaddress/blob/master/CONTRIBUTING.md].
35
+
36
+
37
+ == Installation
38
+
39
+ Install the library using rubygems
40
+
41
+ $ gem install ipaddress
42
+
43
+ You can then use it in your programs:
44
+
45
+ require 'rubygems' # optional
46
+ require 'ipaddress'
47
+
48
+ Another way would be to clone the git repository
49
+
50
+ $ git clone git://github.com/bluemonk/ipaddress.git
51
+
52
+ And then install the library
53
+
54
+ $ cd ipaddress
55
+ ipaddress$ rake install
56
+
57
+ == Documentation
58
+
59
+ The code is fully documented with RDoc. You can generate the
60
+ documentation with Rake:
61
+
62
+ ipaddress$ rake rdoc
63
+
64
+ The latest documentation can be found online at
65
+ {this address}[http://rubydoc.info/gems/ipaddress/0.8.0/frames]
66
+
67
+ == IPv4
68
+
69
+ Class `IPAddress::IPv4` is used to handle IPv4 type addresses. IPAddress
70
+ is similar to other IP Addresses libraries, like Ruby's own
71
+ IPAddr. However it works slightly different, as we will see.
72
+
73
+ === Create a new IPv4 address
74
+
75
+ The usual way to express an IP Address is using its dotted decimal
76
+ form, such as `172.16.10.1`, and a prefix, such as `24`, separated by a
77
+ slash.
78
+
79
+ 172.16.10.1/24
80
+
81
+ To create a new IPv4 object, you can use IPv4 own class
82
+
83
+ ip = IPAddress::IPv4.new "172.16.10.1/24"
84
+
85
+ or, in a easier way, using the IPAddress parse method
86
+
87
+ ip = IPAddress.parse "172.16.10.1/24"
88
+
89
+ which accepts and parses any kind of IP (uint32, IPv4, IPV6 and
90
+ IPv4 IPv6 Mapped addresses).
91
+
92
+ If you like syntactic sugar, you can use the wrapper method
93
+ `IPAddress()`, which is built around `IPAddress::parse`:
94
+
95
+ ip = IPAddress "172.16.10.1/24"
96
+
97
+ You can specify an IPv4 address in any of two ways:
98
+
99
+ IPAddress "172.16.10.1/24"
100
+ IPAddress "172.16.10.1/255.255.255.0"
101
+
102
+ In this example, prefix `/24` and netmask `255.255.255.0` are the same and
103
+ you have the flexibility to use either one of them.
104
+
105
+ If you don't explicitly specify the prefix (or the subnet mask),
106
+ IPAddress thinks you're dealing with host addresses and not with
107
+ networks. Therefore, the default prefix will be `/32`, or
108
+ `255.255.255.255`. For example:
109
+
110
+ # let's declare an host address
111
+ host = IPAddress::IPv4.new "10.1.1.1"
112
+
113
+ puts host.to_string
114
+ #=> "10.1.1.1/32"
115
+
116
+ The new created object has prefix `/32`, which is the same
117
+ as we created the following:
118
+
119
+ host = IPAddress::IPv4.new "10.1.1.1/32"
120
+
121
+ You can also pass a `uint32` to obtain an `IPAddress::IPv4` object:
122
+
123
+ # Create host object
124
+ ip = IPAddress 167837953
125
+ puts ip.to_string
126
+ #=> "10.1.1.1/32"
127
+
128
+ === Handling the IPv4 address
129
+
130
+ Once created, you can obtain the attributes for an IPv4 object:
131
+
132
+ ip = IPAddress("172.16.10.1/24")
133
+
134
+ ip.address
135
+ #=> "172.16.10.1"
136
+ ip.prefix
137
+ #=> 24
138
+
139
+ In case you need to retrieve the netmask in IPv4 format, you can use
140
+ the `IPv4#netmask` method:
141
+
142
+ ip.netmask
143
+ #=> "255.255.255.0"
144
+
145
+ A special attribute, `IPv4#octets`, is available to get the four
146
+ decimal octets from the IP address:
147
+
148
+ ip.octets
149
+ #=> [172,16,10,1]
150
+
151
+ Shortcut method `IPv4#[]`, provides access to a given octet whithin the
152
+ range:
153
+
154
+ ip[1]
155
+ #=> 16
156
+
157
+ If you need to print out the IPv4 address in a canonical form, you can
158
+ use `IPv4#to_string`:
159
+
160
+ ip.to_string
161
+ #=> "172.16.10.l/24"
162
+
163
+ === Changing netmask
164
+
165
+ You can set a new prefix (netmask) after creating an IPv4
166
+ object. For example:
167
+
168
+ ip.prefix = 25
169
+
170
+ ip.to_string
171
+ #=> "172.16.10.l/25"
172
+
173
+ If you need to use a netmask in IPv4 format, you can achive so by
174
+ using the `IPv4#netmask=` method:
175
+
176
+ ip.netmask = "255.255.255.252"
177
+
178
+ ip.to_string
179
+ #=> "172.16.10.1/30"
180
+
181
+ === Working with networks, broadcasts and addresses
182
+
183
+ Some very important topics in dealing with IP addresses are the
184
+ concepts of +network+ and +broadcast+, as well as the addresses
185
+ included in a range.
186
+
187
+ When you specify an IPv4 address such as `172.16.10.1/24`, you are
188
+ actually handling two different information:
189
+
190
+ * The IP address itself, "172.16.10.1"
191
+ * The subnet mask which indicates the network
192
+
193
+ The network number is the IP which has all zeroes in the host
194
+ portion. In our example, because the prefix is 24, we identify our
195
+ network number to have the last 8 (32-24) bits all zeroes. Thus, IP
196
+ address `172.16.10.1/24` belongs to network `172.16.10.0/24`.
197
+
198
+ This is very important because, for instance, IP `172.16.10.1/16` is
199
+ very different to the previous one, belonging to the very different
200
+ network `172.16.0.0/16`.
201
+
202
+ ==== Networks
203
+
204
+ With IPAddress it's very easy to calculate the network for an IP
205
+ address:
206
+
207
+ ip = IPAddress "172.16.10.1/24"
208
+
209
+ net = ip.network
210
+ #=> #<IPAddress::IPv4:0xb7a5ab24 @octets=[172, 16, 10, 0],
211
+ @prefix=24,
212
+ @address="172.16.10.0">
213
+ net.to_string
214
+ #=> "172.16.10.0/24"
215
+
216
+ Method IPv4#network creates a new IPv4 object from the network
217
+ number, calculated after the original object. We want to outline here
218
+ that the network address is a perfect legitimate IPv4 address, which
219
+ just happen to have all zeroes in the host portion.
220
+
221
+ You can use method `IPv4#network?` to check whether an IP address is a
222
+ network or not:
223
+
224
+ ip1 = IPAddress "172.16.10.1/24"
225
+ ip2 = IPAddress "172.16.10.4/30"
226
+
227
+ ip1.network?
228
+ #=> false
229
+ ip2.network?
230
+ #=> true
231
+
232
+ ==== Broadcast
233
+
234
+ The broadcast address is the contrary than the network number: where
235
+ the network number has all zeroes in the host portion, the broadcast
236
+ address has all one's. For example, ip `172.16.10.1/24` has broadcast
237
+ `172.16.10.255/24`, where ip `172.16.10.1/16` has broadcast
238
+ `172.16.255.255/16`.
239
+
240
+ Method `IPv4#broadcast` has the same behavior as is `#network`
241
+ counterpart: it creates a new IPv4 object to handle the broadcast
242
+ address:
243
+
244
+ ip = IPAddress "172.16.10.1/24"
245
+
246
+ bcast = ip.broadcast
247
+ #=> #<IPAddress::IPv4:0xb7a406fc @octets=[172, 16, 10, 255],
248
+ @prefix=24,
249
+ @address="172.16.10.255">
250
+ bcast.to_string
251
+ #=> "172.16.10.255/24"
252
+
253
+ ==== Addresses, ranges and iterators
254
+
255
+ So we see that the netmask essentially specifies a range for IP
256
+ addresses that are included in a network: all the addresses between
257
+ the network number and the broadcast. IPAddress has many methods to
258
+ iterate between those addresses. Let's start with `IPv4#each`, which
259
+ iterates over all addresses in a range
260
+
261
+ ip = IPAddress "172.16.10.1/24"
262
+
263
+ ip.each do |addr|
264
+ puts addr
265
+ end
266
+
267
+ It is important to note that it doesn't matter if the original IP is a
268
+ host IP or a network number (or a broadcast address): the #each method
269
+ only considers the range that the original IP specifies.
270
+
271
+ If you only want to iterate over hosts IP, use the `IPv4#each_host`
272
+ method:
273
+
274
+ ip = IPAddress "172.16.10.1/24"
275
+
276
+ ip.each_host do |host|
277
+ puts host
278
+ end
279
+
280
+ Methods `IPv4#first` and `IPv4#last` return a new object containing
281
+ respectively the first and the last host address in the range
282
+
283
+ ip = IPAddress "172.16.10.100/24"
284
+
285
+ ip.first.to_string
286
+ #=> "172.16.10.1/24"
287
+
288
+ ip.last.to_string
289
+ #=> "172.16.10.254/24"
290
+
291
+ Checking if an address is loopback is easy with the `IPv4#loopback?`
292
+ method:
293
+
294
+ ip = IPAddress "127.0.0.1"
295
+
296
+ ip.loopback?
297
+ #=> true
298
+
299
+ Checking if an address is in the multicast range can be done using the `IPv4#multicast?`
300
+ method:
301
+
302
+ ip = IPAddress "224.0.0.1/32"
303
+
304
+ ip.multicast?
305
+ #=> true
306
+
307
+ The ability to generate a range also exists by using the `IPv4#to()` method. This allows you to create a subnet agnostic range based off a fixed amount.
308
+
309
+ ip = IPAddress "172.16.10.100/24"
310
+ ip.to('172.16.10.110')
311
+ #=> ["172.16.10.100", ..., "172.16.10.110"]
312
+
313
+ === IP special formats
314
+
315
+ The IPAddress library provides a complete set of methods to access an
316
+ IPv4 address in special formats, such as binary, 32 bits unsigned int,
317
+ data and hexadecimal.
318
+
319
+ Let's take the following IPv4 as an example:
320
+
321
+ ip = IPAddress "172.16.10.1/24"
322
+
323
+ ip.address
324
+ #=> "172.16.10.1"
325
+
326
+ The first thing to highlight here is that all these conversion methods
327
+ only take into consideration the address portion of an IPv4 object and
328
+ not the prefix (netmask).
329
+
330
+ So, to express the address in binary format, use the `IPv4#bits` method:
331
+
332
+ ip.bits
333
+ #=> "10101100000100000000101000000001"
334
+
335
+ To calculate the 32 bits unsigned int format of the ip address, use
336
+ the `IPv4#to_u32` method
337
+
338
+ ip.to_u32
339
+ #=> 2886732289
340
+
341
+ This method is the equivalent of the Unix call `pton()`, expressing an
342
+ IP address in the so called +network byte order+ notation. However, if
343
+ you want to transmit your IP over a network socket, you might need to
344
+ transform it in data format using the `IPv4#data` method:
345
+
346
+ ip.data
347
+ #=> "\254\020\n\001"
348
+
349
+ Also, you can transform an IPv4 address into a format which is
350
+ suitable to use in IPv4-IPv6 mapped addresses:
351
+
352
+ ip.to_ipv6
353
+ #=> "ac10:0a01"
354
+
355
+ Finally, much like `IPv4#to_ipv6` you can use to `IPv4#to_h` method to return a non-semicolon delineated string (useful with pcap/byte level usage):
356
+
357
+ ip.to_h
358
+ #=> "ac100a01"
359
+
360
+ === Classful networks
361
+
362
+ IPAddress allows you to create and manipulate objects using the old
363
+ and deprecated (but apparently still popular) classful networks concept.
364
+
365
+ Classful networks and addresses don't have a prefix: their subnet mask
366
+ is univocally identified by their address, and therefore divided in classes.
367
+ As per RFC 791, these classes are:
368
+
369
+ * Class A, from 0.0.0.0 to 127.255.255.255
370
+ * Class B, from 128.0.0.0 to 191.255.255.255
371
+ * Class C, from 192.0.0.0 to 255.255.255.255
372
+
373
+ Since classful networks here are only considered to calculate the default
374
+ prefix number, classes D and E are not considered.
375
+
376
+ To create a classful IP and prefix from an IP address, use the
377
+ IPv4::parse_classful method:
378
+
379
+ # classful ip
380
+ ip = IPAddress::IPv4::parse_classful "10.1.1.1"
381
+
382
+ ip.prefix
383
+ #=> 8
384
+
385
+ The method automatically created a new IPv4 object and assigned it
386
+ the correct prefix.
387
+
388
+ You can easily check which CLASSFUL network an IPv4 object belongs:
389
+
390
+ ip = IPAddress("10.0.0.1/24")
391
+ ip.a?
392
+ #=> true
393
+
394
+ ip = IPAddress("172.16.10.1/24")
395
+ ip.b?
396
+ #=> true
397
+
398
+ ip = IPAddress("192.168.1.1/30")
399
+ ip.c?
400
+ #=> true
401
+
402
+ Remember that these methods are only checking the address portion of an IP, and are
403
+ independent from its prefix, as classful networks have no concept of prefix.
404
+
405
+ For more information on CLASSFUL networks visit the
406
+ {Wikipedia page}[http://en.wikipedia.org/wiki/Classful_network]
407
+
408
+ === Network design with IPAddress
409
+
410
+ IPAddress includes a lot of useful methods to manipulate IPv4 and IPv6
411
+ networks and do some basic network design.
412
+
413
+ ==== Subnetting
414
+
415
+ The process of subnetting is the division of a network into smaller
416
+ (in terms of hosts capacity) networks, called subnets, so that they
417
+ all share a common root, which is the starting network.
418
+
419
+ For example, if you have network "172.16.10.0/24", we can subnet it
420
+ into 4 smaller subnets. The new prefix will be /26, because 4 is 2^2
421
+ and therefore we add 2 bits to the network prefix (24+2=26).
422
+
423
+ Subnetting is easy with IPAddress. You actually have two options:
424
+
425
+ * IPv4#subnet: specify a new prefix
426
+ * IPv4#split: tell IPAddress how many subnets you want to create.
427
+
428
+ Let's examine `IPv4#subnet` first. Say you have network "172.16.10.0/24"
429
+ and you want to subnet it into /26 networks. With IPAddress it's very
430
+ easy:
431
+
432
+ network = IPAddress "172.16.10.0/24"
433
+
434
+ subnets = network.subnet(26)
435
+
436
+ subnets.map{|i| i.to_string}
437
+ #=> ["172.16.10.0/26",
438
+ "172.16.10.64/26",
439
+ "172.16.10.128/26",
440
+ "172.16.10.192/26"]
441
+
442
+ As you can see, an Array has been created, containing 4 new IPv4 objects
443
+ representing the new subnets.
444
+
445
+ Another way to create subnets is to tell IPAddress how many subnets you'd
446
+ like to have, and letting the library calculate the new prefix for you.
447
+
448
+ Let's see how it works, using `IPv4#split` method. Say you want 4 new subnets:
449
+
450
+ network = IPAddress("172.16.10.0/24")
451
+
452
+ subnets = network.split(4)
453
+
454
+ subnets.map{|i| i.to_string}
455
+ #=> ["172.16.10.0/26",
456
+ "172.16.10.64/26",
457
+ "172.16.10.128/26",
458
+ "172.16.10.192/26"]
459
+
460
+ Hey, that's the same result as before! This actually makes sense, as the
461
+ two operations are complementary. When you use `IPv4#subnet` with the new
462
+ prefix, IPAddress will always create a number of subnets that is a power
463
+ of two. This is equivalent to use IPv4#split with a power of 2.
464
+
465
+ Where `IPv4#split` really shines is with the so called "uneven subnetting".
466
+ You are not limited to split a network into a power-of-two numbers of
467
+ subnets: IPAddress lets you create any number of subnets, and it will
468
+ try to organize the new created network in the best possible way, making
469
+ an efficient allocation of the space.
470
+
471
+ An example here is worth a thousand words. Let's use the same network
472
+ as the previous examples:
473
+
474
+ network = IPAddress("172.16.10.0/24")
475
+
476
+ How do we split this network into 3 subnets? Very easy:
477
+
478
+ subnets = network.split(3)
479
+
480
+ subnets.map{|i| i.to_string}
481
+ #=> ["172.16.10.0/26",
482
+ "172.16.10.64/26",
483
+ "172.16.10.128/25"]
484
+
485
+ As you can see, IPAddress tried to perform a good allocation by filling up
486
+ all the address space from the original network. There is no point in splitting
487
+ a network into 3 subnets like `172.16.10.0/26`, `172.16.10.64/26` and
488
+ `172.16.10.128/26`, as you would end up having `172.16.10.192/26` wasted (plus,
489
+ I suppose I wouldn't need a Ruby library to perform un-efficient IP
490
+ allocation, as I do that myself very well ;) ).
491
+
492
+ We can go even further and split into 11 subnets:
493
+
494
+ network.split(11)
495
+ #=> ["172.16.10.0/28", "172.16.10.16/28", "172.16.10.32/28",
496
+ "172.16.10.48/28", "172.16.10.64/28", "172.16.10.80/28",
497
+ "172.16.10.96/28", "172.16.10.112/28", "172.16.10.128/27",
498
+ "172.16.10.160/27", "172.16.10.192/26"]
499
+
500
+ As you can see, most of the networks are `/28`, with a few `/27` and one
501
+ `/26` to fill up the remaining space.
502
+
503
+ ==== Summarization
504
+
505
+ Summarization (or aggregation) is the process when two or more
506
+ networks are taken together to check if a supernet, including
507
+ all and only these networks, exists. If it exists then this supernet
508
+ is called the summarized (or aggregated) network.
509
+ It is very important to understand that summarization can only
510
+ occur if there are no holes in the aggregated network, or, in
511
+ other words, if the given networks fill completely the address space
512
+ of the supernet. So the two rules are:
513
+
514
+ 1) The aggregate network must contain +all+ the IP addresses of the
515
+ original networks;
516
+
517
+ 2) The aggregate network must contain +only+ the IP addresses of the
518
+ original networks;
519
+
520
+ A few examples will help clarify the above. Let's consider for
521
+ instance the following two networks:
522
+
523
+ ip1 = IPAddress("172.16.10.0/24")
524
+ ip2 = IPAddress("172.16.11.0/24")
525
+
526
+ These two networks can be expressed using only one IP address
527
+ network if we change the prefix. Let Ruby do the work:
528
+
529
+ IPAddress::IPv4::summarize(ip1,ip2).map(&:to_string)
530
+ #=> "172.16.10.0/23"
531
+
532
+ We note how the network `172.16.10.0/23` includes all the
533
+ addresses specified in the above networks, and (more important) includes
534
+ ONLY those addresses.
535
+
536
+ If we summarized +ip1+ and +ip2+ with the following network:
537
+
538
+ "172.16.0.0/16"
539
+
540
+ we would have satisfied rule #1 above, but not rule #2. So
541
+
542
+ "172.16.0.0/16"
543
+
544
+ is not an aggregate network for +ip1+ and +ip2+.
545
+
546
+ If it's not possible to compute a single aggregated network for
547
+ all the original networks, the method returns an array with all the
548
+ aggregate networks found. For example, the following four networks can be
549
+ aggregated in a single `/22`:
550
+
551
+ ip1 = IPAddress("10.0.0.1/24")
552
+ ip2 = IPAddress("10.0.1.1/24")
553
+ ip3 = IPAddress("10.0.2.1/24")
554
+ ip4 = IPAddress("10.0.3.1/24")
555
+
556
+ IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
557
+ #=> ["10.0.0.0/22"]
558
+
559
+ But the following networks can't be summarized in a single
560
+ network:
561
+
562
+ ip1 = IPAddress("10.0.1.1/24")
563
+ ip2 = IPAddress("10.0.2.1/24")
564
+ ip3 = IPAddress("10.0.3.1/24")
565
+ ip4 = IPAddress("10.0.4.1/24")
566
+
567
+ IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
568
+ #=> ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
569
+
570
+ In this case, the two summarizables networks have been aggregated into
571
+ a single `/23`, while the other two networks have been left untouched.
572
+
573
+ ==== Supernetting
574
+
575
+ Supernetting is a different operation than aggregation, as it only
576
+ works on a single network and returns a new single IPv4 object,
577
+ representing the supernet.
578
+
579
+ Supernetting is similar to subnetting, except that you getting as a
580
+ result a network with a smaller prefix (bigger host space). For
581
+ example, given the network
582
+
583
+ ip = IPAddress("172.16.10.0/24")
584
+
585
+ you can supernet it with a new /23 prefix
586
+
587
+ ip.supernet(23).to_string
588
+ #=> "172.16.10.0/23"
589
+
590
+ However if you supernet it with a `/22` prefix, the network address will
591
+ change:
592
+
593
+ ip.supernet(22).to_string
594
+ #=> "172.16.8.0/22"
595
+
596
+ This is because `172.16.10.0/22` is not a network anymore, but an host
597
+ address.
598
+
599
+ == IPv6
600
+
601
+ IPAddress is not only fantastic for IPv4 addresses, it's also great to
602
+ handle IPv6 addresses family! Let's discover together how to use it in
603
+ our projects.
604
+
605
+ === IPv6 addresses
606
+
607
+ IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
608
+ which are only 32 bits long. An IPv6 address is generally written as
609
+ eight groups of four hexadecimal digits, each group representing 16
610
+ bits or two octet. For example, the following is a valid IPv6
611
+ address:
612
+
613
+ 2001:0db8:0000:0000:0008:0800:200c:417a
614
+
615
+ Letters in an IPv6 address are usually written downcase, as per
616
+ RFC. You can create a new IPv6 object using uppercase letters, but
617
+ they will be converted.
618
+
619
+ ==== Compression
620
+
621
+ Since IPv6 addresses are very long to write, there are some
622
+ simplifications and compressions that you can use to shorten them.
623
+
624
+ * Leading zeroes: all the leading zeroes within a group can be
625
+ omitted: "0008" would become "8"
626
+
627
+ * A string of consecutive zeroes can be replaced by the string
628
+ "::". This can be only applied once.
629
+
630
+ Using compression, the IPv6 address written above can be shorten into
631
+ the following, equivalent, address
632
+
633
+ 2001:db8::8:800:200c:417a
634
+
635
+ This short version is often used in human representation.
636
+
637
+ ==== Network Mask
638
+
639
+ As we used to do with IPv4 addresses, an IPv6 address can be written
640
+ using the prefix notation to specify the subnet mask:
641
+
642
+ 2001:db8::8:800:200c:417a/64
643
+
644
+ The /64 part means that the first 64 bits of the address are
645
+ representing the network portion, and the last 64 bits are the host
646
+ portion.
647
+
648
+ === Using IPAddress with IPv6 addresses
649
+
650
+ All the IPv6 representations we've just seen are perfectly fine when
651
+ you want to create a new IPv6 address:
652
+
653
+ ip6 = IPAddress "2001:0db8:0000:0000:0008:0800:200C:417A"
654
+
655
+ ip6 = IPAddress "2001:db8:0:0:8:800:200C:417A"
656
+
657
+ ip6 = IPAddress "2001:db8:8:800:200C:417A"
658
+
659
+ All three are giving out the same IPv6 object. The default subnet mask
660
+ for an IPv6 is 128, as IPv6 addresses don't have classes like IPv4
661
+ addresses. If you want a different mask, you can go ahead and explicit
662
+ it:
663
+
664
+ ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
665
+
666
+ Access the address portion and the prefix by using the respective
667
+ methods:
668
+
669
+ ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
670
+
671
+ ip6.address
672
+ #=> "2001:0db8:0000:0000:0008:0800:200c:417a"
673
+
674
+ ip6.prefix
675
+ #=> 64
676
+
677
+ A compressed version of the IPv6 address can be obtained with the
678
+ `IPv6#compressed` method:
679
+
680
+ ip6 = IPAddress "2001:0db8:0000:0000:0008:200c:417a:00ab/64"
681
+
682
+ ip6.compressed
683
+ #=> "2001:db8::8:800:200c:417a"
684
+
685
+ === Handling the IPv6 address
686
+
687
+ Accessing the groups that form an IPv6 address is very easy with the
688
+ `IPv6#groups` method:
689
+
690
+ ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
691
+
692
+ ip6.groups
693
+ #=> [8193, 3512, 0, 0, 8, 2048, 8204, 16762]
694
+
695
+ As with IPv4 addresses, each individual group can be accessed using
696
+ the `IPv6#[]` shortcut method:
697
+
698
+ ip6[0]
699
+ #=> 8193
700
+ ip6[1]
701
+ #=> 3512
702
+ ip6[2]
703
+ #=> 0
704
+ ip6[3]
705
+ #=> 0
706
+
707
+ Note that each 16 bits group is expressed in its decimal form. You can
708
+ also obtain the groups into hexadecimal format using the `IPv6#hexs`
709
+ method:
710
+
711
+ ip6.hexs
712
+ #=> => ["2001", "0db8", "0000", "0000", "0008", "0800", "200c", "417a"]
713
+
714
+ A few other methods are available to transform an IPv6 address into
715
+ decimal representation, with `IPv6.to_i`
716
+
717
+ ip6.to_i
718
+ #=> 42540766411282592856906245548098208122
719
+
720
+ or to hexadecimal representation
721
+
722
+ ip6.to_hex
723
+ #=> "20010db80000000000080800200c417a"
724
+
725
+ To print out an IPv6 address in human readable form, use the `IPv6#to_s`, `IPv6#to_string`
726
+ and `IPv6#to_string_uncompressed` methods
727
+
728
+ ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
729
+
730
+ ip6.to_string
731
+ #=> "2001:db8::8:800:200c:417a/96"
732
+
733
+ ip6.to_string_uncompressed
734
+ #=> "2001:0db8:0000:0000:0008:0800:200c:417a/96"
735
+
736
+ As you can see, `IPv6.to_string` prints out the compressed form, while
737
+ `IPv6.to_string_uncompressed` uses the expanded version.
738
+
739
+ ==== Compressing and uncompressing
740
+
741
+ If you have a string representing an IPv6 address, you can easily
742
+ compress it and uncompress it using the two class methods IPv6::expand
743
+ and IPv6::compress.
744
+
745
+ For example, let's say you have the following uncompressed IPv6
746
+ address:
747
+
748
+ ip6str = "2001:0DB8:0000:CD30:0000:0000:0000:0000"
749
+
750
+ Here is the compressed version:
751
+
752
+ IPAddress::IPv6.compress ip6str
753
+ #=> "2001:db8:0:cd30::"
754
+
755
+ The other way works as well:
756
+
757
+ ip6str = "2001:db8:0:cd30::"
758
+
759
+ IPAddress::IPv6.expand ip6str
760
+ #=> "2001:0DB8:0000:CD30:0000:0000:0000:0000"
761
+
762
+ These methods can be used when you don't want to create a new object
763
+ just for expanding or compressing an address (although a new object is
764
+ actually created internally).
765
+
766
+ === New IPv6 address from other formats
767
+
768
+ You can create a new IPv6 address from different formats than just a
769
+ string representing the colon-hex groups.
770
+
771
+ For instance, if you have a data stream, you can use `IPv6::parse_data`,
772
+ like in the following example:
773
+
774
+ data = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
775
+
776
+ ip6 = IPAddress::IPv6::parse_data data
777
+ ip6.prefix = 64
778
+
779
+ ip6.to_string
780
+ #=> "2001:db8::8:800:200c:417a/64"
781
+
782
+ A new IPv6 address can also be created from an unsigned 128 bits
783
+ integer:
784
+
785
+ u128 = 42540766411282592856906245548098208122
786
+
787
+ ip6 = IPAddress::IPv6::parse_u128 u128
788
+ ip6.prefix = 64
789
+
790
+ ip6.to_string
791
+ #=>"2001:db8::8:800:200c:417a/64"
792
+
793
+ Finally, a new IPv6 address can be created from an hex string:
794
+
795
+ hex = "20010db80000000000080800200c417a"
796
+
797
+ ip6 = IPAddress::IPv6::parse_hex hex
798
+ ip6.prefix = 64
799
+
800
+ ip6.to_string
801
+ #=> "2001:db8::8:800:200c:417a/64"
802
+
803
+ === Special IPv6 addresses
804
+
805
+ Some IPv6 have a special meaning and are expressed in a special form,
806
+ quite different than an usual IPv6 address. IPAddress has built-in
807
+ support for unspecified, loopback and mapped IPv6 addresses.
808
+
809
+ ==== Unspecified address
810
+
811
+ The address with all zero bits is called the +unspecified+ address
812
+ (corresponding to `0.0.0.0` in IPv4). It should be something like this:
813
+
814
+ 0000:0000:0000:0000:0000:0000:0000:0000
815
+
816
+ but, with the use of compression, it is usually written as just two
817
+ colons:
818
+
819
+ ::
820
+
821
+ or, specifying the netmask:
822
+
823
+ ::/128
824
+
825
+ With IPAddress, create a new unspecified IPv6 address using its own
826
+ subclass:
827
+
828
+ ip = IPAddress::IPv6::Unspecified.new
829
+
830
+ ip.to_string
831
+ #=> "::/128"
832
+
833
+ You can easily check if an IPv6 object is an unspecified address by
834
+ using the IPv6#unspecified? method
835
+
836
+ ip.unspecified?
837
+ #=> true
838
+
839
+ An unspecified IPv6 address can also be created with the wrapper
840
+ method, like we've seen before
841
+
842
+ ip = IPAddress "::"
843
+
844
+ ip.unspecified?
845
+ #=> true
846
+
847
+ This address must never be assigned to an interface and is to be used
848
+ only in software before the application has learned its host's source
849
+ address appropriate for a pending connection. Routers must not forward
850
+ packets with the unspecified address.
851
+
852
+ ==== Loopback address
853
+
854
+ The loopback address is a unicast localhost address. If an
855
+ application in a host sends packets to this address, the IPv6 stack
856
+ will loop these packets back on the same virtual interface.
857
+
858
+ Loopback addresses are expressed in the following form:
859
+
860
+ ::1
861
+
862
+ or, with their appropriate prefix,
863
+
864
+ ::1/128
865
+
866
+ As for the unspecified addresses, IPv6 loopbacks can be created with
867
+ IPAddress calling their own class:
868
+
869
+ ip = IPAddress::IPv6::Loopback.new
870
+
871
+ ip.to_string
872
+ #=> "::1/128"
873
+
874
+ or by using the wrapper:
875
+
876
+ ip = IPAddress "::1"
877
+
878
+ ip.to_string
879
+ #=> "::1/128"
880
+
881
+ Checking if an address is loopback is easy with the `IPv6#loopback?`
882
+ method:
883
+
884
+ ip.loopback?
885
+ #=> true
886
+
887
+ The IPv6 loopback address corresponds to `127.0.0.1` in IPv4.
888
+
889
+ ==== Mapped address
890
+
891
+ It is usually identified as a IPv4 mapped IPv6 address, a particular
892
+ IPv6 address which aids the transition from IPv4 to IPv6. The
893
+ structure of the address is
894
+
895
+ ::ffff:w.y.x.z
896
+
897
+ where `w.x.y.z` is a normal IPv4 address. For example, the following is
898
+ a mapped IPv6 address:
899
+
900
+ ::ffff:192.168.100.1
901
+
902
+ IPAddress is very powerful in handling mapped IPv6 addresses, as the
903
+ IPv4 portion is stored internally as a normal IPv4 object. Let's have
904
+ a look at some examples. To create a new mapped address, just use the
905
+ class builder itself
906
+
907
+ ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
908
+
909
+ or just use the wrapper method
910
+
911
+ ip6 = IPAddress "::ffff:172.16.10.1/128"
912
+
913
+ Let's check it's really a mapped address:
914
+
915
+ ip6.mapped?
916
+ #=> true
917
+
918
+ ip6.to_string
919
+ #=> "::ffff:172.16.10.1/128"
920
+
921
+ Now with the `#ipv4` attribute, we can easily access the IPv4 portion
922
+ of the mapped IPv6 address:
923
+
924
+ ip6.ipv4.address
925
+ #=> "172.16.10.1"
926
+
927
+ Internally, the IPv4 address is stored as two 16 bits
928
+ groups. Therefore all the usual methods for an IPv6 address are
929
+ working perfectly fine:
930
+
931
+ ip6.to_hex
932
+ #=> "00000000000000000000ffffac100a01"
933
+
934
+ ip6.address
935
+ #=> "0000:0000:0000:0000:0000:ffff:ac10:0a01"
936
+
937
+ A mapped IPv6 can also be created just by specify the address in the
938
+ following format:
939
+
940
+ ip6 = IPAddress "::172.16.10.1"
941
+
942
+ That is, two colons and the IPv4 address. However, as by RFC, the `ffff`
943
+ group will be automatically added at the beginning
944
+
945
+ ip6.to_string
946
+ => "::ffff:172.16.10.1/128"
947
+
948
+ making it a mapped IPv6 compatible address.
949
+
950
+ == Why not using IPAddr?
951
+
952
+ IPAddr is the IP addresses library that comes with Ruby standard
953
+ lib. We found this library, although very well written, not very
954
+ suitable for all our needs, and not very flexible.
955
+
956
+ Some quick examples of things you can't do with IPAddr:
957
+
958
+ * store both the address and the prefix information
959
+ * quickly find the broadcast address of a network
960
+ * iterate over hosts
961
+ * perform subnetting or network aggregation
962
+
963
+ Many methods and procedures are so old that they have been
964
+ declared deprecated by the IETF, and some others have bugs in their
965
+ implementation.
966
+
967
+ Moreover, IPAddress is more robust and is already around 50% faster than IPAddr,
968
+ in addition to provide an organic API with logical separation and OO structure.
969
+
970
+ We hope that IPAddress will address all these issues and meet all your
971
+ needs in network programming.
972
+
973
+
974
+ == Community
975
+
976
+ Want to join the community?
977
+
978
+ * {IPAddress google group}[http://groups.google.com/group/ruby-ipaddress]
979
+
980
+ We've created a group to discuss about
981
+ IPAddress future development, features and provide some kind of support.
982
+ Feel free to join us and tell us what you think!
983
+
984
+ == Thanks to
985
+
986
+ Thanks to Luca Russo (vargolo) and Simone Carletti
987
+ (weppos) for all the support and technical review. Thanks to Marco Beri,
988
+ Bryan T. Richardson, Nicolas Fevrier, jdpace, Daniele Alessandri, jrdioko,
989
+ Ghislain Charrier, Pawel Krzesniak, Mark Sullivan, Leif Gensert,
990
+ Erik Ahlström, Peter Vandenberk and Steve Rawlinson for their support,
991
+ feedback and bug reports.
992
+
993
+ == Copyright
994
+
995
+ Copyright (c) 2009-2015 Marco Ceresa and Mike Mackintosh. See LICENSE for details.