Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

302 linhas
7.5 KiB

  1. // Copyright 2013 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 ipv6
  5. import (
  6. "net"
  7. "golang.org/x/net/bpf"
  8. )
  9. // MulticastHopLimit returns the hop limit field value for outgoing
  10. // multicast packets.
  11. func (c *dgramOpt) MulticastHopLimit() (int, error) {
  12. if !c.ok() {
  13. return 0, errInvalidConn
  14. }
  15. so, ok := sockOpts[ssoMulticastHopLimit]
  16. if !ok {
  17. return 0, errNotImplemented
  18. }
  19. return so.GetInt(c.Conn)
  20. }
  21. // SetMulticastHopLimit sets the hop limit field value for future
  22. // outgoing multicast packets.
  23. func (c *dgramOpt) SetMulticastHopLimit(hoplim int) error {
  24. if !c.ok() {
  25. return errInvalidConn
  26. }
  27. so, ok := sockOpts[ssoMulticastHopLimit]
  28. if !ok {
  29. return errNotImplemented
  30. }
  31. return so.SetInt(c.Conn, hoplim)
  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 := netAddrToIP16(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 := netAddrToIP16(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 := netAddrToIP16(group)
  140. if grp == nil {
  141. return errMissingAddress
  142. }
  143. src := netAddrToIP16(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 := netAddrToIP16(group)
  160. if grp == nil {
  161. return errMissingAddress
  162. }
  163. src := netAddrToIP16(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 := netAddrToIP16(group)
  181. if grp == nil {
  182. return errMissingAddress
  183. }
  184. src := netAddrToIP16(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 := netAddrToIP16(group)
  201. if grp == nil {
  202. return errMissingAddress
  203. }
  204. src := netAddrToIP16(source)
  205. if src == nil {
  206. return errMissingAddress
  207. }
  208. return so.setSourceGroup(c.Conn, ifi, grp, src)
  209. }
  210. // Checksum reports whether the kernel will compute, store or verify a
  211. // checksum for both incoming and outgoing packets. If on is true, it
  212. // returns an offset in bytes into the data of where the checksum
  213. // field is located.
  214. func (c *dgramOpt) Checksum() (on bool, offset int, err error) {
  215. if !c.ok() {
  216. return false, 0, errInvalidConn
  217. }
  218. so, ok := sockOpts[ssoChecksum]
  219. if !ok {
  220. return false, 0, errNotImplemented
  221. }
  222. offset, err = so.GetInt(c.Conn)
  223. if err != nil {
  224. return false, 0, err
  225. }
  226. if offset < 0 {
  227. return false, 0, nil
  228. }
  229. return true, offset, nil
  230. }
  231. // SetChecksum enables the kernel checksum processing. If on is ture,
  232. // the offset should be an offset in bytes into the data of where the
  233. // checksum field is located.
  234. func (c *dgramOpt) SetChecksum(on bool, offset int) error {
  235. if !c.ok() {
  236. return errInvalidConn
  237. }
  238. so, ok := sockOpts[ssoChecksum]
  239. if !ok {
  240. return errNotImplemented
  241. }
  242. if !on {
  243. offset = -1
  244. }
  245. return so.SetInt(c.Conn, offset)
  246. }
  247. // ICMPFilter returns an ICMP filter.
  248. func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
  249. if !c.ok() {
  250. return nil, errInvalidConn
  251. }
  252. so, ok := sockOpts[ssoICMPFilter]
  253. if !ok {
  254. return nil, errNotImplemented
  255. }
  256. return so.getICMPFilter(c.Conn)
  257. }
  258. // SetICMPFilter deploys the ICMP filter.
  259. func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
  260. if !c.ok() {
  261. return errInvalidConn
  262. }
  263. so, ok := sockOpts[ssoICMPFilter]
  264. if !ok {
  265. return errNotImplemented
  266. }
  267. return so.setICMPFilter(c.Conn, f)
  268. }
  269. // SetBPF attaches a BPF program to the connection.
  270. //
  271. // Only supported on Linux.
  272. func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
  273. if !c.ok() {
  274. return errInvalidConn
  275. }
  276. so, ok := sockOpts[ssoAttachFilter]
  277. if !ok {
  278. return errNotImplemented
  279. }
  280. return so.setBPF(c.Conn, filter)
  281. }