mirror of
https://github.com/zrepl/zrepl.git
synced 2024-12-23 15:38:49 +01:00
d35e2400b2
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
28 lines
522 B
Go
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
|
|
}
|