Documentation ¶
Overview ¶
IP addresses as numeric types
Index ¶
- func FromBigInt[A Address[A]](family Family[A], i *big.Int) (A, error)
- func FromBytes(address ...byte) (any, error)
- func MaxAddress[A Address[A]](fam Family[A]) A
- func MinAddress[A Address[A]](fam Family[A]) A
- func Next[A Address[A]](address A) A
- func Parse[A Address[A]](family Family[A], candidate string) (A, error)
- func ParseUnknown(candidate string) (any, error)
- func Prev[A Address[A]](address A) A
- func ToBigInt[A Address[A]](address A) *big.Int
- type Address
- type Address4
- func (a Address4) Add(addend Address4) Address4
- func (a Address4) And(operand Address4) Address4
- func (a Address4) Bytes() []byte
- func (a Address4) Compare(other Address4) int
- func (a Address4) Divide(denominator Address4) Address4
- func (a Address4) Family() Family[Address4]
- func (a Address4) LeadingZeros() int
- func (a Address4) Mod(denominator Address4) Address4
- func (a Address4) Multiply(multiplicand Address4) Address4
- func (a Address4) Not() Address4
- func (a Address4) Or(operand Address4) Address4
- func (a Address4) Shift(bits int) Address4
- func (a Address4) String() string
- func (a Address4) Subtract(addend Address4) Address4
- func (a Address4) TrailingZeros() int
- func (a Address4) Xor(operand Address4) Address4
- type Address6
- func (a Address6) Add(addend Address6) Address6
- func (a Address6) And(operand Address6) Address6
- func (a Address6) Bytes() []byte
- func (a Address6) Compare(other Address6) int
- func (a Address6) Divide(denominator Address6) Address6
- func (a Address6) Family() Family[Address6]
- func (a Address6) LeadingZeros() int
- func (a Address6) Mod(denominator Address6) Address6
- func (a Address6) Multiply(multiplicand Address6) Address6
- func (a Address6) Not() Address6
- func (a Address6) Or(operand Address6) Address6
- func (a Address6) Shift(bits int) Address6
- func (a Address6) String() string
- func (a Address6) Subtract(subtrahend Address6) Address6
- func (a Address6) TrailingZeros() int
- func (a Address6) Xor(operand Address6) Address6
- type Family
- type Version
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromBigInt ¶
Converts big integer to address
func FromBytes ¶
Parse IP address bytes from unknown family
Example ¶
package main import ( "github.com/ipfreely-uk/go/ip" ) func main() { address, err := ip.FromBytes(127, 0, 0, 1) if err != nil { println("Not address: %v", err) } switch a := address.(type) { case ip.Address4: println("Not address: %v", a.String()) case ip.Address6: println("Not address: %v", a.String()) } }
Output:
func ParseUnknown ¶
Parse IP address string from unknown family
Example ¶
package main import ( "github.com/ipfreely-uk/go/ip" ) func main() { examples := []string{"::1", "127.0.0.1", "foobar"} for _, s := range examples { address, err := ip.ParseUnknown(s) if err != nil { println("Not address: %s", err) } switch a := address.(type) { case ip.Address4: println("IPv4 address: %s", a.String()) case ip.Address6: println("IPv6 address: %s", a.String()) } } }
Output:
Types ¶
type Address ¶
type Address[A any] interface { // IP address family Family() Family[A] // Address as bytes Bytes() []byte // Addition with overflow Add(A) A // Subtraction with overflow Subtract(A) A // Multiplication with overflow Multiply(A) A // Division Divide(A) A // Modulus Mod(A) A // Bitwise NOT Not() A // Bitwise AND And(A) A // Bitwise OR Or(A) A // Bitwise XOR Xor(A) A // Bit shift. Use negative int for left shift; use positive in for right shift. Shift(int) A // Returns 1 if operand is less than this. // Returns -1 if operand is more than this. // Returns 0 if operand is equal. Compare(A) int // Similar to math/bits.LeadingZeros* LeadingZeros() int // Similar to math/bits.TrailingZeros* TrailingZeros() int // Canonical string form String() string // contains filtered or unexported methods }
Generic IP address type.
type Address4 ¶
type Address4 struct {
// contains filtered or unexported fields
}
Immutable 32bit unsigned integer IP Address representation
func (Address4) LeadingZeros ¶
func (Address4) TrailingZeros ¶
type Address6 ¶
type Address6 struct {
// contains filtered or unexported fields
}
Immutable 128bit unsigned integer IP Address representation
func (Address6) LeadingZeros ¶
func (Address6) TrailingZeros ¶
type Family ¶
type Family[A any] interface { // IP address version Version() Version // Address width in bits - 32 or 128 Width() int // Create address from bytes. // Returns error if slice is not [Width]/8 bytes. FromBytes(...byte) (A, error) // Create address from unsigned integer. // All values are valid. FromInt(i uint32) A // contains filtered or unexported methods }
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Compare ordered values
|
Compare ordered values |
Network ranges and sets.
|
Network ranges and sets. |
cidr
RFC-4632 Classless Inter-domain Routing notation handling https://www.rfc-editor.org/rfc/rfc4632
|
RFC-4632 Classless Inter-domain Routing notation handling https://www.rfc-editor.org/rfc/rfc4632 |
Functions for working with subnets
|
Functions for working with subnets |
Click to show internal directories.
Click to hide internal directories.