2016-12-08 10:27:41 -05:00
|
|
|
package srtp
|
|
|
|
|
|
|
|
import (
|
2017-01-12 16:47:10 -05:00
|
|
|
"context"
|
2018-11-02 13:20:02 -04:00
|
|
|
"encoding/binary"
|
2016-12-08 10:27:41 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/dice"
|
2016-12-08 10:27:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type SRTP struct {
|
|
|
|
header uint16
|
|
|
|
number uint16
|
|
|
|
}
|
|
|
|
|
2018-04-02 14:00:50 -04:00
|
|
|
func (*SRTP) Size() int32 {
|
2016-12-08 10:27:41 -05:00
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:34:04 -04:00
|
|
|
// Serialize implements PacketHeader.
|
|
|
|
func (s *SRTP) Serialize(b []byte) {
|
2017-04-13 16:17:58 -04:00
|
|
|
s.number++
|
2018-11-30 11:37:38 -05:00
|
|
|
binary.BigEndian.PutUint16(b, s.header)
|
2018-11-02 13:20:02 -04:00
|
|
|
binary.BigEndian.PutUint16(b[2:], s.number)
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|
|
|
|
|
2017-12-16 17:31:05 -05:00
|
|
|
// New returns a new SRTP instance based on the given config.
|
|
|
|
func New(ctx context.Context, config interface{}) (interface{}, error) {
|
2016-12-08 10:27:41 -05:00
|
|
|
return &SRTP{
|
|
|
|
header: 0xB5E8,
|
2017-04-27 05:54:15 -04:00
|
|
|
number: dice.RollUint16(),
|
2017-01-12 16:47:10 -05:00
|
|
|
}, nil
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-12-16 17:31:05 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), New))
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|