dgramopt_posix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 dragonfly 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 getInt(fd, &sockOpts[ssoMulticastTTL])
  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 setInt(fd, &sockOpts[ssoMulticastTTL], 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 getInterface(fd, &sockOpts[ssoMulticastInterface])
  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 setInterface(fd, &sockOpts[ssoMulticastInterface], 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. on, err := getInt(fd, &sockOpts[ssoMulticastLoopback])
  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. fd, err := c.sysfd()
  81. if err != nil {
  82. return err
  83. }
  84. return setInt(fd, &sockOpts[ssoMulticastLoopback], boolint(on))
  85. }
  86. // JoinGroup joins the group address group on the interface ifi.
  87. // It uses the system assigned multicast interface when ifi is nil,
  88. // although this is not recommended because the assignment depends on
  89. // platforms and sometimes it might require routing configuration.
  90. func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
  91. if !c.ok() {
  92. return syscall.EINVAL
  93. }
  94. fd, err := c.sysfd()
  95. if err != nil {
  96. return err
  97. }
  98. grp := netAddrToIP4(group)
  99. if grp == nil {
  100. return errMissingAddress
  101. }
  102. return setGroup(fd, &sockOpts[ssoJoinGroup], ifi, grp)
  103. }
  104. // LeaveGroup leaves the group address group on the interface ifi.
  105. func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
  106. if !c.ok() {
  107. return syscall.EINVAL
  108. }
  109. fd, err := c.sysfd()
  110. if err != nil {
  111. return err
  112. }
  113. grp := netAddrToIP4(group)
  114. if grp == nil {
  115. return errMissingAddress
  116. }
  117. return setGroup(fd, &sockOpts[ssoLeaveGroup], ifi, grp)
  118. }