1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:04:41 -07:00

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
This commit is contained in:
Alexey Borzenkov 2011-03-29 14:23:42 -04:00 committed by Russ Cox
parent 85c79ef7cb
commit 0793176451
3 changed files with 30 additions and 35 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)