You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

291 lines
7.7 KiB

  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 dragonfly freebsd linux netbsd openbsd solaris windows
  5. package ipv6
  6. import (
  7. "net"
  8. "syscall"
  9. "golang.org/x/net/internal/netreflect"
  10. )
  11. // MulticastHopLimit returns the hop limit field value for outgoing
  12. // multicast packets.
  13. func (c *dgramOpt) MulticastHopLimit() (int, error) {
  14. if !c.ok() {
  15. return 0, syscall.EINVAL
  16. }
  17. s, err := netreflect.PacketSocketOf(c.PacketConn)
  18. if err != nil {
  19. return 0, err
  20. }
  21. return getInt(s, &sockOpts[ssoMulticastHopLimit])
  22. }
  23. // SetMulticastHopLimit sets the hop limit field value for future
  24. // outgoing multicast packets.
  25. func (c *dgramOpt) SetMulticastHopLimit(hoplim int) error {
  26. if !c.ok() {
  27. return syscall.EINVAL
  28. }
  29. s, err := netreflect.PacketSocketOf(c.PacketConn)
  30. if err != nil {
  31. return err
  32. }
  33. return setInt(s, &sockOpts[ssoMulticastHopLimit], hoplim)
  34. }
  35. // MulticastInterface returns the default interface for multicast
  36. // packet transmissions.
  37. func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
  38. if !c.ok() {
  39. return nil, syscall.EINVAL
  40. }
  41. s, err := netreflect.PacketSocketOf(c.PacketConn)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return getInterface(s, &sockOpts[ssoMulticastInterface])
  46. }
  47. // SetMulticastInterface sets the default interface for future
  48. // multicast packet transmissions.
  49. func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
  50. if !c.ok() {
  51. return syscall.EINVAL
  52. }
  53. s, err := netreflect.PacketSocketOf(c.PacketConn)
  54. if err != nil {
  55. return err
  56. }
  57. return setInterface(s, &sockOpts[ssoMulticastInterface], ifi)
  58. }
  59. // MulticastLoopback reports whether transmitted multicast packets
  60. // should be copied and send back to the originator.
  61. func (c *dgramOpt) MulticastLoopback() (bool, error) {
  62. if !c.ok() {
  63. return false, syscall.EINVAL
  64. }
  65. s, err := netreflect.PacketSocketOf(c.PacketConn)
  66. if err != nil {
  67. return false, err
  68. }
  69. on, err := getInt(s, &sockOpts[ssoMulticastLoopback])
  70. if err != nil {
  71. return false, err
  72. }
  73. return on == 1, nil
  74. }
  75. // SetMulticastLoopback sets whether transmitted multicast packets
  76. // should be copied and send back to the originator.
  77. func (c *dgramOpt) SetMulticastLoopback(on bool) error {
  78. if !c.ok() {
  79. return syscall.EINVAL
  80. }
  81. s, err := netreflect.PacketSocketOf(c.PacketConn)
  82. if err != nil {
  83. return err
  84. }
  85. return setInt(s, &sockOpts[ssoMulticastLoopback], boolint(on))
  86. }
  87. // JoinGroup joins the group address group on the interface ifi.
  88. // By default all sources that can cast data to group are accepted.
  89. // It's possible to mute and unmute data transmission from a specific
  90. // source by using ExcludeSourceSpecificGroup and
  91. // IncludeSourceSpecificGroup.
  92. // JoinGroup uses the system assigned multicast interface when ifi is
  93. // nil, although this is not recommended because the assignment
  94. // depends on platforms and sometimes it might require routing
  95. // configuration.
  96. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  97. if !c.ok() {
  98. return syscall.EINVAL
  99. }
  100. s, err := netreflect.PacketSocketOf(c.PacketConn)
  101. if err != nil {
  102. return err
  103. }
  104. grp := netAddrToIP16(group)
  105. if grp == nil {
  106. return errMissingAddress
  107. }
  108. return setGroup(s, &sockOpts[ssoJoinGroup], ifi, grp)
  109. }
  110. // LeaveGroup leaves the group address group on the interface ifi
  111. // regardless of whether the group is any-source group or
  112. // source-specific group.
  113. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  114. if !c.ok() {
  115. return syscall.EINVAL
  116. }
  117. s, err := netreflect.PacketSocketOf(c.PacketConn)
  118. if err != nil {
  119. return err
  120. }
  121. grp := netAddrToIP16(group)
  122. if grp == nil {
  123. return errMissingAddress
  124. }
  125. return setGroup(s, &sockOpts[ssoLeaveGroup], ifi, grp)
  126. }
  127. // JoinSourceSpecificGroup joins the source-specific group comprising
  128. // group and source on the interface ifi.
  129. // JoinSourceSpecificGroup uses the system assigned multicast
  130. // interface when ifi is nil, although this is not recommended because
  131. // the assignment depends on platforms and sometimes it might require
  132. // routing configuration.
  133. func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  134. if !c.ok() {
  135. return syscall.EINVAL
  136. }
  137. s, err := netreflect.PacketSocketOf(c.PacketConn)
  138. if err != nil {
  139. return err
  140. }
  141. grp := netAddrToIP16(group)
  142. if grp == nil {
  143. return errMissingAddress
  144. }
  145. src := netAddrToIP16(source)
  146. if src == nil {
  147. return errMissingAddress
  148. }
  149. return setSourceGroup(s, &sockOpts[ssoJoinSourceGroup], ifi, grp, src)
  150. }
  151. // LeaveSourceSpecificGroup leaves the source-specific group on the
  152. // interface ifi.
  153. func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  154. if !c.ok() {
  155. return syscall.EINVAL
  156. }
  157. s, err := netreflect.PacketSocketOf(c.PacketConn)
  158. if err != nil {
  159. return err
  160. }
  161. grp := netAddrToIP16(group)
  162. if grp == nil {
  163. return errMissingAddress
  164. }
  165. src := netAddrToIP16(source)
  166. if src == nil {
  167. return errMissingAddress
  168. }
  169. return setSourceGroup(s, &sockOpts[ssoLeaveSourceGroup], ifi, grp, src)
  170. }
  171. // ExcludeSourceSpecificGroup excludes the source-specific group from
  172. // the already joined any-source groups by JoinGroup on the interface
  173. // ifi.
  174. func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  175. if !c.ok() {
  176. return syscall.EINVAL
  177. }
  178. s, err := netreflect.PacketSocketOf(c.PacketConn)
  179. if err != nil {
  180. return err
  181. }
  182. grp := netAddrToIP16(group)
  183. if grp == nil {
  184. return errMissingAddress
  185. }
  186. src := netAddrToIP16(source)
  187. if src == nil {
  188. return errMissingAddress
  189. }
  190. return setSourceGroup(s, &sockOpts[ssoBlockSourceGroup], ifi, grp, src)
  191. }
  192. // IncludeSourceSpecificGroup includes the excluded source-specific
  193. // group by ExcludeSourceSpecificGroup again on the interface ifi.
  194. func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  195. if !c.ok() {
  196. return syscall.EINVAL
  197. }
  198. s, err := netreflect.PacketSocketOf(c.PacketConn)
  199. if err != nil {
  200. return err
  201. }
  202. grp := netAddrToIP16(group)
  203. if grp == nil {
  204. return errMissingAddress
  205. }
  206. src := netAddrToIP16(source)
  207. if src == nil {
  208. return errMissingAddress
  209. }
  210. return setSourceGroup(s, &sockOpts[ssoUnblockSourceGroup], ifi, grp, src)
  211. }
  212. // Checksum reports whether the kernel will compute, store or verify a
  213. // checksum for both incoming and outgoing packets. If on is true, it
  214. // returns an offset in bytes into the data of where the checksum
  215. // field is located.
  216. func (c *dgramOpt) Checksum() (on bool, offset int, err error) {
  217. if !c.ok() {
  218. return false, 0, syscall.EINVAL
  219. }
  220. s, err := netreflect.PacketSocketOf(c.PacketConn)
  221. if err != nil {
  222. return false, 0, err
  223. }
  224. offset, err = getInt(s, &sockOpts[ssoChecksum])
  225. if err != nil {
  226. return false, 0, err
  227. }
  228. if offset < 0 {
  229. return false, 0, nil
  230. }
  231. return true, offset, nil
  232. }
  233. // SetChecksum enables the kernel checksum processing. If on is ture,
  234. // the offset should be an offset in bytes into the data of where the
  235. // checksum field is located.
  236. func (c *dgramOpt) SetChecksum(on bool, offset int) error {
  237. if !c.ok() {
  238. return syscall.EINVAL
  239. }
  240. s, err := netreflect.PacketSocketOf(c.PacketConn)
  241. if err != nil {
  242. return err
  243. }
  244. if !on {
  245. offset = -1
  246. }
  247. return setInt(s, &sockOpts[ssoChecksum], offset)
  248. }
  249. // ICMPFilter returns an ICMP filter.
  250. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  251. if !c.ok() {
  252. return nil, syscall.EINVAL
  253. }
  254. s, err := netreflect.PacketSocketOf(c.PacketConn)
  255. if err != nil {
  256. return nil, err
  257. }
  258. return getICMPFilter(s, &sockOpts[ssoICMPFilter])
  259. }
  260. // SetICMPFilter deploys the ICMP filter.
  261. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  262. if !c.ok() {
  263. return syscall.EINVAL
  264. }
  265. s, err := netreflect.PacketSocketOf(c.PacketConn)
  266. if err != nil {
  267. return err
  268. }
  269. return setICMPFilter(s, &sockOpts[ssoICMPFilter], f)
  270. }