inet

package module
v0.0.0-...-0e99a8a Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Overview

Package inet provides an overview of the available global unicast addresses and IP versions (IPv4/IPv6/DualStack) and the ability to check connectivity to rely decisions upon for your applications internet connections.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Service to resolve IP address against. I have no affiliation with ip.me.
	DefaultTarget string = "https://ip.me"
	// UserAgent fake string to use.
	DefaultUserAgent = "curl/8.11.0-inet"
)

Functions

func Connectivity

func Connectivity() (bool, bool)

Connectivity returns boolean values for IPv4 and IPv6 checked state respectively.

true = success
false = failure
Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/inet"
)

func main() {
	var ip4, ip6 bool

	if inet.DualStack() {
		ip4, ip6 = inet.Connectivity()
	} else {
		if inet.HasIPv4() {
			ip, err := inet.GetIPv4()
			if err != nil {
				log.Fatal(err)
			}

			if ip.IsGlobalUnicast() {
				ip4 = true
			}
		}

		if inet.HasIPv6() {
			ip, err := inet.GetIPv6()
			if err != nil {
				log.Fatal(err)
			}

			if ip.IsGlobalUnicast() {
				ip6 = true
			}
		}
	}

	fmt.Println(ip4, ip6)
}
Output:

true true

func DualStack

func DualStack() bool

DualStack returns a boolean value. It retursn true, if the system has a global unicast addresses for IPv4 and IPv6.

func GetIPv4

func GetIPv4() (net.IP, error)

GetIPv4 returns the retrieved pulbic IP and nil or an occured error.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/inet"
)

func main() {
	ip, err := inet.GetIPv4()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(ip.IsGlobalUnicast())
}
Output:

true

func GetIPv6

func GetIPv6() (net.IP, error)

GetIPv6 returns the retrieved pulbic IP and nil or an occured error.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/inet"
)

func main() {
	ip, err := inet.GetIPv6()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(ip.IsGlobalUnicast())
}
Output:

true

func HasIP

func HasIP() (bool, bool)

HasIP returns boolean values for IPv4 and IPv6 respectively.

func HasIPv4

func HasIPv4() bool

HasIPv4 returns a boolean value as true, if the system has a global unicast IPv4.

func HasIPv6

func HasIPv6() bool

IPv6 returns a boolean value as true, if the system has a global unicast IPv6.

func List

func List() []net.IP

List returns a list of all available global unicast addresses for that application.

Types

type Version

type Version uint8

Internet Protocol

const (
	None Version = 0
	IPv4 Version = 4
	IPv6 Version = 6
)

func Is

func Is(ip net.IP) Version

Is returns the IP version of the given IP.

func (Version) String

func (v Version) String() string

String retuns the Version as string.

Jump to

Keyboard shortcuts

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