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. "golang.org/x/net/bpf"
  8. )
  9. // MulticastTTL returns the time-to-live field value for outgoing
  10. // multicast packets.
  11. func (c *dgramOpt) MulticastTTL() (int, error) {
  12. if !c.ok() {
  13. return 0, errInvalidConn
  14. }
  15. so, ok := sockOpts[ssoMulticastTTL]
  16. if !ok {
  17. return 0, errNotImplemented
  18. }
  19. return so.GetInt(c.Conn)
  20. }
  21. // SetMulticastTTL sets the time-to-live field value for future
  22. // outgoing multicast packets.
  23. func (c *dgramOpt) SetMulticastTTL(ttl int) error {
  24. if !c.ok() {
  25. return errInvalidConn
  26. }
  27. so, ok := sockOpts[ssoMulticastTTL]
  28. if !ok {
  29. return errNotImplemented
  30. }
  31. return so.SetInt(c.Conn, ttl)
  32. }
  33. // MulticastInterface returns the default interface for multicast
  34. // packet transmissions.
  35. func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
  36. if !c.ok() {
  37. return nil, errInvalidConn
  38. }
  39. so, ok := sockOpts[ssoMulticastInterface]
  40. if !ok {
  41. return nil, errNotImplemented
  42. }
  43. return so.getMulticastInterface(c.Conn)
  44. }
  45. // SetMulticastInterface sets the default interface for future
  46. // multicast packet transmissions.
  47. func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
  48. if !c.ok() {
  49. return errInvalidConn
  50. }
  51. so, ok := sockOpts[ssoMulticastInterface]
  52. if !ok {
  53. return errNotImplemented
  54. }
  55. return so.setMulticastInterface(c.Conn, ifi)
  56. }
  57. // MulticastLoopback reports whether transmitted multicast packets
  58. // should be copied and send back to the originator.
  59. func (c *dgramOpt) MulticastLoopback() (bool, error) {
  60. if !c.ok() {
  61. return false, errInvalidConn
  62. }
  63. so, ok := sockOpts[ssoMulticastLoopback]
  64. if !ok {
  65. return false, errNotImplemented
  66. }
  67. on, err := so.GetInt(c.Conn)
  68. if err != nil {
  69. return false, err
  70. }
  71. return on == 1, nil
  72. }
  73. // SetMulticastLoopback sets whether transmitted multicast packets
  74. // should be copied and send back to the originator.
  75. func (c *dgramOpt) SetMulticastLoopback(on bool) error {
  76. if !c.ok() {
  77. return errInvalidConn
  78. }
  79. so, ok := sockOpts[ssoMulticastLoopback]
  80. if !ok {
  81. return errNotImplemented
  82. }
  83. return so.SetInt(c.Conn, boolint(on))
  84. }
  85. // JoinGroup joins the group address group on the interface ifi.
  86. // By default all sources that can cast data to group are accepted.
  87. // It's possible to mute and unmute data transmission from a specific
  88. // source by using ExcludeSourceSpecificGroup and
  89. // IncludeSourceSpecificGroup.
  90. // JoinGroup uses the system assigned multicast interface when ifi is
  91. // nil, although this is not recommended because the assignment
  92. // depends on platforms and sometimes it might require routing
  93. // configuration.
  94. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  95. if !c.ok() {
  96. return errInvalidConn
  97. }
  98. so, ok := sockOpts[ssoJoinGroup]
  99. if !ok {
  100. return errNotImplemented
  101. }
  102. grp := netAddrToIP4(group)
  103. if grp == nil {
  104. return errMissingAddress
  105. }
  106. return so.setGroup(c.Conn, ifi, grp)
  107. }
  108. // LeaveGroup leaves the group address group on the interface ifi
  109. // regardless of whether the group is any-source group or
  110. // source-specific group.
  111. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  112. if !c.ok() {
  113. return errInvalidConn
  114. }
  115. so, ok := sockOpts[ssoLeaveGroup]
  116. if !ok {
  117. return errNotImplemented
  118. }
  119. grp := netAddrToIP4(group)
  120. if grp == nil {
  121. return errMissingAddress
  122. }
  123. return so.setGroup(c.Conn, ifi, grp)
  124. }
  125. // JoinSourceSpecificGroup joins the source-specific group comprising
  126. // group and source on the interface ifi.
  127. // JoinSourceSpecificGroup uses the system assigned multicast
  128. // interface when ifi is nil, although this is not recommended because
  129. // the assignment depends on platforms and sometimes it might require
  130. // routing configuration.
  131. func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  132. if !c.ok() {
  133. return errInvalidConn
  134. }
  135. so, ok := sockOpts[ssoJoinSourceGroup]
  136. if !ok {
  137. return errNotImplemented
  138. }
  139. grp := netAddrToIP4(group)
  140. if grp == nil {
  141. return errMissingAddress
  142. }
  143. src := netAddrToIP4(source)
  144. if src == nil {
  145. return errMissingAddress
  146. }
  147. return so.setSourceGroup(c.Conn, ifi, grp, src)
  148. }
  149. // LeaveSourceSpecificGroup leaves the source-specific group on the
  150. // interface ifi.
  151. func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  152. if !c.ok() {
  153. return errInvalidConn
  154. }
  155. so, ok := sockOpts[ssoLeaveSourceGroup]
  156. if !ok {
  157. return errNotImplemented
  158. }
  159. grp := netAddrToIP4(group)
  160. if grp == nil {
  161. return errMissingAddress
  162. }
  163. src := netAddrToIP4(source)
  164. if src == nil {
  165. return errMissingAddress
  166. }
  167. return so.setSourceGroup(c.Conn, ifi, grp, src)
  168. }
  169. // ExcludeSourceSpecificGroup excludes the source-specific group from
  170. // the already joined any-source groups by JoinGroup on the interface
  171. // ifi.
  172. func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  173. if !c.ok() {
  174. return errInvalidConn
  175. }
  176. so, ok := sockOpts[ssoBlockSourceGroup]
  177. if !ok {
  178. return errNotImplemented
  179. }
  180. grp := netAddrToIP4(group)
  181. if grp == nil {
  182. return errMissingAddress
  183. }
  184. src := netAddrToIP4(source)
  185. if src == nil {
  186. return errMissingAddress
  187. }
  188. return so.setSourceGroup(c.Conn, ifi, grp, src)
  189. }
  190. // IncludeSourceSpecificGroup includes the excluded source-specific
  191. // group by ExcludeSourceSpecificGroup again on the interface ifi.
  192. func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
  193. if !c.ok() {
  194. return errInvalidConn
  195. }
  196. so, ok := sockOpts[ssoUnblockSourceGroup]
  197. if !ok {
  198. return errNotImplemented
  199. }
  200. grp := netAddrToIP4(group)
  201. if grp == nil {
  202. return errMissingAddress
  203. }
  204. src := netAddrToIP4(source)
  205. if src == nil {
  206. return errMissingAddress
  207. }
  208. return so.setSourceGroup(c.Conn, ifi, grp, src)
  209. }
  210. // ICMPFilter returns an ICMP filter.
  211. // Currently only Linux supports this.
  212. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  213. if !c.ok() {
  214. return nil, errInvalidConn
  215. }
  216. so, ok := sockOpts[ssoICMPFilter]
  217. if !ok {
  218. return nil, errNotImplemented
  219. }
  220. return so.getICMPFilter(c.Conn)
  221. }
  222. // SetICMPFilter deploys the ICMP filter.
  223. // Currently only Linux supports this.
  224. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  225. if !c.ok() {
  226. return errInvalidConn
  227. }
  228. so, ok := sockOpts[ssoICMPFilter]
  229. if !ok {
  230. return errNotImplemented
  231. }
  232. return so.setICMPFilter(c.Conn, f)
  233. }
  234. // SetBPF attaches a BPF program to the connection.
  235. //
  236. // Only supported on Linux.
  237. func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
  238. if !c.ok() {
  239. return errInvalidConn
  240. }
  241. so, ok := sockOpts[ssoAttachFilter]
  242. if !ok {
  243. return errNotImplemented
  244. }
  245. return so.setBPF(c.Conn, filter)
  246. }