mirror of
https://github.com/gortc/stun.git
synced 2024-05-03 14:44:55 +08:00
stun: add hotfix for padding issue
This commit is contained in:
parent
7e479c8d7f
commit
613bc1105d
12
stun.go
12
stun.go
@ -348,6 +348,7 @@ func IsMessage(b []byte) bool {
|
||||
//
|
||||
// Any error is unrecoverable, but message could be partially decoded.
|
||||
func (m *Message) ReadBytes(tBuf []byte) (int, error) {
|
||||
// TODO(ar): handle padding
|
||||
var (
|
||||
read int
|
||||
err error
|
||||
@ -384,6 +385,17 @@ func (m *Message) ReadBytes(tBuf []byte) (int, error) {
|
||||
b := buf[offset:]
|
||||
// checking that we have enough bytes to read header
|
||||
if len(b) < attributeHeaderSize {
|
||||
// HACK: hotfix for chrome padding
|
||||
allZero := true
|
||||
for _, c := range b {
|
||||
if c != 0 {
|
||||
allZero = false
|
||||
}
|
||||
}
|
||||
if allZero {
|
||||
break
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"buffer length %d is less than %d (expected header size)",
|
||||
len(b), attributeHeaderSize,
|
||||
|
31
stun_test.go
31
stun_test.go
@ -13,6 +13,9 @@ import (
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/pkg/errors"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func bUint16(v uint16) string {
|
||||
@ -517,3 +520,31 @@ func BenchmarkIsMessage(b *testing.B) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadData(tb testing.TB, name string) []byte {
|
||||
name = filepath.Join("testdata", name)
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if errClose := f.Close(); errClose != nil {
|
||||
tb.Fatal(errClose)
|
||||
}
|
||||
}()
|
||||
v, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func TestExampleChrome(t *testing.T) {
|
||||
buf := loadData(t, "ex1_chrome.stun")
|
||||
m := AcquireMessage()
|
||||
defer ReleaseMessage(m)
|
||||
_, err := m.ReadBytes(buf)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse ex1_chrome: %s", err)
|
||||
}
|
||||
}
|
||||
|
BIN
testdata/ex1_chrome.stun
vendored
Normal file
BIN
testdata/ex1_chrome.stun
vendored
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user