netaddr 1.5.3 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of netaddr might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: fd69b081c04694ed2db113b810b07e19ba7f8c9ebe446bca31af3b9f73955777
4
- data.tar.gz: 4510ea5730575cbc8f606de4edd21ef054ba661fb9b1b2aacbdfa6f872055ad5
2
+ SHA1:
3
+ metadata.gz: 8baac76897c5888273f82c02b7719ac40704dde4
4
+ data.tar.gz: 55829efe0e322cd809cc8113fe88c74e4597f7fb
5
5
  SHA512:
6
- metadata.gz: 12f90e81555cb39d0b51cd10c108b185da1cc4fb48d684858b60e5ada9d3584bb74ec54904335e962a9f14bd194e5c27cee4ab0d017a00ee0a03c223a79d3894
7
- data.tar.gz: 78efa87669e4f37defda27d375e57d2dde8cd356b9a9ceac19e0ade7ce080733ee48e766ca100d11c411e7cb87e0d2230c711a24a1db90d73292fff3b69cea18
6
+ metadata.gz: 64de30c364321364056cd9e50f1468991109203cdc58d7d3d0fa0f7626455ee8c42fd2fabe505efdaefa442b344fb03ed0354c43b39abf31470359007722bec7
7
+ data.tar.gz: ff8813e6e0319932083ea4e994e4194473ac439d579df47b45faf2e2b86a72895be082b33a663ad9705a55a706815685c18c8880ac5313d59b54265dc0e599a1
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,9 +1,29 @@
1
1
  # netaddr
