@@ -155,38 +155,40 @@ func (p *IPv4Packet) UnmarshalBinary(data []byte) error {
155155 if len (data ) < headerSizeIPv4 {
156156 return fmt .Errorf ("minimum header size for IPv4 is %d bytes, got %d bytes" , headerSizeIPv4 , len (data ))
157157 }
158- versionIHL := data [0 ]
158+ buf := make ([]byte , 0 , len (data ))
159+ buf = append (buf , data ... )
160+ versionIHL := buf [0 ]
159161 p .Version = versionIHL >> 4
160162 p .IHL = versionIHL & 15
161- dscpECN := data [1 ]
163+ dscpECN := buf [1 ]
162164 p .DSCP = dscpECN >> 2
163165 p .DSCPDesc = dscpdesc (p .DSCP )
164166 p .ECN = dscpECN & 3
165- p .TotalLength = binary .BigEndian .Uint16 (data [2 :4 ])
166- p .Identification = binary .BigEndian .Uint16 (data [4 :6 ])
167- flagsOffset := binary .BigEndian .Uint16 (data [6 :8 ])
167+ p .TotalLength = binary .BigEndian .Uint16 (buf [2 :4 ])
168+ p .Identification = binary .BigEndian .Uint16 (buf [4 :6 ])
169+ flagsOffset := binary .BigEndian .Uint16 (buf [6 :8 ])
168170 flags := uint8 (flagsOffset >> 13 )
169171 p .Flags = NewIPv4Flags (flags )
170172 p .FragmentOffset = flagsOffset & (1 << 13 - 1 )
171- p .TTL = data [8 ]
172- proto := IPProto (data [9 ])
173+ p .TTL = buf [8 ]
174+ proto := IPProto (buf [9 ])
173175 p .Protocol = & IPv4Proto {Val : proto , Desc : protodesc (proto )}
174- p .HeaderChecksum = binary .BigEndian .Uint16 (data [headerChecksumOffsetIPv4 :12 ])
176+ p .HeaderChecksum = binary .BigEndian .Uint16 (buf [headerChecksumOffsetIPv4 :12 ])
175177 var ok bool
176- p .SrcIP , ok = netip .AddrFromSlice (data [12 :16 ])
178+ p .SrcIP , ok = netip .AddrFromSlice (buf [12 :16 ])
177179 if ! ok {
178180 return fmt .Errorf ("malformed IPv4 address" )
179181 }
180- p .DstIP , ok = netip .AddrFromSlice (data [16 :headerSizeIPv4 ])
182+ p .DstIP , ok = netip .AddrFromSlice (buf [16 :headerSizeIPv4 ])
181183 if ! ok {
182184 return fmt .Errorf ("malformed IPv4 address" )
183185 }
184186 if p .IHL > 5 {
185187 offset := headerSizeIPv4 + ((p .IHL - 5 ) << 2 )
186- p .Options = data [headerSizeIPv4 :offset ]
187- p .Payload = data [offset :]
188+ p .Options = buf [headerSizeIPv4 :offset ]
189+ p .Payload = buf [offset :]
188190 } else {
189- p .Payload = data [headerSizeIPv4 :]
191+ p .Payload = buf [headerSizeIPv4 :]
190192 }
191193 return nil
192194}
0 commit comments