LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // DNS packet assembly. See RFC 1035. | 5 // DNS packet assembly. See RFC 1035. |
6 // | 6 // |
7 // This is intended to support name resolution during net.Dial. | 7 // This is intended to support name resolution during net.Dial. |
8 // It doesn't have to be blazing fast. | 8 // It doesn't have to be blazing fast. |
9 // | 9 // |
10 // Rather than write the usual handful of routines to pack and | 10 // Rather than write the usual handful of routines to pack and |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 off1 = off | 373 off1 = off |
374 } | 374 } |
375 return s, off1, true | 375 return s, off1, true |
376 } | 376 } |
377 | 377 |
378 // TODO(rsc): Move into generic library? | 378 // TODO(rsc): Move into generic library? |
379 // Pack a reflect.StructValue into msg. Struct members can only be uint16, uint
32, string, | 379 // Pack a reflect.StructValue into msg. Struct members can only be uint16, uint
32, string, |
380 // and other (often anonymous) structs. | 380 // and other (often anonymous) structs. |
381 func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
k bool) { | 381 func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
k bool) { |
382 for i := 0; i < val.NumField(); i++ { | 382 for i := 0; i < val.NumField(); i++ { |
383 » » f := val.Type().(*reflect.StructType).Field(i) | 383 » » f := val.Type().Field(i) |
384 switch fv := val.Field(i).(type) { | 384 switch fv := val.Field(i).(type) { |
385 default: | 385 default: |
386 BadType: | 386 BadType: |
387 fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %
v", f.Type) | 387 fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %
v", f.Type) |
388 return len(msg), false | 388 return len(msg), false |
389 case *reflect.StructValue: | 389 case *reflect.StructValue: |
390 off, ok = packStructValue(fv, msg, off) | 390 off, ok = packStructValue(fv, msg, off) |
391 case *reflect.UintValue: | 391 case *reflect.UintValue: |
392 i := fv.Get() | 392 i := fv.Get() |
393 switch fv.Type().Kind() { | 393 switch fv.Type().Kind() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { | 444 func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { |
445 off, ok = packStructValue(structValue(any), msg, off) | 445 off, ok = packStructValue(structValue(any), msg, off) |
446 return off, ok | 446 return off, ok |
447 } | 447 } |
448 | 448 |
449 // TODO(rsc): Move into generic library? | 449 // TODO(rsc): Move into generic library? |
450 // Unpack a reflect.StructValue from msg. | 450 // Unpack a reflect.StructValue from msg. |
451 // Same restrictions as packStructValue. | 451 // Same restrictions as packStructValue. |
452 func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
ok bool) { | 452 func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
ok bool) { |
453 for i := 0; i < val.NumField(); i++ { | 453 for i := 0; i < val.NumField(); i++ { |
454 » » f := val.Type().(*reflect.StructType).Field(i) | 454 » » f := val.Type().Field(i) |
455 switch fv := val.Field(i).(type) { | 455 switch fv := val.Field(i).(type) { |
456 default: | 456 default: |
457 BadType: | 457 BadType: |
458 fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %
v", f.Type) | 458 fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %
v", f.Type) |
459 return len(msg), false | 459 return len(msg), false |
460 case *reflect.StructValue: | 460 case *reflect.StructValue: |
461 off, ok = unpackStructValue(fv, msg, off) | 461 off, ok = unpackStructValue(fv, msg, off) |
462 case *reflect.UintValue: | 462 case *reflect.UintValue: |
463 switch fv.Type().Kind() { | 463 switch fv.Type().Kind() { |
464 default: | 464 default: |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 // Generic struct printer. | 516 // Generic struct printer. |
517 // Doesn't care about the string tag "domain-name", | 517 // Doesn't care about the string tag "domain-name", |
518 // but does look for an "ipv4" tag on uint32 variables, | 518 // but does look for an "ipv4" tag on uint32 variables, |
519 // printing them as IP addresses. | 519 // printing them as IP addresses. |
520 func printStructValue(val *reflect.StructValue) string { | 520 func printStructValue(val *reflect.StructValue) string { |
521 s := "{" | 521 s := "{" |
522 for i := 0; i < val.NumField(); i++ { | 522 for i := 0; i < val.NumField(); i++ { |
523 if i > 0 { | 523 if i > 0 { |
524 s += ", " | 524 s += ", " |
525 } | 525 } |
526 » » f := val.Type().(*reflect.StructType).Field(i) | 526 » » f := val.Type().Field(i) |
527 if !f.Anonymous { | 527 if !f.Anonymous { |
528 s += f.Name + "=" | 528 s += f.Name + "=" |
529 } | 529 } |
530 fval := val.Field(i) | 530 fval := val.Field(i) |
531 if fv, ok := fval.(*reflect.StructValue); ok { | 531 if fv, ok := fval.(*reflect.StructValue); ok { |
532 s += printStructValue(fv) | 532 s += printStructValue(fv) |
533 } else if fv, ok := fval.(*reflect.UintValue); ok && f.Tag == "i
pv4" { | 533 } else if fv, ok := fval.(*reflect.UintValue); ok && f.Tag == "i
pv4" { |
534 i := fv.Get() | 534 i := fv.Get() |
535 s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))
.String() | 535 s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))
.String() |
536 } else { | 536 } else { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 } | 734 } |
735 } | 735 } |
736 if len(dns.extra) > 0 { | 736 if len(dns.extra) > 0 { |
737 s += "-- Extra\n" | 737 s += "-- Extra\n" |
738 for i := 0; i < len(dns.extra); i++ { | 738 for i := 0; i < len(dns.extra); i++ { |
739 s += printStruct(dns.extra[i]) + "\n" | 739 s += printStruct(dns.extra[i]) + "\n" |
740 } | 740 } |
741 } | 741 } |
742 return s | 742 return s |
743 } | 743 } |
LEFT | RIGHT |