ip
import "github.com/TheManticoreProject/Manticore/network/ip"
Index
- type IPv4
- func NewIPv4(a, b, c, d, maskBits uint8) *IPv4
- func NewIPv4FromString(s string) *IPv4
- func (i *IPv4) CIDRAddress() string
- func (i *IPv4) CIDRMask() string
- func (i *IPv4) ComputeMask() *IPv4
- func (i *IPv4) IsInRange(start, end *IPv4) bool
- func (i *IPv4) IsInSubnet(subnet *IPv4) bool
- func (i *IPv4) String() string
- func (i *IPv4) ToUInt32() uint32
- type IPv4Range
- type IPv6
- type IPv6Range
- type TCPPortRange
type IPv4
type IPv4 struct {
A, B, C, D uint8
MaskBits uint8
}
func NewIPv4
func NewIPv4(a, b, c, d, maskBits uint8) *IPv4
NewIPv4 creates a new IPv4 instance.
Parameters:
a, b, c, d: The four octets of the IPv4 address.
maskBits: The number of bits in the subnet mask.
Returns:
*IPv4: A new IPv4 instance.
func NewIPv4FromString
func NewIPv4FromString(s string) *IPv4
NewIPv4FromString creates a new IPv4 instance from a string.
Parameters:
s: The string representation of the IPv4 address.
Returns:
*IPv4: A new IPv4 instance.
func (*IPv4) CIDRAddress
func (i *IPv4) CIDRAddress() string
CIDRAddress returns the CIDR address of the IPv4 address.
Returns:
string: The CIDR address of the IPv4 address.
func (*IPv4) CIDRMask
func (i *IPv4) CIDRMask() string
CIDRMask returns the CIDR mask of the IPv4 address.
Returns:
string: The CIDR mask of the IPv4 address.
func (*IPv4) ComputeMask
func (i *IPv4) ComputeMask() *IPv4
ComputeMask calculates the network address by applying the subnet mask to the IPv4 address. It returns a new IPv4 instance representing the network address.
The function first converts the IPv4 address to a 32-bit unsigned integer. Then, it creates a subnet mask by shifting the bits of 0xFFFFFFFF to the left by (32 - MaskBits) positions. The subnet mask is then applied to the 32-bit representation of the IPv4 address using a bitwise AND operation.
Finally, the resulting network address is split back into its four octets (A, B, C, D) and a new IPv4 instance is created and returned with the same MaskBits value.
Example:
ip := NewIPv4(192, 168, 1, 17, 24)
networkAddress := ip.ComputeMask()
fmt.Println(networkAddress.String()) // Output: 192.168.1.0/24
Returns:
*IPv4: A new IPv4 instance representing the network address.
func (*IPv4) IsInRange
func (i *IPv4) IsInRange(start, end *IPv4) bool
IsInRange checks if the IPv4 address is within the range of two other IPv4 addresses.
Parameters:
start: The start of the range.
end: The end of the range.
Returns:
bool: True if the IPv4 address is within the range, false otherwise.
func (*IPv4) IsInSubnet
func (i *IPv4) IsInSubnet(subnet *IPv4) bool
IsInSubnet checks if the IPv4 address is within the subnet of another IPv4 address.
Parameters:
subnet: The subnet to check against.
Returns:
bool: True if the IPv4 address is within the subnet, false otherwise.
func (*IPv4) String
func (i *IPv4) String() string
String returns the string representation of the IPv4 address.
Returns:
string: The string representation of the IPv4 address.
func (*IPv4) ToUInt32
func (i *IPv4) ToUInt32() uint32
ToUInt32 converts the IPv4 address to a 32-bit unsigned integer.
Returns:
uint32: The 32-bit unsigned integer representation of the IPv4 address.
type IPv4Range
type IPv4Range struct {
Start *IPv4
End *IPv4
}
func (*IPv4Range) Contains
func (r *IPv4Range) Contains(ip *IPv4) bool
Contains checks if the given IPv4 address is within the range.
Parameters:
ip: The IPv4 address to check.
Returns:
bool: True if the IPv4 address is within the range, false otherwise.
func (*IPv4Range) String
func (r *IPv4Range) String() string
String returns the string representation of the IPv4 range.
Returns:
string: The string representation of the IPv4 range.
type IPv6
type IPv6 struct {
A, B, C, D, E, F, G, H uint16
}
func NewIPv6
func NewIPv6(a, b, c, d, e, f, g, h uint16) *IPv6
NewIPv6 creates a new IPv6 instance.
Parameters:
a, b, c, d, e, f, g, h: The eight segments of the IPv6 address.
Returns:
*IPv6: A new IPv6 instance.
func NewIPv6FromString
func NewIPv6FromString(s string) *IPv6
NewIPv6FromString creates a new IPv6 instance from a string.
Parameters:
s: The string representation of the IPv6 address.
Returns:
*IPv6: A new IPv6 instance.
func (*IPv6) IsInRange
func (i *IPv6) IsInRange(start, end *IPv6) bool
IsInRange checks if the IPv6 address is within the range of two other IPv6 addresses.
Parameters:
start: The start of the range.
end: The end of the range.
Returns:
bool: True if the IPv6 address is within the range, false otherwise.
func (*IPv6) IsInSubnet
func (i *IPv6) IsInSubnet(subnet *IPv6) bool
IsInSubnet checks if the IPv6 address is within the subnet of another IPv6 address.
Parameters:
subnet: The subnet to check against.
Returns:
bool: True if the IPv6 address is within the subnet, false otherwise.
func (*IPv6) String
func (i *IPv6) String() string
String returns the string representation of the IPv6 address.
Returns:
string: The string representation of the IPv6 address.
func (*IPv6) ToUInt128
func (i *IPv6) ToUInt128() [2]uint64
ToUInt128 converts the IPv6 address to a 128-bit unsigned integer.
Returns:
[2]uint64: The 128-bit unsigned integer representation of the IPv6 address.
type IPv6Range
IPv6Range represents a range of IPv6 addresses.
type IPv6Range struct {
Start *IPv6
End *IPv6
}
func (*IPv6Range) Contains
func (r *IPv6Range) Contains(ip *IPv6) bool
Contains checks if the given IPv6 address is within the range.
Parameters:
ip: The IPv6 address to check.
Returns:
bool: True if the IPv6 address is within the range, false otherwise.
func (*IPv6Range) String
func (r *IPv6Range) String() string
String returns the string representation of the IPv6 range.
Returns:
string: The string representation of the IPv6 range.
type TCPPortRange
type TCPPortRange struct {
Start uint16
End uint16
}
func NewTCPPortRange
func NewTCPPortRange(start, end uint16) *TCPPortRange
NewTCPPortRange creates a new TCP port range instance.
Parameters:
start: The start port number.
end: The end port number.
Returns:
*TCPPortRange: A new TCP port range instance.
func NewTCPPortRangeFromString
func NewTCPPortRangeFromString(s string) (*TCPPortRange, error)
NewTCPPortRangeFromString creates a new TCP port range instance from a string.
Parameters:
s: The string representation of the TCP port range.
Returns:
*TCPPortRange: A new TCP port range instance.
func (*TCPPortRange) String
func (t *TCPPortRange) String() string
String returns the string representation of the TCP port range.
Returns:
string: The string representation of the TCP port range.