diff options
Diffstat (limited to 'server/util.go')
-rw-r--r-- | server/util.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/server/util.go b/server/util.go index 92f73504..b122fbdb 100644 --- a/server/util.go +++ b/server/util.go @@ -66,6 +66,31 @@ func decodeAdministrativeCommunication(data []byte) (string, []byte) { return string(data[1 : communicationLen+1]), data[communicationLen+1:] } +func extractFileAndFamilyFromTCPListener(l *net.TCPListener) (*os.File, int, error) { + // Note #1: TCPListener.File() has the unexpected side-effect of putting + // the original socket into blocking mode. See Note #2. + fi, err := l.File() + if err != nil { + return nil, 0, err + } + + // Note #2: Call net.FileListener() to put the original socket back into + // non-blocking mode. + fl, err := net.FileListener(fi) + if err != nil { + fi.Close() + return nil, 0, err + } + fl.Close() + + family := syscall.AF_INET + if strings.Contains(l.Addr().String(), "[") { + family = syscall.AF_INET6 + } + + return fi, family, nil +} + func extractFileAndFamilyFromTCPConn(conn *net.TCPConn) (*os.File, int, error) { // Note #1: TCPConn.File() has the unexpected side-effect of putting // the original socket into blocking mode. See Note #2. |