Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix, unix socket bind should be only used in passive mode #373

Merged
merged 3 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions internal/socket/tcp_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func tcpSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne
return
}
defer func() {
// ignore EINPROGRESS for non-blocking socket connect, should be processed by caller
if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS {
return
}
if err != nil {
_ = unix.Close(fd)
}
Expand All @@ -140,11 +144,10 @@ func tcpSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne
}
}

if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil {
return
}

if passive {
if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil {
return
}
// Set backlog size to the maximum.
err = os.NewSyscallError("listen", unix.Listen(fd, listenerBacklogMaxSize))
} else {
Expand Down
8 changes: 6 additions & 2 deletions internal/socket/udp_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func udpSocket(proto, addr string, connect bool, sockOpts ...Option) (fd int, ne
return
}
defer func() {
// ignore EINPROGRESS for non-blocking socket connect, should be processed by caller
if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS {
return
}
if err != nil {
_ = unix.Close(fd)
}
Expand All @@ -143,10 +147,10 @@ func udpSocket(proto, addr string, connect bool, sockOpts ...Option) (fd int, ne
}
}

err = os.NewSyscallError("bind", unix.Bind(fd, sa))

if connect {
err = os.NewSyscallError("connect", unix.Connect(fd, sa))
} else {
err = os.NewSyscallError("bind", unix.Bind(fd, sa))
}

return
Expand Down
13 changes: 9 additions & 4 deletions internal/socket/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func udsSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne
return
}
defer func() {
// ignore EINPROGRESS for non-blocking socket connect, should be processed by caller
// though there is less situation for EINPROGRESS when using unix socket
if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS {
return
}
if err != nil {
_ = unix.Close(fd)
}
Expand All @@ -71,11 +76,11 @@ func udsSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne
}
}

if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil {
return
}

if passive {
if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil {
return
}

// Set backlog size to the maximum.
err = os.NewSyscallError("listen", unix.Listen(fd, listenerBacklogMaxSize))
} else {
Expand Down