~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/net/udpsock_plan9.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
 
5
 
// UDP for Plan 9
6
 
 
7
5
package net
8
6
 
9
7
import (
13
11
        "time"
14
12
)
15
13
 
16
 
// UDPConn is the implementation of the Conn and PacketConn
17
 
// interfaces for UDP network connections.
 
14
// UDPConn is the implementation of the Conn and PacketConn interfaces
 
15
// for UDP network connections.
18
16
type UDPConn struct {
19
 
        plan9Conn
20
 
}
21
 
 
22
 
// SetDeadline implements the Conn SetDeadline method.
23
 
func (c *UDPConn) SetDeadline(t time.Time) error {
24
 
        return syscall.EPLAN9
25
 
}
26
 
 
27
 
// SetReadDeadline implements the Conn SetReadDeadline method.
28
 
func (c *UDPConn) SetReadDeadline(t time.Time) error {
29
 
        return syscall.EPLAN9
30
 
}
31
 
 
32
 
// SetWriteDeadline implements the Conn SetWriteDeadline method.
33
 
func (c *UDPConn) SetWriteDeadline(t time.Time) error {
34
 
        return syscall.EPLAN9
35
 
}
36
 
 
37
 
// UDP-specific methods.
 
17
        conn
 
18
}
 
19
 
 
20
func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
38
21
 
39
22
// ReadFromUDP reads a UDP packet from c, copying the payload into b.
40
23
// It returns the number of bytes copied into b and the return address
41
24
// that was on the packet.
42
25
//
43
 
// ReadFromUDP can be made to time out and return an error with Timeout() == true
44
 
// after a fixed time limit; see SetDeadline and SetReadDeadline.
 
26
// ReadFromUDP can be made to time out and return an error with
 
27
// Timeout() == true after a fixed time limit; see SetDeadline and
 
28
// SetReadDeadline.
45
29
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
46
 
        if !c.ok() {
 
30
        if !c.ok() || c.fd.data == nil {
47
31
                return 0, nil, syscall.EINVAL
48
32
        }
49
 
        if c.data == nil {
50
 
                c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
51
 
                if err != nil {
52
 
                        return 0, nil, err
53
 
                }
54
 
        }
55
33
        buf := make([]byte, udpHeaderSize+len(b))
56
 
        m, err := c.data.Read(buf)
 
34
        m, err := c.fd.data.Read(buf)
57
35
        if err != nil {
58
36
                return
59
37
        }
64
42
 
65
43
        h, buf := unmarshalUDPHeader(buf)
66
44
        n = copy(b, buf)
67
 
        return n, &UDPAddr{h.raddr, int(h.rport)}, nil
 
45
        return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil
68
46
}
69
47
 
70
48
// ReadFrom implements the PacketConn ReadFrom method.
71
 
func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err error) {
 
49
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
72
50
        if !c.ok() {
73
51
                return 0, nil, syscall.EINVAL
74
52
        }
75
53
        return c.ReadFromUDP(b)
76
54
}
77
55
 
78
 
// WriteToUDP writes a UDP packet to addr via c, copying the payload from b.
 
56
// ReadMsgUDP reads a packet from c, copying the payload into b and
 
57
// the associated out-of-band data into oob.  It returns the number
 
58
// of bytes copied into b, the number of bytes copied into oob, the
 
59
// flags that were set on the packet and the source address of the
 
60
// packet.
 
61
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
 
62
        return 0, 0, 0, nil, syscall.EPLAN9
 
63
}
 
64
 
 
65
// WriteToUDP writes a UDP packet to addr via c, copying the payload
 
66
// from b.
79
67
//
80
 
// WriteToUDP can be made to time out and return
81
 
// an error with Timeout() == true after a fixed time limit;
82
 
// see SetDeadline and SetWriteDeadline.
83
 
