udp: Add constructor for ConnectionIDGenerator, return buffer to pool on socket timeout

This commit is contained in:
Justin Li
2015-02-22 16:58:43 -05:00
parent 7512f50731
commit 3d28f281fb
3 changed files with 17 additions and 15 deletions

View File

@@ -18,20 +18,24 @@ type ConnectionIDGenerator struct {
block cipher.Block
}
// Init generates the AES key and sets up the first initialization vector.
func (g *ConnectionIDGenerator) Init() error {
// NewConnectionIDGenerator creates a ConnectionIDGenerator and generates its
// AES key and first initialization vector.
func NewConnectionIDGenerator() (gen *ConnectionIDGenerator, err error) {
gen = &ConnectionIDGenerator{}
key := make([]byte, 16)
_, err := rand.Read(key)
_, err = rand.Read(key)
if err != nil {
return err
return
}
g.block, err = aes.NewCipher(key)
gen.block, err = aes.NewCipher(key)
if err != nil {
return err
return
}
return g.NewIV()
err = gen.NewIV()
return
}
// Generate returns the 64-bit connection ID for an IP