libp2p / go-reuseport

reuse tcp/udp ports in golang
ISC License
763 stars 107 forks source link

[Question] Not clear implementation of Available() #114

Closed g41797 closed 1 year ago

g41797 commented 1 year ago
// Available returns whether or not SO_REUSEPORT or equivalent behaviour is
// available in the OS.
func Available() bool {
    return true
}

So for every os _SO_REUSEPORT_ is available

But for windows (see below) it does not exist

func Control(network, address string, c syscall.RawConn) (err error) {
    controlErr := c.Control(func(fd uintptr) {
        err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
    })
    if controlErr != nil {
        err = controlErr
    }
    return
}

do you accept PRs?

willscott commented 1 year ago

Yes - PRs welcome!

I wonder if Available is used anywhere? code search doesn't surface any obvious uses of it, so it may just be a left over remnant.

g41797 commented 1 year ago

I'd like to create N udp servers for the same port Because on windows this functionality does not exist I should to call Available in order to write portable code
Looks like Available will call control.available() Also looks strange that for reuseaddr and reuseport returns 1 bool your turn

marten-seemann commented 1 year ago

I wonder if Available is used anywhere? code search doesn't surface any obvious uses of it, so it may just be a left over remnant.

It is here: https://github.com/libp2p/go-libp2p/blob/4e2a16dd3f4f980bf9429572b3d2aed885594ec4/p2p/net/reuseport/listen.go#L46-L48

g41797 commented 1 year ago

btw what is the reason to require go 1.20?

marten-seemann commented 1 year ago

btw what is the reason to require go 1.20?

It's our release policy to support the two most recent Go releases.

  • leave Available as-is and add AvailReuseAddr & AvailReusePort or
  • func Available() (reuseaddr, reuseport bool)

What's the use case for splitting it into two?

g41797 commented 1 year ago

so something like:

// Available returns whether or not SO_REUSEADDR and SO_REUSEPORT are available in the OS.
func Available() (reuseaddr, reuseport bool) {
    return available()
}
marten-seemann commented 1 year ago

Again, what's the use case? What problem are you trying to solve?

g41797 commented 1 year ago

because SO_REUSEADDR is not enough: "..traditional SO_REUSEADDR socket option already allows multiple UDP sockets to be bound to, and accept datagrams on, the same UDP port. However, by contrast with SO_REUSEPORT, SO_REUSEADDR does not prevent port hijacking and does not distribute datagrams evenly across the receiving threads" The SO_REUSEPORT socket option

marten-seemann commented 1 year ago

That's not what we're using reuseport though. We only use it for TCP, to be able to run a listener and outgoing connections on the same port. I don't think we should get into the specifics of SO_REUSEPORT and SO_REUSEADDR, as they're highly platform specific.

g41797 commented 1 year ago

no problem you got star and new fork