dgramopt_posix.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // +build darwin dragonfly freebsd linux netbsd openbsd windows
  5. package ipv4
  6. import (
  7. "net"
  8. "syscall"
  9. )
  10. // MulticastTTL returns the time-to-live field value for outgoing
  11. // multicast packets.
  12. func (c *dgramOpt) MulticastTTL() (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[ssoMulticastTTL])
  21. }
  22. // SetMulticastTTL sets the time-to-live field value for future
  23. // outgoing multicast packets.
  24. func (c *dgramOpt) SetMulticastTTL(ttl 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[ssoMulticastTTL], ttl)
  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 := netAddrToIP4(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 := netAddrToIP4(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 := netAddrToIP4(group)
  141. if grp == nil {
  142. return errMissingAddress
  143. }
  144. src := netAddrToIP4(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 := netAddrToIP4(group)
  161. if grp == nil {
  162. return errMissingAddress
  163. }
  164. src := netAddrToIP4(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 := netAddrToIP4(group)
  182. if grp == nil {
  183. return errMissingAddress
  184. }
  185. src := netAddrToIP4(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 := netAddrToIP4(group)
  202. if grp == nil {
  203. return errMissingAddress
  204. }
  205. src := netAddrToIP4(source)
  206. if src == nil {
  207. return errMissingAddress
  208. }
  209. return setSourceGroup(fd, &sockOpts[ssoUnblockSourceGroup], ifi, grp, src)
  210. }
  211. // ICMPFilter returns an ICMP filter.
  212. // Currently only Linux supports this.
  213. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  214. if !c.ok() {
  215. return nil, syscall.EINVAL
  216. }
  217. fd, err := c.sysfd()
  218. if err != nil {
  219. return nil, err
  220. }
  221. return getICMPFilter(fd, &sockOpts[ssoICMPFilter])
  222. }
  223. // SetICMPFilter deploys the ICMP filter.
  224. // Currently only Linux supports this.
  225. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  226. if !c.ok() {
  227. return syscall.EINVAL
  228. }
  229. fd, err := c.sysfd()
  230. if err != nil {
  231. return err
  232. }
  233. return setICMPFilter(fd, &sockOpts[ssoICMPFilter], f)
  234. }