endpoint.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "time"
  8. "golang.org/x/net/internal/socket"
  9. )
  10. // BUG(mikio): On Windows, the JoinSourceSpecificGroup,
  11. // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
  12. // IncludeSourceSpecificGroup methods of PacketConn and RawConn are
  13. // not implemented.
  14. // A Conn represents a network endpoint that uses the IPv4 transport.
  15. // It is used to control basic IP-level socket options such as TOS and
  16. // TTL.
  17. type Conn struct {
  18. genericOpt
  19. }
  20. type genericOpt struct {
  21. *socket.Conn
  22. }
  23. func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
  24. // NewConn returns a new Conn.
  25. func NewConn(c net.Conn) *Conn {
  26. cc, _ := socket.NewConn(c)
  27. return &Conn{
  28. genericOpt: genericOpt{Conn: cc},
  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. *socket.Conn
  43. }
  44. func (c *dgramOpt) ok() bool { return c != nil && c.Conn != 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 errInvalidConn
  49. }
  50. return setControlMessage(c.dgramOpt.Conn, &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 errInvalidConn
  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 errInvalidConn
  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 errInvalidConn
  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 errInvalidConn
  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. cc, _ := socket.NewConn(c.(net.Conn))
  87. p := &PacketConn{
  88. genericOpt: genericOpt{Conn: cc},
  89. dgramOpt: dgramOpt{Conn: cc},
  90. payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
  91. }
  92. return p
  93. }
  94. // A RawConn represents a packet network endpoint that uses the IPv4
  95. // transport. It is used to control several IP-level socket options
  96. // including IPv4 header manipulation. It also provides datagram
  97. // based network I/O methods specific to the IPv4 and higher layer
  98. // protocols that handle IPv4 datagram directly such as OSPF, GRE.
  99. type RawConn struct {
  100. genericOpt
  101. dgramOpt
  102. packetHandler
  103. }
  104. // SetControlMessage sets the per packet IP-level socket options.
  105. func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
  106. if !c.packetHandler.ok() {
  107. return errInvalidConn
  108. }
  109. return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
  110. }
  111. // SetDeadline sets the read and write deadlines associated with the
  112. // endpoint.
  113. func (c *RawConn) SetDeadline(t time.Time) error {
  114. if !c.packetHandler.ok() {
  115. return errInvalidConn
  116. }
  117. return c.packetHandler.IPConn.SetDeadline(t)
  118. }
  119. // SetReadDeadline sets the read deadline associated with the
  120. // endpoint.
  121. func (c *RawConn) SetReadDeadline(t time.Time) error {
  122. if !c.packetHandler.ok() {
  123. return errInvalidConn
  124. }
  125. return c.packetHandler.IPConn.SetReadDeadline(t)
  126. }
  127. // SetWriteDeadline sets the write deadline associated with the
  128. // endpoint.
  129. func (c *RawConn) SetWriteDeadline(t time.Time) error {
  130. if !c.packetHandler.ok() {
  131. return errInvalidConn
  132. }
  133. return c.packetHandler.IPConn.SetWriteDeadline(t)
  134. }
  135. // Close closes the endpoint.
  136. func (c *RawConn) Close() error {
  137. if !c.packetHandler.ok() {
  138. return errInvalidConn
  139. }
  140. return c.packetHandler.IPConn.Close()
  141. }
  142. // NewRawConn returns a new RawConn using c as its underlying
  143. // transport.
  144. func NewRawConn(c net.PacketConn) (*RawConn, error) {
  145. cc, err := socket.NewConn(c.(net.Conn))
  146. if err != nil {
  147. return nil, err
  148. }
  149. r := &RawConn{
  150. genericOpt: genericOpt{Conn: cc},
  151. dgramOpt: dgramOpt{Conn: cc},
  152. packetHandler: packetHandler{IPConn: c.(*net.IPConn), Conn: cc},
  153. }
  154. so, ok := sockOpts[ssoHeaderPrepend]
  155. if !ok {
  156. return nil, errNotImplemented
  157. }
  158. if err := so.SetInt(r.dgramOpt.Conn, boolint(true)); err != nil {
  159. return nil, err
  160. }
  161. return r, nil
  162. }