Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal packets only once #304

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,22 +645,9 @@ func (a *Association) marshalPacket(p *packet) ([]byte, error) {

func (a *Association) unmarshalPacket(raw []byte) (*packet, error) {
p := &packet{}
if !a.useZeroChecksum {
if err := p.unmarshal(true, raw); err != nil {
return nil, err
}
return p, nil
}

if err := p.unmarshal(false, raw); err != nil {
if err := p.unmarshal(!a.useZeroChecksum, raw); err != nil {
return nil, err
}
if chunkMandatoryChecksum(p.chunks) {
if err := p.unmarshal(true, raw); err != nil {
return nil, err
}
}

return p, nil
}

Expand Down
29 changes: 20 additions & 9 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,30 @@
return fmt.Errorf("%w: raw only %d bytes, %d is the minimum length", ErrPacketRawTooSmall, len(raw), packetHeaderSize)
}

offset := packetHeaderSize

// Check if doing CRC32c is required.
// Without having SCTP AUTH implemented, this depends only on the type
// og the first chunk.
if offset+chunkHeaderSize <= len(raw) {
switch chunkType(raw[offset]) {
case ctInit, ctCookieEcho:
doChecksum = true
default:
}
}
theirChecksum := binary.LittleEndian.Uint32(raw[8:])
if theirChecksum != 0 || doChecksum {
ourChecksum := generatePacketChecksum(raw)
if theirChecksum != ourChecksum {
return fmt.Errorf("%w: %d ours: %d", ErrChecksumMismatch, theirChecksum, ourChecksum)
}

Check warning on line 90 in packet.go

View check run for this annotation

Codecov / codecov/patch

packet.go#L89-L90

Added lines #L89 - L90 were not covered by tests
}

p.sourcePort = binary.BigEndian.Uint16(raw[0:])
p.destinationPort = binary.BigEndian.Uint16(raw[2:])
p.verificationTag = binary.BigEndian.Uint32(raw[4:])

offset := packetHeaderSize
for {
// Exact match, no more chunks
if offset == len(raw) {
Expand Down Expand Up @@ -126,14 +145,6 @@
offset += chunkHeaderSize + c.valueLength() + chunkValuePadding
}

theirChecksum := binary.LittleEndian.Uint32(raw[8:])
if theirChecksum != 0 || doChecksum {
ourChecksum := generatePacketChecksum(raw)
if theirChecksum != ourChecksum {
return fmt.Errorf("%w: %d ours: %d", ErrChecksumMismatch, theirChecksum, ourChecksum)
}
}

return nil
}

Expand Down
Loading