endpoint.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. package ipv4
  5. import (
  6. "net"
  7. "syscall"
  8. "time"
  9. "golang.org/x/net/internal/netreflect"
  10. )
  11. // A Conn represents a network endpoint that uses the IPv4 transport.
  12. // It is used to control basic IP-level socket options such as TOS and
  13. // TTL.
  14. type Conn struct {
  15. genericOpt
  16. }
  17. type genericOpt struct {
  18. net.Conn
  19. }
  20. func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
  21. // NewConn returns a new Conn.
  22. func NewConn(c net.Conn) *Conn {
  23. return &Conn{
  24. genericOpt: genericOpt{Conn: c},
  25. }
  26. }
  27. // A PacketConn represents a packet network endpoint that uses the
  28. // IPv4 transport. It is used to control several IP-level socket
  29. // options including multicasting. It also provides datagram based
  30. // network I/O methods specific to the IPv4 and higher layer protocols
  31. // such as UDP.
  32. type PacketConn struct {
  33. genericOpt
  34. dgramOpt
  35. payloadHandler
  36. }
  37. type dgramOpt struct {
  38. net.PacketConn
  39. }
  40. func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil }
  41. // SetControlMessage sets the per packet IP-level socket options.
  42. func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
  43. if !c.payloadHandler.ok() {
  44. return syscall.EINVAL
  45. }
  46. s, err := netreflect.PacketSocketOf(c.dgramOpt.PacketConn)
  47. if err != nil {
  48. return err
  49. }
  50. return setControlMessage(s, &c.payloadHandler.rawOpt, cf, on)
  51. }
  52. // SetDeadline sets the read and write deadlines associated with the
  53. // endpoint.
  54. func (c *PacketConn) SetDeadline(t time.Time) error {
  55. if !c.payloadHandler.ok() {
  56. return syscall.EINVAL
  57. }
  58. return c.payloadHandler.PacketConn.SetDeadline(t)
  59. }
  60. // SetReadDeadline sets the read deadline associated with the
  61. // endpoint.
  62. func (c *PacketConn) SetReadDeadline(t time.Time) error {
  63. if !c.payloadHandler.ok() {
  64. return syscall.EINVAL
  65. }
  66. return c.payloadHandler.PacketConn.SetReadDeadline(t)
  67. }
  68. // SetWriteDeadline sets the write deadline associated with the
  69. // endpoint.
  70. func (c *PacketConn) SetWriteDeadline(t time.Time) error {
  71. if !c.payloadHandler.ok() {
  72. return syscall.EINVAL
  73. }
  74. return c.payloadHandler.PacketConn.SetWriteDeadline(t)
  75. }
  76. // Close closes the endpoint.
  77. func (c *PacketConn) Close() error {
  78. if !c.payloadHandler.ok() {
  79. return syscall.EINVAL
  80. }
  81. return c.payloadHandler.PacketConn.Close()
  82. }
  83. // NewPacketConn returns a new PacketConn using c as its underlying
  84. // transport.
  85. func NewPacketConn(c net.PacketConn) *PacketConn {
  86. p := &PacketConn{
  87. genericOpt: genericOpt{Conn: c.(net.Conn)},
  88. dgramOpt: dgramOpt{PacketConn: c},
  89. payloadHandler: payloadHandler{PacketConn: c},
  90. }
  91. if _, ok := c.(*net.IPConn); ok && sockOpts[ssoStripHeader].name > 0 {
  92. if s, err := netreflect.PacketSocketOf(c); err == nil {
  93. setInt(s, &sockOpts[ssoStripHeader], boolint(true))
  94. }
  95. }
  96. return p
  97. }
  98. // A RawConn represents a packet network endpoint that uses the IPv4
  99. // transport. It is used to control several IP-level socket options
  100. // including IPv4 header manipulation. It also provides datagram
  101. // based network I/O methods specific to the IPv4 and higher layer
  102. // protocols that handle IPv4 datagram directly such as OSPF, GRE.
  103. type RawConn struct {
  104. genericOpt
  105. dgramOpt
  106. packetHandler
  107. }
  108. // SetControlMessage sets the per packet IP-level socket options.
  109. func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
  110. if !c.packetHandler.ok() {
  111. return syscall.EINVAL
  112. }
  113. s, err := netreflect.PacketSocketOf(c.dgramOpt.PacketConn)
  114. if err != nil {
  115. return err
  116. }
  117. return setControlMessage(s, &c.packetHandler.rawOpt, cf, on)
  118. }
  119. // SetDeadline sets the read and write deadlines associated with the
  120. // endpoint.
  121. func (c *RawConn) SetDeadline(t time.Time) error {
  122. if !c.packetHandler.ok() {
  123. return syscall.EINVAL
  124. }
  125. return c.packetHandler.c.SetDeadline(t)
  126. }
  127. // SetReadDeadline sets the read deadline associated with the
  128. // endpoint.
  129. func (c *RawConn) SetReadDeadline(t time.Time) error {
  130. if !c.packetHandler.ok() {
  131. return syscall.EINVAL
  132. }
  133. return c.packetHandler.c.SetReadDeadline(t)
  134. }
  135. // SetWriteDeadline sets the write deadline associated with the
  136. // endpoint.
  137. func (c *RawConn) SetWriteDeadline(t time.Time) error {
  138. if !c.packetHandler.ok() {
  139. return syscall.EINVAL
  140. }
  141. return c.packetHandler.c.SetWriteDeadline(t)
  142. }
  143. // Close closes the endpoint.
  144. func (c *RawConn) Close() error {
  145. if !c.packetHandler.ok() {
  146. return syscall.EINVAL
  147. }
  148. return c.packetHandler.c.Close()
  149. }
  150. // NewRawConn returns a new RawConn using c as its underlying
  151. // transport.
  152. func NewRawConn(c net.PacketConn) (*RawConn, error) {
  153. r := &RawConn{
  154. genericOpt: genericOpt{Conn: c.(net.Conn)},
  155. dgramOpt: dgramOpt{PacketConn: c},
  156. packetHandler: packetHandler{c: c.(*net.IPConn)},
  157. }
  158. s, err := netreflect.PacketSocketOf(c)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if err := setInt(s, &sockOpts[ssoHeaderPrepend], boolint(true)); err != nil {
  163. return nil, err
  164. }
  165. return r, nil
  166. }