dgramopt.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. package ipv4
  5. import (
  6. "net"
  7. "syscall"
  8. "golang.org/x/net/bpf"
  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. so, ok := sockOpts[ssoMulticastTTL]
  17. if !ok {
  18. return 0, errOpNoSupport
  19. }
  20. return so.GetInt(c.Conn)
  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. so, ok := sockOpts[ssoMulticastTTL]
  29. if !ok {
  30. return errOpNoSupport
  31. }
  32. return so.SetInt(c.Conn, 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. so, ok := sockOpts[ssoMulticastInterface]
  41. if !ok {
  42. return nil, errOpNoSupport
  43. }
  44. return so.getMulticastInterface(c.Conn)
  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. so, ok := sockOpts[ssoMulticastInterface]
  53. if !ok {
  54. return errOpNoSupport
  55. }
  56. return so.setMulticastInterface(c.Conn, 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. so, ok := sockOpts[ssoMulticastLoopback]
  65. if !ok {
  66. return false, errOpNoSupport
  67. }
  68. on, err := so.GetInt(c.Conn)
  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. so, ok := sockOpts[ssoMulticastLoopback]
  81. if !ok {
  82. return errOpNoSupport
  83. }
  84. return so.SetInt(c.Conn, 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. so, ok := sockOpts[ssoJoinGroup]
  100. if !ok {
  101. return errOpNoSupport
  102. }
  103. grp := netAddrToIP4(group)
  104. if grp == nil {
  105. return errMissingAddress
  106. }
  107. return so.setGroup(c.Conn, 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. so, ok := sockOpts[ssoLeaveGroup]
  117. if !ok {
  118. return errOpNoSupport
  119. }
  120. grp := netAddrToIP4(group)
  121. if grp == nil {
  122. return errMissingAddress
  123. }
  124. return so.setGroup(c.Conn, 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. so, ok := sockOpts[ssoJoinSourceGroup]
  137. if !ok {
  138. return errOpNoSupport
  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 so.setSourceGroup(c.Conn, 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. so, ok := sockOpts[ssoLeaveSourceGroup]
  157. if !ok {
  158. return errOpNoSupport
  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 so.setSourceGroup(c.Conn, 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. so, ok := sockOpts[ssoBlockSourceGroup]
  178. if !ok {
  179. return errOpNoSupport
  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 so.setSourceGroup(c.Conn, 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. so, ok := sockOpts[ssoUnblockSourceGroup]
  198. if !ok {
  199. return errOpNoSupport
  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 so.setSourceGroup(c.Conn, 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. so, ok := sockOpts[ssoICMPFilter]
  218. if !ok {
  219. return nil, errOpNoSupport
  220. }
  221. return so.getICMPFilter(c.Conn)
  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. so, ok := sockOpts[ssoICMPFilter]
  230. if !ok {
  231. return errOpNoSupport
  232. }
  233. return so.setICMPFilter(c.Conn, f)
  234. }
  235. // SetBPF attaches a BPF program to the connection.
  236. //
  237. // Only supported on Linux.
  238. func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
  239. if !c.ok() {
  240. return syscall.EINVAL
  241. }
  242. so, ok := sockOpts[ssoAttachFilter]
  243. if !ok {
  244. return errOpNoSupport
  245. }
  246. return so.setBPF(c.Conn, filter)
  247. }