Documentation
¶
Overview ¶
Package ipaddr is a pure-Go (no cgo) reimplementation of Ruby's `ipaddr` standard library — MRI 4.0.5's IPAddr class. It models an IP address together with a netmask (IPv4 or IPv6) and reproduces MRI's parsing, masking, string formatting (to_s / to_string / cidr / inspect), set predicates, bitwise operators and comparison semantics byte-for-byte.
Addresses and netmasks are carried in a fixed-width native-integer representation (see [u128]): IPv4 in 32 bits, IPv6 in 128, with masks, arithmetic and comparisons done in machine words so the common paths never allocate. MRI's unbounded-integer edges — the succ/`+` overflow that raises "invalid address: 4294967296", the packed-integer constructors and #to_i — are handled through a math/big.Int bridge used only where arbitrary precision is genuinely required. The representation is endianness-independent.
Index ¶
- func Ntop(addr []byte) (string, error)
- func NtopString(s, encoding string) (string, error)
- type AddressFamilyError
- type Error
- type Family
- type IPAddr
- func (ip *IPAddr) Add(offset int64) (*IPAddr, error)
- func (ip *IPAddr) And(other any) (*IPAddr, error)
- func (ip *IPAddr) Cidr() string
- func (ip *IPAddr) Cmp(other any) (int, bool)
- func (ip *IPAddr) Each(fn func(*IPAddr) error) error
- func (ip *IPAddr) Eql(other any) bool
- func (ip *IPAddr) Family() Family
- func (ip *IPAddr) Hash() uint64
- func (ip *IPAddr) HtonString() ([]byte, error)
- func (ip *IPAddr) Include(other any) (bool, error)
- func (ip *IPAddr) Inspect() string
- func (ip *IPAddr) Ipv4() bool
- func (ip *IPAddr) Ipv4Compat() (*IPAddr, error)
- func (ip *IPAddr) Ipv4Mapped() (*IPAddr, error)
- func (ip *IPAddr) Ipv6() bool
- func (ip *IPAddr) IsIpv4Compat() bool
- func (ip *IPAddr) IsIpv4Mapped() bool
- func (ip *IPAddr) LinkLocal() bool
- func (ip *IPAddr) Loopback() bool
- func (ip *IPAddr) Mask(prefixlen string) (*IPAddr, error)
- func (ip *IPAddr) MaskLen(prefixlen int) (*IPAddr, error)
- func (ip *IPAddr) Multicast() bool
- func (ip *IPAddr) Native() (*IPAddr, error)
- func (ip *IPAddr) Netmask() string
- func (ip *IPAddr) Not() (*IPAddr, error)
- func (ip *IPAddr) Or(other any) (*IPAddr, error)
- func (ip *IPAddr) Prefix() int
- func (ip *IPAddr) Private() bool
- func (ip *IPAddr) SetPrefix(prefix int) (*IPAddr, error)
- func (ip *IPAddr) String() string
- func (ip *IPAddr) Sub(offset int64) (*IPAddr, error)
- func (ip *IPAddr) Succ() (*IPAddr, error)
- func (ip *IPAddr) ToI() *big.Int
- func (ip *IPAddr) ToRange() (*IPAddr, *IPAddr, error)
- func (ip *IPAddr) ToS() string
- func (ip *IPAddr) ToString() string
- func (ip *IPAddr) Xor(other any) (*IPAddr, error)
- type InvalidAddressError
- type InvalidPrefixError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ntop ¶
Ntop converts a packed network-byte-ordered address (4 or 16 bytes) to its readable form, mirroring IPAddr.ntop. A []byte carries no Ruby encoding, so it is treated as BINARY (Encoding::ASCII_8BIT): a length other than 4 or 16 raises AddressFamilyError, exactly as MRI does for a BINARY-encoded String.
func NtopString ¶
NtopString mirrors IPAddr.ntop for a Ruby String argument, honouring MRI's encoding precedence: the encoding is checked *before* the byte length.
MRI raises InvalidAddressError "invalid encoding (given <enc>, expected BINARY)" for any String whose encoding is not Encoding::ASCII_8BIT/BINARY — and it does so even when the length would otherwise be valid (e.g. a 4-byte US-ASCII string). Only once the encoding is BINARY does it dispatch on length, raising AddressFamilyError for a length other than 4 or 16. encoding is the Ruby encoding name of s (e.g. "UTF-8", "US-ASCII", "ASCII-8BIT", "BINARY"); the canonical BINARY aliases are "ASCII-8BIT" and "BINARY".
Types ¶
type AddressFamilyError ¶
type AddressFamilyError struct{ Msg string }
AddressFamilyError mirrors IPAddr::AddressFamilyError.
func (*AddressFamilyError) Error ¶
func (e *AddressFamilyError) Error() string
type Error ¶
type Error struct{ Msg string }
Error is the base class of every error this package raises, mirroring IPAddr::Error < ArgumentError.
type Family ¶
type Family int
Family is an address family, mirroring the value IPAddr#family returns. MRI stores Socket::AF_INET / Socket::AF_INET6 there; this port uses the canonical constants below. Compare with IPAddr.Ipv4/IPAddr.Ipv6 for family-independent checks.
const ( // AFInet is the IPv4 address family (Socket::AF_INET == 2 on every platform). AFInet Family = 2 // AFInet6 is the IPv6 address family. MRI uses the host's Socket::AF_INET6 // (which varies: 10 on Linux, 30 on the BSDs/macOS). The exact integer is an // implementation detail; use [IPAddr.Ipv6]. We expose Linux's value as the // canonical one. AFInet6 Family = 10 )
type IPAddr ¶
type IPAddr struct {
// contains filtered or unexported fields
}
IPAddr is a Ruby IPAddr: an address family plus the address and netmask, all carried in the fixed-width native-integer representation [u128].
func New ¶
New parses a human-readable IP address, mirroring IPAddr.new(addr). It accepts "address", "address/prefixlen" and "address/netmask"; an IPv6 address may be wrapped in square brackets and may carry a %zone suffix. When a prefix or mask is given the address is masked. It is the string-argument form of MRI's initialize; for the packed-integer form use NewFromInt.
func NewFamily ¶
NewFamily parses like New but forces the address family (Socket::AF_INET / AF_INET6), raising AddressFamilyError on a mismatch — the two-argument IPAddr.new(addr, family) form for string addresses.
func NewFromInt ¶
NewFromInt builds an IPAddr from a packed integer address and an explicit family, mirroring IPAddr.new(integer, family). family must be AFInet or AFInet6.
func NewNtoh ¶
NewNtoh builds an IPAddr from a packed network-byte-ordered address, mirroring IPAddr.new_ntoh.
func (*IPAddr) And ¶
And returns a new IPAddr built by bitwise AND, mirroring IPAddr#&. other may be an *IPAddr, a string, an int/int64/uint64 or a *big.Int.
func (*IPAddr) Cmp ¶
Cmp compares two addresses, mirroring IPAddr#<=> (Comparable). It returns (result, true) where result is -1, 0 or 1; ok is false when the operands are incomparable (different family or an uncoercible operand), matching MRI's nil.
func (*IPAddr) Each ¶
Each iterates every address in the network range, lowest first, invoking fn with a host-masked IPAddr for each. MRI's IPAddr has no #each; this is the idiomatic Go iteration over to_range that rbgo binds to an each block. fn may return an error to stop iteration early.
func (*IPAddr) Eql ¶
Eql reports value equality, mirroring IPAddr#==: same family and same integer address. A coercion failure yields false (not an error), as MRI's rescue does.
func (*IPAddr) Hash ¶
Hash returns a hash value used for Hash/Set membership, mirroring IPAddr#hash: ([@addr, @mask_addr, @zone_id].hash << 1) | (ipv4? ? 0 : 1). The high bits derive from a stable FNV-style mix of the operands; only the parity bit is guaranteed to match MRI (the array hash itself is interpreter-specific), so Hash is for in-process Set/Hash keying, not cross-runtime equality.
func (*IPAddr) HtonString ¶
HtonString returns the network-byte-ordered packed form, mirroring IPAddr#hton (4 bytes for IPv4, 16 for IPv6). The byte order is written with explicit shifts, so it is identical on little- and big-endian hosts.
func (*IPAddr) Include ¶
Include reports whether other is contained in this address's range, mirroring IPAddr#include? (aliased as ===). A non-IPAddr operand is coerced; a different family yields false rather than an error (a bad coercion is reported as err).
func (*IPAddr) Ipv4Compat ¶
Ipv4Compat converts a native IPv4 address into an IPv4-compatible IPv6 address, mirroring IPAddr#ipv4_compat (obsolete in MRI but reproduced).
func (*IPAddr) Ipv4Mapped ¶
Ipv4Mapped converts a native IPv4 address into an IPv4-mapped IPv6 address, mirroring IPAddr#ipv4_mapped.
func (*IPAddr) IsIpv4Compat ¶
IsIpv4Compat is the exported predicate for ipv4_compat?.
func (*IPAddr) IsIpv4Mapped ¶
IsIpv4Mapped is the exported predicate for ipv4_mapped?.
func (*IPAddr) LinkLocal ¶
LinkLocal mirrors IPAddr#link_local?. IPv4 169.254.0.0/16, IPv6 fe80::/10, plus the IPv4-mapped form.
func (*IPAddr) Loopback ¶
Loopback mirrors IPAddr#loopback?. IPv4 127.0.0.0/8, IPv6 ::1, and the IPv4-mapped 127.0.0.0/8 are loopback.
func (*IPAddr) Mask ¶
Mask returns a new IPAddr built by masking with the given prefix length or netmask string, mirroring IPAddr#mask. Accepts "8", "64", "255.255.255.0", etc.
func (*IPAddr) Multicast ¶
Multicast reports whether the address is multicast (IPv4 224.0.0.0/4, IPv6 ff00::/8). MRI 4.0.5's IPAddr has no #multicast?; this is provided for completeness and follows the conventional definitions.
func (*IPAddr) Native ¶
Native converts an IPv4-mapped or IPv4-compatible IPv6 address back to native IPv4, mirroring IPAddr#native. Any other address is returned unchanged.
func (*IPAddr) Not ¶
Not returns a new IPAddr built by bitwise negation (width-masked), mirroring IPAddr#~.
func (*IPAddr) Private ¶
Private mirrors IPAddr#private?. IPv4 RFC1918 ranges and IPv6 fc00::/7, plus their IPv4-mapped forms.
func (*IPAddr) SetPrefix ¶
SetPrefix sets the prefix length in bits, mirroring IPAddr#prefix=. It mutates the receiver (masking the address) and returns it for convenience.
func (*IPAddr) Succ ¶
Succ returns the successor address, mirroring IPAddr#succ. As in MRI, the successor of the broadcast address raises InvalidAddressError because the incremented value overflows the family width.
func (*IPAddr) ToRange ¶
ToRange returns the [begin, end] IPAddr pair spanning the network, mirroring IPAddr#to_range (each endpoint carries a host mask). Use IPAddr.Each to iterate the addresses.
func (*IPAddr) ToS ¶
ToS returns the compact, human-readable string, mirroring IPAddr#to_s — IPv4 dotted-quad, IPv6 with leading zeros stripped and the longest run of zero groups collapsed to "::" (RFC 5952, byte-identical to MRI), including the ::a.b.c.d / ::ffff:a.b.c.d embedded-IPv4 forms. The zero-collapse is a direct scan over the eight hextets — no regexp.
type InvalidAddressError ¶
type InvalidAddressError struct{ Msg string }
InvalidAddressError mirrors IPAddr::InvalidAddressError.
func (*InvalidAddressError) Error ¶
func (e *InvalidAddressError) Error() string
type InvalidPrefixError ¶
type InvalidPrefixError struct{ Msg string }
InvalidPrefixError mirrors IPAddr::InvalidPrefixError, which in MRI is a subclass of InvalidAddressError.
func (*InvalidPrefixError) Error ¶
func (e *InvalidPrefixError) Error() string
