Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

82 строки
2.7 KiB

  1. // Copyright 2012 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 darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package ipv4
  6. import (
  7. "net"
  8. "golang.org/x/net/internal/socket"
  9. )
  10. // ReadFrom reads a payload of the received IPv4 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, errInvalidConn
  17. }
  18. c.rawOpt.RLock()
  19. m := socket.Message{
  20. OOB: NewControlMessage(c.rawOpt.cflags),
  21. }
  22. c.rawOpt.RUnlock()
  23. switch c.PacketConn.(type) {
  24. case *net.UDPConn:
  25. m.Buffers = [][]byte{b}
  26. if err := c.RecvMsg(&m, 0); err != nil {
  27. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  28. }
  29. case *net.IPConn:
  30. h := make([]byte, HeaderLen)
  31. m.Buffers = [][]byte{h, b}
  32. if err := c.RecvMsg(&m, 0); err != nil {
  33. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  34. }
  35. hdrlen := int(h[0]&0x0f) << 2
  36. if hdrlen > len(h) {
  37. d := hdrlen - len(h)
  38. copy(b, b[d:])
  39. m.N -= d
  40. } else {
  41. m.N -= hdrlen
  42. }
  43. default:
  44. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
  45. }
  46. if m.NN > 0 {
  47. cm = new(ControlMessage)
  48. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  49. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  50. }
  51. cm.Src = netAddrToIP4(m.Addr)
  52. }
  53. return m.N, cm, m.Addr, nil
  54. }
  55. // WriteTo writes a payload of the IPv4 datagram, to the destination
  56. // address dst through the endpoint c, copying the payload from b. It
  57. // returns the number of bytes written. The control message cm allows
  58. // the datagram path and the outgoing interface to be specified.
  59. // Currently only Darwin and Linux support this. The cm may be nil if
  60. // control of the outgoing datagram is not required.
  61. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  62. if !c.ok() {
  63. return 0, errInvalidConn
  64. }
  65. m := socket.Message{
  66. Buffers: [][]byte{b},
  67. OOB: cm.Marshal(),
  68. Addr: dst,
  69. }
  70. err = c.SendMsg(&m, 0)
  71. if err != nil {
  72. err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  73. }
  74. return m.N, err
  75. }