You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> From orchard to endpoint: _CIDRE_ delivers a smooth, dry balance of IP handling and subnet math, served consistently
19
+
> From orchard to endpoint: _CIDRE_ delivers a smooth, dry balance of IP handling and subnet math, served consistently
21
20
> sparkling across **all** KMP targets. Unchaptalized — zero added dependencies; just natural, refreshing clarity.
22
21
23
-
—_Sir Evander Marchbank_, self-proclaimed cider cartographer who insists that every orchard has its own “gravitational pull” affecting the bubbles.
22
+
—_Sir Evander Marchbank_, self-proclaimed cider cartographer, who insists that every orchard has its own “gravitational pull” affecting the bubbles.
24
23
25
24
---
26
25
27
-
CIDRE focuses on parsing and representing IP addresses, IP networks and providing CIDR math. On the JVM and Android it maps from/to `InetAddress`/`Inet4Address`/`Inet6Address`. On native targets, it maps from/to `in_addr`/`in6_addr`.
28
-
It is not a full IP networking implementation, but you can use it to implement IP routing.
29
-
It has a total of zero external dependencies.
26
+
CIDRE focuses on parsing and representing IP addresses, IP networks, and performing CIDR math. On the JVM and Android it maps from/to `InetAddress`/`Inet4Address`/`Inet6Address`. On native targets, it maps from/to `in_addr`/`in6_addr`.
27
+
It is not a full IP networking implementation, but you can use it to implement IP routing.
28
+
It has exactly zero external dependencies.
29
+
30
30
Currently, CIDRE provides the following functionality:
31
-
* parsing and encoding IPv4 and IPv6 addresses from/to String and ByteArray representations
31
+
* parsing and encoding IPv4 and IPv6 addresses from/to `String` and `ByteArray` representations
32
32
* converting CIDR prefixes from/to netmasks
33
-
* checking whether addresses or networks are are fully contained within a network
34
-
*comparability of networks and addresses inside a family (IPv4/IPv6)
33
+
* checking whether addresses or networks are fully contained within a network
34
+
*comparing networks and addresses within the same family (IPv4/IPv6)
35
35
36
-
Planned features are:
36
+
Planned features include:
37
37
* iterating over, slicing, splitting, and merging networks
38
38
* subnetting and supernetting
39
39
40
-
41
-
In general, CIDRE's data model has semantics influenced by [netaddr](https://github.com/netaddr/netaddr/?tab=readme-ov-file): An `IpNetwork` covers a range of `IpInterface`s, both of which consist of an `IpAddress` and a `prefix`.
40
+
In general, CIDRE's data model has semantics influenced by [netaddr](https://github.com/netaddr/netaddr/?tab=readme-ov-file): An `IpNetwork` covers a range of `IpInterface`s, both of which consist of an `address` and a `prefix`.
42
41
Semantically, an `IpInterface` has only a single `IpAddress` (although no validation is performed whether it is distinct from the associated network's address), while a network spans a range.
43
42
In more technical terms, CIDRE introduces three main classes:
44
-
-`IpAddress` - a sealed class, specialised as
45
-
-`IpAddress.V4` representing IPv4 addresses
46
-
-`IpAddress.V6` representing IPv6 addresses
47
-
-`IpNetwork` - a sealed class, following the same hierarchy:
48
-
-`IpNetwork.V4` representing an IPv4 network consisting of an `IpAddress.V4` network address and a prefix/netmask
49
-
-`IpNetwork.V6` representing an IPv4 network consisting of an `IpAddress.V6` network address and a prefix/netmask
50
-
-`IpInterface` a concrete IP address belonging to a network. Like a network, this is also a combination of IP address and prefix/netmask but with distinctly different semantics.
51
-
-`IpInterface.V4` consisting of an `IpAddress.V4` network address and a prefix/netmask
52
-
-`IpInterface.V6` consisting of an `IpAddress.V6` network address and a prefix/netmask
53
-
54
-
`IpNetwork`, `IpAddress` and their IPv4 and IPv6 specializations share the `IpAddressAndPrefix` interface hierarchy, which groups common semantics and functionality.
55
-
However, addresses and networks are not comparable, so this is mainly an application of DRY.
56
-
In addition, two typealiases are used:
57
-
*`typealias Prefix = UInt`
58
-
*`typealias Netmask = ByteArray`.
43
+
-`IpAddress` — a sealed class, specialized as:
44
+
-`IpAddress.V4` representing IPv4 addresses
45
+
-`IpAddress.V6` representing IPv6 addresses
46
+
-`IpNetwork` — a sealed class, following the same hierarchy:
47
+
-`IpNetwork.V4` representing an IPv4 network consisting of an `IpAddress.V4` and a prefix/netmask
48
+
-`IpNetwork.V6` representing an IPv6 network consisting of an `IpAddress.V6` and a prefix/netmask
49
+
-`IpInterface` — a concrete IP address belonging to a network. Like a network, this is a combination of IP address and prefix/netmask, but with distinctly different semantics:
50
+
-`IpInterface.V4` consisting of an `IpAddress.V4` and a prefix/netmask
51
+
-`IpInterface.V6` consisting of an `IpAddress.V6` and a prefix/netmask
52
+
53
+
`IpNetwork`, `IpAddress`, and their IPv4/IPv6 specializations share the `IpAddressAndPrefix` interface hierarchy, which groups common semantics and functionality.
54
+
Addresses and networks are not comparable, so this is mainly an application of DRY.
55
+
59
56
60
57
## Using in your Projects
61
58
62
-
This library is available at maven central.
59
+
This library is available at Maven Central.
63
60
64
61
### Gradle
65
62
@@ -78,7 +75,8 @@ val ip6 = IpAddress("2002:ac1d:2d64::1") //returns an IpAddress.V6
78
75
val ip4mappedIp6 =IpAddress("0000:0000:0000:0000:0000:FFFF:192.168.255.255") // returns an IPv4-mapped IpAddress.V6
79
76
```
80
77
81
-
Simply `toString()` any IP address to get its string representation, or access `octets` to get its network-oder byte representation.
78
+
79
+
Simply `toString()` any IP address to get its string representation, or access `octets` to get its network-order byte representation.
82
80
An `IpAddress`'s companion object also provides helpful properties such as segment separator, number of octets, and readily usable `Regex` instances to check whether a string is a valid representation of
83
81
an IP address or a single address segment.
84
82
@@ -102,12 +100,12 @@ Except for JavaScript and Wasm targets (which lack a native non-string IP addres
102
100
103
101
104
102
#### IPv4 Specifics
105
-
Though it has long since been superseded by CIDR, `IpAddress.V4` still features a `class` property (albeit marked as deprecated), that indicates its pre-CIDR
103
+
Though it has long since been superseded by CIDR, `IpAddress.V4` still features a `class` property (albeit marked as deprecated) that indicates its pre-CIDR
106
104
address class.
107
105
108
106
#### IPv6 Specifics
109
107
IPv6 addresses can embed IPv4 addresses in two ways:
110
-
* IPv4 _mapped_addresses: `0000:0000:0000:0000:0000:FFFF:<IPv4 Address in IPv4 Notation>`
108
+
* IPv4 _mapped_ addresses: `0000:0000:0000:0000:0000:FFFF:<IPv4 Address in IPv4 Notation>`
111
109
* IPv4 _compatible_ addresses: `0000:0000:0000:0000:0000:0000:<IPv4 Address in IPv4 Notation>`
112
110
113
111
While the former is still very much a thing (and exposed through the `isIpv4Mapped` flag), the latter has been deprecated.
@@ -120,31 +118,27 @@ It is possible to extract the contained IPv4 address from an IPv4-mapped or IPv4
120
118
121
119
CIDRE models two closely related concepts:
122
120
-`IpNetwork`: a contiguous address range, defined by a network address and prefix.
123
-
The network’s address itself is part of the network (and for IPv4, the broadcast address is also considered inside for membership checks).
124
-
-`IpInterface`: a single address bound to a prefix and associated with a network, and therefore carry a reference to their associated `IpNetwork`.
121
+
The network’s address itself is part of the network (and for IPv4, the broadcast address is also considered inside for membership checks).
122
+
-`IpInterface`: a single address bound to a prefix and associated with a network, and therefore carries a reference to the associated `IpNetwork`.
125
123
126
124
Both share the `IpAddressAndPrefix` interface and its respective IPv4 and IPv6 specializations and therefore expose:
127
125
-`address` and prefix (CIDR prefix length)
128
126
-`netmask` (network-order ByteArray)
129
127
- common flags (e.g., `isLinkLocal`, `isLoopback`, `isMulticast`). IPv4- and IPv6-specific flags are available on their
- consistent `toString()` behavior ("address/prefix"); IPv4 variants also support netmask printing helpers.
132
130
133
131
134
132
#### Creating IpInterfaces from Networks
135
133
136
134
Given an `IpAddress` and a prefix, it is possible to get the corresponding network in two ways:
137
-
-`IpNetwork(address strict = false)` to create a new `IpNetwork` and deep-copy the ip address into the network's `address` property.
138
-
- If `strict = true` the passed address must already be the network address (i.e., correctly masked), according to the specified prefix
139
-
- If `strict = false` the passed address will be masked to the network address, according to the specified prefix
140
-
-`IpNetwork.forAddress(address, prefix)` creates a new network, referencing and masking the passed `address`. This avoids copying, but modifies any not-correctly-masked address in-place, according to the given `prefix`.
135
+
-`IpNetwork(address, strict = false)` to create a new `IpNetwork` and deep-copy the IP address into the network's `address` property.
136
+
- If `strict = true`, the passed address must already be the network address (i.e., correctly masked), according to the specified prefix.
137
+
- If `strict = false`, the passed address will be masked to the network address according to the specified prefix.
138
+
-`IpNetwork.forAddress(address, prefix)` creates a new network, referencing and masking the passed `address`. This avoids copying but modifies any not-correctly-masked address in-place, according to the given `prefix`.
141
139
142
140
#### Netmasks and Prefixes
143
141
144
-
CIDRE uses type aliases
145
-
- Prefix is a UInt (`typealias Prefix = UInt`)
146
-
- Netmask is a network-order byte array (`typealias Netmask = ByteArray`)
147
-
148
142
Round-tripping between prefixes and netmasks is straightforward:
149
143
- Create a netmask from a prefix:
150
144
- For a specific IP family: `prefix.toNetmask(IpAddress.Family.V4)` or `prefix.toNetmask(IpAddress.Family.V6)`
@@ -155,9 +149,9 @@ IP addresses can be masked in-place by calling either `mask(prefix)` or `mask(ne
155
149
To create a deep-copied masked version of an address, manually `copy()` it before masking.
156
150
157
151
For IPv4, it is also possible to get a dotted-quad representation and choose a preferred textual form when working with `IpAddressAndPrefix`:
158
-
-`netmaskToString()` yields a `#.#.#.#` string
152
+
-`netmaskToString()` yields a `#.#.#.#` string.
159
153
-`toString(preferNetmaskOverPrefix = true)` prints `A.A.A.A N.N.N.N`, where `A` is an IP address quad and `N` is a netmask quad.
160
-
-`toString(preferNetmaskOverPrefix = false)` prints standard `#.#.#.#/prefix`
154
+
-`toString(preferNetmaskOverPrefix = false)` prints standard `#.#.#.#/prefix`.
161
155
162
156
#### Host Ranges and Address Spaces
163
157
@@ -166,12 +160,6 @@ Conceptually:
166
160
- An `IpInterface` is a single address bound to a prefix.
167
161
- The network address is part of the network; for IPv4, the broadcast address (when applicable) is also inside.
168
162
169
-
Coming soon:
170
-
- Iteration over the full address space of a network
171
-
- Convenience accessors for first/last interface and broadcast (where applicable)
*`infix fun ByteArray.and(other: ByteArray): ByteArray` performing a logical `AND` operation, returning a fresh ByteArray.
188
176
*`fun ByteArray.andInplace(other: ByteArray): Int` performing an in-place logical `AND` operation, modifying the receiver ByteArray. Returns the number of modified bits.
189
-
*`fun ByteArray.compareUnsignedBE(other: ByteArray): Int` comparing two same-sized byte arrays by interpreting their contents as unsigned BE integers
190
-
*`fun Prefix.toNetmask(family: IpAddress.Family): Netmask` converting an`UInt` CIDR prefix to its byte representation
191
-
*`fun Netmask.toPrefix(): Prefix` converting a netmask into its CIDR prefix length
177
+
*`fun ByteArray.compareUnsignedBE(other: ByteArray): Int` comparing two same-sized byte arrays by interpreting their contents as unsigned BE integers.
178
+
*`fun Prefix.toNetmask(family: IpAddress.Family): Netmask` converting a`UInt` CIDR prefix to its byte representation.
179
+
*`fun Netmask.toPrefix(): Prefix` converting a netmask into its CIDR prefix length.
192
180
*`ByteArray.toShortArray(bigEndian: Boolean = true): ShortArray` grouping pairs of bytes into a short. Useful to get IPv6 hextets from octets.
193
181
194
182
195
-
## Roadmap:
183
+
## Roadmap
196
184
- More comprehensive tests
197
185
- Address range enumeration
198
186
- Subnet enumeration (absolute and relative, e.g., `/24` or “+2 bits”)
@@ -209,6 +197,6 @@ Just be sure to observe the contribution guidelines (see [CONTRIBUTING.md](CONTR
209
197
---
210
198
211
199
<palign="center">
212
-
The Apache License does not apply to the logos, (including the A-SIT logo) and the project/module name(s), as these are the sole property of
200
+
The Apache License does not apply to the logos (including the A-SIT logo) and the project/module name(s), as these are the sole property of
213
201
A-SIT/A-SIT Plus GmbH and may not be used in derivative works without explicit permission!
0 commit comments