endpoint.go 5.0 KB

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