endpoint.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. c net.Conn
  18. }
  19. func (c *genericOpt) ok() bool { return c != nil && c.c != nil }
  20. // NewConn returns a new Conn.
  21. func NewConn(c net.Conn) *Conn {
  22. return &Conn{
  23. genericOpt: genericOpt{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. c net.PacketConn
  38. }
  39. func (c *dgramOpt) ok() bool { return c != nil && c.c != 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.c.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.c.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.c.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.c.Close()
  81. }
  82. // NewPacketConn returns a new PacketConn using c as its underlying
  83. // transport.
  84. func NewPacketConn(c net.PacketConn) *PacketConn {
  85. return &PacketConn{
  86. genericOpt: genericOpt{c.(net.Conn)},
  87. dgramOpt: dgramOpt{c},
  88. payloadHandler: payloadHandler{c: c},
  89. }
  90. }
  91. // A RawConn represents a packet network endpoint that uses the IPv4
  92. // transport. It is used to control several IP-level socket options
  93. // including IPv4 header manipulation. It also provides datagram
  94. // based network I/O methods specific to the IPv4 and higher layer
  95. // protocols that handle IPv4 datagram directly such as OSPF, GRE.
  96. type RawConn struct {
  97. genericOpt
  98. dgramOpt
  99. packetHandler
  100. }
  101. // SetControlMessage sets the per packet IP-level socket options.
  102. func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
  103. if !c.packetHandler.ok() {
  104. return syscall.EINVAL
  105. }
  106. fd, err := c.packetHandler.sysfd()
  107. if err != nil {
  108. return err
  109. }
  110. return setControlMessage(fd, &c.packetHandler.rawOpt, cf, on)
  111. }
  112. // SetDeadline sets the read and write deadlines associated with the
  113. // endpoint.
  114. func (c *RawConn) SetDeadline(t time.Time) error {
  115. if !c.packetHandler.ok() {
  116. return syscall.EINVAL
  117. }
  118. return c.packetHandler.c.SetDeadline(t)
  119. }
  120. // SetReadDeadline sets the read deadline associated with the
  121. // endpoint.
  122. func (c *RawConn) SetReadDeadline(t time.Time) error {
  123. if !c.packetHandler.ok() {
  124. return syscall.EINVAL
  125. }
  126. return c.packetHandler.c.SetReadDeadline(t)
  127. }
  128. // SetWriteDeadline sets the write deadline associated with the
  129. // endpoint.
  130. func (c *RawConn) SetWriteDeadline(t time.Time) error {
  131. if !c.packetHandler.ok() {
  132. return syscall.EINVAL
  133. }
  134. return c.packetHandler.c.SetWriteDeadline(t)
  135. }
  136. // Close closes the endpoint.
  137. func (c *RawConn) Close() error {
  138. if !c.packetHandler.ok() {
  139. return syscall.EINVAL
  140. }
  141. return c.packetHandler.c.Close()
  142. }
  143. // NewRawConn returns a new RawConn using c as it sunderlying
  144. // transport.
  145. func NewRawConn(c net.PacketConn) (*RawConn, error) {
  146. r := &RawConn{
  147. genericOpt: genericOpt{c.(net.Conn)},
  148. dgramOpt: dgramOpt{c},
  149. packetHandler: packetHandler{c: c.(*net.IPConn)},
  150. }
  151. fd, err := r.packetHandler.sysfd()
  152. if err != nil {
  153. return nil, err
  154. }
  155. if err := setIPv4HeaderPrepend(fd, true); err != nil {
  156. return nil, err
  157. }
  158. return r, nil
  159. }