2014-07-08 22:20:10 +10:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Extensions of the original work are copyright (c) 2011 Miek Gieben
|
|
|
|
|
2014-07-08 01:16:42 +10:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-07-08 18:42:02 +10:00
|
|
|
// Linux implementation for preparing the socket for sessions
|
|
|
|
// Based on http://stackoverflow.com/questions/3062205/setting-the-source-ip-for-a-udp-socket
|
|
|
|
// and http://blog.powerdns.com/2012/10/08/on-binding-datagram-udp-sockets-to-the-any-addresses/
|
2014-07-08 22:18:33 +10:00
|
|
|
func udpPatchSocketTypes(conn *net.UDPConn, ipv4, ipv6 bool) (err error) {
|
2014-07-08 01:16:42 +10:00
|
|
|
file, err := conn.File()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-08 22:18:33 +10:00
|
|
|
if ipv4 {
|
|
|
|
// socket supports IPv4
|
|
|
|
|
|
|
|
err = syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-08 07:19:51 +10:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:18:33 +10:00
|
|
|
if ipv6 {
|
|
|
|
// socket supports IPv6
|
|
|
|
|
|
|
|
err = syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_RECVPKTINFO, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-07-08 01:16:42 +10:00
|
|
|
|
2014-07-08 22:18:33 +10:00
|
|
|
return nil
|
2014-07-08 01:16:42 +10:00
|
|
|
}
|