forked from okmeter/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google#198 from tzneal/master
Add serialization support for the DNS layer
- Loading branch information
Showing
5 changed files
with
512 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,13 @@ import ( | |
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/bytediff" | ||
"net" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/bytediff" | ||
) | ||
|
||
var testSimpleTCPPacket []byte = []byte{ | ||
|
@@ -1207,66 +1208,3 @@ func TestPacketIPv4Fragmented(t *testing.T) { | |
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, gopacket.LayerTypeFragment}, t) | ||
testSerializationWithOpts(t, p, testPacketIPv4Fragmented, gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}) | ||
} | ||
|
||
// testPacketDNSRegression is the packet: | ||
// 11:08:05.708342 IP 109.194.160.4.57766 > 95.211.92.14.53: 63000% [1au] A? picslife.ru. (40) | ||
// 0x0000: 0022 19b6 7e22 000f 35bb 0b40 0800 4500 ."..~"[email protected]. | ||
// 0x0010: 0044 89c4 0000 3811 2f3d 6dc2 a004 5fd3 .D....8./=m..._. | ||
// 0x0020: 5c0e e1a6 0035 0030 a597 f618 0010 0001 \....5.0........ | ||
// 0x0030: 0000 0000 0001 0870 6963 736c 6966 6502 .......picslife. | ||
// 0x0040: 7275 0000 0100 0100 0029 1000 0000 8000 ru.......)...... | ||
// 0x0050: 0000 .. | ||
var testPacketDNSRegression = []byte{ | ||
0x00, 0x22, 0x19, 0xb6, 0x7e, 0x22, 0x00, 0x0f, 0x35, 0xbb, 0x0b, 0x40, 0x08, 0x00, 0x45, 0x00, | ||
0x00, 0x44, 0x89, 0xc4, 0x00, 0x00, 0x38, 0x11, 0x2f, 0x3d, 0x6d, 0xc2, 0xa0, 0x04, 0x5f, 0xd3, | ||
0x5c, 0x0e, 0xe1, 0xa6, 0x00, 0x35, 0x00, 0x30, 0xa5, 0x97, 0xf6, 0x18, 0x00, 0x10, 0x00, 0x01, | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x70, 0x69, 0x63, 0x73, 0x6c, 0x69, 0x66, 0x65, 0x02, | ||
0x72, 0x75, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, | ||
0x00, 0x00, | ||
} | ||
|
||
func TestPacketDNSRegression(t *testing.T) { | ||
p := gopacket.NewPacket(testPacketDNSRegression, LinkTypeEthernet, testDecodeOptions) | ||
if p.ErrorLayer() != nil { | ||
t.Error("Failed to decode packet:", p.ErrorLayer().Error()) | ||
} | ||
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeDNS}, t) | ||
} | ||
func BenchmarkDecodePacketDNSRegression(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
gopacket.NewPacket(testPacketDNSRegression, LinkTypeEthernet, gopacket.NoCopy) | ||
} | ||
} | ||
|
||
// response to `dig TXT google.com` over IPv4 link: | ||
var testParseDNSTypeTXTValue = `v=spf1 include:_spf.google.com ~all` | ||
var testParseDNSTypeTXT = []byte{ | ||
0x02, 0x00, 0x00, 0x00, // PF_INET | ||
0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, 0x00, 0x39, 0x11, 0x64, 0x98, 0xd0, 0x43, 0xde, 0xde, | ||
0x0a, 0xba, 0x23, 0x06, 0x00, 0x35, 0x81, 0xb2, 0x00, 0x5f, 0xdc, 0xb5, 0x98, 0x71, 0x81, 0x80, | ||
0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, | ||
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x10, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, | ||
0x0e, 0x10, 0x00, 0x24, 0x23, 0x76, 0x3d, 0x73, 0x70, 0x66, 0x31, 0x20, 0x69, 0x6e, 0x63, 0x6c, | ||
0x75, 0x64, 0x65, 0x3a, 0x5f, 0x73, 0x70, 0x66, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, | ||
0x63, 0x6f, 0x6d, 0x20, 0x7e, 0x61, 0x6c, 0x6c, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, | ||
} | ||
|
||
func TestParseDNSTypeTXT(t *testing.T) { | ||
p := gopacket.NewPacket(testParseDNSTypeTXT, LinkTypeNull, testDecodeOptions) | ||
if p.ErrorLayer() != nil { | ||
t.Error("Failed to decode packet:", p.ErrorLayer().Error()) | ||
} | ||
checkLayers(p, []gopacket.LayerType{LayerTypeLoopback, LayerTypeIPv4, LayerTypeUDP, LayerTypeDNS}, t) | ||
answers := p.Layer(LayerTypeDNS).(*DNS).Answers | ||
if len(answers) != 1 { | ||
t.Error("Failed to parse 1 DNS answer") | ||
} | ||
if len(answers[0].TXTs) != 1 { | ||
t.Error("Failed to parse 1 TXT record") | ||
} | ||
txt := string(answers[0].TXTs[0]) | ||
if txt != testParseDNSTypeTXTValue { | ||
t.Errorf("Incorrect TXT value, expected %q, got %q", testParseDNSTypeTXTValue, txt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.