This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
control_message.go
67 lines (57 loc) · 1.58 KB
/
control_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"errors"
"io"
)
type MessageType uint8
const (
MessageClientInformation MessageType = iota
MessageClientSetIPValueAccepted
MessageClientSetIPValueRejected
MessageServerAssignedIPValue
MessageServerAssignableIPExhausted
MessageServerConfiguration
MessageClientReady
MessageServerReady
)
type ClientIPMode uint8
const (
ClientIPModeServerAssign ClientIPMode = iota
ClientIPModeClientSet
ClientIPModeOther
)
func buildControlMessage(message []byte) []byte {
buffer := make([]byte, len(message)+2)
buffer[0] = 0b11010101
var i int
messageLength := len(message)
for i = 0; i < messageLength; i++ {
buffer[1+i] = message[i]
}
buffer[1+i] = buffer[0]
return buffer
}
func retrieveControlMessage(message []byte) (int, []byte, error) {
messageLength := len(message)
if messageLength <= 2 {
return -1, nil, errors.New("invalid message length")
}
if message[0] != 0b11010101 && message[messageLength-1] != 0b11010101 {
return -1, nil, errors.New("invalid message tag")
}
return messageLength - 2, message[1 : messageLength-1], nil
}
func writeControlMessage(writer io.Writer, buffer []byte) (int, error) {
return writer.Write(buildControlMessage(buffer))
}
func readControlMessageWithProvidedBuffer(reader io.Reader, buffer []byte) (int, []byte, error) {
n, err := reader.Read(buffer)
if err != nil {
return -1, nil, err
}
return retrieveControlMessage(buffer[:n])
}
func readControlMessage(reader io.Reader, messageLength uint) (int, []byte, error) {
buffer := make([]byte, messageLength+2)
return readControlMessageWithProvidedBuffer(reader, buffer)
}