-
Notifications
You must be signed in to change notification settings - Fork 7
/
ipaux.go
58 lines (52 loc) · 1.19 KB
/
ipaux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mdns
// Helper routines for manipulating ip connections.
import (
"net"
"os"
"syscall"
)
func boolint(b bool) int {
if b {
return 1
}
return 0
}
// SetMulticastTTL sets the TTL on packets from this connection.
func SetMulticastTTL(conn *net.UDPConn, ipversion int, v int) error {
var proto, opt int
switch ipversion {
default:
proto = syscall.IPPROTO_IP
opt = syscall.IP_MULTICAST_TTL
case 6:
proto = syscall.IPPROTO_IPV6
opt = syscall.IPV6_MULTICAST_HOPS
}
if file, err := conn.File(); err == nil {
fd := int(file.Fd())
err := syscall.SetsockoptInt(fd, proto, opt, v)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
file.Close()
} else {
return err
}
return nil
}
// SetMulticastLoopback turns on or off multicast loopbacks on the interface the connection is on.
func SetMulticastLoopback(conn *net.UDPConn, ipversion int, v bool) error {
file, err := conn.File()
if err != nil {
return err
}
fd := int(file.Fd())
switch ipversion {
default:
return setIPv4MulticastLoopback(fd, v)
case 6:
return setIPv6MulticastLoopback(fd, v)
}
}