stun: add hotfix for padding issue

This commit is contained in:
Aleksandr Razumov 2017-01-24 17:03:40 +03:00
parent 7e479c8d7f
commit 613bc1105d
3 changed files with 43 additions and 0 deletions

12
stun.go
View File

@ -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,

View File

@ -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

Binary file not shown.