// On packet-oriented connections, write timeouts are rare.
84
 
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err error) {
85
 
        if !c.ok() {
 
68
// WriteToUDP can be made to time out and return an error with
 
69
// Timeout() == true after a fixed time limit; see SetDeadline and
 
70
// SetWriteDeadline.  On packet-oriented connections, write timeouts
 
71
// are rare.
 
72
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
 
73
        if !c.ok() || c.fd.data == nil {
86
74
                return 0, syscall.EINVAL
87
75
        }
88
 
        if c.data == nil {
89
 
                c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
90
 
                if err != nil {
91
 
                        return 0, err
92
 
                }
93
 
        }
94
76
        h := new(udpHeader)
95
77
        h.raddr = addr.IP.To16()
96
 
        h.laddr = c.laddr.(*UDPAddr).IP.To16()
 
78
        h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
97
79
        h.ifcaddr = IPv6zero // ignored (receive only)
98
80
        h.rport = uint16(addr.Port)
99
 
        h.lport = uint16(c.laddr.(*UDPAddr).Port)
 
81
        h.lport = uint16(c.fd.laddr.(*UDPAddr).Port)
100
82
 
101
83
        buf := make([]byte, udpHeaderSize+len(b))
102
84
        i := copy(buf, h.Bytes())
103
85
        copy(buf[i:], b)
104
 
        return c.data.Write(buf)
 
86
        return c.fd.data.Write(buf)
105
87
}
106
88
 
107
89
// WriteTo implements the PacketConn WriteTo method.
108
 
func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err error) {
 
90
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
109
91
        if !c.ok() {
110
92
                return 0, syscall.EINVAL
111
93
        }
112
94
        a, ok := addr.(*UDPAddr)
113
95
        if !ok {
114
 
                return 0, &OpError{"write", c.dir, addr, syscall.EINVAL}
 
96
                return 0, &OpError{"write", c.fd.dir, addr, syscall.EINVAL}
115
97
        }
116
98
        return c.WriteToUDP(b, a)
117
99
}
118
100
 
 
101
// WriteMsgUDP writes a packet to addr via c, copying the payload from
 
102
// b and the associated out-of-band data from oob.  It returns the
 
103
// number of payload and out-of-band bytes written.
 
104
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
 
105
        return 0, 0, syscall.EPLAN9
 
106
}
 
107
 
119
108
// DialUDP connects to the remote address raddr on the network net,
120
 
// which must be "udp", "udp4", or "udp6".  If laddr is not nil, it is used
121
 
// as the local address for the connection.
122
 
func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) {
 
109
// which must be "udp", "udp4", or "udp6".  If laddr is not nil, it is
 
110
// used as the local address for the connection.
 
111
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
 
112
        return dialUDP(net, laddr, raddr, noDeadline)
 
113
}
 
114
 
 
115
func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
 
116
        if !deadline.IsZero() {
 
117
                panic("net.dialUDP: deadline not implemented on Plan 9")
 
118
        }
123
119
        switch net {
124
120
        case "udp", "udp4", "udp6":
125
121
        default:
128
124
        if raddr == nil {
129
125
                return nil, &OpError{"dial", net, nil, errMissingAddress}
130
126
        }
131
 
        c1, err := dialPlan9(net, laddr, raddr)
 
127
        fd, err := dialPlan9(net, laddr, raddr)
132
128
        if err != nil {
133
 
                return
 
129
                return nil, err
134
130
        }
135
 
        return &UDPConn{*c1}, nil
 
131
        return newUDPConn(fd), nil
136
132
}
137
133
 
138
134
const udpHeaderSize = 16*3 + 2*2
163
159
        return h, b
164
160
}
165
161
 
166
 
// ListenUDP listens for incoming UDP packets addressed to the
167
 
// local address laddr.  The returned connection c's ReadFrom
168
 
// and WriteTo methods can be used to receive and send UDP
169
 
// packets with per-packet addressing.
170
 
func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err error) {
 
162
// ListenUDP listens for incoming UDP packets addressed to the local
 
163
// address laddr.  Net must be "udp", "udp4", or "udp6".  If laddr has
 
164
// a port of 0, ListenUDP will choose an available port.
 
165
// The LocalAddr method of the returned UDPConn can be used to
 
166
// discover the port.  The returned connection's ReadFrom and WriteTo
 
167
// methods can be used to receive and send UDP packets with per-packet
 
168
// addressing.
 
169
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
171
170
        switch net {
172
171
        case "udp", "udp4", "udp6":
173
172
        default:
174
173
                return nil, UnknownNetworkError(net)
175
174
        }
176
175
        if laddr == nil {
177
 
                return nil, &OpError{"listen", net, nil, errMissingAddress}
 
176
                laddr = &UDPAddr{}
178
177
        }
179
178
        l, err := listenPlan9(net, laddr)
180
179
        if err != nil {
181
 
                return
 
180
                return nil, err
182
181
        }
183
182
        _, err = l.ctl.WriteString("headers")
184
183
        if err != nil {
185
 
                return
186
 
        }
187
 
        return &UDPConn{*l.plan9Conn()}, nil
 
184
                return nil, err
 
185
        }
 
186
        l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
 
187
        if err != nil {
 
188
                return nil, err
 
189
        }
 
190
        return newUDPConn(l.netFD()), nil
188
191
}
189
192
 
190
193
// ListenMulticastUDP listens for incoming multicast UDP packets
191
 
// addressed to the group address gaddr on ifi, which specifies
192
 
// the interface to join.  ListenMulticastUDP uses default
193
 
// multicast interface if ifi is nil.
 
194
// addressed to the group address gaddr on ifi, which specifies the
 
195
// interface to join.  ListenMulticastUDP uses default multicast
 
196
// interface if ifi is nil.
194
197
func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
195
198
        return nil, syscall.EPLAN9
196
199
}