dgramopt_posix.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. By
  87. // default all sources that can cast data to group are accepted. It's
  88. // possible to mute and unmute data transmission from a specific
  89. // source by using ExcludeSourceSpecificGroup and
  90. // IncludeSourceSpecificGroup.
  91. // It uses the system assigned multicast interface when ifi is nil,
  92. // although this is not recommended because the assignment depends on
  93. // platforms and sometimes it might require routing configuration.
  94. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  95. if !c.ok() {
  96. return syscall.EINVAL
  97. }
  98. fd, err := c.sysfd()
  99. if err != nil {
  100. return err
  101. }
  102. grp := netAddrToIP4(group)
  103. if grp == nil {
  104. return errMissingAddress
  105. }
  106. return setGroup(fd, &sockOpts[ssoJoinGroup], ifi, grp)
  107. }
  108. // LeaveGroup leaves the group address group on the interface ifi.
  109. // It's allowed to leave the group which is formed by
  110. // JoinSourceSpecificGroup for convenience.
  111. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  112. if !c.ok() {
  113. return syscall.EINVAL
  114. }
  115. fd, err := c.sysfd()
  116. if err != nil {
  117. return err
  118. }
  119. grp := netAddrToIP4(group)
  120. if grp == nil {
  121. return errMissingAddress
  122. }
  123. return setGroup(fd, &sockOpts[ssoLeaveGroup], ifi, grp)
  124. }
  125. // JoinSourceSpecificGroup joins the source-specific group consisting
  126. // group and source on the interface ifi. It uses the system assigned
  127. // multicast interface when ifi is nil, although this is not
  128. // recommended because the assignment depends on platforms and
  129. // sometimes it might require routing configuration.
  130. func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  131. if !c.ok() {
  132. return syscall.EINVAL
  133. }
  134. fd, err := c.sysfd()
  135. if err != nil {
  136. return err
  137. }
  138. grp := netAddrToIP4(group)
  139. if grp == nil {
  140. return errMissingAddress
  141. }
  142. src := netAddrToIP4(source)
  143. if src == nil {
  144. return errMissingAddress
  145. }
  146. return setSourceGroup(fd, &sockOpts[ssoJoinSourceGroup], ifi, grp, src)
  147. }
  148. // LeaveSourceSpecificGroup leaves the source-specific group on the
  149. // interface ifi.
  150. func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  151. if !c.ok() {
  152. return syscall.EINVAL
  153. }
  154. fd, err := c.sysfd()
  155. if err != nil {
  156. return err
  157. }
  158. grp := netAddrToIP4(group)
  159. if grp == nil {
  160. return errMissingAddress
  161. }
  162. src := netAddrToIP4(source)
  163. if src == nil {
  164. return errMissingAddress
  165. }
  166. return setSourceGroup(fd, &sockOpts[ssoLeaveSourceGroup], ifi, grp, src)
  167. }
  168. // ExcludeSourceSpecificGroup excludes the source-specific group from
  169. // the already joined groups by either JoinGroup or
  170. // JoinSourceSpecificGroup on the interface ifi.
  171. func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  172. if !c.ok() {
  173. return syscall.EINVAL
  174. }
  175. fd, err := c.sysfd()
  176. if err != nil {
  177. return err
  178. }
  179. grp := netAddrToIP4(group)
  180. if grp == nil {
  181. return errMissingAddress
  182. }
  183. src := netAddrToIP4(source)
  184. if src == nil {
  185. return errMissingAddress
  186. }
  187. return setSourceGroup(fd, &sockOpts[ssoBlockSourceGroup], ifi, grp, src)
  188. }
  189. // IncludeSourceSpecificGroup includes the excluded source-specific
  190. // group by ExcludeSourceSpecificGroup again on the interface ifi.
  191. func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  192. if !c.ok() {
  193. return syscall.EINVAL
  194. }
  195. fd, err := c.sysfd()
  196. if err != nil {
  197. return err
  198. }
  199. grp := netAddrToIP4(group)
  200. if grp == nil {
  201. return errMissingAddress
  202. }
  203. src := netAddrToIP4(source)
  204. if src == nil {
  205. return errMissingAddress
  206. }
  207. return setSourceGroup(fd, &sockOpts[ssoUnblockSourceGroup], ifi, grp, src)
  208. }