| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/rand" |
| 8 |
"fmt" |
| 9 |
) |
| 10 |
|
| 11 |
type paramStateCookie struct { |
| 12 |
paramHeader |
| 13 |
cookie []byte |
| 14 |
} |
| 15 |
|
| 16 |
func newRandomStateCookie() (*paramStateCookie, error) { |
| 17 |
randCookie := make([]byte, 32) |
| 18 |
_, err := rand.Read(randCookie) |
| 19 |
// crypto/rand.Read returns n == len(b) if and only if err == nil. |
| 20 |
if err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
|
| 24 |
s := ¶mStateCookie{ |
| 25 |
cookie: randCookie, |
| 26 |
} |
| 27 |
|
| 28 |
return s, nil |
| 29 |
} |
| 30 |
|
| 31 |
func (s *paramStateCookie) marshal() ([]byte, error) { |
| 32 |
s.typ = stateCookie |
| 33 |
s.raw = s.cookie |
| 34 |
|
| 35 |
return s.paramHeader.marshal() |
| 36 |
} |
| 37 |
|
| 38 |
func (s *paramStateCookie) unmarshal(raw []byte) (param, error) { |
| 39 |
err := s.paramHeader.unmarshal(raw) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
s.cookie = s.raw |
| 44 |
|
| 45 |
return s, nil |
| 46 |
} |
| 47 |
|
| 48 |
// String makes paramStateCookie printable. |
| 49 |
func (s *paramStateCookie) String() string { |
| 50 |
return fmt.Sprintf("%s: %s", s.paramHeader, s.cookie) |
| 51 |
} |