@@ -318,41 +318,54 @@ type EDNS0_SUBNET struct {
318318func (e * EDNS0_SUBNET ) Option () uint16 { return EDNS0SUBNET }
319319
320320func (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
325321 switch e .Family {
326322 case 0 :
327323 // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
328324 // We might don't need to complain either
329325 if e .SourceNetmask != 0 {
330326 return nil , errors .New ("bad address family" )
331327 }
328+ b := make ([]byte , 4 )
329+ b [3 ] = e .SourceScope
330+ return b , nil
332331 case 1 :
333332 if e .SourceNetmask > net .IPv4len * 8 {
334333 return nil , errors .New ("bad netmask" )
335334 }
336- if len (e .Address .To4 ()) != net .IPv4len {
335+ ip4 := e .Address .To4 ()
336+ if len (ip4 ) != net .IPv4len {
337337 return nil , errors .New ("bad address" )
338338 }
339- ip := e .Address .To4 ().Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv4len * 8 ))
340339 needLength := (e .SourceNetmask + 8 - 1 ) / 8 // division rounding up
341- b = append (b , ip [:needLength ]... )
340+ b := make ([]byte , 4 + needLength )
341+ binary .BigEndian .PutUint16 (b [0 :], e .Family )
342+ b [2 ] = e .SourceNetmask
343+ b [3 ] = e .SourceScope
344+ if needLength > 0 {
345+ ip := ip4 .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv4len * 8 ))
346+ copy (b [4 :], ip [:needLength ])
347+ }
348+ return b , nil
342349 case 2 :
343350 if e .SourceNetmask > net .IPv6len * 8 {
344351 return nil , errors .New ("bad netmask" )
345352 }
346353 if len (e .Address ) != net .IPv6len {
347354 return nil , errors .New ("bad address" )
348355 }
349- ip := e .Address .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv6len * 8 ))
350356 needLength := (e .SourceNetmask + 8 - 1 ) / 8 // division rounding up
351- b = append (b , ip [:needLength ]... )
357+ b := make ([]byte , 4 + needLength )
358+ binary .BigEndian .PutUint16 (b [0 :], e .Family )
359+ b [2 ] = e .SourceNetmask
360+ b [3 ] = e .SourceScope
361+ if needLength > 0 {
362+ ip := e .Address .Mask (net .CIDRMask (int (e .SourceNetmask ), net .IPv6len * 8 ))
363+ copy (b [4 :], ip [:needLength ])
364+ }
365+ return b , nil
352366 default :
353367 return nil , errors .New ("bad address family" )
354368 }
355- return b , nil
356369}
357370
358371func (e * EDNS0_SUBNET ) unpack (b []byte ) error {
0 commit comments