dgramopt_posix.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 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 getInt(fd, &sockOpts[ssoMulticastHopLimit])
  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 setInt(fd, &sockOpts[ssoMulticastHopLimit], 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 getInterface(fd, &sockOpts[ssoMulticastInterface])
  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 setInterface(fd, &sockOpts[ssoMulticastInterface], 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. on, err := getInt(fd, &sockOpts[ssoMulticastLoopback])
  69. if err != nil {
  70. return false, err
  71. }
  72. return on == 1, nil
  73. }
  74. // SetMulticastLoopback sets whether transmitted multicast packets
  75. // should be copied and send back to the originator.
  76. func (c *dgramOpt) SetMulticastLoopback(on bool) error {
  77. if !c.ok() {
  78. return syscall.EINVAL
  79. }
  80. fd, err := c.sysfd()
  81. if err != nil {
  82. return err
  83. }
  84. return setInt(fd, &sockOpts[ssoMulticastLoopback], boolint(on))
  85. }
  86. // JoinGroup joins the group address group on the interface ifi.
  87. // By default all sources that can cast data to group are accepted.
  88. // It's possible to mute and unmute data transmission from a specific
  89. // source by using ExcludeSourceSpecificGroup and
  90. // IncludeSourceSpecificGroup.
  91. // JoinGroup uses the system assigned multicast interface when ifi is
  92. // nil, although this is not recommended because the assignment
  93. // depends on platforms and sometimes it might require routing
  94. // configuration.
  95. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  96. if !c.ok() {
  97. return syscall.EINVAL
  98. }
  99. fd, err := c.sysfd()
  100. if err != nil {
  101. return err
  102. }
  103. grp := netAddrToIP16(group)
  104. if grp == nil {
  105. return errMissingAddress
  106. }
  107. return setGroup(fd, &sockOpts[ssoJoinGroup], ifi, grp)
  108. }
  109. // LeaveGroup leaves the group address group on the interface ifi
  110. // regardless of whether the group is any-source group or
  111. // source-specific group.
  112. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  113. if !c.ok() {
  114. return syscall.EINVAL
  115. }
  116. fd, err := c.sysfd()
  117. if err != nil {
  118. return err
  119. }
  120. grp := netAddrToIP16(group)
  121. if grp == nil {
  122. return errMissingAddress
  123. }
  124. return setGroup(fd, &sockOpts[ssoLeaveGroup], ifi, grp)
  125. }
  126. // JoinSourceSpecificGroup joins the source-specific group comprising
  127. // group and source on the interface ifi.
  128. // JoinSourceSpecificGroup uses the system assigned multicast
  129. // interface when ifi is nil, although this is not recommended because
  130. // the assignment depends on platforms and sometimes it might require
  131. // routing configuration.
  132. func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  133. if !c.ok() {
  134. return syscall.EINVAL
  135. }
  136. fd, err := c.sysfd()
  137. if err != nil {
  138. return err
  139. }
  140. grp := netAddrToIP16(group)
  141. if grp == nil {
  142. return errMissingAddress
  143. }
  144. src := netAddrToIP16(source)
  145. if src == nil {
  146. return errMissingAddress
  147. }
  148. return setSourceGroup(fd, &sockOpts[ssoJoinSourceGroup], ifi, grp, src)
  149. }
  150. // LeaveSourceSpecificGroup leaves the source-specific group on the
  151. // interface ifi.
  152. func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  153. if !c.ok() {
  154. return syscall.EINVAL
  155. }
  156. fd, err := c.sysfd()
  157. if err != nil {
  158. return err
  159. }
  160. grp := netAddrToIP16(group)
  161. if grp == nil {
  162. return errMissingAddress
  163. }
  164. src := netAddrToIP16(source)
  165. if src == nil {
  166. return errMissingAddress
  167. }
  168. return setSourceGroup(fd, &sockOpts[ssoLeaveSourceGroup], ifi, grp, src)
  169. }
  170. // ExcludeSourceSpecificGroup excludes the source-specific group from
  171. // the already joined any-source groups by JoinGroup on the interface
  172. // ifi.
  173. func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  174. if !c.ok() {
  175. return syscall.EINVAL
  176. }
  177. fd, err := c.sysfd()
  178. if err != nil {
  179. return err
  180. }
  181. grp := netAddrToIP16(group)
  182. if grp == nil {
  183. return errMissingAddress
  184. }
  185. src := netAddrToIP16(source)
  186. if src == nil {
  187. return errMissingAddress
  188. }
  189. return setSourceGroup(fd, &sockOpts[ssoBlockSourceGroup], ifi, grp, src)
  190. }
  191. // IncludeSourceSpecificGroup includes the excluded source-specific
  192. // group by ExcludeSourceSpecificGroup again on the interface ifi.
  193. func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  194. if !c.ok() {
  195. return syscall.EINVAL
  196. }
  197. fd, err := c.sysfd()
  198. if err != nil {
  199. return err
  200. }
  201. grp := netAddrToIP16(group)
  202. if grp == nil {
  203. return errMissingAddress
  204. }
  205. src := netAddrToIP16(source)
  206. if src == nil {
  207. return errMissingAddress
  208. }
  209. return setSourceGroup(fd, &sockOpts[ssoUnblockSourceGroup], ifi, grp, src)
  210. }
  211. // Checksum reports whether the kernel will compute, store or verify a
  212. // checksum for both incoming and outgoing packets. If on is true, it
  213. // returns an offset in bytes into the data of where the checksum
  214. // field is located.
  215. func (c *dgramOpt) Checksum() (on bool, offset int, err error) {
  216. if !c.ok() {
  217. return false, 0, syscall.EINVAL
  218. }
  219. fd, err := c.sysfd()
  220. if err != nil {
  221. return false, 0, err
  222. }
  223. offset, err = getInt(fd, &sockOpts[ssoChecksum])
  224. if err != nil {
  225. return false, 0, err
  226. }
  227. if offset < 0 {
  228. return false, 0, nil
  229. }
  230. return true, offset, nil
  231. }
  232. // SetChecksum enables the kernel checksum processing. If on is ture,
  233. // the offset should be an offset in bytes into the data of where the
  234. // checksum field is located.
  235. func (c *dgramOpt) SetChecksum(on bool, offset int) error {
  236. if !c.ok() {
  237. return syscall.EINVAL
  238. }
  239. fd, err := c.sysfd()
  240. if err != nil {
  241. return err
  242. }
  243. if !on {
  244. offset = -1
  245. }
  246. return setInt(fd, &sockOpts[ssoChecksum], offset)
  247. }
  248. // ICMPFilter returns an ICMP filter.
  249. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  250. if !c.ok() {
  251. return nil, syscall.EINVAL
  252. }
  253. fd, err := c.sysfd()
  254. if err != nil {
  255. return nil, err
  256. }
  257. return getICMPFilter(fd, &sockOpts[ssoICMPFilter])
  258. }
  259. // SetICMPFilter deploys the ICMP filter.
  260. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  261. if !c.ok() {
  262. return syscall.EINVAL
  263. }
  264. fd, err := c.sysfd()
  265. if err != nil {
  266. return err
  267. }
  268. return setICMPFilter(fd, &sockOpts[ssoICMPFilter], f)
  269. }