Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support to resolve Ipv6 Addresses #30

Open
guepjo opened this issue Apr 2, 2024 · 0 comments
Open

Add Support to resolve Ipv6 Addresses #30

guepjo opened this issue Apr 2, 2024 · 0 comments

Comments

@guepjo
Copy link

guepjo commented Apr 2, 2024

Issue

Right now, when users use CheckAddr by passing in a hostname, the address is resolved using net.ResolveTCPAddr("tcp", addr). Unfortunately, this returns an Ipv4 address even in cases when users would rather have preferred to use the Ipv6 address for this host.

Proposed Fix

Option 1: get desired ip address to resolve from users

This option suggest to modify the current CheckAddr to have a parameter for whether users want to check the ipv4 or ipv6 address for this host. Naturally, this means CheckAddrZeroLinger and parseSockAddr need to be changes accordingly. Ex:

func (c *Checker) CheckAddr(addr string, addrType string, timeout time.Duration) (err error) {
	return c.CheckAddrZeroLinger(addr, addrType, timeout, c.zeroLinger)
}

func (c *Checker) CheckAddrZeroLinger(addr string, addrType string, timeout time.Duration, zeroLinger bool) error {
	// fmt.Println("check address addr: ", addr)
	// Set deadline
	deadline := time.Now().Add(timeout)

	// Parse address
	rAddr, family, err := parseSockAddr(addr, addrType)
	if err != nil {
		return err
	}

        ....
}

In parseSockAddr some logic can be added to parse for ipv6 or ipv4 according to whatever the user provides. (More details below)

Option 2: Add a separate function for checking ipv6 addresses

Option 1 will require users to update their current usage of CheckAddr even if it works just fine for them now. Adding a new function like CheckIpv6Addr would make it easier for users to move to this new function if needed without disrupting current users. Ex:

func (c *Checker) CheckIpv6Addr(addr string, addrType string, timeout time.Duration) (err error) {
	return c.CheckAddrZeroLinger(addr, addrType, timeout, c.zeroLinger)
}

func (c *Checker) CheckAddrZeroLinger(addr string, addrType string, timeout time.Duration, zeroLinger bool) error {
	// fmt.Println("check address addr: ", addr)
	// Set deadline
	deadline := time.Now().Add(timeout)

	// Parse address
	rAddr, family, err := parseSockAddr(addr, addrType)
	if err != nil {
		return err
	}

        ....
}

Changing parseSockAddr

Regardless of the options above, the way the hostname is resolved would need to be updated to return an ipv4 or v6 address according to user input. Ex:

// parseSockAddr resolves given addr to unix.Sockaddr
func parseSockAddr(addr string) (sAddr unix.Sockaddr, addrType string,  family int, err error) {
	addrOption := "ip"
	if addrType == "ip6" {
		addrOption = "ip6"
	}
	tAddr, err := net.ResolveIPAddr(addrOption, addr)
	if err != nil {
		return
	}
        ...
        ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant