1
2
3
4
5
6
7 package ipv6
8
9 import (
10 "net"
11
12 "golang.org/x/net/internal/socket"
13 )
14
15
16
17
18
19 func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
20 if !c.ok() {
21 return 0, nil, nil, errInvalidConn
22 }
23 c.rawOpt.RLock()
24 m := socket.Message{
25 Buffers: [][]byte{b},
26 OOB: NewControlMessage(c.rawOpt.cflags),
27 }
28 c.rawOpt.RUnlock()
29 switch c.PacketConn.(type) {
30 case *net.UDPConn:
31 if err := c.RecvMsg(&m, 0); err != nil {
32 return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
33 }
34 case *net.IPConn:
35 if err := c.RecvMsg(&m, 0); err != nil {
36 return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
37 }
38 default:
39 return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
40 }
41 if m.NN > 0 {
42 cm = new(ControlMessage)
43 if err := cm.Parse(m.OOB[:m.NN]); err != nil {
44 return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
45 }
46 cm.Src = netAddrToIP16(m.Addr)
47 }
48 return m.N, cm, m.Addr, nil
49 }
50
51
52
53
54
55
56 func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
57 if !c.ok() {
58 return 0, errInvalidConn
59 }
60 m := socket.Message{
61 Buffers: [][]byte{b},
62 OOB: cm.Marshal(),
63 Addr: dst,
64 }
65 err = c.SendMsg(&m, 0)
66 if err != nil {
67 err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
68 }
69 return m.N, err
70 }
71
View as plain text