summaryrefslogtreecommitdiff
path: root/src/pkg/net/dnsmsg.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/net/dnsmsg.go')
-rw-r--r--src/pkg/net/dnsmsg.go293
1 files changed, 146 insertions, 147 deletions
diff --git a/src/pkg/net/dnsmsg.go b/src/pkg/net/dnsmsg.go
index a166c9661..93ab3aac0 100644
--- a/src/pkg/net/dnsmsg.go
+++ b/src/pkg/net/dnsmsg.go
@@ -34,93 +34,93 @@ import (
// Wire constants.
const (
// valid _DNS_RR_Header.Rrtype and _DNS_Question.qtype
- _DNS_TypeA = 1;
- _DNS_TypeNS = 2;
- _DNS_TypeMD = 3;
- _DNS_TypeMF = 4;
- _DNS_TypeCNAME = 5;
- _DNS_TypeSOA = 6;
- _DNS_TypeMB = 7;
- _DNS_TypeMG = 8;
- _DNS_TypeMR = 9;
- _DNS_TypeNULL = 10;
- _DNS_TypeWKS = 11;
- _DNS_TypePTR = 12;
- _DNS_TypeHINFO = 13;
- _DNS_TypeMINFO = 14;
- _DNS_TypeMX = 15;
- _DNS_TypeTXT = 16;
+ _DNS_TypeA = 1;
+ _DNS_TypeNS = 2;
+ _DNS_TypeMD = 3;
+ _DNS_TypeMF = 4;
+ _DNS_TypeCNAME = 5;
+ _DNS_TypeSOA = 6;
+ _DNS_TypeMB = 7;
+ _DNS_TypeMG = 8;
+ _DNS_TypeMR = 9;
+ _DNS_TypeNULL = 10;
+ _DNS_TypeWKS = 11;
+ _DNS_TypePTR = 12;
+ _DNS_TypeHINFO = 13;
+ _DNS_TypeMINFO = 14;
+ _DNS_TypeMX = 15;
+ _DNS_TypeTXT = 16;
// valid _DNS_Question.qtype only
- _DNS_TypeAXFR = 252;
- _DNS_TypeMAILB = 253;
- _DNS_TypeMAILA = 254;
- _DNS_TypeALL = 255;
+ _DNS_TypeAXFR = 252;
+ _DNS_TypeMAILB = 253;
+ _DNS_TypeMAILA = 254;
+ _DNS_TypeALL = 255;
// valid _DNS_Question.qclass
- _DNS_ClassINET = 1;
- _DNS_ClassCSNET = 2;
- _DNS_ClassCHAOS = 3;
- _DNS_ClassHESIOD = 4;
- _DNS_ClassANY = 255;
+ _DNS_ClassINET = 1;
+ _DNS_ClassCSNET = 2;
+ _DNS_ClassCHAOS = 3;
+ _DNS_ClassHESIOD = 4;
+ _DNS_ClassANY = 255;
// _DNS_Msg.rcode
- _DNS_RcodeSuccess = 0;
- _DNS_RcodeFormatError = 1;
- _DNS_RcodeServerFailure = 2;
- _DNS_RcodeNameError = 3;
- _DNS_RcodeNotImplemented = 4;
- _DNS_RcodeRefused = 5;
+ _DNS_RcodeSuccess = 0;
+ _DNS_RcodeFormatError = 1;
+ _DNS_RcodeServerFailure = 2;
+ _DNS_RcodeNameError = 3;
+ _DNS_RcodeNotImplemented = 4;
+ _DNS_RcodeRefused = 5;
)
// The wire format for the DNS packet header.
type __DNS_Header struct {
- Id uint16;
- Bits uint16;
- Qdcount, Ancount, Nscount, Arcount uint16;
+ Id uint16;
+ Bits uint16;
+ Qdcount, Ancount, Nscount, Arcount uint16;
}
const (
// __DNS_Header.Bits
- _QR = 1<<15; // query/response (response=1)
- _AA = 1<<10; // authoritative
- _TC = 1<<9; // truncated
- _RD = 1<<8; // recursion desired
- _RA = 1<<7; // recursion available
+ _QR = 1<<15; // query/response (response=1)
+ _AA = 1<<10; // authoritative
+ _TC = 1<<9; // truncated
+ _RD = 1<<8; // recursion desired
+ _RA = 1<<7; // recursion available
)
// DNS queries.
type _DNS_Question struct {
- Name string "domain-name"; // "domain-name" specifies encoding; see packers below
- Qtype uint16;
- Qclass uint16;
+ Name string "domain-name"; // "domain-name" specifies encoding; see packers below
+ Qtype uint16;
+ Qclass uint16;
}
// DNS responses (resource records).
// There are many types of messages,
// but they all share the same header.
type _DNS_RR_Header struct {
- Name string "domain-name";
- Rrtype uint16;
- Class uint16;
- Ttl uint32;
- Rdlength uint16; // length of data after header
+ Name string "domain-name";
+ Rrtype uint16;
+ Class uint16;
+ Ttl uint32;
+ Rdlength uint16; // length of data after header
}
func (h *_DNS_RR_Header) Header() *_DNS_RR_Header {
- return h
+ return h;
}
type _DNS_RR interface {
- Header() *_DNS_RR_Header
+ Header() *_DNS_RR_Header;
}
// Specific DNS RR formats for each query type.
type _DNS_RR_CNAME struct {
- Hdr _DNS_RR_Header;
- Cname string "domain-name";
+ Hdr _DNS_RR_Header;
+ Cname string "domain-name";
}
func (rr *_DNS_RR_CNAME) Header() *_DNS_RR_Header {
@@ -128,9 +128,9 @@ func (rr *_DNS_RR_CNAME) Header() *_DNS_RR_Header {
}
type _DNS_RR_HINFO struct {
- Hdr _DNS_RR_Header;
- Cpu string;
- Os string;
+ Hdr _DNS_RR_Header;
+ Cpu string;
+ Os string;
}
func (rr *_DNS_RR_HINFO) Header() *_DNS_RR_Header {
@@ -138,8 +138,8 @@ func (rr *_DNS_RR_HINFO) Header() *_DNS_RR_Header {
}
type _DNS_RR_MB struct {
- Hdr _DNS_RR_Header;
- Mb string "domain-name";
+ Hdr _DNS_RR_Header;
+ Mb string "domain-name";
}
func (rr *_DNS_RR_MB) Header() *_DNS_RR_Header {
@@ -147,8 +147,8 @@ func (rr *_DNS_RR_MB) Header() *_DNS_RR_Header {
}
type _DNS_RR_MG struct {
- Hdr _DNS_RR_Header;
- Mg string "domain-name";
+ Hdr _DNS_RR_Header;
+ Mg string "domain-name";
}
func (rr *_DNS_RR_MG) Header() *_DNS_RR_Header {
@@ -156,9 +156,9 @@ func (rr *_DNS_RR_MG) Header() *_DNS_RR_Header {
}
type _DNS_RR_MINFO struct {
- Hdr _DNS_RR_Header;
- Rmail string "domain-name";
- Email string "domain-name";
+ Hdr _DNS_RR_Header;
+ Rmail string "domain-name";
+ Email string "domain-name";
}
func (rr *_DNS_RR_MINFO) Header() *_DNS_RR_Header {
@@ -166,8 +166,8 @@ func (rr *_DNS_RR_MINFO) Header() *_DNS_RR_Header {
}
type _DNS_RR_MR struct {
- Hdr _DNS_RR_Header;
- Mr string "domain-name";
+ Hdr _DNS_RR_Header;
+ Mr string "domain-name";
}
func (rr *_DNS_RR_MR) Header() *_DNS_RR_Header {
@@ -175,9 +175,9 @@ func (rr *_DNS_RR_MR) Header() *_DNS_RR_Header {
}
type _DNS_RR_MX struct {
- Hdr _DNS_RR_Header;
- Pref uint16;
- Mx string "domain-name";
+ Hdr _DNS_RR_Header;
+ Pref uint16;
+ Mx string "domain-name";
}
func (rr *_DNS_RR_MX) Header() *_DNS_RR_Header {
@@ -185,8 +185,8 @@ func (rr *_DNS_RR_MX) Header() *_DNS_RR_Header {
}
type _DNS_RR_NS struct {
- Hdr _DNS_RR_Header;
- Ns string "domain-name";
+ Hdr _DNS_RR_Header;
+ Ns string "domain-name";
}
func (rr *_DNS_RR_NS) Header() *_DNS_RR_Header {
@@ -194,8 +194,8 @@ func (rr *_DNS_RR_NS) Header() *_DNS_RR_Header {
}
type _DNS_RR_PTR struct {
- Hdr _DNS_RR_Header;
- Ptr string "domain-name";
+ Hdr _DNS_RR_Header;
+ Ptr string "domain-name";
}
func (rr *_DNS_RR_PTR) Header() *_DNS_RR_Header {
@@ -203,14 +203,14 @@ func (rr *_DNS_RR_PTR) Header() *_DNS_RR_Header {
}
type _DNS_RR_SOA struct {
- Hdr _DNS_RR_Header;
- Ns string "domain-name";
- Mbox string "domain-name";
- Serial uint32;
- Refresh uint32;
- Retry uint32;
- Expire uint32;
- Minttl uint32;
+ Hdr _DNS_RR_Header;
+ Ns string "domain-name";
+ Mbox string "domain-name";
+ Serial uint32;
+ Refresh uint32;
+ Retry uint32;
+ Expire uint32;
+ Minttl uint32;
}
func (rr *_DNS_RR_SOA) Header() *_DNS_RR_Header {
@@ -218,8 +218,8 @@ func (rr *_DNS_RR_SOA) Header() *_DNS_RR_Header {
}
type _DNS_RR_TXT struct {
- Hdr _DNS_RR_Header;
- Txt string; // not domain name
+ Hdr _DNS_RR_Header;
+ Txt string; // not domain name
}
func (rr *_DNS_RR_TXT) Header() *_DNS_RR_Header {
@@ -227,8 +227,8 @@ func (rr *_DNS_RR_TXT) Header() *_DNS_RR_Header {
}
type _DNS_RR_A struct {
- Hdr _DNS_RR_Header;
- A uint32 "ipv4";
+ Hdr _DNS_RR_Header;
+ A uint32 "ipv4";
}
func (rr *_DNS_RR_A) Header() *_DNS_RR_Header {
@@ -236,7 +236,6 @@ func (rr *_DNS_RR_A) Header() *_DNS_RR_Header {
}
-
// Packing and unpacking.
//
// All the packers and unpackers take a (msg []byte, off int)
@@ -246,7 +245,7 @@ func (rr *_DNS_RR_A) Header() *_DNS_RR_Header {
// packing sequence.
// Map of constructors for each RR wire type.
-var rr_mk = map[int] func()_DNS_RR {
+var rr_mk = map[int]func() _DNS_RR{
_DNS_TypeCNAME: func() _DNS_RR { return new(_DNS_RR_CNAME) },
_DNS_TypeHINFO: func() _DNS_RR { return new(_DNS_RR_HINFO) },
_DNS_TypeMB: func() _DNS_RR { return new(_DNS_RR_MB) },
@@ -274,19 +273,19 @@ func packDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
// We trade each dot byte for a length byte.
// There is also a trailing zero.
// Check that we have all the space we need.
- tot := len(s) + 1;
+ tot := len(s)+1;
if off+tot > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
// Emit sequence of counted strings, chopping at dots.
begin := 0;
for i := 0; i < len(s); i++ {
if s[i] == '.' {
- if i - begin >= 1<<6 { // top two bits of length must be clear
- return len(msg), false
+ if i-begin >= 1<<6 { // top two bits of length must be clear
+ return len(msg), false;
}
- msg[off] = byte(i - begin);
+ msg[off] = byte(i-begin);
off++;
for j := begin; j < i; j++ {
msg[off] = s[j];
@@ -297,7 +296,7 @@ func packDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
}
msg[off] = 0;
off++;
- return off, true
+ return off, true;
}
// Unpack a domain name.
@@ -319,7 +318,7 @@ func unpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) {
Loop:
for {
if off >= len(msg) {
- return "", len(msg), false
+ return "", len(msg), false;
}
c := int(msg[off]);
off++;
@@ -327,13 +326,13 @@ Loop:
case 0x00:
if c == 0x00 {
// end of name
- break Loop
+ break Loop;
}
// literal string
if off+c > len(msg) {
- return "", len(msg), false
+ return "", len(msg), false;
}
- s += string(msg[off:off+c]) + ".";
+ s += string(msg[off : off+c])+".";
off += c;
case 0xC0:
// pointer to somewhere else in msg.
@@ -342,26 +341,26 @@ Loop:
// also, don't follow too many pointers --
// maybe there's a loop.
if off >= len(msg) {
- return "", len(msg), false
+ return "", len(msg), false;
}
c1 := msg[off];
off++;
if ptr == 0 {
- off1 = off
+ off1 = off;
}
if ptr++; ptr > 10 {
- return "", len(msg), false
+ return "", len(msg), false;
}
off = (c^0xC0)<<8 | int(c1);
default:
// 0x80 and 0x40 are reserved
- return "", len(msg), false
+ return "", len(msg), false;
}
}
if ptr == 0 {
- off1 = off
+ off1 = off;
}
- return s, off1, true
+ return s, off1, true;
}
// TODO(rsc): Move into generic library?
@@ -379,7 +378,7 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
case *reflect.Uint16Value:
i := fv.Get();
if off+2 > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
msg[off] = byte(i>>8);
msg[off+1] = byte(i);
@@ -387,7 +386,7 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
case *reflect.Uint32Value:
i := fv.Get();
if off+4 > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
msg[off] = byte(i>>24);
msg[off+1] = byte(i>>16);
@@ -405,12 +404,12 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
case "domain-name":
off, ok = packDomainName(s, msg, off);
if !ok {
- return len(msg), false
+ return len(msg), false;
}
case "":
// Counted string: 1 byte length.
- if len(s) > 255 || off + 1 + len(s) > len(msg) {
- return len(msg), false
+ if len(s) > 255 || off+1+len(s) > len(msg) {
+ return len(msg), false;
}
msg[off] = byte(len(s));
off++;
@@ -421,7 +420,7 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
}
}
}
- return off, true
+ return off, true;
}
func structValue(any interface{}) *reflect.StructValue {
@@ -430,7 +429,7 @@ func structValue(any interface{}) *reflect.StructValue {
func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
off, ok = packStructValue(structValue(any), msg, off);
- return off, ok
+ return off, ok;
}
// TODO(rsc): Move into generic library?
@@ -447,14 +446,14 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
off, ok = unpackStructValue(fv, msg, off);
case *reflect.Uint16Value:
if off+2 > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
i := uint16(msg[off])<<8 | uint16(msg[off+1]);
fv.Set(i);
off += 2;
case *reflect.Uint32Value:
if off+4 > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
i := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3]);
fv.Set(i);
@@ -468,11 +467,11 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
case "domain-name":
s, off, ok = unpackDomainName(msg, off);
if !ok {
- return len(msg), false
+ return len(msg), false;
}
case "":
if off >= len(msg) || off+1+int(msg[off]) > len(msg) {
- return len(msg), false
+ return len(msg), false;
}
n := int(msg[off]);
off++;
@@ -486,12 +485,12 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
fv.Set(s);
}
}
- return off, true
+ return off, true;
}
func unpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
off, ok = unpackStructValue(structValue(any), msg, off);
- return off, ok
+ return off, ok;
}
// Generic struct printer.
@@ -515,7 +514,7 @@ func printStructValue(val *reflect.StructValue) string {
i := fv.Get();
s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)).String();
} else {
- s += fmt.Sprint(fval.Interface())
+ s += fmt.Sprint(fval.Interface());
}
}
s += "}";
@@ -537,12 +536,12 @@ func packRR(rr _DNS_RR, msg []byte, off int) (off2 int, ok bool) {
off1, ok = packStruct(rr.Header(), msg, off);
off2, ok = packStruct(rr, msg, off);
if !ok {
- return len(msg), false
+ return len(msg), false;
}
// pack a third time; redo header with correct data length
- rr.Header().Rdlength = uint16(off2 - off1);
+ rr.Header().Rdlength = uint16(off2-off1);
packStruct(rr.Header(), msg, off);
- return off2, true
+ return off2, true;
}
// Resource record unpacker.
@@ -551,7 +550,7 @@ func unpackRR(msg []byte, off int) (rr _DNS_RR, off1 int, ok bool) {
var h _DNS_RR_Header;
off0 := off;
if off, ok = unpackStruct(&h, msg, off); !ok {
- return nil, len(msg), false
+ return nil, len(msg), false;
}
end := off+int(h.Rdlength);
@@ -559,14 +558,14 @@ func unpackRR(msg []byte, off int) (rr _DNS_RR, off1 int, ok bool) {
// again inefficient but doesn't need to be fast.
mk, known := rr_mk[int(h.Rrtype)];
if !known {
- return &h, end, true
+ return &h, end, true;
}
rr = mk();
off, ok = unpackStruct(rr, msg, off0);
if off != end {
- return &h, end, true
+ return &h, end, true;
}
- return rr, off, ok
+ return rr, off, ok;
}
// Usable representation of a DNS packet.
@@ -574,22 +573,22 @@ func unpackRR(msg []byte, off int) (rr _DNS_RR, off1 int, ok bool) {
// A manually-unpacked version of (id, bits).
// This is in its own struct for easy printing.
type __DNS_Msg_Top struct {
- id uint16;
- response bool;
- opcode int;
- authoritative bool;
- truncated bool;
- recursion_desired bool;
- recursion_available bool;
- rcode int;
+ id uint16;
+ response bool;
+ opcode int;
+ authoritative bool;
+ truncated bool;
+ recursion_desired bool;
+ recursion_available bool;
+ rcode int;
}
type _DNS_Msg struct {
__DNS_Msg_Top;
- question []_DNS_Question;
- answer []_DNS_RR;
- ns []_DNS_RR;
- extra []_DNS_RR;
+ question []_DNS_Question;
+ answer []_DNS_RR;
+ ns []_DNS_RR;
+ extra []_DNS_RR;
}
@@ -647,9 +646,9 @@ func (dns *_DNS_Msg) Pack() (msg []byte, ok bool) {
off, ok = packStruct(extra[i], msg, off);
}
if !ok {
- return nil, false
+ return nil, false;
}
- return msg[0:off], true
+ return msg[0:off], true;
}
func (dns *_DNS_Msg) Unpack(msg []byte) bool {
@@ -658,11 +657,11 @@ func (dns *_DNS_Msg) Unpack(msg []byte) bool {
off := 0;
var ok bool;
if off, ok = unpackStruct(&dh, msg, off); !ok {
- return false
+ return false;
}
dns.id = dh.Id;
dns.response = (dh.Bits & _QR) != 0;
- dns.opcode = int(dh.Bits >> 11) & 0xF;
+ dns.opcode = int(dh.Bits >> 11)&0xF;
dns.authoritative = (dh.Bits & _AA) != 0;
dns.truncated = (dh.Bits & _TC) != 0;
dns.recursion_desired = (dh.Bits & _RD) != 0;
@@ -688,38 +687,38 @@ func (dns *_DNS_Msg) Unpack(msg []byte) bool {
dns.extra[i], off, ok = unpackRR(msg, off);
}
if !ok {
- return false
+ return false;
}
-// if off != len(msg) {
-// println("extra bytes in dns packet", off, "<", len(msg));
-// }
- return true
+ // if off != len(msg) {
+ // println("extra bytes in dns packet", off, "<", len(msg));
+ // }
+ return true;
}
func (dns *_DNS_Msg) String() string {
- s := "DNS: "+printStruct(&dns.__DNS_Msg_Top)+"\n";
+ s := "DNS: " + printStruct(&dns.__DNS_Msg_Top) + "\n";
if len(dns.question) > 0 {
s += "-- Questions\n";
for i := 0; i < len(dns.question); i++ {
- s += printStruct(&dns.question[i])+"\n";
+ s += printStruct(&dns.question[i]) + "\n";
}
}
if len(dns.answer) > 0 {
s += "-- Answers\n";
for i := 0; i < len(dns.answer); i++ {
- s += printStruct(dns.answer[i])+"\n";
+ s += printStruct(dns.answer[i]) + "\n";
}
}
if len(dns.ns) > 0 {
s += "-- Name servers\n";
for i := 0; i < len(dns.ns); i++ {
- s += printStruct(dns.ns[i])+"\n";
+ s += printStruct(dns.ns[i]) + "\n";
}
}
if len(dns.extra) > 0 {
s += "-- Extra\n";
for i := 0; i < len(dns.extra); i++ {
- s += printStruct(dns.extra[i])+"\n";
+ s += printStruct(dns.extra[i]) + "\n";
}
}
return s;