zrepl/util/tcpsock/tcpsock.go
Juergen Hoetzel d35e2400b2 transport/{TCP,TLS}: optional IP_FREEBIND / IP_BINDANY bind socketops
Allows to bind to an address even if it is not actually (yet or ever)
configured. Fixes #238

Rationale:
https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/#whatdoesthismeanformeadeveloper
2020-01-04 17:21:48 +01:00

28 lines
522 B
Go

package tcpsock
import (
"context"
"net"
"syscall"
)
func Listen(address string, tryFreeBind bool) (*net.TCPListener, error) {
control := func(network, address string, c syscall.RawConn) error {
if tryFreeBind {
if err := freeBind(network, address, c); err != nil {
return err
}
}
return nil
}
var listenConfig = net.ListenConfig{
Control: control,
}
l, err := listenConfig.Listen(context.Background(), "tcp", address)
if err != nil {
return nil, err
}
return l.(*net.TCPListener), nil
}