diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2013-03-04 21:27:36 +0100 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2013-03-04 21:27:36 +0100 |
commit | 04b08da9af0c450d645ab7389d1467308cfc2db8 (patch) | |
tree | db247935fa4f2f94408edc3acd5d0d4f997aa0d8 /src/pkg/net/udpsock_plan9.go | |
parent | 917c5fb8ec48e22459d77e3849e6d388f93d3260 (diff) | |
download | golang-upstream/1.1_hg20130304.tar.gz |
Imported Upstream version 1.1~hg20130304upstream/1.1_hg20130304
Diffstat (limited to 'src/pkg/net/udpsock_plan9.go')
-rw-r--r-- | src/pkg/net/udpsock_plan9.go | 134 |
1 files changed, 69 insertions, 65 deletions
diff --git a/src/pkg/net/udpsock_plan9.go b/src/pkg/net/udpsock_plan9.go index 4f298a42f..2a7e3d19c 100644 --- a/src/pkg/net/udpsock_plan9.go +++ b/src/pkg/net/udpsock_plan9.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// UDP for Plan 9 +// UDP sockets for Plan 9 package net @@ -16,44 +16,26 @@ import ( // UDPConn is the implementation of the Conn and PacketConn // interfaces for UDP network connections. type UDPConn struct { - plan9Conn + conn } -// SetDeadline implements the Conn SetDeadline method. -func (c *UDPConn) SetDeadline(t time.Time) error { - return syscall.EPLAN9 +func newUDPConn(fd *netFD) *UDPConn { + return &UDPConn{conn{fd}} } -// SetReadDeadline implements the Conn SetReadDeadline method. -func (c *UDPConn) SetReadDeadline(t time.Time) error { - return syscall.EPLAN9 -} - -// SetWriteDeadline implements the Conn SetWriteDeadline method. -func (c *UDPConn) SetWriteDeadline(t time.Time) error { - return syscall.EPLAN9 -} - -// UDP-specific methods. - // 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. // -// ReadFromUDP can be made to time out and return an error with Timeout() == true -// after a fixed time limit; see SetDeadline and SetReadDeadline. +// ReadFromUDP can be made to time out and return an error with +// 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.data == nil { - c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0) - if err != nil { - return 0, nil, err - } - } buf := make([]byte, udpHeaderSize+len(b)) - m, err := c.data.Read(buf) + m, err := c.fd.data.Read(buf) if err != nil { return } @@ -64,62 +46,80 @@ func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) { h, buf := unmarshalUDPHeader(buf) n = copy(b, buf) - return n, &UDPAddr{h.raddr, int(h.rport)}, nil + return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil } // ReadFrom implements the PacketConn ReadFrom method. -func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err error) { +func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) { if !c.ok() { return 0, nil, syscall.EINVAL } return c.ReadFromUDP(b) } -// WriteToUDP writes a UDP packet to addr via c, copying the payload from b. +// ReadMsgUDP reads a packet from c, copying the payload into b and +// the associdated out-of-band data into oob. It returns the number +// of bytes copied into b, the number of bytes copied into oob, the +// flags that were set on the packet and the source address of the +// packet. +func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) { + return 0, 0, 0, nil, syscall.EPLAN9 +} + +// WriteToUDP writes a UDP packet to addr via c, copying the payload +// from b. // -// WriteToUDP can be made to time out and return -// an error with Timeout() == true after a fixed time limit; -// see SetDeadline and SetWriteDeadline. -// On packet-oriented connections, write timeouts are rare. -func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err error) { - if !c.ok() { +// WriteToUDP can be made to time out and return an error with +// Timeout() == true after a fixed time limit; see SetDeadline and +// SetWriteDeadline. On packet-oriented connections, write timeouts +// are rare. +func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) { + if !c.ok() || c.fd.data == nil { return 0, syscall.EINVAL } - if c.data == nil { - c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0) - if err != nil { - return 0, err - } - } h := new(udpHeader) h.raddr = addr.IP.To16() - h.laddr = c.laddr.(*UDPAddr).IP.To16() + h.laddr = c.fd.laddr.(*UDPAddr).IP.To16() h.ifcaddr = IPv6zero // ignored (receive only) h.rport = uint16(addr.Port) - h.lport = uint16(c.laddr.(*UDPAddr).Port) + h.lport = uint16(c.fd.laddr.(*UDPAddr).Port) buf := make([]byte, udpHeaderSize+len(b)) i := copy(buf, h.Bytes()) copy(buf[i:], b) - return c.data.Write(buf) + return c.fd.data.Write(buf) } // WriteTo implements the PacketConn WriteTo method. -func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err error) { +func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) { if !c.ok() { return 0, syscall.EINVAL } a, ok := addr.(*UDPAddr) if !ok { - return 0, &OpError{"write", c.dir, addr, syscall.EINVAL} + return 0, &OpError{"write", c.fd.dir, addr, syscall.EINVAL} } return c.WriteToUDP(b, a) } +// WriteMsgUDP writes a packet to addr via c, copying the payload from +// b and the associated out-of-band data from oob. It returns the +// number of payload and out-of-band bytes written. +func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) { + return 0, 0, syscall.EPLAN9 +} + // DialUDP connects to the remote address raddr on the network net, -// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used -// as the local address for the connection. -func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) { +// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is +// used as the local address for the connection. +func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) { + return dialUDP(net, laddr, raddr, noDeadline) +} + +func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) { + if !deadline.IsZero() { + panic("net.dialUDP: deadline not implemented on Plan 9") + } switch net { case "udp", "udp4", "udp6": default: @@ -128,11 +128,11 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) { if raddr == nil { return nil, &OpError{"dial", net, nil, errMissingAddress} } - c1, err := dialPlan9(net, laddr, raddr) + fd, err := dialPlan9(net, laddr, raddr) if err != nil { - return + return nil, err } - return &UDPConn{*c1}, nil + return newUDPConn(fd), nil } const udpHeaderSize = 16*3 + 2*2 @@ -163,34 +163,38 @@ func unmarshalUDPHeader(b []byte) (*udpHeader, []byte) { return h, b } -// ListenUDP listens for incoming UDP packets addressed to the -// local address laddr. The returned connection c's ReadFrom -// and WriteTo methods can be used to receive and send UDP -// packets with per-packet addressing. -func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err error) { +// ListenUDP listens for incoming UDP packets addressed to the local +// address laddr. The returned connection c's ReadFrom and WriteTo +// methods can be used to receive and send UDP packets with per-packet +// addressing. +func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) { switch net { case "udp", "udp4", "udp6": default: return nil, UnknownNetworkError(net) } if laddr == nil { - return nil, &OpError{"listen", net, nil, errMissingAddress} + laddr = &UDPAddr{} } l, err := listenPlan9(net, laddr) if err != nil { - return + return nil, err } _, err = l.ctl.WriteString("headers") if err != nil { - return + return nil, err + } + l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0) + if err != nil { + return nil, err } - return &UDPConn{*l.plan9Conn()}, nil + return newUDPConn(l.netFD()), nil } // ListenMulticastUDP listens for incoming multicast UDP packets -// addressed to the group address gaddr on ifi, which specifies -// the interface to join. ListenMulticastUDP uses default -// multicast interface if ifi is nil. +// addressed to the group address gaddr on ifi, which specifies the +// interface to join. ListenMulticastUDP uses default multicast +// interface if ifi is nil. func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) { return nil, syscall.EPLAN9 } |