2014-07-10 05:07:23 +10:00
|
|
|
// Copyright 2011 Miek Gieben. All rights reserved.
|
2014-07-08 22:20:10 +10:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-08 01:16:42 +10:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
2014-07-10 05:07:23 +10:00
|
|
|
// See:
|
|
|
|
// * 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/
|
|
|
|
//
|
|
|
|
// Why do we need this: When listening on 0.0.0.0 with UDP so kernel decides what is the outoging
|
|
|
|
// interface, this might not always be the correct one. This code will make sure the egress
|
|
|
|
// packet's interface matched the ingress' one.
|
|
|
|
|
2014-07-08 01:16:42 +10:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-07-10 05:07:23 +10:00
|
|
|
// SetUDPSocketOptions4 prepares the v4 socket for sessions.
|
|
|
|
func SetUDPSocketOptions4(conn *net.UDPConn) error {
|
|
|
|
// We got the .File() in NewUDPConn, this this will work
|
|
|
|
file, _ := conn.File()
|
|
|
|
err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1)
|
2014-07-08 01:16:42 +10:00
|
|
|
if err != nil {
|
2014-07-10 05:07:23 +10:00
|
|
|
return err
|
2014-07-08 01:16:42 +10:00
|
|
|
}
|
2014-07-10 05:07:23 +10:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-08 01:16:42 +10:00
|
|
|
|
2014-07-10 05:07:23 +10:00
|
|
|
// SetUDPSocketOptions6 prepares the v6 socket for sessions.
|
|
|
|
func SetUDPSocketOptions6(conn *net.UDPConn) error {
|
|
|
|
// We got the .File() in NewUDPConn, this this will work
|
|
|
|
file, _ := conn.File()
|
|
|
|
err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_RECVPKTINFO, 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-08 22:18:33 +10:00
|
|
|
}
|
|
|
|
return nil
|
2014-07-08 01:16:42 +10:00
|
|
|
}
|