endpoint.go 5.1 KB

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