You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
1.9 KiB

  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. // +build !nacl,!plan9,!windows
  5. package ipv6
  6. import (
  7. "net"
  8. "syscall"
  9. )
  10. // ReadFrom reads a payload of the received IPv6 datagram, from the
  11. // endpoint c, copying the payload into b. It returns the number of
  12. // bytes copied into b, the control message cm and the source address
  13. // src of the received datagram.
  14. func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
  15. if !c.ok() {
  16. return 0, nil, nil, syscall.EINVAL
  17. }
  18. oob := newControlMessage(&c.rawOpt)
  19. var oobn int
  20. switch c := c.PacketConn.(type) {
  21. case *net.UDPConn:
  22. if n, oobn, _, src, err = c.ReadMsgUDP(b, oob); err != nil {
  23. return 0, nil, nil, err
  24. }
  25. case *net.IPConn:
  26. if n, oobn, _, src, err = c.ReadMsgIP(b, oob); err != nil {
  27. return 0, nil, nil, err
  28. }
  29. default:
  30. return 0, nil, nil, errInvalidConnType
  31. }
  32. if cm, err = parseControlMessage(oob[:oobn]); err != nil {
  33. return 0, nil, nil, err
  34. }
  35. if cm != nil {
  36. cm.Src = netAddrToIP16(src)
  37. }
  38. return
  39. }
  40. // WriteTo writes a payload of the IPv6 datagram, to the destination
  41. // address dst through the endpoint c, copying the payload from b. It
  42. // returns the number of bytes written. The control message cm allows
  43. // the IPv6 header fields and the datagram path to be specified. The
  44. // cm may be nil if control of the outgoing datagram is not required.
  45. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  46. if !c.ok() {
  47. return 0, syscall.EINVAL
  48. }
  49. oob := marshalControlMessage(cm)
  50. if dst == nil {
  51. return 0, errMissingAddress
  52. }
  53. switch c := c.PacketConn.(type) {
  54. case *net.UDPConn:
  55. n, _, err = c.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
  56. case *net.IPConn:
  57. n, _, err = c.WriteMsgIP(b, oob, dst.(*net.IPAddr))
  58. default:
  59. return 0, errInvalidConnType
  60. }
  61. if err != nil {
  62. return 0, err
  63. }
  64. return
  65. }