inet

package module
v0.0.0-...-7baac14 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: BSD-3-Clause Imports: 9 Imported by: 2

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.
	DefaultTargetIPv4 string = "https://v4.ip.ac-x.eu"
	DefaultTargetIPv6 string = "https://v6.ip.ac-x.eu"
	// UserAgent
	DefaultUserAgent = "catinello.eu/inet"
)

Functions

func Connectivity

func Connectivity() (bool, bool)

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

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.IP().IsGlobalUnicast() {
				ip4 = true
			}
		}

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

			if ip.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() (ip.Addr, 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.IP().IsGlobalUnicast())
}
Output:
true

func GetIPv6

func GetIPv6() (ip.Addr, 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.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() []ip.Addr

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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