all: report detailed fingerprint mismatch only in debug mode

This commit is contained in:
Aleksandr Razumov
2018-08-01 23:25:52 +03:00
parent a9e45ac0ea
commit 1b2e787047
7 changed files with 48 additions and 25 deletions

View File

@@ -175,7 +175,6 @@ pkg github.com/gortc/stun, method (AttrType) Required() bool
pkg github.com/gortc/stun, method (AttrType) String() string pkg github.com/gortc/stun, method (AttrType) String() string
pkg github.com/gortc/stun, method (AttrType) Value() uint16 pkg github.com/gortc/stun, method (AttrType) Value() uint16
pkg github.com/gortc/stun, method (Attributes) Get(AttrType) (RawAttribute, bool) pkg github.com/gortc/stun, method (Attributes) Get(AttrType) (RawAttribute, bool)
pkg github.com/gortc/stun, method (CRCMismatch) Error() string
pkg github.com/gortc/stun, method (CloseErr) Error() string pkg github.com/gortc/stun, method (CloseErr) Error() string
pkg github.com/gortc/stun, method (DecodeErr) Error() string pkg github.com/gortc/stun, method (DecodeErr) Error() string
pkg github.com/gortc/stun, method (DecodeErr) IsInvalidCookie() bool pkg github.com/gortc/stun, method (DecodeErr) IsInvalidCookie() bool
@@ -231,9 +230,6 @@ pkg github.com/gortc/stun, type AttrOverflowErr struct, Max int
pkg github.com/gortc/stun, type AttrOverflowErr struct, Type AttrType pkg github.com/gortc/stun, type AttrOverflowErr struct, Type AttrType
pkg github.com/gortc/stun, type AttrType uint16 pkg github.com/gortc/stun, type AttrType uint16
pkg github.com/gortc/stun, type Attributes []RawAttribute pkg github.com/gortc/stun, type Attributes []RawAttribute
pkg github.com/gortc/stun, type CRCMismatch struct
pkg github.com/gortc/stun, type CRCMismatch struct, Actual uint32
pkg github.com/gortc/stun, type CRCMismatch struct, Expected uint32
pkg github.com/gortc/stun, type Checker interface { Check } pkg github.com/gortc/stun, type Checker interface { Check }
pkg github.com/gortc/stun, type Checker interface, Check(*Message) error pkg github.com/gortc/stun, type Checker interface, Check(*Message) error
pkg github.com/gortc/stun, type Client struct pkg github.com/gortc/stun, type Client struct
@@ -318,6 +314,7 @@ pkg github.com/gortc/stun, var ErrClientClosed error
pkg github.com/gortc/stun, var ErrClientNotInitialized error pkg github.com/gortc/stun, var ErrClientNotInitialized error
pkg github.com/gortc/stun, var ErrDecodeToNil error pkg github.com/gortc/stun, var ErrDecodeToNil error
pkg github.com/gortc/stun, var ErrFingerprintBeforeIntegrity error pkg github.com/gortc/stun, var ErrFingerprintBeforeIntegrity error
pkg github.com/gortc/stun, var ErrFingerprintMismatch error
pkg github.com/gortc/stun, var ErrIntegrityMismatch error pkg github.com/gortc/stun, var ErrIntegrityMismatch error
pkg github.com/gortc/stun, var ErrNoConnection error pkg github.com/gortc/stun, var ErrNoConnection error
pkg github.com/gortc/stun, var ErrNoDefaultReason error pkg github.com/gortc/stun, var ErrNoDefaultReason error

View File

@@ -18,3 +18,10 @@ func checkHMAC(got, expected []byte) error {
} }
return ErrIntegrityMismatch return ErrIntegrityMismatch
} }
func checkFingerprint(got, expected uint32) error {
if got == expected {
return nil
}
return ErrFingerprintMismatch
}

View File

@@ -25,3 +25,13 @@ func checkHMAC(got, expected []byte) error {
Actual: got, Actual: got,
} }
} }
func checkFingerprint(got, expected uint32) error {
if got == expected {
return nil
}
return &CRCMismatch{
Actual: got,
Expected: expected,
}
}

View File

@@ -1,7 +1,7 @@
package stun package stun
import ( import (
"fmt" "errors"
"hash/crc32" "hash/crc32"
) )
@@ -10,18 +10,8 @@ import (
// RFC 5389 Section 15.5 // RFC 5389 Section 15.5
type FingerprintAttr struct{} type FingerprintAttr struct{}
// CRCMismatch represents CRC check error. // ErrFingerprintMismatch means that computed fingerprint differs from expected.
type CRCMismatch struct { var ErrFingerprintMismatch = errors.New("fingerprint check failed")
Expected uint32
Actual uint32
}
func (m CRCMismatch) Error() string {
return fmt.Sprintf("CRC mismatch: %x (expected) != %x (actual)",
m.Expected,
m.Actual,
)
}
// Fingerprint is shorthand for FingerprintAttr. // Fingerprint is shorthand for FingerprintAttr.
// //
@@ -73,8 +63,5 @@ func (FingerprintAttr) Check(m *Message) error {
val := bin.Uint32(b) val := bin.Uint32(b)
attrStart := len(m.Raw) - (fingerprintSize + attributeHeaderSize) attrStart := len(m.Raw) - (fingerprintSize + attributeHeaderSize)
expected := FingerprintValue(m.Raw[:attrStart]) expected := FingerprintValue(m.Raw[:attrStart])
if expected != val { return checkFingerprint(val, expected)
return &CRCMismatch{Expected: expected, Actual: val}
}
return nil
} }

18
fingerprint_debug.go Normal file
View File

@@ -0,0 +1,18 @@
// +build debug
package stun
import "fmt"
// CRCMismatch represents CRC check error.
type CRCMismatch struct {
Expected uint32
Actual uint32
}
func (m CRCMismatch) Error() string {
return fmt.Sprintf("CRC mismatch: %x (expected) != %x (actual)",
m.Expected,
m.Actual,
)
}

View File

@@ -34,8 +34,8 @@ func TestFingerprint_Check(t *testing.T) {
t.Error(err) t.Error(err)
} }
m.Raw[3] = m.Raw[3] + 1 m.Raw[3] = m.Raw[3] + 1
if err, ok := Fingerprint.Check(m).(*CRCMismatch); !ok { if err := Fingerprint.Check(m); err == nil {
t.Error(err, "should be *CRCMissmatch") t.Error("should error")
} }
} }

View File

@@ -706,7 +706,11 @@ func ExampleMessage() {
} }
fmt.Println("for corrupted message:") fmt.Println("for corrupted message:")
decoded.Raw[22] = 33 decoded.Raw[22] = 33
fmt.Println("fingerprint:", Fingerprint.Check(decoded)) if Fingerprint.Check(decoded) == nil {
fmt.Println("fingerprint: ok")
} else {
fmt.Println("fingerprint: failed")
}
// Output: // Output:
// binding request l=48 attrs=3 id=AQIDBAUGBwgJAAEA buff length: 68 // binding request l=48 attrs=3 id=AQIDBAUGBwgJAAEA buff length: 68
@@ -717,7 +721,7 @@ func ExampleMessage() {
// fingerprint is correct // fingerprint is correct
// integrity ok // integrity ok
// for corrupted message: // for corrupted message:
// fingerprint: CRC mismatch: b36d2c38 (expected) != 8ef13141 (actual) // fingerprint: failed
} }
func TestAllocations(t *testing.T) { func TestAllocations(t *testing.T) {