dgramopt.go 7.5 KB

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