Skip to content

Commit

Permalink
Add details of duplicated error
Browse files Browse the repository at this point in the history
It was not clear which stream caused error in the session.
Add details of duplication error.
  • Loading branch information
at-wat committed Jul 30, 2020
1 parent f871f43 commit 4ae126e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
15 changes: 15 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ package srtp

import (
"errors"
"fmt"
)

var errDuplicated = errors.New("duplicated packet")

type errorDuplicated struct {
Proto string // srtp or srtcp
SSRC uint32
Index uint32 // sequence number or index
}

func (e *errorDuplicated) Error() string {
return fmt.Sprintf("%s ssrc=%d index=%d: %v", e.Proto, e.SSRC, e.Index, errDuplicated)
}

func (e *errorDuplicated) Unwrap() error {
return errDuplicated
}
4 changes: 2 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ func (s *session) start(localMasterKey, localMasterSalt, remoteMasterKey, remote
i, err = s.nextConn.Read(b)
if err != nil {
if err != io.EOF {
s.log.Errorf("srtp: %s", err.Error())
s.log.Error(err.Error())
}
return
}

if err = child.decrypt(b[:i]); err != nil {
s.log.Infof("%v \n", err)
s.log.Info(err.Error())
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion srtcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Context) decryptRTCP(dst, encrypted []byte) ([]byte, error) {
s := c.getSRTCPSSRCState(ssrc)
markAsValid, ok := s.replayDetector.Check(uint64(index))
if !ok {
return nil, errDuplicated
return nil, &errorDuplicated{Proto: "srtcp", SSRC: ssrc, Index: index}
}

out, err := c.cipher.decryptRTCP(out, encrypted, index, ssrc)
Expand Down
5 changes: 3 additions & 2 deletions srtcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package srtp
import (
"bytes"
"encoding/binary"
"errors"
"testing"

"github.com/pion/rtcp"
Expand Down Expand Up @@ -178,10 +179,10 @@ func TestRTCPReplayDetectorSeparation(t *testing.T) {
}
assert.Equal(decryptResult2, rtcpTestDecrypted2, "RTCP failed to decrypt")

if _, err = decryptContext.DecryptRTCP(nil, rtcpPacket1, nil); err != errDuplicated {
if _, err = decryptContext.DecryptRTCP(nil, rtcpPacket1, nil); !errors.Is(err, errDuplicated) {
t.Errorf("Was able to decrypt duplicated RTCP packet")
}
if _, err = decryptContext.DecryptRTCP(nil, rtcpPacket2, nil); err != errDuplicated {
if _, err = decryptContext.DecryptRTCP(nil, rtcpPacket2, nil); !errors.Is(err, errDuplicated) {
t.Errorf("Was able to decrypt duplicated RTCP packet")
}
}
Expand Down
4 changes: 3 additions & 1 deletion srtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ func (c *Context) decryptRTP(dst, ciphertext []byte, header *rtp.Header) ([]byte

markAsValid, ok := s.replayDetector.Check(uint64(header.SequenceNumber))
if !ok {
return nil, errDuplicated
return nil, &errorDuplicated{
Proto: "srtp", SSRC: header.SSRC, Index: uint32(header.SequenceNumber),
}
}

dst = growBufferSize(dst, len(ciphertext)-c.cipher.authTagLen())
Expand Down
3 changes: 2 additions & 1 deletion srtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package srtp

import (
"bytes"
"errors"
"testing"

"github.com/pion/rtp"
Expand Down Expand Up @@ -343,7 +344,7 @@ func TestRTPReplayProtection(t *testing.T) {
assert.Equalf(actualDecrypted, decryptedRaw, "RTP packet with SeqNum invalid decryption: %d", testCase.sequenceNumber)

_, errReplay := decryptContext.DecryptRTP(decryptInput, decryptInput, decryptHeader)
if errReplay != errDuplicated {
if !errors.Is(errReplay, errDuplicated) {
t.Errorf("Replayed packet must be errored with %v, got %v", errDuplicated, errReplay)
}
}
Expand Down

0 comments on commit 4ae126e

Please sign in to comment.