tcp-shaker icon indicating copy to clipboard operation
tcp-shaker copied to clipboard

Add Support to resolve Ipv6 Addresses

Open guepjo opened this issue 1 year ago • 0 comments

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
	}
        ...
        ...
}

guepjo avatar Apr 02 '24 00:04 guepjo