dgramopt_posix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // +build darwin freebsd linux netbsd openbsd windows
  5. package ipv4
  6. import (
  7. "net"
  8. "syscall"
  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. fd, err := c.sysfd()
  17. if err != nil {
  18. return 0, err
  19. }
  20. return ipv4MulticastTTL(fd)
  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. fd, err := c.sysfd()
  29. if err != nil {
  30. return err
  31. }
  32. return setIPv4MulticastTTL(fd, 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. fd, err := c.sysfd()
  41. if err != nil {
  42. return nil, err
  43. }
  44. return ipv4MulticastInterface(fd)
  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. fd, err := c.sysfd()
  53. if err != nil {
  54. return err
  55. }
  56. return setIPv4MulticastInterface(fd, 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. fd, err := c.sysfd()
  65. if err != nil {
  66. return false, err
  67. }
  68. return ipv4MulticastLoopback(fd)
  69. }
  70. // SetMulticastLoopback sets whether transmitted multicast packets
  71. // should be copied and send back to the originator.
  72. func (c *dgramOpt) SetMulticastLoopback(on bool) error {
  73. if !c.ok() {
  74. return syscall.EINVAL
  75. }
  76. fd, err := c.sysfd()
  77. if err != nil {
  78. return err
  79. }
  80. return setIPv4MulticastLoopback(fd, on)
  81. }
  82. // JoinGroup joins the group address group on the interface ifi.
  83. // It uses the system assigned multicast interface when ifi is nil,
  84. // although this is not recommended because the assignment depends on
  85. // platforms and sometimes it might require routing configuration.
  86. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  87. if !c.ok() {
  88. return syscall.EINVAL
  89. }
  90. fd, err := c.sysfd()
  91. if err != nil {
  92. return err
  93. }
  94. grp := netAddrToIP4(group)
  95. if grp == nil {
  96. return errMissingAddress
  97. }
  98. return joinIPv4Group(fd, ifi, grp)
  99. }
  100. // LeaveGroup leaves the group address group on the interface ifi.
  101. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  102. if !c.ok() {
  103. return syscall.EINVAL
  104. }
  105. fd, err := c.sysfd()
  106. if err != nil {
  107. return err
  108. }
  109. grp := netAddrToIP4(group)
  110. if grp == nil {
  111. return errMissingAddress
  112. }
  113. return leaveIPv4Group(fd, ifi, grp)
  114. }