@@ -81,17 +81,16 @@ type OPT struct {
8181
8282func (rr * OPT ) String () string {
8383 s := "\n ;; OPT PSEUDOSECTION:\n ; EDNS: version " + strconv .Itoa (int (rr .Version ())) + "; "
84+ s += "flags:"
8485 if rr .Do () {
85- if rr .Co () {
86- s += "flags: do, co; "
87- } else {
88- s += "flags: do; "
89- }
90- } else {
91- s += "flags:; "
86+ s += " do"
9287 }
93- if rr .Hdr .Ttl & 0x7FFF != 0 {
94- s += fmt .Sprintf ("MBZ: 0x%04x, " , rr .Hdr .Ttl & 0x7FFF )
88+ if rr .Co () {
89+ s += " co"
90+ }
91+ s += "; "
92+ if z := rr .Z (); z != 0 {
93+ s += fmt .Sprintf ("MBZ: 0x%04x, " , z )
9594 }
9695 s += "udp: " + strconv .Itoa (int (rr .UDPSize ()))
9796
@@ -318,41 +317,54 @@ type EDNS0_SUBNET struct {
318317func (e * EDNS0_SUBNET ) Option () uint16 { return EDNS0SUBNET }
319318
320319func (e * EDNS0_SUBNET ) pack () ([]byte , error ) {
321- b := make ([]byte , 4 )
322- binary .BigEndian .PutUint16 (b [0 :], e .Family )
323- b [2 ] = e .SourceNetmask
324- b [3 ] = e .SourceScope
325320 switch e .Family {
326321 case 0 :
327322 // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
328323 // We might don't need to complain either
329324 if e .SourceNetmask != 0 {
330325 return nil , errors .New ("bad address family" )
331326 }
327+ b := make ([]byte , 4 )
328+ b [3 ] = e .SourceScope
329+ return b , nil
332330 case 1 :
333331 if e .SourceNetmask > net .IPv4len * 8 {
334332 return nil , errors .New ("bad netmask" )
335333 }
336- if len (e .Address .To4 ()) != net .IPv4len {
334+ ip4 := e .Address .To4 ()
335+ if len (ip4 ) != net .IPv4len {
337336 return nil , errors .New ("bad address" )
338337 }
339- ip := e .Address .To4 ().Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv4len * 8 ))
340338 needLength := (e .SourceNetmask + 8 - 1 ) / 8 // division rounding up
341- b = append (b , ip [:needLength ]... )
339+ b := make ([]byte , 4 + needLength )
340+ binary .BigEndian .PutUint16 (b [0 :], e .Family )
341+ b [2 ] = e .SourceNetmask
342+ b [3 ] = e .SourceScope
343+ if needLength > 0 {
344+ ip := ip4 .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv4len * 8 ))
345+ copy (b [4 :], ip [:needLength ])
346+ }
347+ return b , nil
342348 case 2 :
343349 if e .SourceNetmask > net .IPv6len * 8 {
344350 return nil , errors .New ("bad netmask" )
345351 }
346352 if len (e .Address ) != net .IPv6len {
347353 return nil , errors .New ("bad address" )
348354 }
349- ip := e .Address .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv6len * 8 ))
350355 needLength := (e .SourceNetmask + 8 - 1 ) / 8 // division rounding up
351- b = append (b , ip [:needLength ]... )
356+ b := make ([]byte , 4 + needLength )
357+ binary .BigEndian .PutUint16 (b [0 :], e .Family )
358+ b [2 ] = e .SourceNetmask
359+ b [3 ] = e .SourceScope
360+ if needLength > 0 {
361+ ip := e .Address .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv6len * 8 ))
362+ copy (b [4 :], ip [:needLength ])
363+ }
364+ return b , nil
352365 default :
353366 return nil , errors .New ("bad address family" )
354367 }
355- return b , nil
356368}
357369
358370func (e * EDNS0_SUBNET ) unpack (b []byte ) error {
0 commit comments