dgramopt_posix.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2013 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. // +build darwin freebsd linux netbsd openbsd windows
  5. package ipv6
  6. import (
  7. "net"
  8. "syscall"
  9. )
  10. // MulticastHopLimit returns the hop limit field value for outgoing
  11. // multicast packets.
  12. func (c *dgramOpt) MulticastHopLimit() (int, error) {
  13. if !c.ok() {
  14. return 0, syscall.EINVAL
  15. }
  16. fd, err := c.sysfd()
  17. if err != nil {
  18. return 0, err
  19. }
  20. return ipv6MulticastHopLimit(fd)
  21. }
  22. // SetMulticastHopLimit sets the hop limit field value for future
  23. // outgoing multicast packets.
  24. func (c *dgramOpt) SetMulticastHopLimit(hoplim int) error {
  25. if !c.ok() {
  26. return syscall.EINVAL
  27. }
  28. fd, err := c.sysfd()
  29. if err != nil {
  30. return err
  31. }
  32. return setIPv6MulticastHopLimit(fd, hoplim)
  33. }
  34. // MulticastInterface returns the default interface for multicast
  35. // packet transmissions.
  36. func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
  37. if !c.ok() {
  38. return nil, syscall.EINVAL
  39. }
  40. fd, err := c.sysfd()
  41. if err != nil {
  42. return nil, err
  43. }
  44. return ipv6MulticastInterface(fd)
  45. }
  46. // SetMulticastInterface sets the default interface for future
  47. // multicast packet transmissions.
  48. func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
  49. if !c.ok() {
  50. return syscall.EINVAL
  51. }
  52. fd, err := c.sysfd()
  53. if err != nil {
  54. return err
  55. }
  56. return setIPv6MulticastInterface(fd, ifi)
  57. }
  58. // MulticastLoopback reports whether transmitted multicast packets
  59. // should be copied and send back to the originator.
  60. func (c *dgramOpt) MulticastLoopback() (bool, error) {
  61. if !c.ok() {
  62. return false, syscall.EINVAL
  63. }
  64. fd, err := c.sysfd()
  65. if err != nil {
  66. return false, err
  67. }
  68. return ipv6MulticastLoopback(fd)
  69. }
  70. // SetMulticastLoopback sets whether transmitted multicast packets
  71. // should be copied and send back to the originator.
  72. func (c *dgramOpt) SetMulticastLoopback(on bool) error {
  73. if !c.ok() {
  74. return syscall.EINVAL
  75. }
  76. fd, err := c.sysfd()
  77. if err != nil {
  78. return err
  79. }
  80. return setIPv6MulticastLoopback(fd, on)
  81. }
  82. // JoinGroup joins the group address group on the interface ifi.
  83. // It uses the system assigned multicast interface when ifi is nil,
  84. // although this is not recommended because the assignment depends on
  85. // platforms and sometimes it might require routing configuration.
  86. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  87. if !c.ok() {
  88. return syscall.EINVAL
  89. }
  90. fd, err := c.sysfd()
  91. if err != nil {
  92. return err
  93. }
  94. grp := netAddrToIP16(group)
  95. if grp == nil {
  96. return errMissingAddress
  97. }
  98. return joinIPv6Group(fd, ifi, grp)
  99. }
  100. // LeaveGroup leaves the group address group on the interface ifi.
  101. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  102. if !c.ok() {
  103. return syscall.EINVAL
  104. }
  105. fd, err := c.sysfd()
  106. if err != nil {
  107. return err
  108. }
  109. grp := netAddrToIP16(group)
  110. if grp == nil {
  111. return errMissingAddress
  112. }
  113. return leaveIPv6Group(fd, ifi, grp)
  114. }
  115. // Checksum reports whether the kernel will compute, store or verify a
  116. // checksum for both incoming and outgoing packets. If on is true, it
  117. // returns an offset in bytes into the data of where the checksum
  118. // field is located.
  119. func (c *dgramOpt) Checksum() (on bool, offset int, err error) {
  120. if !c.ok() {
  121. return false, 0, syscall.EINVAL
  122. }
  123. fd, err := c.sysfd()
  124. if err != nil {
  125. return false, 0, err
  126. }
  127. return ipv6Checksum(fd)
  128. }
  129. // SetChecksum enables the kernel checksum processing. If on is ture,
  130. // the offset should be an offset in bytes into the data of where the
  131. // checksum field is located.
  132. func (c *dgramOpt) SetChecksum(on bool, offset int) error {
  133. if !c.ok() {
  134. return syscall.EINVAL
  135. }
  136. fd, err := c.sysfd()
  137. if err != nil {
  138. return err
  139. }
  140. return setIPv6Checksum(fd, on, offset)
  141. }
  142. // ICMPFilter returns an ICMP filter.
  143. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  144. if !c.ok() {
  145. return nil, syscall.EINVAL
  146. }
  147. fd, err := c.sysfd()
  148. if err != nil {
  149. return nil, err
  150. }
  151. return ipv6ICMPFilter(fd)
  152. }
  153. // SetICMPFilter deploys the ICMP filter.
  154. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  155. if !c.ok() {
  156. return syscall.EINVAL
  157. }
  158. fd, err := c.sysfd()
  159. if err != nil {
  160. return err
  161. }
  162. return setIPv6ICMPFilter(fd, f)
  163. }