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