OLD | NEW |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // +build darwin dragonfly freebsd linux netbsd openbsd windows | 5 // +build darwin dragonfly freebsd linux netbsd openbsd windows |
6 | 6 |
7 package ipv4 | 7 package ipv4 |
8 | 8 |
9 import ( | 9 import ( |
10 "net" | 10 "net" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 if !c.ok() { | 86 if !c.ok() { |
87 return syscall.EINVAL | 87 return syscall.EINVAL |
88 } | 88 } |
89 fd, err := c.sysfd() | 89 fd, err := c.sysfd() |
90 if err != nil { | 90 if err != nil { |
91 return err | 91 return err |
92 } | 92 } |
93 return setInt(fd, &sockOpts[ssoMulticastLoopback], boolint(on)) | 93 return setInt(fd, &sockOpts[ssoMulticastLoopback], boolint(on)) |
94 } | 94 } |
95 | 95 |
96 // JoinGroup joins the group address group on the interface ifi. | 96 // JoinGroup joins the group address group on the interface ifi. By |
| 97 // default all sources that can cast data to group are accepted. It's |
| 98 // possible to mute and unmute data transmission from a specific |
| 99 // source by using ExcludeSourceSpecificGroup and |
| 100 // IncludeSourceSpecificGroup. |
97 // It uses the system assigned multicast interface when ifi is nil, | 101 // It uses the system assigned multicast interface when ifi is nil, |
98 // although this is not recommended because the assignment depends on | 102 // although this is not recommended because the assignment depends on |
99 // platforms and sometimes it might require routing configuration. | 103 // platforms and sometimes it might require routing configuration. |
100 func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error { | 104 func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error { |
101 if !c.ok() { | 105 if !c.ok() { |
102 return syscall.EINVAL | 106 return syscall.EINVAL |
103 } | 107 } |
104 fd, err := c.sysfd() | 108 fd, err := c.sysfd() |
105 if err != nil { | 109 if err != nil { |
106 return err | 110 return err |
107 } | 111 } |
108 grp := netAddrToIP4(group) | 112 grp := netAddrToIP4(group) |
109 if grp == nil { | 113 if grp == nil { |
110 return errMissingAddress | 114 return errMissingAddress |
111 } | 115 } |
112 return setGroup(fd, &sockOpts[ssoJoinGroup], ifi, grp) | 116 return setGroup(fd, &sockOpts[ssoJoinGroup], ifi, grp) |
113 } | 117 } |
114 | 118 |
115 // LeaveGroup leaves the group address group on the interface ifi. | 119 // LeaveGroup leaves the group address group on the interface ifi. |
| 120 // It's allowed to leave the group which is formed by |
| 121 // JoinSourceSpecificGroup for convenience. |
116 func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error { | 122 func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error { |
117 if !c.ok() { | 123 if !c.ok() { |
118 return syscall.EINVAL | 124 return syscall.EINVAL |
119 } | 125 } |
120 fd, err := c.sysfd() | 126 fd, err := c.sysfd() |
121 if err != nil { | 127 if err != nil { |
122 return err | 128 return err |
123 } | 129 } |
124 grp := netAddrToIP4(group) | 130 grp := netAddrToIP4(group) |
125 if grp == nil { | 131 if grp == nil { |
126 return errMissingAddress | 132 return errMissingAddress |
127 } | 133 } |
128 return setGroup(fd, &sockOpts[ssoLeaveGroup], ifi, grp) | 134 return setGroup(fd, &sockOpts[ssoLeaveGroup], ifi, grp) |
129 } | 135 } |
| 136 |
| 137 // JoinSourceSpecificGroup joins the source-specific group consisting |
| 138 // group and source on the interface ifi. It uses the system assigned |
| 139 // multicast interface when ifi is nil, although this is not |
| 140 // recommended because the assignment depends on platforms and |
| 141 // sometimes it might require routing configuration. |
| 142 func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net
.Addr) error { |
| 143 if !c.ok() { |
| 144 return syscall.EINVAL |
| 145 } |
| 146 fd, err := c.sysfd() |
| 147 if err != nil { |
| 148 return err |
| 149 } |
| 150 grp := netAddrToIP4(group) |
| 151 if grp == nil { |
| 152 return errMissingAddress |
| 153 } |
| 154 src := netAddrToIP4(source) |
| 155 if src == nil { |
| 156 return errMissingAddress |
| 157 } |
| 158 return setSourceGroup(fd, &sockOpts[ssoJoinSourceGroup], ifi, grp, src) |
| 159 } |
| 160 |
| 161 // LeaveSourceSpecificGroup leaves the source-specific group on the |
| 162 // interface ifi. |
| 163 func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source ne
t.Addr) error { |
| 164 if !c.ok() { |
| 165 return syscall.EINVAL |
| 166 } |
| 167 fd, err := c.sysfd() |
| 168 if err != nil { |
| 169 return err |
| 170 } |
| 171 grp := netAddrToIP4(group) |
| 172 if grp == nil { |
| 173 return errMissingAddress |
| 174 } |
| 175 src := netAddrToIP4(source) |
| 176 if src == nil { |
| 177 return errMissingAddress |
| 178 } |
| 179 return setSourceGroup(fd, &sockOpts[ssoLeaveSourceGroup], ifi, grp, src) |
| 180 } |
| 181 |
| 182 // ExcludeSourceSpecificGroup excludes the source-specific group from |
| 183 // the already joined groups by either JoinGroup or |
| 184 // JoinSourceSpecificGroup on the interface ifi. |
| 185 func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source
net.Addr) error { |
| 186 if !c.ok() { |
| 187 return syscall.EINVAL |
| 188 } |
| 189 fd, err := c.sysfd() |
| 190 if err != nil { |
| 191 return err |
| 192 } |
| 193 grp := netAddrToIP4(group) |
| 194 if grp == nil { |
| 195 return errMissingAddress |
| 196 } |
| 197 src := netAddrToIP4(source) |
| 198 if src == nil { |
| 199 return errMissingAddress |
| 200 } |
| 201 return setSourceGroup(fd, &sockOpts[ssoBlockSourceGroup], ifi, grp, src) |
| 202 } |
| 203 |
| 204 // IncludeSourceSpecificGroup includes the excluded source-specific |
| 205 // group by ExcludeSourceSpecificGroup again on the interface ifi. |
| 206 func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source
net.Addr) error { |
| 207 if !c.ok() { |
| 208 return syscall.EINVAL |
| 209 } |
| 210 fd, err := c.sysfd() |
| 211 if err != nil { |
| 212 return err |
| 213 } |
| 214 grp := netAddrToIP4(group) |
| 215 if grp == nil { |
| 216 return errMissingAddress |
| 217 } |
| 218 src := netAddrToIP4(source) |
| 219 if src == nil { |
| 220 return errMissingAddress |
| 221 } |
| 222 return setSourceGroup(fd, &sockOpts[ssoUnblockSourceGroup], ifi, grp, sr
c) |
| 223 } |
OLD | NEW |