ip

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Copyright © 2024 Mikael Schultz <mikael@conf-t.se>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Copyright © 2024 Mikael Schultz <mikael@conf-t.se>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHexAddress = errors.New("invalid hexadecimal IPv4 address")
View Source
var ErrInvalidNetmask = errors.New("invalid netmask")

Functions

func IPv4ToBinary added in v1.1.0

func IPv4ToBinary(ipStr string) string

IPv4ToBinary is a function that takes an IPv4 address in dotted-decimal notation as input and returns the IP address in binary notation.

func IPv4ToDecimal added in v1.1.0

func IPv4ToDecimal(ipStr string) string

IPv4ToDecimal is a function that takes an IPv4 address in dotted-decimal notation as input and returns the IP address in decimal notation (integer).

func IPv4ToHex added in v1.1.0

func IPv4ToHex(ipStr string) string

IPv4ToHex is a function that takes an IPv4 address in dotted-decimal notation as input and returns the IP address in hexadecimal notation.

func IPv4ToInt added in v1.2.0

func IPv4ToInt(ipStr string) uint32

IPv4ToInt is a function that takes an IPv4 address in dotted-decimal notation as input and returns the IP address in decimal notation (integer).

func IntToIPv4 added in v1.2.0

func IntToIPv4(ipInt uint32) string

IntToIPv4 is a function that takes an IP address in decimal notation (integer) as input and returns the IP address in dotted-decimal notation.

func IsIPv4

func IsIPv4(s string) bool

Function that checks if an IP address is an IPv4 address

func IsIPv4Hex added in v1.1.0

func IsIPv4Hex(hexIP string) bool

IsHexIPv4 is a function that takes a string as input and returns true if the string is a valid hexadecimal IPv4 address. Otherwise it returns false.

func IsIPv6

func IsIPv6(s string) bool

IsIPv6 is a function that checks if an IP address is an IPv6 address

func NetmaskPrefixLength

func NetmaskPrefixLength(mask string) (int, error)

NetmaskPrefixLength is a function that takes a netmask in dotted-decimal notation (e.g. 255.255.255.0) as input and returns the number of bits set in the netmask

func ParseIP

func ParseIP(s string) (net.IP, *net.IPNet, error)

Function that takes a string as input and returns an IP address and a subnet mask as output.

func ParseIPv4FromHex added in v1.1.0

func ParseIPv4FromHex(hexIP string) (string, error)

ParseIPv4FromHex is a function that takes a string as input and returns an IPv4 address in dotted-decimal notation. The input string must be a valid hexadecimal IPv4 address.

func ResolveIP added in v1.1.0

func ResolveIP(hostname string) (string, error)

ResolveIP is a function that resolves a hostname to an IP address and returns the first IPv4 address found.

Types

type IPv4

type IPv4 struct {
	IP   net.IP
	Mask net.IPMask
	Net  *net.IPNet
}

The IPv4 struct represents an IPv4 address as an IP address, a subnet mask and a network address. It also contains functions for calculating the broadcast address, the first and last usable host addresses, the number of usable hosts and the size of the network in number of IP addresses.

func ParseIPv4

func ParseIPv4(s string) (*IPv4, error)

ParseIPv4 is a function that takes a string as input and returns an IPv4 address and a subnet mask as output. The input string can be in the following formats: - "X.X.X.X/Y" - "X.X.X.X Y" - "X.X.X.X" - "0xXXXXXXXX/Y" - "0xXXXXXXXX Y" - "0xXXXXXXXX" - "XXXXXXXX/Y" - "XXXXXXXX Y" - "XXXXXXXX" - "XXXXXXXX XXXXXXXX/

func (*IPv4) Address

func (ip *IPv4) Address() string

Address is a function that returns the IP address in dotted-decimal notation

func (*IPv4) Broadcast

func (ip *IPv4) Broadcast() string

Broadcast is a function that returns the broadcast address in the network

func (*IPv4) FirstHost

func (ip *IPv4) FirstHost() string

FirstHost is a function that returns the first usable host address in the network

func (*IPv4) LastHost

func (ip *IPv4) LastHost() string

LastHost is a function that returns the last usable host address in the network

func (*IPv4) Netmask

func (ip *IPv4) Netmask() string

Netmask is a function that returns the netmask in dotted-decimal notation

func (*IPv4) Network

func (ip *IPv4) Network() string

Network is a function that returns the network address of the network

func (*IPv4) NetworkSize

func (ip *IPv4) NetworkSize() uint32

NetworkSize is a function that returns the size of the network in number of IP addresses

func (*IPv4) PrefixLength

func (ip *IPv4) PrefixLength() int

PrefixLength is a function that returns the number of bits set in the netmask

func (*IPv4) Split added in v1.2.0

func (ip *IPv4) Split(bits int) ([]*IPv4, error)

Split is a function that takes an IPv4 address and a number of bits as input and returns a list of subnets as output.

func (*IPv4) String

func (ip *IPv4) String() string

String is a function that returns the IP address and the prefix length in CIDR notation

func (*IPv4) UsableHosts

func (ip *IPv4) UsableHosts() uint32

UsableHosts is a function that returns the number of usable hosts in the network

func (*IPv4) Wildcard

func (ip *IPv4) Wildcard() string

Wildcard is a function that returns the wildcard mask in dotted-decimal notation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL