Commit 07931764 authored by Alexey Borzenkov's avatar Alexey Borzenkov Committed by Russ Cox

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
parent 85c79ef7
......@@ -303,26 +303,18 @@ func (fd *netFD) setAddr(laddr, raddr Addr) {
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
}
......
......@@ -253,18 +253,10 @@ func (fd *netFD) setAddr(laddr, raddr Addr) {
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
}
......
......@@ -44,19 +44,30 @@ func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscal
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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment