net: move bind back to sock.go
It was left in netFD.connect() by an oversight (as the name
implies, bind has no business being in connect). As a result
of this change and by only calling netFD.connect() when ra
isn't nil it becomes simpler with less code duplication.
Additionally, if netFD.connect() fails, set sysfd to -1 to
avoid finalizers (e.g. on windows) calling shutdown on a
closed and possibly reopened socket that just happened to
share the same descriptor.
R=golang-dev, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4328043
diff --git a/src/pkg/net/fd.go b/src/pkg/net/fd.go
index 3e87800..cd1a21d 100644
--- a/src/pkg/net/fd.go
+++ b/src/pkg/net/fd.go
@@ -303,26 +303,18 @@
fd.sysfile = os.NewFile(fd.sysfd, fd.net+":"+ls+"->"+rs)
}
-func (fd *netFD) connect(la, ra syscall.Sockaddr) (err os.Error) {
- if la != nil {
- e := syscall.Bind(fd.sysfd, la)
- if e != 0 {
- return os.Errno(e)
+func (fd *netFD) connect(ra syscall.Sockaddr) (err os.Error) {
+ e := syscall.Connect(fd.sysfd, ra)
+ if e == syscall.EINPROGRESS {
+ var errno int
+ pollserver.WaitWrite(fd)
+ e, errno = syscall.GetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_ERROR)
+ if errno != 0 {
+ return os.NewSyscallError("getsockopt", errno)
}
}
- if ra != nil {
- e := syscall.Connect(fd.sysfd, ra)
- if e == syscall.EINPROGRESS {
- var errno int
- pollserver.WaitWrite(fd)
- e, errno = syscall.GetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_ERROR)
- if errno != 0 {
- return os.NewSyscallError("getsockopt", errno)
- }
- }
- if e != 0 {
- return os.Errno(e)
- }
+ if e != 0 {
+ return os.Errno(e)
}
return nil
}
diff --git a/src/pkg/net/fd_windows.go b/src/pkg/net/fd_windows.go
index 0abf230..c2f736c 100644
--- a/src/pkg/net/fd_windows.go
+++ b/src/pkg/net/fd_windows.go
@@ -253,18 +253,10 @@
fd.raddr = raddr
}
-func (fd *netFD) connect(la, ra syscall.Sockaddr) (err os.Error) {
- if la != nil {
- e := syscall.Bind(fd.sysfd, la)
- if e != 0 {
- return os.Errno(e)
- }
- }
- if ra != nil {
- e := syscall.Connect(fd.sysfd, ra)
- if e != 0 {
- return os.Errno(e)
- }
+func (fd *netFD) connect(ra syscall.Sockaddr) (err os.Error) {
+ e := syscall.Connect(fd.sysfd, ra)
+ if e != 0 {
+ return os.Errno(e)
}
return nil
}
diff --git a/src/pkg/net/sock.go b/src/pkg/net/sock.go
index 9b99ad5..933700a 100644
--- a/src/pkg/net/sock.go
+++ b/src/pkg/net/sock.go
@@ -44,19 +44,30 @@
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
+ if la != nil {
+ e = syscall.Bind(s, la)
+ if e != 0 {
+ closesocket(s)
+ return nil, os.Errno(e)
+ }
+ }
+
if fd, err = newFD(s, f, p, net); err != nil {
closesocket(s)
return nil, err
}
- if err = fd.connect(la, ra); err != nil {
- closesocket(s)
- return nil, err
+ if ra != nil {
+ if err = fd.connect(ra); err != nil {
+ fd.sysfd = -1
+ closesocket(s)
+ return nil, err
+ }
}
- sa, _ := syscall.Getsockname(fd.sysfd)
+ sa, _ := syscall.Getsockname(s)
laddr := toAddr(sa)
- sa, _ = syscall.Getpeername(fd.sysfd)
+ sa, _ = syscall.Getpeername(s)
raddr := toAddr(sa)
fd.setAddr(laddr, raddr)