Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 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 | |
| 5 | // +build darwin freebsd linux netbsd openbsd windows |
| 6 | |
| 7 | package ipv4 |
| 8 | |
| 9 | import ( |
| 10 | "net" |
| 11 | "syscall" |
| 12 | ) |
| 13 | |
| 14 | // MulticastTTL returns the time-to-live field value for outgoing |
| 15 | // multicast packets. |
| 16 | func (c *dgramOpt) MulticastTTL() (int, error) { |
| 17 | if !c.ok() { |
| 18 | return 0, syscall.EINVAL |
| 19 | } |
| 20 | fd, err := c.sysfd() |
| 21 | if err != nil { |
| 22 | return 0, err |
| 23 | } |
| 24 | return ipv4MulticastTTL(fd) |
| 25 | } |
| 26 | |
| 27 | // SetMulticastTTL sets the time-to-live field value for future |
| 28 | // outgoing multicast packets. |
| 29 | func (c *dgramOpt) SetMulticastTTL(ttl int) error { |
| 30 | if !c.ok() { |
| 31 | return syscall.EINVAL |
| 32 | } |
| 33 | fd, err := c.sysfd() |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | return setIPv4MulticastTTL(fd, ttl) |
| 38 | } |
| 39 | |
| 40 | // MulticastInterface returns the default interface for multicast |
| 41 | // packet transmissions. |
| 42 | func (c *dgramOpt) MulticastInterface() (*net.Interface, error) { |
| 43 | if !c.ok() { |
| 44 | return nil, syscall.EINVAL |
| 45 | } |
| 46 | fd, err := c.sysfd() |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | return ipv4MulticastInterface(fd) |
| 51 | } |
| 52 | |
| 53 | // SetMulticastInterface sets the default interface for future |
| 54 | // multicast packet transmissions. |
| 55 | func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error { |
| 56 | if !c.ok() { |
| 57 | return syscall.EINVAL |
| 58 | } |
| 59 | fd, err := c.sysfd() |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | return setIPv4MulticastInterface(fd, ifi) |
| 64 | } |
| 65 | |
| 66 | // MulticastLoopback reports whether transmitted multicast packets |
| 67 | // should be copied and send back to the originator. |
| 68 | func (c *dgramOpt) MulticastLoopback() (bool, error) { |
| 69 | if !c.ok() { |
| 70 | return false, syscall.EINVAL |
| 71 | } |
| 72 | fd, err := c.sysfd() |
| 73 | if err != nil { |
| 74 | return false, err |
| 75 | } |
| 76 | return ipv4MulticastLoopback(fd) |
| 77 | } |
| 78 | |
| 79 | // SetMulticastLoopback sets whether transmitted multicast packets |
| 80 | // should be copied and send back to the originator. |
| 81 | func (c *dgramOpt) SetMulticastLoopback(on bool) error { |
| 82 | if !c.ok() { |
| 83 | return syscall.EINVAL |
| 84 | } |
| 85 | fd, err := c.sysfd() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | return setIPv4MulticastLoopback(fd, on) |
| 90 | } |
| 91 | |
| 92 | // JoinGroup joins the group address group on the interface ifi. |
| 93 | // It uses the system assigned multicast interface when ifi is nil, |
| 94 | // although this is not recommended because the assignment depends on |
| 95 | // platforms and sometimes it might require routing configuration. |
| 96 | func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error { |
| 97 | if !c.ok() { |
| 98 | return syscall.EINVAL |
| 99 | } |
| 100 | fd, err := c.sysfd() |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | grp := netAddrToIP4(group) |
| 105 | if grp == nil { |
| 106 | return errMissingAddress |
| 107 | } |
| 108 | return joinIPv4Group(fd, ifi, grp) |
| 109 | } |
| 110 | |
| 111 | // LeaveGroup leaves the group address group on the interface ifi. |
| 112 | func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error { |
| 113 | if !c.ok() { |
| 114 | return syscall.EINVAL |
| 115 | } |
| 116 | fd, err := c.sysfd() |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | grp := netAddrToIP4(group) |
| 121 | if grp == nil { |
| 122 | return errMissingAddress |
| 123 | } |
| 124 | return leaveIPv4Group(fd, ifi, grp) |
| 125 | } |