change the type of Connection ID to byte slice

This commit is contained in:
Marten Seemann
2018-03-16 13:29:47 +01:00
parent af8971d8c9
commit 1a035a265c
28 changed files with 258 additions and 189 deletions

View File

@@ -1,19 +1,23 @@
package protocol
import (
"bytes"
"crypto/rand"
"encoding/binary"
)
// A ConnectionID in QUIC
type ConnectionID uint64
type ConnectionID []byte
// GenerateConnectionID generates a connection ID using cryptographic random
func GenerateConnectionID() (ConnectionID, error) {
b := make([]byte, 8)
_, err := rand.Read(b)
if err != nil {
return 0, err
if _, err := rand.Read(b); err != nil {
return nil, err
}
return ConnectionID(binary.LittleEndian.Uint64(b)), nil
return ConnectionID(b), nil
}
// Equal says if two connection IDs are equal
func (c ConnectionID) Equal(other ConnectionID) bool {
return bytes.Equal(c, other)
}

View File

@@ -14,4 +14,13 @@ var _ = Describe("Connection ID generation", func() {
Expect(err).ToNot(HaveOccurred())
Expect(c1).ToNot(Equal(c2))
})
It("says if connection IDs are equal", func() {
c1 := ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
c2 := ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
Expect(c1.Equal(c1)).To(BeTrue())
Expect(c2.Equal(c2)).To(BeTrue())
Expect(c1.Equal(c2)).To(BeFalse())
Expect(c2.Equal(c1)).To(BeFalse())
})
})