You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

265 lines
6.6 KiB

  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. }