1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-13 15:56:18 -05:00
v2fly/transport/internet/headers/tls/dtls.go

56 lines
1.0 KiB
Go
Raw Normal View History

2018-05-25 11:16:15 -04:00
package tls
import (
"context"
"v2ray.com/core/common"
"v2ray.com/core/common/dice"
)
// DTLS writes header as DTLS. See https://tools.ietf.org/html/rfc6347
type DTLS struct {
epoch uint16
2018-05-25 13:34:32 -04:00
length uint16
2018-05-25 17:20:24 -04:00
sequence uint32
2018-05-25 11:16:15 -04:00
}
// Size implements PacketHeader.
func (*DTLS) Size() int32 {
2018-05-25 14:58:16 -04:00
return 1 + 2 + 2 + 6 + 2
2018-05-25 11:16:15 -04:00
}
2018-11-02 16:34:04 -04:00
// Serialize implements PacketHeader.
func (d *DTLS) Serialize(b []byte) {
2018-05-25 11:16:15 -04:00
b[0] = 23 // application data
b[1] = 254
b[2] = 253
b[3] = byte(d.epoch >> 8)
b[4] = byte(d.epoch)
2018-05-25 14:58:16 -04:00
b[5] = 0
b[6] = 0
b[7] = byte(d.sequence >> 24)
b[8] = byte(d.sequence >> 16)
b[9] = byte(d.sequence >> 8)
b[10] = byte(d.sequence)
2018-05-25 11:16:15 -04:00
d.sequence++
2018-05-25 14:58:16 -04:00
b[11] = byte(d.length >> 8)
b[12] = byte(d.length)
2018-05-25 13:34:32 -04:00
d.length += 17
2018-05-25 14:58:16 -04:00
if d.length > 100 {
d.length -= 50
2018-05-25 13:34:32 -04:00
}
2018-05-25 11:16:15 -04:00
}
// New creates a new UTP header for the given config.
func New(ctx context.Context, config interface{}) (interface{}, error) {
return &DTLS{
epoch: dice.RollUint16(),
sequence: 0,
2018-05-25 14:58:16 -04:00
length: 17,
2018-05-25 11:16:15 -04:00
}, nil
}
func init() {
common.Must(common.RegisterConfig((*PacketConfig)(nil), New))
}