payload_cmsg_go1_9.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.9
  5. // +build !nacl,!plan9,!windows
  6. package ipv6
  7. import (
  8. "net"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. func (c *payloadHandler) readFrom(b []byte) (int, *ControlMessage, net.Addr, error) {
  12. c.rawOpt.RLock()
  13. m := socket.Message{
  14. Buffers: [][]byte{b},
  15. OOB: NewControlMessage(c.rawOpt.cflags),
  16. }
  17. c.rawOpt.RUnlock()
  18. switch c.PacketConn.(type) {
  19. case *net.UDPConn:
  20. if err := c.RecvMsg(&m, 0); err != nil {
  21. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  22. }
  23. case *net.IPConn:
  24. if err := c.RecvMsg(&m, 0); err != nil {
  25. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  26. }
  27. default:
  28. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
  29. }
  30. var cm *ControlMessage
  31. if m.NN > 0 {
  32. cm = new(ControlMessage)
  33. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  34. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  35. }
  36. cm.Src = netAddrToIP16(m.Addr)
  37. }
  38. return m.N, cm, m.Addr, nil
  39. }
  40. func (c *payloadHandler) writeTo(b []byte, cm *ControlMessage, dst net.Addr) (int, error) {
  41. m := socket.Message{
  42. Buffers: [][]byte{b},
  43. OOB: cm.Marshal(),
  44. Addr: dst,
  45. }
  46. err := c.SendMsg(&m, 0)
  47. if err != nil {
  48. err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  49. }
  50. return m.N, err
  51. }