param_state_cookie.go 859 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package sctp
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. )
  6. type paramStateCookie struct {
  7. paramHeader
  8. cookie []byte
  9. }
  10. func newRandomStateCookie() (*paramStateCookie, error) {
  11. randCookie := make([]byte, 32)
  12. _, err := rand.Read(randCookie)
  13. // crypto/rand.Read returns n == len(b) if and only if err == nil.
  14. if err != nil {
  15. return nil, err
  16. }
  17. s := &paramStateCookie{
  18. cookie: randCookie,
  19. }
  20. return s, nil
  21. }
  22. func (s *paramStateCookie) marshal() ([]byte, error) {
  23. s.typ = stateCookie
  24. s.raw = s.cookie
  25. return s.paramHeader.marshal()
  26. }
  27. func (s *paramStateCookie) unmarshal(raw []byte) (param, error) {
  28. err := s.paramHeader.unmarshal(raw)
  29. if err != nil {
  30. return nil, err
  31. }
  32. s.cookie = s.raw
  33. return s, nil
  34. }
  35. // String makes paramStateCookie printable
  36. func (s *paramStateCookie) String() string {
  37. return fmt.Sprintf("%s: %s", s.paramHeader, s.cookie)
  38. }