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

Expose a property to set Dialer.Control #184

Merged
merged 1 commit into from
Apr 7, 2024

Conversation

danielealbano
Copy link
Contributor

The PR introduces a DialerControl property to allow a caller to execute syscalls on the socket before a connection is established.

Binding an IP via Source property is not always an option, in my case I am using a series of devices which get all the same IP and all have the same subnet: the only way to control which one has to be used is to bind the socket to a specific interface.

Here an example of the code

interfaceName := "XYZ"
speedTestClient = speedtest.New(speedtest.WithUserConfig(&speedtest.UserConfig{
	DialerControl: func(network, address string, conn syscall.RawConn) error {
		var operr error
		if err = conn.Control(func(fd uintptr) {
			operr = syscall.SetsockoptString(int(fd), syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, interfaceName)
		}); err != nil {
			return err
		}

		if operr != nil {
			return operr
		}

		return nil
	},
}))

This allows speedtest-go to work directly on the interface without having to bind on a specific IP.

The PR doesn't change the dialer used for the resolver as the library alters the DefaultResolver.

NOTE: SO_BINDTODEVICE is Linux specific, not sure what the other OS support, but the PR has no impact on the portability of the library itself as it only allows to define the Control property of the Dialer

@r3inbowari r3inbowari merged commit 156e462 into showwin:master Apr 7, 2024
@r3inbowari
Copy link
Collaborator

Thank you!

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

Successfully merging this pull request may close these issues.

2 participants