dgramopt_posix.go 6.8 KB

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