summaryrefslogtreecommitdiff
path: root/ipv6test.go
blob: c18faf9ba07db03a6b3da7bcb6cf6dd9a449291d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
	"fmt"
	"net"
	"time"
)

// The host we will try to connect to.
// ipv6.google.com is a good choice as it's highly available and resolves only to AAAA records (IPv6).
const ipv6TestHost = "ipv6.google.com"
const testPort = "443" // HTTPS port, almost always open.

func IPv6test() (string, error) {
	// We use net.DialTimeout to attempt a TCP connection.
	// By specifying "tcp6" as the network, we force the dialer to use IPv6 only.
	// If the system can't resolve the AAAA record or can't route to the IPv6 address,
	// this will fail.
	timeout := 2 * time.Second
	conn, err := net.DialTimeout("tcp6", net.JoinHostPort(ipv6TestHost, testPort), timeout)

	// Check the result.
	if err != nil {
		// The connection failed. This indicates that IPv6 is not working correctly.
		return fmt.Sprintf("IPv6 connectivity test failed: %v"), err
	}

	// If we get here, the connection was successful.
	// It's good practice to close the connection we opened.
	conn.Close()

	return fmt.Sprintf("IPv6 connectivity test successful."), nil
}