ReadFrom reads a payload of the received IPv6 datagram, from the endpoint c, copying the payload into b. It returns the number of bytes copied into b, the control message cm and the source address src of the received datagram.
(b []byte)
| 17 | // bytes copied into b, the control message cm and the source address |
| 18 | // src of the received datagram. |
| 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 | // WriteTo writes a payload of the IPv6 datagram, to the destination |
| 52 | // address dst through the endpoint c, copying the payload from b. It |