2
- I originally created this package back in 2007 out of the need for a tool
3
- which I could use to track an inventory of constantly changing IP subnets.
4
- At the time, I was in the process of migrating away from Perl and towards Ruby
5
- as my primary scripting language. I have since migrated away from using Ruby so
6
- I have not made any major modifications to this code base since 2008 (aside from a
7
- handful of bug fixes that others have pointed out).
8
-
9
- Dustin Spinhirne
2
+ A Ruby library for performing calculations on IPv4 and IPv6 subnets. There is also limited support for EUI addresses.
3
+
4
+ ### Version 1.x
5
+ The original netaddr which I created in 2007. My use case then was creating an internal IPAM system for Rackspace.
6
+
7
+ ### Version 2.x
8
+ A complete rewrite and totally incompatible with 1.x. My main motivation now is to reduce bug reports resulting from the poor code quality of 1.x.
9
+
10
+
11
+ # Building
12
+ To run unit tests, execute the following from the top level directory
13
+
14
+ ruby test/run_all.rb
15
+
16
+ To build the gem, execute the following from the top level directory
17
+
18
+ gem build netaddr.gemspec
19
+
20
+
21
+ # Examples
22
+ Example code may be found in test/example.rb. This example code runs as part of the unit tests.
23
+
24
+
25
+ # Coding Style
26
+ I use the following conventions:
27
+ * I use tabs for indention since tabs make it really easy to adjust indention widths on the fly.
28
+ * I do not follow rigid limits on line lengths. My editor auto-wraps so I add a line break where it feels appropriate.
29
+ * I'm not a fan of obfuscation. I prefer clear code over fancy code.
data/lib/eui48.rb ADDED
@@ -0,0 +1,65 @@
1
+ module NetAddr
2
+
3
+ # EUI48 (Extended Unique Identifier 48-bit, or EUI-48) represents a 48-bit hardware address.
4
+ # It is typically associated with mac-addresses.
5
+ class EUI48
6
+
7
+ # addr is the Integer representation of this EUI
8
+ attr_reader :addr
9
+
10
+ #Create an EUI48 from an Integer. Must be between 0 and 2**48-1.
11
+ #Throws ValidationError on error.
12
+ def initialize(i)
13
+ if (!i.kind_of?(Integer))
14
+ raise ValidationError, "Expected an Integer for 'i' but got a #{i.class}."
15
+ elsif ( (i < 0) || (i > 2**48-1) )
16
+ raise ValidationError, "#{i} is out of bounds for EUI48."
17
+ end
18
+ @addr = i
19
+ end
20
+
21
+ # Parse an EUI-48 string into an EUI48 type.
22
+ # This will successfully parse most of the typically used formats such as:
23
+ # * aa-bb-cc-dd-ee-ff
24
+ # * aa:bb:cc:dd:ee:ff
25
+ # * aabb.ccdd.eeff
26
+ # * aabbccddeeff
27
+ #
28
+ # Although, in truth, its not picky about the exact format as long as
29
+ # it contains exactly 12 hex characters with the optional delimiting characters
30
+ # '-', ':', or '.'.
31
+ def EUI48.parse(addr)
32
+ addr = addr.strip.gsub(/[\-\:\.]/,"")
33
+ if (addr.length != 12)
34
+ raise ValidationError, "Must contain exactly 12 hex characters with optional delimiters."
35
+ elsif (addr =~ /[^0-9a-fA-F\:]/)
36
+ raise ValidationError, "#{addr} contains invalid characters."
37
+ end
38
+ return EUI48.new(addr.to_i(16))
39
+ end
40
+
41
+ # bytes returns a list containing each byte of the EUI48 as String.
42
+ def bytes()
43
+ return [
44
+ (@addr >> 40 & 0xff).to_s(16).rjust(2, "0"),
45
+ (@addr >> 32 & 0xff).to_s(16).rjust(2, "0"),
46
+ (@addr >> 24 & 0xff).to_s(16).rjust(2, "0"),
47
+ (@addr >> 16 & 0xff).to_s(16).rjust(2, "0"),
48
+ (@addr >> 8 & 0xff).to_s(16).rjust(2, "0"),
49
+ (@addr & 0xff).to_s(16).rjust(2, "0"),
50
+ ]
51
+ end
52
+
53
+
54
+ # to_eui64 converts this EUI48 into an EUI64 by inserting 0xfffe between the first and last 24-bits of the address.
55
+ def to_eui64()
56
+ return EUI64.new((@addr & 0xffffff000000) << 16 | (@addr & 0x000000ffffff) | 0x000000fffe000000)
57
+ end
58
+
59
+ def to_s()
60
+ return self.bytes.join("-")
61
+ end
62
+
63
+ end # end class EUI48
64
+
65
+ end # end module
data/lib/eui64.rb ADDED
@@ -0,0 +1,77 @@
1
+ module NetAddr
2
+
3
+ # EUI64 (Extended Unique Identifier 64-bit, or EUI-64) represents a 64-bit hardware address.
4
+ class EUI64
5
+
6
+ # addr is the Integer representation of this EUI
7
+ attr_reader :addr
8
+
9
+ #Create an EUI64 from an Integer. Must be between 0 and 2**64-1.
10
+ #Throws ValidationError on error.
11
+ def initialize(i)
12
+ if (!i.kind_of?(Integer))
13
+ raise ValidationError, "Expected an Integer for 'i' but got a #{i.class}."
14
+ elsif ( (i < 0) || (i > 2**64-1) )
15
+ raise ValidationError, "#{i} is out of bounds for EUI64."
16
+ end
17
+ @addr = i
18
+ end
19
+
20
+ # Parse an EUI-64 string into an EUI64 type.
21
+ # This will successfully parse most of the typically used formats such as:
22
+ # * aa-bb-cc-dd-ee-ff-00-11
23
+ # * aa:bb:cc:dd:ee:ff:00:11
24
+ # * aabb.ccdd.eeff.0011
25
+ # * aabbccddeeff0011
26
+ #
27
+ # Although, in truth, its not picky about the exact format as long as
28
+ # it contains exactly 16 hex characters with the optional delimiting characters
29
+ # '-', ':', or '.'.
30
+ def EUI64.parse(addr)
31
+ addr = addr.strip.gsub(/[\-\:\.]/,"")
32
+ if (addr.length != 16)
33
+ raise ValidationError, "Must contain exactly 16 hex characters with optional delimiters."
34
+ elsif (addr =~ /[^0-9a-fA-F\:]/)
35
+ raise ValidationError, "#{addr} contains invalid characters."
36
+ end
37
+ return EUI64.new(addr.to_i(16))
38
+ end
39
+
40
+ # bytes returns a list containing each byte of the EUI64 as a String.
41
+ def bytes()
42
+ return [
43
+ (@addr >> 56 & 0xff).to_s(16).rjust(2, "0"),
44
+ (@addr >> 48 & 0xff).to_s(16).rjust(2, "0"),
45
+ (@addr >> 40 & 0xff).to_s(16).rjust(2, "0"),
46
+ (@addr >> 32 & 0xff).to_s(16).rjust(2, "0"),
47
+ (@addr >> 24 & 0xff).to_s(16).rjust(2, "0"),
48
+ (@addr >> 16 & 0xff).to_s(16).rjust(2, "0"),
49
+ (@addr >> 8 & 0xff).to_s(16).rjust(2, "0"),
50
+ (@addr & 0xff).to_s(16).rjust(2, "0"),
51
+ ]
52
+ end
53
+
54
+ # to_ipv6 generates an IPv6 address from this EUI64 address and the provided IPv6Net.
55
+ # Nil will be returned if net is not a /64.
56
+ def to_ipv6(net)
57
+ if (!net.kind_of?(IPv6Net))
58
+ raise ArgumentError, "Expected an IPv6Net object for 'net' but got a #{net.class}."
59
+ end
60
+
61
+ if (net.netmask.prefix_len != 64)
62
+ return nil
63
+ end
64
+
65
+ # set u/l bit to 0
66
+ hostId = @addr ^ 0x0200000000000000
67
+ ipAddr = net.network.addr | hostId
68
+ return IPv6.new(ipAddr)
69
+ end
70
+
71
+ def to_s()
72
+ return self.bytes.join("-")
73
+ end
74
+
75
+ end # end class EUI64
76
+
77
+ end # end module
data/lib/ipv4.rb ADDED
@@ -0,0 +1,77 @@
1
+ module NetAddr
2
+
3
+ #IPv4 represents a single IPv4 address.
4
+ class IPv4
5
+ # addr is the Integer representation of this IP address
6
+ attr_reader :addr
7
+
8
+ #Create an IPv4 from an Integer. Must be between 0 and 2**32-1.
9
+ #Throws ValidationError on error.
10
+ def initialize(i)
11
+ if (!i.kind_of?(Integer))
12
+ raise ValidationError, "Expected an Integer for 'i' but got a #{i.class}."
13
+ elsif ( (i < 0) || (i > 2**32-1) )
14
+ raise ValidationError, "#{i} is out of bounds for IPv4."
15
+ end
16
+ @addr = i
17
+ end
18
+
19
+ # parse will create an IPv4 from its string representation (ie. "192.168.1.1").
20
+ # Throws ValidationError on error.
21
+ def IPv4.parse(ip)
22
+ ip.strip!
23
+ i = Util.parse_IPv4(ip)
24
+ return IPv4.new(i)
25
+ end
26
+
27
+ #cmp compares equality with another IPv4. Return:
28
+ #* 1 if this IPv4 is numerically greater
29
+ #* 0 if the two are equal
30
+ #* -1 if this IPv4 is numerically less
31
+ def cmp(other)
32
+ if (!other.kind_of?(IPv4))
33
+ raise ArgumentError, "Expected an IPv4 object for 'other' but got a #{other.class}."
34
+ end
35
+ if (self.addr > other.addr)
36
+ return 1
37
+ elsif (self.addr < other.addr)
38
+ return -1
39
+ end
40
+ return 0
41
+ end
42
+
43
+ # multicast_mac returns the EUI48 multicast mac-address for this IP.
44
+ # It will return the zero address for IPs outside of the multicast range 224.0.0.0/4.
45
+ def multicast_mac
46
+ mac = 0
47
+ if (@addr&0xf0000000 == 0xe0000000) # within 224.0.0.0/4 ?
48
+ # map lower 23-bits of ip to 01:00:5e:00:00:00
49
+ mac = (@addr&0x007fffff) | 0x01005e000000
50
+ end
51
+ return EUI48.new(mac)
52
+ end
53
+
54
+ # next returns the next consecutive IPv4 or nil if the address space is exceeded
55
+ def next()
56
+ if (self.addr == NetAddr::F32)
57
+ return nil
58
+ end
59
+ return IPv4.new(self.addr + 1)
60
+ end
61
+
62
+ # prev returns the preceding IPv4 or nil if this is 0.0.0.0
63
+ def prev()
64
+ if (self.addr == 0)
65
+ return nil
66
+ end
67
+ return IPv4.new(self.addr - 1)
68
+ end
69
+
70
+ # to_s returns the IPv4 as a String
71
+ def to_s()
72
+ Util.int_to_IPv4(@addr)
73
+ end
74
+
75
+ end # end class IPv4
76
+
77
+ end # end module