2018-09-29 09:25:59 +00:00
|
|
|
// +build linux,!appengine
|
|
|
|
|
2017-12-04 09:59:24 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
2018-04-06 13:51:59 +00:00
|
|
|
"runtime"
|
2017-12-04 09:59:24 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetUDPSocketOptions(t *testing.T) {
|
|
|
|
// returns an error if we cannot resolve that address
|
|
|
|
testFamily := func(n, addr string) error {
|
|
|
|
a, err := net.ResolveUDPAddr(n, addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c, err := net.ListenUDP(n, a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setUDPSocketOptions(c); err != nil {
|
|
|
|
t.Fatalf("failed to set socket options: %v", err)
|
|
|
|
}
|
|
|
|
ch := make(chan *SessionUDP)
|
|
|
|
go func() {
|
|
|
|
// Set some deadline so this goroutine doesn't hang forever
|
|
|
|
c.SetReadDeadline(time.Now().Add(time.Minute))
|
|
|
|
b := make([]byte, 1)
|
|
|
|
_, sess, err := ReadFromSessionUDP(c, b)
|
|
|
|
if err != nil {
|
Fix dominikh/go-tools nits (#758)
* Remove unused functions and consts
* Address gosimple nits
* Address staticcheck nits
This excludes several that were intentional or weren't actual errors.
* Reduce size of lex struct
This reduces the size of the lex struct by 8 bytes from:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
padding: 42-48 (size 6, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.torc uint16: 64-66 (size 2, align 2)
padding: 66-72 (size 6, align 0)
lex.comment string: 72-88 (size 16, align 8)
to:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
lex.torc uint16: 42-44 (size 2, align 2)
padding: 44-48 (size 4, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.comment string: 64-80 (size 16, align 8)
* Reduce size of response struct
This reduces the size of the response struct by 8 bytes from:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
padding: 25-32 (size 7, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigTimersOnly bool: 48-49 (size 1, align 1)
padding: 49-56 (size 7, align 0)
response.tsigRequestMAC string: 56-72 (size 16, align 8)
response.tsigSecret map[string]string: 72-80 (size 8, align 8)
response.udp *net.UDPConn: 80-88 (size 8, align 8)
response.tcp net.Conn: 88-104 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 104-112 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 112-128 (size 16, align 8)
response.wg *sync.WaitGroup: 128-136 (size 8, align 8)
to:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
response.tsigTimersOnly bool: 25-26 (size 1, align 1)
padding: 26-32 (size 6, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigRequestMAC string: 48-64 (size 16, align 8)
response.tsigSecret map[string]string: 64-72 (size 8, align 8)
response.udp *net.UDPConn: 72-80 (size 8, align 8)
response.tcp net.Conn: 80-96 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 96-104 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 104-120 (size 16, align 8)
response.wg *sync.WaitGroup: 120-128 (size 8, align 8)
2018-09-26 18:32:05 +00:00
|
|
|
t.Errorf("failed to read from conn: %v", err)
|
|
|
|
// fallthrough to chan send below
|
2017-12-04 09:59:24 +00:00
|
|
|
}
|
|
|
|
ch <- sess
|
|
|
|
}()
|
|
|
|
|
|
|
|
c2, err := net.Dial("udp", c.LocalAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to dial udp: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := c2.Write([]byte{1}); err != nil {
|
|
|
|
t.Fatalf("failed to write to conn: %v", err)
|
|
|
|
}
|
|
|
|
sess := <-ch
|
Fix dominikh/go-tools nits (#758)
* Remove unused functions and consts
* Address gosimple nits
* Address staticcheck nits
This excludes several that were intentional or weren't actual errors.
* Reduce size of lex struct
This reduces the size of the lex struct by 8 bytes from:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
padding: 42-48 (size 6, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.torc uint16: 64-66 (size 2, align 2)
padding: 66-72 (size 6, align 0)
lex.comment string: 72-88 (size 16, align 8)
to:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
lex.torc uint16: 42-44 (size 2, align 2)
padding: 44-48 (size 4, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.comment string: 64-80 (size 16, align 8)
* Reduce size of response struct
This reduces the size of the response struct by 8 bytes from:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
padding: 25-32 (size 7, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigTimersOnly bool: 48-49 (size 1, align 1)
padding: 49-56 (size 7, align 0)
response.tsigRequestMAC string: 56-72 (size 16, align 8)
response.tsigSecret map[string]string: 72-80 (size 8, align 8)
response.udp *net.UDPConn: 80-88 (size 8, align 8)
response.tcp net.Conn: 88-104 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 104-112 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 112-128 (size 16, align 8)
response.wg *sync.WaitGroup: 128-136 (size 8, align 8)
to:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
response.tsigTimersOnly bool: 25-26 (size 1, align 1)
padding: 26-32 (size 6, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigRequestMAC string: 48-64 (size 16, align 8)
response.tsigSecret map[string]string: 64-72 (size 8, align 8)
response.udp *net.UDPConn: 72-80 (size 8, align 8)
response.tcp net.Conn: 80-96 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 96-104 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 104-120 (size 16, align 8)
response.wg *sync.WaitGroup: 120-128 (size 8, align 8)
2018-09-26 18:32:05 +00:00
|
|
|
if sess == nil {
|
|
|
|
// t.Error was already called in the goroutine above.
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-04 09:59:24 +00:00
|
|
|
if len(sess.context) == 0 {
|
|
|
|
t.Fatalf("empty session context: %v", sess)
|
|
|
|
}
|
|
|
|
ip := parseDstFromOOB(sess.context)
|
|
|
|
if ip == nil {
|
|
|
|
t.Fatalf("failed to parse dst: %v", sess)
|
|
|
|
}
|
|
|
|
if !strings.Contains(c.LocalAddr().String(), ip.String()) {
|
|
|
|
t.Fatalf("dst was different than listen addr: %v != %v", ip.String(), c.LocalAddr().String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// we require that ipv4 be supported
|
|
|
|
if err := testFamily("udp4", "127.0.0.1:0"); err != nil {
|
|
|
|
t.Fatalf("failed to test socket options on IPv4: %v", err)
|
|
|
|
}
|
|
|
|
// IPv6 might not be supported so these will just log
|
|
|
|
if err := testFamily("udp6", "[::1]:0"); err != nil {
|
|
|
|
t.Logf("failed to test socket options on IPv6-only: %v", err)
|
|
|
|
}
|
|
|
|
if err := testFamily("udp", "[::1]:0"); err != nil {
|
|
|
|
t.Logf("failed to test socket options on IPv6/IPv4: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDstFromOOB(t *testing.T) {
|
2018-04-06 13:51:59 +00:00
|
|
|
if runtime.GOARCH != "amd64" {
|
|
|
|
// The cmsghdr struct differs in the width (32/64-bit) of
|
|
|
|
// lengths and the struct padding between architectures.
|
|
|
|
// The data below was only written with amd64 in mind, and
|
|
|
|
// thus the test must be skipped on other architectures.
|
|
|
|
t.Skip("skipping test on unsupported architecture")
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:59:24 +00:00
|
|
|
// dst is :ffff:100.100.100.100
|
|
|
|
oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0}
|
|
|
|
dst := parseDstFromOOB(oob)
|
|
|
|
dst4 := dst.To4()
|
|
|
|
if dst4 == nil {
|
|
|
|
t.Errorf("failed to parse IPv4 in IPv6: %v", dst)
|
|
|
|
} else if dst4.String() != "100.100.100.100" {
|
|
|
|
t.Errorf("unexpected IPv4: %v", dst4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dst is 2001:db8::1
|
|
|
|
oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
dst = parseDstFromOOB(oob)
|
|
|
|
dst6 := dst.To16()
|
|
|
|
if dst6 == nil {
|
|
|
|
t.Errorf("failed to parse IPv6: %v", dst)
|
|
|
|
} else if dst6.String() != "2001:db8::1" {
|
|
|
|
t.Errorf("unexpected IPv6: %v", dst4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dst is 100.100.100.100 but was received on 10.10.10.10
|
|
|
|
oob = []byte{28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 10, 10, 10, 10, 100, 100, 100, 100, 0, 0, 0, 0}
|
|
|
|
dst = parseDstFromOOB(oob)
|
|
|
|
dst4 = dst.To4()
|
|
|
|
if dst4 == nil {
|
|
|
|
t.Errorf("failed to parse IPv4: %v", dst)
|
|
|
|
} else if dst4.String() != "100.100.100.100" {
|
|
|
|
t.Errorf("unexpected IPv4: %v", dst4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCorrectSource(t *testing.T) {
|
2018-04-06 13:51:59 +00:00
|
|
|
if runtime.GOARCH != "amd64" {
|
|
|
|
// See comment above in TestParseDstFromOOB.
|
|
|
|
t.Skip("skipping test on unsupported architecture")
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:59:24 +00:00
|
|
|
// dst is :ffff:100.100.100.100 which should be counted as IPv4
|
|
|
|
oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0}
|
|
|
|
soob := correctSource(oob)
|
|
|
|
cm4 := new(ipv4.ControlMessage)
|
|
|
|
cm4.Src = net.ParseIP("100.100.100.100")
|
|
|
|
if !bytes.Equal(soob, cm4.Marshal()) {
|
|
|
|
t.Errorf("unexpected oob for ipv4 address: %v", soob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dst is 2001:db8::1
|
|
|
|
oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
soob = correctSource(oob)
|
|
|
|
cm6 := new(ipv6.ControlMessage)
|
|
|
|
cm6.Src = net.ParseIP("2001:db8::1")
|
|
|
|
if !bytes.Equal(soob, cm6.Marshal()) {
|
|
|
|
t.Errorf("unexpected oob for IPv6 address: %v", soob)
|
|
|
|
}
|
|
|
|
}
|