v2ray-core/transport/internet/headers/srtp/srtp.go

39 lines
684 B
Go
Raw Normal View History

2016-12-08 15:27:41 +00:00
package srtp
import (
2017-01-12 21:47:10 +00:00
"context"
2016-12-08 15:27:41 +00:00
2017-01-12 21:47:10 +00:00
"v2ray.com/core/common"
2017-02-14 21:29:44 +00:00
"v2ray.com/core/common/dice"
2016-12-08 15:27:41 +00:00
"v2ray.com/core/common/serial"
)
type SRTP struct {
header uint16
number uint16
}
2018-04-02 18:00:50 +00:00
func (*SRTP) Size() int32 {
2016-12-08 15:27:41 +00:00
return 4
}
2017-04-21 13:36:05 +00:00
// Write implements io.Writer.
2017-04-13 20:17:58 +00:00
func (s *SRTP) Write(b []byte) (int, error) {
s.number++
serial.Uint16ToBytes(s.number, b[:0])
serial.Uint16ToBytes(s.number, b[:2])
2016-12-09 11:08:25 +00:00
return 4, nil
2016-12-08 15:27:41 +00:00
}
2017-12-16 22:31:05 +00:00
// New returns a new SRTP instance based on the given config.
func New(ctx context.Context, config interface{}) (interface{}, error) {
2016-12-08 15:27:41 +00:00
return &SRTP{
header: 0xB5E8,
2017-04-27 09:54:15 +00:00
number: dice.RollUint16(),
2017-01-12 21:47:10 +00:00
}, nil
2016-12-08 15:27:41 +00:00
}
func init() {
2017-12-16 22:31:05 +00:00
common.Must(common.RegisterConfig((*Config)(nil), New))
2016-12-08 15:27:41 +00:00
}