net: Plan 9: open data file and set remote-addr properly
The data file should be opened when a Conn is first
established, rather than waiting for the first Read or
Write.
Upon Close, we now make sure to try to close both, the
ctl as well as data files and set both to nil, even in
the face of errors, instead of returning early.
The Accept call was not setting the remote address
of the connection properly. Now, we read the correct
file.
Make functions that establish Conn use newTCPConn
or newUDPConn.
R=rsc, rminnich, ality, dave
CC=golang-dev
https://golang.org/cl/7228068
diff --git a/src/pkg/net/udpsock_plan9.go b/src/pkg/net/udpsock_plan9.go
index b9ade48..2a7e3d1 100644
--- a/src/pkg/net/udpsock_plan9.go
+++ b/src/pkg/net/udpsock_plan9.go
@@ -19,6 +19,10 @@
conn
}
+func newUDPConn(fd *netFD) *UDPConn {
+ return &UDPConn{conn{fd}}
+}
+
// ReadFromUDP reads a UDP packet from c, copying the payload into b.
// It returns the number of bytes copied into b and the return address
// that was on the packet.
@@ -27,15 +31,9 @@
// Timeout() == true after a fixed time limit; see SetDeadline and
// SetReadDeadline.
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
- if !c.ok() {
+ if !c.ok() || c.fd.data == nil {
return 0, nil, syscall.EINVAL
}
- if c.fd.data == nil {
- c.fd.data, err = os.OpenFile(c.fd.dir+"/data", os.O_RDWR, 0)
- if err != nil {
- return 0, nil, err
- }
- }
buf := make([]byte, udpHeaderSize+len(b))
m, err := c.fd.data.Read(buf)
if err != nil {
@@ -76,16 +74,9 @@
// SetWriteDeadline. On packet-oriented connections, write timeouts
// are rare.
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
- if !c.ok() {
+ if !c.ok() || c.fd.data == nil {
return 0, syscall.EINVAL
}
- if c.fd.data == nil {
- f, err := os.OpenFile(c.fd.dir+"/data", os.O_RDWR, 0)
- if err != nil {
- return 0, err
- }
- c.fd.data = f
- }
h := new(udpHeader)
h.raddr = addr.IP.To16()
h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
@@ -141,7 +132,7 @@
if err != nil {
return nil, err
}
- return &UDPConn{conn{fd}}, nil
+ return newUDPConn(fd), nil
}
const udpHeaderSize = 16*3 + 2*2
@@ -193,7 +184,11 @@
if err != nil {
return nil, err
}
- return &UDPConn{conn{l.netFD()}}, nil
+ l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
+ if err != nil {
+ return nil, err
+ }
+ return newUDPConn(l.netFD()), nil
}
// ListenMulticastUDP listens for incoming multicast UDP packets