payload_cmsg.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2013 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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  6. package ipv6
  7. import (
  8. "net"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. // ReadFrom reads a payload of the received IPv6 datagram, from the
  12. // endpoint c, copying the payload into b. It returns the number of
  13. // bytes copied into b, the control message cm and the source address
  14. // src of the received datagram.
  15. func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
  16. if !c.ok() {
  17. return 0, nil, nil, errInvalidConn
  18. }
  19. c.rawOpt.RLock()
  20. m := socket.Message{
  21. Buffers: [][]byte{b},
  22. OOB: NewControlMessage(c.rawOpt.cflags),
  23. }
  24. c.rawOpt.RUnlock()
  25. switch c.PacketConn.(type) {
  26. case *net.UDPConn:
  27. if err := c.RecvMsg(&m, 0); err != nil {
  28. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  29. }
  30. case *net.IPConn:
  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. default:
  35. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
  36. }
  37. if m.NN > 0 {
  38. cm = new(ControlMessage)
  39. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  40. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  41. }
  42. cm.Src = netAddrToIP16(m.Addr)
  43. }
  44. return m.N, cm, m.Addr, nil
  45. }
  46. // WriteTo writes a payload of the IPv6 datagram, to the destination
  47. // address dst through the endpoint c, copying the payload from b. It
  48. // returns the number of bytes written. The control message cm allows
  49. // the IPv6 header fields and the datagram path to be specified. The
  50. // cm may be nil if control of the outgoing datagram is not required.
  51. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  52. if !c.ok() {
  53. return 0, errInvalidConn
  54. }
  55. m := socket.Message{
  56. Buffers: [][]byte{b},
  57. OOB: cm.Marshal(),
  58. Addr: dst,
  59. }
  60. err = c.SendMsg(&m, 0)
  61. if err != nil {
  62. err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  63. }
  64. return m.N, err
  65. }