diff options
author | Russ Cox <rsc@golang.org> | 2009-01-16 11:04:44 -0800 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-01-16 11:04:44 -0800 |
commit | 090417987ea5fe909106e50402a212d66bd509e7 (patch) | |
tree | 39f83d6bffdfcf002cd18eaf7f653f6c80a906a2 /src/lib/net/dnsmsg.go | |
parent | 7e260b9427a0bd2bb671f17ffa4eb5ad4a1921a0 (diff) | |
download | golang-090417987ea5fe909106e50402a212d66bd509e7.tar.gz |
casify DNS
R=r
DELTA=221 (0 added, 0 deleted, 221 changed)
OCL=22946
CL=22948
Diffstat (limited to 'src/lib/net/dnsmsg.go')
-rw-r--r-- | src/lib/net/dnsmsg.go | 136 |
1 files changed, 68 insertions, 68 deletions
diff --git a/src/lib/net/dnsmsg.go b/src/lib/net/dnsmsg.go index e497fa9b8..159ea7bb2 100644 --- a/src/lib/net/dnsmsg.go +++ b/src/lib/net/dnsmsg.go @@ -29,7 +29,7 @@ import ( "reflect"; ) -// Packet formats +// _Packet formats // Wire constants. export const ( @@ -74,19 +74,19 @@ export const ( ) // The wire format for the DNS packet header. -type DNS_Header struct { +type _DNS_Header struct { 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 + // _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 ) // DNS queries. @@ -188,7 +188,7 @@ export type DNS_RR_A struct { } -// Packing and unpacking. +// _Packing and unpacking. // // All the packers and unpackers take a (msg []byte, off int) // and return (off1 int, ok bool). If they return ok==false, they @@ -212,10 +212,10 @@ var rr_mk = map[int]*()DNS_RR { DNS_TypeA: func() DNS_RR { return new(DNS_RR_A) }, } -// Pack a domain name s into msg[off:]. +// _Pack a domain name s into msg[off:]. // Domain names are a sequence of counted strings // split at the dots. They end with a zero-length string. -func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) { +func _PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) { // Add trailing dot to canonicalize name. if n := len(s); n == 0 || s[n-1] != '.' { s += "."; @@ -251,7 +251,7 @@ func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) { return off, true } -// Unpack a domain name. +// _Unpack a domain name. // In addition to the simple sequences of counted strings above, // domain names are allowed to refer to strings elsewhere in the // packet, to avoid repeating common suffixes when returning @@ -264,7 +264,7 @@ func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) { // which is where the next record will start. // In theory, the pointers are only allowed to jump backward. // We let them jump anywhere and stop jumping after a while. -func UnpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) { +func _UnpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) { s = ""; ptr := 0; // number of pointers followed Loop: @@ -315,9 +315,9 @@ Loop: return s, off1, true } -// Pack a reflect.StructValue into msg. Struct members can only be uint16, uint32, string, +// _Pack a reflect.StructValue into msg. Struct members can only be uint16, uint32, string, // and other (often anonymous) structs. -func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { +func _PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { for i := 0; i < val.Len(); i++ { fld := val.Field(i); name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i); @@ -326,7 +326,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type()); return len(msg), false; case reflect.StructKind: - off, ok = PackStructValue(fld.(reflect.StructValue), msg, off); + off, ok = _PackStructValue(fld.(reflect.StructValue), msg, off); case reflect.Uint16Kind: i := fld.(reflect.Uint16Value).Get(); if off+2 > len(msg) { @@ -354,7 +354,7 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag); return len(msg), false; case "domain-name": - off, ok = PackDomainName(s, msg, off); + off, ok = _PackDomainName(s, msg, off); if !ok { return len(msg), false } @@ -375,15 +375,15 @@ func PackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok return off, true } -func PackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { +func _PackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); - off, ok = PackStructValue(val, msg, off); + off, ok = _PackStructValue(val, msg, off); return off, ok } -// Unpack a reflect.StructValue from msg. -// Same restrictions as PackStructValue. -func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { +// _Unpack a reflect.StructValue from msg. +// Same restrictions as _PackStructValue. +func _UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, ok bool) { for i := 0; i < val.Len(); i++ { name, typ, tag, xxx := val.Type().(reflect.StructType).Field(i); fld := val.Field(i); @@ -392,7 +392,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", fld.Type()); return len(msg), false; case reflect.StructKind: - off, ok = UnpackStructValue(fld.(reflect.StructValue), msg, off); + off, ok = _UnpackStructValue(fld.(reflect.StructValue), msg, off); case reflect.Uint16Kind: if off+2 > len(msg) { return len(msg), false @@ -414,7 +414,7 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", tag); return len(msg), false; case "domain-name": - s, off, ok = UnpackDomainName(msg, off); + s, off, ok = _UnpackDomainName(msg, off); if !ok { return len(msg), false } @@ -437,9 +437,9 @@ func UnpackStructValue(val reflect.StructValue, msg []byte, off int) (off1 int, return off, true } -func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { +func _UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); - off, ok = UnpackStructValue(val, msg, off); + off, ok = _UnpackStructValue(val, msg, off); return off, ok } @@ -447,7 +447,7 @@ func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { // Doesn't care about the string tag "domain-name", // but does look for an "ipv4" tag on uint32 variables, // printing them as IP addresses. -func PrintStructValue(val reflect.StructValue) string { +func _PrintStructValue(val reflect.StructValue) string { s := "{"; for i := 0; i < val.Len(); i++ { if i > 0 { @@ -461,7 +461,7 @@ func PrintStructValue(val reflect.StructValue) string { kind := fld.Kind(); switch { case kind == reflect.StructKind: - s += PrintStructValue(fld.(reflect.StructValue)); + s += _PrintStructValue(fld.(reflect.StructValue)); case kind == reflect.Uint32Kind && tag == "ipv4": i := fld.(reflect.Uint32Value).Get(); s += fmt.Sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF); @@ -473,37 +473,37 @@ func PrintStructValue(val reflect.StructValue) string { return s; } -func PrintStruct(any interface{}) string { +func _PrintStruct(any interface{}) string { val := reflect.NewValue(any).(reflect.PtrValue).Sub().(reflect.StructValue); - s := PrintStructValue(val); + s := _PrintStructValue(val); return s } // Resource record packer. -func PackRR(rr DNS_RR, msg []byte, off int) (off2 int, ok bool) { +func _PackRR(rr DNS_RR, msg []byte, off int) (off2 int, ok bool) { var off1 int; // pack twice, once to find end of header // and again to find end of packet. // a bit inefficient but this doesn't need to be fast. // off1 is end of header // off2 is end of rr - off1, ok = PackStruct(rr.Header(), msg, off); - off2, ok = PackStruct(rr, msg, off); + off1, ok = _PackStruct(rr.Header(), msg, off); + off2, ok = _PackStruct(rr, msg, off); if !ok { return len(msg), false } // pack a third time; redo header with correct data length rr.Header().rdlength = uint16(off2 - off1); - PackStruct(rr.Header(), msg, off); + _PackStruct(rr.Header(), msg, off); return off2, true } // Resource record unpacker. -func UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) { +func _UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) { // unpack just the header, to find the rr type and length var h DNS_RR_Header; off0 := off; - if off, ok = UnpackStruct(&h, msg, off); !ok { + if off, ok = _UnpackStruct(&h, msg, off); !ok { return nil, len(msg), false } end := off+int(h.rdlength); @@ -515,7 +515,7 @@ func UnpackRR(msg []byte, off int) (rr DNS_RR, off1 int, ok bool) { return &h, end, true } rr = mk(); - off, ok = UnpackStruct(rr, msg, off0); + off, ok = _UnpackStruct(rr, msg, off0); if off != end { return &h, end, true } @@ -526,7 +526,7 @@ 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 { +type _DNS_Msg_Top struct { id uint16; response bool; opcode int; @@ -538,7 +538,7 @@ type DNS_Msg_Top struct { } export type DNS_Msg struct { - DNS_Msg_Top; + _DNS_Msg_Top; question []DNS_Question; answer []DNS_RR; ns []DNS_RR; @@ -547,25 +547,25 @@ export type DNS_Msg struct { func (dns *DNS_Msg) Pack() (msg []byte, ok bool) { - var dh DNS_Header; + var dh _DNS_Header; - // Convert convenient DNS_Msg into wire-like DNS_Header. + // Convert convenient DNS_Msg into wire-like _DNS_Header. dh.id = dns.id; dh.bits = uint16(dns.opcode)<<11 | uint16(dns.rcode); if dns.recursion_available { - dh.bits |= RA; + dh.bits |= _RA; } if dns.recursion_desired { - dh.bits |= RD; + dh.bits |= _RD; } if dns.truncated { - dh.bits |= TC; + dh.bits |= _TC; } if dns.authoritative { - dh.bits |= AA; + dh.bits |= _AA; } if dns.response { - dh.bits |= QR; + dh.bits |= _QR; } // Prepare variable sized arrays. @@ -584,20 +584,20 @@ func (dns *DNS_Msg) Pack() (msg []byte, ok bool) { // big enough to hurt the allocator. msg = make([]byte, 2000); - // Pack it in: header and then the pieces. + // _Pack it in: header and then the pieces. off := 0; - off, ok = PackStruct(&dh, msg, off); + off, ok = _PackStruct(&dh, msg, off); for i := 0; i < len(question); i++ { - off, ok = PackStruct(&question[i], msg, off); + off, ok = _PackStruct(&question[i], msg, off); } for i := 0; i < len(answer); i++ { - off, ok = PackStruct(answer[i], msg, off); + off, ok = _PackStruct(answer[i], msg, off); } for i := 0; i < len(ns); i++ { - off, ok = PackStruct(ns[i], msg, off); + off, ok = _PackStruct(ns[i], msg, off); } for i := 0; i < len(extra); i++ { - off, ok = PackStruct(extra[i], msg, off); + off, ok = _PackStruct(extra[i], msg, off); } if !ok { return nil, false @@ -607,19 +607,19 @@ func (dns *DNS_Msg) Pack() (msg []byte, ok bool) { func (dns *DNS_Msg) Unpack(msg []byte) bool { // Header. - var dh DNS_Header; + var dh _DNS_Header; off := 0; var ok bool; - if off, ok = UnpackStruct(&dh, msg, off); !ok { + if off, ok = _UnpackStruct(&dh, msg, off); !ok { return false } dns.id = dh.id; - dns.response = (dh.bits & QR) != 0; + dns.response = (dh.bits & _QR) != 0; 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; - dns.recursion_available = (dh.bits & RA) != 0; + dns.authoritative = (dh.bits & _AA) != 0; + dns.truncated = (dh.bits & _TC) != 0; + dns.recursion_desired = (dh.bits & _RD) != 0; + dns.recursion_available = (dh.bits & _RA) != 0; dns.rcode = int(dh.bits & 0xF); // Arrays. @@ -629,16 +629,16 @@ func (dns *DNS_Msg) Unpack(msg []byte) bool { dns.extra = make([]DNS_RR, dh.arcount); for i := 0; i < len(dns.question); i++ { - off, ok = UnpackStruct(&dns.question[i], msg, off); + off, ok = _UnpackStruct(&dns.question[i], msg, off); } for i := 0; i < len(dns.answer); i++ { - dns.answer[i], off, ok = UnpackRR(msg, off); + dns.answer[i], off, ok = _UnpackRR(msg, off); } for i := 0; i < len(dns.ns); i++ { - dns.ns[i], off, ok = UnpackRR(msg, off); + dns.ns[i], off, ok = _UnpackRR(msg, off); } for i := 0; i < len(dns.extra); i++ { - dns.extra[i], off, ok = UnpackRR(msg, off); + dns.extra[i], off, ok = _UnpackRR(msg, off); } if !ok { return false @@ -650,29 +650,29 @@ func (dns *DNS_Msg) Unpack(msg []byte) bool { } 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; |